From dc518e7032bbbd6cd4ff4a4a0a670be618925a51 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Tue, 21 Sep 2021 20:39:25 -0400 Subject: [PATCH] asdf --- src/NetscriptFunctions.js | 12 +++--- src/NetscriptPort.ts | 86 ++++++++++++++++++++------------------- src/NetscriptWorker.js | 2 +- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 65c8cbff5..3e5c8e3e6 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -2354,7 +2354,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("write", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port.write(data); @@ -2421,7 +2421,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("tryWrite", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port.tryWrite(data); @@ -2442,7 +2442,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("read", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port.read(); @@ -2489,7 +2489,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("peek", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port.peek(); @@ -2506,7 +2506,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("clear", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port.clear(); @@ -2542,7 +2542,7 @@ function NetscriptFunctions(workerScript) { ); } var port = NetscriptPorts[port - 1]; - if (port == null || !(port instanceof NetscriptPort)) { + if (port == null || !(port instanceof Object)) { throw makeRuntimeErrorMsg("getPortHandle", `Could not find port: ${port}. This is a bug. Report to dev.`); } return port; diff --git a/src/NetscriptPort.ts b/src/NetscriptPort.ts index 27576879c..fa6b9b7a8 100644 --- a/src/NetscriptPort.ts +++ b/src/NetscriptPort.ts @@ -1,51 +1,55 @@ import { Settings } from "./Settings/Settings"; -export class NetscriptPort { - data: any[] = []; +interface IPort {} - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - write(data: any): any { - this.data.push(data); - if (this.data.length > Settings.MaxPortCapacity) { - return this.data.shift(); - } - return null; - } +export function NetscriptPort(): IPort { + const data: any[] = []; - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types - tryWrite(data: any): boolean { - if (this.data.length >= Settings.MaxPortCapacity) { - return false; - } - this.data.push(data); - return true; - } + return { + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + write: (value: any): any => { + data.push(value); + if (data.length > Settings.MaxPortCapacity) { + return data.shift(); + } + return null; + }, - read(): any { - if (this.data.length === 0) { - return "NULL PORT DATA"; - } - return this.data.shift(); - } + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + tryWrite: (value: any): boolean => { + if (data.length >= Settings.MaxPortCapacity) { + return false; + } + data.push(value); + return true; + }, - peek(): any { - if (this.data.length === 0) { - return "NULL PORT DATA"; - } else { - const foo = this.data.slice(); - return foo[0]; - } - } + read: (): any => { + if (data.length === 0) { + return "NULL PORT DATA"; + } + return data.shift(); + }, - full(): boolean { - return this.data.length == Settings.MaxPortCapacity; - } + peek: (): any => { + if (data.length === 0) { + return "NULL PORT DATA"; + } else { + const foo = data.slice(); + return foo[0]; + } + }, - empty(): boolean { - return this.data.length === 0; - } + full: (): boolean => { + return data.length == Settings.MaxPortCapacity; + }, - clear(): void { - this.data.length = 0; - } + empty: (): boolean => { + return data.length === 0; + }, + + clear: (): void => { + data.length = 0; + }, + }; } diff --git a/src/NetscriptWorker.js b/src/NetscriptWorker.js index e65ba65df..7aff7b30e 100644 --- a/src/NetscriptWorker.js +++ b/src/NetscriptWorker.js @@ -35,7 +35,7 @@ import { simple as walksimple } from "acorn-walk"; // Netscript Ports are instantiated here export const NetscriptPorts = []; for (var i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) { - NetscriptPorts.push(new NetscriptPort()); + NetscriptPorts.push(NetscriptPort()); } export function prestigeWorkerScripts() {