fix args not beign passed to scripts

This commit is contained in:
Olivier Gagnon 2022-07-20 11:34:49 -04:00
parent 28b9c821d2
commit e0a24cf381
2 changed files with 11 additions and 14 deletions

@ -122,29 +122,24 @@ export function wrapAPI(
helpers: INetscriptHelper, helpers: INetscriptHelper,
wrappedAPI: ExternalAPI, wrappedAPI: ExternalAPI,
workerScript: WorkerScript, workerScript: WorkerScript,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
namespace: object, namespace: object,
...tree: string[] ...tree: string[]
): WrappedNetscriptAPI { ): WrappedNetscriptAPI {
if (typeof namespace !== "object") throw new Error("Invalid namespace?");
for (const [key, value] of Object.entries(namespace)) { for (const [key, value] of Object.entries(namespace)) {
switch (typeof value) { if (typeof value === "function") {
case "function": {
wrapFunction(helpers, wrappedAPI, workerScript, value, ...tree, key); wrapFunction(helpers, wrappedAPI, workerScript, value, ...tree, key);
break; } else if (Array.isArray(value)) {
} setNestedProperty(wrappedAPI, value, key);
case "object": { } else if (typeof value === "object") {
wrapAPI(helpers, wrappedAPI, workerScript, value, ...tree, key); wrapAPI(helpers, wrappedAPI, workerScript, value, ...tree, key);
break; } else {
}
default: {
setNestedProperty(wrappedAPI, value, ...tree, key); setNestedProperty(wrappedAPI, value, ...tree, key);
} }
} }
}
return wrappedAPI; return wrappedAPI;
} }
// TODO: This doesn't even work properly.
function setNestedProperty(root: object, value: unknown, ...tree: string[]): void { function setNestedProperty(root: object, value: unknown, ...tree: string[]): void {
let target = root; let target = root;
const key = tree.pop(); const key = tree.pop();

@ -2596,5 +2596,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const possibleLogs = Object.fromEntries([...getFunctionNames(ns, "")].map((a) => [a, true])); const possibleLogs = Object.fromEntries([...getFunctionNames(ns, "")].map((a) => [a, true]));
const wrappedNS = wrapAPI(helper, {}, workerScript, ns) as unknown as INS; const wrappedNS = wrapAPI(helper, {}, workerScript, ns) as unknown as INS;
(wrappedNS as any).args = ns.args;
(wrappedNS as any).enums = ns.enums;
return wrappedNS; return wrappedNS;
} }