// Perry WASM Runtime Bridge // Provides JavaScript runtime functions imported by the WASM module. // Handles NaN-boxing, string management, handle store, and browser API access. // NaN-boxing constants (matching perry-runtime/src/value.rs) const __hostGlobal = globalThis; // The program shares the host Math and JSON and may replace their members (#15104). // The glue's own arithmetic and formatting call the originals, captured before any // program code runs. The math_* / json_* bridges keep reading the live members: a call // bound to the intrinsic still reaches a replacement the compiler cannot see (eval). const __Math = Object.fromEntries(Object.getOwnPropertyNames(Math).map((k) => [k, Math[k]])); const __JSON = { parse: JSON.parse, stringify: JSON.stringify }; // The runtime calls functions and checks own keys through these: a program may overwrite // `Function.prototype.call` / `apply`, `Object.prototype.hasOwnProperty` or `Reflect.apply`. const __reflectApply = Reflect.apply; const __hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty); const TAG_UNDEFINED = 0x7FFC_0000_0000_0001n; const TAG_NULL = 0x7FFC_0000_0000_0002n; const TAG_FALSE = 0x7FFC_0000_0000_0003n; const TAG_TRUE = 0x7FFC_0000_0000_0004n; const STRING_TAG = 0x7FFFn; const POINTER_TAG = 0x7FFDn; const INT32_TAG = 0x7FFEn; // f64 <-> u64 conversion via shared buffer const _convBuf = new ArrayBuffer(8); const _f64 = new Float64Array(_convBuf); const _u64 = new BigUint64Array(_convBuf); function f64ToU64(f) { _f64[0] = f; return _u64[0]; } function u64ToF64(u) { _u64[0] = u; return _f64[0]; } // Issue #1035: pad a BigInt arg list to match the WASM function's declared arity. // Missing trailing slots correspond to TS optional params the caller omitted; the // WASM ABI expects an i64 (BigInt) per declared slot, so we fill with TAG_UNDEFINED. // Without this, V8/SpiderMonkey auto-coerce `undefined` via BigInt(undefined) and // throw "Cannot convert undefined to a BigInt". `prefix` is the count of leading // args already on the call (e.g. 1 for the receiver, or closure capture count). // A function with a rest parameter exports its slot as `__wasm_rest_`; the // arguments from that slot on become one array, as a direct call packs them. The last // `caps` arguments are a class's captures, the constructor's last parameters. function __padBigintArgs(fn, prefix, bigintArgs, caps = 0) { const rest = wasmInstance?.exports['__wasm_rest_' + fn.name]; const captures = bigintArgs.splice(bigintArgs.length - caps); if (rest) { const at = __Math.max(0, rest.value - (prefix | 0)); while (bigintArgs.length < at) bigintArgs.push(TAG_UNDEFINED); bigintArgs.push(__jsValueToBits(bigintArgs.splice(at).map(__bitsToJsValue))); } else if (caps > 0) { const own = fn.length - (prefix | 0) - caps; while (bigintArgs.length < own) bigintArgs.push(TAG_UNDEFINED); bigintArgs.length = own; } bigintArgs.push(...captures); const need = __Math.max(0, (fn.length | 0) - (prefix | 0)); while (bigintArgs.length < need) bigintArgs.push(TAG_UNDEFINED); return bigintArgs; } // NaN-box helpers function nanboxString(id) { return u64ToF64((STRING_TAG << 48n) | BigInt(id)); } function nanboxPointer(id) { return u64ToF64((POINTER_TAG << 48n) | BigInt(id)); } function isString(val) { return (f64ToU64(val) >> 48n) === STRING_TAG; } function isPointer(val) { return (f64ToU64(val) >> 48n) === POINTER_TAG; } function getStringId(val) { return Number(f64ToU64(val) & 0xFFFFFFFFn); } function getPointerId(val) { return Number(f64ToU64(val) & 0xFFFFFFFFn); } function isUndefined(val) { return f64ToU64(val) === TAG_UNDEFINED; } function isNull(val) { return f64ToU64(val) === TAG_NULL; } function isTrue(val) { return f64ToU64(val) === TAG_TRUE; } function isFalse(val) { return f64ToU64(val) === TAG_FALSE; } // String table — maps string_id (index) to JS string const stringTable = []; // Handle store — maps handle_id to JS objects/arrays/closures/etc. const handleStore = new Map(); let nextHandleId = 1; const objectHandles = new WeakMap(); // A Map, not the WeakMap: a registered symbol cannot be a weak key. const symbolHandles = new Map(); function allocHandle(obj) { const reference = obj !== null && (typeof obj === 'object' || typeof obj === 'function'); if (reference && objectHandles.has(obj)) return objectHandles.get(obj); if (typeof obj === 'symbol' && symbolHandles.has(obj)) return symbolHandles.get(obj); const id = nextHandleId++; if (reference) objectHandles.set(obj, id); else if (typeof obj === 'symbol') symbolHandles.set(obj, id); handleStore.set(id, obj); return id; } function getHandle(val) { if (!isPointer(val)) return undefined; return handleStore.get(getPointerId(val)); } function getHandleId(val) { return getPointerId(val); } // Convert a NaN-boxed f64 value to a JS value function toJsValue(val) { const bits = f64ToU64(val); if (bits === TAG_UNDEFINED) return undefined; if (bits === TAG_NULL) return null; if (bits === TAG_TRUE) return true; if (bits === TAG_FALSE) return false; const tag = bits >> 48n; if (tag === STRING_TAG) return stringTable[Number(bits & 0xFFFFFFFFn)]; if (tag === POINTER_TAG) { const obj = handleStore.get(Number(bits & 0xFFFFFFFFn)); if (obj !== undefined) return obj; } return val; // plain number } // Convert a JS value to a NaN-boxed f64 function fromJsValue(v) { if (v === undefined) return u64ToF64(TAG_UNDEFINED); if (v === null) return u64ToF64(TAG_NULL); if (v === true) return u64ToF64(TAG_TRUE); if (v === false) return u64ToF64(TAG_FALSE); if (typeof v === 'number') return v; if (typeof v === 'string') { const id = stringTable.length; stringTable.push(v); return nanboxString(id); } // Object/Array/Function — store as handle const id = allocHandle(v); return nanboxPointer(id); } // globalThis as a NaN-boxed value, boxed once: each fromJsValue of an object takes a // handle that is never released, so boxing it per read grows handleStore without bound // in an async loop that reads builtins. let __globalThisBoxed; function globalThisValue() { if (__globalThisBoxed === undefined) __globalThisBoxed = fromJsValue(__hostGlobal); return __globalThisBoxed; } // Get string from NaN-boxed value (supports both string tag and pointer to string) function getString(val) { if (isString(val)) return stringTable[getStringId(val)]; const js = toJsValue(val); return String(js); } let wasmMemory = null; let wasmInstance = null; // Expose NaN-box conversion for FFI bridge (bloom_glue.js etc.) // These allow external code to convert between Perry's NaN-boxed i64 values and JS values. globalThis.__perryToJsValue = function(nanboxedF64) { return toJsValue(nanboxedF64); }; globalThis.__perryFromJsValue = function(jsValue) { return fromJsValue(jsValue); }; globalThis.__perryI64ToF64 = function(i64BigInt) { return u64ToF64(i64BigInt); }; globalThis.__perryF64ToI64 = function(f64Val) { return f64ToU64(f64Val); }; // BigInt-native helpers (Issue #133 item 5). Prefer these on Firefox/Safari: // going through f64 loses NaN-box tag payloads when the Number crosses a JS // function-call boundary (SpiderMonkey collapses foreign NaNs to the canonical // 0x7FF8_0000_0000_0000). __perry*Bits helpers skip the f64 round-trip entirely. // __perryJsValueToBits(v) -> BigInt (raw NaN-box bits, ready to pass to a WASM // i64 export). __perryBitsToJsValue(bits) -> JS value (handle, string, number, …). globalThis.__perryJsValueToBits = function(v) { return __jsValueToBits(v); }; globalThis.__perryBitsToJsValue = function(bits) { return __bitsToJsValue(bits); }; // Build the import object for WASM instantiation function buildImports() { return { rt: { // ===== Core (Phase 0) ===== string_new: (offset, len) => { const bytes = new Uint8Array(wasmMemory.buffer, offset, len); stringTable.push(new TextDecoder().decode(bytes)); }, console_log: (val) => { console.log(toJsValue(val)); }, console_warn: (val) => { console.warn(toJsValue(val)); }, console_error: (val) => { console.error(toJsValue(val)); }, string_concat: (a, b) => { const s = stringTable[getStringId(a)] + stringTable[getStringId(b)]; stringTable.push(s); return nanboxString(stringTable.length - 1); }, js_add: (a, b) => fromJsValue(toJsValue(a) + toJsValue(b)), string_eq: (a, b) => stringTable[getStringId(a)] === stringTable[getStringId(b)] ? 1 : 0, string_len: (val) => { if (isString(val)) return stringTable[getStringId(val)].length; // Array length const obj = getHandle(val); if (Array.isArray(obj)) return obj.length; return 0; }, jsvalue_to_string: (val) => { stringTable.push(String(toJsValue(val))); return nanboxString(stringTable.length - 1); }, jsvalue_to_template_string: (val) => { stringTable.push(`${toJsValue(val)}`); return nanboxString(stringTable.length - 1); }, is_truthy: (val) => { const bits = f64ToU64(val); if (bits === TAG_FALSE || bits === TAG_NULL || bits === TAG_UNDEFINED) return 0; if (bits === TAG_TRUE) return 1; const tag = bits >> 48n; if (tag === STRING_TAG) return stringTable[Number(bits & 0xFFFFFFFFn)].length > 0 ? 1 : 0; if (tag === POINTER_TAG) return 1; // objects are truthy return (val === 0 || Number.isNaN(val)) ? 0 : 1; }, js_strict_eq: (a, b) => toJsValue(a) === toJsValue(b) ? 1 : 0, // Math math_floor: (x) => Math.floor(x), math_ceil: (x) => Math.ceil(x), math_round: (x) => Math.round(x), math_abs: (x) => Math.abs(x), math_sqrt: (x) => Math.sqrt(x), math_pow: (base, exp) => Math.pow(base, exp), math_random: () => Math.random(), math_log: (x) => Math.log(x), date_now: () => Date.now(), js_typeof: (val) => { const t = __typeOf(toJsValue(val)); stringTable.push(t); return nanboxString(stringTable.length - 1); }, math_min: (a, b) => Math.min(a, b), math_max: (a, b) => Math.max(a, b), parse_int: (val) => { const s = isString(val) ? stringTable[getStringId(val)] : String(toJsValue(val)); return parseInt(s, 10); }, parse_float: (val) => { const s = isString(val) ? stringTable[getStringId(val)] : String(toJsValue(val)); return parseFloat(s); }, // Phase 0 fixes js_mod: (a, b) => a % b, is_null_or_undefined: (val) => { const bits = f64ToU64(val); return (bits === TAG_NULL || bits === TAG_UNDEFINED) ? 1 : 0; }, // ===== Phase 1: Objects ===== object_new: () => nanboxPointer(allocHandle({})), // object_set(handle, key_str, value) -> handle (for chaining) object_set: (handle, key, value) => { const obj = getHandle(handle); if (obj) obj[getString(key)] = toJsValue(value); return handle; }, object_get: (handle, key) => { const obj = getHandle(handle); if (!obj) return u64ToF64(TAG_UNDEFINED); return fromJsValue(obj[getString(key)]); }, object_get_dynamic: (handle, key) => { const obj = getHandle(handle); if (!obj) return u64ToF64(TAG_UNDEFINED); const k = toJsValue(key); return fromJsValue(obj[k]); }, object_set_dynamic: (handle, key, value) => { const obj = getHandle(handle); if (obj) obj[toJsValue(key)] = toJsValue(value); }, object_delete: (handle, key) => { const obj = getHandle(handle); if (obj) delete obj[getString(key)]; }, object_delete_dynamic: (handle, key) => { const obj = getHandle(handle); if (obj) delete obj[toJsValue(key)]; }, object_keys: (handle) => { const obj = getHandle(handle); return nanboxPointer(allocHandle(obj ? Object.keys(obj) : [])); }, object_values: (handle) => { const obj = getHandle(handle); return nanboxPointer(allocHandle(obj ? Object.keys(obj).map(k => obj[k]) : [])); }, object_entries: (handle) => { const obj = getHandle(handle); return nanboxPointer(allocHandle(obj ? Object.entries(obj) : [])); }, object_has_property: (handle, key) => { const obj = getHandle(handle); if (!obj) return 0; const k = toJsValue(key); return (k in obj) ? 1 : 0; }, // object_assign(target, source) -> target handle object_assign: (target, source) => { const rawTarget = toJsValue(target); if (rawTarget == null) throw new TypeError('Cannot convert undefined or null to object'); const t = Object(rawTarget); const s = toJsValue(source); if (s != null) Object.assign(t, s); return isPointer(target) ? target : fromJsValue(t); }, // ===== Phase 1: Arrays ===== array_new: () => nanboxPointer(allocHandle([])), // `new Array(x)` — ES2015 §22.1.1: a single NUMBER argument is a // length (must be a non-negative integer < 2^32), anything else is a // one-element array. Mirrors js_array_constructor_single in the // native runtime. array_constructor_single: (value) => { const v = toJsValue(value); if (typeof v === 'number') { if (!Number.isInteger(v) || v < 0 || v > 0xFFFFFFFF) { throw new RangeError('Invalid array length'); } return nanboxPointer(allocHandle(new Array(v))); } return nanboxPointer(allocHandle([v])); }, // array_push(handle, value) -> handle (for chaining) array_push: (handle, value) => { const arr = getHandle(handle); if (arr) arr.push(toJsValue(value)); return handle; }, array_pop: (handle) => { const arr = getHandle(handle); if (!arr || arr.length === 0) return u64ToF64(TAG_UNDEFINED); return fromJsValue(arr.pop()); }, array_get: (handle, index) => { const arr = getHandle(handle); if (!arr) return u64ToF64(TAG_UNDEFINED); const i = typeof index === 'number' ? index : toJsValue(index); return fromJsValue(arr[i]); }, array_set: (handle, index, value) => { const arr = getHandle(handle); if (arr) arr[typeof index === 'number' ? index : toJsValue(index)] = toJsValue(value); }, array_length: (handle) => { const arr = getHandle(handle); return arr ? arr.length : 0; }, array_slice: (handle, start, end) => { const arr = getHandle(handle); if (!arr) return nanboxPointer(allocHandle([])); const s = typeof start === 'number' ? start : 0; const e = isUndefined(end) ? undefined : (typeof end === 'number' ? end : toJsValue(end)); return nanboxPointer(allocHandle(arr.slice(s, e))); }, array_splice: (handle, start, deleteCount) => { const arr = getHandle(handle); if (!arr) return nanboxPointer(allocHandle([])); const s = typeof start === 'number' ? start : 0; const dc = isUndefined(deleteCount) ? arr.length - s : deleteCount; return nanboxPointer(allocHandle(arr.splice(s, dc))); }, array_shift: (handle) => { const arr = getHandle(handle); if (!arr || arr.length === 0) return u64ToF64(TAG_UNDEFINED); return fromJsValue(arr.shift()); }, array_unshift: (handle, value) => { const arr = getHandle(handle); if (arr) arr.unshift(toJsValue(value)); }, array_join: (handle, separator) => { const arr = getHandle(handle); if (!arr) return nanboxString(allocHandle('')); const sep = getString(separator); const result = arr.map(v => typeof v === 'object' && v !== null ? __JSON.stringify(v) : String(v)).join(sep); stringTable.push(result); return nanboxString(stringTable.length - 1); }, array_index_of: (handle, value) => { const arr = getHandle(handle); if (!arr) return -1; const v = toJsValue(value); return arr.indexOf(v); }, array_includes: (handle, value) => { const arr = getHandle(handle); if (!arr) return 0; return arr.includes(toJsValue(value)) ? 1 : 0; }, array_concat: (h1, h2) => { const a1 = getHandle(h1) || []; const a2 = getHandle(h2) || []; return nanboxPointer(allocHandle(a1.concat(a2))); }, array_reverse: (handle) => { const arr = getHandle(handle); if (arr) arr.reverse(); return handle; }, array_flat: (handle) => { const arr = getHandle(handle); return nanboxPointer(allocHandle(arr ? arr.flat() : [])); }, array_is_array: (val) => { if (!isPointer(val)) return 0; const obj = getHandle(val); return Array.isArray(obj) ? 1 : 0; }, array_from: (val) => { const v = toJsValue(val); return nanboxPointer(allocHandle(Array.from(v))); }, array_push_spread: (target, source) => { const t = getHandle(target); const s = getHandle(source); if (t && s) t.push(...s); return target; }, // ===== Phase 1: String methods ===== string_charAt: (str, idx) => { const s = stringTable[getStringId(str)]; stringTable.push(s.charAt(idx)); return nanboxString(stringTable.length - 1); }, string_substring: (str, start, end) => { const s = stringTable[getStringId(str)]; stringTable.push(s.substring(start, end)); return nanboxString(stringTable.length - 1); }, string_indexOf: (str, search) => { return stringTable[getStringId(str)].indexOf(stringTable[getStringId(search)]); }, string_slice: (str, start, end) => { const s = stringTable[getStringId(str)]; stringTable.push(s.slice(start, end)); return nanboxString(stringTable.length - 1); }, string_toLowerCase: (str) => { stringTable.push(stringTable[getStringId(str)].toLowerCase()); return nanboxString(stringTable.length - 1); }, string_toUpperCase: (str) => { stringTable.push(stringTable[getStringId(str)].toUpperCase()); return nanboxString(stringTable.length - 1); }, string_trim: (str) => { stringTable.push(stringTable[getStringId(str)].trim()); return nanboxString(stringTable.length - 1); }, string_includes: (str, search) => { return stringTable[getStringId(str)].includes(stringTable[getStringId(search)]) ? 1 : 0; }, string_startsWith: (str, search) => { return stringTable[getStringId(str)].startsWith(stringTable[getStringId(search)]) ? 1 : 0; }, string_endsWith: (str, search) => { return stringTable[getStringId(str)].endsWith(stringTable[getStringId(search)]) ? 1 : 0; }, string_replace: (str, pattern, replacement) => { const s = stringTable[getStringId(str)]; // pattern might be a regex handle or a string let p, r; if (isPointer(pattern)) { p = getHandle(pattern); // RegExp object } else { p = getString(pattern); } r = getString(replacement); stringTable.push(s.replace(p, r)); return nanboxString(stringTable.length - 1); }, string_split: (str, delim) => { const s = stringTable[getStringId(str)]; const d = getString(delim); return nanboxPointer(allocHandle(s.split(d))); }, string_fromCharCode: (code) => { stringTable.push(String.fromCharCode(code)); return nanboxString(stringTable.length - 1); }, string_padStart: (str, len, fill) => { const s = stringTable[getStringId(str)]; stringTable.push(s.padStart(len, getString(fill))); return nanboxString(stringTable.length - 1); }, string_padEnd: (str, len, fill) => { const s = stringTable[getStringId(str)]; stringTable.push(s.padEnd(len, getString(fill))); return nanboxString(stringTable.length - 1); }, string_repeat: (str, count) => { stringTable.push(stringTable[getStringId(str)].repeat(count)); return nanboxString(stringTable.length - 1); }, string_match: (str, regex) => { const s = stringTable[getStringId(str)]; const re = isPointer(regex) ? getHandle(regex) : new RegExp(getString(regex)); const result = s.match(re); if (!result) return u64ToF64(TAG_NULL); return nanboxPointer(allocHandle(Array.from(result))); }, math_log2: (x) => Math.log2(x), math_log10: (x) => Math.log10(x), // ===== Phase 2: Closures ===== // closure_new(func_table_idx, capture_count) -> handle closure_new: (funcIdx, captureCount) => { return nanboxPointer(allocHandle(new __PerryClosure(funcIdx, captureCount))); }, // closure_set_capture(handle, idx, value) -> handle (chaining) // value is NaN-boxed f64 — convert to BigInt for WASM i64 ABI closure_set_capture: (handle, idx, value) => { const c = getHandle(handle); if (c) c.captures[idx | 0] = f64ToU64(value); // store as BigInt for i64 WASM calls return handle; }, // closure_call_N(handle, args...) -> result // WASM functions use i64 (BigInt) params/returns. Captures are already BigInt. // Args (a0, a1, ...) are NaN-boxed f64 from the old rt: import ABI — convert via f64ToU64. // Return is BigInt from WASM — convert back to f64 via u64ToF64. closure_call_0: (handle) => { const c = getHandle(handle); if (!c || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(c.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); // Pad for omitted TS optional params (Issue #1035). const padded = __padBigintArgs(fn, c.captures.length, []); return u64ToF64(fn(...c.captures, ...padded)); }, closure_call_1: (handle, a0) => { const c = getHandle(handle); if (!c || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(c.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); const padded = __padBigintArgs(fn, c.captures.length, [f64ToU64(a0)]); return u64ToF64(fn(...c.captures, ...padded)); }, closure_call_2: (handle, a0, a1) => { const c = getHandle(handle); if (!c || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(c.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); const padded = __padBigintArgs(fn, c.captures.length, [f64ToU64(a0), f64ToU64(a1)]); return u64ToF64(fn(...c.captures, ...padded)); }, closure_call_3: (handle, a0, a1, a2) => { const c = getHandle(handle); if (!c || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(c.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); const padded = __padBigintArgs(fn, c.captures.length, [f64ToU64(a0), f64ToU64(a1), f64ToU64(a2)]); return u64ToF64(fn(...c.captures, ...padded)); }, // closure_call_spread(handle, args_array_handle) -> result closure_call_spread: (handle, argsHandle) => { const c = getHandle(handle); const args = getHandle(argsHandle) || []; if (!c || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(c.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); const padded = __padBigintArgs(fn, c.captures.length, args.map(v => f64ToU64(fromJsValue(v)))); return u64ToF64(fn(...c.captures, ...padded)); }, // ===== Phase 2: Array higher-order methods ===== // Helper: call a closure/wasm function with an element // The closure is a handle, the callback takes (element, index, array) array_map: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return nanboxPointer(allocHandle([])); const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return nanboxPointer(allocHandle([])); const result = arr.map((v, i) => { const r = __invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)); return __bitsToJsValue(r); }); return nanboxPointer(allocHandle(result)); }, array_filter: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return nanboxPointer(allocHandle([])); const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return nanboxPointer(allocHandle([])); const result = arr.filter((v, i) => { const r = __invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)); const rv = __bitsToJsValue(r); if (rv === true) return true; if (rv === false || rv === null || rv === undefined) return false; return !!rv; }); return nanboxPointer(allocHandle(result)); }, array_forEach: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return; const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return; arr.forEach((v, i) => __invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr))); }, array_reduce: (handle, cbHandle, initial) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); let acc = isUndefined(initial) ? __jsValueToBits(arr[0]) : f64ToU64(initial); const startIdx = isUndefined(initial) ? 1 : 0; for (let i = startIdx; i < arr.length; i++) { acc = __invokeWasmCallback(fn, ...cb.captures, acc, __jsValueToBits(arr[i]), __jsValueToBits(i), __jsValueToBits(arr)); } return u64ToF64(acc); }, array_find: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return u64ToF64(TAG_UNDEFINED); const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return u64ToF64(TAG_UNDEFINED); for (let i = 0; i < arr.length; i++) { const vBits = __jsValueToBits(arr[i]); const r = __invokeWasmCallback(fn, ...cb.captures, vBits, __jsValueToBits(i), __jsValueToBits(arr)); if (__bitsToJsValue(r)) return fromJsValue(arr[i]); } return u64ToF64(TAG_UNDEFINED); }, array_find_index: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return -1; const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return -1; for (let i = 0; i < arr.length; i++) { const r = __invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(arr[i]), __jsValueToBits(i), __jsValueToBits(arr)); if (__bitsToJsValue(r)) return i; } return -1; }, array_sort: (handle, cbHandle) => { const arr = getHandle(handle); if (!arr) return handle; if (isUndefined(cbHandle) || isNull(cbHandle)) { arr.sort(); return handle; } const cb = getHandle(cbHandle); if (!cb || !wasmInstance) { arr.sort(); return handle; } const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) { arr.sort(); return handle; } arr.sort((a, b) => { const r = __invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(a), __jsValueToBits(b)); return __bitsToJsValue(r); }); return handle; }, array_some: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return 0; const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return 0; return arr.some((v, i) => __bitsToJsValue(__invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)))) ? 1 : 0; }, array_every: (handle, cbHandle) => { const arr = getHandle(handle); const cb = getHandle(cbHandle); if (!arr || !cb || !wasmInstance) return 0; const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (!fn) return 0; return arr.every((v, i) => __bitsToJsValue(__invokeWasmCallback(fn, ...cb.captures, __jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)))) ? 1 : 0; }, // ===== Phase 3: Classes ===== // Class registry: class_name -> { methods: Map, statics: Map, parent: string|null } // class_new(class_name_str, field_count) -> handle class_new: (classNameVal, fieldCount) => { return nanboxPointer(allocHandle(__classInstance(getString(classNameVal)))); }, // class_set_method(class_id_str, method_name_str, func_table_idx, proto_kind) class_set_method: (classId, methodName, funcIdx, protoKind) => { const cls = getString(classId); const name = getString(methodName); if (!classMethodTable[cls]) classMethodTable[cls] = {}; classMethodTable[cls][name] = new __FnInfo(name, 0, protoKind, funcIdx); }, // class_call_method(handle, method_name_id, args_array_handle) -> result // method_name_id is a plain f64 string table index (not NaN-boxed). // WASM functions use i64 (BigInt) params/returns. class_call_method: (handle, methodNameId, argsHandle) => { const obj = getHandle(handle); const mname = stringTable[methodNameId | 0]; // Try class method dispatch first if (obj && __classOf(obj)) { let cls = __classOf(obj); while (cls) { const methods = classMethodTable[cls]; if (methods && mname in methods) { const fn = wasmInstance?.exports.__indirect_function_table?.get(methods[mname].funcIdx); if (fn) { const args = getHandle(argsHandle) || []; // Pad for omitted TS optional params (Issue #1035). const padded = __padBigintArgs(fn, 1, args.map(v => f64ToU64(fromJsValue(v)))); return u64ToF64(fn(f64ToU64(handle), ...padded)); } } cls = classParentTable[cls] || null; } } // Fallback: UI widget method dispatch // Map common method names to perry_ui_* bridge functions const uiMethodMap = { addChild: "perry_ui_widget_add_child", removeAllChildren: "perry_ui_widget_remove_all_children", setBackground: "perry_ui_set_background", setForeground: "perry_ui_set_foreground", setFontSize: "perry_ui_set_font_size", setFontWeight: "perry_ui_set_font_weight", setFontFamily: "perry_ui_set_font_family", setPadding: "perry_ui_set_padding", setFrame: "perry_ui_set_frame", setCornerRadius: "perry_ui_set_corner_radius", setBorder: "perry_ui_set_border", setOpacity: "perry_ui_set_opacity", setEnabled: "perry_ui_set_enabled", setTooltip: "perry_ui_set_tooltip", setControlSize: "perry_ui_set_control_size", animateOpacity: "perry_ui_animate_opacity", animatePosition: "perry_ui_animate_position", setOnClick: "perry_ui_set_on_click", setOnHover: "perry_ui_set_on_hover", setOnDoubleClick: "perry_ui_set_on_double_click", // State methods get: "perry_ui_state_get", set: "perry_ui_state_set", create: "perry_ui_state_create", bindText: "perry_ui_state_bind_text", bindTextNumeric: "perry_ui_state_bind_text_numeric", bindSlider: "perry_ui_state_bind_slider", bindToggle: "perry_ui_state_bind_toggle", bindVisibility: "perry_ui_state_bind_visibility", bindForEach: "perry_ui_state_bind_foreach", onChange: "perry_ui_state_on_change", // Text/Button ops setString: "perry_ui_text_set_string", setSelectable: "perry_ui_text_set_selectable", setBordered: "perry_ui_button_set_bordered", setTitle: "perry_ui_button_set_title", setTextColor: "perry_ui_button_set_text_color", setImage: "perry_ui_button_set_image", // Canvas fillRect: "perry_ui_canvas_fill_rect", strokeRect: "perry_ui_canvas_stroke_rect", clearRect: "perry_ui_canvas_clear_rect", fillGradient: "perry_ui_canvas_fill_gradient", setFillColor: "perry_ui_canvas_set_fill_color", setStrokeColor: "perry_ui_canvas_set_stroke_color", beginPath: "perry_ui_canvas_begin_path", moveTo: "perry_ui_canvas_move_to", lineTo: "perry_ui_canvas_line_to", arc: "perry_ui_canvas_arc", closePath: "perry_ui_canvas_close_path", fill: "perry_ui_canvas_fill", stroke: "perry_ui_canvas_stroke", setLineWidth: "perry_ui_canvas_set_line_width", fillText: "perry_ui_canvas_fill_text", setFont: "perry_ui_canvas_set_font", drawImage: "perry_ui_canvas_draw_image", // ScrollView setChild: "perry_ui_scrollview_set_child", // TextField focus: "perry_ui_textfield_focus", // Widget sizing setWidth: "perry_ui_widget_set_width", setHeight: "perry_ui_widget_set_height", matchParentWidth: "perry_ui_widget_match_parent_width", matchParentHeight: "perry_ui_widget_match_parent_height", setHidden: "perry_ui_set_widget_hidden", setEdgeInsets: "perry_ui_widget_set_edge_insets", setShadow: "perry_ui_widget_set_shadow", // App run: "perry_ui_app_run", setBody: "perry_ui_app_set_body", }; const uiFnName = uiMethodMap[mname]; if (uiFnName) { const fn = __perryUiDispatch[uiFnName]; if (fn) { const args = getHandle(argsHandle) || []; // First arg to the UI function is the handle/object, rest are the call args const objVal = toJsValue(handle); // Check if any args are closures (POINTER_TAG) and keep them raw for callWasmClosure const closureMethods = new Set(["setOnClick","setOnHover","setOnDoubleClick","onChange","bindForEach","set"]); if (closureMethods.has(mname)) { const jsArgs = args.map(v => { const bits = f64ToU64(v); if ((bits >> 48n) === POINTER_TAG) return v; return toJsValue(v); }); return fromJsValue(fn(objVal, ...jsArgs)); } const jsArgs = args.map(v => toJsValue(v)); return fromJsValue(fn(objVal, ...jsArgs)); } } return u64ToF64(TAG_UNDEFINED); }, class_get_field: (handle, name) => { const obj = getHandle(handle); if (!obj) return u64ToF64(TAG_UNDEFINED); const fname = getString(name); return fromJsValue(obj[fname]); }, class_set_field: (handle, name, value) => { const obj = getHandle(handle); if (!obj) return; const fname = getString(name); obj[fname] = toJsValue(value); }, class_set_static: (classId, name, value) => { const cls = getString(classId); if (!classStaticTable[cls]) classStaticTable[cls] = {}; classStaticTable[cls][getString(name)] = toJsValue(value); }, class_get_static: (classId, name) => { const cls = getString(classId); const statics = classStaticTable[cls]; if (!statics) return u64ToF64(TAG_UNDEFINED); return fromJsValue(statics[getString(name)]); }, class_instanceof: (handle, classId) => { const obj = getHandle(handle); if (!obj) return 0; let cls = __classOf(obj); const target = getString(classId); while (cls) { if (cls === target) return 1; cls = classParentTable[cls] || null; } return 0; }, // ===== Phase 4: JSON ===== json_parse: (str) => { try { const s = getString(str); return fromJsValue(JSON.parse(s)); } catch (e) { if (tryDepth > 0) { currentException = fromJsValue(e); } return u64ToF64(TAG_UNDEFINED); } }, json_stringify: (val) => { const js = toJsValue(val); stringTable.push(JSON.stringify(js)); return nanboxString(stringTable.length - 1); }, // ===== Phase 4: Map ===== map_new: () => nanboxPointer(allocHandle(new Map())), map_set: (handle, key, value) => { const m = getHandle(handle); if (m) m.set(toJsValue(key), toJsValue(value)); }, map_get: (handle, key) => { const m = getHandle(handle); if (!m) return u64ToF64(TAG_UNDEFINED); return fromJsValue(m.get(toJsValue(key))); }, map_has: (handle, key) => { const m = getHandle(handle); return m?.has(toJsValue(key)) ? 1 : 0; }, map_delete: (handle, key) => { const m = getHandle(handle); if (m) m.delete(toJsValue(key)); }, map_size: (handle) => { const m = getHandle(handle); return m ? m.size : 0; }, map_clear: (handle) => { const m = getHandle(handle); if (m) m.clear(); }, map_entries: (handle) => { const m = getHandle(handle); return nanboxPointer(allocHandle(m ? [...m.entries()] : [])); }, map_keys: (handle) => { const m = getHandle(handle); return nanboxPointer(allocHandle(m ? [...m.keys()] : [])); }, map_values: (handle) => { const m = getHandle(handle); return nanboxPointer(allocHandle(m ? [...m.values()] : [])); }, // ===== Phase 4: Set ===== set_new: () => nanboxPointer(allocHandle(new Set())), set_new_from_array: (arrHandle) => { const arr = getHandle(arrHandle) || toJsValue(arrHandle); return nanboxPointer(allocHandle(new Set(Array.isArray(arr) ? arr : []))); }, set_add: (handle, value) => { const s = getHandle(handle); if (s) s.add(toJsValue(value)); }, set_has: (handle, value) => { const s = getHandle(handle); return s?.has(toJsValue(value)) ? 1 : 0; }, set_delete: (handle, value) => { const s = getHandle(handle); if (s) s.delete(toJsValue(value)); }, set_size: (handle) => { const s = getHandle(handle); return s ? s.size : 0; }, set_clear: (handle) => { const s = getHandle(handle); if (s) s.clear(); }, set_values: (handle) => { const s = getHandle(handle); return nanboxPointer(allocHandle(s ? [...s.values()] : [])); }, // ===== Phase 4: Date ===== date_new_val: (arg) => { const d = isUndefined(arg) ? new Date() : new Date(toJsValue(arg)); return nanboxPointer(allocHandle(d)); }, date_get_time: (handle) => { const d = getHandle(handle); return d instanceof Date ? d.getTime() : 0; }, date_to_iso_string: (handle) => { const d = getHandle(handle); if (!(d instanceof Date)) return u64ToF64(TAG_UNDEFINED); stringTable.push(d.toISOString()); return nanboxString(stringTable.length - 1); }, date_get_full_year: (h) => { const d = getHandle(h); return d instanceof Date ? d.getFullYear() : 0; }, date_get_month: (h) => { const d = getHandle(h); return d instanceof Date ? d.getMonth() : 0; }, date_get_date: (h) => { const d = getHandle(h); return d instanceof Date ? d.getDate() : 0; }, date_get_day: (h) => { const d = getHandle(h); return d instanceof Date ? d.getDay() : 0; }, date_get_hours: (h) => { const d = getHandle(h); return d instanceof Date ? d.getHours() : 0; }, date_get_minutes: (h) => { const d = getHandle(h); return d instanceof Date ? d.getMinutes() : 0; }, date_get_seconds: (h) => { const d = getHandle(h); return d instanceof Date ? d.getSeconds() : 0; }, date_get_milliseconds: (h) => { const d = getHandle(h); return d instanceof Date ? d.getMilliseconds() : 0; }, // ===== Phase 4: Error ===== error_new: (msg) => { const message = isUndefined(msg) ? undefined : getString(msg); return nanboxPointer(allocHandle(new Error(message))); }, error_message: (handle) => { const e = getHandle(handle); const msg = e instanceof Error ? e.message : ''; stringTable.push(msg); return nanboxString(stringTable.length - 1); }, // ===== Phase 4: RegExp ===== regexp_new: (pattern, flags) => { try { const p = getString(pattern); const f = getString(flags); return nanboxPointer(allocHandle(new RegExp(p, f))); } catch (e) { if (tryDepth > 0) { currentException = fromJsValue(e); } return u64ToF64(TAG_UNDEFINED); } }, regexp_test: (regex, str) => { const re = getHandle(regex); if (!(re instanceof RegExp)) return 0; return re.test(getString(str)) ? 1 : 0; }, // ===== Phase 4: Globals ===== number_coerce: (val) => { const v = toJsValue(val); return Number(v); }, is_nan: (val) => Number.isNaN(val) ? 1 : 0, is_finite: (val) => Number.isFinite(val) ? 1 : 0, // ===== Phase 5: Misc ===== console_log_multi: (argsHandle) => { const args = getHandle(argsHandle); if (args) console.log(...args.map(toJsValue)); }, // ===== Phase 1 Addition: Class inheritance ===== class_set_parent: (childId, parentId) => { const child = getString(childId); const parent = getString(parentId); classParentTable[child] = parent; }, // ===== Phase 3: Try/Catch ===== try_start: () => { tryDepth++; }, try_end: () => { if (tryDepth > 0) tryDepth--; }, throw_value: (val) => { currentException = val; }, has_exception: () => currentException !== NO_EXCEPTION ? 1 : 0, get_exception: () => { const e = currentException; currentException = NO_EXCEPTION; return e !== NO_EXCEPTION ? e : u64ToF64(TAG_UNDEFINED); }, // ===== Phase 4: URL ===== url_parse: (urlStr) => { try { return nanboxPointer(allocHandle(new URL(getString(urlStr)))); } catch (e) { if (tryDepth > 0) { currentException = fromJsValue(e); } return u64ToF64(TAG_UNDEFINED); } }, url_get_href: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.href); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_pathname: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.pathname); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_hostname: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.hostname); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_port: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.port); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_search: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.search); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_hash: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.hash); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_origin: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.origin); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_protocol: (handle) => { const u = getHandle(handle); if (u instanceof URL) { stringTable.push(u.protocol); return nanboxString(stringTable.length - 1); } return u64ToF64(TAG_UNDEFINED); }, url_get_search_params: (handle) => { const u = getHandle(handle); if (u instanceof URL) return nanboxPointer(allocHandle(u.searchParams)); return u64ToF64(TAG_UNDEFINED); }, searchparams_get: (handle, key) => { const sp = getHandle(handle); if (!sp || typeof sp.get !== 'function') return u64ToF64(TAG_NULL); const v = sp.get(getString(key)); if (v === null) return u64ToF64(TAG_NULL); stringTable.push(v); return nanboxString(stringTable.length - 1); }, searchparams_has: (handle, key) => { const sp = getHandle(handle); return (sp && typeof sp.has === 'function' && sp.has(getString(key))) ? 1 : 0; }, searchparams_set: (handle, key, val) => { const sp = getHandle(handle); if (sp && typeof sp.set === 'function') sp.set(getString(key), getString(val)); }, searchparams_append: (handle, key, val) => { const sp = getHandle(handle); if (sp && typeof sp.append === 'function') sp.append(getString(key), getString(val)); }, searchparams_delete: (handle, key) => { const sp = getHandle(handle); if (sp && typeof sp.delete === 'function') sp.delete(getString(key)); }, searchparams_to_string: (handle) => { const sp = getHandle(handle); stringTable.push(sp ? sp.toString() : ''); return nanboxString(stringTable.length - 1); }, // ===== Phase 4: Crypto ===== crypto_random_uuid: () => { stringTable.push(crypto.randomUUID()); return nanboxString(stringTable.length - 1); }, crypto_random_bytes: (n) => { return nanboxPointer(allocHandle(crypto.getRandomValues(new Uint8Array(n)))); }, // ===== Phase 4: Path ===== path_join: (a, b) => { const sa = getString(a), sb = getString(b); const joined = (sa + '/' + sb).replace(/\/+/g, '/'); stringTable.push(joined); return nanboxString(stringTable.length - 1); }, path_dirname: (str) => { const s = getString(str); const idx = s.lastIndexOf('/'); stringTable.push(idx >= 0 ? (s.substring(0, idx) || '/') : '.'); return nanboxString(stringTable.length - 1); }, path_basename: (str) => { const s = getString(str); const idx = s.lastIndexOf('/'); stringTable.push(idx >= 0 ? s.substring(idx + 1) : s); return nanboxString(stringTable.length - 1); }, path_extname: (str) => { const s = getString(str); const base = s.substring(s.lastIndexOf('/') + 1); const idx = base.lastIndexOf('.'); stringTable.push(idx > 0 ? base.substring(idx) : ''); return nanboxString(stringTable.length - 1); }, path_resolve: (str) => { stringTable.push(getString(str)); return nanboxString(stringTable.length - 1); }, // ===== Phase 4: Process/OS ===== os_platform: () => { stringTable.push('wasm'); return nanboxString(stringTable.length - 1); }, process_argv: () => nanboxPointer(allocHandle([])), process_cwd: () => { stringTable.push('/'); return nanboxString(stringTable.length - 1); }, // ===== Phase 6: Buffer/Uint8Array ===== buffer_alloc: (size) => nanboxPointer(allocHandle(new Uint8Array(size))), buffer_from_string: (str, encoding) => { const s = getString(str); return nanboxPointer(allocHandle(new TextEncoder().encode(s))); }, buffer_to_string: (handle, encoding) => { const buf = getHandle(handle); if (!buf) { stringTable.push(''); return nanboxString(stringTable.length - 1); } stringTable.push(new TextDecoder().decode(buf)); return nanboxString(stringTable.length - 1); }, buffer_get: (handle, idx) => { const buf = getHandle(handle); return buf ? buf[idx] : 0; }, buffer_set: (handle, idx, val) => { const buf = getHandle(handle); if (buf) buf[idx] = val; }, buffer_length: (handle) => { const buf = getHandle(handle); return buf ? buf.length : 0; }, buffer_slice: (handle, start, end) => { const buf = getHandle(handle); if (!buf) return nanboxPointer(allocHandle(new Uint8Array(0))); const e = isUndefined(end) ? undefined : end; return nanboxPointer(allocHandle(buf.slice(start, e))); }, buffer_concat: (arrHandle) => { const arr = getHandle(arrHandle); if (!arr || !Array.isArray(arr)) return nanboxPointer(allocHandle(new Uint8Array(0))); const bufs = arr.map(v => { const h = typeof v === 'number' ? getHandle(v) : v; return h instanceof Uint8Array ? h : new Uint8Array(0); }); const total = bufs.reduce((s, b) => s + b.length, 0); const result = new Uint8Array(total); let offset = 0; bufs.forEach(b => { result.set(b, offset); offset += b.length; }); return nanboxPointer(allocHandle(result)); }, uint8array_new: (size) => nanboxPointer(allocHandle(new Uint8Array(size))), uint8array_from: (val) => { const v = toJsValue(val); return nanboxPointer(allocHandle(Uint8Array.from(Array.isArray(v) ? v : []))); }, uint8array_length: (handle) => { const buf = getHandle(handle); return buf ? buf.length : 0; }, uint8array_get: (handle, idx) => { const buf = getHandle(handle); return buf ? buf[idx] : 0; }, uint8array_set: (handle, idx, val) => { const buf = getHandle(handle); if (buf) buf[idx] = val; }, // ===== Timers ===== set_timeout: (closureHandle, delay) => { const cb = getHandle(closureHandle); if (!cb || !wasmInstance) return 0; const id = setTimeout(() => { const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (fn) fn(...__padBigintArgs(fn, 0, [...cb.captures])); // captures are already BigInt }, delay); return id; }, set_interval: (closureHandle, delay) => { const cb = getHandle(closureHandle); if (!cb || !wasmInstance) return 0; const id = setInterval(() => { const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (fn) fn(...__padBigintArgs(fn, 0, [...cb.captures])); // captures are already BigInt }, delay); return id; }, clear_timeout: (id) => { clearTimeout(id); }, clear_interval: (id) => { clearInterval(id); }, // ===== Response properties ===== response_status: (handle) => { const r = getHandle(handle); return (r && typeof r.status === 'number') ? r.status : 0; }, response_ok: (handle) => { const r = getHandle(handle); return (r && r.ok) ? 1 : 0; }, response_headers_get: (handle, name) => { const r = getHandle(handle); if (!r || !r.headers) return u64ToF64(TAG_NULL); const v = r.headers.get(getString(name)); if (v === null) return u64ToF64(TAG_NULL); stringTable.push(v); return nanboxString(stringTable.length - 1); }, response_url: (handle) => { const r = getHandle(handle); if (!r || typeof r.url !== 'string') return u64ToF64(TAG_UNDEFINED); stringTable.push(r.url); return nanboxString(stringTable.length - 1); }, // ===== Buffer extras ===== buffer_copy: (source, target, targetStart, sourceStart, sourceEnd) => { const src = getHandle(source); const tgt = getHandle(target); if (!src || !tgt) return 0; const ts = isUndefined(targetStart) ? 0 : targetStart; const ss = isUndefined(sourceStart) ? 0 : sourceStart; const se = isUndefined(sourceEnd) ? src.length : sourceEnd; let copied = 0; for (let i = ss; i < se && (ts + copied) < tgt.length; i++) { tgt[ts + copied] = src[i]; copied++; } return copied; }, buffer_write: (handle, str, offset, encoding) => { const buf = getHandle(handle); if (!buf) return 0; const s = getString(str); const encoded = new TextEncoder().encode(s); const off = isUndefined(offset) ? 0 : offset; let written = 0; for (let i = 0; i < encoded.length && (off + i) < buf.length; i++) { buf[off + i] = encoded[i]; written++; } return written; }, buffer_equals: (handle, other) => { const a = getHandle(handle); const b = getHandle(other); if (!a || !b) return 0; if (a.length !== b.length) return 0; for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) return 0; } return 1; }, buffer_is_buffer: (val) => { if (!isPointer(val)) return 0; const obj = getHandle(val); return (obj instanceof Uint8Array || (obj && obj.constructor && obj.constructor.name === 'Buffer')) ? 1 : 0; }, buffer_byte_length: (val) => { if (isString(val)) return new TextEncoder().encode(getString(val)).length; if (isPointer(val)) { const obj = getHandle(val); if (obj instanceof Uint8Array) return obj.length; } return 0; }, // ===== Crypto extras ===== crypto_sha256: (data) => { const str = getString(data); const p = (typeof crypto !== 'undefined' && crypto.subtle) ? crypto.subtle.digest('SHA-256', new TextEncoder().encode(str)) .then(buf => { const hex = [...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join(''); stringTable.push(hex); return nanboxString(stringTable.length - 1); }) : Promise.resolve(u64ToF64(TAG_UNDEFINED)); return nanboxPointer(allocHandle(p)); }, crypto_md5: (data) => { // MD5 not available in Web Crypto API; return undefined // Users should use SHA-256 instead return u64ToF64(TAG_UNDEFINED); }, // ===== Path extras ===== path_is_absolute: (str) => { const s = getString(str); return (s.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(s)) ? 1 : 0; }, // ===== Phase 5: Async/Promise/Fetch ===== fetch_url: (urlStr) => { const url = getString(urlStr); const p = fetch(url); return nanboxPointer(allocHandle(p)); }, fetch_with_options: (urlStr, methodVal, bodyVal, headersVal, redirectVal) => { const url = getString(urlStr); const opts = {}; if (!isUndefined(methodVal)) opts.method = getString(methodVal); if (!isUndefined(bodyVal)) opts.body = getString(bodyVal); if (isPointer(headersVal)) { const h = getHandle(headersVal); if (h && typeof h === 'object') opts.headers = h; } if (!isUndefined(redirectVal)) opts.redirect = getString(redirectVal); const p = fetch(url, opts); return nanboxPointer(allocHandle(p)); }, response_json: (handle) => { const resp = getHandle(handle); if (!resp || typeof resp.json !== 'function') return u64ToF64(TAG_UNDEFINED); const p = resp.json().then(v => fromJsValue(v)); return nanboxPointer(allocHandle(p)); }, response_text: (handle) => { const resp = getHandle(handle); if (!resp || typeof resp.text !== 'function') return u64ToF64(TAG_UNDEFINED); const p = resp.text().then(v => fromJsValue(v)); return nanboxPointer(allocHandle(p)); }, promise_new: () => { let resolve; const p = new Promise(r => { resolve = r; }); p.__resolve = resolve; return nanboxPointer(allocHandle(p)); }, promise_resolve: (handle, value) => { const p = getHandle(handle); if (p && p.__resolve) p.__resolve(toJsValue(value)); }, promise_then: (handle, closureHandle) => { const p = getHandle(handle); const cb = getHandle(closureHandle); if (!p || !(p instanceof Promise)) return handle; const newP = p.then(val => { if (cb && wasmInstance) { const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); if (fn) return u64ToF64(fn(...__padBigintArgs(fn, 0, [...cb.captures, __jsValueToBits(val)]))); } return fromJsValue(val); }); return nanboxPointer(allocHandle(newP)); }, await_promise: (val) => { // In synchronous WASM context, we can't truly await. // If it's not a promise handle, return as-is. // If it IS a promise, return the handle (caller gets a promise handle back). // True awaiting happens in JS async functions. if (!isPointer(val)) return val; const obj = getHandle(val); if (obj instanceof Promise) { // Can't await synchronously; return the promise handle return val; } return val; }, // Memory-based bridge: ALL bridge function calls go through here. // Args are written to WASM memory at 0xFF00 as raw f64, preserving NaN payloads // that Firefox would canonicalize if passed as function parameters. // nameId = plain f64 string table index, argCount = number of f64 slots written. // Returns dummy f64 (0.0). Actual result is written to memory at 0xFF00. mem_call: (nameId, argCount, baseAddr) => { const name = stringTable[nameId | 0]; const argc = argCount | 0; const base = baseAddr | 0; const u64View = new BigUint64Array(wasmMemory.buffer, base, __Math.max(argc, 1)); const args = []; for (let i = 0; i < argc; i++) args.push(__bitsToJsValue(u64View[i])); let result; const coreFn = __memDispatch[name]; try { if (coreFn) { result = coreFn(...args); } else { const uiFn = __perryUiDispatch[name]; if (uiFn) { result = uiFn(...args); } else if (argc > 0) { result = __classDispatch(args[0], name, args.slice(1)); } } } catch (error) { if (tryDepth === 0) throw error; currentException = error; } // Write result back to same base address const outU64 = new BigUint64Array(wasmMemory.buffer, base, 1); outU64[0] = __jsValueToBits(result); return 0; }, mem_call_i32: (nameId, argCount, baseAddr) => { const name = stringTable[nameId | 0]; const argc = argCount | 0; const base = baseAddr | 0; const u64View = new BigUint64Array(wasmMemory.buffer, base, __Math.max(argc, 1)); const args = []; for (let i = 0; i < argc; i++) args.push(__bitsToJsValue(u64View[i])); const fn = __memDispatch[name]; if (!fn) return 0; try { return fn(...args) | 0; } catch (error) { if (tryDepth === 0) throw error; currentException = error; return 0; } }, // Async function implementations (merged from generated code) ...(typeof __asyncFuncImpls !== 'undefined' ? __asyncFuncImpls : {}), }, // FFI namespace: external native functions (e.g., bloom_init_window, bloom_draw_rect) // Wrapped in Proxy so missing imports are auto-stubbed with no-ops returning // NaN-boxed undefined as BigInt, allowing the WASM module to instantiate. ffi: new Proxy(typeof __ffiImports !== 'undefined' ? __ffiImports : {}, { get(target, prop) { if (prop in target) return target[prop]; return function ffiStub() { return TAG_UNDEFINED; }; }, }), }; } // rt-namespace imports whose WASM-declared return type is NOT i64. // `wrapForI64` MUST NOT BigInt-coerce Number returns for these — the // WASM caller expects an i32 or f64, and a BigInt return throws // "Cannot convert a BigInt value to a number" at the import boundary // (#1049 instances 2/3). Built from the type tables in `emit.rs`: // every entry with a `t_*_i32` signature plus the f64-return `mem_call`. const __NON_I64_RETURN_IMPORTS = new Set([ // i32 return 'string_eq', 'is_truthy', 'js_strict_eq', 'is_null_or_undefined', 'object_has_property', 'array_includes', 'array_is_array', 'string_includes', 'string_startsWith', 'string_endsWith', 'array_some', 'array_every', 'class_instanceof', 'map_has', 'set_has', 'regexp_test', 'is_nan', 'is_finite', 'has_exception', 'searchparams_has', 'response_ok', 'buffer_equals', 'buffer_is_buffer', 'path_is_absolute', // f64 return — dummy 0, real result is in WASM memory 'mem_call', // i32-return memory-bridge 'mem_call_i32', // void return — wrapper passes through `undefined`, but listing here // makes the contract explicit and guards against future changes to the // wrapper that might try to coerce `0` for a void callee. 'string_new', 'console_log', 'console_warn', 'console_error', 'console_log_multi', 'object_delete', 'object_delete_dynamic', 'array_unshift', 'array_forEach', 'try_start', 'try_end', 'throw_value', 'map_clear', 'map_delete', 'set_clear', 'set_add', 'set_delete', 'searchparams_set', 'searchparams_append', 'searchparams_delete', 'class_set_method', 'class_set_field', 'class_set_static', 'class_set_parent', 'object_set_dynamic', 'array_set', 'buffer_set', 'buffer_copy', 'buffer_write', 'uint8array_set', 'set_timeout', 'set_interval', 'clear_timeout', 'clear_interval', 'promise_resolve', ]); // Wrap a JS function so it interoperates with WASM i64 params/results. // // Default wrapping (used for the rt namespace): convert BigInt i64 args to f64 // via bit-reinterpret, so legacy rt.* functions that expect NaN-boxed f64 keep // working. Number results are coerced back to BigInt for WASM i64 return. // Necessary because BigInt(NaN) throws. Escape hatch: set `fn.__rawBigint = true` // on the callee to skip the wrapper entirely and receive raw BigInt bits. // // `coerceNumberToBigInt` (default true) is set to false at the wrap site for // imports whose declared WASM return is `i32`/`f64`/void — see // `__NON_I64_RETURN_IMPORTS`. Without this guard, #1049 instance 1's // `BigInt(result)` would mis-encode every i32-return import (is_truthy, // js_strict_eq, mem_call_i32, …) and the f64-return `mem_call`, throwing // "Cannot convert a BigInt value to a number" on the very first call // (instances 2/3 reproduce as soon as `_start` invokes `class_set_method` // via `mem_call`). // An import argument's i64 bits as the f64 the runtime reads them as. function __i64ArgToF64(a) { if (typeof a !== 'bigint') return a; _u64[0] = a; return _f64[0]; } function wrapForI64(fn, coerceNumberToBigInt = true) { if (fn.__rawBigint === true) return fn; // The converted arguments go to `fn` as call arguments, never through an array // of numbers: V8 may store such an array as doubles and silence a NaN's payload // there, which turned a NaN-boxed string argument into NaN once the page was hot. // The arrays for longer calls hold `undefined` first, so they never become doubles. // WebAssembly calls an import without a receiver, so `fn` needs none. return function(...args) { const c = __i64ArgToF64; let result; switch (args.length) { case 0: result = fn(); break; case 1: result = fn(c(args[0])); break; case 2: result = fn(c(args[0]), c(args[1])); break; case 3: result = fn(c(args[0]), c(args[1]), c(args[2])); break; case 4: result = fn(c(args[0]), c(args[1]), c(args[2]), c(args[3])); break; default: { const convertedArgs = new Array(args.length).fill(undefined); for (let i = 0; i < args.length; i++) convertedArgs[i] = c(args[i]); result = __reflectApply(fn, undefined, convertedArgs); } } if (typeof result === 'bigint') return result; if (typeof result === 'number') { // #1049 instance 1: this wrapper exists because most callees here are // imported from a WASM site declared `i64`. A plain Number return — // even a small integer that JS would happily round-trip — throws // `TypeError: Cannot convert X to a BigInt` at the import boundary. // Coerce integers via `BigInt(n)` and non-integers (NaN-boxed f64 // payloads) via bit-reinterpret. Skipped for non-i64-return imports // (#1049 instances 2/3) — those declare i32/f64 returns and would // reject a BigInt with the inverse error. if (coerceNumberToBigInt && Number.isInteger(result) && __Math.abs(result) < 2147483648) { return BigInt(result); } if (!coerceNumberToBigInt) return result; _f64[0] = result; return _u64[0]; } return result; }; } // Wrapper for the ffi namespace: FFI imports are wasm-bindgen-generated functions // and similar externals that expect PLAIN JS values, not NaN-boxed f64. Decode // BigInt i64 args (NaN-box bits) to JS values via __bitsToJsValue before calling. // Fixes Issue #133 items 3/5/6: Firefox/Safari canonicalize foreign NaNs when a // Number crosses a JS function-call boundary, stripping STRING/POINTER/INT32 tags, // so bit-reinterpreting BigInt → f64 here would lose the payload. Decoding bypasses // the f64 round-trip entirely and also gives wasm-bindgen a plain Number for // INT32-tagged values (avoiding `Cannot convert BigInt value to a number`). function wrapFfiForI64(fn) { if (fn.__rawBigint === true) return fn; return function(...args) { const convertedArgs = args.map(a => (typeof a === 'bigint') ? __bitsToJsValue(a) : a); const result = __reflectApply(fn, this, convertedArgs); if (typeof result === 'bigint') return result; if (typeof result === 'number') { // #1049 instance 1: the WASM import side declares these as `i64` // returns (handles, NaN-box bits, etc.). A plain Number return — // even a small integer JS could round-trip — throws // `TypeError: Cannot convert X to a BigInt` at the import // boundary. NaN-box the Number via f64 bit-reinterpret for ALL // integers (not just non-integers): a raw `BigInt(1)` round-trips // as i64=1 which Perry then mis-reads as f64=5e-324 inside the WASM // body (#1049 instance 3 with `add_numbers` returning 7 surfacing // as `3.5e-323`). NaN-boxing via __jsValueToBits encodes the value // as a proper JSValue so downstream WASM code that reinterprets // i64 → f64 sees the original Number. return __jsValueToBits(result); } // Non-numeric result: encode via __jsValueToBits so callers that thread results // back into Perry get proper NaN-box bits. if (result === undefined) return TAG_UNDEFINED; return __jsValueToBits(result); }; } function wrapNamespace(ns, wrapper) { return new Proxy(ns, { get(target, prop) { const v = target[prop]; if (typeof v === 'function') return wrapper(v); return v; }, }); } // rt-namespace Proxy: pass each import's name into `wrapForI64` so it knows // whether to BigInt-coerce Number returns. Names listed in // `__NON_I64_RETURN_IMPORTS` have a non-i64 WASM-declared return (#1049 // instances 2/3) and must pass Numbers through unchanged. function wrapRtNamespace(ns) { return new Proxy(ns, { get(target, prop) { const v = target[prop]; if (typeof v === 'function') { const coerce = !__NON_I64_RETURN_IMPORTS.has(prop); return wrapForI64(v, coerce); } return v; }, }); } function wrapImportsForI64(imports) { const wrapped = {}; for (const nsName in imports) { if (nsName === 'ffi') { wrapped[nsName] = wrapNamespace(imports[nsName], wrapFfiForI64); } else { wrapped[nsName] = wrapRtNamespace(imports[nsName]); } } return wrapped; } // The key of a cell's dead-zone mark: a symbol, which no property of a program's can be. const __tdz = Symbol('perry.tdz'); function __throwDeadZone(name) { throw new ReferenceError(`Cannot access '${name}' before initialization`); } // What closure_new makes. Its class, not its keys, tells a closure from a plain // object that has `funcIdx` / `captures` fields, at no per-closure cost. class __PerryClosure { // A brand, not `instanceof`: an object whose prototype chain reaches a closure is no closure. #brand; static is(v) { return typeof v === 'object' && v !== null && #brand in v; } // Its function's `__FnInfo`: private, so no program property can change it. #info; // Non-enumerable, so `for...in` and `Object.keys` see only a closure's own properties. constructor(funcIdx, captureCount, info) { __defineHidden(this, 'funcIdx', funcIdx); __defineHidden(this, 'captures', new Array(captureCount | 0)); this.#info = info; } static protoKindOf(closure) { return #info in closure ? closure.#info?.protoKind ?? 0 : 0; } // A closure without a record (none was emitted for its function) keeps `new`. static isConstructor(closure) { return #info in closure ? closure.#info?.constructible ?? true : false; } // A closure reads its function's name and length from the record it points at. get name() { return #info in this ? this.#info?.name : undefined; } get length() { return #info in this ? this.#info?.length : undefined; } } // A compiled function's own facts. `fn_info` makes a declaration's or closure's once when // the program starts, and every closure of the function points at it. A class member's is // made by its registration and held by its class's method table or template, with the // member's table index. class __FnInfo { constructor(name, length, protoKind, funcIdx, constructible = false) { this.name = name; this.length = length; this.protoKind = protoKind | 0; this.funcIdx = funcIdx | 0; // `new` may make an instance: not an arrow, a method, or an async or generator function. this.constructible = constructible; Object.freeze(this); } } // A function expression whose body reads its own `this`: its body takes the // receiver of a method call through `closure_this` on entry. class __PerryReceiverClosure extends __PerryClosure {} let __closureReceiver; // One value per function declaration, keyed by its table index (`function_value`). const __functionValues = new Map(); // One namespace object per source module, keyed by its module index (`module_namespace`). const __moduleNamespaces = new Map(); function __isPerryClosure(v) { return __PerryClosure.is(v); } // `receiver.m(...args)` where `m` is a Perry closure. The receiver is cleared // after the call either way, so no later closure entry can see it. function __callClosureMethod(closure, receiver, args) { if (closure instanceof __PerryReceiverClosure) __closureReceiver = receiver; try { return __memDispatch.closure_call_spread(closure, args); } finally { __closureReceiver = undefined; } } function __typeOf(v) { return __isPerryClosure(v) ? 'function' : typeof v; } // The prototype a function of each kind has: plain, async, generator, async generator. const __fnKindProtos = [ Function.prototype, Object.getPrototypeOf(async function () {}), Object.getPrototypeOf(function* () {}), Object.getPrototypeOf(async function* () {}), ]; // A host function standing for a function of `kind` has that kind's prototype. function __withKindProto(fn, kind) { if (kind) Object.setPrototypeOf(fn, __fnKindProtos[kind]); return fn; } // A closure's host prototype is its class's; the program sees its kind's. function __programPrototype(obj, proto) { return proto === __PerryClosure.prototype || proto === __PerryReceiverClosure.prototype ? __fnKindProtos[__PerryClosure.protoKindOf(obj)] : proto; } // Class method/static/parent tables. A class's instance methods: name -> the method's // `__FnInfo` (`class_set_method`, `class_define_computed`). const classMethodTable = {}; // A class's named and computed instance accessors, and its named static ones: key -> // { get, set } function table indices, -1 for none (`class_set_accessor`). const classAccessorTable = Object.create(null); const classStaticAccessorTable = Object.create(null); // The resolved keys of a class's computed fields (`__perry_computed_field_key_*`, see // `lower_class_prop`); its static fields are properties of its class value. const classStaticTable = {}; // A class's named static methods: name -> the method's `__FnInfo` (`class_set_static_method`). // Null-prototype, so a method named `__proto__` is an own entry. const classStaticMethodTable = Object.create(null); const classParentTable = {}; // A user class's constructor slot (`class_set_parent`, `class_new`), for its class value. const classCtorSlotTable = Object.create(null); // Core bridge dispatch table — maps bridge function names to their implementations. // Args are decoded from NaN-boxed BigInt bits to plain JS values by __bitsToJsValue before dispatch. // Return values are plain JS values, converted back to BigInt bits by __jsValueToBits in mem_call. // A user-class instance inherits its class name from one prototype per class, so its // own keys are only its declared fields (Node's `new Headers(obj)` copies every own // key). An object literal's shape class has no members to dispatch, so it is a plain // `{}`. `proto` is a class-expression evaluation's prototype (see __classCtor). const __classTag = Symbol('perry.class'); function __classOf(obj) { return obj[__classTag]; } // The class whose instance `obj` is, for `instanceof`: a class prototype holds its // class's tag as an own key and is an instance of the parent class only. function __instanceClassOf(obj) { return __hasOwn(obj, __classTag) ? classParentTable[obj[__classTag]] || null : __classOf(obj); } // That prototype is also the class value's `prototype`. It inherits from a user parent // class's prototype, or from the `prototype` a function parent had when the class was // defined (`class A extends F`), and holds the class's own methods and accessors. A // declared class with a known constructor slot gets its class value here, so // `constructor` holds before any read of the class. function __classProto(className) { const t = __classTemplate(className); if (t.proto) return t.proto; t.proto = Object.create(__classProtoBase(className), { [__classTag]: { value: className } }); for (const m of Reflect.ownKeys(classMethodTable[className] || {})) { __defineHidden(t.proto, m, __classMethodValue(className, m, undefined)); } for (const [key, { get, set }] of Object.entries(classAccessorTable[className] || {})) { __defineAccessor(t.proto, key, get, set, undefined); } if (t.first === undefined && className in classCtorSlotTable) __classRef(className, classCtorSlotTable[className]); return t.proto; } function __classProtoBase(className) { const parent = classParentTable[className]; return parent && __hasOwn(classCtorSlotTable, parent) ? __classProto(parent) : __classFunctionParentProto(className); } // A compiled accessor as a class definition defines it: a pair's second half joins the // first. Either table index is -1 for none. function __defineAccessor(target, key, getIdx, setIdx, evaluation) { const prev = Object.getOwnPropertyDescriptor(target, key); const { get, set } = Object.getOwnPropertyDescriptor({ get [key]() { return __classMemberCall(getIdx, evaluation, this, []); }, set [key](v) { __classMemberCall(setIdx, evaluation, this, [v]); }, }, key); Object.defineProperty(target, key, { get: getIdx >= 0 ? get : prev && prev.get, set: setIdx >= 0 ? set : prev && prev.set, configurable: true, enumerable: false, }); } // `class A extends F` with F a function value: F's `prototype` at definition time, which // may be null. const __classFunctionParentProtos = new Map(); function __classFunctionParentProto(className) { return __classFunctionParentProtos.has(className) ? __classFunctionParentProtos.get(className) : Object.prototype; } // A collection constructor's adder (`set` or `add`), as a function that calls it on // `obj`. It must be callable: a host function or a Perry closure. function __hostAdder(obj, name) { const fn = obj[name]; if (__isPerryClosure(fn)) { return (...args) => { __callClosureMethod(fn, obj, args); __memDispatch.propagate_exception(true); }; } if (typeof fn !== 'function') throw new TypeError(`'${String(fn)}' returned for property '${name}' of object '#<${obj.constructor.name}>' is not a function`); return (...args) => __reflectApply(fn, obj, args); } // A Map or WeakMap constructor's entries: the adder is read once before the iteration, // so a subclass override sees them, and an entry is any object whose keys 0 and 1 are // read as property gets, so a compiled getter runs. function __addMapEntries(obj, entries) { const set = __hostAdder(obj, 'set'); for (const e of entries) { if (Object(e) !== e) throw new TypeError(`Iterator value ${String(e)} is not an entry object`); const k = e[0]; set(k, e[1]); } } // The class name, after recording its constructor slot the first time `new` sees it. function __classNoteCtorSlot(className, ctorIdx) { const cls = String(className); if (ctorIdx !== undefined && !(cls in classCtorSlotTable)) classCtorSlotTable[cls] = ctorIdx | 0; return cls; } function __classInstance(className, proto) { if (className.startsWith('__AnonShape_') || className.startsWith('__PerryDeclaredKeys')) return {}; return Object.create(proto || __classProto(className)); } // An object or a function: a class value (see __classRef) is a host function, and // its static side (`(K as any).key = 1`) is its own properties. function __holdsProperties(v) { return v !== null && (typeof v === 'object' || typeof v === 'function'); } // The Set/Map `for...of` cursors (see `collection_cursor_next`), by collection and // then by loop id. Kept beside the collection so a frozen one iterates too. const __collectionCursors = new WeakMap(); let __collectionCursorSeq = 0; let __collectionCursorLast = 0; function __collectionCursorValue(coll, slot) { const loops = __collectionCursors.get(coll); const cur = loops && loops.get(slot); return cur ? cur.value : undefined; } // A class used as a value (`const K2 = K`, `{ K }`): one host constructor per class, // so `new` on it builds what `new K()` builds — an instance (see __classInstance) run // through the compiled constructor. `ctorIdx` is the constructor's // table slot, or -1 when the class has none. Its arguments stay Perry values: a // closure passed to it must reach the compiled constructor as a closure, so // __constructHostFunction does not wrap them for a constructor in __perryClassCtors. const __classRefs = new Map(); const __perryClassCtors = new WeakSet(); function __classRef(className, ctorIdx) { let ctor = __classRefs.get(className); if (!ctor) { ctor = __classCtor(className, ctorIdx, undefined); __linkClassParent(className, ctor); __classRefs.set(className, ctor); } return ctor; } // `class B extends A` with a user class A: B's class value has A's as its // [[Prototype]], so it inherits A's static side. function __linkClassParent(className, ctor) { const parent = __classParentRef(classParentTable[className]); if (parent) Object.setPrototypeOf(ctor, parent); } // The class value that stands for `parent`, a class table entry; undefined for a built-in. function __classParentRef(parent) { return parent && __hasOwn(classCtorSlotTable, parent) ? __classRef(parent, classCtorSlotTable[parent]) : undefined; } function __isComputedKeySlot(key) { return key.startsWith('__perry_computed_field_key_'); } // Re-parents onto the built-in `base`'s prototype the first prototype up `className`'s // chain of user classes that inherits from Object.prototype. function __linkHostBase(className, base) { const { prototype } = globalThis[base]; for (let c = className; c && c !== base; c = classParentTable[c]) { const p = __classProto(c); const up = Object.getPrototypeOf(p); if (up === prototype) break; if (up === Object.prototype) { Object.setPrototypeOf(p, prototype); break; } } } // With `caps`, the constructor is one evaluation of a class expression // (`Expr::ClassExprFresh`) with that capture environment. Its `prototype` holds // the class's own members bound to the evaluation, and its instances inherit from it. // `base` / `init` (an evaluation only; see `builtin_base_init` in emit/expr/classes.rs): // the built-in parent the instance is made from, and how the implicit constructor // forwards its args to it, as `new` on a declared class does (class_new_host, // host_super_init, error_super_init). function __classCtor(className, ctorIdx, caps, base, init = 0) { const evaluated = caps !== undefined; const ctor = function (...args) { if (new.target === undefined) { throw new TypeError(`Class constructor ${className} cannot be invoked without 'new'`); } const self = typeof base === 'string' ? Object.setPrototypeOf(Reflect.construct(globalThis[base], []), proto) : __classInstance(className, evaluated ? proto : undefined); if (init & 1) __memDispatch.host_super_init(self, args); if (init & 2) __memDispatch.error_super_init(self, init & 4 ? args[0] : undefined); if (ctorIdx < 0) return self; const impl = wasmInstance.exports.__indirect_function_table.get(ctorIdx); // The constructor's trailing params are the class's captures (see // `synthesize_class_captures`): an evaluation passes its own. const envCaps = evaluated ? __evaluationOf(ctor).caps : []; args.push(...envCaps); const result = __bitsToJsValue( __invokeConstructor(impl, envCaps.length, __jsValueToBits(self), ...args.map(__jsValueToBits))); // [[Construct]]: only an object result replaces the instance; `return;` yields undefined. return result !== null && (typeof result === 'object' || typeof result === 'function') ? result : self; }; Object.defineProperty(ctor, 'name', { value: className }); Object.defineProperty(ctor, 'length', { value: ctorIdx < 0 ? 0 : __classTemplate(className).ctor?.length ?? 0 }); // Host `instanceof` walks the class table as class_instanceof does; an instance of // an evaluated template answers for its evaluation, as class_instanceof_value does. Object.defineProperty(ctor, Symbol.hasInstance, { value: (obj) => { if (!obj || typeof obj !== 'object') return false; if (evaluated && __instanceClassOf(obj) === className) { return (__instanceEvaluationOf(obj) || __classTemplate(className).first) === ctor; } for (let c = __instanceClassOf(obj); c; c = classParentTable[c] || null) { if (c === className) return true; } return false; }, }); __perryClassCtors.add(ctor); if (evaluated) Object.defineProperty(ctor, __evaluationRecord, { value: { template: className, caps } }); // A declared class's value shares its instances' prototype (see __classProto). An // evaluation has its own, holding the class's members bound to it. // With a built-in parent, an evaluation's instances are the built-in's objects, whose // methods its prototype inherits, directly or through its declared parent classes. if (typeof base === 'string') __linkHostBase(classParentTable[className], base); const protoBase = evaluated ? __classProtoBase(className) : undefined; const proto = evaluated ? Object.create(typeof base === 'string' && protoBase === Object.prototype ? globalThis[base].prototype : protoBase) : __classProto(className); __defineHidden(proto, 'constructor', ctor); if (evaluated) { for (const m of Object.keys(classMethodTable[className] || {})) { __defineHidden(proto, m, __classMethodValue(className, m, ctor)); } // A computed accessor is the one evaluation's that defines it. for (const [key, { get, set, computed }] of Object.entries(classAccessorTable[className] || {})) { if (!computed) __defineAccessor(proto, key, get, set, ctor); } } // Own named static members only: an inherited one is found along [[Prototype]] // (`__linkClassParent`), where a nearer static field shadows it. for (const [m, info] of Object.entries(classStaticMethodTable[className] || {})) { __defineHidden(ctor, m, evaluated ? __classStaticMethodFn(className, m, info, ctor) : __classStaticMethodValue(className, m)); } for (const [key, { get, set }] of Object.entries(classStaticAccessorTable[className] || {})) { __defineAccessor(ctor, key, get, set, evaluated ? ctor : undefined); } if (evaluated) Object.defineProperties(proto, { [__evaluationTag]: { value: ctor }, [__classTag]: { value: className } }); Object.defineProperty(ctor, 'prototype', { value: proto, writable: false }); return ctor; } // Class environments (`Expr::ClassEnvGet` and friends). An evaluation is its // constructor, which owns its record `{template, caps}`. A member reads the captures of // the evaluation it runs in: the method value's it was called through, else the // receiver's, else the class's first evaluation (the template's `slots` serve a class // that never had one). An instance inherits its evaluation from the evaluation's // prototype rather than holding an own key, which host APIs that copy own keys (Node's // `new Headers(obj)`) would see. const __evaluationRecord = Symbol('perry.classEvaluationRecord'); const __evaluationTag = Symbol('perry.classEvaluation'); function __evaluationOf(ctor) { return typeof ctor === 'function' ? ctor[__evaluationRecord] : undefined; } function __instanceEvaluationOf(obj) { return obj !== null && typeof obj === 'object' ? obj[__evaluationTag] : undefined; } // The own keys the runtime keeps on a program's functions and classes, which no own-key // reflection reports. const __internalSymbols = new Set([__classTag, __evaluationRecord, __evaluationTag]); function __isInternalKey(obj, key) { if (typeof key === 'symbol') { return __internalSymbols.has(key) || (key === Symbol.hasInstance && __perryClassCtors.has(obj)); } if (key === 'funcIdx' || key === 'captures') return __isPerryClosure(obj) || __classMethodFns.has(obj); return __isComputedKeySlot(key) && __perryClassCtors.has(obj) && !__reflectApply(Object.prototype.propertyIsEnumerable, obj, [key]); } // A class's template, by name: its first evaluation, capture slots, prototype, fields, // and its constructor's `__FnInfo` (`class_set_ctor`). const __classTemplates = new Map(); let __classEnvCall = null; function __classTemplate(className) { let t = __classTemplates.get(className); if (!t) __classTemplates.set(className, (t = { first: undefined, slots: [], proto: undefined, fields: new Set(), ctor: undefined })); return t; } function __classEvaluate(className, ctorIdx, caps, base, init) { const ctor = __classCtor(className, ctorIdx, Array.isArray(caps) ? caps : [], base, init); __linkClassParent(className, ctor); const t = __classTemplate(className); if (t.first === undefined) t.first = ctor; return ctor; } function __classEvaluationOf(receiver, className) { const call = __classEnvCall; if (call && call.receiver === receiver && __evaluationOf(call.evaluation).template === className) { return call.evaluation; } const own = __instanceEvaluationOf(receiver); if (own && __evaluationOf(own).template === className) return own; return __classTemplate(className).first; } function __classEnvCaps(receiver, className) { const ev = __classEvaluationOf(receiver, className); return ev ? __evaluationOf(ev).caps : __classTemplate(className).slots; } // Class definition evaluation (emit/expr/class_eval.rs). `__classDefining` holds the // evaluations whose definitions are running, innermost last: a template's computed // members, key slots and statics go to the evaluation on top while it is the // template's, else to the class itself. const __classDefining = []; let __classStaticThis = null; function __classDefiningOf(className) { const top = __classDefining[__classDefining.length - 1]; return top !== undefined && __evaluationOf(top).template === className ? top : undefined; } function __classDefiningTop() { return __classDefining[__classDefining.length - 1]; } function __defineHidden(target, key, value) { Object.defineProperty(target, key, { value, writable: true, configurable: true, enumerable: false }); } // A named static method as a function value. The compiled method takes no receiver: // the receiver it is called with is armed for that one method, which takes it on // entry (`class_static_method_this`), so a direct `C.m()` it makes sees `C`. // `evaluation`, a class expression's evaluation, supplies its captures. function __classStaticMethodFn(template, name, info, evaluation) { const fn = function (...args) { const impl = wasmInstance.exports.__indirect_function_table.get(info.funcIdx); const bits = args.map(__jsValueToBits); const outerThis = __classStaticThis; const outerCall = __classEnvCall; if (this !== undefined) __classStaticThis = { template, method: name, value: this, outer: outerThis }; if (evaluation) __classEnvCall = { receiver: undefined, evaluation }; try { return __bitsToJsValue(__invokeWasmCallback(impl, ...bits)); } finally { __classStaticThis = outerThis; __classEnvCall = outerCall; } }; Object.defineProperty(fn, 'name', { value: name }); Object.defineProperty(fn, 'length', { value: info.length }); __classMethodFns.add(fn); return __withKindProto(fn, info.protoKind); } // The i64 entry an array bridge calls for `cb`: a Perry closure's compiled function // after its captures, or a host function (a class method value, a bound function) called // with the values, which supplies its own `this` and class evaluation. function __arrayCallback(cb) { if (typeof cb === 'function' && cb.funcIdx === undefined) { return (...bits) => __jsValueToBits(cb(...bits.map(__bitsToJsValue))); } if (!cb || typeof cb.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(cb.funcIdx | 0); return fn && ((...bits) => __invokeWasmCallback(fn, ...cb.captures, ...bits)); } // An array callback as a host array method calls it; a value that is not a Perry // callable is passed on for the host to call or reject. function __hostPredicate(cb) { const call = __arrayCallback(cb); return call ? (...args) => __bitsToJsValue(call(...args.map(__jsValueToBits))) : cb; } // The one value of a declared class's static method, shared by every class value // that reaches it, so `Sub.m === Base.m`. const __classStaticMethodValues = new Map(); function __classStaticMethodValue(cls, name) { let byName = __classStaticMethodValues.get(cls); if (!byName) __classStaticMethodValues.set(cls, (byName = new Map())); let fn = byName.get(name); if (!fn) byName.set(name, (fn = __classStaticMethodFn(cls, name, classStaticMethodTable[cls][name], undefined))); return fn; } // A compiled member called on `self`, in `evaluation`'s capture environment. function __classMemberCall(funcIdx, evaluation, self, args) { const impl = wasmInstance.exports.__indirect_function_table.get(funcIdx); const bits = args.map(__jsValueToBits); const outer = __classEnvCall; if (evaluation) __classEnvCall = { receiver: self, evaluation }; try { return __bitsToJsValue(__invokeWasmCallback(impl, __jsValueToBits(self), ...bits)); } finally { __classEnvCall = outer; } } // A static block runs with `this` = the evaluation it initializes, in that // evaluation's capture environment (static code reads it with no receiver). function __classStaticRun(template, value, funcIdx) { const impl = wasmInstance.exports.__indirect_function_table.get(funcIdx); const outerThis = __classStaticThis; const outerCall = __classEnvCall; __classStaticThis = { template, value }; __classEnvCall = { receiver: undefined, evaluation: value }; try { return __bitsToJsValue(__invokeWasmCallback(impl)); } finally { __classStaticThis = outerThis; __classEnvCall = outerCall; } } // The table entry of a class's accessor, made on first use. function __classAccessorPair(table, className, key, computed) { return ((table[className] ||= Object.create(null))[key] ||= { get: -1, set: -1, computed }); } // A computed member (`RegisterClassComputedMethod` / `...Accessor`). An instance // member goes in the template's method table, so every evaluation's instances // dispatch it; a static one is a property of the class value. function __classDefineComputed(className, ctorIdx, key, kind, isStatic, a, b, info) { const evaluation = __classDefiningOf(className); if (isStatic) { const target = evaluation || __classRef(className, ctorIdx); if (kind === 0) { const fn = function (...args) { return __classMemberCall(a, evaluation, this, args); }; Object.defineProperty(fn, 'name', { value: __methodKeyName(key) }); Object.defineProperty(fn, 'length', { value: info.length }); __classMethodFns.add(fn); __defineHidden(target, key, __withKindProto(fn, info.protoKind)); return; } __defineAccessor(target, key, a, b, evaluation); return; } const methods = (classMethodTable[className] ||= {}); if (kind === 0) { methods[key] = info; if (evaluation) __defineHidden(evaluation.prototype, key, __classMethodValue(className, key, evaluation)); // A prototype made before this member was defined gets it too. const proto = __classTemplate(className).proto; if (proto) __defineHidden(proto, key, __classMethodValue(className, key, undefined)); return; } if (evaluation) __defineAccessor(evaluation.prototype, key, a, b, evaluation); __defineAccessor(__classProto(className), key, a, b, undefined); if (typeof key === 'string') { const pair = __classAccessorPair(classAccessorTable, className, key, true); if (a >= 0) pair.get = a; if (b >= 0) pair.set = b; } } // A lowered sync generator drives its resume record, a wasm closure taking // `(op, arg)` (op 0 next, 1 return, 2 throw), or `(arg)` when it has no catch, // finally or `yield*`. The record's first five captures are the cells it shares // with this driver: state, done, sent, executing, and step (how the call ended: // 0 yield, 1 done, 2 an inner iterator result returned as-is). const __generatorRecords = new WeakMap(); function __generatorResume(gen, op, arg, method) { const entry = __generatorRecords.get(gen); if (!entry) throw new TypeError(`Generator.prototype.${method} called on incompatible receiver`); const { record, nextOnly } = entry; const [state, done, sent, executing, step] = record.captures.slice(0, 5).map(__bitsToJsValue); if (executing.value) throw new TypeError("Generator is already executing"); if (done.value || (op !== 0 && (nextOnly || state.value === 0))) { done.value = true; if (op === 2) throw arg; step.value = 1; return { value: op === 1 ? arg : undefined, done: true }; } executing.value = true; if (op === 0) sent.value = arg; let value; try { const fn = wasmInstance.exports.__indirect_function_table.get(record.funcIdx | 0); const args = (nextOnly ? [arg] : [op, arg]).map(__jsValueToBits); value = __bitsToJsValue(__invokeWasmCallback(fn, ...record.captures, ...args)); } catch (error) { done.value = true; throw error; } finally { executing.value = false; } return step.value === 2 ? value : { value, done: step.value === 1 }; } const __GeneratorPrototype = Object.create( Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())), { next: { value(v) { return __generatorResume(this, 0, v, "next"); }, writable: true, configurable: true }, return: { value(v) { return __generatorResume(this, 1, v, "return"); }, writable: true, configurable: true }, throw: { value(e) { return __generatorResume(this, 2, e, "throw"); }, writable: true, configurable: true }, [Symbol.toStringTag]: { value: "Generator", configurable: true }, }, ); // The `__perry_runtime` iterator-record intrinsics the HIR lowers for...of and array // destructuring to (perry-hir/src/iterator_record.rs). `iteratorStep` returns `done` // and leaves the value for the `iteratorStepOutput()` read emitted right after it. const __ArrayIteratorPrototype = Object.getPrototypeOf([][Symbol.iterator]()); const __intrinsicArrayValues = Array.prototype[Symbol.iterator]; const __intrinsicArrayIteratorNext = __ArrayIteratorPrototype.next; const __accessorMarker = {}; let __iteratorStepValue; // The data value `key` resolves to on `obj`'s chain; an accessor is never invoked. function __lookupData(obj, key) { for (let o = obj; o !== null && o !== undefined; o = Object.getPrototypeOf(o)) { const d = Object.getOwnPropertyDescriptor(o, key); if (d) return "value" in d ? d.value : __accessorMarker; } return undefined; } function __arrayIterationPatched() { return __lookupData(Array.prototype, Symbol.iterator) !== __intrinsicArrayValues || __lookupData(__ArrayIteratorPrototype, "next") !== __intrinsicArrayIteratorNext; } function __isObject(v) { return v !== null && (typeof v === "object" || typeof v === "function"); } function __iteratorClose(iter) { const ret = iter.return; if (ret === undefined || ret === null) return; if (typeof ret !== "function") throw new TypeError(`${String(ret)} is not a function`); const result = __reflectApply(ret, iter, []); if (!__isObject(result)) throw new TypeError(`Iterator result ${String(result)} is not an object`); } function __iteratorStep(iter, next, read) { if (typeof next !== "function") throw new TypeError(`${String(next)} is not a function`); const result = __reflectApply(next, iter, []); if (!__isObject(result)) throw new TypeError(`Iterator result ${String(result)} is not an object`); const done = Boolean(result.done); __iteratorStepValue = !done && read !== false ? result.value : undefined; return done; } function __iterableToArray(v) { if (Array.isArray(v)) return v; if (v !== null && v !== undefined && typeof v[Symbol.iterator] === "function") return Array.from(v); const out = []; if (__isObject(v)) { const next = v.next; while (!__iteratorStep(v, next, true)) out.push(__iteratorStepValue); } return out; } // A tagged template's strings array per call site, keyed by the HIR site id's halves. const __taggedTemplates = new Map(); // Perry's own identity for a well-known symbol the host's `Symbol` lacks. const __missingWellKnownSymbols = Object.create(null); // The bridge tables have no prototype: mem_call indexes them by a call site's method name, // and `o.isPrototypeOf(x)` must fall through to __classDispatch, not find Object.prototype's. // The receivers the array bridges walk (`indexOf`, `map`, `join`, ...): an array or a // typed array, whose own method then runs with the typed array's semantics. const __arrayOrTyped = (v) => Array.isArray(v) || (ArrayBuffer.isView(v) && !(v instanceof DataView)); const __memDispatch = { __proto__: null, // `()` asks only whether array iteration is patched; `(value[, packed])` whether // `value` must use the iterator protocol instead of its indices. rt_arrayRecordNeedsIterator: (...args) => args.length === 0 ? __arrayIterationPatched() : !(Array.isArray(args[0]) && Object.getPrototypeOf(args[0]) === Array.prototype && __lookupData(args[0], Symbol.iterator) === __intrinsicArrayValues && __lookupData(__ArrayIteratorPrototype, "next") === __intrinsicArrayIteratorNext), rt_arrayRecordLength: (array) => array.length, // A numeric typed array may be walked by index (HIR `typedArrayForOfAdmits`). rt_typedArrayForOfAdmits: (v) => ArrayBuffer.isView(v) && !(v instanceof DataView) && !(v instanceof BigInt64Array) && !(v instanceof BigUint64Array), rt_arrayRecordLiteral: (...values) => values, rt_arrayRecordCloseAbsent: () => !("return" in __ArrayIteratorPrototype), // ponytail: closes a fresh intrinsic iterator over `src`; JS cannot place its cursor at `index`. rt_arrayRecordClose: (src, index, done, error, throwing) => { if (done || !("return" in __ArrayIteratorPrototype)) return throwing ? error : undefined; const iter = __reflectApply(__intrinsicArrayValues, src, []); if (!throwing) return __iteratorClose(iter); try { __iteratorClose(iter); } catch (_) {} return error; }, rt_iteratorNextMethod: (iter) => iter.next, rt_iteratorStep: (iter, next, read) => __iteratorStep(iter, next, read), rt_iteratorStepOutput: () => __iteratorStepValue, rt_iteratorCloseIfNotDone: (iter, done) => { if (!done) __iteratorClose(iter); }, rt_iteratorCloseOnThrow: (iter, done, error) => { if (!done) { try { __iteratorClose(iter); } catch (_) {} } return error; }, rt_iteratorRestAppend: (array, value) => { array.push(value); return array; }, rt_iteratorRestToArray: (iter, next, done) => { const out = []; if (!done) while (!__iteratorStep(iter, next, true)) out.push(__iteratorStepValue); return out; }, rt_getIterator: (v) => v !== null && v !== undefined && typeof v[Symbol.iterator] === "function" ? v[Symbol.iterator]() : v, rt_iterableToArray: (v) => __iterableToArray(v), rt_arrayIterationPatched: () => __arrayIterationPatched(), rt_requireObjectCoercible: (v) => { if (v == null) throw new TypeError('Cannot convert undefined or null to object'); return v; }, generator_new: (record, paramCount) => { const gen = Object.create(__GeneratorPrototype); __generatorRecords.set(gen, { record, nextOnly: paramCount === 1 }); return gen; }, // A cell holds a local that closures capture and mutate, so all of them share it. box_new: (value) => ({ value }), // A lexical binding's cell in its dead zone carries the binding's name under `__tdz`; reading // or assigning it throws until its declaration runs `box_init`. The mark is on the cell // because testing the value could run a Proxy trap of the program's. box_new_tdz: (name) => ({ value: undefined, [__tdz]: name }), box_get: (cell) => { if (cell[__tdz] !== undefined) __throwDeadZone(cell[__tdz]); return cell.value; }, // The compiler's own read of a binding (see with_tdz_suppressed): undefined in its dead zone. box_peek: (cell) => cell.value, box_set: (cell, value) => { if (cell[__tdz] !== undefined) __throwDeadZone(cell[__tdz]); cell.value = value; return value; }, box_init: (cell, value) => { if (cell[__tdz] !== undefined) cell[__tdz] = undefined; cell.value = value; return value; }, // Console — args are already decoded by __bitsToJsValue console_log: (...args) => { console.log(...args); }, console_warn: (...args) => { console.warn(...args); }, console_error: (...args) => { console.error(...args); }, console_log_multi: (args) => { if (Array.isArray(args)) console.log(...args); }, // `console.(...xs)`: the argument list arrives as one array. console_call_spread: (name, args) => { console[name](...(Array.isArray(args) ? args : [])); }, // String core — args are already decoded JS values via __bitsToJsValue string_concat: (a, b) => String(a) + String(b), js_add: (a, b) => a + b, string_eq: (a, b) => a === b ? 1 : 0, string_len: (val) => { if (typeof val === 'string') return val.length; // A typed array's own `length` (a DataView has none, so it reads undefined). if (ArrayBuffer.isView(val)) return val.length; if (Array.isArray(val)) return val.length; if (typeof val === 'function' || __isPerryClosure(val)) return val.length; return 0; }, jsvalue_to_string: (val) => String(val), jsvalue_to_template_string: (val) => `${val}`, is_truthy: (val) => { if (val === false || val === null || val === undefined || val === 0 || val === '') return 0; if (Number.isNaN(val)) return 0; return 1; }, js_strict_eq: (a, b) => a === b ? 1 : 0, // `==` / `!=`: the emitter calls this for CompareOp::LooseEq / LooseNe. js_loose_eq: (a, b) => a == b ? 1 : 0, // Math math_floor: (x) => Math.floor(x), math_ceil: (x) => Math.ceil(x), math_round: (x) => Math.round(x), math_abs: (x) => Math.abs(x), math_sqrt: (x) => Math.sqrt(x), math_pow: (base, exp) => Math.pow(base, exp), math_random: () => Math.random(), math_log: (x) => Math.log(x), math_log2: (x) => Math.log2(x), math_log10: (x) => Math.log10(x), math_min: (a, b) => Math.min(a, b), math_max: (a, b) => Math.max(a, b), math_min_spread: (src) => Math.min(...src), math_max_spread: (src) => Math.max(...src), // Trig / exp / sign / trunc / cbrt / hypot / etc. (Issue #133 item 4) math_sin: (x) => Math.sin(x), math_cos: (x) => Math.cos(x), math_tan: (x) => Math.tan(x), math_asin: (x) => Math.asin(x), math_acos: (x) => Math.acos(x), math_atan: (x) => Math.atan(x), math_atan2: (y, x) => Math.atan2(y, x), math_sinh: (x) => Math.sinh(x), math_cosh: (x) => Math.cosh(x), math_tanh: (x) => Math.tanh(x), math_asinh: (x) => Math.asinh(x), math_acosh: (x) => Math.acosh(x), math_atanh: (x) => Math.atanh(x), math_exp: (x) => Math.exp(x), math_expm1: (x) => Math.expm1(x), math_log1p: (x) => Math.log1p(x), math_sign: (x) => Math.sign(x), math_trunc: (x) => Math.trunc(x), math_cbrt: (x) => Math.cbrt(x), math_hypot: (a, b) => Math.hypot(a, b), math_fround: (x) => Math.fround(x), math_clz32: (x) => Math.clz32(x), math_imul: (a, b) => Math.imul(a, b), date_now: () => Date.now(), js_typeof: (val) => __typeOf(val), parse_int: (val, radix) => parseInt(String(val), radix), parse_float: (val) => parseFloat(String(val)), js_mod: (a, b) => a % b, is_null_or_undefined: (val) => (val === null || val === undefined) ? 1 : 0, // SameValue: NaN is itself and 0 is not -0, unlike `===`. object_is: (a, b) => Object.is(a, b), // `import * as ns` used as a value: the source module's exports sorted on a null // prototype, tagged `Module`, not extensible. `values` pairs each export with its // value and `globals` each promoted-let export with its wasm global, read on every // access so the binding stays live. module_namespace: (id, values, globals) => { let ns = __moduleNamespaces.get(id); if (ns) return ns; ns = Object.create(null); const members = [...values, ...globals.map(([key, g]) => [key, undefined, g])]; for (const [key, value, g] of members.sort(([a], [b]) => (a < b ? -1 : 1))) { if (g !== undefined) { const cell = wasmInstance.exports[`__wasm_global_${g}`]; Object.defineProperty(ns, key, { get: () => toJsValue(u64ToF64(cell.value)), enumerable: true }); } else { // Not writable, as an importer cannot assign an export. Object.defineProperty(ns, key, { value, enumerable: true }); } } Object.defineProperty(ns, Symbol.toStringTag, { value: 'Module' }); __moduleNamespaces.set(id, Object.preventExtensions(ns)); return ns; }, // Objects — args are plain JS values (obj is the object itself, key is a string, etc.) object_new: () => ({}), object_set: (obj, key, value) => { if (obj && typeof obj === 'object') obj[__hostKey(key)] = value; return obj; }, object_get: (obj, key) => { if (!obj || typeof obj !== 'object') return undefined; return obj[__hostKey(key)]; }, // A literal's property is defined, so a setter on its prototype does not run. object_literal_define: (obj, key, value) => { if (obj && typeof obj === 'object') { Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true }); } return obj; }, // One half of a pair: the other is undefined and keeps what an earlier half defined. object_literal_define_accessor: (obj, key, get, set) => { const desc = { __proto__: null, enumerable: true, configurable: true }; if (get !== undefined) desc.get = __hostAccessor(get); if (set !== undefined) desc.set = __hostAccessor(set); return Object.defineProperty(obj, __memDispatch.to_property_key(key), desc); }, // Only an object or null becomes the prototype; any other value is ignored. object_literal_set_prototype: (obj, proto) => { if (obj && typeof obj === 'object' && (proto === null || typeof proto === 'object' || typeof proto === 'function')) { Object.setPrototypeOf(obj, proto); } return obj; }, object_set_prototype_of: (obj, proto) => Object.setPrototypeOf(obj, proto), // Object.getPrototypeOf boxes a primitive; Reflect.getPrototypeOf throws for one. object_get_prototype_of: (obj) => __programPrototype(obj, Object.getPrototypeOf(obj)), reflect_get_prototype_of: (obj) => __programPrototype(obj, Reflect.getPrototypeOf(obj)), // `super[key]` with HomeObject `home`: the lookup starts at home's [[Prototype]] // at call time (see __superGet, which also takes a class method's home). object_super_get: (home, receiver, key) => __superGet(home, key, receiver), object_get_dynamic: (obj, key) => { if (obj === null || obj === undefined) __throwNullReceiver(obj, key); if (typeof obj === 'symbol') return obj[__hostKey(key)]; if (!__holdsProperties(obj)) return undefined; return obj[__hostKey(key)]; }, object_set_dynamic: (obj, key, value) => { if (__holdsProperties(obj)) obj[__hostKey(key)] = value; }, object_delete: (obj, key) => { if (__holdsProperties(obj)) delete obj[String(key)]; }, object_delete_dynamic: (obj, key) => { if (__holdsProperties(obj)) delete obj[__hostKey(key)]; }, // `for (k in o)`: the enumerable string keys of o and its prototype chain, in the // order a `for...in` visits them. A primitive is boxed, so a string yields its indices. for_in_keys: (obj) => { const keys = []; if (obj !== null && obj !== undefined) for (const k in Object(obj)) keys.push(k); return keys; }, object_keys: (obj) => __holdsProperties(obj) ? Object.keys(obj) : [], object_values: (obj) => __holdsProperties(obj) ? Object.keys(obj).map(k => obj[k]) : [], // Object.create keeps its own checks: a proto that is neither an object nor null throws. object_create: (proto, props) => { const obj = Object.create(proto); return props === undefined ? obj : Object.defineProperties(obj, __hostDescriptors(props)); }, // The target is checked, then the key converted once, before the descriptor's getters run. object_define_property: (obj, key, desc) => { if (!__holdsProperties(obj)) return Object.defineProperty(obj, key, desc); const k = __memDispatch.to_property_key(key); return Object.defineProperty(obj, k, __hostDescriptor(desc)); }, object_define_properties: (obj, props) => Object.defineProperties(obj, __hostDescriptors(props)), reflect_define_property: (obj, key, desc) => { if (!__holdsProperties(obj)) return Reflect.defineProperty(obj, key, desc); const k = __memDispatch.to_property_key(key); return Reflect.defineProperty(obj, k, __hostDescriptor(desc)); }, reflect_has: (obj, key) => { if (!__holdsProperties(obj)) return Reflect.has(obj, key); const k = __memDispatch.to_property_key(key); if (k !== 'funcIdx' && k !== 'captures') return Reflect.has(obj, k); // A closure's bookkeeping keys are also hidden from what inherits from it. for (let o = obj; o !== null; o = Reflect.getPrototypeOf(o)) { if (__memDispatch.object_has_own(o, k)) return true; } return false; }, reflect_own_keys: (obj) => Reflect.ownKeys(obj).filter(k => !__isInternalKey(obj, k)), // An omitted receiver arrives as `undefined` and means the target. reflect_get: (obj, key, receiver) => { if (!__holdsProperties(obj)) return Reflect.get(obj, key); const k = __memDispatch.to_property_key(key); let holder = obj; if (k === 'funcIdx' || k === 'captures') { while (holder !== null && !__memDispatch.object_has_own(holder, k)) holder = Reflect.getPrototypeOf(holder); if (holder === null) return undefined; } return Reflect.get(holder, k, receiver === undefined ? obj : receiver); }, reflect_set: (obj, key, value, receiver) => { if (!__holdsProperties(obj)) return Reflect.set(obj, key, value); const k = __memDispatch.to_property_key(key); return Reflect.set(obj, k, value, receiver === undefined ? obj : receiver); }, reflect_delete_property: (obj, key) => { if (!__holdsProperties(obj)) return Reflect.deleteProperty(obj, key); const k = __memDispatch.to_property_key(key); return __isInternalKey(obj, k) || Reflect.deleteProperty(obj, k); }, reflect_is_extensible: (obj) => Reflect.isExtensible(obj), reflect_prevent_extensions: (obj) => Reflect.preventExtensions(obj), reflect_set_prototype_of: (obj, proto) => Reflect.setPrototypeOf(obj, proto), reflect_get_own_property_descriptor: (obj, key) => __holdsProperties(obj) ? __memDispatch.object_get_own_property_descriptor(obj, key) : Reflect.getOwnPropertyDescriptor(obj, key), object_has_own: (obj, key) => { if (obj === null || obj === undefined) return Object.hasOwn(obj, key); const k = __memDispatch.to_property_key(key); return __hasOwn(obj, k) && !__isInternalKey(obj, k); }, object_get_own_property_names: (obj) => Object.getOwnPropertyNames(obj).filter(k => !__isInternalKey(obj, k)), object_get_own_property_symbols: (obj) => Object.getOwnPropertySymbols(obj).filter(k => !__isInternalKey(obj, k)), object_get_own_property_descriptors: (obj) => { if (obj === null || obj === undefined) return Object.getOwnPropertyDescriptors(obj); const descs = {}; for (const key of __memDispatch.reflect_own_keys(Object(obj))) { const value = __memDispatch.object_get_own_property_descriptor(obj, key); if (value === undefined) continue; Object.defineProperty(descs, key, { value, writable: true, enumerable: true, configurable: true }); } return descs; }, object_get_own_property_descriptor: (obj, key) => { if (obj === null || obj === undefined) return Object.getOwnPropertyDescriptor(obj, key); const k = __memDispatch.to_property_key(key); if (__isInternalKey(obj, k)) return undefined; const desc = Object.getOwnPropertyDescriptor(obj, k); if (desc && 'get' in desc) { desc.get = hostAccessorClosures.get(desc.get) ?? desc.get; desc.set = hostAccessorClosures.get(desc.set) ?? desc.set; } return desc; }, object_entries: (obj) => __holdsProperties(obj) ? Object.entries(obj) : [], // `{ a, ...rest } = obj`: a new object with obj's own enumerable keys, symbols // included, except the statically named ones (CopyDataProperties). Defined, not // assigned, so an own `__proto__` key stays a data property. object_rest: (obj, exclude) => { const rest = {}; if (obj === null || obj === undefined) return rest; const from = Object(obj); for (const key of Reflect.ownKeys(from)) { if (typeof key === 'string' && exclude.includes(key)) continue; const desc = Object.getOwnPropertyDescriptor(from, key); if (desc && desc.enumerable) { Object.defineProperty(rest, key, { value: from[key], writable: true, enumerable: true, configurable: true }); } } return rest; }, object_has_property: (obj, key) => { if (!__holdsProperties(obj)) return 0; return (__hostKey(key) in obj) ? 1 : 0; }, object_coerce: (v) => Object(v), symbol_for: (key) => Symbol.for(key), symbol_well_known: (name) => Symbol[name] ?? (__missingWellKnownSymbols[name] ??= Symbol('Symbol.' + name)), symbol_new: (...desc) => Symbol(...desc), symbol_key_for: (sym) => Symbol.keyFor(sym), // `key in Object(v)` without handing the temporary wrapper back as a handle. object_coerce_has_property: (obj, key) => (__hostKey(key) in Object(obj)) ? 1 : 0, object_assign: (target, source) => { if (target == null) throw new TypeError('Cannot convert undefined or null to object'); const t = Object(target); if (source != null) Object.assign(t, source); return t; }, // Arrays — args are plain JS values (arr is the array itself, etc.) array_new: () => [], // GetTemplateObject: the first evaluation of a site freezes `cooked` with a frozen, // non-enumerable `raw`; every evaluation of that site returns the same object. tagged_template: (siteHi, siteLo, cooked, raw) => { const key = siteHi + ':' + siteLo; let strings = __taggedTemplates.get(key); if (strings === undefined) { Object.defineProperty(cooked, 'raw', { value: Object.freeze(raw) }); strings = Object.freeze(cooked); __taggedTemplates.set(key, strings); } return strings; }, template_raw: (strings) => strings.raw, // `new Array(x)`: single number = length (ES2015 §22.1.1), else element. array_constructor_single: (value) => { if (typeof value === 'number') { if (!Number.isInteger(value) || value < 0 || value > 0xFFFFFFFF) { throw new RangeError('Invalid array length'); } return new Array(value); } return [value]; }, // `[a, , b]`: a hole adds to the length without creating an element. array_push_hole: (arr) => { if (Array.isArray(arr)) arr.length += 1; return arr; }, // `a.keys()` / `a.entries()` / `a.values()`: the method the receiver holds, which a // program may have replaced with a Perry closure. array_keys: (arr) => __jsMethodInvoke(__jsMethodGet(arr, 'keys'), []), array_entries: (arr) => __jsMethodInvoke(__jsMethodGet(arr, 'entries'), []), array_values: (arr) => __jsMethodInvoke(__jsMethodGet(arr, 'values'), []), // Every argument in one `push` call, so `a.push(x, y)` is the single call JS makes. array_push: (arr, ...values) => { if (Array.isArray(arr)) arr.push(...values); return arr; }, array_pop: (arr) => { if (!Array.isArray(arr) || arr.length === 0) return undefined; return arr.pop(); }, array_get: (arr, index) => { if (!Array.isArray(arr)) return undefined; return arr[typeof index === 'number' ? index : index]; }, array_set: (arr, index, value) => { if (Array.isArray(arr)) arr[typeof index === 'number' ? index : index] = value; }, array_length: (arr) => Array.isArray(arr) ? arr.length : 0, array_slice: (arr, start, end) => (__arrayOrTyped(arr) ? arr.slice(start, end) : []), array_splice: (arr, start, deleteCount, ...items) => { if (!Array.isArray(arr)) return []; const s = typeof start === 'number' ? start : 0; const dc = deleteCount === undefined ? arr.length - s : deleteCount; // Non-numeric start must reach splice uncoerced: JS ToIntegerOrInfinity // coerces "1" to 1, while the pre-coerced 0 silently shifts insertions. return arr.splice(typeof start === 'number' ? s : start, dc, ...items); }, array_shift: (arr) => { if (!Array.isArray(arr) || arr.length === 0) return undefined; return arr.shift(); }, array_unshift: (arr, value) => { if (Array.isArray(arr)) arr.unshift(value); }, // The host join owns element conversion (nested arrays, nullish elements) and the // separator's: an undefined one is ",", not "undefined". array_join: (arr, separator) => { if (!__arrayOrTyped(arr)) return ''; return arr.join(separator); }, array_index_of: (arr, value, fromIndex) => { if (!__arrayOrTyped(arr)) return -1; return arr.indexOf(value, fromIndex); }, array_includes: (arr, value, fromIndex) => { if (!__arrayOrTyped(arr)) return 0; return arr.includes(value, fromIndex) ? 1 : 0; }, // fromIndex goes through only when the call passes one: an explicit `undefined` // searches from index 0, not from the end. array_last_index_of: (arr, value, ...fromIndex) => __arrayOrTyped(arr) ? arr.lastIndexOf(value, ...fromIndex) : -1, array_concat: (a1, a2) => (Array.isArray(a1) ? a1 : []).concat(Array.isArray(a2) ? a2 : []), array_reverse: (arr) => { if (__arrayOrTyped(arr)) arr.reverse(); return arr; }, array_flat: (arr) => Array.isArray(arr) ? arr.flat() : [], array_is_array: (val) => Array.isArray(val) ? 1 : 0, array_from: (val) => Array.from(val), // `Array.from(value, mapFn, thisArg)`: mapFn receives (element, index), with `this` // bound to thisArg. An undefined mapFn maps nothing; any other value that is not // callable is a TypeError before `value` is read. An exception mapFn left pending is // thrown here, so the host stops and closes the iterator. array_from_mapped: (val, fn, thisArg) => { if (fn === undefined) return Array.from(val); if (__typeOf(fn) !== 'function') { const what = fn === null ? 'object null' : typeof fn === 'string' ? `string "${fn}"` : ['object', 'bigint', 'symbol'].includes(typeof fn) ? typeof fn : `${typeof fn} ${fn}`; throw new TypeError(`${what} is not a function`); } return Array.from(val, (v, i) => { const mapped = __memDispatch.reflect_apply(fn, thisArg, [v, i]); __memDispatch.propagate_exception(true); return mapped; }); }, // `[...source]`: any iterable source (a Set, a Map, an iterator) spreads its values. // One push per value: `push(...source)` passes each value as an argument and // overflows the stack for a large source. array_push_spread: (target, source) => { const iterate = source?.[Symbol.iterator]; if (Array.isArray(target) && typeof iterate === 'function') { const it = __reflectApply(iterate, source, []); const next = it.next; while (!__iteratorStep(it, next, true)) target.push(__iteratorStepValue); } return target; }, // String methods — args are plain JS strings string_charAt: (str, idx) => String(str).charAt(idx), string_substring: (str, start, end) => String(str).substring(start, end), // Codegen picks these five bridges by method name alone and passes every argument. string_indexOf: (str, ...args) => __jsMethodInvoke(__jsMethodGet(str, 'indexOf'), args), string_slice: (str, ...args) => __jsMethodInvoke(__jsMethodGet(str, 'slice'), args), string_toLowerCase: (str) => String(str).toLowerCase(), string_toUpperCase: (str) => String(str).toUpperCase(), string_trim: (str) => String(str).trim(), string_includes: (str, ...args) => __jsMethodInvoke(__jsMethodGet(str, 'includes'), args), string_startsWith: (str, ...args) => __jsMethodInvoke(__jsMethodGet(str, 'startsWith'), args), string_endsWith: (str, ...args) => __jsMethodInvoke(__jsMethodGet(str, 'endsWith'), args), string_replace: (str, pattern, replacement) => { const s = String(str); const p = (pattern instanceof RegExp) ? pattern : String(pattern); return s.replace(p, String(replacement)); }, string_split: (str, delim) => String(str).split(String(delim)), string_fromCharCode: (code) => String.fromCharCode(code), string_padStart: (str, len, fill) => String(str).padStart(len, String(fill)), string_padEnd: (str, len, fill) => String(str).padEnd(len, String(fill)), string_repeat: (str, count) => String(str).repeat(count), string_match: (str, regex) => { const s = String(str); const re = (regex instanceof RegExp) ? regex : new RegExp(String(regex)); const result = s.match(re); if (!result) return null; return Array.from(result); }, // Closures — WASM functions now use i64 params/returns (BigInt in JS). // Captures are stored as BigInt (i64 NaN-boxed bits). // __jsValueToBits converts JS values to BigInt for WASM calls. // __bitsToJsValue converts BigInt return values back to JS. // A function's `__FnInfo`, made once per function before any of its closures. fn_info: (name, length, protoKind, constructible) => new __FnInfo(name, length, protoKind, 0, constructible === true || constructible === 1), closure_new: (funcIdx, captureCount, receivesThis, info) => receivesThis ? new __PerryReceiverClosure(funcIdx, captureCount, info) : new __PerryClosure(funcIdx, captureCount, info), // A class constructor's `__FnInfo`, registered with the class's methods and the same // arguments, kept on the class's template (see __classCtor). class_set_ctor: (classId, _name, funcIdx, _protoKind, length) => { const cls = String(classId); __classTemplate(cls).ctor = new __FnInfo(cls, length, 0, funcIdx); }, // A function declaration used as a value: one closure per function, so it keeps its // identity and properties across reads, with the `prototype` object an ordinary // function has (`prototype.constructor` is the function). function_value: (funcIdx, info) => { let fn = __functionValues.get(funcIdx); if (!fn) { fn = new __PerryClosure(funcIdx, 0, info); Object.defineProperty(fn, 'length', { value: info?.length ?? 0, configurable: true }); Object.defineProperty(fn, 'name', { value: info?.name ?? '', configurable: true }); const prototype = {}; Object.defineProperty(prototype, 'constructor', { value: fn, writable: true, configurable: true }); Object.defineProperty(fn, 'prototype', { value: prototype, writable: true }); __functionValues.set(funcIdx, fn); } return fn; }, // `F.prototype = proto`. A class's `prototype` is read-only: the store is ignored, // or a TypeError in strict code. function_set_prototype: (fn, proto, strict) => { if ((!__holdsProperties(fn) || !Reflect.set(fn, 'prototype', proto)) && strict) { throw new TypeError(`Cannot assign to read only property 'prototype' of ${typeof fn}`); } return proto; }, closure_this: () => { const receiver = __closureReceiver; __closureReceiver = undefined; return receiver; }, closure_set_capture: (closure, idx, value) => { if (closure) closure.captures[idx | 0] = __jsValueToBits(value); return closure; }, closure_call_0: (closure) => { if (typeof closure === 'function') return __callHostFunction(closure, []); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (!fn) return undefined; // Pad for omitted TS optional params (Issue #1035). const padded = __padBigintArgs(fn, closure.captures.length, []); return __bitsToJsValue(fn(...closure.captures, ...padded)); }, closure_call_1: (closure, a0) => { if (typeof closure === 'function') return __callHostFunction(closure, [a0]); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (!fn) return undefined; const padded = __padBigintArgs(fn, closure.captures.length, [__jsValueToBits(a0)]); return __bitsToJsValue(fn(...closure.captures, ...padded)); }, closure_call_2: (closure, a0, a1) => { if (typeof closure === 'function') return __callHostFunction(closure, [a0, a1]); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (!fn) return undefined; const padded = __padBigintArgs(fn, closure.captures.length, [__jsValueToBits(a0), __jsValueToBits(a1)]); return __bitsToJsValue(fn(...closure.captures, ...padded)); }, closure_call_3: (closure, a0, a1, a2) => { if (typeof closure === 'function') return __callHostFunction(closure, [a0, a1, a2]); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (!fn) return undefined; const padded = __padBigintArgs(fn, closure.captures.length, [__jsValueToBits(a0), __jsValueToBits(a1), __jsValueToBits(a2)]); return __bitsToJsValue(fn(...closure.captures, ...padded)); }, // `new callee(...args)` with a callee known only at run time (`new Map()` reaches here // through `global_property`). A host constructor must be constructed: called without // `new`, Map / Set / URLSearchParams throw. A Perry closure keeps the plain call the // closure_call_N bridges make, for any number of arguments. new_dynamic: (callee, ...args) => { if (typeof callee === 'function') return __constructHostFunction(callee, args); if (!callee || typeof callee.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(callee.funcIdx | 0); if (!fn) return undefined; const padded = __padBigintArgs(fn, callee.captures.length, args.map(__jsValueToBits)); return __bitsToJsValue(fn(...callee.captures, ...padded)); }, reflect_apply: (fn, thisArg, args) => { const argArr = Array.isArray(args) ? args : args !== null && typeof args === 'object' ? Array.from(args) : []; if (__classMethodFns.has(fn)) return __reflectApply(fn, thisArg, argArr); if (typeof fn === 'function') return __callHostFunction(fn, argArr, thisArg); return __callClosureMethod(fn, thisArg, argArr); }, // Four or more arguments: `closure_call_n(closure, a0, a1, ...)`. closure_call_n: (closure, ...args) => __memDispatch.closure_call_spread(closure, args), closure_call_spread: (closure, args) => { const argArr = Array.isArray(args) ? args : []; if (typeof closure === 'function') return __callHostFunction(closure, argArr); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return undefined; const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (!fn) return undefined; const padded = __padBigintArgs(fn, closure.captures.length, argArr.map(v => __jsValueToBits(v))); // A void function's export returns `undefined`, not bits. const result = fn(...closure.captures, ...padded); return result === undefined ? undefined : __bitsToJsValue(result); }, // `obj.name(...xs)` / `obj[key](...xs)`: the member is read and invoked as the async // path does, so an own property wins and a non-callable one throws. method_call_spread: (obj, name, args) => __jsMethodInvoke(__jsMethodGet(obj, name), args), // Array higher-order methods — WASM callbacks use i64 (BigInt) params/returns. array_map: (arr, cb) => { const call = __arrayOrTyped(arr) ? __arrayCallback(cb) : undefined; if (!call) return []; return arr.map((v, i) => __bitsToJsValue(call(__jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)))); }, array_filter: (arr, cb) => { const call = __arrayOrTyped(arr) ? __arrayCallback(cb) : undefined; if (!call) return []; return arr.filter((v, i) => { const r = call(__jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)); const rv = __bitsToJsValue(r); if (rv === true) return true; if (rv === false || rv === null || rv === undefined) return false; return !!rv; }); }, // `forEach` is dispatched by name, so a Set or a Map receiver reaches it too. array_forEach: (arr, cb) => { const isCollection = __arrayOrTyped(arr) || arr instanceof Set || arr instanceof Map; const call = isCollection ? __arrayCallback(cb) : undefined; if (!call) return; arr.forEach((v, i) => call(__jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr))); }, array_reduce: (arr, cb, initial) => { const call = __arrayOrTyped(arr) ? __arrayCallback(cb) : undefined; if (!call) return undefined; let acc = initial === undefined ? __jsValueToBits(arr[0]) : __jsValueToBits(initial); const startIdx = initial === undefined ? 1 : 0; for (let i = startIdx; i < arr.length; i++) acc = call(acc, __jsValueToBits(arr[i]), __jsValueToBits(i), __jsValueToBits(arr)); return __bitsToJsValue(acc); }, array_find: (arr, cb) => { const call = __arrayOrTyped(arr) ? __arrayCallback(cb) : undefined; if (!call) return undefined; for (let i = 0; i < arr.length; i++) { // The element the predicate saw, whatever the predicate writes. const v = arr[i]; if (__bitsToJsValue(call(__jsValueToBits(v), __jsValueToBits(i), __jsValueToBits(arr)))) return v; } return undefined; }, array_find_index: (arr, cb) => { const call = __arrayOrTyped(arr) ? __arrayCallback(cb) : undefined; if (!call) return -1; for (let i = 0; i < arr.length; i++) { if (__bitsToJsValue(call(__jsValueToBits(arr[i]), __jsValueToBits(i), __jsValueToBits(arr)))) return i; } return -1; }, array_find_last: (arr, cb) => __reflectApply(Array.prototype.findLast, arr, [__hostPredicate(cb)]), array_find_last_index: (arr, cb) => __reflectApply(Array.prototype.findLastIndex, arr, [__hostPredicate(cb)]), array_sort: (arr, cb) => { if (!__arrayOrTyped(arr)) return arr; if (cb === undefined || cb === null) { arr.sort(); return arr; } const call = __arrayCallback(cb); if (!call) { arr.sort(); return arr; } arr.sort((a, b) => { const r = call(__jsValueToBits(a), __jsValueToBits(b)); return __bitsToJsValue(r); }); return arr; }, // `reduceRight`, the change-by-copy methods and `copyWithin` run the host's own, so // hole handling, argument coercions and errors are the host's. `initial` is empty when // the call passes none: an explicit undefined is an initial value. array_reduce_right: (arr, cb, ...initial) => { const call = __arrayCallback(cb); const fn = call ? (...args) => __bitsToJsValue(call(...args.map(__jsValueToBits))) : cb; return __reflectApply(Array.prototype.reduceRight, arr, [fn, ...initial]); }, array_to_reversed: (arr) => __reflectApply(Array.prototype.toReversed, arr, []), array_to_sorted: (arr) => __reflectApply(Array.prototype.toSorted, arr, []), array_to_sorted_cmp: (arr, cb) => { const call = __arrayCallback(cb); const cmp = call ? (a, b) => __bitsToJsValue(call(__jsValueToBits(a), __jsValueToBits(b))) : cb; return __reflectApply(Array.prototype.toSorted, arr, [cmp]); }, array_to_spliced: (arr, start, deleteCount, ...items) => __reflectApply(Array.prototype.toSpliced, arr, [start, deleteCount, ...items]), array_with: (arr, index, value) => __reflectApply(Array.prototype.with, arr, [index, value]), array_copy_within: (arr, target, start, end) => __reflectApply(Array.prototype.copyWithin, arr, [target, start, end]), array_some: (arr, cb) => __arrayOrTyped(arr) && arr.some(__hostPredicate(cb)) ? 1 : 0, array_every: (arr, cb) => __arrayOrTyped(arr) && arr.every(__hostPredicate(cb)) ? 1 : 0, // Classes — args are plain JS values. obj is the object, className/methodName are strings. // class_new returns a plain object carrying its class name (see __classInstance). // class_set_method/class_call_method interact with WASM indirect call table. class_new: (className, ctorIdx) => __classInstance(__classNoteCtorSlot(className, ctorIdx)), // `new C()` where C's chain of user classes ends at the built-in `base` (Map, Array, // ...): the built-in object itself, on C's prototype. class_new_host: (className, ctorIdx, base) => { const cls = __classNoteCtorSlot(className, ctorIdx); __linkHostBase(cls, String(base)); return Object.setPrototypeOf(Reflect.construct(globalThis[String(base)], []), __classProto(cls)); }, // `super(...args)` into the built-in an instance from class_new_host was made from. // A collection's entries go through the instance's `set`/`add` (see __addMapEntries). // Array elements are own data properties and a Date's time value is // set directly, past any accessor or override on the subclass. host_super_init: (obj, args) => { const a = Array.isArray(args) ? args : []; if (obj instanceof Map || obj instanceof WeakMap) { if (a[0] != null) __addMapEntries(obj, a[0]); } else if (obj instanceof Set || obj instanceof WeakSet) { if (a[0] != null) { const add = __hostAdder(obj, 'add'); for (const v of a[0]) add(v); } } else if (Array.isArray(obj)) { const made = Array(...a); obj.length = made.length; for (const i of Object.keys(made)) { Object.defineProperty(obj, i, { value: made[i], writable: true, enumerable: true, configurable: true }); } } else if (obj instanceof Date) { __reflectApply(Date.prototype.setTime, obj, [new Date(...a).getTime()]); } return obj; }, class_ref: (className, ctorIdx) => __classRef(String(className), ctorIdx | 0), class_fresh: (className, ctorIdx, caps, base, init) => __classEvaluate(String(className), ctorIdx | 0, caps, base, init | 0), class_eval_begin: (className, ctorIdx, base, init) => { const ctor = __classEvaluate(String(className), ctorIdx | 0, [], base, init | 0); __classDefining.push(ctor); return ctor; }, class_eval_slot: (className, slot, key) => { __defineHidden(__classDefiningTop(), String(slot), key); }, class_eval_caps: (className, caps) => { if (Array.isArray(caps)) __evaluationOf(__classDefiningTop()).caps = caps; }, class_eval_static: (className, name, value) => { Object.defineProperty(__classDefiningTop(), String(name), { value, writable: true, configurable: true, enumerable: true }); }, class_eval_static_computed: (className, slot, value) => { const ctor = __classDefiningTop(); Object.defineProperty(ctor, ctor[String(slot)], { value, writable: true, configurable: true, enumerable: true }); }, class_eval_block: (className, funcIdx) => { if ((funcIdx | 0) >= 0) __classStaticRun(String(className), __classDefiningTop(), funcIdx | 0); }, class_eval_end: (className) => __classDefining.pop(), class_define_computed: (className, ctorIdx, key, kind, isStatic, a, b, protoKind, length) => __classDefineComputed(String(className), ctorIdx | 0, key, kind | 0, !!isStatic, a | 0, b | 0, (kind | 0) === 0 ? new __FnInfo(__methodKeyName(key), length | 0, protoKind, a | 0) : undefined), // `static [key] = value` of a class declared once. class_static_define: (className, ctorIdx, key, value) => { const cls = String(className); Object.defineProperty(__classDefiningOf(cls) || __classRef(cls, ctorIdx | 0), key, { value, writable: true, configurable: true, enumerable: true }); }, class_static_this: (className, ctorIdx) => { let armed = __classStaticThis; // A receiver armed for one static method (`method`) is that method's alone. while (armed && armed.method !== undefined) armed = armed.outer; if (armed && armed.template === String(className)) return armed.value; return __classRef(String(className), ctorIdx | 0); }, // A static method's `this`, read once on entry: the receiver armed for it, which it // consumes, else what class_static_this answers. class_static_method_this: (className, ctorIdx, methodName) => { const armed = __classStaticThis; if (armed && armed.method === String(methodName) && armed.template === String(className)) { __classStaticThis = armed.outer; return armed.value; } return __memDispatch.class_static_this(className, ctorIdx); }, // A computed instance field: its key is the hidden slot its class definition // resolved, on the instance's evaluation or on the class. class_init_computed_field: (className, slot, instance, value) => { if (!instance || typeof instance !== 'object') return; const name = String(slot); const ev = __instanceEvaluationOf(instance); let key; if (ev && __hasOwn(ev, name)) key = ev[name]; else key = (classStaticTable[String(className)] || {})[name]; Object.defineProperty(instance, key, { value, writable: true, configurable: true, enumerable: true }); }, // ToPropertyKey, object-to-primitive conversion included (`Object(Symbol.iterator)`). to_property_key: (v) => Reflect.ownKeys({ [__hostKey(v)]: 0 })[0], // A refresh of a path that never evaluated the class gets `undefined`: a no-op. class_env_refresh: (ctor, caps) => { const evaluation = __evaluationOf(ctor); if (evaluation && Array.isArray(caps)) evaluation.caps = caps; return ctor; }, class_env_get: (className, index, receiver) => __classEnvCaps(receiver, String(className))[index | 0], // A constructor's publish of its capture params never overwrites a capture with // a param the caller left out (see `class_env.rs` in perry-codegen). class_env_set: (className, index, receiver, value, publish) => { if (!(publish && value === undefined)) __classEnvCaps(receiver, String(className))[index | 0] = value; return value; }, class_env_stamp: (className, instance, evaluation) => { const e = __evaluationOf(evaluation); // class_new built `new C()` on the class's prototype (or another evaluation's). if (e && instance && typeof instance === 'object' && __classOf(instance) === e.template) { const proto = Object.getPrototypeOf(instance); if (proto !== evaluation.prototype && (proto === __classProto(e.template) || __instanceEvaluationOf(proto) !== undefined)) { Object.setPrototypeOf(instance, evaluation.prototype); } } return instance; }, class_env_current: (className, receiver) => __classEvaluationOf(receiver, String(className)), class_register_captures: (className, caps) => { if (Array.isArray(caps)) __classTemplate(String(className)).slots = caps; }, // The decl-site snapshot (`RegisterClassCaptures`) unless it is `undefined`, or the // new-site param first when `preferFallback` says the param wins. class_capture_value: (className, index, fallback, preferFallback) => { const snapshot = __classTemplate(String(className)).slots[index | 0]; if (preferFallback) return fallback !== undefined ? fallback : snapshot; return snapshot !== undefined ? snapshot : fallback; }, // `obj instanceof C` where C is a class-expression evaluation: an instance of the // template answers for the evaluation that built it. class_instanceof_value: (classId, obj, ctor) => { const e = __evaluationOf(ctor); if (e && obj && typeof obj === 'object' && __instanceClassOf(obj) === e.template) { return (__instanceEvaluationOf(obj) || __classTemplate(e.template).first) === ctor; } return __memDispatch.class_instanceof(obj, classId) === 1; }, class_set_method: (classId, methodName, funcIdx, protoKind, length) => { const cls = String(classId); const name = String(methodName); if (!classMethodTable[cls]) classMethodTable[cls] = {}; classMethodTable[cls][name] = new __FnInfo(name, length, protoKind, funcIdx); }, // A class value made before registration gets the method too. class_set_static_method: (classId, methodName, funcIdx, protoKind, length) => { const cls = String(classId); const name = String(methodName); (classStaticMethodTable[cls] ||= Object.create(null))[name] = new __FnInfo(name, length, protoKind, funcIdx); __classStaticMethodValues.get(cls)?.delete(name); const ref = __classRefs.get(cls); if (ref) __defineHidden(ref, name, __classStaticMethodValue(cls, name)); }, // A private accessor (`#x`) is not a property. class_set_accessor: (classId, name, funcIdx, code) => { const key = String(name); if (key.startsWith('#')) return; const pair = __classAccessorPair(code & 2 ? classStaticAccessorTable : classAccessorTable, String(classId), key, false); pair[code & 1 ? 'set' : 'get'] = funcIdx | 0; }, class_call_method: (obj, methodNameId, argsArr) => { // methodNameId is a plain number (string table index), not a decoded JS value const mname = stringTable[methodNameId | 0]; // A static member of a class value. if (typeof obj === 'function') { const member = obj[mname]; if (typeof member === 'function') return __callHostFunction(member, Array.isArray(argsArr) ? argsArr : [], obj); } if (obj && __classOf(obj)) { let cls = __classOf(obj); while (cls) { const methods = classMethodTable[cls]; if (methods && mname in methods) { const fn = wasmInstance?.exports.__indirect_function_table?.get(methods[mname].funcIdx); if (fn) { const args = Array.isArray(argsArr) ? argsArr : []; // WASM functions use i64 (BigInt) params/returns. // Pad for omitted TS optional params (Issue #1035). const padded = __padBigintArgs(fn, 1, args.map(v => __jsValueToBits(v))); return __bitsToJsValue(fn(__jsValueToBits(obj), ...padded)); } } cls = classParentTable[cls] || null; } } // Fallback to UI method dispatch const uiMethodMap = { addChild: "perry_ui_widget_add_child", removeAllChildren: "perry_ui_widget_remove_all_children", setBackground: "perry_ui_set_background", setForeground: "perry_ui_set_foreground", setFontSize: "perry_ui_set_font_size", setFontWeight: "perry_ui_set_font_weight", setFontFamily: "perry_ui_set_font_family", setPadding: "perry_ui_set_padding", setFrame: "perry_ui_set_frame", setCornerRadius: "perry_ui_set_corner_radius", setBorder: "perry_ui_set_border", setOpacity: "perry_ui_set_opacity", setEnabled: "perry_ui_set_enabled", setTooltip: "perry_ui_set_tooltip", setControlSize: "perry_ui_set_control_size", animateOpacity: "perry_ui_animate_opacity", animatePosition: "perry_ui_animate_position", setOnClick: "perry_ui_set_on_click", setOnHover: "perry_ui_set_on_hover", setOnDoubleClick: "perry_ui_set_on_double_click", get: "perry_ui_state_get", set: "perry_ui_state_set", create: "perry_ui_state_create", bindText: "perry_ui_state_bind_text", bindTextNumeric: "perry_ui_state_bind_text_numeric", bindSlider: "perry_ui_state_bind_slider", bindToggle: "perry_ui_state_bind_toggle", bindVisibility: "perry_ui_state_bind_visibility", bindForEach: "perry_ui_state_bind_foreach", onChange: "perry_ui_state_on_change", setString: "perry_ui_text_set_string", setSelectable: "perry_ui_text_set_selectable", setBordered: "perry_ui_button_set_bordered", setTitle: "perry_ui_button_set_title", setTextColor: "perry_ui_button_set_text_color", setImage: "perry_ui_button_set_image", fillRect: "perry_ui_canvas_fill_rect", strokeRect: "perry_ui_canvas_stroke_rect", clearRect: "perry_ui_canvas_clear_rect", fillGradient: "perry_ui_canvas_fill_gradient", setFillColor: "perry_ui_canvas_set_fill_color", setStrokeColor: "perry_ui_canvas_set_stroke_color", beginPath: "perry_ui_canvas_begin_path", moveTo: "perry_ui_canvas_move_to", lineTo: "perry_ui_canvas_line_to", arc: "perry_ui_canvas_arc", closePath: "perry_ui_canvas_close_path", fill: "perry_ui_canvas_fill", stroke: "perry_ui_canvas_stroke", setLineWidth: "perry_ui_canvas_set_line_width", fillText: "perry_ui_canvas_fill_text", setFont: "perry_ui_canvas_set_font", drawImage: "perry_ui_canvas_draw_image", setChild: "perry_ui_scrollview_set_child", focus: "perry_ui_textfield_focus", setWidth: "perry_ui_widget_set_width", setHeight: "perry_ui_widget_set_height", matchParentWidth: "perry_ui_widget_match_parent_width", matchParentHeight: "perry_ui_widget_match_parent_height", setHidden: "perry_ui_set_widget_hidden", setEdgeInsets: "perry_ui_widget_set_edge_insets", setShadow: "perry_ui_widget_set_shadow", run: "perry_ui_app_run", setBody: "perry_ui_app_set_body", }; const uiFnName = uiMethodMap[mname]; if (uiFnName) { const fn = __perryUiDispatch[uiFnName]; if (!fn) return undefined; const args = Array.isArray(argsArr) ? argsArr : []; return fn(obj, ...args); } return undefined; }, // A class method's [[HomeObject]] names its class and side: the class's prototype is // its method table, not a host object (see __superGet). class_method_home: (className, isStatic) => ({ __classHome__: String(className), isStatic: !!isStatic }), // A null parent is `extends null`; the parent walks end there as at a root class. class_extends_null: (className) => { classParentTable[String(className)] = null; }, // `class A extends F` with a user function F: A's prototype inherits F's current one. class_extends_value: (className, value) => { if (__isPerryClosure(value) && (value.prototype === null || __holdsProperties(value.prototype))) { __classFunctionParentProtos.set(String(className), value.prototype); } }, class_get_field: (obj, name) => { if (obj === null || obj === undefined) __throwNullReceiver(obj, name); if (!__holdsProperties(obj) && typeof obj !== 'symbol') return undefined; return obj[String(name)]; }, class_set_field: (obj, name, value) => { if (__holdsProperties(obj)) obj[String(name)] = value; }, // A named field is defined: an inherited accessor or read-only property does not see it. class_init_field: (obj, name, value) => { if (!__holdsProperties(obj)) return; const key = String(name); if (key in obj) Object.defineProperty(obj, key, { value, writable: true, configurable: true, enumerable: true }); else obj[key] = value; }, // The first store is the initializer, which defines the field over an intrinsic // property or static method of that name; a later one is an assignment. class_set_static: (classId, name, value, ctorIdx) => { const cls = String(classId); const key = String(name); if (__isComputedKeySlot(key)) { (classStaticTable[cls] ||= {})[key] = value; return value; } const ref = __classRef(cls, ctorIdx | 0); const { fields } = __classTemplate(cls); if (fields.has(key)) ref[key] = value; else Object.defineProperty(ref, key, { value, writable: true, configurable: true, enumerable: true }); fields.add(key); return value; }, class_get_static: (classId, name, ctorIdx) => { const cls = String(classId); const key = String(name); return __isComputedKeySlot(key) ? (classStaticTable[cls] || {})[key] : __classRef(cls, ctorIdx | 0)[key]; }, class_instanceof: (obj, classId) => { if (!obj || typeof obj !== 'object') return 0; const target = String(classId); // A user-class instance (see __classInstance): its chain is the class table, // and as a plain object it is also an instance of Object. if (__classOf(obj)) { let cls = __instanceClassOf(obj); while (cls) { if (cls === target) return 1; cls = classParentTable[cls] || null; } return target === 'Object' ? 1 : 0; } // A host object (Error, Array, Map, ...): ask the host constructor of that name. const ctor = globalThis[target]; return typeof ctor === 'function' && obj instanceof ctor ? 1 : 0; }, class_set_parent: (childId, parentId, parentCtorIdx) => { classParentTable[String(childId)] = String(parentId); if (parentCtorIdx !== undefined) classCtorSlotTable[String(parentId)] = parentCtorIdx | 0; }, // `super[key]` in an instance method of `cls`, with the receiver as `this`. class_super_get: (obj, cls, mname) => __superGet({ __classHome__: String(cls), isStatic: false }, mname, obj), // Calls what class_super_get read, after the arguments were evaluated. A compiled // method gets its arguments as Perry values; a non-callable throws, as in JS. class_super_call: (fn, obj, mname, args) => { if (__classMethodFns.has(fn)) return __reflectApply(fn, obj, args); if (typeof fn === 'function') return __callHostFunction(fn, args, obj); if (fn && typeof fn === 'object' && typeof fn.funcIdx !== 'undefined') return __callClosureMethod(fn, obj, args); throw new TypeError(`(intermediate value)${mname === undefined ? '' : '.' + String(mname)} is not a function`); }, // JSON — args are plain JS values json_parse: (str) => { try { return JSON.parse(String(str)); } catch (e) { if (tryDepth > 0) currentException = e; return undefined; } }, // A closure value is omitted as a function is, through a `toJSON` its prototype has // only meanwhile; a replacer function gets the closure itself. json_stringify: (val, replacer, space) => { const host = __hostAccessor(replacer); const replace = typeof host === 'function' ? function (...args) { const result = __reflectApply(host, this, args); return __isPerryClosure(result) ? undefined : result; } : host; const proto = __PerryClosure.prototype; const outer = Object.getOwnPropertyDescriptor(proto, 'toJSON'); if (typeof replace === 'function') delete proto.toJSON; else __defineHidden(proto, 'toJSON', function () { return __isPerryClosure(this) ? undefined : this; }); try { return JSON.stringify(val, replace, space); } finally { if (outer) Object.defineProperty(proto, 'toJSON', outer); else delete proto.toJSON; } }, // Map — args are plain JS values (m is the Map object itself) map_new: () => new Map(), map_set: (m, key, value) => { if (m instanceof Map) m.set(key, value); }, map_get: (m, key) => { if (!(m instanceof Map)) return undefined; return m.get(key); }, map_has: (m, key) => (m instanceof Map && m.has(key)) ? 1 : 0, map_delete: (m, key) => { if (m instanceof Map) m.delete(key); }, map_size: (m) => (m instanceof Map) ? m.size : 0, map_clear: (m) => { if (m instanceof Map) m.clear(); }, map_entries: (m) => (m instanceof Map) ? [...m.entries()] : [], map_keys: (m) => (m instanceof Map) ? [...m.keys()] : [], map_values: (m) => (m instanceof Map) ? [...m.values()] : [], // Set — args are plain JS values (s is the Set object itself) set_new: () => new Set(), set_new_from_array: (iterable) => new Set(iterable), set_add: (s, value) => { if (s instanceof Set) s.add(value); }, set_has: (s, value) => (s instanceof Set && s.has(value)) ? 1 : 0, set_delete: (s, value) => { if (s instanceof Set) s.delete(value); }, set_size: (s) => (s instanceof Set) ? s.size : 0, set_clear: (s) => { if (s instanceof Set) s.clear(); }, set_values: (s) => (s instanceof Set) ? [...s.values()] : [], // WeakRef / FinalizationRegistry are the host's; a Perry closure cleanup callback is // wrapped so the host can call it. weakref_new: (target) => new WeakRef(target), weakref_deref: (ref) => __reflectApply(WeakRef.prototype.deref, ref, []), finreg_new: (cb) => { const call = __arrayCallback(cb); return new FinalizationRegistry(call ? (held) => { call(__jsValueToBits(held)); } : cb); }, finreg_register: (reg, target, held, token) => __reflectApply(FinalizationRegistry.prototype.register, reg, [target, held, token]), finreg_unregister: (reg, token) => __reflectApply(FinalizationRegistry.prototype.unregister, reg, [token]), // The Set/Map `for...of` fast path steps `slot = cursor_next(coll, slot + 1, ep)`, // then `ep = compaction_epoch(coll)`, and reads the entry with `*_at(coll, slot)`; // -1 ends the loop. A host collection has no slots, so each loop owns a live iterator: // `ep` carries its id (0 before the first step) and the slot is that same id, so a // nested loop over the same collection cannot move it, and entries added or deleted // mid-loop behave as in a host loop. // The emitters release a loop's cursor around it (`cursor_release`): a loop that // ends by `break`, `return` or `throw` still holds it, so its iterator is closed. collection_cursor_next: (coll, _slot, ep) => { __collectionCursorLast = 0; if (!(coll instanceof Set || coll instanceof Map)) return -1; let loops = __collectionCursors.get(coll); if (!loops) __collectionCursors.set(coll, (loops = new Map())); let id = ep; let cur = loops.get(id); if (!cur) { id = ++__collectionCursorSeq; const iter = coll[Symbol.iterator](); cur = { iter, next: iter.next, value: undefined }; loops.set(id, cur); } // A step that throws ends the loop without closing the iterator, as a host loop does. // User code in `next` may step another loop, so only a successful step publishes `id`. try { const step = __reflectApply(cur.next, cur.iter, []); if (Object(step) !== step) throw new TypeError('Iterator result is not an object'); if (step.done) { loops.delete(id); __collectionCursorLast = 0; return -1; } cur.value = step.value; } catch (error) { loops.delete(id); __collectionCursorLast = 0; throw error; } __collectionCursorLast = id; return id; }, // Read right after `cursor_next`: the id of the loop it stepped. collection_cursor_epoch: () => __collectionCursorLast, // After a `throw` (`threw`), the body's error wins over any from closing (IteratorClose). collection_cursor_release: (coll, ep, threw) => { const loops = __collectionCursors.get(coll); const cur = loops && loops.get(ep); if (!cur) return; loops.delete(ep); try { const close = cur.iter.return; if (close == null) return; const result = __reflectApply(close, cur.iter, []); if (Object(result) !== result) throw new TypeError('Iterator result is not an object'); } catch (error) { if (!threw) throw error; } }, set_value_at: (s, slot) => __collectionCursorValue(s, slot), map_entry_key_at: (m, slot) => (__collectionCursorValue(m, slot) || [])[0], map_entry_value_at: (m, slot) => (__collectionCursorValue(m, slot) || [])[1], // Date — arg is a plain JS value (the Date object itself once created) date_new: () => new Date(), date_new_val: (...args) => new Date(...args), date_now: () => Date.now(), date_parse: (s) => Date.parse(String(s)), date_utc: (...args) => Date.UTC(...args.map(Number)), date_get_time: (d) => (d instanceof Date) ? d.getTime() : 0, date_value_of: (d) => (d instanceof Date) ? d.valueOf() : 0, date_to_iso_string: (d) => { if (!(d instanceof Date)) return undefined; return d.toISOString(); }, date_to_date_string: (d) => (d instanceof Date) ? d.toDateString() : '', date_to_time_string: (d) => (d instanceof Date) ? d.toTimeString() : '', date_to_locale_string: (d) => (d instanceof Date) ? d.toLocaleString() : '', date_to_locale_date_string: (d) => (d instanceof Date) ? d.toLocaleDateString() : '', date_to_locale_time_string: (d) => (d instanceof Date) ? d.toLocaleTimeString() : '', date_to_json: (d) => (d instanceof Date) ? d.toJSON() : null, date_to_utc_string: (d) => __reflectApply(Date.prototype.toUTCString, d, []), date_get_full_year: (d) => (d instanceof Date) ? d.getFullYear() : 0, date_get_month: (d) => (d instanceof Date) ? d.getMonth() : 0, date_get_date: (d) => (d instanceof Date) ? d.getDate() : 0, date_get_day: (d) => (d instanceof Date) ? d.getDay() : 0, date_get_hours: (d) => (d instanceof Date) ? d.getHours() : 0, date_get_minutes: (d) => (d instanceof Date) ? d.getMinutes() : 0, date_get_seconds: (d) => (d instanceof Date) ? d.getSeconds() : 0, date_get_milliseconds: (d) => (d instanceof Date) ? d.getMilliseconds() : 0, date_get_utc_full_year: (d) => (d instanceof Date) ? d.getUTCFullYear() : 0, date_get_utc_month: (d) => (d instanceof Date) ? d.getUTCMonth() : 0, date_get_utc_date: (d) => (d instanceof Date) ? d.getUTCDate() : 0, date_get_utc_day: (d) => (d instanceof Date) ? d.getUTCDay() : 0, date_get_utc_hours: (d) => (d instanceof Date) ? d.getUTCHours() : 0, date_get_utc_minutes: (d) => (d instanceof Date) ? d.getUTCMinutes() : 0, date_get_utc_seconds: (d) => (d instanceof Date) ? d.getUTCSeconds() : 0, date_get_utc_milliseconds: (d) => (d instanceof Date) ? d.getUTCMilliseconds() : 0, date_get_timezone_offset: (d) => (d instanceof Date) ? d.getTimezoneOffset() : 0, // A setter takes every argument the call passed and a non-Date receiver throws, as // the host's own setter does. date_set_utc_full_year: (d, ...v) => __reflectApply(Date.prototype.setUTCFullYear, d, v), date_set_utc_month: (d, ...v) => __reflectApply(Date.prototype.setUTCMonth, d, v), date_set_utc_date: (d, ...v) => __reflectApply(Date.prototype.setUTCDate, d, v), date_set_utc_hours: (d, ...v) => __reflectApply(Date.prototype.setUTCHours, d, v), date_set_utc_minutes: (d, ...v) => __reflectApply(Date.prototype.setUTCMinutes, d, v), date_set_utc_seconds: (d, ...v) => __reflectApply(Date.prototype.setUTCSeconds, d, v), date_set_utc_milliseconds: (d, ...v) => __reflectApply(Date.prototype.setUTCMilliseconds, d, v), date_set_full_year: (d, ...v) => __reflectApply(Date.prototype.setFullYear, d, v), date_set_month: (d, ...v) => __reflectApply(Date.prototype.setMonth, d, v), date_set_date: (d, ...v) => __reflectApply(Date.prototype.setDate, d, v), date_set_hours: (d, ...v) => __reflectApply(Date.prototype.setHours, d, v), date_set_minutes: (d, ...v) => __reflectApply(Date.prototype.setMinutes, d, v), date_set_seconds: (d, ...v) => __reflectApply(Date.prototype.setSeconds, d, v), date_set_milliseconds: (d, ...v) => __reflectApply(Date.prototype.setMilliseconds, d, v), date_set_time: (d, ...v) => __reflectApply(Date.prototype.setTime, d, v), // Error — args are plain JS values error_new: (msg) => new Error(msg === undefined ? undefined : String(msg)), error_new_kind: (kind, msg) => new ([Error, TypeError, RangeError, ReferenceError, SyntaxError][kind] || Error)(msg === undefined ? undefined : String(msg)), error_message: (e) => (e instanceof Error) ? e.message : '', // `super(...args)`: the parent's compiled constructor at table slot `ctorIdx` runs // on `self` with `args`, then the parent's captures `caps`. super_call_spread: (self, ctorIdx, args, caps) => { const impl = wasmInstance.exports.__indirect_function_table.get(ctorIdx | 0); __invokeConstructor(impl, caps.length, __jsValueToBits(self), ...[...args, ...caps].map(__jsValueToBits)); return undefined; }, // `super(msg)` in a class extending a built-in Error: the instance is a plain // object (see __classInstance), so it takes the message the Error constructor would // give it, non-enumerable as on a real Error. Returns the instance. error_super_init: (obj, msg) => { if (!obj || typeof obj !== 'object') return obj; Object.defineProperty(obj, 'message', { value: msg === undefined ? '' : String(msg), writable: true, configurable: true, enumerable: false, }); return obj; }, global_receiver: () => __hostGlobal, global_property: (name) => __hostGlobal[name], // RegExp — args are plain JS values regexp_new: (pattern, flags) => { try { return new RegExp(String(pattern), String(flags)); } catch (e) { if (tryDepth > 0) currentException = e; return undefined; } }, regexp_test: (re, str) => { if (!(re instanceof RegExp)) return 0; return re.test(String(str)) ? 1 : 0; }, // Globals — args are plain JS values number_coerce: (val) => Number(val), is_nan: (val) => Number.isNaN(val) ? 1 : 0, is_finite: (val) => Number.isFinite(val) ? 1 : 0, number_is_integer: (val) => Number.isInteger(val) ? 1 : 0, number_is_safe_integer: (val) => Number.isSafeInteger(val) ? 1 : 0, global_is_nan: (val) => isNaN(val) ? 1 : 0, global_is_finite: (val) => isFinite(val) ? 1 : 0, // A malformed sequence throws URIError, which mem_call routes to an enclosing try. encode_uri: (val) => encodeURI(val), decode_uri: (val) => decodeURI(val), encode_uri_component: (val) => encodeURIComponent(val), decode_uri_component: (val) => decodeURIComponent(val), // Try/Catch — args are plain JS values try_start: () => { tryDepth++; }, try_end: () => { if (tryDepth > 0) tryDepth--; }, throw_value: (val) => { currentException = val; }, propagate_exception: (callbackBoundary = false) => { if ((callbackBoundary || tryDepth === 0) && currentException !== NO_EXCEPTION) { const error = currentException; currentException = NO_EXCEPTION; throw error; } }, has_exception: () => currentException !== NO_EXCEPTION ? 1 : 0, get_exception: () => { const e = currentException; currentException = NO_EXCEPTION; return e !== NO_EXCEPTION ? e : undefined; }, // URL — args are plain JS values (u is the URL object itself) url_parse: (urlStr) => { try { return new URL(String(urlStr)); } catch (e) { if (tryDepth > 0) currentException = e; return undefined; } }, url_get_href: (u) => (u instanceof URL) ? u.href : undefined, url_get_pathname: (u) => (u instanceof URL) ? u.pathname : undefined, url_get_hostname: (u) => (u instanceof URL) ? u.hostname : undefined, url_get_port: (u) => (u instanceof URL) ? u.port : undefined, url_get_search: (u) => (u instanceof URL) ? u.search : undefined, url_get_hash: (u) => (u instanceof URL) ? u.hash : undefined, url_get_origin: (u) => (u instanceof URL) ? u.origin : undefined, url_get_protocol: (u) => (u instanceof URL) ? u.protocol : undefined, url_get_search_params: (u) => (u instanceof URL) ? u.searchParams : undefined, searchparams_get: (sp, key) => { if (!sp || typeof sp.get !== 'function') return null; const v = sp.get(String(key)); return v === null ? null : v; }, searchparams_has: (sp, key) => (sp && typeof sp.has === 'function' && sp.has(String(key))) ? 1 : 0, searchparams_set: (sp, key, val) => { if (sp && typeof sp.set === 'function') sp.set(String(key), String(val)); }, searchparams_append: (sp, key, val) => { if (sp && typeof sp.append === 'function') sp.append(String(key), String(val)); }, searchparams_delete: (sp, key) => { if (sp && typeof sp.delete === 'function') sp.delete(String(key)); }, searchparams_to_string: (sp) => sp ? sp.toString() : '', // Crypto — args are plain JS values crypto_random_uuid: () => crypto.randomUUID(), // Web Crypto has no `randomUUIDv7`: an RFC 9562 version-7 UUID is the 48-bit Unix // millisecond time, then random bits under the version and variant fields. crypto_random_uuidv7: () => { const b = crypto.getRandomValues(new Uint8Array(16)); let ms = Date.now(); for (let i = 5; i >= 0; i--, ms = Math.floor(ms / 256)) b[i] = ms % 256; b[6] = (b[6] & 0x0f) | 0x70; b[8] = (b[8] & 0x3f) | 0x80; const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join(''); return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`; }, crypto_random_bytes: (n) => crypto.getRandomValues(new Uint8Array(n)), crypto_sha256: (data) => { const str = String(data); const p = (typeof crypto !== 'undefined' && crypto.subtle) ? crypto.subtle.digest('SHA-256', new TextEncoder().encode(str)) .then(buf => [...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join('')) : Promise.resolve(undefined); return p; }, crypto_md5: () => undefined, // Path — args are plain JS strings path_join: (...parts) => parts.map(String).join('/').replace(/\/+/g, '/'), path_dirname: (str) => { const s = String(str); const idx = s.lastIndexOf('/'); return idx >= 0 ? (s.substring(0, idx) || '/') : '.'; }, path_basename: (str) => { const s = String(str); const idx = s.lastIndexOf('/'); return idx >= 0 ? s.substring(idx + 1) : s; }, path_extname: (str) => { const s = String(str); const base = s.substring(s.lastIndexOf('/') + 1); const idx = base.lastIndexOf('.'); return idx > 0 ? base.substring(idx) : ''; }, path_resolve: (str) => String(str), path_is_absolute: (str) => { const s = String(str); return (s.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(s)) ? 1 : 0; }, // Process/OS — return plain JS values os_platform: () => 'wasm', process_argv: () => [], process_cwd: () => '/', // Buffer/Uint8Array — args are plain JS values (buf is the Uint8Array itself) buffer_alloc: (size) => new Uint8Array(size), buffer_from_string: (str, encoding) => new TextEncoder().encode(String(str)), buffer_to_string: (buf, encoding) => { if (!buf || !(buf instanceof Uint8Array)) return ''; return new TextDecoder().decode(buf); }, buffer_get: (buf, idx) => (buf instanceof Uint8Array) ? buf[idx] : 0, buffer_set: (buf, idx, val) => { if (buf instanceof Uint8Array) buf[idx] = val; }, buffer_length: (buf) => (buf instanceof Uint8Array) ? buf.length : 0, buffer_slice: (buf, start, end) => { if (!(buf instanceof Uint8Array)) return new Uint8Array(0); return buf.slice(start, end === undefined ? undefined : end); }, buffer_concat: (arr) => { if (!Array.isArray(arr)) return new Uint8Array(0); const bufs = arr.map(v => (v instanceof Uint8Array) ? v : new Uint8Array(0)); const total = bufs.reduce((s, b) => s + b.length, 0); const result = new Uint8Array(total); let offset = 0; bufs.forEach(b => { result.set(b, offset); offset += b.length; }); return result; }, uint8array_new: (size) => new Uint8Array(size), uint8array_from: (val) => Uint8Array.from(val), uint8array_length: (buf) => (buf instanceof Uint8Array) ? buf.length : 0, uint8array_get: (buf, idx) => (buf instanceof Uint8Array) ? buf[idx] : 0, uint8array_set: (buf, idx, val) => { if (buf instanceof Uint8Array) buf[idx] = val; }, // Timers — closure arg is the closure object itself (decoded from POINTER_TAG) // Captures are already BigInt from closure_set_capture. // A host function (a bound one) is the host's own callback. set_timeout: (closure, delay) => { if (typeof closure === 'function') return setTimeout(closure, delay); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return 0; return setTimeout(() => { const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (fn) fn(...__padBigintArgs(fn, 0, [...closure.captures])); }, delay); }, set_interval: (closure, delay) => { if (typeof closure === 'function') return setInterval(closure, delay); if (!closure || typeof closure.funcIdx === 'undefined' || !wasmInstance) return 0; return setInterval(() => { const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (fn) fn(...__padBigintArgs(fn, 0, [...closure.captures])); }, delay); }, clear_timeout: (id) => { clearTimeout(id); }, clear_interval: (id) => { clearInterval(id); }, // Response — args are plain JS values (r is the Response object itself) response_status: (r) => (r && typeof r.status === 'number') ? r.status : 0, response_ok: (r) => (r && r.ok) ? 1 : 0, response_headers_get: (r, name) => { if (!r || !r.headers) return null; const v = r.headers.get(String(name)); return v === null ? null : v; }, response_url: (r) => (r && typeof r.url === 'string') ? r.url : undefined, // Buffer extras — args are plain JS values (src/tgt are Uint8Array objects) buffer_copy: (src, tgt, targetStart, sourceStart, sourceEnd) => { if (!(src instanceof Uint8Array) || !(tgt instanceof Uint8Array)) return 0; const ts = targetStart === undefined ? 0 : targetStart; const ss = sourceStart === undefined ? 0 : sourceStart; const se = sourceEnd === undefined ? src.length : sourceEnd; let copied = 0; for (let i = ss; i < se && (ts + copied) < tgt.length; i++) { tgt[ts + copied] = src[i]; copied++; } return copied; }, buffer_write: (buf, str, offset, encoding) => { if (!(buf instanceof Uint8Array)) return 0; const encoded = new TextEncoder().encode(String(str)); const off = offset === undefined ? 0 : offset; let written = 0; for (let i = 0; i < encoded.length && (off + i) < buf.length; i++) { buf[off + i] = encoded[i]; written++; } return written; }, buffer_equals: (a, b) => { if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array) || a.length !== b.length) return 0; for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return 0; return 1; }, buffer_is_buffer: (val) => (val instanceof Uint8Array || (val && val.constructor && val.constructor.name === 'Buffer')) ? 1 : 0, buffer_byte_length: (val) => { if (typeof val === 'string') return new TextEncoder().encode(val).length; if (val instanceof Uint8Array) return val.length; return 0; }, // Fetch/Promise — args are plain JS values fetch_url: (urlStr) => fetch(String(urlStr)), fetch_with_options: (urlStr, methodVal, bodyVal, headersVal, redirectVal) => { const url = String(urlStr); const opts = {}; if (methodVal !== undefined) opts.method = String(methodVal); if (bodyVal !== undefined) opts.body = String(bodyVal); if (headersVal && typeof headersVal === 'object') opts.headers = headersVal; if (redirectVal !== undefined) opts.redirect = String(redirectVal); return fetch(url, opts); }, response_json: (resp) => { if (!resp || typeof resp.json !== 'function') return undefined; return resp.json(); }, response_text: (resp) => { if (!resp || typeof resp.text !== 'function') return undefined; return resp.text(); }, promise_new: () => { let resolve; const p = new Promise(r => { resolve = r; }); p.__resolve = resolve; return p; }, promise_resolve: (p, value) => { if (p && p.__resolve) p.__resolve(value); }, promise_then: (p, closure) => { if (!p || !(p instanceof Promise)) return p; const newP = p.then(val => { if (closure && typeof closure.funcIdx !== 'undefined' && wasmInstance) { const fn = wasmInstance.exports.__indirect_function_table?.get(closure.funcIdx | 0); if (fn) return __bitsToJsValue(fn(...__padBigintArgs(fn, 0, [...closure.captures, __jsValueToBits(val)]))); } return val; }); return newP; }, await_promise: (val) => val, // === perry/thread — Web Worker parallelism === thread_parallel_map: (array, closure) => { if (!Array.isArray(array)) return []; return __threadPool.map(array, closure); }, thread_parallel_filter: (array, closure) => { if (!Array.isArray(array)) return []; return __threadPool.filter(array, closure); }, thread_spawn: (closure) => { return __threadPool.spawn(closure); }, }; // --- perry/thread Worker Pool --- // Manages a pool of Web Workers that each instantiate the WASM module. // Workers call compiled WASM functions via the indirect function table. const __threadPool = (() => { const MIN_PARALLEL_SIZE = 1024; let workers = null; let wasmBinary = null; // Minimal runtime bridge for workers — they need NaN-boxing, string table, handle store const WORKER_BOOTSTRAP = ` "use strict"; const TAG_UNDEFINED = 0x7FFC0000_00000001n; const TAG_NULL = 0x7FFC0000_00000002n; const TAG_FALSE = 0x7FFC0000_00000003n; const TAG_TRUE = 0x7FFC0000_00000004n; const STRING_TAG = 0x7FFFn; const POINTER_TAG = 0x7FFDn; const INT32_TAG = 0x7FFEn; const _convBuf = new ArrayBuffer(8); const _f64 = new Float64Array(_convBuf); const _u64 = new BigUint64Array(_convBuf); function f64ToU64(f) { _f64[0] = f; return _u64[0]; } function u64ToF64(u) { _u64[0] = u; return _f64[0]; } const stringTable = []; const handleStore = new Map(); let nextHandleId = 1; const objectHandles = new WeakMap(); function allocHandle(obj) { const reference = obj !== null && (typeof obj === 'object' || typeof obj === 'function'); if (reference && objectHandles.has(obj)) return objectHandles.get(obj); const id = nextHandleId++; if (reference) objectHandles.set(obj, id); handleStore.set(id, obj); return id; } function __bitsToJsValue(bits) { if (bits === TAG_UNDEFINED) return undefined; if (bits === TAG_NULL) return null; if (bits === TAG_TRUE) return true; if (bits === TAG_FALSE) return false; const tag = bits >> 48n; if (tag === STRING_TAG) return stringTable[Number(bits & 0xFFFFFFFFn)]; if (tag === POINTER_TAG) return handleStore.get(Number(bits & 0xFFFFFFFFn)); if (tag === INT32_TAG) return Number(BigInt.asIntN(32, bits & 0xFFFFFFFFn)); _u64[0] = bits; return _f64[0]; } function __jsValueToBits(v) { if (v === undefined) return TAG_UNDEFINED; if (v === null) return TAG_NULL; if (v === true) return TAG_TRUE; if (v === false) return TAG_FALSE; if (typeof v === "number") { _f64[0] = v; return _u64[0]; } if (typeof v === "string") { const id = stringTable.length; stringTable.push(v); return (STRING_TAG << 48n) | BigInt(id); } if (Array.isArray(v)) { const id = allocHandle(v); return (POINTER_TAG << 48n) | BigInt(id); } if (typeof v === "object") { const id = allocHandle(v); return (POINTER_TAG << 48n) | BigInt(id); } _f64[0] = Number(v); return _u64[0]; } let wasmInst = null; let wasmMem = null; self.onmessage = async function(e) { const msg = e.data; try { if (msg.type === "init") { // Build minimal imports for the WASM module const nan_temp = { value: 0x10000 }; const imports = { rt: {} }; // String init — workers need this for __init_strings imports.rt.string_new = (offset, len) => { const bytes = new Uint8Array(wasmMem.buffer, offset, len); stringTable.push(new TextDecoder().decode(bytes)); }; // Stubs for other imports — workers only call user functions via indirect table const noop = () => 0; const noopVoid = () => {}; // Create a Proxy to provide stubs for any import the WASM module needs imports.rt = new Proxy(imports.rt, { get(target, prop) { if (prop in target) return target[prop]; return noop; } }); const module = await WebAssembly.compile(msg.binary); const instance = await WebAssembly.instantiate(module, imports); wasmInst = instance; wasmMem = instance.exports.memory; // Initialize string table if (instance.exports.__init_strings) instance.exports.__init_strings(); self.postMessage({ type: "ready" }); return; } if (!wasmInst) { self.postMessage({ type: "error", message: "Worker not initialized" }); return; } const table = wasmInst.exports.__indirect_function_table; const fn = table.get(msg.funcIdx | 0); if (!fn) { self.postMessage({ type: "error", message: "Function not found at index " + msg.funcIdx }); return; } const captures = (msg.captures || []).map(c => __jsValueToBits(c)); // A rest parameter's exported slot counts the captures; the arguments from it on become one array. const rest = wasmInst.exports["__wasm_rest_" + fn.name]; const call = (...args) => { if (rest) { while (args.length < rest.value) args.push(TAG_UNDEFINED); args.push(__jsValueToBits(args.splice(rest.value).map(__bitsToJsValue))); } while (args.length < fn.length) args.push(TAG_UNDEFINED); return fn(...args); }; if (msg.type === "map") { const results = []; for (let i = 0; i < msg.chunk.length; i++) { const argBits = __jsValueToBits(msg.chunk[i]); const resultBits = call(...captures, argBits); results.push(__bitsToJsValue(resultBits)); } self.postMessage({ type: "result", data: results }); } else if (msg.type === "filter") { const filtered = []; for (let i = 0; i < msg.chunk.length; i++) { const argBits = __jsValueToBits(msg.chunk[i]); const resultBits = call(...captures, argBits); const jsResult = __bitsToJsValue(resultBits); // Truthy check matching Perry semantics if (jsResult !== undefined && jsResult !== null && jsResult !== false && jsResult !== 0 && jsResult !== "") { filtered.push(msg.chunk[i]); } } self.postMessage({ type: "result", data: filtered }); } else if (msg.type === "exec") { const resultBits = call(...captures); self.postMessage({ type: "result", data: __bitsToJsValue(resultBits) }); } } catch (err) { self.postMessage({ type: "error", message: String(err), stack: err && err.stack }); } }; `; function getConcurrency() { return (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) || 4; } function getWasmBinary() { if (wasmBinary) return wasmBinary; // Decode the base64 WASM that's stored on window by the HTML wrapper const b64 = (typeof window !== 'undefined' && window.__perryWasmB64) || null; if (!b64) return null; const raw = atob(b64); const bytes = new Uint8Array(raw.length); for (let i = 0; i < raw.length; i++) bytes[i] = raw.charCodeAt(i); wasmBinary = bytes.buffer; return wasmBinary; } function ensureWorkers(count) { if (workers && workers.length >= count) return workers; const binary = getWasmBinary(); if (!binary) return null; const blob = new Blob([WORKER_BOOTSTRAP], { type: 'text/javascript' }); const url = URL.createObjectURL(blob); workers = workers || []; const initPromises = []; while (workers.length < count) { const w = new Worker(url); const ready = new Promise((resolve, reject) => { const handler = (e) => { w.removeEventListener('message', handler); if (e.data.type === 'ready') resolve(); else reject(new Error(e.data.message || 'Worker init failed')); }; w.addEventListener('message', handler); }); w.postMessage({ type: 'init', binary }); workers.push({ worker: w, ready }); initPromises.push(ready); } return workers; } function dispatchToWorker(w, msg) { return new Promise((resolve, reject) => { const handler = (e) => { w.worker.removeEventListener('message', handler); if (e.data.type === 'error') reject(new Error(e.data.message)); else resolve(e.data.data); }; w.worker.addEventListener('message', handler); w.worker.postMessage(msg); }); } // Convert a closure object { funcIdx, captures } to plain JS captures for the worker function serializeClosure(closure) { if (!closure || typeof closure !== 'object') return { funcIdx: 0, captures: [] }; const captures = (closure.captures || []).map(c => { // captures are BigInt (NaN-boxed i64) — convert to plain JS values return __bitsToJsValue(typeof c === 'bigint' ? c : BigInt(c || 0)); }); return { funcIdx: closure.funcIdx | 0, captures }; } // Sequential fallback — runs on main thread, no workers function mapSequential(array, closure) { const table = wasmInstance?.exports?.__indirect_function_table; if (!table) return array; const fn = table.get(closure.funcIdx | 0); if (!fn) return array; const captures = (closure.captures || []); const results = []; for (let i = 0; i < array.length; i++) { const resultBits = fn(...captures, ...__padBigintArgs(fn, captures.length, [__jsValueToBits(array[i])])); results.push(__bitsToJsValue(resultBits)); } return results; } function filterSequential(array, closure) { const table = wasmInstance?.exports?.__indirect_function_table; if (!table) return array; const fn = table.get(closure.funcIdx | 0); if (!fn) return array; const captures = (closure.captures || []); const results = []; for (let i = 0; i < array.length; i++) { const resultBits = fn(...captures, ...__padBigintArgs(fn, captures.length, [__jsValueToBits(array[i])])); const jsResult = __bitsToJsValue(resultBits); if (jsResult !== undefined && jsResult !== null && jsResult !== false && jsResult !== 0 && jsResult !== "") { results.push(array[i]); } } return results; } function spawnSequential(closure) { const table = wasmInstance?.exports?.__indirect_function_table; if (!table) return undefined; const fn = table.get(closure.funcIdx | 0); if (!fn) return undefined; const captures = (closure.captures || []); return __bitsToJsValue(fn(...__padBigintArgs(fn, 0, [...captures]))); } return { map(array, closure) { const concurrency = getConcurrency(); if (typeof Worker === 'undefined' || array.length < MIN_PARALLEL_SIZE || concurrency <= 1 || !getWasmBinary()) { return mapSequential(array, closure); } const numChunks = __Math.min(concurrency, array.length); const ws = ensureWorkers(numChunks); if (!ws) return mapSequential(array, closure); const { funcIdx, captures } = serializeClosure(closure); const chunkSize = __Math.ceil(array.length / numChunks); const promises = []; for (let i = 0; i < numChunks; i++) { const chunk = array.slice(i * chunkSize, (i + 1) * chunkSize); if (chunk.length === 0) continue; const p = ws[i].ready.then(() => dispatchToWorker(ws[i], { type: 'map', funcIdx, captures, chunk }) ); promises.push(p); } return Promise.all(promises).then(results => { const out = []; for (const r of results) for (const item of r) out.push(item); return out; }); }, filter(array, closure) { const concurrency = getConcurrency(); if (typeof Worker === 'undefined' || array.length < MIN_PARALLEL_SIZE || concurrency <= 1 || !getWasmBinary()) { return filterSequential(array, closure); } const numChunks = __Math.min(concurrency, array.length); const ws = ensureWorkers(numChunks); if (!ws) return filterSequential(array, closure); const { funcIdx, captures } = serializeClosure(closure); const chunkSize = __Math.ceil(array.length / numChunks); const promises = []; for (let i = 0; i < numChunks; i++) { const chunk = array.slice(i * chunkSize, (i + 1) * chunkSize); if (chunk.length === 0) continue; const p = ws[i].ready.then(() => dispatchToWorker(ws[i], { type: 'filter', funcIdx, captures, chunk }) ); promises.push(p); } return Promise.all(promises).then(results => { const out = []; for (const r of results) for (const item of r) out.push(item); return out; }); }, spawn(closure) { if (typeof Worker === 'undefined' || !getWasmBinary()) { return Promise.resolve(spawnSequential(closure)); } const ws = ensureWorkers(1); if (!ws) return Promise.resolve(spawnSequential(closure)); const { funcIdx, captures } = serializeClosure(closure); return ws[0].ready.then(() => dispatchToWorker(ws[0], { type: 'exec', funcIdx, captures }) ); }, }; })(); // Exception state for try/catch bridge let tryDepth = 0; const NO_EXCEPTION = Symbol('no exception'); let currentException = NO_EXCEPTION; // ===== Perry UI Runtime (DOM-based, for --target wasm) ===== // Ported from perry-codegen-js/web_runtime.js — maps perry/ui widgets to DOM elements. const uiHandles = new Map(); // handle_id -> DOM element or state object const uiStates = new Map(); // handle_id -> { _value, subscribers[] } let uiNextHandle = 1; const perryImageCache = new Map(); function uiAlloc(el) { const h = uiNextHandle++; uiHandles.set(h, el); return h; } function uiGet(h) { return uiHandles.get(h); } function __invokeWasmCallback(fn, ...bigintArgs) { return __invokeConstructor(fn, 0, ...bigintArgs); } // `__invokeWasmCallback` of a constructor whose last `caps` arguments are its class's // captures. function __invokeConstructor(fn, caps, ...bigintArgs) { const result = fn(...__padBigintArgs(fn, 0, bigintArgs, caps)); __memDispatch.propagate_exception(true); return result; } // Accept a boxed handle or decoded closure; extra arguments are plain JS values. // Convert them once to i64: boxing strings through f64 can erase NaN payloads. function callWasmClosure(closureVal, ...extraArgs) { let closure; // A host function (a bound one) takes and returns plain values. if (typeof closureVal === 'function') return closureVal(...extraArgs); if (typeof closureVal === 'object' && closureVal !== null) { // Already a JS closure object (came through toJsValue) closure = closureVal; } else if (typeof closureVal === 'number') { if (isUndefined(closureVal) || isNull(closureVal)) return u64ToF64(TAG_UNDEFINED); closure = isPointer(closureVal) ? handleStore.get(getPointerId(closureVal)) : undefined; } if (!closure) return u64ToF64(TAG_UNDEFINED); if (typeof closure === 'function') return fromJsValue(closure(...extraArgs.map(v => typeof v === 'number' ? v : fromJsValue(v)))); if (typeof closure.funcIdx !== 'undefined' && wasmInstance) { const fn = wasmInstance.exports.__indirect_function_table.get(closure.funcIdx | 0); // Extra args need to be BigInt (i64) for WASM functions. Captures are already BigInt. const wasmArgs = extraArgs.map(v => __jsValueToBits(v)); if (fn) { const captures = closure.captures || []; const result = fn(...captures, ...__padBigintArgs(fn, captures.length, wasmArgs)); return (typeof result === 'bigint') ? __bitsToJsValue(result) : undefined; } } return u64ToF64(TAG_UNDEFINED); } // CSS Reset for UI (function() { const s = document.createElement("style"); s.textContent = ` *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } html, body { width: 100vw; height: 100vh; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; } #perry-root { display: flex; flex-direction: column; width: 100%; flex: 1 1 0%; min-height: 0; overflow: hidden; } button { cursor: pointer; padding: 6px 16px; border: 1px solid #ccc; border-radius: 6px; background: transparent; font: inherit; color: inherit; } button:hover { opacity: 0.85; } button:active { opacity: 0.7; } input[type="text"], input[type="tel"], input[type="email"], input[type="number"], input[type="url"], input[type="search"], input[type="password"], select, textarea { padding: 6px 10px; border: 1px solid #ccc; border-radius: 6px; font: inherit; } input[type="range"] { width: 100%; } hr { border: none; border-top: 1px solid #ddd; margin: 4px 0; } fieldset { border: 1px solid #ddd; border-radius: 8px; padding: 12px; } legend { font-weight: 600; padding: 0 6px; } details::details-content { display: flex; flex-direction: column; gap: inherit; } progress { width: 100%; } `; document.head.appendChild(s); })(); function uiGetRoot() { let r = document.getElementById("perry-root"); if (!r) { r = document.createElement("div"); r.id = "perry-root"; document.body.appendChild(r); } return r; } // State system function uiStateCreate(initial) { const h = uiNextHandle++; uiStates.set(h, { _value: initial, subscribers: [] }); return h; } function uiStateGet(h) { const s = uiStates.get(h); return s ? s._value : undefined; } function uiStateSet(h, value) { const s = uiStates.get(h); if (!s) return; s._value = value; const subs = s.subscribers.slice(); for (const sub of subs) { try { sub(value); } catch(e) { console.error("State subscriber error:", e); } } } // ---------- Widget creation functions (take JS values, return handle_id) ---------- // #1546: showToast(msg) — bottom-center fade-in/out toast for --target web. // HarmonyOS routes through `promptAction.showToast` via a runtime drain queue; // on web the previous behavior was a documented no-op. The implementation here // stays minimal: one host-managed div, queued messages, default 3s visible. function perry_ui_show_toast(message) { if (typeof document === "undefined") return; const msg = (message === null || message === undefined) ? "" : String(message); if (!perry_ui_show_toast._q) perry_ui_show_toast._q = []; perry_ui_show_toast._q.push(msg); if (perry_ui_show_toast._busy) return; perry_ui_show_toast._busy = true; const drain = () => { const next = perry_ui_show_toast._q.shift(); if (next === undefined) { perry_ui_show_toast._busy = false; return; } let el = perry_ui_show_toast._el; if (!el) { el = document.createElement("div"); el.setAttribute("data-perry-toast", ""); el.style.cssText = "position:fixed;left:50%;bottom:32px;transform:translateX(-50%);" + "background:rgba(28,28,30,0.92);color:#fff;padding:10px 18px;" + "border-radius:18px;font:14px -apple-system,system-ui,Segoe UI,Roboto,sans-serif;" + "max-width:80vw;box-shadow:0 6px 24px rgba(0,0,0,0.25);" + "opacity:0;transition:opacity .18s ease;pointer-events:none;z-index:2147483647;"; document.body.appendChild(el); perry_ui_show_toast._el = el; } el.textContent = next; requestAnimationFrame(() => { el.style.opacity = "1"; }); setTimeout(() => { el.style.opacity = "0"; setTimeout(drain, 200); }, 3000); }; drain(); } function perry_ui_app_create(titleOrOpts, _width, _height) { let title = "Perry App", bodyH; if (typeof titleOrOpts === "object" && titleOrOpts !== null) { title = titleOrOpts.title || title; bodyH = titleOrOpts.body; } else { title = titleOrOpts || title; } document.title = title; // The browser window is the app's window: `width` and `height` are a native // window's initial size, and the user resizes that window, so the root fills // the viewport instead of drawing a fixed-size box inside it. const root = uiGetRoot(); root.style.width = "100%"; root.style.height = "100%"; const h = uiAlloc(root); if (bodyH !== undefined) { const bodyEl = uiGet(bodyH); if (bodyEl) root.appendChild(bodyEl); } return h; } function perry_ui_vstack_create(spacing, children) { const el = document.createElement("div"); el.style.display = "flex"; el.style.flexDirection = "column"; if (spacing) el.style.gap = spacing + "px"; // A stack starts from its content's size, as a native one does; a sibling with a // low hugging priority takes the free space (widgetSetHugging). el.style.flex = "1 1 auto"; el.style.minHeight = "0"; el.style.overflow = "auto"; el._perryStack = true; const h = uiAlloc(el); if (Array.isArray(children)) { for (var i = 0; i < children.length; i++) { var c = uiGet(children[i]); if (c) { el.appendChild(c); uiStackItem(el, c); } } } return h; } function perry_ui_hstack_create(spacing, children) { const el = document.createElement("div"); el.style.display = "flex"; el.style.flexDirection = "row"; el.style.alignItems = "center"; if (spacing) el.style.gap = spacing + "px"; el._perryStack = true; const h = uiAlloc(el); if (Array.isArray(children)) { for (var i = 0; i < children.length; i++) { var c = uiGet(children[i]); if (c) { el.appendChild(c); uiStackItem(el, c); } } } return h; } function perry_ui_zstack_create() { const el = document.createElement("div"); el.style.position = "relative"; el.style.display = "flex"; el.style.flex = "1 1 0%"; return uiAlloc(el); } function perry_ui_text_create(text, id) { const el = document.createElement("span"); el.textContent = (text !== undefined && text !== null) ? String(text) : ""; const h = uiAlloc(el); // Issue #1392 — 2-arg `Text(content, id)` form. The state_desugar pass // emits `count.text()` as `Text("", "__state_N")`; the id string // is reused as the keyed-state key, so registering the widget here lets // `setText(id, …)` and `__state_set("__state_N", …)` find and re-render it. if (typeof id === "string" && id.length > 0) uiTextRegister(h, id); return h; } function perry_ui_button_create(label, callback) { const el = document.createElement("button"); el.textContent = (typeof label === 'string') ? label : String(label ?? ""); el._perryCallback = callback; el.addEventListener("click", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback); }); return uiAlloc(el); } function perry_ui_textfield_create(placeholder, callback) { const el = document.createElement("input"); el.type = "text"; el.placeholder = placeholder || ""; el._perryCallback = callback; el.addEventListener("input", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, el.value); }); return uiAlloc(el); } function perry_ui_securefield_create(placeholder, callback) { const el = document.createElement("input"); el.type = "password"; el.placeholder = placeholder || ""; el._perryCallback = callback; el.addEventListener("input", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, el.value); }); return uiAlloc(el); } function perry_ui_toggle_create(label, callback) { const wrap = document.createElement("label"); wrap.style.display = "flex"; wrap.style.alignItems = "center"; wrap.style.gap = "8px"; const inp = document.createElement("input"); inp.type = "checkbox"; wrap.appendChild(inp); if (label) { const sp = document.createElement("span"); sp.textContent = label; wrap.appendChild(sp); } wrap._perryCallback = callback; wrap._inp = inp; // `Toggle(label, onChange: (value: boolean) => void)`: the callback receives the // boolean itself, so `value === true` holds there. inp.addEventListener("change", () => { if (wrap._perryCallback !== undefined) callWasmClosure(wrap._perryCallback, inp.checked); }); return uiAlloc(wrap); } function perry_ui_slider_create(min, max, callback) { // Codegen emits 3-arg Slider(min, max, onChange); default initial=min const el = document.createElement("input"); el.type = "range"; el.min = min || 0; el.max = max || 100; el.value = el.min; el.step = "any"; el._perryCallback = callback; el.addEventListener("input", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, parseFloat(el.value)); }); return uiAlloc(el); } function perry_ui_scrollview_create() { const el = document.createElement("div"); el.style.overflow = "auto"; el.style.flex = "1 1 0%"; el.style.minHeight = "0"; return uiAlloc(el); } function perry_ui_spacer_create() { const el = document.createElement("div"); el.style.flex = "1 1 0%"; return uiAlloc(el); } function perry_ui_divider_create() { return uiAlloc(document.createElement("hr")); } function perry_ui_progressview_create(value) { const el = document.createElement("progress"); if (value !== undefined && value > 0) { el.max = 1; el.value = value; } return uiAlloc(el); } // perry/ui `Image(url, alt?)`, `Image({url, alt?})` and `Image({systemName})`, // routed as the native backends route them: to `image_create_url` / `_symbol`. function perry_ui_image_create(src, alt) { if (src && typeof src === "object") { if (src.systemName !== undefined) return perry_ui_image_create_symbol(src.systemName); return perry_ui_image_create_url(src.url, src.alt); } return perry_ui_image_create_url(src, alt); } function perry_ui_picker_create(callback) { const el = document.createElement("select"); el._perryCallback = callback; el.addEventListener("change", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, el.selectedIndex); }); return uiAlloc(el); } // DatePicker: an ``, whose value is already the ISO `yyyy-MM-dd` string // the native backends report. `year`/`month` are 1-based; out of range defaults to 2026-01. function __isoDate(year, month, day) { const y = (year >= 1 && year <= 9999) ? (year | 0) : 2026; const m = (month >= 1 && month <= 12) ? (month | 0) : 1; // A date input drops a date that does not exist, so the day stops at the month's last. const d = day >= 1 ? __Math.min(day | 0, new Date(Date.UTC(y, m, 0)).getUTCDate()) : 1; return String(y).padStart(4, "0") + "-" + String(m).padStart(2, "0") + "-" + String(d).padStart(2, "0"); } function perry_ui_date_picker_create(year, month, callback) { const el = document.createElement("input"); el.type = "date"; el.value = __isoDate(year, month, 1); el._perryCallback = callback; el.addEventListener("change", () => { if (el._perryCallback !== undefined && el.value) callWasmClosure(el._perryCallback, el.value); }); return uiAlloc(el); } function perry_ui_date_picker_set_date(h, year, month, day) { const el = uiGet(h); if (el) el.value = __isoDate(year, month, day); } function perry_ui_date_picker_get_selected_date(h) { const el = uiGet(h); return el ? el.value : undefined; } function perry_ui_wheel_picker_create(callback) { const el = document.createElement("select"); el.size = 5; el.className = "perry-wheel-picker"; el.style.overflowY = "auto"; el.style.scrollSnapType = "y mandatory"; el._perryCallback = callback; el.addEventListener("change", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, el.selectedIndex); }); return uiAlloc(el); } function perry_ui_form_create() { const el = document.createElement("fieldset"); el.style.display = "flex"; el.style.flexDirection = "column"; el.style.gap = "8px"; return uiAlloc(el); } // `Section(title)` or `Section(title, children)`: the children array appends like a // VStack's, after the legend. function perry_ui_section_create(title, children) { const el = document.createElement("fieldset"); el.style.display = "flex"; el.style.flexDirection = "column"; el.style.gap = "8px"; if (title) { const lg = document.createElement("legend"); lg.textContent = title; el.appendChild(lg); el._perryTitle = lg; } const h = uiAlloc(el); if (Array.isArray(children)) { for (var i = 0; i < children.length; i++) { var c = uiGet(children[i]); if (c) el.appendChild(c); } } return h; } // Disclosure: a `
` whose `` is the title; children added with // widgetAddChild show when the reader expands it. function perry_ui_disclosure_create(title) { const el = document.createElement("details"); const summary = document.createElement("summary"); summary.textContent = title === undefined ? "" : String(title); el.appendChild(summary); el._perryTitle = summary; el.style.gap = "8px"; return uiAlloc(el); } function perry_ui_navigationstack_create() { const el = document.createElement("div"); el.style.display = "flex"; el.style.flexDirection = "column"; el.style.flex = "1 1 0%"; el._navStack = []; return uiAlloc(el); } function perry_ui_canvas_create(width, height) { const el = document.createElement("canvas"); el.width = width || 300; el.height = height || 150; el._ctx = el.getContext("2d"); return uiAlloc(el); } // BloomView (issue #2395) — a render-surface host for an external GPU // renderer. The web has no HWND; bloomViewGetHwnd returns the handle id (the // surrogate the renderer keys off of) or 0 if unknown. function perry_ui_bloomview_create(width, height) { const el = document.createElement("canvas"); el.width = width || 300; el.height = height || 150; return uiAlloc(el); } function perry_ui_bloomview_get_hwnd(h) { return uiGet(h) ? h : 0; } function perry_ui_lazyvstack_create(count, renderClosure) { const el = document.createElement("div"); el.style.display = "flex"; el.style.flexDirection = "column"; el.style.overflow = "auto"; el.style.flex = "1 1 0%"; el._renderClosure = renderClosure; el._count = count; for (let i = 0; i < count; i++) { const childVal = callWasmClosure(renderClosure, i); const childEl = uiGet(typeof childVal === 'number' ? childVal : 0); if (childEl) el.appendChild(childEl); } return uiAlloc(el); } function perry_ui_lazyvstack_update(h, count) { const el = uiGet(h); if (!el) return; el.innerHTML = ''; el._count = count; for (let i = 0; i < count; i++) { const childVal = callWasmClosure(el._renderClosure, i); const childEl = uiGet(typeof childVal === 'number' ? childVal : 0); if (childEl) el.appendChild(childEl); } } function perry_ui_table_create(rowCount, colCount, renderClosure) { const table = document.createElement("table"); table.style.width = "100%"; table.style.borderCollapse = "collapse"; table._renderClosure = renderClosure; table._rowCount = rowCount; table._colCount = colCount; table._selectedRow = -1; const thead = document.createElement("thead"); table.appendChild(thead); const headerRow = document.createElement("tr"); thead.appendChild(headerRow); for (let c = 0; c < colCount; c++) { const th = document.createElement("th"); th.textContent = "Col " + c; headerRow.appendChild(th); } const tbody = document.createElement("tbody"); table.appendChild(tbody); __tableRenderRows(table); return uiAlloc(table); } // Fill a table's tbody with `_rowCount` rows from its render closure; shared by create // and `tableUpdateRowCount`, which re-renders every row the way lazyvstack_update does. function __tableRenderRows(table) { const tbody = table.querySelector("tbody"); tbody.innerHTML = ''; for (let r = 0; r < table._rowCount; r++) { const tr = document.createElement("tr"); tr.style.cursor = "pointer"; tr.addEventListener("click", () => { table._selectedRow = r; if (table._onRowSelect) callWasmClosure(table._onRowSelect, r); }); for (let c = 0; c < table._colCount; c++) { const td = document.createElement("td"); td.style.padding = "4px 8px"; td.style.borderBottom = "1px solid #eee"; const cellVal = callWasmClosure(table._renderClosure, r, c); const cellEl = uiGet(typeof cellVal === 'number' ? cellVal : 0); if (cellEl) td.appendChild(cellEl); else td.textContent = String(toJsValue(cellVal) ?? ""); tr.appendChild(td); } tbody.appendChild(tr); } } function perry_ui_table_set_column_header(h, col, title) { const t = uiGet(h); if (!t) return; const th = t.querySelector("thead tr")?.children[col]; if (th) th.textContent = title; } function perry_ui_table_set_column_width(h, col, width) { const t = uiGet(h); if (!t) return; const th = t.querySelector("thead tr")?.children[col]; if (th) th.style.width = width + "px"; } function perry_ui_table_update_row_count(h, count) { const t = uiGet(h); if (!t) return; t._rowCount = count | 0; if (t._selectedRow >= t._rowCount) t._selectedRow = -1; __tableRenderRows(t); } function perry_ui_table_set_on_row_select(h, cb) { const t = uiGet(h); if (t) t._onRowSelect = cb; } function perry_ui_table_get_selected_row(h) { const t = uiGet(h); return t ? t._selectedRow : -1; } function perry_ui_textarea_create(placeholder, callback) { const el = document.createElement("textarea"); el.placeholder = placeholder || ""; el._perryCallback = callback; el.addEventListener("input", () => { if (el._perryCallback !== undefined) callWasmClosure(el._perryCallback, el.value); }); return uiAlloc(el); } function perry_ui_textarea_set_string(h, text) { const el = uiGet(h); if (el) el.value = text; } function perry_ui_textarea_get_string(h) { const el = uiGet(h); return el ? el.value : ""; } // ---------- Child management ---------- function perry_ui_widget_add_child(parentH, childH) { const p = uiGet(parentH), c = uiGet(childH); if (p && c) { p.appendChild(c); uiStackItem(p, c); } } // Sizes `child` along `stack`'s main axis as the stack's distribution says // (stackSetDistribution): equal shares for FillEqually, shares by content for // FillProportionally. Under Fill, the default, a row gives its free width to a // child stack that has no hugging priority of its own, since a native stack view // hugs its content at low priority; widgetSetHugging decides for everything else. function uiStackItem(stack, child) { if (!stack._perryStack) return; const row = stack.style.flexDirection === "row"; const distribution = stack._perryDistribution; if (distribution === 1 || distribution === 2) { child.style.flex = distribution === 1 ? "1 1 0%" : "1 1 auto"; } else if (row && child._perryStack && child._perryHugging === undefined) { // Weight 1 on top of its content: a sibling with a low hugging priority of its // own (weight 250 - priority) takes nearly all the free width, as it would natively. child.style.flex = "1 1 auto"; } else return; if (row) child.style.minWidth = "0"; else child.style.minHeight = "0"; } // The widgets added to `el`: a Section's legend or a Disclosure's summary is its title. function uiChildren(el) { return Array.from(el.children).filter(c => c !== el._perryTitle); } function perry_ui_widget_remove_all_children(h) { const el = uiGet(h); if (!el) return; if (el._perryTitle) uiChildren(el).forEach(c => el.removeChild(c)); else el.innerHTML = ""; } function perry_ui_widget_remove_child(parentH, childH) { const p = uiGet(parentH), c = uiGet(childH); if (p && c && p.contains(c)) p.removeChild(c); } function perry_ui_widget_reorder_child(parentH, fromIdx, toIdx) { const p = uiGet(parentH); if (!p) return; const children = uiChildren(p); if (fromIdx >= 0 && fromIdx < children.length) { const [child] = children.splice(fromIdx, 1); p.removeChild(child); if (toIdx >= children.length) p.appendChild(child); else p.insertBefore(child, children[toIdx]); } } function perry_ui_widget_add_overlay(parentH, overlayH) { const p = uiGet(parentH), o = uiGet(overlayH); if (p && o) { o.style.position = "absolute"; p.appendChild(o); } } function perry_ui_widget_set_overlay_frame(overlayH, x, y, width, height) { const o = uiGet(overlayH); if (!o) return; o.style.left = x + "px"; o.style.top = y + "px"; o.style.width = width + "px"; o.style.height = height + "px"; } // ---------- Styling ---------- function perry_ui_set_background(h, r, g, b, a) { const el = uiGet(h); if (el) el.style.backgroundColor = `rgba(${r*255|0},${g*255|0},${b*255|0},${a})`; } function perry_ui_set_foreground(h, r, g, b, a) { const el = uiGet(h); if (el) el.style.color = `rgba(${r*255|0},${g*255|0},${b*255|0},${a})`; } function perry_ui_set_font_size(h, size) { const el = uiGet(h); if (el) el.style.fontSize = size + "px"; } function perry_ui_set_font_weight(h, weight) { const el = uiGet(h); if (el) el.style.fontWeight = weight; } // `textSetFontWeight(widget, size, weight)` sets the size and the weight together. // The weight is Apple's `systemFont(ofSize:weight:)` scale, which macOS uses as is: // regular 0, medium 0.23, semibold 0.3, bold 0.4, heavy 0.56, black 0.62, and the // light weights below 0. It is not linear, so CSS weights come from its named points. const __appleFontWeights = [[-0.8, 100], [-0.6, 200], [-0.4, 300], [0, 400], [0.23, 500], [0.3, 600], [0.4, 700], [0.56, 800], [0.62, 900]]; function uiCssFontWeight(weight) { const w = Number(weight) || 0; const points = __appleFontWeights; if (w <= points[0][0]) return points[0][1]; for (let i = 1; i < points.length; i++) { const [x1, y1] = points[i]; if (w <= x1) { const [x0, y0] = points[i - 1]; return __Math.round((y0 + (y1 - y0) * (w - x0) / (x1 - x0)) / 100) * 100; } } return 900; } function perry_ui_text_set_font_weight(h, size, weight) { const el = uiGet(h); if (!el) return; el.style.fontSize = size + "px"; el.style.fontWeight = String(uiCssFontWeight(weight)); } function perry_ui_set_font_family(h, family) { const el = uiGet(h); if (el) el.style.fontFamily = family; } function perry_ui_set_padding(h, top, left, bottom, right) { const el = uiGet(h); if (!el) return; if (left === undefined) left = bottom = right = top; el.style.padding = `${top}px ${right}px ${bottom}px ${left}px`; } function perry_ui_set_frame(h, width, height) { const el = uiGet(h); if (!el) return; if (width > 0) el.style.width = width + "px"; if (height > 0) el.style.height = height + "px"; } function perry_ui_set_corner_radius(h, radius) { const el = uiGet(h); if (el) el.style.borderRadius = radius + "px"; } function perry_ui_set_border(h, width, r, g, b, a) { const el = uiGet(h); if (el) el.style.border = `${width}px solid rgba(${r*255|0},${g*255|0},${b*255|0},${a})`; } // Joint border state for the Apple-style split setters // (`widgetSetBorderColor` / `widgetSetBorderWidth`). CSS won't render a // border unless style + color + width are all set in the same rule, so // we cache (color, width) per handle and re-apply `el.style.border` on // every change. Defaults match CALayer-ish behavior: missing color = // black, missing width = 1px. Issue #185 Phase B closure. const __perryBorderState = new Map(); function __perry_apply_border(h) { const el = uiGet(h); if (!el) return; const s = __perryBorderState.get(h) || {}; const color = s.color || [0, 0, 0, 1]; const width = s.width != null ? s.width : 1; const [r, g, b, a] = color; el.style.border = `${width}px solid rgba(${r*255|0},${g*255|0},${b*255|0},${a})`; } function perry_ui_widget_set_border_color(h, r, g, b, a) { const s = __perryBorderState.get(h) || {}; s.color = [r, g, b, a]; __perryBorderState.set(h, s); __perry_apply_border(h); } function perry_ui_widget_set_border_width(h, width) { const s = __perryBorderState.get(h) || {}; s.width = width; __perryBorderState.set(h, s); __perry_apply_border(h); } // Text decoration (issue #185 Phase B closure). 0=none, 1=underline, // 2=strikethrough. CSS `text-decoration` values map cleanly. function perry_ui_text_set_decoration(h, decoration) { const el = uiGet(h); if (!el) return; const css = decoration === 1 ? "underline" : decoration === 2 ? "line-through" : "none"; el.style.textDecoration = css; } // Issue #3621 — horizontal text alignment. Canonical Perry/AppKit values: // 0=left, 1=right, 2=center, 3=justified, 4=natural. Maps to CSS // `text-align` (natural → `start`, which follows the element's direction). // Lines beyond `lines` are cut with an ellipsis; 0 removes the limit. function perry_ui_text_set_number_of_lines(h, lines) { const el = uiGet(h); if (!el) return; const n = Number(lines); if (n > 0) { el.style.display = "-webkit-box"; el.style.webkitBoxOrient = "vertical"; el.style.webkitLineClamp = String(n); el.style.overflow = "hidden"; } else { el.style.webkitLineClamp = ""; el.style.overflow = ""; } } function perry_ui_text_set_text_alignment(h, alignment) { const el = uiGet(h); if (!el) return; const css = alignment === 1 ? "right" : alignment === 2 ? "center" : alignment === 3 ? "justify" : alignment === 4 ? "start" : "left"; el.style.textAlign = css; } // Issue #185 Phase B closure 11 — TextField borderless. Drops the // rendered border so the input visually matches a non-bordered // borderless setter on the Apple side. function perry_ui_textfield_set_borderless(h, borderless) { const el = uiGet(h); if (!el) return; el.style.border = borderless ? "none" : ""; // Also strip the default browser focus outline so "borderless" // really feels borderless. User can override with explicit // border / outline rules afterwards. el.style.outline = borderless ? "none" : ""; } // Issue #185 Phase B closure 11 — Stack alignment via CSS flex. // Numeric enum: 0=fill, 1=center, 2=leading, 3=trailing, 4=baseline. // Maps to CSS `align-items` (cross-axis on flex containers). // `alignment` is an NSLayoutAttribute, as on macOS where it goes to NSStackView // unchanged (docs/src/ui/layout.md): it places children on the cross axis. function perry_ui_stack_set_alignment(h, alignment) { const el = uiGet(h); if (!el) return; const value = { 1: "flex-start", 3: "flex-start", 5: "flex-start", // left, top, leading 2: "flex-end", 4: "flex-end", 6: "flex-end", // right, bottom, trailing 7: "stretch", 8: "stretch", // width, height 9: "center", 10: "center", // centerX, centerY 11: "last baseline", 12: "first baseline", }[Number(alignment)]; if (value) el.style.alignItems = value; } // Drop shadow (issue #185 Phase B closure 2). Same arg shape as the // Apple CALayer twin — `(r,g,b,a)` is shadow color, `blur` is shadow // radius, `(offsetX, offsetY)` is shadow offset (positive y = downward, // matching CALayer + native HTML semantics). function perry_ui_widget_set_shadow(h, r, g, b, a, blur, offsetX, offsetY) { const el = uiGet(h); if (!el) return; el.style.boxShadow = `${offsetX}px ${offsetY}px ${blur}px rgba(${r*255|0},${g*255|0},${b*255|0},${a})`; } function perry_ui_set_opacity(h, opacity) { const el = uiGet(h); if (el) el.style.opacity = opacity; } function perry_ui_set_enabled(h, enabled) { // On a Toggle the checkbox is the control: disabling its wrapper leaves the keyboard working. const el = uiGet(h); if (el) { (el._inp || el).disabled = !enabled; el.style.pointerEvents = enabled ? "" : "none"; el.style.opacity = enabled ? "" : "0.5"; } } function perry_ui_set_tooltip(h, text) { const el = uiGet(h); if (el) el.title = text || ""; } // On a Toggle the name goes on the checkbox: a screen reader ignores one on its wrapper. function perry_ui_widget_set_accessibility_label(h, text) { const el = uiGet(h); if (el) (el._inp || el).setAttribute("aria-label", String(text)); } // A `