From bf5b3c371e739edfb4ace63d168f882659280740 Mon Sep 17 00:00:00 2001 From: danielyxie Date: Sat, 12 May 2018 18:19:49 -0500 Subject: [PATCH] More work on NSJS memory leak fix --- dist/engine.bundle.js | 32 ++++++++++++++++++++++++-------- dist/tests.bundle.js | 32 ++++++++++++++++++++++++-------- src/NetscriptJSEvaluator.js | 27 +++++++++++++++++++-------- src/Script.js | 5 +++++ 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/dist/engine.bundle.js b/dist/engine.bundle.js index 6154ed04f..7a9a3f654 100644 --- a/dist/engine.bundle.js +++ b/dist/engine.bundle.js @@ -26512,6 +26512,11 @@ function loadAllRunningScripts() { //Reset each server's RAM usage to 0 server.ramUsed = 0; + //Reset modules on all scripts + for (var i = 0; i < server.scripts.length; ++i) { + server.scripts[i].module = ""; + } + for (var j = 0; j < server.runningScripts.length; ++j) { count++; server.runningScripts[j].scriptRef.module = ""; @@ -63659,9 +63664,10 @@ async function executeJSScript(script, scripts = [], workerScript) { console.log("loadedModule:"); console.log(loadedModule); let constructedModule = await ConstructModule(loadedModule, ns); + constructedModule = await constructedModule(); console.log("constructedModule: "); console.log(constructedModule); - return await constructedModule.main(); + return constructedModule.main(); } finally { // Revoke the generated URLs if (urlStack != null) { @@ -63670,14 +63676,24 @@ async function executeJSScript(script, scripts = [], workerScript) { }; } -async function ConstructModule(module, ns) { - for (var prop in ns) { - eval("var " + prop + " = ns[\"" + prop + "\"];"); +function ConstructModule(module, ns) { + function mod() { + /* + for (var fn in ns) { + let fooName = fn.toString(); + var evalStatement = "var " + fooName + " = ns[\"" + fooName + "\"];" + console.log(evalStatement); + eval(evalStatement); + console.log(ns[fooName]); + console.log("Setting " + fooName + " in ConstructModule()"); + console.log(fooName); + console.log(" "); + }*/ + var newModule = Object.create(module); + newModule.tprint = ns.tprint; + return newModule; } - async function mod() { - return module; - } - return mod(); + return mod; } // Gets a stack of blob urls, the top/right-most element being diff --git a/dist/tests.bundle.js b/dist/tests.bundle.js index 309881fab..d19308951 100644 --- a/dist/tests.bundle.js +++ b/dist/tests.bundle.js @@ -26512,6 +26512,11 @@ function loadAllRunningScripts() { //Reset each server's RAM usage to 0 server.ramUsed = 0; + //Reset modules on all scripts + for (var i = 0; i < server.scripts.length; ++i) { + server.scripts[i].module = ""; + } + for (var j = 0; j < server.runningScripts.length; ++j) { count++; server.runningScripts[j].scriptRef.module = ""; @@ -63700,9 +63705,10 @@ async function executeJSScript(script, scripts = [], workerScript) { console.log("loadedModule:"); console.log(loadedModule); let constructedModule = await ConstructModule(loadedModule, ns); + constructedModule = await constructedModule(); console.log("constructedModule: "); console.log(constructedModule); - return await constructedModule.main(); + return constructedModule.main(); } finally { // Revoke the generated URLs if (urlStack != null) { @@ -63711,14 +63717,24 @@ async function executeJSScript(script, scripts = [], workerScript) { }; } -async function ConstructModule(module, ns) { - for (var prop in ns) { - eval("var " + prop + " = ns[\"" + prop + "\"];"); +function ConstructModule(module, ns) { + function mod() { + /* + for (var fn in ns) { + let fooName = fn.toString(); + var evalStatement = "var " + fooName + " = ns[\"" + fooName + "\"];" + console.log(evalStatement); + eval(evalStatement); + console.log(ns[fooName]); + console.log("Setting " + fooName + " in ConstructModule()"); + console.log(fooName); + console.log(" "); + }*/ + var newModule = Object.create(module); + newModule.tprint = ns.tprint; + return newModule; } - async function mod() { - return module; - } - return mod(); + return mod; } // Gets a stack of blob urls, the top/right-most element being diff --git a/src/NetscriptJSEvaluator.js b/src/NetscriptJSEvaluator.js index a46971cb5..8533b8c30 100644 --- a/src/NetscriptJSEvaluator.js +++ b/src/NetscriptJSEvaluator.js @@ -44,9 +44,10 @@ export async function executeJSScript(script, scripts = [], workerScript) { console.log("loadedModule:"); console.log(loadedModule); let constructedModule = await ConstructModule(loadedModule, ns); + constructedModule = await constructedModule(); console.log("constructedModule: "); console.log(constructedModule); - return await constructedModule.main(); + return constructedModule.main(); } finally { // Revoke the generated URLs if (urlStack != null) { @@ -55,14 +56,24 @@ export async function executeJSScript(script, scripts = [], workerScript) { }; } -export async function ConstructModule(module, ns) { - for (var prop in ns) { - eval("var " + prop + " = ns[\"" + prop + "\"];"); +export function ConstructModule(module, ns) { + function mod() { + /* + for (var fn in ns) { + let fooName = fn.toString(); + var evalStatement = "var " + fooName + " = ns[\"" + fooName + "\"];" + console.log(evalStatement); + eval(evalStatement); + console.log(ns[fooName]); + console.log("Setting " + fooName + " in ConstructModule()"); + console.log(fooName); + console.log(" "); + }*/ + var newModule = Object.create(module); + newModule.tprint = ns.tprint; + return newModule; } - async function mod() { - return module; - } - return mod(); + return mod; } // Gets a stack of blob urls, the top/right-most element being diff --git a/src/Script.js b/src/Script.js index 83a1ada23..872119e29 100644 --- a/src/Script.js +++ b/src/Script.js @@ -713,6 +713,11 @@ function loadAllRunningScripts() { //Reset each server's RAM usage to 0 server.ramUsed = 0; + //Reset modules on all scripts + for (var i = 0; i < server.scripts.length; ++i) { + server.scripts[i].module = ""; + } + for (var j = 0; j < server.runningScripts.length; ++j) { count++; server.runningScripts[j].scriptRef.module = "";