2017-08-30 19:44:29 +02:00
|
|
|
import {addActiveScriptsItem,
|
|
|
|
deleteActiveScriptsItem,
|
2018-06-26 18:34:11 +02:00
|
|
|
updateActiveScriptsItems} from "./ActiveScriptsUI";
|
|
|
|
import {CONSTANTS} from "./Constants";
|
|
|
|
import {Engine} from "./engine";
|
2018-07-09 01:53:24 +02:00
|
|
|
import {Interpreter} from "./JSInterpreter";
|
2018-06-26 18:34:11 +02:00
|
|
|
import {Environment} from "./NetscriptEnvironment";
|
2017-11-01 23:56:30 +01:00
|
|
|
import {evaluate, isScriptErrorMessage,
|
2018-05-06 00:13:35 +02:00
|
|
|
makeRuntimeRejectMsg,
|
2018-06-26 18:34:11 +02:00
|
|
|
killNetscriptDelay} from "./NetscriptEvaluator";
|
2018-07-09 01:53:24 +02:00
|
|
|
import {NetscriptFunctions} from "./NetscriptFunctions";
|
2018-06-26 18:34:11 +02:00
|
|
|
import {executeJSScript} from "./NetscriptJSEvaluator";
|
|
|
|
import {NetscriptPort} from "./NetscriptPort";
|
|
|
|
import {AllServers} from "./Server";
|
|
|
|
import {Settings} from "./Settings";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
import {generate} from 'escodegen';
|
|
|
|
|
|
|
|
import {parse, Node} from "../utils/acorn";
|
2018-06-26 18:34:11 +02:00
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox";
|
2018-07-05 22:50:51 +02:00
|
|
|
import {compareArrays} from "../utils/helpers/compareArrays";
|
2018-07-05 20:12:20 +02:00
|
|
|
import {arrayToString} from "../utils/helpers/arrayToString";
|
2018-07-05 02:22:29 +02:00
|
|
|
import {roundToTwo} from "../utils/helpers/roundToTwo";
|
2018-07-09 01:53:24 +02:00
|
|
|
import {isString} from "../utils/StringHelperFunctions";
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
const walk = require("acorn/dist/walk");
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
function WorkerScript(runningScriptObj) {
|
|
|
|
this.name = runningScriptObj.filename;
|
2016-11-30 00:38:50 +01:00
|
|
|
this.running = false;
|
2016-12-02 22:57:20 +01:00
|
|
|
this.serverIp = null;
|
2017-06-17 04:53:57 +02:00
|
|
|
this.code = runningScriptObj.scriptRef.code;
|
2017-06-28 11:47:42 +02:00
|
|
|
this.env = new Environment(this);
|
2018-02-24 23:55:06 +01:00
|
|
|
this.env.set("args", runningScriptObj.args.slice());
|
2016-12-02 22:57:20 +01:00
|
|
|
this.output = "";
|
2016-12-19 19:20:19 +01:00
|
|
|
this.ramUsage = 0;
|
2017-06-17 04:53:57 +02:00
|
|
|
this.scriptRef = runningScriptObj;
|
2017-05-01 07:39:48 +02:00
|
|
|
this.errorMessage = "";
|
2018-02-24 23:55:06 +01:00
|
|
|
this.args = runningScriptObj.args.slice();
|
2017-11-01 23:56:30 +01:00
|
|
|
this.delay = null;
|
2017-10-12 04:00:22 +02:00
|
|
|
this.fnWorker = null; //Workerscript for a function call
|
2018-01-27 07:52:39 +01:00
|
|
|
this.checkingRam = false;
|
2018-06-06 18:02:21 +02:00
|
|
|
this.loadedFns = {}; //Stores names of fns that are "loaded" by this script, thus using RAM. Used for static RAM evaluation
|
2018-02-24 23:55:06 +01:00
|
|
|
this.disableLogs = {}; //Stores names of fns that should have logs disabled
|
2018-06-06 18:02:21 +02:00
|
|
|
|
|
|
|
//Properties used for dynamic RAM evaluation
|
2018-07-10 07:11:18 +02:00
|
|
|
this.dynamicRamUsage = CONSTANTS.ScriptBaseRamCost;
|
2018-06-06 18:02:21 +02:00
|
|
|
this.dynamicLoadedFns = {};
|
2016-12-19 19:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the server on which the workerScript is running
|
|
|
|
WorkerScript.prototype.getServer = function() {
|
|
|
|
return AllServers[this.serverIp];
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:10:12 +02:00
|
|
|
//Returns the Script object for the underlying script
|
|
|
|
WorkerScript.prototype.getScript = function() {
|
|
|
|
let server = this.getServer();
|
2018-09-14 23:03:31 +02:00
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
2018-05-17 19:10:12 +02:00
|
|
|
if (server.scripts[i].filename === this.name) {
|
|
|
|
return server.scripts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("ERROR: Failed to find underlying Script object in WorkerScript.getScript(). This probably means somethings wrong");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-14 23:03:31 +02:00
|
|
|
//Returns the Script object for the specified script
|
|
|
|
WorkerScript.prototype.getScriptOnServer = function(fn, server) {
|
|
|
|
if (server == null) {
|
|
|
|
server = this.getServer();
|
|
|
|
}
|
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
|
|
|
if (server.scripts[i].filename === fn) {
|
|
|
|
return server.scripts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-08 17:51:48 +02:00
|
|
|
WorkerScript.prototype.shouldLog = function(fn) {
|
|
|
|
return (this.disableLogs.ALL == null && this.disableLogs[fn] == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerScript.prototype.log = function(txt) {
|
|
|
|
this.scriptRef.log(txt);
|
|
|
|
}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
//Array containing all scripts that are running across all servers, to easily run them all
|
2017-08-30 19:44:29 +02:00
|
|
|
let workerScripts = [];
|
2016-11-28 23:02:06 +01:00
|
|
|
|
2018-03-03 22:05:33 +01:00
|
|
|
var NetscriptPorts = [];
|
|
|
|
for (var i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
|
|
|
|
NetscriptPorts.push(new NetscriptPort());
|
2017-07-22 00:54:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function prestigeWorkerScripts() {
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
deleteActiveScriptsItem(workerScripts[i]);
|
|
|
|
workerScripts[i].env.stopFlag = true;
|
|
|
|
}
|
2018-05-12 03:54:59 +02:00
|
|
|
updateActiveScriptsItems(5000); //Force UI to update
|
2017-08-30 19:44:29 +02:00
|
|
|
workerScripts.length = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
// JS script promises need a little massaging to have the same guarantees as netscript
|
|
|
|
// promises. This does said massaging and kicks the script off. It returns a promise
|
|
|
|
// that resolves or rejects when the corresponding worker script is done.
|
2018-07-09 01:53:24 +02:00
|
|
|
function startNetscript2Script(workerScript) {
|
2018-05-06 00:13:35 +02:00
|
|
|
workerScript.running = true;
|
|
|
|
|
2018-05-06 02:39:34 +02:00
|
|
|
// The name of the currently running netscript function, to prevent concurrent
|
|
|
|
// calls to hack, grow, etc.
|
|
|
|
let runningFn = null;
|
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
// We need to go through the environment and wrap each function in such a way that it
|
|
|
|
// can be called at most once at a time. This will prevent situations where multiple
|
|
|
|
// hack promises are outstanding, for example.
|
|
|
|
function wrap(propName, f) {
|
|
|
|
// This function unfortunately cannot be an async function, because we don't
|
|
|
|
// know if the original one was, and there's no way to tell.
|
|
|
|
return function (...args) {
|
2018-05-07 04:16:28 +02:00
|
|
|
// Wrap every netscript function with a check for the stop flag.
|
|
|
|
// This prevents cases where we never stop because we are only calling
|
|
|
|
// netscript functions that don't check this.
|
|
|
|
// This is not a problem for legacy Netscript because it also checks the
|
|
|
|
// stop flag in the evaluator.
|
2018-05-09 03:40:07 +02:00
|
|
|
if (workerScript.env.stopFlag) {throw workerScript;}
|
|
|
|
|
|
|
|
if (propName === "sleep") return f(...args); // OK for multiple simultaneous calls to sleep.
|
2018-05-07 04:16:28 +02:00
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
const msg = "Concurrent calls to Netscript functions not allowed! " +
|
|
|
|
"Did you forget to await hack(), grow(), or some other " +
|
|
|
|
"promise-returning function? (Currently running: %s tried to run: %s)"
|
2018-05-06 02:39:34 +02:00
|
|
|
if (runningFn) {
|
|
|
|
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, runningFn, propName), null)
|
2018-05-06 00:13:35 +02:00
|
|
|
throw workerScript;
|
|
|
|
}
|
2018-05-06 02:39:34 +02:00
|
|
|
runningFn = propName;
|
2018-10-23 20:55:42 +02:00
|
|
|
|
|
|
|
// If the function throws an error, clear the runningFn flag first, and then re-throw it
|
|
|
|
// This allows people to properly catch errors thrown by NS functions without getting
|
|
|
|
// the concurrent call error above
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = f(...args);
|
|
|
|
} catch(e) {
|
|
|
|
runningFn = null;
|
|
|
|
throw(e);
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
if (result && result.finally !== undefined) {
|
|
|
|
return result.finally(function () {
|
2018-05-06 02:39:34 +02:00
|
|
|
runningFn = null;
|
2018-05-06 00:13:35 +02:00
|
|
|
});
|
|
|
|
} else {
|
2018-05-06 02:39:34 +02:00
|
|
|
runningFn = null;
|
2018-05-06 00:13:35 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-05-09 03:40:07 +02:00
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
for (let prop in workerScript.env.vars) {
|
|
|
|
if (typeof workerScript.env.vars[prop] !== "function") continue;
|
|
|
|
workerScript.env.vars[prop] = wrap(prop, workerScript.env.vars[prop]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: the environment that we pass to the JS script only needs to contain the functions visible
|
|
|
|
// to that script, which env.vars does at this point.
|
2018-05-17 19:10:12 +02:00
|
|
|
return executeJSScript(workerScript.getServer().scripts,
|
2018-05-12 02:45:40 +02:00
|
|
|
workerScript).then(function (mainReturnValue) {
|
2018-05-06 00:13:35 +02:00
|
|
|
if (mainReturnValue === undefined) return workerScript;
|
|
|
|
return [mainReturnValue, workerScript];
|
|
|
|
}).catch(e => {
|
|
|
|
if (e instanceof Error) {
|
2018-05-06 02:39:34 +02:00
|
|
|
workerScript.errorMessage = makeRuntimeRejectMsg(
|
|
|
|
workerScript, e.message + (e.stack && ("\nstack:\n" + e.stack.toString()) || ""));
|
2018-05-06 00:13:35 +02:00
|
|
|
throw workerScript;
|
|
|
|
} else if (isScriptErrorMessage(e)) {
|
|
|
|
workerScript.errorMessage = e;
|
|
|
|
throw workerScript;
|
|
|
|
}
|
|
|
|
throw e; // Don't know what to do with it, let's rethrow.
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-09 01:53:24 +02:00
|
|
|
function startNetscript1Script(workerScript) {
|
|
|
|
var code = workerScript.code;
|
|
|
|
workerScript.running = true;
|
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
//Process imports
|
2018-07-25 21:53:54 +02:00
|
|
|
var codeWithImports, codeLineOffset;
|
2018-07-20 16:28:03 +02:00
|
|
|
try {
|
2018-07-25 21:53:54 +02:00
|
|
|
let importProcessingRes = processNetscript1Imports(code, workerScript);
|
|
|
|
codeWithImports = importProcessingRes.code;
|
|
|
|
codeLineOffset = importProcessingRes.lineOffset;
|
2018-07-20 16:28:03 +02:00
|
|
|
} catch(e) {
|
|
|
|
dialogBoxCreate("Error processing Imports in " + workerScript.name + ":<br>" + e);
|
|
|
|
workerScript.env.stopFlag = true;
|
|
|
|
workerScript.running = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 01:53:24 +02:00
|
|
|
var interpreterInitialization = function(int, scope) {
|
|
|
|
//Add the Netscript environment
|
|
|
|
var ns = NetscriptFunctions(workerScript);
|
2018-07-11 06:41:26 +02:00
|
|
|
for (let name in ns) {
|
2018-07-09 01:53:24 +02:00
|
|
|
let entry = ns[name];
|
|
|
|
if (typeof entry === "function") {
|
|
|
|
//Async functions need to be wrapped. See JS-Interpreter documentation
|
|
|
|
if (name === "hack" || name === "grow" || name === "weaken" || name === "sleep" ||
|
|
|
|
name === "prompt" || name === "run" || name === "exec") {
|
|
|
|
let tempWrapper = function() {
|
|
|
|
let fnArgs = [];
|
|
|
|
for (let i = 0; i < arguments.length-1; ++i) {
|
|
|
|
fnArgs.push(arguments[i]);
|
|
|
|
}
|
|
|
|
let cb = arguments[arguments.length-1];
|
|
|
|
let fnPromise = entry.apply(null, fnArgs);
|
|
|
|
fnPromise.then(function(res) {
|
|
|
|
cb(res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
int.setProperty(scope, name, int.createAsyncFunction(tempWrapper));
|
2018-08-06 17:25:22 +02:00
|
|
|
} else if (name === "sprintf" || name === "vsprintf" || name === "scp" ||
|
|
|
|
name == "write" || name === "read" || name === "tryWrite") {
|
2018-07-25 21:53:54 +02:00
|
|
|
let tempWrapper = function() {
|
|
|
|
let fnArgs = [];
|
|
|
|
|
|
|
|
//All of the Object/array elements are in JSInterpreter format, so
|
|
|
|
//we have to convert them back to native format to pass them to these fns
|
|
|
|
for (let i = 0; i < arguments.length; ++i) {
|
|
|
|
if (typeof arguments[i] === 'object' || arguments[i].constructor === Array) {
|
|
|
|
fnArgs.push(int.pseudoToNative(arguments[i]));
|
|
|
|
} else {
|
|
|
|
fnArgs.push(arguments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.apply(null, fnArgs);
|
|
|
|
}
|
|
|
|
int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
|
2018-07-09 01:53:24 +02:00
|
|
|
} else {
|
2018-07-11 06:41:26 +02:00
|
|
|
let tempWrapper = function() {
|
|
|
|
let res = entry.apply(null, arguments);
|
|
|
|
|
|
|
|
if (res == null) {
|
|
|
|
return res;
|
|
|
|
} else if (res.constructor === Array || (res === Object(res))) {
|
|
|
|
//Objects and Arrays must be converted to the interpreter's format
|
|
|
|
return int.nativeToPseudo(res);
|
|
|
|
} else {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-11 06:41:26 +02:00
|
|
|
//bladeburner, or anything else
|
2018-07-09 01:53:24 +02:00
|
|
|
int.setProperty(scope, name, int.nativeToPseudo(entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add the arguments
|
|
|
|
int.setProperty(scope, "args", int.nativeToPseudo(workerScript.args));
|
|
|
|
}
|
2018-07-11 06:41:26 +02:00
|
|
|
|
|
|
|
var interpreter;
|
|
|
|
try {
|
2018-07-25 21:53:54 +02:00
|
|
|
interpreter = new Interpreter(codeWithImports, interpreterInitialization, codeLineOffset);
|
2018-07-11 06:41:26 +02:00
|
|
|
} catch(e) {
|
|
|
|
dialogBoxCreate("Syntax ERROR in " + workerScript.name + ":<br>" + e);
|
|
|
|
workerScript.env.stopFlag = true;
|
|
|
|
workerScript.running = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 01:53:24 +02:00
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
function runInterpreter() {
|
|
|
|
try {
|
|
|
|
if (workerScript.env.stopFlag) {return reject(workerScript);}
|
|
|
|
|
|
|
|
if (interpreter.step()) {
|
|
|
|
window.setTimeout(runInterpreter, Settings.CodeInstructionRunTime);
|
|
|
|
} else {
|
|
|
|
resolve(workerScript);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2018-07-15 20:03:33 +02:00
|
|
|
e = e.toString();
|
|
|
|
if (!isScriptErrorMessage(e)) {
|
|
|
|
e = makeRuntimeRejectMsg(workerScript, e);
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
2018-07-15 20:03:33 +02:00
|
|
|
workerScript.errorMessage = e;
|
|
|
|
return reject(workerScript);
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 06:41:26 +02:00
|
|
|
try {
|
|
|
|
runInterpreter();
|
|
|
|
} catch(e) {
|
|
|
|
if (isString(e)) {
|
|
|
|
workerScript.errorMessage = e;
|
|
|
|
return reject(workerScript);
|
|
|
|
} else if (e instanceof WorkerScript) {
|
|
|
|
return reject(e);
|
|
|
|
} else {
|
|
|
|
return reject(workerScript);
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 01:53:24 +02:00
|
|
|
});
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Since the JS Interpreter used for Netscript 1.0 only supports ES5, the keyword
|
|
|
|
'import' throws an error. However, since we want to support import funtionality
|
|
|
|
we'll implement it ourselves by parsing the Nodes in the AST out.
|
|
|
|
|
|
|
|
@param code - The script's code
|
2018-07-25 21:53:54 +02:00
|
|
|
@returns {Object} {
|
|
|
|
code: Newly-generated code with imported functions
|
|
|
|
lineOffset: Net number of lines of code added/removed due to imported functions
|
|
|
|
Should typically be positive
|
|
|
|
}
|
2018-07-20 16:28:03 +02:00
|
|
|
*/
|
|
|
|
function processNetscript1Imports(code, workerScript) {
|
|
|
|
//allowReserved prevents 'import' from throwing error in ES5
|
|
|
|
var ast = parse(code, {ecmaVersion:6, allowReserved:true, sourceType:"module"});
|
|
|
|
|
|
|
|
var server = workerScript.getServer();
|
|
|
|
if (server == null) {
|
|
|
|
throw new Error("Failed to find underlying Server object for script");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getScript(scriptName) {
|
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
|
|
|
if (server.scripts[i].filename === scriptName) {
|
|
|
|
return server.scripts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var generatedCode = ""; //Generated Javascript Code
|
2018-07-25 21:53:54 +02:00
|
|
|
var hasImports = false;
|
2018-07-20 16:28:03 +02:00
|
|
|
|
|
|
|
//Walk over the tree and process ImportDeclaration nodes
|
|
|
|
walk.simple(ast, {
|
|
|
|
ImportDeclaration: (node) => {
|
2018-07-25 21:53:54 +02:00
|
|
|
hasImports = true;
|
2018-07-20 16:28:03 +02:00
|
|
|
let scriptName = node.source.value;
|
|
|
|
let script = getScript(scriptName);
|
|
|
|
if (script == null) {
|
|
|
|
throw new Error("'Import' failed due to invalid script: " + scriptName);
|
|
|
|
}
|
|
|
|
let scriptAst = parse(script.code, {ecmaVersion:5, allowReserved:true, sourceType:"module"});
|
|
|
|
|
|
|
|
if (node.specifiers.length === 1 && node.specifiers[0].type === "ImportNamespaceSpecifier") {
|
|
|
|
//import * as namespace from script
|
|
|
|
let namespace = node.specifiers[0].local.name;
|
|
|
|
let fnNames = []; //Names only
|
|
|
|
let fnDeclarations = []; //FunctionDeclaration Node objects
|
|
|
|
walk.simple(scriptAst, {
|
|
|
|
FunctionDeclaration: (node) => {
|
|
|
|
fnNames.push(node.id.name);
|
|
|
|
fnDeclarations.push(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//Now we have to generate the code that would create the namespace
|
|
|
|
generatedCode =
|
|
|
|
"var " + namespace + ";\n" +
|
|
|
|
"(function (namespace) {\n";
|
|
|
|
|
|
|
|
//Add the function declarations
|
|
|
|
fnDeclarations.forEach((fn) => {
|
|
|
|
generatedCode += generate(fn);
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add functions to namespace
|
|
|
|
fnNames.forEach((fnName) => {
|
|
|
|
generatedCode += ("namespace." + fnName + " = " + fnName);
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
//Finish
|
|
|
|
generatedCode += (
|
2018-07-25 21:53:54 +02:00
|
|
|
"})(" + namespace + " || " + "(" + namespace + " = {}));\n"
|
2018-07-20 16:28:03 +02:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
//import {...} from script
|
|
|
|
|
|
|
|
//Get array of all fns to import
|
|
|
|
let fnsToImport = [];
|
|
|
|
node.specifiers.forEach((e) => {
|
|
|
|
fnsToImport.push(e.local.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
//Walk through script and get FunctionDeclaration code for all specified fns
|
|
|
|
let fnDeclarations = [];
|
|
|
|
walk.simple(scriptAst, {
|
|
|
|
FunctionDeclaration: (node) => {
|
|
|
|
if (fnsToImport.includes(node.id.name)) {
|
|
|
|
fnDeclarations.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//Convert FunctionDeclarations into code
|
|
|
|
fnDeclarations.forEach((fn) => {
|
|
|
|
generatedCode += generate(fn);
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-25 21:53:54 +02:00
|
|
|
//If there are no imports, just return the original code
|
|
|
|
if (!hasImports) {return {code:code, lineOffset:0};}
|
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
//Remove ImportDeclarations from AST. These ImportDeclarations must be in top-level
|
2018-07-25 21:53:54 +02:00
|
|
|
var linesRemoved = 0;
|
2018-07-20 16:28:03 +02:00
|
|
|
if (ast.type !== "Program" || ast.body == null) {
|
|
|
|
throw new Error("Code could not be properly parsed");
|
|
|
|
}
|
|
|
|
for (let i = ast.body.length-1; i >= 0; --i) {
|
|
|
|
if (ast.body[i].type === "ImportDeclaration") {
|
|
|
|
ast.body.splice(i, 1);
|
2018-07-25 21:53:54 +02:00
|
|
|
++linesRemoved;
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 01:53:24 +02:00
|
|
|
|
2018-07-25 21:53:54 +02:00
|
|
|
//Calculated line offset
|
|
|
|
var lineOffset = (generatedCode.match(/\n/g) || []).length - linesRemoved;
|
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
//Convert the AST back into code
|
|
|
|
code = generate(ast);
|
2018-07-09 01:53:24 +02:00
|
|
|
|
2018-07-20 16:28:03 +02:00
|
|
|
//Add the imported code and re-generate in ES5 (JS Interpreter for NS1 only supports ES5);
|
|
|
|
code = generatedCode + code;
|
2018-07-25 21:53:54 +02:00
|
|
|
var res = {
|
|
|
|
code: code,
|
|
|
|
lineOffset: lineOffset
|
|
|
|
}
|
|
|
|
return res;
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
//Loop through workerScripts and run every script that is not currently running
|
|
|
|
function runScriptsLoop() {
|
2018-05-17 21:50:27 +02:00
|
|
|
var scriptDeleted = false;
|
|
|
|
|
|
|
|
//Delete any scripts that finished or have been killed. Loop backwards bc removing items screws up indexing
|
2017-09-08 04:56:27 +02:00
|
|
|
for (var i = workerScripts.length - 1; i >= 0; i--) {
|
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
|
2018-05-17 21:50:27 +02:00
|
|
|
scriptDeleted = true;
|
2017-09-08 04:56:27 +02:00
|
|
|
//Delete script from the runningScripts array on its host serverIp
|
|
|
|
var ip = workerScripts[i].serverIp;
|
|
|
|
var name = workerScripts[i].name;
|
|
|
|
|
2018-07-06 05:15:01 +02:00
|
|
|
//recalculate ram used
|
|
|
|
AllServers[ip].ramUsed = 0;
|
|
|
|
for(let j = 0; j < workerScripts.length; j++) {
|
|
|
|
if(workerScripts[j].serverIp !== ip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if(j === i) { // not this one
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
AllServers[ip].ramUsed += workerScripts[j].ramUsage;
|
|
|
|
}
|
2018-01-09 21:48:06 +01:00
|
|
|
|
|
|
|
//Delete script from Active Scripts
|
|
|
|
deleteActiveScriptsItem(workerScripts[i]);
|
|
|
|
|
2017-09-08 04:56:27 +02:00
|
|
|
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
|
|
|
|
if (AllServers[ip].runningScripts[j].filename == name &&
|
|
|
|
compareArrays(AllServers[ip].runningScripts[j].args, workerScripts[i].args)) {
|
|
|
|
AllServers[ip].runningScripts.splice(j, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete script from workerScripts
|
|
|
|
workerScripts.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 21:50:27 +02:00
|
|
|
if (scriptDeleted) {updateActiveScriptsItems();} //Force Update
|
|
|
|
|
2017-09-08 04:56:27 +02:00
|
|
|
|
2016-12-05 23:31:46 +01:00
|
|
|
//Run any scripts that haven't been started
|
2016-11-28 23:02:06 +01:00
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
2016-12-05 23:31:46 +01:00
|
|
|
//If it isn't running, start the script
|
2016-12-06 17:59:20 +01:00
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
2018-05-06 00:13:35 +02:00
|
|
|
let p = null; // p is the script's result promise.
|
2018-06-26 19:38:55 +02:00
|
|
|
if (workerScripts[i].name.endsWith(".js") || workerScripts[i].name.endsWith(".ns")) {
|
2018-07-09 01:53:24 +02:00
|
|
|
p = startNetscript2Script(workerScripts[i]);
|
2018-05-06 00:13:35 +02:00
|
|
|
} else {
|
2018-07-09 01:53:24 +02:00
|
|
|
p = startNetscript1Script(workerScripts[i]);
|
2018-07-11 06:41:26 +02:00
|
|
|
if (!(p instanceof Promise)) {continue;}
|
2018-07-09 01:53:24 +02:00
|
|
|
/*
|
2018-05-06 00:13:35 +02:00
|
|
|
try {
|
|
|
|
var ast = parse(workerScripts[i].code, {sourceType:"module"});
|
2018-06-25 02:13:50 +02:00
|
|
|
//console.log(ast);
|
2018-05-06 00:13:35 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log("Error parsing script: " + workerScripts[i].name);
|
|
|
|
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":<br>" + e);
|
|
|
|
workerScripts[i].env.stopFlag = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
workerScripts[i].running = true;
|
|
|
|
p = evaluate(ast, workerScripts[i]);
|
2018-07-09 01:53:24 +02:00
|
|
|
*/
|
2018-05-06 00:13:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 23:31:46 +01:00
|
|
|
//Once the code finishes (either resolved or rejected, doesnt matter), set its
|
|
|
|
//running status to false
|
2016-12-06 17:59:20 +01:00
|
|
|
p.then(function(w) {
|
2016-12-14 21:29:40 +01:00
|
|
|
console.log("Stopping script " + w.name + " because it finished running naturally");
|
2016-12-06 17:59:20 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
2017-06-17 04:53:57 +02:00
|
|
|
w.scriptRef.log("Script finished running");
|
2017-11-01 23:56:30 +01:00
|
|
|
}).catch(function(w) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (w instanceof Error) {
|
2017-06-17 04:53:57 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
2017-08-30 19:44:29 +02:00
|
|
|
console.log("ERROR: Evaluating workerscript returns an Error. THIS SHOULDN'T HAPPEN: " + w.toString());
|
2017-06-17 04:53:57 +02:00
|
|
|
return;
|
2017-10-10 06:56:48 +02:00
|
|
|
} else if (w.constructor === Array && w.length === 2 && w[0] === "RETURNSTATEMENT") {
|
|
|
|
//Script ends with a return statement
|
|
|
|
console.log("Script returning with value: " + w[1]);
|
|
|
|
//TODO maybe do something with this in the future
|
|
|
|
return;
|
2017-05-01 07:39:48 +02:00
|
|
|
} else if (w instanceof WorkerScript) {
|
|
|
|
if (isScriptErrorMessage(w.errorMessage)) {
|
|
|
|
var errorTextArray = w.errorMessage.split("|");
|
|
|
|
if (errorTextArray.length != 4) {
|
|
|
|
console.log("ERROR: Something wrong with Error text in evaluator...");
|
|
|
|
console.log("Error text: " + errorText);
|
2017-05-31 02:00:24 +02:00
|
|
|
return;
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|
|
|
|
var serverIp = errorTextArray[1];
|
|
|
|
var scriptName = errorTextArray[2];
|
|
|
|
var errorMsg = errorTextArray[3];
|
2017-07-27 04:56:14 +02:00
|
|
|
|
|
|
|
dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp +
|
|
|
|
"<br>Script name: " + scriptName +
|
2018-07-05 20:12:20 +02:00
|
|
|
"<br>Args:" + arrayToString(w.args) + "<br>" + errorMsg);
|
2017-06-17 04:53:57 +02:00
|
|
|
w.scriptRef.log("Script crashed with runtime error");
|
|
|
|
} else {
|
|
|
|
w.scriptRef.log("Script killed");
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|
2016-12-15 23:22:42 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-05-31 02:00:24 +02:00
|
|
|
} else if (isScriptErrorMessage(w)) {
|
2017-06-17 04:53:57 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
2017-08-30 19:44:29 +02:00
|
|
|
console.log("ERROR: Evaluating workerscript returns only error message rather than WorkerScript object. THIS SHOULDN'T HAPPEN: " + w.toString());
|
2017-06-17 04:53:57 +02:00
|
|
|
return;
|
2017-06-07 06:09:53 +02:00
|
|
|
} else {
|
|
|
|
dialogBoxCreate("An unknown script died for an unknown reason. This is a bug please contact game dev");
|
2018-05-12 02:45:40 +02:00
|
|
|
console.log(w);
|
2017-05-31 02:00:24 +02:00
|
|
|
}
|
2016-12-05 23:31:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-09-15 16:06:59 +02:00
|
|
|
setTimeout(runScriptsLoop, 6000);
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:09:23 +01:00
|
|
|
//Queues a script to be killed by settings its stop flag to true. Then, the code will reject
|
|
|
|
//all of its promises recursively, and when it does so it will no longer be running.
|
|
|
|
//The runScriptsLoop() will then delete the script from worker scripts
|
2017-06-17 04:53:57 +02:00
|
|
|
function killWorkerScript(runningScriptObj, serverIp) {
|
2016-12-06 19:09:23 +01:00
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
2017-06-17 04:53:57 +02:00
|
|
|
if (workerScripts[i].name == runningScriptObj.filename && workerScripts[i].serverIp == serverIp &&
|
|
|
|
compareArrays(workerScripts[i].args, runningScriptObj.args)) {
|
2016-12-06 19:09:23 +01:00
|
|
|
workerScripts[i].env.stopFlag = true;
|
2017-11-01 23:56:30 +01:00
|
|
|
killNetscriptDelay(workerScripts[i]);
|
2018-03-03 22:05:33 +01:00
|
|
|
//Recursively kill all functions
|
|
|
|
var curr = workerScripts[i];
|
|
|
|
while (curr.fnWorker) {
|
|
|
|
curr.fnWorker.env.stopFlag = true;
|
|
|
|
killNetscriptDelay(curr.fnWorker);
|
|
|
|
curr = curr.fnWorker;
|
2017-10-12 04:00:22 +02:00
|
|
|
}
|
2017-06-07 06:09:53 +02:00
|
|
|
return true;
|
2016-12-06 19:09:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 06:09:53 +02:00
|
|
|
return false;
|
2016-12-06 19:09:23 +01:00
|
|
|
}
|
|
|
|
|
2017-07-27 04:56:14 +02:00
|
|
|
//Queues a script to be run
|
2017-06-17 04:53:57 +02:00
|
|
|
function addWorkerScript(runningScriptObj, server) {
|
|
|
|
var filename = runningScriptObj.filename;
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
//Update server's ram usage
|
2017-06-11 23:07:38 +02:00
|
|
|
var threads = 1;
|
2017-06-17 04:53:57 +02:00
|
|
|
if (runningScriptObj.threads && !isNaN(runningScriptObj.threads)) {
|
|
|
|
threads = runningScriptObj.threads;
|
2017-06-11 23:07:38 +02:00
|
|
|
} else {
|
2017-06-17 05:32:58 +02:00
|
|
|
runningScriptObj.threads = 1;
|
2017-06-11 23:07:38 +02:00
|
|
|
}
|
2018-07-04 04:12:46 +02:00
|
|
|
var ramUsage = roundToTwo(runningScriptObj.scriptRef.ramUsage * threads);
|
2017-09-29 17:02:33 +02:00
|
|
|
var ramAvailable = server.maxRam - server.ramUsed;
|
|
|
|
if (ramUsage > ramAvailable) {
|
|
|
|
dialogBoxCreate("Not enough RAM to run script " + runningScriptObj.filename + " with args " +
|
2018-07-05 20:12:20 +02:00
|
|
|
arrayToString(runningScriptObj.args) + ". This likely occurred because you re-loaded " +
|
2017-09-29 17:02:33 +02:00
|
|
|
"the game and the script's RAM usage increased (either because of an update to the game or " +
|
|
|
|
"your changes to the script.)");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-04 04:12:46 +02:00
|
|
|
server.ramUsed = roundToTwo(server.ramUsed + ramUsage);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Create the WorkerScript
|
2017-06-17 04:53:57 +02:00
|
|
|
var s = new WorkerScript(runningScriptObj);
|
2016-12-14 21:29:40 +01:00
|
|
|
s.serverIp = server.ip;
|
2017-06-11 03:46:02 +02:00
|
|
|
s.ramUsage = ramUsage;
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Add the WorkerScript to the Active Scripts list
|
2017-06-06 23:22:57 +02:00
|
|
|
addActiveScriptsItem(s);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Add the WorkerScript
|
2016-12-14 21:29:40 +01:00
|
|
|
workerScripts.push(s);
|
2016-12-15 18:51:23 +01:00
|
|
|
return;
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Updates the online running time stat of all running scripts
|
|
|
|
function updateOnlineScriptTimes(numCycles = 1) {
|
|
|
|
var time = (numCycles * Engine._idleSpeed) / 1000; //seconds
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
workerScripts[i].scriptRef.onlineRunningTime += time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
export {WorkerScript, workerScripts, NetscriptPorts, runScriptsLoop,
|
|
|
|
killWorkerScript, addWorkerScript, updateOnlineScriptTimes,
|
|
|
|
prestigeWorkerScripts};
|