From f220965a73a031cd5421a2bc88ef5ad131c52169 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Tue, 19 Jul 2022 22:54:38 -0400 Subject: [PATCH] more anys --- src/NetscriptFunctions/toNative.ts | 35 ++++++++++++++++++------------ src/utils/JSONReviver.ts | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/NetscriptFunctions/toNative.ts b/src/NetscriptFunctions/toNative.ts index a72b183d5..d68b7b144 100644 --- a/src/NetscriptFunctions/toNative.ts +++ b/src/NetscriptFunctions/toNative.ts @@ -2,38 +2,45 @@ import { Interpreter } from "../ThirdParty/JSInterpreter"; const defaultInterpreter = new Interpreter("", () => undefined); +interface PseudoObject { + properties: Record; + class: string; +} + +const isPseudoObject = (v: unknown): v is PseudoObject => + !!v && + typeof v === "object" && + v.hasOwnProperty("properties") && + v.hasOwnProperty("getter") && + v.hasOwnProperty("setter") && + v.hasOwnProperty("proto"); + // the acorn interpreter has a bug where it doesn't convert arrays correctly. // so we have to more or less copy it here. -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export function toNative(pseudoObj: any): any { +export function toNative(pseudoObj: unknown): unknown { if (pseudoObj == null) return null; - if ( - !pseudoObj.hasOwnProperty("properties") || - !pseudoObj.hasOwnProperty("getter") || - !pseudoObj.hasOwnProperty("setter") || - !pseudoObj.hasOwnProperty("proto") - ) { + if (!isPseudoObject(pseudoObj)) { return pseudoObj; // it wasn't a pseudo object anyway. } - let nativeObj: any; if (pseudoObj.hasOwnProperty("class") && pseudoObj.class === "Array") { - nativeObj = []; + const arr: unknown[] = []; const length = defaultInterpreter.getProperty(pseudoObj, "length"); if (typeof length === "number") { for (let i = 0; i < length; i++) { if (defaultInterpreter.hasProperty(pseudoObj, i)) { - nativeObj[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i)); + arr[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i)); } } } + return arr; } else { // Object. - nativeObj = {}; + const obj: Record = {}; for (const key of Object.keys(pseudoObj.properties)) { const val = pseudoObj.properties[key]; - nativeObj[key] = toNative(val); + obj[key] = toNative(val); } + return obj; } - return nativeObj; } diff --git a/src/utils/JSONReviver.ts b/src/utils/JSONReviver.ts index 7df9c175c..e38fc2e67 100644 --- a/src/utils/JSONReviver.ts +++ b/src/utils/JSONReviver.ts @@ -60,7 +60,7 @@ export function Generic_toJSON(ctorName: string, obj: Record, keys? keys = Object.keys(obj); // Only "own" properties are included } - const data: any = {}; + const data: Record = {}; for (let index = 0; index < keys.length; ++index) { const key = keys[index]; data[key] = obj[key];