import { Error, ObjectCreate, ObjectSetPrototypeOf, String, StringPrototypeToWellFormed, TypeError, uncurryThis, WeakMapPrototypeGet, } from "ext:core/primordials"; const charCode = uncurryThis(String.prototype.charCodeAt); export function string(value: unknown): string { if (typeof value === "symbol") { throw new TypeError("Cannot convert a Symbol to a string"); } return String(value); } export function usv(value: unknown): string { return StringPrototypeToWellFormed(string(value)); } export function byteString(value: unknown): string { const result = string(value); for (let i = 0; i < result.length; i++) { if (charCode(result, i) > 255) { throw new TypeError("Expected a ByteString"); } } return result; } export function dictionary(value: unknown): Record { if (value === undefined || value === null) return ObjectCreate(null); if (typeof value !== "object" && typeof value !== "function") { throw new TypeError("Expected an options dictionary"); } return value as Record; } export function isObject( value: unknown, ): value is Record { return value !== null && (typeof value === "object" || typeof value === "function"); } export function required(count: number, minimum: number): void { if (count < minimum) { throw new TypeError(`Expected at least ${minimum} argument(s)`); } } export function state( map: WeakMap, receiver: unknown, ): NonNullable { const value = WeakMapPrototypeGet(map, receiver); if (!value) throw new TypeError("Illegal invocation"); return value; } export function array(): T[] { const value: T[] = []; ObjectSetPrototypeOf(value, null); return value; } export function missing(feature: string): never { throw new Error(`not yet implemented: ${feature}`); }