Merge pull request #216 from jaguilar/dev

Move the runningFn var to a higher level.
This commit is contained in:
danielyxie 2018-05-06 15:31:47 -05:00 committed by GitHub
commit 4c52cfc6f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -62,7 +62,9 @@ function prestigeWorkerScripts() {
function startJsScript(workerScript) {
workerScript.running = true;
let running = null; // The name of the currently running netscript function.
// 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
// can be called at most once at a time. This will prevent situations where multiple
@ -74,18 +76,18 @@ function startJsScript(workerScript) {
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)"
if (running) {
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, running, propName), null)
if (runningFn) {
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, runningFn, propName), null)
throw workerScript;
}
running = propName;
runningFn = propName;
let result = f(...args);
if (result && result.finally !== undefined) {
return result.finally(function () {
running = null;
runningFn = null;
});
} else {
running = null;
runningFn = null;
return result;
}
}
@ -105,7 +107,8 @@ function startJsScript(workerScript) {
return [mainReturnValue, workerScript];
}).catch(e => {
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;
} else if (isScriptErrorMessage(e)) {
workerScript.errorMessage = e;