From ceb9fa12492720752baddebb2ead253526b0c721 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Wed, 20 Jul 2022 01:13:06 -0400 Subject: [PATCH] fix more any --- src/Netscript/Environment.ts | 62 ++------------------------------ src/Netscript/WorkerScript.ts | 3 +- src/NetscriptFunctions.ts | 1 - src/NetscriptJSEvaluator.ts | 6 ++++ src/Script/RamCalculations.ts | 67 +++++++++++++---------------------- 5 files changed, 35 insertions(+), 104 deletions(-) diff --git a/src/Netscript/Environment.ts b/src/Netscript/Environment.ts index 77514b88c..919d593ab 100644 --- a/src/Netscript/Environment.ts +++ b/src/Netscript/Environment.ts @@ -1,13 +1,10 @@ +import { NS } from "../ScriptEditor/NetscriptDefinitions"; + /** * The environment in which a script runs. The environment holds * Netscript functions and arguments for that script. */ export class Environment { - /** - * Parent environment. Used to implement "scope" - */ - parent: Environment | null = null; - /** * Whether or not the script that uses this Environment should stop running */ @@ -16,58 +13,5 @@ export class Environment { /** * Environment variables (currently only Netscript functions) */ - vars: any = {}; - - constructor(parent: Environment | null) { - if (parent instanceof Environment) { - this.vars = Object.assign({}, parent.vars); - } - - this.parent = parent; - } - - /** - * Finds the scope where the variable with the given name is defined - */ - lookup(name: string): Environment | null { - // eslint-disable-next-line @typescript-eslint/no-this-alias - let scope: Environment | null = this; - while (scope) { - if (Object.prototype.hasOwnProperty.call(scope.vars, name)) { - return scope; - } - scope = scope.parent; - } - - return null; - } - - //Get the current value of a variable - get(name: string): any { - if (name in this.vars) { - return this.vars[name]; - } - - throw new Error(`Undefined variable ${name}`); - } - - //Sets the value of a variable in any scope - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - set(name: string, value: any): any { - const scope = this.lookup(name); - - //If scope has a value, then this variable is already set in a higher scope, so - //set is there. Otherwise, create a new variable in the local scope - if (scope !== null) { - return (scope.vars[name] = value); - } else { - return (this.vars[name] = value); - } - } - - //Creates (or overwrites) a variable in the current scope - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - def(name: string, value: any): any { - return (this.vars[name] = value); - } + vars: NS | null = null; } diff --git a/src/Netscript/WorkerScript.ts b/src/Netscript/WorkerScript.ts index 23ac56281..c8c9515d3 100644 --- a/src/Netscript/WorkerScript.ts +++ b/src/Netscript/WorkerScript.ts @@ -142,11 +142,10 @@ export class WorkerScript { } this.scriptRef = runningScriptObj; this.args = runningScriptObj.args.slice(); - this.env = new Environment(null); + this.env = new Environment(); if (typeof nsFuncsGenerator === "function") { this.env.vars = nsFuncsGenerator(this); } - this.env.set("args", runningScriptObj.args.slice()); } /** diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 7743d220e..711d48714 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -110,7 +110,6 @@ import { CityName } from "./Locations/data/CityNames"; import { InternalAPI, NetscriptContext, wrapAPI } from "./Netscript/APIWrapper"; import { INetscriptHelper, ScriptIdentifier } from "./NetscriptFunctions/INetscriptHelper"; import { IPlayer } from "./PersonObjects/IPlayer"; -import { PlayerObject } from "./PersonObjects/Player/PlayerObject"; import { GangMember } from "./Gang/GangMember"; import { GangMemberTask } from "./Gang/GangMemberTask"; import { ScriptArg } from "./Netscript/ScriptArg"; diff --git a/src/NetscriptJSEvaluator.ts b/src/NetscriptJSEvaluator.ts index 1622586d6..cb5102b55 100644 --- a/src/NetscriptJSEvaluator.ts +++ b/src/NetscriptJSEvaluator.ts @@ -84,6 +84,12 @@ export async function executeJSScript( `${script.filename} cannot be run because it does not have a main function.`, ); } + if (!ns) { + throw makeRuntimeRejectMsg( + workerScript, + `${script.filename} cannot be run because the NS object hasn't been constructed properly.`, + ); + } return loadedModule.main(ns); } diff --git a/src/Script/RamCalculations.ts b/src/Script/RamCalculations.ts index b7216e816..bf88dfdab 100644 --- a/src/Script/RamCalculations.ts +++ b/src/Script/RamCalculations.ts @@ -145,6 +145,7 @@ async function parseOnlyRamCalculate( ]; const unresolvedRefs = Object.keys(dependencyMap).filter((s) => s.startsWith(initialModule)); const resolvedRefs = new Set(); + const loadedFns: Record = {}; while (unresolvedRefs.length > 0) { const ref = unresolvedRefs.shift(); if (ref === undefined) throw new Error("ref should not be undefined"); @@ -198,53 +199,35 @@ async function parseOnlyRamCalculate( } // Only count each function once - if (workerScript.loadedFns[ref]) { + if (loadedFns[ref]) { continue; - } else { - workerScript.loadedFns[ref] = true; } + loadedFns[ref] = true; - // This accounts for namespaces (Bladeburner, CodingCpntract, etc.) - let func; - let refDetail = "n/a"; - if (ref in workerScript.env.vars.bladeburner) { - func = workerScript.env.vars.bladeburner[ref]; - refDetail = `bladeburner.${ref}`; - } else if (ref in workerScript.env.vars.codingcontract) { - func = workerScript.env.vars.codingcontract[ref]; - refDetail = `codingcontract.${ref}`; - } else if (ref in workerScript.env.vars.stanek) { - func = workerScript.env.vars.stanek[ref]; - refDetail = `stanek.${ref}`; - } else if (ref in workerScript.env.vars.infiltration) { - func = workerScript.env.vars.infiltration[ref]; - refDetail = `infiltration.${ref}`; - } else if (ref in workerScript.env.vars.gang) { - func = workerScript.env.vars.gang[ref]; - refDetail = `gang.${ref}`; - } else if (ref in workerScript.env.vars.sleeve) { - func = workerScript.env.vars.sleeve[ref]; - refDetail = `sleeve.${ref}`; - } else if (ref in workerScript.env.vars.stock) { - func = workerScript.env.vars.stock[ref]; - refDetail = `stock.${ref}`; - } else if (ref in workerScript.env.vars.ui) { - func = workerScript.env.vars.ui[ref]; - refDetail = `ui.${ref}`; - } else if (ref in workerScript.env.vars.grafting) { - func = workerScript.env.vars.grafting[ref]; - refDetail = `grafting.${ref}`; - } else if (ref in workerScript.env.vars.singularity) { - func = workerScript.env.vars.singularity[ref]; - refDetail = `singularity.${ref}`; - } else { - func = workerScript.env.vars[ref]; - refDetail = `${ref}`; - } - const fnRam = applyFuncRam(func); + // This accounts for namespaces (Bladeburner, CodingContract, etc.) + const findFunc = ( + prefix: string, + obj: object, + ref: string, + ): { func: (p: IPlayer) => number | number; refDetail: string } | undefined => { + if (!obj) return; + const elem = Object.entries(obj).find(([key]) => key === ref); + if (elem !== undefined && (typeof elem[1] === "function" || typeof elem[1] === "number")) { + return { func: elem[1], refDetail: `${prefix}${ref}` }; + } + for (const [key, value] of Object.entries(obj)) { + const found = findFunc(`${key}.`, value, ref); + if (found) return found; + } + return undefined; + }; + + const details = findFunc("", RamCosts, ref); + const fnRam = applyFuncRam(details?.func ?? 0); ram += fnRam; - detailedCosts.push({ type: "fn", name: refDetail, cost: fnRam }); + detailedCosts.push({ type: "fn", name: details?.refDetail ?? "", cost: fnRam }); } catch (error) { + console.log(error); continue; } }