Merge pull request #4249 from Snarling/typeAssertion

NETSCRIPT: Fix ns.prompt typechecking
This commit is contained in:
hydroflame 2022-10-19 11:44:59 -04:00 committed by GitHub
commit 1b7b4fa466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 53 deletions

@ -65,6 +65,8 @@ export const helpers = {
failOnHacknetServer, failOnHacknetServer,
}; };
/** Will probably remove the below function in favor of a different approach to object type assertion.
* This method cannot be used to handle optional properties. */
export function assertObjectType<T extends object>( export function assertObjectType<T extends object>(
ctx: NetscriptContext, ctx: NetscriptContext,
name: string, name: string,

@ -76,6 +76,7 @@ import { InternalAPI, wrapAPI } from "./Netscript/APIWrapper";
import { INetscriptExtra } from "./NetscriptFunctions/Extra"; import { INetscriptExtra } from "./NetscriptFunctions/Extra";
import { ScriptDeath } from "./Netscript/ScriptDeath"; import { ScriptDeath } from "./Netscript/ScriptDeath";
import { getBitNodeMultipliers } from "./BitNode/BitNode"; import { getBitNodeMultipliers } from "./BitNode/BitNode";
import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion";
// "Enums" as object // "Enums" as object
export const enums = { export const enums = {
@ -1752,13 +1753,36 @@ const base: InternalAPI<NS> = {
throw new Error(`variant must be one of ${Object.values(ToastVariant).join(", ")}`); throw new Error(`variant must be one of ${Object.values(ToastVariant).join(", ")}`);
SnackbarEvents.emit(message, variant as ToastVariant, duration); SnackbarEvents.emit(message, variant as ToastVariant, duration);
}, },
prompt: prompt: (ctx) => (_txt, _options) => {
(ctx) => const options: { type?: string; choices?: string[] } = {};
(_txt, options = {}) => { _options ??= options;
const txt = helpers.string(ctx, "txt", _txt); const txt = helpers.string(ctx, "txt", _txt);
const optionsValidator: { type?: string; options?: string[] } = {}; assert(_options, objectAssert, (type) =>
assertObjectType(ctx, "options", options, optionsValidator); helpers.makeRuntimeErrorMsg(ctx, `Invalid type for options: ${type}. Should be object.`, "TYPE"),
);
if (_options.type !== undefined) {
assert(_options.type, stringAssert, (type) =>
helpers.makeRuntimeErrorMsg(ctx, `Invalid type for options.type: ${type}. Should be string.`, "TYPE"),
);
options.type = _options.type;
const validTypes = ["boolean", "text", "select"];
if (!["boolean", "text", "select"].includes(options.type)) {
throw helpers.makeRuntimeErrorMsg(
ctx,
`Invalid value for options.type: ${options.type}. Must be one of ${validTypes.join(", ")}.`,
);
}
if (options.type === "select") {
assert(_options.choices, arrayAssert, (type) =>
helpers.makeRuntimeErrorMsg(
ctx,
`Invalid type for options.choices: ${type}. If options.type is "select", options.choices must be an array.`,
"TYPE",
),
);
options.choices = _options.choices.map((choice, i) => helpers.string(ctx, `options.choices[${i}]`, choice));
}
}
return new Promise(function (resolve) { return new Promise(function (resolve) {
PromptEvent.emit({ PromptEvent.emit({
txt: txt, txt: txt,
@ -1767,12 +1791,10 @@ const base: InternalAPI<NS> = {
}); });
}); });
}, },
wget: wget: (ctx) => async (_url, _target, _hostname) => {
(ctx) =>
async (_url, _target, _hostname = ctx.workerScript.hostname) => {
const url = helpers.string(ctx, "url", _url); const url = helpers.string(ctx, "url", _url);
const target = helpers.string(ctx, "target", _target); const target = helpers.string(ctx, "target", _target);
const hostname = helpers.string(ctx, "hostname", _hostname); const hostname = _hostname ? helpers.string(ctx, "hostname", _hostname) : ctx.workerScript.hostname;
if (!isScriptFilename(target) && !target.endsWith(".txt")) { if (!isScriptFilename(target) && !target.endsWith(".txt")) {
helpers.log(ctx, () => `Invalid target file: '${target}'. Must be a script or text file.`); helpers.log(ctx, () => `Invalid target file: '${target}'. Must be a script or text file.`);
return Promise.resolve(false); return Promise.resolve(false);

@ -6621,7 +6621,7 @@ export interface NS {
*/ */
prompt( prompt(
txt: string, txt: string,
options?: { type?: "boolean" | "text" | "select" | undefined; choices?: string[] }, options?: { type?: "boolean" | "text" | "select"; choices?: string[] },
): Promise<boolean | string>; ): Promise<boolean | string>;
/** /**

@ -0,0 +1,42 @@
// Various functions for asserting types.
/** Function for providing custom error message to throw for a type assertion.
* @param v: Value to assert type of
* @param assertFn: Typechecking function to use for asserting type of v.
* @param msgFn: Function to use to generate an error message if an error is produced. */
export function assert<T>(
v: unknown,
assertFn: (v: unknown) => asserts v is T,
msgFn: (type: string) => string,
): asserts v is T {
try {
assertFn(v);
} catch (type: unknown) {
if (typeof type !== "string") type = "unknown";
throw msgFn(type as string);
}
}
/** Returns the friendlyType of v. arrays are "array" and null is "null". */
export function getFriendlyType(v: unknown): string {
return v === null ? "null" : Array.isArray(v) ? "array" : typeof v;
}
//All assertion functions used here should return the friendlyType of the input.
/** For non-objects, and for array/null, throws the friendlyType of v. */
export function objectAssert(v: unknown): asserts v is Partial<Record<string, unknown>> {
const type = getFriendlyType(v);
if (type !== "object") throw type;
}
/** For non-string, throws the friendlyType of v. */
export function stringAssert(v: unknown): asserts v is string {
const type = getFriendlyType(v);
if (type !== "string") throw type;
}
/** For non-array, throws the friendlyType of v. */
export function arrayAssert(v: unknown): asserts v is unknown[] {
if (!Array.isArray(v)) throw getFriendlyType(v);
}