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 { 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<F extends (...args: unknown[]) => 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);
}

@ -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<NS> = {