Type correctness

This commit is contained in:
Snarling 2022-08-09 19:02:33 -04:00
parent ea7c2c4981
commit c6cb4ca033
2 changed files with 16 additions and 9 deletions

@ -4,10 +4,11 @@ import { Player } from "../Player";
import { helpers } from "./NetscriptHelpers"; import { helpers } from "./NetscriptHelpers";
import { ScriptArg } from "./ScriptArg"; import { ScriptArg } from "./ScriptArg";
import { NSEnums } from "src/ScriptEditor/NetscriptDefinitions"; import { NSEnums } from "src/ScriptEditor/NetscriptDefinitions";
import { NSFull } from "src/NetscriptFunctions";
type ExternalFunction = (...args: unknown[]) => unknown; type ExternalFunction = (...args: unknown[]) => unknown;
export type ExternalAPI = { export type ExternalAPI = {
[string: string]: ExternalAPI | ExternalFunction; [string: string]: ExternalAPI | ExternalFunction | ScriptArg[];
}; };
type InternalFunction<F extends (...args: unknown[]) => unknown> = (ctx: NetscriptContext) => F; type InternalFunction<F extends (...args: unknown[]) => unknown> = (ctx: NetscriptContext) => F;
@ -65,18 +66,23 @@ function wrapFunction(
} }
export function wrapAPI( export function wrapAPI(
wrappedAPI: ExternalAPI,
workerScript: WorkerScript, workerScript: WorkerScript,
namespace: object, namespace: object,
...tree: string[] args: ScriptArg[]
): WrappedNetscriptAPI { ): 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)) { for (const [key, value] of Object.entries(namespace)) {
if (typeof value === "function") { if (typeof value === "function") {
wrapFunction(wrappedAPI, workerScript, value, ...tree, key); wrapFunction(wrappedAPI, workerScript, value, ...tree, key);
} else if (Array.isArray(value)) { } else if (Array.isArray(value)) {
setNestedProperty(wrappedAPI, value.slice(), key); setNestedProperty(wrappedAPI, value.slice(), key);
} else if (typeof value === "object") { } else if (typeof value === "object") {
wrapAPI(wrappedAPI, workerScript, value, ...tree, key); wrapAPILayer(wrappedAPI, workerScript, value, ...tree, key);
} else { } else {
setNestedProperty(wrappedAPI, value, ...tree, key); setNestedProperty(wrappedAPI, value, ...tree, key);
} }

@ -79,11 +79,12 @@ import { CalculateShareMult, StartSharing } from "./NetworkShare/Share";
import { recentScripts } from "./Netscript/RecentScripts"; import { recentScripts } from "./Netscript/RecentScripts";
import { InternalAPI, NetscriptContext, wrapAPI } from "./Netscript/APIWrapper"; import { InternalAPI, NetscriptContext, wrapAPI } from "./Netscript/APIWrapper";
import { ScriptArg } from "./Netscript/ScriptArg"; import { ScriptArg } from "./Netscript/ScriptArg";
import { INetscriptExtra } from "./NetscriptFunctions/Extra";
export function NetscriptFunctions(workerScript: WorkerScript): NS { export type NSFull = NS & INetscriptExtra;
const wrappedNS = wrapAPI({}, workerScript, ns) as unknown as NS;
(wrappedNS.args as ScriptArg[]) = workerScript.args; export function NetscriptFunctions(workerScript: WorkerScript): NSFull {
return wrappedNS; return wrapAPI(workerScript, ns, workerScript.args.slice());
} }
const base: InternalAPI<NS> = { const base: InternalAPI<NS> = {