mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-18 13:43:49 +01:00
Added utilities for generic reviver, toJSON, and fromJSON. Eventually will be used for loading and saving objects
This commit is contained in:
parent
2d41a63707
commit
b821fc2ea9
@ -28,6 +28,9 @@
|
|||||||
|
|
||||||
<script src="src/engine.js"></script>
|
<script src="src/engine.js"></script>
|
||||||
|
|
||||||
|
<!-- Utils -->
|
||||||
|
<script src="utils/jsonfn/jsonfn.js"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="mainmenu-container">
|
<div id="mainmenu-container">
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
//this flag from outside. If the evaluate() function sees that flag it rejects the current
|
//this flag from outside. If the evaluate() function sees that flag it rejects the current
|
||||||
// Promise. We can catch that rejection and stop the script.
|
// Promise. We can catch that rejection and stop the script.
|
||||||
|
|
||||||
|
//TODO Tested For and while and generic call statements. Have not tested if statements
|
||||||
|
|
||||||
/* Actual Worker Code */
|
/* Actual Worker Code */
|
||||||
function WorkerScript() {
|
function WorkerScript() {
|
||||||
this.name = "";
|
this.name = "";
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
function TestObj() {
|
||||||
|
this.value = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
TestObj.prototype.setValue = function(val) {
|
||||||
|
this.value = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
var testObj = new TestObj();
|
||||||
|
|
||||||
//Terminal
|
//Terminal
|
||||||
var post = function(input) {
|
var post = function(input) {
|
||||||
$("#terminal-input").before('<tr class="posted"><td style="color: #66ff33;">' + input.replace( / /g, " " ) + '</td></tr>');
|
$("#terminal-input").before('<tr class="posted"><td style="color: #66ff33;">' + input.replace( / /g, " " ) + '</td></tr>');
|
||||||
@ -365,17 +375,30 @@ var Terminal = {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "test":
|
case "test":
|
||||||
//TODO
|
post(testObj.value.toString());
|
||||||
//TESTED: print, for loops, while loops, prog,
|
testObj.setValue(testObj.value + 1);
|
||||||
// basic ops, var, assign all seem fine
|
break;
|
||||||
//UNTESTED: if, elif, else
|
case "testSave":
|
||||||
|
var testSave = JSONfn.stringify(testObj);
|
||||||
var code = "i = 0; while (i <= 20) {print(i); i = i+2; hack(); sleep();}";
|
window.localStorage.setItem("netburnerTest", testSave);
|
||||||
var ast = Parser(Tokenizer(InputStream(code)));
|
console.log("Netburner TestSave saved");
|
||||||
console.log("Printing AST below")
|
break;
|
||||||
console.log(ast);
|
case "testLoad":
|
||||||
var globalEnv = new Environment();
|
if (!window.localStorage.getItem("netburnerTest")) {
|
||||||
evaluate(ast, globalEnv);
|
console.log("No TestSave file to load");
|
||||||
|
} else {
|
||||||
|
var testSave = window.localStorage.getItem("netburnerTest");
|
||||||
|
testObj = JSONfn.parse(testSave);
|
||||||
|
console.log("TestSave loaded");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "testDelete":
|
||||||
|
if (!window.localStorage.getItem("netburnetTest")) {
|
||||||
|
console.log("No TestSave file to delete");
|
||||||
|
} else {
|
||||||
|
window.localStorage.removeItem("netburnerTest");
|
||||||
|
console.log("TestSave deleted");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
post("Command not found");
|
post("Command not found");
|
||||||
|
65
utils/JSONReviver.js
Normal file
65
utils/JSONReviver.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */
|
||||||
|
|
||||||
|
// A generic "smart reviver" function.
|
||||||
|
// Looks for object values with a `ctor` property and
|
||||||
|
// a `data` property. If it finds them, and finds a matching
|
||||||
|
// constructor that has a `fromJSON` property on it, it hands
|
||||||
|
// off to that `fromJSON` fuunction, passing in the value.
|
||||||
|
function Reviver(key, value) {
|
||||||
|
var ctor;
|
||||||
|
|
||||||
|
if (typeof value === "object" &&
|
||||||
|
typeof value.ctor === "string" &&
|
||||||
|
typeof value.data !== "undefined") {
|
||||||
|
ctor = Reviver.constructors[value.ctor] || window[value.ctor];
|
||||||
|
if (typeof ctor === "function" &&
|
||||||
|
typeof ctor.fromJSON === "function") {
|
||||||
|
return ctor.fromJSON(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
Reviver.constructors = {}; // A list of constructors the smart reviver should know about
|
||||||
|
|
||||||
|
// A generic "toJSON" function that creates the data expected
|
||||||
|
// by Reviver.
|
||||||
|
// `ctorName` The name of the constructor to use to revive it
|
||||||
|
// `obj` The object being serialized
|
||||||
|
// `keys` (Optional) Array of the properties to serialize,
|
||||||
|
// if not given then all of the objects "own" properties
|
||||||
|
// that don't have function values will be serialized.
|
||||||
|
// (Note: If you list a property in `keys`, it will be serialized
|
||||||
|
// regardless of whether it's an "own" property.)
|
||||||
|
// Returns: The structure (which will then be turned into a string
|
||||||
|
// as part of the JSON.stringify algorithm)
|
||||||
|
function Generic_toJSON(ctorName, obj, keys) {
|
||||||
|
var data, index, key;
|
||||||
|
|
||||||
|
if (!keys) {
|
||||||
|
keys = Object.keys(obj); // Only "own" properties are included
|
||||||
|
}
|
||||||
|
|
||||||
|
data = {};
|
||||||
|
for (index = 0; index < keys.length; ++index) {
|
||||||
|
key = keys[index];
|
||||||
|
data[key] = obj[key];
|
||||||
|
}
|
||||||
|
return {ctor: ctorName, data: data};
|
||||||
|
}
|
||||||
|
|
||||||
|
// A generic "fromJSON" function for use with Reviver: Just calls the
|
||||||
|
// constructor function with no arguments, then applies all of the
|
||||||
|
// key/value pairs from the raw data to the instance. Only useful for
|
||||||
|
// constructors that can be reasonably called without arguments!
|
||||||
|
// `ctor` The constructor to call
|
||||||
|
// `data` The data to apply
|
||||||
|
// Returns: The object
|
||||||
|
function Generic_fromJSON(ctor, data) {
|
||||||
|
var obj, name;
|
||||||
|
|
||||||
|
obj = new ctor();
|
||||||
|
for (name in data) {
|
||||||
|
obj[name] = data[name];
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user