Refactored NetscriptJS to take in the NS environment as a parameter to main

This commit is contained in:
danielyxie
2018-05-17 12:10:12 -05:00
parent 9a9096bc70
commit c7e155d4bc
25 changed files with 1490 additions and 682 deletions
+15 -3
View File
@@ -40,6 +40,18 @@ WorkerScript.prototype.getServer = function() {
return AllServers[this.serverIp];
}
//Returns the Script object for the underlying script
WorkerScript.prototype.getScript = function() {
let server = this.getServer();
for (var i = 0; i < server.scripts.length; ++i) {
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;
}
//Array containing all scripts that are running across all servers, to easily run them all
let workerScripts = [];
@@ -110,8 +122,7 @@ function startJsScript(workerScript) {
// 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.
return executeJSScript(workerScript.scriptRef.scriptRef,
workerScript.getServer().scripts,
return executeJSScript(workerScript.getServer().scripts,
workerScript).then(function (mainReturnValue) {
if (mainReturnValue === undefined) return workerScript;
return [mainReturnValue, workerScript];
@@ -156,13 +167,14 @@ function runScriptsLoop() {
workerScripts.splice(i, 1);
}
}
updateActiveScriptsItems(); //Force Update
//Run any scripts that haven't been started
for (var i = 0; i < workerScripts.length; i++) {
//If it isn't running, start the script
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
let p = null; // p is the script's result promise.
if (workerScripts[i].name.endsWith(".js")) {
if (workerScripts[i].name.endsWith(".js") || workerScripts[i].name.endsWith(".ns")) {
p = startJsScript(workerScripts[i]);
} else {
try {