mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-04 14:47:53 +02:00
typefix netscriptFunctions (see desc)
* Types for InternalFunction and ExternalFunction have been modified to actually typecheck ns functions against docs. * Internal functions are required to use unknown for any params on the inner function. * Return types for internal function inner-function must match the respective external function. * Added new typecheck assertion function for asserting dynamic object types, to allow unknownifying params that were previously hardcoded objec structures. * Because type assertion for parameter types and return types is enforced by InternalAPI, removed all duplicate type declarations on NetscriptFunction params and returns.
This commit is contained in:
+27
-22
@@ -1,28 +1,39 @@
|
||||
import { Settings } from "./Settings/Settings";
|
||||
|
||||
type PortData = string | number;
|
||||
export interface IPort {
|
||||
write: (value: unknown) => unknown;
|
||||
write: (value: unknown) => PortData | null;
|
||||
tryWrite: (value: unknown) => boolean;
|
||||
read: () => unknown;
|
||||
peek: () => unknown;
|
||||
read: () => PortData;
|
||||
peek: () => PortData;
|
||||
full: () => boolean;
|
||||
empty: () => boolean;
|
||||
clear: () => void;
|
||||
}
|
||||
|
||||
export function NetscriptPort(): IPort {
|
||||
const data: unknown[] = [];
|
||||
const data: PortData[] = [];
|
||||
|
||||
return {
|
||||
write: (value: unknown): unknown => {
|
||||
write: (value) => {
|
||||
if (typeof value !== "number" && typeof value !== "string") {
|
||||
throw new Error(
|
||||
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
||||
);
|
||||
}
|
||||
data.push(value);
|
||||
if (data.length > Settings.MaxPortCapacity) {
|
||||
return data.shift();
|
||||
return data.shift() as PortData;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
tryWrite: (value: unknown): boolean => {
|
||||
tryWrite: (value) => {
|
||||
if (typeof value != "number" && typeof value != "string") {
|
||||
throw new Error(
|
||||
`port.write: Tried to write type ${typeof value}. Only string and number types may be written to ports.`,
|
||||
);
|
||||
}
|
||||
if (data.length >= Settings.MaxPortCapacity) {
|
||||
return false;
|
||||
}
|
||||
@@ -30,31 +41,25 @@ export function NetscriptPort(): IPort {
|
||||
return true;
|
||||
},
|
||||
|
||||
read: (): unknown => {
|
||||
if (data.length === 0) {
|
||||
return "NULL PORT DATA";
|
||||
}
|
||||
return data.shift();
|
||||
read: () => {
|
||||
if (data.length === 0) return "NULL PORT DATA";
|
||||
return data.shift() as PortData;
|
||||
},
|
||||
|
||||
peek: (): unknown => {
|
||||
if (data.length === 0) {
|
||||
return "NULL PORT DATA";
|
||||
} else {
|
||||
const foo = data.slice();
|
||||
return foo[0];
|
||||
}
|
||||
peek: () => {
|
||||
if (data.length === 0) return "NULL PORT DATA";
|
||||
return data[0];
|
||||
},
|
||||
|
||||
full: (): boolean => {
|
||||
full: () => {
|
||||
return data.length == Settings.MaxPortCapacity;
|
||||
},
|
||||
|
||||
empty: (): boolean => {
|
||||
empty: () => {
|
||||
return data.length === 0;
|
||||
},
|
||||
|
||||
clear: (): void => {
|
||||
clear: () => {
|
||||
data.length = 0;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user