fix netscript wrapper to wrap all functions, not just the top level

This commit is contained in:
TheMas3212 2022-04-12 08:20:34 +10:00
parent 72fe3c6981
commit 8d5632eb3e
No known key found for this signature in database
GPG Key ID: 62A173A4FDA683CA

@ -116,11 +116,22 @@ function startNetscript2Script(player: IPlayer, workerScript: WorkerScript): Pro
};
}
for (const prop of Object.keys(workerScript.env.vars)) {
if (typeof workerScript.env.vars[prop] !== "function") continue;
workerScript.env.vars[prop] = wrap(prop, workerScript.env.vars[prop]);
function wrapObject(vars: any, ...tree: string[]): void {
for (const prop of Object.keys(vars)) {
switch (typeof vars[prop]) {
case 'function': {
vars[prop] = wrap([...tree, prop].join('.'), vars[prop]);
break;
}
case 'object': {
if (Array.isArray(vars[prop])) continue;
wrapObject(vars[prop], ...tree, prop)
break;
}
}
}
}
workerScript.env.vars.stanek.charge = wrap("stanek.charge", workerScript.env.vars.stanek.charge);
wrapObject(workerScript.env.vars);
// Note: the environment that we pass to the JS script only needs to contain the functions visible
// to that script, which env.vars does at this point.