From ce2ebf576e7e314b4095bcc01b1280e7d0fb4019 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Tue, 19 Jul 2022 22:44:45 -0400 Subject: [PATCH] fix an any --- src/NetscriptJSEvaluator.ts | 5 ++++- src/Script/Script.ts | 7 ++++--- src/Script/ScriptModule.ts | 6 ++++++ src/ScriptEditor/NetscriptDefinitions.d.ts | 3 ++- src/Terminal/determineAllPossibilitiesForTabCompletion.ts | 6 ++++-- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 src/Script/ScriptModule.ts diff --git a/src/NetscriptJSEvaluator.ts b/src/NetscriptJSEvaluator.ts index 191bad566..1622586d6 100644 --- a/src/NetscriptJSEvaluator.ts +++ b/src/NetscriptJSEvaluator.ts @@ -73,6 +73,9 @@ export async function executeJSScript( const ns = workerScript.env.vars; + if (!loadedModule) { + throw makeRuntimeRejectMsg(workerScript, `${script.filename} cannot be run because the script module won't load`); + } // TODO: putting await in a non-async function yields unhelpful // "SyntaxError: unexpected reserved word" with no line number information. if (!loadedModule.main) { @@ -100,7 +103,7 @@ function isDependencyOutOfDate(filename: string, scripts: Script[], scriptModule * @param {Script[]} scripts */ function shouldCompile(script: Script, scripts: Script[]): boolean { - if (script.module === "") return true; + if (!script.module) return true; return script.dependencies.some((dep) => isDependencyOutOfDate(dep.filename, scripts, script.moduleSequenceNumber)); } diff --git a/src/Script/Script.ts b/src/Script/Script.ts index 70f6a72f6..be46c3e8f 100644 --- a/src/Script/Script.ts +++ b/src/Script/Script.ts @@ -10,6 +10,7 @@ import { ScriptUrl } from "./ScriptUrl"; import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver"; import { roundToTwo } from "../utils/helpers/roundToTwo"; import { IPlayer } from "../PersonObjects/IPlayer"; +import { ScriptModule } from "./ScriptModule"; let globalModuleSequenceNumber = 0; @@ -30,7 +31,7 @@ export class Script { // The dynamic module generated for this script when it is run. // This is only applicable for NetscriptJS - module: any = ""; + module: Promise | null = null; // The timestamp when when the script was last updated. moduleSequenceNumber: number; @@ -53,7 +54,7 @@ export class Script { this.code = code; this.ramUsage = 0; this.server = server; // hostname of server this script is on - this.module = ""; + this.module = null; this.moduleSequenceNumber = ++globalModuleSequenceNumber; if (this.code !== "" && player !== null) { this.updateRamUsage(player, otherScripts); @@ -90,7 +91,7 @@ export class Script { * to exec it. */ markUpdated(): void { - this.module = ""; + this.module = null; this.moduleSequenceNumber = ++globalModuleSequenceNumber; } diff --git a/src/Script/ScriptModule.ts b/src/Script/ScriptModule.ts new file mode 100644 index 000000000..f5a3ee425 --- /dev/null +++ b/src/Script/ScriptModule.ts @@ -0,0 +1,6 @@ +import { AutocompleteData, NS } from "../ScriptEditor/NetscriptDefinitions"; + +export interface ScriptModule { + main?: (ns: NS) => Promise; + autocomplete?: (data: AutocompleteData, flags: string[]) => unknown; +} diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 4605f9901..752c89a55 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -2037,9 +2037,10 @@ export interface Singularity { * guarantee that your browser will follow that time limit. * * @param crime - Name of crime to attempt. + * @param focus - Acquire player focus on this program creation. Optional. Defaults to true. * @returns The number of milliseconds it takes to attempt the specified crime. */ - commitCrime(crime: string): number; + commitCrime(crime: string, focus?: boolean): number; /** * Get chance to successfully commit a crime. diff --git a/src/Terminal/determineAllPossibilitiesForTabCompletion.ts b/src/Terminal/determineAllPossibilitiesForTabCompletion.ts index d21a3d97c..34e41a236 100644 --- a/src/Terminal/determineAllPossibilitiesForTabCompletion.ts +++ b/src/Terminal/determineAllPossibilitiesForTabCompletion.ts @@ -289,7 +289,7 @@ export async function determineAllPossibilitiesForTabCompletion( await compile(p, script, currServ.scripts); } const loadedModule = await script.module; - if (!loadedModule.autocomplete) return; // Doesn't have an autocomplete function. + if (!loadedModule || !loadedModule.autocomplete) return; // Doesn't have an autocomplete function. const runArgs = { "--tail": Boolean, "-t": Number }; const flags = libarg(runArgs, { @@ -317,7 +317,9 @@ export async function determineAllPossibilitiesForTabCompletion( }; let pos: string[] = []; let pos2: string[] = []; - pos = pos.concat(loadedModule.autocomplete(autocompleteData, flags._)); + const options = loadedModule.autocomplete(autocompleteData, flags._); + if (!Array.isArray(options)) throw new Error("autocomplete did not return list of strings"); + pos = pos.concat(options.map((x) => String(x))); return pos.concat(pos2); } const pos = await scriptAutocomplete();