Move the runningFn var to a higher level.

This commit is contained in:
James Aguilar 2018-05-05 20:39:34 -04:00
parent 8dba456b65
commit edca9a64ed

@ -62,29 +62,32 @@ function prestigeWorkerScripts() {
function startJsScript(workerScript) { function startJsScript(workerScript) {
workerScript.running = true; workerScript.running = true;
// The name of the currently running netscript function, to prevent concurrent
// calls to hack, grow, etc.
let runningFn = null;
// We need to go through the environment and wrap each function in such a way that it // 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 // can be called at most once at a time. This will prevent situations where multiple
// hack promises are outstanding, for example. // hack promises are outstanding, for example.
function wrap(propName, f) { function wrap(propName, f) {
let running = null; // The name of the currently running netscript function.
// This function unfortunately cannot be an async function, because we don't // 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. // know if the original one was, and there's no way to tell.
return function (...args) { return function (...args) {
const msg = "Concurrent calls to Netscript functions not allowed! " + const msg = "Concurrent calls to Netscript functions not allowed! " +
"Did you forget to await hack(), grow(), or some other " + "Did you forget to await hack(), grow(), or some other " +
"promise-returning function? (Currently running: %s tried to run: %s)" "promise-returning function? (Currently running: %s tried to run: %s)"
if (running) { if (runningFn) {
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, running, propName), null) workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, runningFn, propName), null)
throw workerScript; throw workerScript;
} }
running = propName; runningFn = propName;
let result = f(...args); let result = f(...args);
if (result && result.finally !== undefined) { if (result && result.finally !== undefined) {
return result.finally(function () { return result.finally(function () {
running = null; runningFn = null;
}); });
} else { } else {
running = null; runningFn = null;
return result; return result;
} }
} }
@ -104,7 +107,8 @@ function startJsScript(workerScript) {
return [mainReturnValue, workerScript]; return [mainReturnValue, workerScript];
}).catch(e => { }).catch(e => {
if (e instanceof Error) { if (e instanceof Error) {
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, e.message + (e.stack && ("\nstack:\n" + e.stack.toString()) || "")); workerScript.errorMessage = makeRuntimeRejectMsg(
workerScript, e.message + (e.stack && ("\nstack:\n" + e.stack.toString()) || ""));
throw workerScript; throw workerScript;
} else if (isScriptErrorMessage(e)) { } else if (isScriptErrorMessage(e)) {
workerScript.errorMessage = e; workerScript.errorMessage = e;