diff --git a/src/CodingContracts.ts b/src/CodingContracts.ts index a0889bc93..0ffa743aa 100644 --- a/src/CodingContracts.ts +++ b/src/CodingContracts.ts @@ -106,7 +106,7 @@ export interface ICodingContractReward { */ export class CodingContract { /* Relevant data for the contract's problem */ - data: any; + data: unknown; /* Contract's filename */ fn: string; @@ -137,7 +137,7 @@ export class CodingContract { this.reward = reward; } - getData(): any { + getData(): unknown { return this.data; } diff --git a/src/NetscriptFunctions/CodingContract.ts b/src/NetscriptFunctions/CodingContract.ts index c5d50f59f..3378bcd54 100644 --- a/src/NetscriptFunctions/CodingContract.ts +++ b/src/NetscriptFunctions/CodingContract.ts @@ -88,7 +88,7 @@ export function NetscriptCodingContract(player: IPlayer, workerScript: WorkerScr const hostname = ctx.helper.string("hostname", _hostname); const contract = getCodingContract(ctx, "getData", hostname, filename); const data = contract.getData(); - if (data.constructor === Array) { + if (Array.isArray(data)) { // For two dimensional arrays, we have to copy the internal arrays using // slice() as well. As of right now, no contract has arrays that have // more than two dimensions diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index 298f9438e..afc70e255 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -13,7 +13,7 @@ import { CONSTANTS } from "./Constants"; import { Interpreter } from "./ThirdParty/JSInterpreter"; import { isScriptErrorMessage, makeRuntimeRejectMsg } from "./NetscriptEvaluator"; import { NetscriptFunctions } from "./NetscriptFunctions"; -import { executeJSScript } from "./NetscriptJSEvaluator"; +import { executeJSScript, Node } from "./NetscriptJSEvaluator"; import { NetscriptPort, IPort } from "./NetscriptPort"; import { RunningScript } from "./Script/RunningScript"; import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers"; @@ -37,6 +37,7 @@ import { areFilesEqual } from "./Terminal/DirectoryHelpers"; import { Player } from "./Player"; import { Terminal } from "./Terminal"; import { IPlayer } from "./PersonObjects/IPlayer"; +import { ScriptArg } from "./Netscript/ScriptArg"; // Netscript Ports are instantiated here export const NetscriptPorts: IPort[] = []; @@ -117,16 +118,19 @@ function startNetscript2Script(player: IPlayer, workerScript: WorkerScript): Pro }; } - function wrapObject(vars: any, ...tree: string[]): void { + function wrapObject(vars: unknown, ...tree: string[]): void { + const isObject = (x: unknown): x is { [key: string]: unknown } => typeof x === "object"; + if (!isObject(vars)) throw new Error("wrong argument sent to wrapObject"); for (const prop of Object.keys(vars)) { - switch (typeof vars[prop]) { + let e = vars[prop]; + switch (typeof e) { case "function": { - vars[prop] = wrap([...tree, prop].join("."), vars[prop]); + e = wrap([...tree, prop].join("."), e as any); break; } case "object": { - if (Array.isArray(vars[prop])) continue; - wrapObject(vars[prop], ...tree, prop); + if (Array.isArray(e)) continue; + wrapObject(e, ...tree, prop); break; } } @@ -191,26 +195,28 @@ function startNetscript1Script(workerScript: WorkerScript): Promise { const entry = ns[name]; if (typeof entry === "function") { //Async functions need to be wrapped. See JS-Interpreter documentation - if ( - ["hack", "grow", "weaken", "sleep", "prompt", "manualHack", "scp", "write", "share", "wget"].includes(name) - ) { - const tempWrapper = function (...args: any[]): void { + const asyncFuncs = ["hack", "grow", "weaken", "sleep", "prompt", "manualHack", "scp", "write", "share", "wget"]; + + if (asyncFuncs.includes(name)) { + const tempWrapper = function (...args: unknown[]): void { 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 for (let i = 0; i < args.length - 1; ++i) { - if (typeof args[i] === "object" || args[i].constructor === Array) { + if (typeof args[i] === "object" || Array.isArray(args[i])) { fnArgs.push(int.pseudoToNative(args[i])); } else { fnArgs.push(args[i]); } } - const cb = args[args.length - 1]; + const callb = args[args.length - 1]; const fnPromise = entry(...fnArgs); fnPromise - .then(function (res: any) { - cb(res); + .then(function (res: unknown) { + if (typeof callb === "function") { + callb(res); + } }) .catch(function (err: unknown) { if (typeof err === "string") { @@ -240,13 +246,13 @@ function startNetscript1Script(workerScript: WorkerScript): Promise { name === "run" || name === "exec" ) { - const tempWrapper = function (...args: any[]): void { + const tempWrapper = function (...args: unknown[]): void { 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 for (let i = 0; i < args.length; ++i) { - if (typeof args[i] === "object" || args[i].constructor === Array) { + if (typeof args[i] === "object" || Array.isArray(args[i])) { fnArgs.push(int.pseudoToNative(args[i])); } else { fnArgs.push(args[i]); @@ -257,7 +263,7 @@ function startNetscript1Script(workerScript: WorkerScript): Promise { }; int.setProperty(scope, name, int.createNativeFunction(tempWrapper)); } else { - const tempWrapper = function (...args: any[]): any { + const tempWrapper = function (...args: unknown[]): unknown { const res = entry(...args); if (res == null) { @@ -348,9 +354,9 @@ function startNetscript1Script(workerScript: WorkerScript): Promise { Should typically be positive } */ -function processNetscript1Imports(code: string, workerScript: WorkerScript): any { +function processNetscript1Imports(code: string, workerScript: WorkerScript): { code: string; lineOffset: number } { //allowReserved prevents 'import' from throwing error in ES5 - const ast: any = parse(code, { + const ast: Node = parse(code, { ecmaVersion: 9, allowReserved: true, sourceType: "module", @@ -375,7 +381,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any // Walk over the tree and process ImportDeclaration nodes walksimple(ast, { - ImportDeclaration: (node: any) => { + ImportDeclaration: (node: Node) => { hasImports = true; let scriptName = node.source.value; if (scriptName.startsWith("./")) { @@ -395,9 +401,9 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any // import * as namespace from script const namespace = node.specifiers[0].local.name; const fnNames: string[] = []; //Names only - const fnDeclarations: any[] = []; //FunctionDeclaration Node objects + const fnDeclarations: Node[] = []; //FunctionDeclaration Node objects walksimple(scriptAst, { - FunctionDeclaration: (node: any) => { + FunctionDeclaration: (node: Node) => { fnNames.push(node.id.name); fnDeclarations.push(node); }, @@ -407,7 +413,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any generatedCode += `var ${namespace};\n(function (namespace) {\n`; //Add the function declarations - fnDeclarations.forEach((fn: any) => { + fnDeclarations.forEach((fn: Node) => { generatedCode += generate(fn); generatedCode += "\n"; }); @@ -425,14 +431,14 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any //Get array of all fns to import const fnsToImport: string[] = []; - node.specifiers.forEach((e: any) => { + node.specifiers.forEach((e: Node) => { fnsToImport.push(e.local.name); }); //Walk through script and get FunctionDeclaration code for all specified fns - const fnDeclarations: any[] = []; + const fnDeclarations: Node[] = []; walksimple(scriptAst, { - FunctionDeclaration: (node: any) => { + FunctionDeclaration: (node: Node) => { if (fnsToImport.includes(node.id.name)) { fnDeclarations.push(node); } @@ -440,7 +446,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any }); //Convert FunctionDeclarations into code - fnDeclarations.forEach((fn: any) => { + fnDeclarations.forEach((fn: Node) => { generatedCode += generate(fn); generatedCode += "\n"; }); @@ -692,7 +698,7 @@ export function runScriptFromScript( caller: string, server: BaseServer, scriptname: string, - args: any[], + args: ScriptArg[], workerScript: WorkerScript, threads = 1, ): number {