rm more any

This commit is contained in:
Olivier Gagnon 2022-07-18 02:28:21 -04:00
parent 5b8eea66d4
commit 1b90b42881
3 changed files with 37 additions and 31 deletions

@ -106,7 +106,7 @@ export interface ICodingContractReward {
*/ */
export class CodingContract { export class CodingContract {
/* Relevant data for the contract's problem */ /* Relevant data for the contract's problem */
data: any; data: unknown;
/* Contract's filename */ /* Contract's filename */
fn: string; fn: string;
@ -137,7 +137,7 @@ export class CodingContract {
this.reward = reward; this.reward = reward;
} }
getData(): any { getData(): unknown {
return this.data; return this.data;
} }

@ -88,7 +88,7 @@ export function NetscriptCodingContract(player: IPlayer, workerScript: WorkerScr
const hostname = ctx.helper.string("hostname", _hostname); const hostname = ctx.helper.string("hostname", _hostname);
const contract = getCodingContract(ctx, "getData", hostname, filename); const contract = getCodingContract(ctx, "getData", hostname, filename);
const data = contract.getData(); const data = contract.getData();
if (data.constructor === Array) { if (Array.isArray(data)) {
// For two dimensional arrays, we have to copy the internal arrays using // For two dimensional arrays, we have to copy the internal arrays using
// slice() as well. As of right now, no contract has arrays that have // slice() as well. As of right now, no contract has arrays that have
// more than two dimensions // more than two dimensions

@ -13,7 +13,7 @@ import { CONSTANTS } from "./Constants";
import { Interpreter } from "./ThirdParty/JSInterpreter"; import { Interpreter } from "./ThirdParty/JSInterpreter";
import { isScriptErrorMessage, makeRuntimeRejectMsg } from "./NetscriptEvaluator"; import { isScriptErrorMessage, makeRuntimeRejectMsg } from "./NetscriptEvaluator";
import { NetscriptFunctions } from "./NetscriptFunctions"; import { NetscriptFunctions } from "./NetscriptFunctions";
import { executeJSScript } from "./NetscriptJSEvaluator"; import { executeJSScript, Node } from "./NetscriptJSEvaluator";
import { NetscriptPort, IPort } from "./NetscriptPort"; import { NetscriptPort, IPort } from "./NetscriptPort";
import { RunningScript } from "./Script/RunningScript"; import { RunningScript } from "./Script/RunningScript";
import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers"; import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers";
@ -37,6 +37,7 @@ import { areFilesEqual } from "./Terminal/DirectoryHelpers";
import { Player } from "./Player"; import { Player } from "./Player";
import { Terminal } from "./Terminal"; import { Terminal } from "./Terminal";
import { IPlayer } from "./PersonObjects/IPlayer"; import { IPlayer } from "./PersonObjects/IPlayer";
import { ScriptArg } from "./Netscript/ScriptArg";
// Netscript Ports are instantiated here // Netscript Ports are instantiated here
export const NetscriptPorts: IPort[] = []; export const NetscriptPorts: IPort[] = [];
@ -117,16 +118,19 @@ function startNetscript2Script(player: IPlayer, workerScript: WorkerScript): Pro
}; };
} }
function wrapObject(vars: any, ...tree: string[]): void { function wrapObject(vars: unknown, ...tree: string[]): void {
const isObject = (x: unknown): x is { [key: string]: unknown } => typeof x === "object";
if (!isObject(vars)) throw new Error("wrong argument sent to wrapObject");
for (const prop of Object.keys(vars)) { for (const prop of Object.keys(vars)) {
switch (typeof vars[prop]) { let e = vars[prop];
switch (typeof e) {
case "function": { case "function": {
vars[prop] = wrap([...tree, prop].join("."), vars[prop]); e = wrap([...tree, prop].join("."), e as any);
break; break;
} }
case "object": { case "object": {
if (Array.isArray(vars[prop])) continue; if (Array.isArray(e)) continue;
wrapObject(vars[prop], ...tree, prop); wrapObject(e, ...tree, prop);
break; break;
} }
} }
@ -191,26 +195,28 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
const entry = ns[name]; const entry = ns[name];
if (typeof entry === "function") { if (typeof entry === "function") {
//Async functions need to be wrapped. See JS-Interpreter documentation //Async functions need to be wrapped. See JS-Interpreter documentation
if ( const asyncFuncs = ["hack", "grow", "weaken", "sleep", "prompt", "manualHack", "scp", "write", "share", "wget"];
["hack", "grow", "weaken", "sleep", "prompt", "manualHack", "scp", "write", "share", "wget"].includes(name)
) { if (asyncFuncs.includes(name)) {
const tempWrapper = function (...args: any[]): void { const tempWrapper = function (...args: unknown[]): void {
const fnArgs = []; const fnArgs = [];
//All of the Object/array elements are in JSInterpreter format, so //All of the Object/array elements are in JSInterpreter format, so
//we have to convert them back to native format to pass them to these fns //we have to convert them back to native format to pass them to these fns
for (let i = 0; i < args.length - 1; ++i) { for (let i = 0; i < args.length - 1; ++i) {
if (typeof args[i] === "object" || args[i].constructor === Array) { if (typeof args[i] === "object" || Array.isArray(args[i])) {
fnArgs.push(int.pseudoToNative(args[i])); fnArgs.push(int.pseudoToNative(args[i]));
} else { } else {
fnArgs.push(args[i]); fnArgs.push(args[i]);
} }
} }
const cb = args[args.length - 1]; const callb = args[args.length - 1];
const fnPromise = entry(...fnArgs); const fnPromise = entry(...fnArgs);
fnPromise fnPromise
.then(function (res: any) { .then(function (res: unknown) {
cb(res); if (typeof callb === "function") {
callb(res);
}
}) })
.catch(function (err: unknown) { .catch(function (err: unknown) {
if (typeof err === "string") { if (typeof err === "string") {
@ -240,13 +246,13 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
name === "run" || name === "run" ||
name === "exec" name === "exec"
) { ) {
const tempWrapper = function (...args: any[]): void { const tempWrapper = function (...args: unknown[]): void {
const fnArgs = []; const fnArgs = [];
//All of the Object/array elements are in JSInterpreter format, so //All of the Object/array elements are in JSInterpreter format, so
//we have to convert them back to native format to pass them to these fns //we have to convert them back to native format to pass them to these fns
for (let i = 0; i < args.length; ++i) { for (let i = 0; i < args.length; ++i) {
if (typeof args[i] === "object" || args[i].constructor === Array) { if (typeof args[i] === "object" || Array.isArray(args[i])) {
fnArgs.push(int.pseudoToNative(args[i])); fnArgs.push(int.pseudoToNative(args[i]));
} else { } else {
fnArgs.push(args[i]); fnArgs.push(args[i]);
@ -257,7 +263,7 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
}; };
int.setProperty(scope, name, int.createNativeFunction(tempWrapper)); int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
} else { } else {
const tempWrapper = function (...args: any[]): any { const tempWrapper = function (...args: unknown[]): unknown {
const res = entry(...args); const res = entry(...args);
if (res == null) { if (res == null) {
@ -348,9 +354,9 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
Should typically be positive Should typically be positive
} }
*/ */
function processNetscript1Imports(code: string, workerScript: WorkerScript): any { function processNetscript1Imports(code: string, workerScript: WorkerScript): { code: string; lineOffset: number } {
//allowReserved prevents 'import' from throwing error in ES5 //allowReserved prevents 'import' from throwing error in ES5
const ast: any = parse(code, { const ast: Node = parse(code, {
ecmaVersion: 9, ecmaVersion: 9,
allowReserved: true, allowReserved: true,
sourceType: "module", sourceType: "module",
@ -375,7 +381,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
// Walk over the tree and process ImportDeclaration nodes // Walk over the tree and process ImportDeclaration nodes
walksimple(ast, { walksimple(ast, {
ImportDeclaration: (node: any) => { ImportDeclaration: (node: Node) => {
hasImports = true; hasImports = true;
let scriptName = node.source.value; let scriptName = node.source.value;
if (scriptName.startsWith("./")) { if (scriptName.startsWith("./")) {
@ -395,9 +401,9 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
// import * as namespace from script // import * as namespace from script
const namespace = node.specifiers[0].local.name; const namespace = node.specifiers[0].local.name;
const fnNames: string[] = []; //Names only const fnNames: string[] = []; //Names only
const fnDeclarations: any[] = []; //FunctionDeclaration Node objects const fnDeclarations: Node[] = []; //FunctionDeclaration Node objects
walksimple(scriptAst, { walksimple(scriptAst, {
FunctionDeclaration: (node: any) => { FunctionDeclaration: (node: Node) => {
fnNames.push(node.id.name); fnNames.push(node.id.name);
fnDeclarations.push(node); fnDeclarations.push(node);
}, },
@ -407,7 +413,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
generatedCode += `var ${namespace};\n(function (namespace) {\n`; generatedCode += `var ${namespace};\n(function (namespace) {\n`;
//Add the function declarations //Add the function declarations
fnDeclarations.forEach((fn: any) => { fnDeclarations.forEach((fn: Node) => {
generatedCode += generate(fn); generatedCode += generate(fn);
generatedCode += "\n"; generatedCode += "\n";
}); });
@ -425,14 +431,14 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
//Get array of all fns to import //Get array of all fns to import
const fnsToImport: string[] = []; const fnsToImport: string[] = [];
node.specifiers.forEach((e: any) => { node.specifiers.forEach((e: Node) => {
fnsToImport.push(e.local.name); fnsToImport.push(e.local.name);
}); });
//Walk through script and get FunctionDeclaration code for all specified fns //Walk through script and get FunctionDeclaration code for all specified fns
const fnDeclarations: any[] = []; const fnDeclarations: Node[] = [];
walksimple(scriptAst, { walksimple(scriptAst, {
FunctionDeclaration: (node: any) => { FunctionDeclaration: (node: Node) => {
if (fnsToImport.includes(node.id.name)) { if (fnsToImport.includes(node.id.name)) {
fnDeclarations.push(node); fnDeclarations.push(node);
} }
@ -440,7 +446,7 @@ function processNetscript1Imports(code: string, workerScript: WorkerScript): any
}); });
//Convert FunctionDeclarations into code //Convert FunctionDeclarations into code
fnDeclarations.forEach((fn: any) => { fnDeclarations.forEach((fn: Node) => {
generatedCode += generate(fn); generatedCode += generate(fn);
generatedCode += "\n"; generatedCode += "\n";
}); });
@ -692,7 +698,7 @@ export function runScriptFromScript(
caller: string, caller: string,
server: BaseServer, server: BaseServer,
scriptname: string, scriptname: string,
args: any[], args: ScriptArg[],
workerScript: WorkerScript, workerScript: WorkerScript,
threads = 1, threads = 1,
): number { ): number {