From c6cb4ca033d79c24a8a786c9873dcf0be8065e0a Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 9 Aug 2022 19:02:33 -0400 Subject: [PATCH] Type correctness --- src/Netscript/APIWrapper.ts | 16 +++++++++++----- src/NetscriptFunctions.ts | 9 +++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Netscript/APIWrapper.ts b/src/Netscript/APIWrapper.ts index 5c491b5ef..c70b09c6f 100644 --- a/src/Netscript/APIWrapper.ts +++ b/src/Netscript/APIWrapper.ts @@ -4,10 +4,11 @@ import { Player } from "../Player"; import { helpers } from "./NetscriptHelpers"; import { ScriptArg } from "./ScriptArg"; import { NSEnums } from "src/ScriptEditor/NetscriptDefinitions"; +import { NSFull } from "src/NetscriptFunctions"; type ExternalFunction = (...args: unknown[]) => unknown; export type ExternalAPI = { - [string: string]: ExternalAPI | ExternalFunction; + [string: string]: ExternalAPI | ExternalFunction | ScriptArg[]; }; type InternalFunction unknown> = (ctx: NetscriptContext) => F; @@ -65,18 +66,23 @@ function wrapFunction( } export function wrapAPI( - wrappedAPI: ExternalAPI, workerScript: WorkerScript, namespace: object, - ...tree: string[] -): WrappedNetscriptAPI { + args: ScriptArg[] +): NSFull { + const wrappedAPI = wrapAPILayer({},workerScript,namespace); + wrappedAPI.args=args; + return wrappedAPI as unknown as NSFull; +} + +export function wrapAPILayer(wrappedAPI:ExternalAPI,workerScript:WorkerScript,namespace:object,...tree:string[]){ for (const [key, value] of Object.entries(namespace)) { if (typeof value === "function") { wrapFunction(wrappedAPI, workerScript, value, ...tree, key); } else if (Array.isArray(value)) { setNestedProperty(wrappedAPI, value.slice(), key); } else if (typeof value === "object") { - wrapAPI(wrappedAPI, workerScript, value, ...tree, key); + wrapAPILayer(wrappedAPI, workerScript, value, ...tree, key); } else { setNestedProperty(wrappedAPI, value, ...tree, key); } diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 1c671d564..ab26fe67a 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -79,11 +79,12 @@ import { CalculateShareMult, StartSharing } from "./NetworkShare/Share"; import { recentScripts } from "./Netscript/RecentScripts"; import { InternalAPI, NetscriptContext, wrapAPI } from "./Netscript/APIWrapper"; import { ScriptArg } from "./Netscript/ScriptArg"; +import { INetscriptExtra } from "./NetscriptFunctions/Extra"; -export function NetscriptFunctions(workerScript: WorkerScript): NS { - const wrappedNS = wrapAPI({}, workerScript, ns) as unknown as NS; - (wrappedNS.args as ScriptArg[]) = workerScript.args; - return wrappedNS; +export type NSFull = NS & INetscriptExtra; + +export function NetscriptFunctions(workerScript: WorkerScript): NSFull { + return wrapAPI(workerScript, ns, workerScript.args.slice()); } const base: InternalAPI = {