This commit is contained in:
Olivier Gagnon
2021-09-25 01:26:03 -04:00
parent 5c6c472b64
commit b0f20c8c8f
33 changed files with 3609 additions and 1217 deletions
+32 -32
View File
@@ -38,7 +38,7 @@ import { simple as walksimple } from "acorn-walk";
// Netscript Ports are instantiated here
export const NetscriptPorts: IPort[] = [];
for (var i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
for (let i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
NetscriptPorts.push(NetscriptPort());
}
@@ -112,7 +112,7 @@ function startNetscript2Script(workerScript: WorkerScript): Promise<WorkerScript
};
}
for (let prop in workerScript.env.vars) {
for (const prop in workerScript.env.vars) {
if (typeof workerScript.env.vars[prop] !== "function") continue;
workerScript.env.vars[prop] = wrap(prop, workerScript.env.vars[prop]);
}
@@ -145,9 +145,9 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
workerScript.running = true;
//Process imports
var codeWithImports, codeLineOffset;
let codeWithImports, codeLineOffset;
try {
let importProcessingRes = processNetscript1Imports(code, workerScript);
const importProcessingRes = processNetscript1Imports(code, workerScript);
codeWithImports = importProcessingRes.code;
codeLineOffset = importProcessingRes.lineOffset;
} catch (e) {
@@ -158,11 +158,11 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
return Promise.resolve(workerScript);
}
var interpreterInitialization = function (int: any, scope: any) {
const interpreterInitialization = function (int: any, scope: any) {
//Add the Netscript environment
var ns = NetscriptFunctions(workerScript);
for (let name in ns) {
let entry = ns[name];
const ns = NetscriptFunctions(workerScript);
for (const name in ns) {
const entry = ns[name];
if (typeof entry === "function") {
//Async functions need to be wrapped. See JS-Interpreter documentation
if (
@@ -173,8 +173,8 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
name === "prompt" ||
name === "manualHack"
) {
let tempWrapper = function () {
let fnArgs = [];
const tempWrapper = function () {
const 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
@@ -185,8 +185,8 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
fnArgs.push(arguments[i]);
}
}
let cb = arguments[arguments.length - 1];
let fnPromise = entry.apply(null, fnArgs);
const cb = arguments[arguments.length - 1];
const fnPromise = entry.apply(null, fnArgs);
fnPromise
.then(function (res: any) {
cb(res);
@@ -206,8 +206,8 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
name === "run" ||
name === "exec"
) {
let tempWrapper = function () {
let fnArgs = [];
const tempWrapper = function () {
const 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
@@ -223,8 +223,8 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
};
int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
} else {
let tempWrapper = function () {
let res = entry.apply(null, arguments);
const tempWrapper = function () {
const res = entry.apply(null, arguments);
if (res == null) {
return res;
@@ -247,7 +247,7 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript
int.setProperty(scope, "args", int.nativeToPseudo(workerScript.args));
};
var interpreter: any;
let interpreter: any;
try {
interpreter = new Interpreter(codeWithImports, interpreterInitialization, codeLineOffset);
} catch (e) {
@@ -314,7 +314,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
sourceType: "module",
});
var server = workerScript.getServer();
const server = workerScript.getServer();
if (server == null) {
throw new Error("Failed to find underlying Server object for script");
}
@@ -339,11 +339,11 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
if (scriptName.startsWith("./")) {
scriptName = scriptName.slice(2);
}
let script = getScript(scriptName);
const script = getScript(scriptName);
if (script == null) {
throw new Error("'Import' failed due to invalid script: " + scriptName);
}
let scriptAst = parse(script.code, {
const scriptAst = parse(script.code, {
ecmaVersion: 9,
allowReserved: true,
sourceType: "module",
@@ -351,9 +351,9 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
if (node.specifiers.length === 1 && node.specifiers[0].type === "ImportNamespaceSpecifier") {
// import * as namespace from script
let namespace = node.specifiers[0].local.name;
let fnNames: string[] = []; //Names only
let fnDeclarations: any[] = []; //FunctionDeclaration Node objects
const namespace = node.specifiers[0].local.name;
const fnNames: string[] = []; //Names only
const fnDeclarations: any[] = []; //FunctionDeclaration Node objects
walksimple(scriptAst, {
FunctionDeclaration: (node: any) => {
fnNames.push(node.id.name);
@@ -382,13 +382,13 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
//import {...} from script
//Get array of all fns to import
let fnsToImport: string[] = [];
const fnsToImport: string[] = [];
node.specifiers.forEach((e: any) => {
fnsToImport.push(e.local.name);
});
//Walk through script and get FunctionDeclaration code for all specified fns
let fnDeclarations: any[] = [];
const fnDeclarations: any[] = [];
walksimple(scriptAst, {
FunctionDeclaration: (node: any) => {
if (fnsToImport.includes(node.id.name)) {
@@ -412,7 +412,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
}
//Remove ImportDeclarations from AST. These ImportDeclarations must be in top-level
var linesRemoved = 0;
let linesRemoved = 0;
if (ast.type !== "Program" || ast.body == null) {
throw new Error("Code could not be properly parsed");
}
@@ -424,7 +424,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
}
//Calculated line offset
var lineOffset = (generatedCode.match(/\n/g) || []).length - linesRemoved;
const lineOffset = (generatedCode.match(/\n/g) || []).length - linesRemoved;
//Convert the AST back into code
code = generate(ast);
@@ -432,7 +432,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
//Add the imported code and re-generate in ES5 (JS Interpreter for NS1 only supports ES5);
code = generatedCode + code;
var res = {
const res = {
code: code,
lineOffset: lineOffset,
};
@@ -596,7 +596,7 @@ export function createAndAddWorkerScript(
* Updates the online running time stat of all running scripts
*/
export function updateOnlineScriptTimes(numCycles = 1) {
var time = (numCycles * CONSTANTS._idleSpeed) / 1000; //seconds
const time = (numCycles * CONSTANTS._idleSpeed) / 1000; //seconds
for (const ws of workerScripts.values()) {
ws.scriptRef.onlineRunningTime += time;
}
@@ -607,7 +607,7 @@ export function updateOnlineScriptTimes(numCycles = 1) {
* into worker scripts so that they will start running
*/
export function loadAllRunningScripts() {
let skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1;
const skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1;
if (skipScriptLoad) {
console.info("Skipping the load of any scripts during startup");
}
@@ -661,7 +661,7 @@ export function runScriptFromScript(
}
// Check if the script is already running
let runningScriptObj = server.getRunningScript(scriptname, args);
const runningScriptObj = server.getRunningScript(scriptname, args);
if (runningScriptObj != null) {
workerScript.log(caller, `'${scriptname}' is already running on '${server.hostname}'`);
return 0;
@@ -703,7 +703,7 @@ export function runScriptFromScript(
caller,
`'${scriptname}' on '${server.hostname}' with ${threads} threads and args: ${arrayToString(args)}.`,
);
let runningScriptObj = new RunningScript(script, args);
const runningScriptObj = new RunningScript(script, args);
runningScriptObj.threads = threads;
return startWorkerScript(runningScriptObj, server, workerScript);