fix an any

This commit is contained in:
Olivier Gagnon 2022-07-19 22:44:45 -04:00
parent 83c26903c0
commit ce2ebf576e
5 changed files with 20 additions and 7 deletions

@ -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));
}

@ -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<ScriptModule> | 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;
}

@ -0,0 +1,6 @@
import { AutocompleteData, NS } from "../ScriptEditor/NetscriptDefinitions";
export interface ScriptModule {
main?: (ns: NS) => Promise<void>;
autocomplete?: (data: AutocompleteData, flags: string[]) => unknown;
}

@ -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.

@ -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();