diff --git a/packages/codemode/README.md b/packages/codemode/README.md index 83e2408bd64..4f20f14ef8d 100644 --- a/packages/codemode/README.md +++ b/packages/codemode/README.md @@ -97,6 +97,9 @@ denies it: the host function is never called, and the program catches the failur fails the way the call itself would: a tool call or asynchronous global rejects its promise, and a synchronous global throws. +`engine: "vm"` runs programs on the bytecode VM in `src/vm` instead of the default `"legacy"` interpreter. It is +experimental: `execute` has the same signature and results on both engines. + ### `Extension.make` Extensions are host functions a program calls directly as globals, such as `fetch`. Unlike tools they are not in the diff --git a/packages/codemode/bunfig.toml b/packages/codemode/bunfig.toml new file mode 100644 index 00000000000..786a3774447 --- /dev/null +++ b/packages/codemode/bunfig.toml @@ -0,0 +1,2 @@ +[test] +preload = ["./test/preload.ts"] diff --git a/packages/codemode/package.json b/packages/codemode/package.json index 34a41d3b3b3..b6b006bdcb6 100644 --- a/packages/codemode/package.json +++ b/packages/codemode/package.json @@ -24,7 +24,10 @@ "pack": "bun run script/pack.ts", "typecheck": "bun check --no-script", "typecheck:tsc": "tsc --noEmit", - "test": "bun test" + "test": "bun test && CODEMODE_ENGINE=vm bun test", + "test:legacy": "bun test", + "test:vm": "CODEMODE_ENGINE=vm bun test", + "test:roundtrip": "CODEMODE_ENGINE=vm CODEMODE_ROUNDTRIP=1 bun test" }, "dependencies": { "acorn": "8.15.0", diff --git a/packages/codemode/script/bench.ts b/packages/codemode/script/bench.ts new file mode 100644 index 00000000000..74e38cbc68c --- /dev/null +++ b/packages/codemode/script/bench.ts @@ -0,0 +1,148 @@ +#!/usr/bin/env bun + +// Interpreter benchmarks: median and fastest wall-clock milliseconds per program over several runs. +// Usage: bun run script/bench.ts [--engine vm|legacy] [--iterations N] [--filter name] [--mixed] [--jsc] + +import { numberOfDFGCompiles, reoptimizationRetryCount } from "bun:jsc" +import { Effect, Schema } from "effect" +import { CodeMode, Tool } from "../src/index.js" +import { Vm } from "../src/vm/vm.js" + +const cases: ReadonlyArray = [ + [ + "numeric loop", + `let sum = 0 +for (let i = 0; i < 300000; i++) sum = (sum + i * 3) % 1000003 +return sum`, + ], + [ + "closure recursion (fib 22)", + `const fib = (n) => (n < 2 ? n : fib(n - 1) + fib(n - 2)) +return fib(22)`, + ], + [ + "array callbacks (50k)", + `const items = Array.from({ length: 50000 }, (_, i) => (i * 7919) % 50000) +const mapped = items.map((x) => x * 2).filter((x) => x % 3 !== 0) +const total = mapped.reduce((a, b) => a + b, 0) +const sorted = mapped.slice(0, 20000).sort((a, b) => a - b) +return [total, sorted[0], sorted[sorted.length - 1]]`, + ], + [ + "promise fan-out (5k)", + `const work = async (i) => { await null; return i * 2 } +const results = await Promise.all(Array.from({ length: 5000 }, (_, i) => work(i))) +return results.length`, + ], + [ + "tool fan-out (1k)", + `const results = await Promise.all(Array.from({ length: 1000 }, (_, i) => tools.echo({ id: i }))) +return results.length`, + ], + [ + "JSON round-trip", + `const make = (depth) => depth === 0 ? { a: 1, b: "two", c: [1, 2, 3], d: null } : { left: make(depth - 1), right: make(depth - 1), list: [depth, String(depth)] } +let size = 0 +for (let i = 0; i < 20; i++) size += JSON.stringify(JSON.parse(JSON.stringify(make(9)))).length +return size`, + ], + [ + "generator iteration", + `function* range(n) { for (let i = 0; i < n; i++) yield i } +let sum = 0 +for (const x of range(50000)) sum += x +return sum`, + ], + [ + "string building", + `let out = "" +for (let i = 0; i < 50000; i++) out += String.fromCharCode(97 + (i % 26)) +const parts = [] +for (let i = 0; i < 20000; i++) parts.push(\`item-\${i}\`) +return out.length + parts.join(",").length`, + ], + [ + "Map/Set ops", + `const map = new Map() +const set = new Set() +for (let i = 0; i < 30000; i++) { map.set("k" + (i % 5000), i); set.add(i % 7000) } +let sum = 0 +for (const [k, v] of map) sum += v +for (const v of set) sum += v +for (let i = 0; i < 5000; i++) { map.delete("k" + i); set.delete(i) } +return sum + map.size + set.size`, + ], +] + +const argument = (flag: string) => { + const index = process.argv.indexOf(flag) + return index === -1 ? undefined : process.argv[index + 1] +} +const iterations = Number(argument("--iterations") ?? 7) +const filter = argument("--filter") +const engine = (argument("--engine") as "legacy" | "vm" | undefined) ?? "vm" +const mixed = process.argv.includes("--mixed") +const jsc = mixed || process.argv.includes("--jsc") +const exec = Vm.prototype["exec"] + +const runtime = CodeMode.make({ + engine, + tools: { + echo: Tool.make({ + description: "Echo an id", + input: Schema.Struct({ id: Schema.Number }), + output: Schema.Number, + execute: ({ id }) => Effect.succeed(id), + }), + }, +}) + +const median = (values: Array) => { + const sorted = [...values].sort((a, b) => a - b) + return sorted[Math.floor(sorted.length / 2)]! +} + +const run = (code: string) => runtime.execute(code) + +const selected = cases.filter(([name]) => filter === undefined || name.includes(filter)) + +const formatRow = (name: string, times: Array) => { + const base = `${name.padEnd(28)} ${median(times).toFixed(1).padStart(8)} ms (min ${Math.min(...times).toFixed(1)} ms)` + if (!jsc) return base + return `${base} [dfg=${numberOfDFGCompiles(exec)} retries=${reoptimizationRetryCount(exec)}]` +} + +if (mixed) { + // Pollute profiles in reverse order first, then interleave cases with a stride across rounds. + for (let i = selected.length - 1; i >= 0; i--) { + const [name, code] = selected[i]! + const warm = await Effect.runPromise(run(code)) + if (warm.ok !== true) throw new Error(`${name} failed: ${JSON.stringify(warm)}`) + } + const recorded = selected.map((): Array => []) + for (let round = 0; round < iterations; round++) { + for (let step = 0; step < selected.length; step++) { + const idx = (step * 5 + round * 3) % selected.length + const [, code] = selected[idx]! + const start = performance.now() + await Effect.runPromise(run(code)) + recorded[idx]!.push(performance.now() - start) + } + } + for (let i = 0; i < selected.length; i++) { + console.log(formatRow(selected[i]![0], recorded[i]!)) + } +} else { + for (const [name, code] of selected) { + // One warm-up run, which also checks the program succeeds. + const warm = await Effect.runPromise(run(code)) + if (warm.ok !== true) throw new Error(`${name} failed: ${JSON.stringify(warm)}`) + const times = Array.from({ length: iterations }, () => 0) + for (const index of times.keys()) { + const start = performance.now() + await Effect.runPromise(run(code)) + times[index] = performance.now() - start + } + console.log(formatRow(name, times)) + } +} diff --git a/packages/codemode/script/build.ts b/packages/codemode/script/build.ts index 323a63ddf96..ac0348e7e42 100644 --- a/packages/codemode/script/build.ts +++ b/packages/codemode/script/build.ts @@ -6,4 +6,5 @@ import { fileURLToPath } from "node:url" process.chdir(fileURLToPath(new URL("..", import.meta.url))) await $`rm -rf dist` +await $`bun run script/prelude.ts` await $`bun tsc -p tsconfig.build.json` diff --git a/packages/codemode/script/disasm.ts b/packages/codemode/script/disasm.ts new file mode 100644 index 00000000000..1d031790042 --- /dev/null +++ b/packages/codemode/script/disasm.ts @@ -0,0 +1,261 @@ +#!/usr/bin/env bun + +// The bytecode disassembler: pcs, ops, constants, handler and finally tables, local names, source lines. +import { sourceLocation } from "../src/vm/runtime/model.js" +import type { Code } from "../src/vm/code.js" +import { compileSource } from "../src/vm/compiler/compile.js" +import { + BinOpSymbols, + CheckBaseKind, + CodeFlags, + CondKind, + Op, + OpNames, + PutKind, + UnaryOpSymbols, +} from "../src/vm/opcodes.js" +import { parseProgram } from "../src/vm/runtime/execute.js" + +const formatFlags = (flags: number): string => { + const names: Array = [] + if (flags & CodeFlags.PROGRAM) names.push("PROGRAM") + if (flags & CodeFlags.ARROW) names.push("ARROW") + if (flags & CodeFlags.ASYNC) names.push("ASYNC") + if (flags & CodeFlags.GENERATOR) names.push("GENERATOR") + if (flags & CodeFlags.SIMPLE_PARAMS) names.push("SIMPLE_PARAMS") + if (flags & CodeFlags.USES_THIS) names.push("USES_THIS") + if (flags & CodeFlags.USES_ARGUMENTS) names.push("USES_ARGUMENTS") + if (flags & CodeFlags.INTERNAL) names.push("INTERNAL") + if (flags & CodeFlags.NEEDS_ARGS) names.push("NEEDS_ARGS") + return names.join("|") || "NONE" +} + +export const disassembleCode = (c: Code): string => { + const lines: Array = [] + const header = `=== fn#${c.index} ${c.name || ""} (params=${c.params}, locals=${c.locals}, maxStack=${c.maxStack}, flags=${formatFlags(c.flags)}) ===` + lines.push(header) + if (c.localNames.length > 0) { + lines.push(`locals: ${c.localNames.map((n, i) => `${i}:${n}`).join(", ")}`) + } + if (c.captures.length > 0) { + const caps: Array = [] + for (let i = 0, j = 0; i < c.captures.length; i += 2, j++) { + const from = c.captures[i] === 0 ? "local" : "upval" + const idx = c.captures[i + 1]! + caps.push(`${j}:${c.upvalNames[j] ?? "?"}(${from}[${idx}])`) + } + lines.push(`upvals: ${caps.join(", ")}`) + } + + const words = c.code + for (let pc = 0; pc < words.length; pc++) { + const w = words[pc]! + const op = w & 0xff + let a = (w >>> 8) & 0xff + let bx = w >>> 16 + if (op !== Op.EXTRA && pc > 0 && (words[pc - 1]! & 0xff) === Op.EXTRA) { + const w1 = words[pc - 1]! + const w0 = pc > 1 ? words[pc - 2]! : 0 + a = (w0 & 0xff) === Op.EXTRA ? ((w0 >>> 16) << 16) | (w1 & 0xff00) | a : (w1 & 0xff00) | a + bx = ((w1 >>> 16) << 16) | bx + } + const sbx = w >> 16 + const opName = OpNames[op] ?? `OP_${op}` + const operand = formatOperand(c, op, a, bx, sbx) + const siteNode = c.siteAt(pc) + const loc = siteNode?.loc ? sourceLocation(siteNode) : undefined + const locText = loc ? ` ; L${loc.line}:${loc.column}` : "" + lines.push(`${String(pc).padStart(4)} ${opName.padEnd(18)} ${operand.padEnd(28)}${locText}`.trimEnd()) + } + + if (c.handlers.length > 0) { + const hList: Array = [] + for (let i = 0; i < c.handlers.length; i += 4) { + hList.push(`[${c.handlers[i]}, ${c.handlers[i + 1]}) -> ${c.handlers[i + 2]} (depth ${c.handlers[i + 3]})`) + } + lines.push(`handlers: ${hList.join(", ")}`) + } + + if (c.finallies.length > 0) { + const fList: Array = [] + for (let level = 0; level < c.finallyOffsets.length; level++) { + const off = c.finallyOffsets[level]! + const tag = c.finallies[off]! + const val = c.finallies[off + 1]! + const depth = c.finallies[off + 2]! + const n = c.finallies[off + 3]! + const targets: Array = [] + for (let k = 0; k < n; k++) targets.push(c.finallies[off + 4 + k]!) + fList.push(`#${level}{tag=${tag}, val=${val}, depth=${depth}, targets=[${targets.join(", ")}]}`) + } + lines.push(`finallies: ${fList.join(", ")}`) + } + + return lines.join("\n") +} + +const formatOperand = (c: Code, op: number, a: number, bx: number, sbx: number): string => { + switch (op) { + case Op.PUSH_UNDEF: + case Op.PUSH_NULL: + case Op.POP: + case Op.DUP: + case Op.NEW_OBJECT: + case Op.COPY_DATA_PROPS: + case Op.PUT_ELEM: + case Op.DELETE_ELEM: + case Op.TO_KEY: + case Op.IN: + case Op.INSTANCEOF: + case Op.ADD: + case Op.SUB: + case Op.MUL: + case Op.LT: + case Op.LE: + case Op.GT: + case Op.GE: + case Op.SEQ: + case Op.SNE: + case Op.NOT: + case Op.THROW: + case Op.RETURN: + case Op.END_PROGRAM: + case Op.NOP: + case Op.CALL_SPREAD: + case Op.NEW_SPREAD: + case Op.FOR_IN: + case Op.GENERATOR_START: + return "" + case Op.PUSH_BOOL: + return a === 1 ? "true" : "false" + case Op.PUSH_INT: + return String(sbx) + case Op.PUSH_CONST: + return `${bx} (${JSON.stringify(c.consts[bx])})` + case Op.POPN: + case Op.NEW_ARRAY: + case Op.CONCAT: + case Op.COPY_REST: + case Op.END_FINALLY: + case Op.EXTRA: + return a === 0 ? String(bx) : `a=${a} bx=${bx}` + case Op.PICK: + case Op.ROT: + case Op.CALL: + return String(a) + case Op.NEW: + return a === 255 ? "check" : String(a) + case Op.GET_LOCAL: + case Op.HOLE_LOCAL: + case Op.COPY_CELL: + case Op.GET_CELL: + return `${bx} (${c.localNames[bx] ?? "?"})` + case Op.NEW_CELL: + return `${a === 1 ? "wrap " : ""}${bx} (${c.localNames[bx] ?? "?"})` + case Op.PUT_LOCAL: + case Op.PUT_CELL: { + const prefix = a === PutKind.INIT ? ".init " : a === PutKind.CONST ? ".const " : "" + return `${prefix}${bx} (${c.localNames[bx] ?? "?"})` + } + case Op.GET_UPVAL: + return `${bx} (${c.upvalNames[bx] ?? "?"})` + case Op.PUT_UPVAL: { + const prefix = a === PutKind.INIT ? ".init " : a === PutKind.CONST ? ".const " : "" + return `${prefix}${bx} (${c.upvalNames[bx] ?? "?"})` + } + case Op.GET_GLOBAL: { + const prefix = a === 1 ? ".typeof " : a === 2 ? ".put " : "" + return `${prefix}${JSON.stringify(c.consts[bx])}` + } + case Op.GET_INTRINSIC: + return `${JSON.stringify(c.consts[bx])}` + case Op.ARGS: + return a === 0 ? "arguments" : a === 1 ? `rest(${bx})` : a === 2 ? "self" : a === 3 ? "this" : `arg[${bx}]` + case Op.CLOSURE: + return `fn#${bx}` + case Op.SET_NAME: + return a === 0 ? JSON.stringify(c.consts[bx]) : "(computed)" + case Op.GET_PROP: { + const prefix = a === 1 ? ".keep " : a === 2 ? ".destr " : "" + return `${prefix}${JSON.stringify(c.consts[bx])}` + } + case Op.GET_ELEM: + return a === 1 ? ".keep" : a === 2 ? ".destr" : "" + case Op.PUT_PROP: + case Op.DELETE_PROP: + return JSON.stringify(c.consts[bx]) + case Op.DEFINE_FIELD: + return a === 0 ? JSON.stringify(c.consts[bx]) : "(computed)" + case Op.CHECK_BASE: + return a === CheckBaseKind.READ + ? ".read" + : a === CheckBaseKind.WRITE_PROP + ? `.writeProp ${JSON.stringify(c.consts[bx])}` + : a === CheckBaseKind.WRITE_ELEM + ? ".writeElem" + : ".destructure" + case Op.BINOP: + return BinOpSymbols[a] ?? String(a) + case Op.UNARY: + return UnaryOpSymbols[a] ?? String(a) + case Op.INC: + return a === 0 ? "+1" : "-1" + case Op.JUMP: + case Op.LOOP: + case Op.JUMP_IF_FALSE: + case Op.JUMP_IF_TRUE: + return `-> ${bx}` + case Op.JUMP_COND: { + const kind = + a === CondKind.TRUTHY_KEEP + ? ".truthyKeep" + : a === CondKind.FALSY_KEEP + ? ".falsyKeep" + : a === CondKind.NOT_NULLISH_KEEP + ? ".notNullishKeep" + : a === CondKind.DEFINED + ? ".defined" + : a === CondKind.NULLISH + ? ".nullish" + : ".nullishMethod" + return `${kind} -> ${bx}` + } + case Op.ITER_STEP: + case Op.ITER_RESULT: + case Op.ITER_CLOSE: + case Op.ITER_APPEND_ALL: + case Op.YSTAR: + case Op.YSTAR_RESULT: + return `slot=${a} -> ${bx}` + case Op.AWAIT: + return a === 1 ? ".unlocated" : "" + case Op.YIELD: + return "" + case Op.RESUME: + return `-> ${bx}` + default: + return `a=${a} bx=${bx}` + } +} + +export const disassemble = (codes: ReadonlyArray | Code): string => { + const list = Array.isArray(codes) ? codes : [codes] + return list.map(disassembleCode).join("\n\n") +} + +export const disassembleSource = (sourceText: string): string => { + const parsed = parseProgram(sourceText) + if (!parsed.ok) throw new Error(parsed.error.message) + return disassemble(compileSource(parsed.source)) +} + +if (import.meta.main) { + const arg = process.argv[2] + if (!arg) { + console.error("Usage: bun run script/disasm.ts ") + process.exit(1) + } + const file = Bun.file(arg) + const source = (await file.exists()) ? await file.text() : arg + console.log(disassembleSource(source)) +} diff --git a/packages/codemode/script/prelude.ts b/packages/codemode/script/prelude.ts new file mode 100644 index 00000000000..ca4c5c92587 --- /dev/null +++ b/packages/codemode/script/prelude.ts @@ -0,0 +1,27 @@ +#!/usr/bin/env bun + +// Compiles the built-in sources (the prelude, the VM's internal helpers) ahead of time into src/vm/prelude/bytecode.json, +// which a process loads instead of parsing and compiling them (see src/vm/prelude/bytecode.ts). Run it after changing +// either source or the compiler; test/vm/prelude-bytecode.test.ts fails until then. + +import { parse } from "acorn" +import { Source } from "../src/vm/runtime/source.js" +import { saveCodes } from "../src/vm/prelude/bytecode.js" +import { code } from "../src/vm/prelude/source.js" +import { compileSource } from "../src/vm/compiler/compile.js" +import { INTERNAL_SOURCE } from "../src/vm/internal-prelude.js" + +export const compileBuiltins = () => + Object.fromEntries( + [ + ["@prelude", code], + ["@vm-internal", INTERNAL_SOURCE], + ].map(([id, text]) => { + const source = new Source(id!, text!, parse(text!, { ecmaVersion: "latest", sourceType: "script" })) + return [id, saveCodes(text!, compileSource(source, { internal: true }))] + }), + ) + +if (import.meta.main) { + await Bun.write(new URL("../src/vm/prelude/bytecode.json", import.meta.url), `${JSON.stringify(compileBuiltins())}\n`) +} diff --git a/packages/codemode/script/setup.ts b/packages/codemode/script/setup.ts new file mode 100644 index 00000000000..4fe6b40d1dd --- /dev/null +++ b/packages/codemode/script/setup.ts @@ -0,0 +1,37 @@ +#!/usr/bin/env bun + +// Per-run setup cost: making a runtime, and running a trivial program end to end, in microseconds. +// Usage: bun run script/setup.ts [--iterations N] + +import { Effect } from "effect" +import { CodeMode } from "../src/index.js" +import { Interpreter } from "../src/vm/runtime/interpreter.js" +import { createBuiltins } from "../src/vm/runtime/intrinsics.js" +import { ToolRuntime } from "../src/tool-runtime.js" + +const index = process.argv.indexOf("--iterations") +const iterations = index === -1 ? 2000 : Number(process.argv[index + 1]) + +const runtime = CodeMode.make({ tools: {}, engine: "vm" }) +const prepared = ToolRuntime.prepare({}) + +const measure = async (name: string, work: () => unknown) => { + for (let at = 0; at < 200; at++) await work() + const times: Array = [] + for (let at = 0; at < iterations; at++) { + const start = performance.now() + await work() + times.push(performance.now() - start) + } + times.sort((a, b) => a - b) + const micros = (ms: number) => `${(ms * 1000).toFixed(0).padStart(5)} µs` + console.log( + `${name.padEnd(24)} median ${micros(times[times.length >> 1]!)} p10 ${micros(times[Math.floor(times.length / 10)]!)}`, + ) +} + +await measure( + "runtime", + () => new Interpreter({ tools: ToolRuntime.make(prepared, undefined, {}), builtins: createBuiltins() }), +) +await measure("trivial program", () => Effect.runPromise(runtime.execute("return 1"))) diff --git a/packages/codemode/src/codemode.ts b/packages/codemode/src/codemode.ts index f9d7a877029..98f8b90f9b5 100644 --- a/packages/codemode/src/codemode.ts +++ b/packages/codemode/src/codemode.ts @@ -7,6 +7,7 @@ import { globalNames } from "./interpreter/globals.js" import { scanTools } from "./scan.js" import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js" import type { Tools } from "./tools.js" +import type { VmEngine } from "./vm/index.js" export { scanTools } @@ -54,6 +55,8 @@ export type Options = {}, Extensions ex extensions?: Extensions /** Resource limits enforced on each execution. */ limits?: ExecutionLimits + /** Interpreter that runs programs. `"vm"` is the experimental bytecode VM; the default is `"legacy"`. */ + engine?: "legacy" | "vm" } /** The Effect environment an execution needs: what the tools and the extensions' Effect globals require. */ @@ -171,6 +174,7 @@ export const make = < ) const limits = resolveExecutionLimits(options.limits) const extensions = options.extensions ?? [] + // Both engines bind the same global names; test/engines.test.ts keeps them in step. const bound = new Set(globalNames) for (const extension of extensions) { for (const name of Object.keys(extension.globals)) { @@ -183,9 +187,17 @@ export const make = < return prepared.catalog }, execute: (code, bindings) => - executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) => [ - ...Object.entries(bindings ?? {}).map(([name, value]) => [name, fromJson(ctx, value)] as const), - ...extensionGlobals(ctx, extensions), - ]), + options.engine === "vm" + ? Effect.flatMap(loadVm(), (vm) => + vm.execute(code, prepared, limits, options.hooks ?? {}, extensions, bindings), + ) + : executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) => [ + ...Object.entries(bindings ?? {}).map(([name, value]) => [name, fromJson(ctx, value)] as const), + ...extensionGlobals(ctx, extensions), + ]), } } + +// The VM is loaded on first use so the default engine pulls in none of its code. +let vmEngine: Promise | undefined +const loadVm = () => Effect.promise(() => (vmEngine ??= import("./vm/index.js"))) diff --git a/packages/codemode/src/vm/async.ts b/packages/codemode/src/vm/async.ts new file mode 100644 index 00000000000..9a323ee02f4 --- /dev/null +++ b/packages/codemode/src/vm/async.ts @@ -0,0 +1,452 @@ +import type { Diagnostic } from "../codemode.js" +import { normalizeError, locate } from "./runtime/errors.js" +import { MAX_PENDING_PROMISES } from "./runtime/limits.js" +import { type AstNode, IteratorSymbol, rangeError, typeError } from "./runtime/model.js" +import { + Arr, + BoundFn, + type Callable, + Fn, + get, + iteratorResult, + Obj, + Position, + PromiseObj, + Resolver, + type Value, +} from "./runtime/objects.js" +import { builtinCursor, isThenable } from "./runtime/operations.js" +import { adoptThenable, capability, finishCombination, react } from "./runtime/promises.js" +import { describeValue, typeofValue } from "./runtime/references.js" +import { abrupt, type Completion, Interruption, Invoke, MAX_CALL_DEPTH, normal } from "./runtime/signals.js" +import { type Combination, type Job, type Reaction, Resolving } from "./scheduler.js" +import { internalCode, VmIntrinsic } from "./internal-prelude.js" +import { CodeFlags } from "./opcodes.js" +import { type Frame, type FrameMode, Mode, type Vm } from "./vm.js" + +export type SettlingTarget = { + resolve(value: Value): void + reject(reason: unknown): void +} + +/** + * Target for a `Promise.prototype.finally` cleanup frame running in `Mode.SETTLE`. + * When the cleanup completes with `result`, chains `finallyCleaned` and `finallyOutcome` reactions. + */ +export class FinallyTarget implements SettlingTarget { + constructor( + readonly vm: Vm, + readonly made: Resolving, + readonly site: AstNode | undefined, + readonly outcome: Completion, + ) {} + + resolve(result: Value): void { + const after = this.vm.scheduler.promiseResolve(result) + const derived = capability(this.vm.ctx, this.site) + this.vm.scheduler.then(after, { kind: "finallyCleaned", target: derived }) + this.vm.scheduler.then(derived.promise, { + kind: "finallyOutcome", + target: this.made, + outcome: this.outcome, + }) + } + + reject(reason: unknown): void { + this.made.reject(reason) + } +} + +/** + * Target for `$vmAsyncFromSyncClose`: regardless of whether `iterator.return()` succeeds or throws, + * the original rejection reason wins. + */ +export class AsyncFromSyncCloseTarget implements SettlingTarget { + constructor( + readonly made: Resolving, + readonly reason: unknown, + ) {} + + resolve(_value: Value): void { + this.made.reject(this.reason) + } + + reject(_error: unknown): void { + this.made.reject(this.reason) + } +} + +type Parked = { + readonly promise: PromiseObj + readonly node: AstNode | undefined + readonly result: Value +} + +export class VmScheduler { + intrinsicThen: Callable | undefined + readonly parked = new Map() + readonly queue: Array = [] + head = 0 + pending = 0 + nextID = 0 + readonly unhandled = new Map() + + constructor(readonly vm: Vm) {} + + get hasJobs(): boolean { + return this.head < this.queue.length + } + + newPromise(): PromiseObj { + if (this.pending >= MAX_PENDING_PROMISES) { + throw rangeError( + `Too many pending promises (limit ${MAX_PENDING_PROMISES}); await promises before creating more.`, + ) + } + this.pending++ + return new PromiseObj(this.vm.ctx.builtins.Promise, this.nextID++) + } + + resolving(promise: PromiseObj, site?: AstNode): Resolving { + return new Resolving(this, promise, site) + } + + then(promise: PromiseObj, reaction: Reaction): void { + if (promise.state === "pending") { + promise.reactions.push(reaction) + } else { + if (promise.state === "rejected") this.unhandled.delete(promise) + const settled = promise.state === "fulfilled" ? normal(promise.result) : abrupt(promise.result) + this.enqueue({ kind: "reaction", reaction, settled }) + } + promise.handled = true + } + + detach(promise: PromiseObj, reaction: Reaction): void { + const index = promise.reactions.indexOf(reaction) + if (index >= 0) promise.reactions.splice(index, 1) + } + + promiseResolve(value: Value): PromiseObj { + if (value instanceof PromiseObj) return value + const promise = this.newPromise() + this.resolveWith(promise, value) + return promise + } + + reject(promise: PromiseObj, reason: unknown): void { + this.settle(promise, abrupt(reason)) + } + + resolveWith(promise: PromiseObj, resolution: Value, site?: AstNode): void { + if (resolution === promise) { + const cycle = typeError("Chaining cycle detected: a promise cannot resolve with itself.", site) + this.settle(promise, abrupt(cycle)) + return + } + if (!(resolution instanceof Obj)) { + this.settle(promise, normal(resolution)) + return + } + const then = get(resolution, "then") + if (typeofValue(then) !== "function") { + this.settle(promise, normal(resolution)) + return + } + this.enqueue({ kind: "thenable", promise, thenable: resolution, then }) + } + + settle(promise: PromiseObj, completion: Completion): void { + if (promise.state !== "pending") return + this.pending-- + promise.state = completion.ok ? "fulfilled" : "rejected" + promise.result = completion.ok ? completion.value : completion.error + const reactions = promise.reactions + promise.reactions = [] + if (!completion.ok && !promise.handled && !(completion.error instanceof Interruption)) { + const failure = normalizeError(completion.error) + this.unhandled.set(promise, { + ...failure, + message: `Unhandled rejection from an un-awaited promise: ${failure.message}`, + }) + } + for (const reaction of reactions) { + this.enqueue({ kind: "reaction", reaction, settled: completion }) + } + } + + enqueue(job: Job): void { + this.queue.push(job) + } + + dequeue(): Job | undefined { + if (this.head === this.queue.length) { + if (this.head > 0) { + this.queue.length = 0 + this.head = 0 + } + return undefined + } + const job = this.queue[this.head++] + if (this.head > 4096 && this.head * 2 > this.queue.length) { + this.queue.splice(0, this.head) + this.head = 0 + } + return job + } + + compact(): void { + this.queue.splice(0, this.head) + this.head = 0 + } + + park(frames: Array, promise: PromiseObj, node: AstNode | undefined, result: Value): void { + this.parked.set(frames[0]!, { promise, node, result }) + this.then(promise, { kind: "await", frames }) + } + + stuckAwait(): AstNode | undefined { + const results = new Set() + for (const parked of this.parked.values()) { + if (parked.result instanceof PromiseObj) results.add(parked.result) + } + const located = [...this.parked.values()].filter((parked) => parked.node !== undefined) + return (located.find((parked) => !results.has(parked.promise)) ?? located[0])?.node + } + + diagnostics(): Array { + return [...this.unhandled].sort(([left], [right]) => left.id - right.id).map(([, diagnostic]) => diagnostic) + } + + beginJob(job: Job): void { + const vm = this.vm + if (job.kind === "thenable") { + const call = adoptThenable(vm.ctx, job.promise, job.thenable, job.then) + if (call === undefined || call.kind !== "thenable") return + this.enterSettlingCall( + undefined, + call.then, + call.thenable, + call.functions, + Mode.REJECT_ONLY, + call.resolving, + undefined, + undefined, + ) + return + } + + const reaction = job.reaction + if (reaction.kind === "await") { + const frames = reaction.frames + this.parked.delete(frames[0]!) + for (const f of frames) { + if (f.counted) vm.depth++ + vm.frames.push(f) + } + const top = frames[frames.length - 1]! + if (job.settled.ok) { + top.regs[top.sp++] = job.settled.value + return + } + top.pc-- + vm.unwind(job.settled.error) + return + } + + if (reaction.kind === "asyncFromSync") { + if (job.settled.ok) { + reaction.made.resolve(iteratorResult(vm.ctx.builtins.Object, job.settled.value as Value, reaction.done)) + return + } + if ( + reaction.done || + reaction.close === undefined || + job.settled.error instanceof Interruption || + reaction.close instanceof Position + ) { + reaction.made.reject(job.settled.error) + return + } + vm.pushInternal( + internalCode("$vmAsyncFromSyncClose"), + [reaction.close.iterator], + reaction.site, + Mode.SETTLE, + new AsyncFromSyncCloseTarget(reaction.made, job.settled.error), + ) + return + } + + const call = react(vm.ctx, reaction, job.settled) + if (call === undefined) return + if (call.kind === "handler") { + this.enterSettlingCall( + undefined, + call.handler, + undefined, + [call.argument], + Mode.SETTLE, + call.derived, + undefined, + undefined, + ) + return + } + if (call.kind === "finally") { + this.enterSettlingCall( + undefined, + call.cleanup, + undefined, + [], + Mode.SETTLE, + new FinallyTarget(vm, call.made, call.site, call.outcome), + undefined, + call.site, + ) + } + } + + enterSettlingCall( + callerFrame: Frame | undefined, + callable: Value, + thisVal: Value, + args: Array, + mode: FrameMode, + target: SettlingTarget, + answer: Value, + site: AstNode | undefined, + ): number { + const vm = this.vm + while (callable instanceof BoundFn) { + thisVal = callable.boundThis + args = [...callable.boundArgs, ...args] + callable = callable.target + } + + if (callable instanceof Resolver) { + try { + const res = callable.invoke(thisVal, args) as Value + if (mode === Mode.SETTLE) target.resolve(res) + } catch (err) { + target.reject(locate(err, site ?? vm.currentSite())) + } + if (answer !== undefined && callerFrame !== undefined) { + callerFrame.regs[callerFrame.sp++] = answer + } + return 0 + } + + if (callable instanceof Fn) { + const code = callable.code + if ((code.flags & (CodeFlags.ASYNC | CodeFlags.GENERATOR)) === 0) { + const counted = (code.flags & CodeFlags.INTERNAL) === 0 + if (counted && vm.depth > MAX_CALL_DEPTH) { + target.reject(rangeError("Maximum call stack size exceeded", site ?? vm.currentSite())) + if (answer !== undefined && callerFrame !== undefined) { + callerFrame.regs[callerFrame.sp++] = answer + } + return 0 + } + vm.pushFn(callable, thisVal, args, site ?? callerFrame?.origin, mode, target, answer) + return 1 + } + } + + vm.pushInternal( + internalCode("$vmSettleCall"), + [callable, thisVal, new Arr(vm.ctx.builtins.Array, args)], + site ?? callerFrame?.origin, + mode, + target, + answer, + ) + return 1 + } + + handlePromisePush( + callerFrame: Frame, + kindName: string, + slots: Array, + site: AstNode | undefined, + ): number | undefined { + // An executor's result is ignored and only its failure rejects; `Promise.try` settles with its callback's outcome. + if (kindName === "Promise executor" || kindName === "Promise.try") { + const made = slots[0] as Resolving + const mode = kindName === "Promise.try" ? Mode.SETTLE : Mode.REJECT_ONLY + return this.enterSettlingCall( + callerFrame, + slots[1] as Value, + undefined, + slots[2] as Array, + mode, + made, + made.promise, + site, + ) + } + if (kindName === "Promise combinator") { + const made = slots[0] as Resolving + this.vm.pushInternal( + internalCode("$vmCombine"), + [slots[1], slots[2]], + site ?? callerFrame.origin, + Mode.REJECT_ONLY, + made, + made.promise, + ) + return 1 + } + return undefined + } +} + +export const registerAsyncIntrinsics = (map: Map, vm: Vm): void => { + const ctx = vm.ctx + const builtins = ctx.builtins + const add = (name: string, fn: (args: Array) => Value | Invoke) => { + map.set(name, new VmIntrinsic(name, fn)) + } + + add("$Apply", ([callable, thisVal, args]) => { + const argItems = args instanceof Arr ? [...args.items] : [] + return new Invoke(callable, thisVal, argItems, vm.currentSite()) + }) + + add("$CombineStart", ([nameVal, items]) => { + const name = nameVal as Combination["name"] + const isIter = + builtinCursor(builtins, items) !== undefined || + (items instanceof Obj && get(items, IteratorSymbol) !== undefined && get(items, IteratorSymbol) !== null) + if (!isIter) { + throw typeError(`Promise.${name} expects a synchronous iterable, received ${describeValue(items)}.`) + } + const top = vm.frames[vm.frames.length - 1]! + const made = top.target as Resolving + const comb: Combination = { name, made, values: [], reasons: [], remaining: 1, attached: [] } + return comb as unknown as Value + }) + + add("$CombineAdd", ([combVal, item]) => { + const c = combVal as unknown as Combination + const p = vm.scheduler.promiseResolve(item) + const reaction: Reaction = { kind: "combinator", combination: c, index: c.values.length, called: false } + c.values.push(undefined) + c.reasons.push(undefined) + c.remaining++ + c.attached.push([p, reaction]) + vm.scheduler.then(p, reaction) + return undefined + }) + + add("$CombineEnd", ([combVal]) => { + const c = combVal as unknown as Combination + if (c.name === "race" && c.attached.length === 0) { + throw typeError("Promise.race([]) would never settle; provide at least one promise or value.") + } + finishCombination(ctx, c) + return undefined + }) + + add("$IsThenable", ([val]) => isThenable(val)) +} diff --git a/packages/codemode/src/vm/code.ts b/packages/codemode/src/vm/code.ts new file mode 100644 index 00000000000..8f406707be0 --- /dev/null +++ b/packages/codemode/src/vm/code.ts @@ -0,0 +1,435 @@ +import type { AstNode } from "./runtime/model.js" +import type { Source } from "./runtime/source.js" +import { type ConstValue, Op, type OpCode, packWord } from "./opcodes.js" + +type SourceRef = { + readonly id: string + readonly hash: string +} + +export type Label = { + pc: number + readonly patches: Array + depth: number | undefined +} + +/** + * Immutable compiled function or program body. Holds no per-run mutable state. + */ +export class Code { + readonly source: SourceRef + readonly index: number + readonly name: string + readonly flags: number + readonly length: number + readonly params: number + readonly locals: number + readonly maxStack: number + readonly code: Int32Array + readonly consts: ReadonlyArray + /** Per upvalue: `[from (0 = parent cell slot, 1 = parent upvalue), index]`. */ + readonly captures: Int32Array + /** Exception handlers `[start, end, target, depth]*`, innermost first (`end` exclusive). */ + readonly handlers: Int32Array + /** Per finally level: `[tagSlot, valSlot, entryDepth, n, target_0 … target_{n-1}]`. */ + readonly finallies: Int32Array + /** Offsets into `finallies` indexed by finally level. */ + readonly finallyOffsets: Int32Array + /** Run-length encoded `[pc, nodeIndex]*` sorted by `pc`. */ + readonly sites: Int32Array + readonly localNames: ReadonlyArray + readonly upvalNames: ReadonlyArray + readonly sourceObj: Source | undefined + + constructor(init: { + readonly source: SourceRef + readonly index: number + readonly name: string + readonly flags: number + readonly length: number + readonly params: number + readonly locals: number + readonly maxStack: number + readonly code: Int32Array + readonly consts: ReadonlyArray + readonly captures: Int32Array + readonly handlers: Int32Array + readonly finallies: Int32Array + readonly finallyOffsets: Int32Array + readonly sites: Int32Array + readonly localNames: ReadonlyArray + readonly upvalNames?: ReadonlyArray | undefined + readonly sourceObj?: Source | undefined + }) { + this.source = init.source + this.index = init.index + this.name = init.name + this.flags = init.flags + this.length = init.length + this.params = init.params + this.locals = init.locals + this.maxStack = init.maxStack + this.code = init.code + const consts: Array = [""] + consts.pop() + for (let i = 0; i < init.consts.length; i++) consts.push(init.consts[i]!) + this.consts = consts + this.captures = init.captures + this.handlers = init.handlers + this.finallies = init.finallies + this.finallyOffsets = init.finallyOffsets + this.sites = init.sites + this.localNames = init.localNames + this.upvalNames = init.upvalNames ?? [] + this.sourceObj = init.sourceObj + } + + /** Resolves the AST node index recorded for `pc`, or `-1` when none was recorded. */ + nodeIndexAt(pc: number): number { + const sites = this.sites + const count = sites.length >>> 1 + if (count === 0) return -1 + let low = 0 + let high = count - 1 + let best = -1 + while (low <= high) { + const mid = (low + high) >>> 1 + const entryPc = sites[mid << 1]! + if (entryPc <= pc) { + best = sites[(mid << 1) + 1]! + low = mid + 1 + } else { + high = mid - 1 + } + } + return best + } + + /** Resolves the AST node recorded for `pc`. */ + siteAt(pc: number): AstNode | undefined { + const index = this.nodeIndexAt(pc) + return index >= 0 ? this.sourceObj?.nodes[index] : undefined + } + + /** The same code over another parse of its source, so its sites resolve to that tree's nodes. */ + withSource(sourceObj: Source): Code { + return new Code({ ...this, sourceObj }) + } +} + +// The fields of a `Code` that are packed words. +const words = ["code", "captures", "handlers", "finallies", "finallyOffsets", "sites"] as const + +export type CodeJson = { readonly [field: string]: unknown } + +/** Code as JSON, for bytecode compiled ahead of time: every field but the source tree, which `codeFromJson` is given. */ +export const codeToJson = (code: Code): CodeJson => { + const { sourceObj: _, ...fields } = code + return { ...fields, ...Object.fromEntries(words.map((field) => [field, [...code[field]]])) } +} + +export const codeFromJson = (data: CodeJson, sourceObj: Source): Code => + new Code({ + ...(data as unknown as ConstructorParameters[0]), + ...Object.fromEntries(words.map((field) => [field, Int32Array.from(data[field] as Array)])), + sourceObj, + }) + +/** Raised when a narrow-jump function exceeds 16-bit jump range (`65,535` words) and must recompile in wide mode. */ +export class CodeSizeOverflow extends RangeError { + constructor(message = "Function bytecode exceeds 16-bit jump range") { + super(message) + this.name = "CodeSizeOverflow" + } +} + +const checkUint31 = (value: number, name: string): void => { + if (!Number.isInteger(value) || value < 0 || value > 0x7fffffff) { + throw new RangeError(`${name} out of range: ${value}`) + } +} + +export class CodeBuilder { + private readonly words: Array = [] + private readonly sitePairs: Array = [] + private lastNodeIndex = -2 + private readonly constList: Array = [] + // Strings and numbers (but -0) are pooled; other constants are each their own. + private readonly pooled = new Map() + private readonly handlerWords: Array = [] + private readonly finallyWords: Array = [] + private readonly finallyLevelOffsets: Array = [] + + depth = 0 + maxStack = 0 + wideJumps = false + forceExtra = false + + constructor( + readonly source: SourceRef, + readonly sourceObj: Source | undefined, + readonly index: number, + readonly name: string, + public flags: number, + readonly length: number, + readonly params: number, + public locals: number, + readonly localNames: Array, + readonly captures: Int32Array, + readonly upvalNames: ReadonlyArray = [], + ) {} + + get pc(): number { + return this.words.length + } + + adjustDepth(delta: number): void { + if (this.depth < 0) return + this.depth += delta + if (this.depth > this.maxStack) this.maxStack = this.depth + } + + setDepth(depth: number): void { + this.depth = depth + if (this.depth > this.maxStack) this.maxStack = this.depth + } + + unreachable(): void { + this.depth = -1 + } + + constant(value: ConstValue): number { + const poolable = typeof value === "string" || (typeof value === "number" && !Object.is(value, -0)) + const existing = poolable ? this.pooled.get(value) : undefined + if (existing !== undefined) return existing + const k = this.constList.length + checkUint31(k, "Constant index") + if (poolable) this.pooled.set(value, k) + this.constList.push(value) + return k + } + + private recordSite(pc: number, nodeIndex: number): void { + if (nodeIndex < 0 || nodeIndex === this.lastNodeIndex) return + this.sitePairs.push(pc, nodeIndex) + this.lastNodeIndex = nodeIndex + } + + private pushOp(op: OpCode, a: number, bx: number, nodeIndex: number, forceWide: boolean): number { + if ((op & 0xff) !== op || op === Op.EXTRA) { + throw new RangeError(`Invalid opcode: ${op}`) + } + checkUint31(a, "Operand a") + if (op === Op.NEW && a > 0xff) { + throw new RangeError(`Op.NEW operand a out of range: ${a}`) + } + if (op === Op.PUSH_INT) { + if (a !== 0 || (bx & 0xffff) !== bx) { + throw new RangeError(`Op.PUSH_INT operand out of range: a=${a} bx=${bx}`) + } + } else { + checkUint31(bx, "Operand bx") + } + if (a > 0xffff) { + this.recordSite(this.words.length, nodeIndex) + this.words.push(packWord(Op.EXTRA, 0, a >>> 16)) + this.words.push(packWord(Op.EXTRA, (a >>> 8) & 0xff, bx >>> 16)) + } else if (forceWide || a > 0xff || (op !== Op.PUSH_INT && bx > 0xffff)) { + this.recordSite(this.words.length, nodeIndex) + this.words.push(packWord(Op.EXTRA, a >>> 8, bx >>> 16)) + } + const pc = this.words.length + this.recordSite(pc, nodeIndex) + this.words.push(packWord(op, a & 0xff, bx & 0xffff)) + if (this.words.length > 0x7fffffff) { + throw new RangeError(`Bytecode length ${this.words.length} exceeds 31-bit limit`) + } + if (!this.wideJumps && !this.forceExtra && this.words.length > 0xffff) { + throw new CodeSizeOverflow() + } + return pc + } + + emit(op: OpCode, a = 0, bx = 0, nodeIndex = -1, stackDelta = 0): number { + if (this.depth < 0) return this.words.length + const pc = this.pushOp(op, a, bx, nodeIndex, this.forceExtra) + this.adjustDepth(stackDelta) + return pc + } + + emitNumber(value: number, nodeIndex = -1): void { + if (this.depth < 0) return + if (Number.isInteger(value) && !Object.is(value, -0) && value >= -32768 && value <= 32767 && !this.forceExtra) { + this.emit(Op.PUSH_INT, 0, value & 0xffff, nodeIndex, 1) + return + } + const k = this.constant(value) + this.emit(Op.PUSH_CONST, 0, k, nodeIndex, 1) + } + + emitString(value: string, nodeIndex = -1): void { + if (this.depth < 0) return + const k = this.constant(value) + this.emit(Op.PUSH_CONST, 0, k, nodeIndex, 1) + } + + newLabel(depth?: number): Label { + return { pc: -1, patches: [], depth } + } + + mark(label: Label): void { + const target = this.words.length + checkUint31(target, "Jump target") + label.pc = target + if (label.depth !== undefined) this.setDepth(label.depth) + else if (this.depth >= 0) label.depth = this.depth + for (const patchPc of label.patches) { + const hasExtra = patchPc > 0 && (this.words[patchPc - 1]! & 0xff) === Op.EXTRA + if (!hasExtra) { + if (target > 0xffff) throw new CodeSizeOverflow() + this.words[patchPc] = (this.words[patchPc]! & 0xffff) | (target << 16) + } else { + this.words[patchPc - 1] = (this.words[patchPc - 1]! & 0xffff) | ((target >>> 16) << 16) + this.words[patchPc] = (this.words[patchPc]! & 0xffff) | ((target & 0xffff) << 16) + } + } + label.patches.length = 0 + } + + emitJump(op: OpCode, a: number, label: Label, nodeIndex = -1, jumpDelta = 0, fallDelta: number | null = 0): void { + if (this.depth < 0) return + const targetDepth = this.depth + jumpDelta + if (label.depth === undefined) label.depth = targetDepth + const known = label.pc >= 0 + if (known && !this.wideJumps && !this.forceExtra && label.pc > 0xffff) { + throw new CodeSizeOverflow() + } + const pc = this.pushOp(op, a, known ? label.pc : 0, nodeIndex, this.wideJumps || this.forceExtra) + if (!known) label.patches.push(pc) + if (fallDelta === null) this.depth = -1 + else this.adjustDepth(fallDelta) + } + + addHandler(start: number, end: number, target: number, depth: number): void { + if (start >= end || target < 0) return + checkUint31(start, "Handler start") + checkUint31(end, "Handler end") + checkUint31(target, "Handler target") + checkUint31(depth, "Handler depth") + this.handlerWords.push(start, end, target, depth) + } + + reserveFinally(): number { + const level = this.finallyLevelOffsets.length + checkUint31(level, "Finally level") + this.finallyLevelOffsets.push(-1) + return level + } + + setFinally( + level: number, + tagSlot: number, + valSlot: number, + entryDepth: number, + targets: ReadonlyArray, + ): void { + checkUint31(level, "Finally level") + checkUint31(tagSlot, "Finally tagSlot") + checkUint31(valSlot, "Finally valSlot") + checkUint31(entryDepth, "Finally entryDepth") + checkUint31(targets.length, "Finally target count") + this.finallyLevelOffsets[level] = this.finallyWords.length + this.finallyWords.push(tagSlot, valSlot, entryDepth, targets.length) + for (const t of targets) { + if (!Number.isInteger(t) || t < -1 || t > 0x7fffffff) { + throw new RangeError(`Finally target out of range: ${t}`) + } + this.finallyWords.push(t) + } + } + + finish(): Code { + return new Code({ + source: this.source, + index: this.index, + name: this.name, + flags: this.flags, + length: this.length, + params: this.params, + locals: this.locals, + maxStack: this.maxStack, + code: new Int32Array(this.words), + consts: this.constList, + captures: this.captures, + handlers: new Int32Array(this.handlerWords), + finallies: new Int32Array(this.finallyWords), + finallyOffsets: new Int32Array(this.finallyLevelOffsets), + sites: new Int32Array(this.sitePairs), + localNames: this.localNames, + upvalNames: this.upvalNames, + sourceObj: this.sourceObj, + }) + } +} + +/** Deterministic FNV-1a 64-bit hash over a compiled `Code[]` array. */ +export const hashOf = (codes: ReadonlyArray): string => { + let h1 = 0x811c9dc5 + let h2 = 0xcbf29ce4 + const mix = (v: number) => { + h1 = Math.imul(h1 ^ (v & 0xff), 0x01000193) + h1 = Math.imul(h1 ^ ((v >>> 8) & 0xff), 0x01000193) + h2 = Math.imul(h2 ^ ((v >>> 16) & 0xff), 0x01000193) + h2 = Math.imul(h2 ^ ((v >>> 24) & 0xff), 0xff000193) + } + const mixStr = (s: string) => { + mix(s.length) + for (let i = 0; i < s.length; i++) mix(s.charCodeAt(i)) + } + const mixArr = (arr: Int32Array) => { + mix(arr.length) + for (let i = 0; i < arr.length; i++) mix(arr[i]!) + } + mix(codes.length) + for (const c of codes) { + mix(c.index) + mixStr(c.name) + mix(c.flags) + mix(c.length) + mix(c.params) + mix(c.locals) + mix(c.maxStack) + mixArr(c.code) + mixArr(c.captures) + mixArr(c.handlers) + mixArr(c.finallies) + mixArr(c.sites) + mix(c.consts.length) + for (const k of c.consts) { + if (typeof k === "string") { + mix(1) + mixStr(k) + } else if (typeof k === "number") { + mix(2) + mixStr(Object.is(k, -0) ? "-0" : String(k)) + } else if ("pattern" in k) { + mix(3) + mixStr(k.pattern) + mixStr(k.flags) + } else if ("cooked" in k) { + mix(4) + mix(k.nodeIndex) + for (const s of k.raw) mixStr(s) + } else if ("specifier" in k) { + mix(5) + mixStr(k.specifier) + if (k.imported !== undefined) mixStr(k.imported) + } else { + mix(6) + mixStr(k.type) + mixStr(k.message) + } + } + } + return (h1 >>> 0).toString(16).padStart(8, "0") + (h2 >>> 0).toString(16).padStart(8, "0") +} diff --git a/packages/codemode/src/vm/compiler/compile.ts b/packages/codemode/src/vm/compiler/compile.ts new file mode 100644 index 00000000000..f74cb3f4bda --- /dev/null +++ b/packages/codemode/src/vm/compiler/compile.ts @@ -0,0 +1,1603 @@ +import type { + ArrayExpression, + ArrayPattern, + ArrowFunctionExpression, + AssignmentExpression, + AwaitExpression, + BinaryExpression, + BlockStatement, + CallExpression, + CatchClause, + ConditionalExpression, + DoWhileStatement, + Expression, + ForInStatement, + ForOfStatement, + ForStatement, + FunctionExpression, + IfStatement, + ImportExpression, + LabeledStatement, + Literal, + LogicalExpression, + MemberExpression, + ModuleDeclaration, + NewExpression, + ObjectExpression, + ObjectPattern, + Pattern, + Program, + SpreadElement, + Statement, + SwitchStatement, + TaggedTemplateExpression, + TemplateLiteral, + TryStatement, + UnaryExpression, + UpdateExpression, + VariableDeclaration, + WhileStatement, + YieldExpression, +} from "acorn" +import { type AstNode, supportedSyntaxMessage } from "../runtime/model.js" +import { nodeId, type Source } from "../runtime/source.js" +import { Code, CodeBuilder, CodeSizeOverflow, hashOf, type Label } from "../code.js" +import { + BinOp, + CheckBaseKind, + CodeFlags, + CondKind, + type ErrorSite, + ImportKind, + IterContext, + Op, + type OpCode, + PutKind, + UnaryOp, +} from "../opcodes.js" +import { + analyzeScopes, + type Decl, + type FunctionInfo, + type ResolvedRef, + type Scope, + type ScopeAnalysis, +} from "./scope.js" + +/** Each binary operator as the op and `a` operand that apply it. */ +const BinaryOps: Readonly> = { + "+": [Op.ADD, 0], + "-": [Op.SUB, 0], + "*": [Op.MUL, 0], + "<": [Op.LT, 0], + "<=": [Op.LE, 0], + ">": [Op.GT, 0], + ">=": [Op.GE, 0], + "===": [Op.SEQ, 0], + "!==": [Op.SNE, 0], + in: [Op.IN, 0], + instanceof: [Op.INSTANCEOF, 0], + "/": [Op.BINOP, BinOp.DIV], + "%": [Op.BINOP, BinOp.MOD], + "**": [Op.BINOP, BinOp.EXP], + "&": [Op.BINOP, BinOp.BAND], + "|": [Op.BINOP, BinOp.BOR], + "^": [Op.BINOP, BinOp.BXOR], + "<<": [Op.BINOP, BinOp.SHL], + ">>": [Op.BINOP, BinOp.SAR], + ">>>": [Op.BINOP, BinOp.SHR], + "==": [Op.BINOP, BinOp.EQ], + "!=": [Op.BINOP, BinOp.NE], +} + +const UnaryOps: Readonly> = { + "-": UnaryOp.NEG, + "+": UnaryOp.PLUS, + "~": UnaryOp.BNOT, + typeof: UnaryOp.TYPEOF, + void: UnaryOp.VOID, +} + +/** The jump that skips the right operand of `||`, `&&`, `??` and their assignments. */ +const logicalCond = (operator: string) => + operator.startsWith("||") + ? CondKind.TRUTHY_KEEP + : operator.startsWith("&&") + ? CondKind.FALSY_KEEP + : CondKind.NOT_NULLISH_KEEP + +/** NamedEvaluation: whether `node` is an anonymous function definition, which takes the name it is assigned to. */ +const isAnonymousFunction = (node: Expression): node is FunctionExpression | ArrowFunctionExpression => + node.type === "ArrowFunctionExpression" || (node.type === "FunctionExpression" && !node.id) + +type LoopEntry = { + readonly kind: "loop" + readonly labels: ReadonlySet + readonly breakLabel: Label + readonly continueLabel: Label + readonly entryDepth: number + readonly iterSlot: number + readonly iterAsync: boolean +} + +type ControlEntry = + | LoopEntry + | { + readonly kind: "switch" | "label" + readonly labels: ReadonlySet + readonly breakLabel: Label + readonly entryDepth: number + } + | { readonly kind: "iterator"; readonly itSlot: number; readonly entryDepth: number } + | { + readonly kind: "finally" + readonly tagSlot: number + readonly valSlot: number + readonly finallyLabel: Label + readonly entryDepth: number + readonly exits: Map< + string, + { readonly tag: number; readonly kind: "return" | "break" | "continue"; readonly target: ControlEntry | null } + > + } + +const idx = (node: AstNode | undefined | null): number => (node ? (nodeId(node)?.index ?? -1) : -1) + +/** Bump when the compiler emits different bytecode for the same source; it is part of the snapshot build id. */ +export const COMPILER_VERSION = 1 + +const compileCache = new Map>() +const MAX_CACHE_SIZE = 64 + +export type CompileOptions = { + readonly internal?: boolean + readonly forceExtra?: boolean + readonly noCache?: boolean +} + +/** + * Compiles a `Source` into an immutable `Code[]` array indexed by pre-order function index (`0` = program). + */ +export const compileSource = (source: Source, options?: CompileOptions): ReadonlyArray => { + const internal = options?.internal ?? false + const forceExtra = options?.forceExtra ?? false + const cacheable = !internal && !forceExtra && !options?.noCache && source.id === "main" + const cached = cacheable ? compileCache.get(source.code) : undefined + // Sites resolve against this Source's nodes. + if (cached !== undefined) return cached.map((code) => code.withSource(source)) + + const analysis = analyzeScopes(source, internal) + const preliminary = analysis.functions.map((fnInfo) => + compileFunction(source, { id: source.id, hash: "" }, analysis, fnInfo, forceExtra), + ) + const finalRef = { id: source.id, hash: hashOf(preliminary) } + const codes = preliminary.map((code) => new Code({ ...code, source: finalRef })) + + if (cacheable) { + if (compileCache.size >= MAX_CACHE_SIZE) compileCache.delete(compileCache.keys().next().value!) + compileCache.set(source.code, codes) + } + return codes +} + +const compileFunction = ( + source: Source, + sourceRef: Code["source"], + analysis: ScopeAnalysis, + fnInfo: FunctionInfo, + forceExtra: boolean, +): Code => { + try { + return compileFunctionBody(source, sourceRef, analysis, fnInfo, forceExtra, false) + } catch (err) { + if (err instanceof CodeSizeOverflow) { + return compileFunctionBody(source, sourceRef, analysis, fnInfo, forceExtra, true) + } + throw err + } +} + +const compileFunctionBody = ( + source: Source, + sourceRef: Code["source"], + analysis: ScopeAnalysis, + fnInfo: FunctionInfo, + forceExtra: boolean, + wideJumps: boolean, +): Code => { + const isProgram = fnInfo.index === 0 + const internal = (fnInfo.flags & CodeFlags.INTERNAL) !== 0 + const builder = new CodeBuilder( + sourceRef, + source, + fnInfo.index, + fnInfo.name, + fnInfo.flags, + fnInfo.length, + fnInfo.params.length, + fnInfo.locals, + [...fnInfo.localNames], + Int32Array.from(fnInfo.upvalues.flatMap((u) => [u.from, u.index])), + fnInfo.upvalues.map((u) => u.decl.name), + ) + builder.forceExtra = forceExtra + builder.wideJumps = wideJumps + + const controlStack: Array = [] + let pendingLabels: Set | undefined + let retSlot = -1 + + const allocSlot = (name: string): number => { + builder.localNames.push(name) + return builder.locals++ + } + + const emitThrowError = (site: ErrorSite, node: AstNode): void => { + builder.emit(Op.THROW_ERROR, 0, builder.constant(site), idx(node), 0) + builder.unreachable() + } + + const emitUnsupported = (kindName: string, node: AstNode): void => + emitThrowError( + { + type: "SyntaxError", + message: `Syntax '${kindName}' is not supported. ${supportedSyntaxMessage}`, + kind: "UnsupportedSyntax", + suggestions: [supportedSyntaxMessage], + }, + node, + ) + + // `super.x` parses inside object methods but is not supported. Code after the error is unreachable, so it emits nothing. + const unsupportedSuper = (node: MemberExpression): boolean => { + if (node.object.type !== "Super") return false + emitUnsupported("Super", node.object) + return true + } + + const putKind = (decl: Decl, isInit: boolean) => + isInit ? PutKind.INIT : decl.mutable ? PutKind.ASSIGN : PutKind.CONST + + const emitLoadDecl = (decl: Decl, node?: AstNode) => + builder.emit(decl.captured ? Op.GET_CELL : Op.GET_LOCAL, 0, decl.slot, idx(node), 1) + + const emitStoreDecl = (decl: Decl, isInit: boolean, node?: AstNode) => + builder.emit(decl.captured ? Op.PUT_CELL : Op.PUT_LOCAL, putKind(decl, isInit), decl.slot, idx(node), -1) + + const emitLoadRef = (ref: ResolvedRef, node: AstNode, isTypeof = false): void => { + if (ref.kind === "local") emitLoadDecl(ref.decl, node) + else if (ref.kind === "upval") builder.emit(Op.GET_UPVAL, 0, ref.upvalIndex, idx(node), 1) + else if (internal && !isTypeof) { + // Internal code can't shadow `undefined`. + if (ref.name === "undefined") builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + else builder.emit(Op.GET_INTRINSIC, 0, builder.constant(ref.name), idx(node), 1) + } else builder.emit(Op.GET_GLOBAL, isTypeof ? 1 : 0, builder.constant(ref.name), idx(node), 1) + } + + const emitStoreRef = (ref: ResolvedRef, isInit: boolean, node: AstNode): void => { + if (ref.kind === "local") emitStoreDecl(ref.decl, isInit, node) + else if (ref.kind === "upval") builder.emit(Op.PUT_UPVAL, putKind(ref.decl, isInit), ref.upvalIndex, idx(node), -1) + // Global assignment: checks if global exists (`Cannot assign to constant`) or unknown (`Unknown identifier`) + else builder.emit(Op.GET_GLOBAL, 2, builder.constant(ref.name), idx(node), -1) + } + + const emitScopePrologue = (scope: Scope, resetUncapturedLexicals: boolean): void => { + for (const decl of scope.decls.values()) { + if (decl.kind === "this" || decl.kind === "arguments" || decl.kind === "param" || decl.kind === "self") continue + if (decl.captured) builder.emit(Op.NEW_CELL, 0, decl.slot) + else if (resetUncapturedLexicals && (decl.kind === "let" || decl.kind === "const")) { + builder.emit(Op.HOLE_LOCAL, 0, decl.slot) + } + } + for (const decl of scope.decls.values()) { + if (decl.kind !== "var") continue + if (decl.copyFromParam !== undefined) emitLoadDecl(decl.copyFromParam) + else builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1) + emitStoreDecl(decl, true) + } + emitHoistedFunctions(scope) + } + + const emitHoistedFunctions = (scope: Scope): void => { + for (const hoisted of scope.hoistedFunctions) { + if (hoisted.fnIndex < 0) continue + builder.emit(Op.CLOSURE, 0, hoisted.fnIndex, idx(hoisted.node), 1) + emitStoreDecl(hoisted.decl, true, hoisted.node) + } + } + + const emitImports = (stmts: ReadonlyArray): void => { + for (const stmt of stmts) { + if (stmt.type !== "ImportDeclaration") continue + const specifier = String(stmt.source.value) + if (stmt.specifiers.length === 0) { + builder.emit(Op.IMPORT, ImportKind.SIDE_EFFECT, builder.constant({ specifier }), idx(stmt), 0) + } + for (const spec of stmt.specifiers) { + if (spec.type === "ImportSpecifier") { + const imported = spec.imported.type === "Identifier" ? spec.imported.name : String(spec.imported.value) + builder.emit(Op.IMPORT, ImportKind.NAMED, builder.constant({ specifier, imported }), idx(spec), 1) + } else { + const kind = spec.type === "ImportDefaultSpecifier" ? ImportKind.DEFAULT : ImportKind.NAMESPACE + builder.emit(Op.IMPORT, kind, builder.constant({ specifier }), idx(spec), 1) + } + emitStoreDecl(fnInfo.bodyScope.decls.get(spec.local.name)!, true, spec) + } + } + } + + // --- Iteration (fast paths inline, a custom iterator's calls out of line) --- + + // Opens an iterator over the value on top into `itSlot`. The returned function emits the out-of-line path that + // calls a custom `[Symbol.iterator]` method. + const emitIterOpen = (context: number, itSlot: number, base: number, node: AstNode) => { + const slow = builder.newLabel(base + 2) + const join = builder.newLabel(base + 1) + builder.emitJump(Op.ITER_OPEN, context, slow, idx(node), 0, 1) + builder.emitJump(Op.JUMP, 0, slow, idx(node), 0, null) + builder.mark(join) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, itSlot, idx(node), -1) + return () => { + builder.mark(slow) + builder.emit(Op.CALL, 0, 0, idx(node), -1) + builder.emit(Op.ITER_INIT, context, 0, idx(node), 0) + builder.emitJump(Op.JUMP, 0, join, idx(node), 0, null) + } + } + + // Pushes the next value, or jumps to `done`. The returned function emits the out-of-line path that calls a custom + // `next` method. + const emitIterStep = (itSlot: number, base: number, done: Label, node: AstNode, isAwait = false) => { + const got = builder.newLabel(base + 1) + const slow = builder.newLabel(base + 2) + builder.emitJump(Op.ITER_STEP, itSlot, done, idx(node), 0, 2) + builder.emitJump(Op.JUMP, 0, slow, idx(node), 0, null) + builder.mark(got) + return () => { + builder.mark(slow) + builder.emit(Op.CALL, 0, 0, idx(node), -1) + if (isAwait) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + builder.emitJump(Op.ITER_RESULT, itSlot, done, idx(node), -1, 0) + builder.emitJump(Op.JUMP, 0, got, idx(node), 0, null) + } + } + + // Appends what is left of the iterator to the array on top (at `base`). `beforeSlow` emits other out-of-line code. + const emitAppendRest = (itSlot: number, base: number, node: AstNode, beforeSlow?: () => void): void => { + const loop = builder.newLabel(base) + const done = builder.newLabel(base) + builder.emitJump(Op.ITER_APPEND_ALL, itSlot, loop, idx(node), 0, 0) + builder.emitJump(Op.JUMP, 0, done, idx(node), 0, 0) + builder.mark(loop) + const slow = emitIterStep(itSlot, base, done, node) + builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1) + builder.emitJump(Op.JUMP, 0, loop, idx(node), 0, 0) + beforeSlow?.() + slow() + builder.mark(done) + } + + const emitIteratorCloseNormal = (itSlot: number, iterAsync: boolean, node: AstNode): void => { + const skipLabel = builder.newLabel(builder.depth) + builder.emitJump(Op.ITER_CLOSE, itSlot, skipLabel, idx(node), 0, 2) + builder.emit(Op.CALL, 0, 0, idx(node), -1) + if (iterAsync) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + builder.emit(Op.ITER_INIT, IterContext.CHECK_OBJECT, 0, idx(node), 0) + builder.emit(Op.POP, 0, 0, idx(node), -1) + builder.mark(skipLabel) + } + + // The handler for a throw out of `ranges`: closes the iterator, ignoring an error from closing it, and rethrows. + const emitCloseOnThrow = ( + ranges: ReadonlyArray, + itSlot: number, + errSlot: number, + entryDepth: number, + node: AstNode, + isAwait = false, + ): void => { + const catchLabel = builder.newLabel(entryDepth + 1) + builder.mark(catchLabel) + for (const [start, end] of ranges) builder.addHandler(start, end, catchLabel.pc, entryDepth) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, errSlot, idx(node), -1) + const rethrowLabel = builder.newLabel(entryDepth) + const swallowLabel = builder.newLabel(entryDepth + 1) + const closeStart = builder.pc + builder.emitJump(Op.ITER_CLOSE, itSlot, rethrowLabel, idx(node), 0, 2) + builder.emit(Op.CALL, 0, 0, idx(node), -1) + if (isAwait) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + builder.emit(Op.POP, 0, 0, idx(node), -1) + const closeEnd = builder.pc + builder.mark(rethrowLabel) + builder.emit(Op.GET_LOCAL, 0, errSlot, idx(node), 1) + builder.emit(Op.THROW, 0, 0, idx(node), -1) + builder.mark(swallowLabel) + builder.emit(Op.POP, 0, 0, idx(node), -1) + builder.emitJump(Op.JUMP, 0, rethrowLabel, idx(node), 0, null) + builder.addHandler(closeStart, closeEnd, swallowLabel.pc, entryDepth) + } + + // --- Exit Plan (return, break, continue across iterators and finally blocks; C1, C3, C4) --- + const findExitTarget = (kind: "break" | "continue", label: string | undefined): ControlEntry | null => { + for (let i = controlStack.length - 1; i >= 0; i--) { + const e = controlStack[i]! + if (e.kind === "finally" || e.kind === "iterator") continue + if (kind === "break" && (label !== undefined ? e.labels.has(label) : e.kind === "loop" || e.kind === "switch")) + return e + if (kind === "continue" && e.kind === "loop" && (label === undefined || e.labels.has(label))) return e + } + return null + } + + const exitPlan = (kind: "return" | "break" | "continue", target: ControlEntry | null, node: AstNode): void => { + if (builder.depth < 0) return + const stop = target ? controlStack.indexOf(target) : -1 + const crossing = controlStack.slice(stop + 1).reverse() + const needsRet = + kind === "return" && + crossing.some((e) => e.kind === "finally" || e.kind === "iterator" || (e.kind === "loop" && e.iterSlot >= 0)) + + if (kind === "return" && !needsRet) { + // C1: RETURN discards the frame directly when nothing needs closing or routing + builder.emit(Op.RETURN, 0, 0, idx(node), -1) + builder.unreachable() + return + } + + if (needsRet) { + if (retSlot === -1) retSlot = allocSlot("%ret") + builder.emit(Op.PUT_LOCAL, PutKind.INIT, retSlot, idx(node), -1) + } + + const popTo = (d: number) => { + const drop = builder.depth - d + if (drop > 0) builder.emit(Op.POPN, 0, drop, idx(node), -drop) + } + + for (const e of crossing) { + popTo(e.entryDepth) + if (e.kind === "iterator") emitIteratorCloseNormal(e.itSlot, false, node) + else if (e.kind === "loop" && e.iterSlot >= 0) emitIteratorCloseNormal(e.iterSlot, e.iterAsync, node) + else if (e.kind === "finally") { + const key = `${kind}:${target ? stop : ""}` + let exit = e.exits.get(key) + if (!exit) { + exit = { tag: e.exits.size + 2, kind, target } + e.exits.set(key, exit) + } + if (kind === "return") { + builder.emit(Op.GET_LOCAL, 0, retSlot, idx(node), 1) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, e.valSlot, idx(node), -1) + } + builder.emitNumber(exit.tag, idx(node)) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, e.tagSlot, idx(node), -1) + builder.emitJump(Op.JUMP, 0, e.finallyLabel, idx(node), 0, null) + return + } + } + + if (kind === "return") { + popTo(0) + builder.emit(Op.GET_LOCAL, 0, retSlot, idx(node), 1) + builder.emit(Op.RETURN, 0, 0, idx(node), -1) + builder.unreachable() + return + } + + if (target && target.kind !== "iterator" && target.kind !== "finally") { + popTo(target.entryDepth) + if (kind === "break" && target.kind === "loop" && target.iterSlot >= 0) { + emitIteratorCloseNormal(target.iterSlot, target.iterAsync, node) + } + const dst = kind === "break" ? target.breakLabel : (target as LoopEntry).continueLabel + builder.emitJump(Op.JUMP, 0, dst, idx(node), 0, null) + return + } + + emitThrowError({ type: "TypeError", message: `Unexpected '${kind}' outside of a loop.` }, node) + } + + // --- Pattern Compilation (value to bind is on top of stack: depth -> depth - 1) --- + const compilePattern = (pattern: Pattern, isDecl: boolean, isInit: boolean, contextNode: AstNode): void => { + switch (pattern.type) { + case "Identifier": + emitStoreRef(analysis.refs.get(pattern)!, isInit, isDecl ? contextNode : pattern) + return + case "MemberExpression": { + // Stack has [value]. Evaluate target object and key, then write [value]. + const valSlot = allocSlot("%mval") + builder.emit(Op.PUT_LOCAL, PutKind.INIT, valSlot, idx(pattern), -1) + if (unsupportedSuper(pattern)) return + compileExpression(pattern.object as Expression) + if (pattern.computed) { + compileExpression(pattern.property as Expression) + emitWritableKey(pattern) + builder.emit(Op.GET_LOCAL, 0, valSlot, idx(pattern), 1) + builder.emit(Op.PUT_ELEM, 0, 0, idx(pattern), -2) + } else { + const k = builder.constant(propertyName(pattern)) + builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(pattern), 0) + builder.emit(Op.GET_LOCAL, 0, valSlot, idx(pattern), 1) + builder.emit(Op.PUT_PROP, 0, k, idx(pattern), -1) + } + builder.emit(Op.POP, 0, 0, idx(pattern), -1) + return + } + case "AssignmentPattern": { + const definedLabel = builder.newLabel(builder.depth) + builder.emitJump(Op.JUMP_COND, CondKind.DEFINED, definedLabel, idx(pattern), 0, -1) + if (pattern.left.type === "Identifier") compileNamedExpression(pattern.right, pattern.left.name) + else compileExpression(pattern.right) + builder.mark(definedLabel) + compilePattern(pattern.left, isDecl, isInit, contextNode) + return + } + case "ObjectPattern": + return compileObjectPattern(pattern, isDecl, isInit) + case "ArrayPattern": + return compileArrayPattern(pattern, isDecl, isInit) + default: + emitThrowError({ type: "TypeError", message: `Unsupported binding pattern '${pattern.type}'.` }, pattern) + } + } + + // A computed member's key as a write target: `[o, k] -> [o, key]`. + const emitWritableKey = (node: MemberExpression): void => { + builder.emit(Op.CHECK_BASE, CheckBaseKind.READ, 0, idx(node), 0) + builder.emit(Op.TO_KEY, 0, 0, idx(node.property), 0) + builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_ELEM, 0, idx(node), 0) + } + + const compileObjectPattern = (pattern: ObjectPattern, isDecl: boolean, isInit: boolean): void => { + builder.emit(Op.CHECK_BASE, CheckBaseKind.DESTRUCTURE, 0, idx(pattern), 0) + const props = pattern.properties + const lastProp = props.at(-1) + if (lastProp === undefined) { + builder.emit(Op.POP, 0, 0, idx(pattern), -1) + return + } + if (lastProp.type !== "RestElement") { + props.forEach((prop, i) => { + if (prop.type === "RestElement") return + if (i < props.length - 1) builder.emit(Op.DUP, 0, 0, idx(prop), 1) + if (prop.computed) { + compileExpression(prop.key as Expression) + builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0) + builder.emit(Op.GET_ELEM, 2, 0, idx(prop), -1) + } else builder.emit(Op.GET_PROP, 2, builder.constant(keyName(prop.key)), idx(prop), 0) + compilePattern(prop.value, isDecl, isInit, prop) + }) + return + } + + // ObjectPattern with RestElement at the end: keep `[src, k1, ..., kn]` on the stack for `COPY_REST n` + const count = props.length - 1 + props.slice(0, count).forEach((prop, i) => { + if (prop.type === "RestElement") return + if (prop.computed) { + compileExpression(prop.key as Expression) + builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0) + } else builder.emitString(keyName(prop.key), idx(prop.key)) + // Stack is `[src, k1, ..., k_{i+1}]`. Pick `src` (at offset i + 1) and `k_{i+1}` (at offset 1 after picking src). + builder.emit(Op.PICK, i + 1, 0, idx(prop), 1) + builder.emit(Op.PICK, 1, 0, idx(prop), 1) + builder.emit(Op.GET_ELEM, 2, 0, idx(prop), -1) + compilePattern(prop.value, isDecl, isInit, prop) + }) + builder.emit(Op.COPY_REST, 0, count, idx(lastProp), -count) + compilePattern(lastProp.argument, isDecl, isInit, lastProp) + } + + const compileArrayPattern = (pattern: ArrayPattern, isDecl: boolean, isInit: boolean): void => { + const itSlot = allocSlot("%it") + const errSlot = allocSlot("%e") + const entryDepth = builder.depth - 1 + const openSlow = emitIterOpen(IterContext.DESTRUCTURE, itSlot, entryDepth, pattern) + controlStack.push({ kind: "iterator", itSlot, entryDepth }) + const protectedRanges: Array = [] + const bind = (target: Pattern, node: AstNode) => { + const start = builder.pc + compilePattern(target, isDecl, isInit, node) + protectedRanges.push([start, builder.pc]) + } + + for (const el of pattern.elements) { + if (el?.type === "RestElement") { + // Drain remaining elements into a new array, then bind + builder.emit(Op.NEW_ARRAY, 0, 0, idx(el), 1) + emitAppendRest(itSlot, entryDepth + 1, el) + bind(el.argument, el) + break + } + const node = el ?? pattern + const doneLabel = builder.newLabel(entryDepth) + const afterLabel = builder.newLabel(entryDepth) + const slow = emitIterStep(itSlot, entryDepth, doneLabel, node) + // Elision: step iterator and discard value + if (el === null) builder.emit(Op.POP, 0, 0, idx(pattern), -1) + else bind(el, el) + builder.emitJump(Op.JUMP, 0, afterLabel, idx(node), 0, 0) + slow() + builder.mark(doneLabel) + if (el !== null) { + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(el), 1) + bind(el, el) + } + builder.mark(afterLabel) + } + + controlStack.pop() + emitIteratorCloseNormal(itSlot, false, pattern) + const endLabel = builder.newLabel(entryDepth) + builder.emitJump(Op.JUMP, 0, endLabel, idx(pattern), 0, 0) + openSlow() + emitCloseOnThrow(protectedRanges, itSlot, errSlot, entryDepth, pattern) + builder.mark(endLabel) + } + + // --- Statements --- + const compileStatement = (stmt: Statement | ModuleDeclaration): void => { + const labels = pendingLabels + pendingLabels = undefined + if (builder.depth < 0) return + const compile = statements[stmt.type] as ((node: typeof stmt, labels?: Set) => void) | undefined + if (compile) compile(stmt, labels) + else emitUnsupported(stmt.type, stmt) + } + + const withStatementLabels = (labels: Set | undefined, body: () => void): void => { + if (!labels || labels.size === 0) return body() + const breakLabel = builder.newLabel(builder.depth) + controlStack.push({ kind: "label", labels, breakLabel, entryDepth: builder.depth }) + body() + controlStack.pop() + builder.mark(breakLabel) + } + + const pushLoop = ( + labels: Set | undefined, + breakLabel: Label, + continueLabel: Label, + entryDepth: number, + iterSlot = -1, + iterAsync = false, + ) => + controlStack.push({ + kind: "loop", + labels: labels ?? new Set(), + breakLabel, + continueLabel, + entryDepth, + iterSlot, + iterAsync, + }) + + const compileVariableDeclaration = (node: VariableDeclaration): void => { + const isVar = node.kind === "var" + for (const decl of node.declarations) { + if (isVar && !decl.init && decl.id.type === "Identifier") continue + if (!decl.init) builder.emit(Op.PUSH_UNDEF, 0, 0, idx(decl), 1) + else if (decl.id.type === "Identifier") compileNamedExpression(decl.init, decl.id.name) + else compileExpression(decl.init) + compilePattern(decl.id, !isVar, !isVar, decl) + } + } + + const compileBlockStatement = (node: BlockStatement, labels?: Set): void => + withStatementLabels(labels, () => { + const scope = analysis.scopeOfNode.get(node) + if (scope && (scope.decls.size > 0 || scope.hoistedFunctions.length > 0)) emitScopePrologue(scope, true) + for (const s of node.body) compileStatement(s) + }) + + const compileIfStatement = (node: IfStatement, labels?: Set): void => + withStatementLabels(labels, () => { + const elseLabel = builder.newLabel(builder.depth) + const endLabel = builder.newLabel(builder.depth) + compileExpression(node.test) + builder.emitJump(Op.JUMP_IF_FALSE, 0, node.alternate ? elseLabel : endLabel, idx(node), -1, -1) + compileStatement(node.consequent) + if (node.alternate) { + builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, null) + builder.mark(elseLabel) + compileStatement(node.alternate) + } + builder.mark(endLabel) + }) + + const compileWhileStatement = (node: WhileStatement, labels?: Set): void => { + const entryDepth = builder.depth + const testLabel = builder.newLabel(entryDepth) + const continueLabel = builder.newLabel(entryDepth) + const breakLabel = builder.newLabel(entryDepth) + builder.mark(testLabel) + compileExpression(node.test) + builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1) + pushLoop(labels, breakLabel, continueLabel, entryDepth) + compileStatement(node.body) + controlStack.pop() + builder.mark(continueLabel) + builder.emitJump(Op.LOOP, 0, testLabel, idx(node), 0, null) + builder.mark(breakLabel) + } + + const compileDoWhileStatement = (node: DoWhileStatement, labels?: Set): void => { + const entryDepth = builder.depth + const bodyLabel = builder.newLabel(entryDepth) + const continueLabel = builder.newLabel(entryDepth) + const breakLabel = builder.newLabel(entryDepth) + builder.mark(bodyLabel) + pushLoop(labels, breakLabel, continueLabel, entryDepth) + compileStatement(node.body) + controlStack.pop() + builder.mark(continueLabel) + compileExpression(node.test) + builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1) + builder.emitJump(Op.LOOP, 0, bodyLabel, idx(node), 0, null) + builder.mark(breakLabel) + } + + const compileForStatement = (node: ForStatement, labels?: Set): void => { + const entryDepth = builder.depth + const forScope = analysis.scopeOfNode.get(node)! + emitScopePrologue(forScope, true) + if (node.init?.type === "VariableDeclaration") compileVariableDeclaration(node.init) + else if (node.init) compileEffect(node.init) + // Each iteration gets fresh cells for the bindings its closures capture. + const copyCells = () => { + for (const d of forScope.decls.values()) if (d.captured) builder.emit(Op.COPY_CELL, 0, d.slot, idx(node), 0) + } + copyCells() + const testLabel = builder.newLabel(entryDepth) + const continueLabel = builder.newLabel(entryDepth) + const breakLabel = builder.newLabel(entryDepth) + builder.mark(testLabel) + if (node.test) { + compileExpression(node.test) + builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1) + } + pushLoop(labels, breakLabel, continueLabel, entryDepth) + compileStatement(node.body) + controlStack.pop() + builder.mark(continueLabel) + copyCells() + if (node.update) compileEffect(node.update) + builder.emitJump(Op.LOOP, 0, testLabel, idx(node), 0, null) + builder.mark(breakLabel) + } + + // The binding a for...in or for...of assigns each value to, inside the loop's own scope when it is lexical. + const compileLoopBinding = (node: ForInStatement | ForOfStatement): void => { + if (node.left.type !== "VariableDeclaration") return compilePattern(node.left, false, false, node.left) + const lexical = node.left.kind !== "var" + if (lexical) emitScopePrologue(analysis.scopeOfNode.get(node)!, true) + compilePattern(node.left.declarations[0]!.id, lexical, lexical, node.left) + } + + const loopScopePrologue = (node: ForInStatement | ForOfStatement): void => { + if (node.left.type === "VariableDeclaration" && node.left.kind !== "var") { + emitScopePrologue(analysis.scopeOfNode.get(node)!, true) + } + } + + const compileForInStatement = (node: ForInStatement, labels?: Set): void => { + const entryDepth = builder.depth + loopScopePrologue(node) + compileExpression(node.right) + const itSlot = allocSlot("%it") + builder.emit(Op.FOR_IN, 0, 0, idx(node.right), 0) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, itSlot, idx(node), -1) + const stepLabel = builder.newLabel(entryDepth) + const continueLabel = builder.newLabel(entryDepth) + const breakLabel = builder.newLabel(entryDepth) + builder.mark(stepLabel) + const stepSlow = emitIterStep(itSlot, entryDepth, breakLabel, node) + compileLoopBinding(node) + pushLoop(labels, breakLabel, continueLabel, entryDepth) + compileStatement(node.body) + controlStack.pop() + builder.mark(continueLabel) + builder.emitJump(Op.LOOP, 0, stepLabel, idx(node), 0, null) + stepSlow() + builder.mark(breakLabel) + } + + const compileForOfStatement = (node: ForOfStatement, labels?: Set): void => { + const isAwait = node.await + const entryDepth = builder.depth + loopScopePrologue(node) + compileExpression(node.right) + const itSlot = allocSlot("%it") + const errSlot = allocSlot("%e") + const openSlow = emitIterOpen(isAwait ? IterContext.FOR_AWAIT : IterContext.FOR_OF, itSlot, entryDepth, node) + const stepLabel = builder.newLabel(entryDepth) + const continueLabel = builder.newLabel(entryDepth) + const breakLabel = builder.newLabel(entryDepth) + builder.mark(stepLabel) + const stepSlow = emitIterStep(itSlot, entryDepth, breakLabel, node, isAwait) + // C4: iterSlot belongs to the loop entry itself (`break` and `return` close it, `continue` does not) + pushLoop(labels, breakLabel, continueLabel, entryDepth, itSlot, isAwait) + const bodyStart = builder.pc + compileLoopBinding(node) + compileStatement(node.body) + controlStack.pop() + builder.mark(continueLabel) + builder.emitJump(Op.LOOP, 0, stepLabel, idx(node), 0, null) + const bodyEnd = builder.pc + openSlow() + stepSlow() + emitCloseOnThrow([[bodyStart, bodyEnd]], itSlot, errSlot, entryDepth, node, isAwait) + builder.mark(breakLabel) + } + + const compileSwitchStatement = (node: SwitchStatement, labels?: Set): void => { + const entryDepth = builder.depth + compileExpression(node.discriminant) + const discSlot = allocSlot("%disc") + builder.emit(Op.PUT_LOCAL, PutKind.INIT, discSlot, idx(node), -1) + emitScopePrologue(analysis.scopeOfNode.get(node)!, true) + const breakLabel = builder.newLabel(entryDepth) + const caseLabels = node.cases.map(() => builder.newLabel(entryDepth)) + let defaultLabel: Label | undefined + node.cases.forEach((clause, i) => { + if (!clause.test) { + defaultLabel = caseLabels[i] + return + } + builder.emit(Op.GET_LOCAL, 0, discSlot, idx(clause), 1) + compileExpression(clause.test) + builder.emit(Op.SEQ, 0, 0, idx(clause), -1) + builder.emitJump(Op.JUMP_IF_TRUE, 0, caseLabels[i]!, idx(clause), -1, -1) + }) + builder.emitJump(Op.JUMP, 0, defaultLabel ?? breakLabel, idx(node), 0, 0) + controlStack.push({ kind: "switch", labels: labels ?? new Set(), breakLabel, entryDepth }) + node.cases.forEach((clause, i) => { + builder.mark(caseLabels[i]!) + for (const s of clause.consequent) compileStatement(s) + }) + controlStack.pop() + builder.mark(breakLabel) + } + + const compileLabeledStatement = (node: LabeledStatement, existingLabels?: Set): void => { + const labels = existingLabels ?? new Set() + let cur: Statement = node + for (; cur.type === "LabeledStatement"; cur = cur.body) labels.add(cur.label.name) + pendingLabels = labels + compileStatement(cur) + } + + const compileTryCatch = (block: BlockStatement, handler: CatchClause, parentNode: AstNode): void => { + const entryDepth = builder.depth + const catchLabel = builder.newLabel(entryDepth + 1) + const endLabel = builder.newLabel(entryDepth) + const tryStart = builder.pc + compileBlockStatement(block) + const tryEnd = builder.pc + builder.emitJump(Op.JUMP, 0, endLabel, idx(parentNode), 0, null) + builder.mark(catchLabel) + builder.addHandler(tryStart, tryEnd, catchLabel.pc, entryDepth) + emitScopePrologue(analysis.scopeOfNode.get(handler)!, true) + if (handler.param) compilePattern(handler.param, true, true, handler) + else builder.emit(Op.POP, 0, 0, idx(handler), -1) + compileBlockStatement(handler.body) + builder.mark(endLabel) + } + + const compileTryStatement = (node: TryStatement, labels?: Set): void => + withStatementLabels(labels, () => { + if (!node.finalizer) return compileTryCatch(node.block, node.handler!, node) + + // try/[catch]/finally with static tag routing (§3.5, C3) + const entryDepth = builder.depth + const level = builder.reserveFinally() + const tagSlot = allocSlot(`%ftag${level}`) + const valSlot = allocSlot(`%fval${level}`) + const finallyLabel = builder.newLabel(entryDepth) + const afterFinallyLabel = builder.newLabel(entryDepth) + const finallyThrowLabel = builder.newLabel(entryDepth + 1) + const finallyEntry = { kind: "finally", tagSlot, valSlot, finallyLabel, entryDepth, exits: new Map() } as const + + controlStack.push(finallyEntry) + const tryStart = builder.pc + if (node.handler) compileTryCatch(node.block, node.handler, node) + else compileBlockStatement(node.block) + const tryEnd = builder.pc + builder.emitNumber(0, idx(node)) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, tagSlot, idx(node), -1) + builder.emitJump(Op.JUMP, 0, finallyLabel, idx(node), 0, null) + + // Throw out of try/catch -> tag = 1, val = e, fall into finally + builder.mark(finallyThrowLabel) + builder.addHandler(tryStart, tryEnd, finallyThrowLabel.pc, entryDepth) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, valSlot, idx(node), -1) + builder.emitNumber(1, idx(node)) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, tagSlot, idx(node), -1) + controlStack.pop() + + // Finally body at `entryDepth` + builder.mark(finallyLabel) + compileBlockStatement(node.finalizer) + + const targets = new Array(finallyEntry.exits.size + 2).fill(-1) + if (builder.depth >= 0) { + builder.emit(Op.END_FINALLY, 0, level, idx(node), 0) + builder.unreachable() + for (const exit of finallyEntry.exits.values()) { + targets[exit.tag] = builder.pc + builder.setDepth(entryDepth) + if (exit.kind === "return") builder.emit(Op.GET_LOCAL, 0, valSlot, idx(node), 1) + exitPlan(exit.kind, exit.target, node) + } + } + + builder.mark(afterFinallyLabel) + targets[0] = afterFinallyLabel.pc + builder.setFinally(level, tagSlot, valSlot, entryDepth, targets) + }) + + // --- Expressions (depth -> depth + 1, or `compileEffect` depth -> depth) --- + const compileEffect = (expr: Expression): void => { + if (expr.type === "AssignmentExpression" && expr.left.type === "Identifier" && !isLogicalAssignment(expr)) { + return compileAssignmentExpression(expr, true) + } + if (expr.type === "UpdateExpression" && expr.argument.type === "Identifier") { + const ref = analysis.refs.get(expr.argument)! + emitLoadRef(ref, expr.argument) + builder.emit(Op.INC, expr.operator === "++" ? 0 : 1, 0, idx(expr), 0) + emitStoreRef(ref, false, expr.argument) + return + } + compileExpression(expr) + builder.emit(Op.POP, 0, 0, idx(expr), -1) + } + + const compileNamedExpression = (expr: Expression, targetName: string): void => { + compileExpression(expr) + if (isAnonymousFunction(expr)) builder.emit(Op.SET_NAME, 0, builder.constant(targetName), idx(expr), 0) + } + + const compileExpression = (expr: Expression, chainShortCircuit?: Label): void => { + const compile = expressions[expr.type] as ((node: Expression, short?: Label) => void) | undefined + if (compile) compile(expr, chainShortCircuit) + else emitUnsupported(expr.type, expr) + } + + const compileLiteral = (node: Literal): void => { + const v = node.value + if (node.regex) { + const k = builder.constant({ pattern: node.regex.pattern, flags: node.regex.flags }) + builder.emit(Op.REGEXP, 0, k, idx(node), 1) + } else if (typeof v === "bigint") + emitThrowError({ type: "TypeError", message: "BigInt literals are not supported." }, node) + else if (v === null) builder.emit(Op.PUSH_NULL, 0, 0, idx(node), 1) + else if (typeof v === "boolean") builder.emit(Op.PUSH_BOOL, v ? 1 : 0, 0, idx(node), 1) + else if (typeof v === "number") builder.emitNumber(v, idx(node)) + else builder.emitString(v as string, idx(node)) + } + + // Stack has `[arr]`: appends every value the spread argument iterates to it. + const compileSpreadAppend = (spreadNode: SpreadElement): void => { + const baseDepth = builder.depth + const itSlot = allocSlot("%it") + compileExpression(spreadNode.argument) + const openSlow = emitIterOpen(IterContext.SPREAD, itSlot, baseDepth, spreadNode) + emitAppendRest(itSlot, baseDepth, spreadNode, openSlow) + } + + const compileArrayExpression = (node: ArrayExpression): void => { + const elements = node.elements + if (!elements.some((el) => el === null || el.type === "SpreadElement")) { + for (const el of elements) compileExpression(el as Expression) + builder.emit(Op.NEW_ARRAY, 0, elements.length, idx(node), 1 - elements.length) + return + } + builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1) + for (const el of elements) { + if (el === null) builder.emit(Op.ARRAY_APPEND, 1, 0, idx(node), 0) + else if (el.type === "SpreadElement") compileSpreadAppend(el) + else { + compileExpression(el) + builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1) + } + } + } + + // Pushes call arguments, or one array of them when one is spread (or there are too many for an operand): true then. + const emitArguments = (args: ReadonlyArray, node: AstNode): boolean => { + if (!args.some((a) => a.type === "SpreadElement") && args.length <= 254) { + for (const arg of args) compileExpression(arg as Expression) + return false + } + builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1) + for (const arg of args) { + if (arg.type === "SpreadElement") compileSpreadAppend(arg) + else { + compileExpression(arg) + builder.emit(Op.ARRAY_APPEND, 0, 0, idx(arg), -1) + } + } + return true + } + + const compileObjectExpression = (node: ObjectExpression): void => { + builder.emit(Op.NEW_OBJECT, 0, 0, idx(node), 1) + for (const prop of node.properties) { + if (prop.type === "SpreadElement") { + compileExpression(prop.argument) + builder.emit(Op.COPY_DATA_PROPS, 0, 0, idx(prop), -1) + } else if (prop.kind !== "init") { + emitThrowError({ type: "TypeError", message: "Only init object properties are supported." }, prop) + } else if (prop.computed) { + compileExpression(prop.key as Expression) + builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0) + compileExpression(prop.value as Expression) + if (isAnonymousFunction(prop.value as Expression)) builder.emit(Op.SET_NAME, 1, 0, idx(prop.value), 0) + builder.emit(Op.DEFINE_FIELD, 1, 0, idx(prop), -2) + } else { + const bigint = prop.key.type === "Literal" && typeof prop.key.value === "bigint" + if (bigint) emitThrowError({ type: "TypeError", message: "BigInt literals are not supported." }, prop.key) + const name = bigint ? "" : keyName(prop.key) + compileNamedExpression(prop.value as Expression, name) + builder.emit(Op.DEFINE_FIELD, 0, builder.constant(name), idx(prop), -1) + } + } + } + + const compileTemplateLiteral = (node: TemplateLiteral): void => { + const quasis = node.quasis + const exprs = node.expressions + // Untagged templates always have cooked strings: an invalid escape is a syntax error. + if (exprs.length === 0) return builder.emitString(quasis[0]!.value.cooked!, idx(node)) + let parts = 0 + quasis.forEach((q, i) => { + if (q.value.cooked!.length > 0 || parts === 0) { + builder.emitString(q.value.cooked!, idx(q)) + parts++ + } + if (i >= exprs.length) return + compileExpression(exprs[i]!) + builder.emit(Op.TO_STRING, 0, 0, idx(exprs[i]), 0) + parts++ + }) + if (parts > 1) builder.emit(Op.CONCAT, 0, parts, idx(node), 1 - parts) + } + + const compileTaggedTemplate = (node: TaggedTemplateExpression): void => { + if (node.tag.type === "MemberExpression") compileMemberExpression(node.tag, true) + else { + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + compileExpression(node.tag) + } + const siteConst = builder.constant({ + cooked: node.quasi.quasis.map((q) => q.value.cooked ?? undefined), + raw: node.quasi.quasis.map((q) => q.value.raw), + nodeIndex: idx(node), + }) + const argc = 1 + node.quasi.expressions.length + if (argc <= 254) { + builder.emit(Op.TEMPLATE, 0, siteConst, idx(node), 1) + for (const expr of node.quasi.expressions) compileExpression(expr) + builder.emit(Op.CALL, argc, 0, idx(node), -(argc + 1)) + return + } + builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1) + builder.emit(Op.TEMPLATE, 0, siteConst, idx(node), 1) + builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1) + for (const expr of node.quasi.expressions) { + compileExpression(expr) + builder.emit(Op.ARRAY_APPEND, 0, 0, idx(expr), -1) + } + builder.emit(Op.CALL_SPREAD, 0, 0, idx(node), -2) + } + + const compileUnaryExpression = (node: UnaryExpression): void => { + const op = node.operator + if (op === "delete") return compileDeleteExpression(node) + const ref = node.argument.type === "Identifier" ? analysis.refs.get(node.argument)! : undefined + if (op === "typeof" && ref?.kind === "global") return emitLoadRef(ref, node.argument, true) + compileExpression(node.argument) + if (op === "!") builder.emit(Op.NOT, 0, 0, idx(node), 0) + else builder.emit(Op.UNARY, UnaryOps[op]!, 0, idx(node), 0) + } + + const compileDeleteExpression = (node: UnaryExpression): void => { + const arg = node.argument + const isChain = arg.type === "ChainExpression" + const target = isChain ? arg.expression : arg + if (target.type === "Identifier") { + return emitThrowError({ type: "TypeError", message: "Only data fields may be deleted." }, target) + } + if (target.type !== "MemberExpression") { + compileExpression(arg) + builder.emit(Op.POP, 0, 0, idx(node), -1) + builder.emit(Op.PUSH_BOOL, 1, 0, idx(node), 1) + return + } + if (unsupportedSuper(target)) return + const shortLabel = isChain || target.optional ? builder.newLabel(builder.depth + 1) : undefined + compileExpression(target.object as Expression, shortLabel) + if (target.optional && shortLabel) { + builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, shortLabel, idx(target), 0, 0) + } + if (target.computed) { + compileExpression(target.property as Expression) + builder.emit(Op.DELETE_ELEM, 0, 0, idx(target), -1) + } else builder.emit(Op.DELETE_PROP, 0, builder.constant(propertyName(target)), idx(target), 0) + if (shortLabel && shortLabel.patches.length > 0) { + const endLabel = builder.newLabel(builder.depth) + builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, 0) + builder.mark(shortLabel) + builder.emit(Op.POP, 0, 0, idx(node), -1) + builder.emit(Op.PUSH_BOOL, 1, 0, idx(node), 1) + builder.mark(endLabel) + } + } + + const compileUpdateExpression = (node: UpdateExpression): void => { + const incFlag = node.operator === "++" ? 0 : 1 + const arg = node.argument + if (arg.type === "Identifier") { + const ref = analysis.refs.get(arg)! + emitLoadRef(ref, arg) + if (node.prefix) builder.emit(Op.INC, incFlag, 0, idx(node), 0) + else builder.emit(Op.TO_NUMERIC, incFlag, 0, idx(arg), 0) + builder.emit(Op.DUP, 0, 0, idx(node), 1) + if (!node.prefix) builder.emit(Op.INC, incFlag, 0, idx(node), 0) + emitStoreRef(ref, false, arg) + return + } + if (arg.type !== "MemberExpression") { + return emitThrowError( + { type: "TypeError", message: "Update target must be an Identifier or MemberExpression." }, + arg, + ) + } + if (unsupportedSuper(arg)) return + compileExpression(arg.object as Expression) + const k = arg.computed ? 0 : builder.constant(propertyName(arg)) + if (arg.computed) { + compileExpression(arg.property as Expression) + emitWritableKey(arg) + builder.emit(Op.PICK, 1, 0, idx(arg), 1) + builder.emit(Op.PICK, 1, 0, idx(arg), 1) + builder.emit(Op.GET_ELEM, 0, 0, idx(arg), -1) + } else { + builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(arg), 0) + builder.emit(Op.DUP, 0, 0, idx(arg), 1) + builder.emit(Op.GET_PROP, 0, k, idx(arg), 0) + } + builder.emit(Op.TO_NUMERIC, incFlag, 0, idx(arg), 0) + if (!node.prefix) { + builder.emit(Op.DUP, 0, 0, idx(node), 1) + builder.emit(Op.ROT, arg.computed ? 3 : 2, 0, idx(node), 0) + } + builder.emit(Op.INC, incFlag, 0, idx(node), 0) + if (arg.computed) builder.emit(Op.PUT_ELEM, 0, 0, idx(arg), -2) + else builder.emit(Op.PUT_PROP, 0, k, idx(arg), -1) + if (!node.prefix) builder.emit(Op.POP, 0, 0, idx(node), -1) + } + + const emitBinaryOp = (op: string, node: AstNode): void => { + const [code, a] = BinaryOps[op]! + builder.emit(code, a, 0, idx(node), -1) + } + + const compileBinaryExpression = (node: BinaryExpression): void => { + compileExpression(node.left as Expression) + compileExpression(node.right) + emitBinaryOp(node.operator, node) + } + + const compileLogicalExpression = (node: LogicalExpression): void => { + const endLabel = builder.newLabel(builder.depth + 1) + compileExpression(node.left) + builder.emitJump(Op.JUMP_COND, logicalCond(node.operator), endLabel, idx(node), 0, -1) + compileExpression(node.right) + builder.mark(endLabel) + } + + const compileConditionalExpression = (node: ConditionalExpression): void => { + const elseLabel = builder.newLabel(builder.depth) + const endLabel = builder.newLabel(builder.depth + 1) + compileExpression(node.test) + builder.emitJump(Op.JUMP_IF_FALSE, 0, elseLabel, idx(node), -1, -1) + compileExpression(node.consequent) + builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, -1) + builder.mark(elseLabel) + compileExpression(node.alternate) + builder.mark(endLabel) + } + + const isLogicalAssignment = (node: AssignmentExpression) => + node.operator === "&&=" || node.operator === "||=" || node.operator === "??=" + + // `discard` skips keeping the assigned value, for an assignment to an identifier used as a statement. + const compileAssignmentExpression = (node: AssignmentExpression, discard = false): void => { + const left = node.left + const op = node.operator + const isLogical = isLogicalAssignment(node) + + if (left.type === "Identifier") { + const ref = analysis.refs.get(left)! + const endLabel = builder.newLabel(builder.depth + 1) + if (op !== "=") emitLoadRef(ref, left) + if (isLogical) builder.emitJump(Op.JUMP_COND, logicalCond(op), endLabel, idx(node), 0, -1) + if (op === "=" || isLogical) compileNamedExpression(node.right, left.name) + else { + compileExpression(node.right) + emitBinaryOp(op.slice(0, -1), node) + } + if (!discard) builder.emit(Op.DUP, 0, 0, idx(node), 1) + emitStoreRef(ref, false, left) + if (isLogical) builder.mark(endLabel) + return + } + + if (left.type === "ObjectPattern" || left.type === "ArrayPattern") { + compileExpression(node.right) + builder.emit(Op.DUP, 0, 0, idx(node), 1) + compilePattern(left, false, false, node) + return + } + + if (left.type !== "MemberExpression") { + return emitThrowError( + { type: "TypeError", message: "Assignment target must be an Identifier or MemberExpression." }, + left, + ) + } + if (unsupportedSuper(left)) return + + const baseDepth = builder.depth + compileExpression(left.object as Expression) + const k = left.computed ? 0 : builder.constant(propertyName(left)) + const put = () => + left.computed ? builder.emit(Op.PUT_ELEM, 0, 0, idx(left), -2) : builder.emit(Op.PUT_PROP, 0, k, idx(left), -1) + if (left.computed) { + compileExpression(left.property as Expression) + emitWritableKey(left) + } else builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(left), 0) + if (op === "=") { + compileExpression(node.right) + put() + return + } + if (left.computed) { + builder.emit(Op.PICK, 1, 0, idx(left), 1) + builder.emit(Op.PICK, 1, 0, idx(left), 1) + builder.emit(Op.GET_ELEM, 0, 0, idx(left), -1) + } else { + builder.emit(Op.DUP, 0, 0, idx(left), 1) + builder.emit(Op.GET_PROP, 0, k, idx(left), 0) + } + if (!isLogical) { + compileExpression(node.right) + emitBinaryOp(op.slice(0, -1), node) + put() + return + } + // Short-circuits with `[o, key, current]` (or `[o, current]`) on the stack, and keeps `current`. + const shortLabel = builder.newLabel(baseDepth + (left.computed ? 3 : 2)) + const endLabel = builder.newLabel(baseDepth + 1) + builder.emitJump(Op.JUMP_COND, logicalCond(op), shortLabel, idx(node), 0, -1) + compileExpression(node.right) + put() + builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, -1) + builder.mark(shortLabel) + builder.emit(Op.ROT, left.computed ? 2 : 1, 0, idx(node), 0) + builder.emit(Op.POP, 0, 0, idx(node), -1) + if (left.computed) builder.emit(Op.POP, 0, 0, idx(node), -1) + builder.mark(endLabel) + } + + const compileMemberExpression = (node: MemberExpression, keepReceiver: boolean, chainShortCircuit?: Label): void => { + if (unsupportedSuper(node)) return + compileExpression(node.object as Expression, chainShortCircuit) + if (node.optional && chainShortCircuit) { + builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, chainShortCircuit, idx(node), 0, 0) + } + const keepFlag = keepReceiver ? 1 : 0 + if (node.computed) { + compileExpression(node.property as Expression) + if (keepReceiver) { + builder.emit(Op.CHECK_BASE, CheckBaseKind.READ, 0, idx(node), 0) + builder.emit(Op.TO_KEY, 0, 0, idx(node.property), 0) + } + builder.emit(Op.GET_ELEM, keepFlag, 0, idx(node), keepReceiver ? 0 : -1) + } else { + builder.emit(Op.GET_PROP, keepFlag, builder.constant(propertyName(node)), idx(node), keepReceiver ? 1 : 0) + } + } + + const compileCallExpression = (node: CallExpression, chainShortCircuit?: Label): void => { + if (node.callee.type === "Super") return emitUnsupported("Super", node.callee) + const args = node.arguments + // Lower `$Call(fn, thisArg, ...args)` in internal/prelude code directly to `Op.CALL` (§5.1) + if ( + internal && + !node.optional && + node.callee.type === "Identifier" && + node.callee.name === "$Call" && + args.length >= 2 && + args.length - 2 <= 254 && + !args.some((a) => a.type === "SpreadElement") + ) { + for (const arg of [args[1]!, args[0]!, ...args.slice(2)]) compileExpression(arg as Expression) + builder.emit(Op.CALL, args.length - 2, 0, idx(node), -(args.length - 1)) + return + } + const shortLabel = chainShortCircuit ?? (node.optional ? builder.newLabel(builder.depth + 1) : undefined) + if (node.callee.type === "MemberExpression") { + compileMemberExpression(node.callee, true, shortLabel) + if (node.optional && shortLabel) { + builder.emitJump(Op.JUMP_COND, CondKind.NULLISH_METHOD, shortLabel, idx(node), -1, 0) + } + } else if (shortLabel) { + compileExpression(node.callee, shortLabel) + if (node.optional) builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, shortLabel, idx(node), 0, 0) + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + builder.emit(Op.ROT, 1, 0, idx(node), 0) + } else { + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + compileExpression(node.callee) + } + if (emitArguments(args, node)) builder.emit(Op.CALL_SPREAD, 0, 0, idx(node), -2) + else builder.emit(Op.CALL, args.length, 0, idx(node), -(args.length + 1)) + if (!chainShortCircuit && shortLabel) builder.mark(shortLabel) + } + + const compileNewExpression = (node: NewExpression): void => { + compileExpression(node.callee as Expression) + // Check constructibility before evaluating arguments + builder.emit(Op.NEW, 255, 0, idx(node), 0) + const args = node.arguments + if (emitArguments(args, node)) builder.emit(Op.NEW_SPREAD, 0, 0, idx(node), -1) + else builder.emit(Op.NEW, args.length, 0, idx(node), -args.length) + } + + const compileImportExpression = (node: ImportExpression): void => { + compileExpression(node.source) + builder.emit(Op.TO_STRING, 1, 0, idx(node.source), 0) + if (node.options) { + compileExpression(node.options) + builder.emit(Op.POP, 0, 0, idx(node.options), -1) + } + builder.emit(Op.IMPORT, ImportKind.DYNAMIC, 0, idx(node), 0) + } + + const compileAwaitExpression = (node: AwaitExpression): void => { + compileExpression(node.argument) + builder.emit(Op.AWAIT, internal ? 1 : 0, 0, idx(node), 0) + } + + const isAsync = (fnInfo.flags & CodeFlags.ASYNC) !== 0 + + const compileYieldExpression = (node: YieldExpression): void => { + if (node.delegate) return compileYieldDelegate(node) + const targetDepth = builder.depth + 1 + if (node.argument) compileExpression(node.argument) + else builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + if (isAsync) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + builder.emit(Op.YIELD, 0, 0, idx(node), 1) + // C2: RESUME jumps to `normalLabel` on next (`[recv]`), throws on throw, and falls through on return (`[recv]`). + const normalLabel = builder.newLabel(targetDepth) + builder.emitJump(Op.RESUME, 0, normalLabel, idx(node), -1, -1) + if (isAsync) builder.emit(Op.AWAIT, 1, 0, idx(node), 0) + exitPlan("return", null, node) + builder.mark(normalLabel) + } + + // Pushes whether the resume kind in `kindSlot` (or on top, when `kindSlot` is -1) is `return`. + const emitIsReturn = (kindSlot: number, node: AstNode): void => { + if (kindSlot < 0) builder.emit(Op.DUP, 0, 0, idx(node), 1) + else builder.emit(Op.GET_LOCAL, 0, kindSlot, idx(node), 1) + builder.emitNumber(2, idx(node)) + builder.emit(Op.SEQ, 0, 0, idx(node), -1) + } + + const compileYieldDelegate = (node: YieldExpression): void => { + const baseDepth = builder.depth + const itSlot = allocSlot("%it") + const kindSlot = allocSlot("%kind") + compileExpression(node.argument!) + const openSlow = emitIterOpen(isAsync ? IterContext.ASYNC_DELEGATE : IterContext.DELEGATE, itSlot, baseDepth, node) + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1) + builder.emitNumber(0, idx(node)) + + const loopLabel = builder.newLabel(baseDepth + 2) + const callLabel = builder.newLabel(baseDepth + 3) + const noThrowLabel = builder.newLabel(baseDepth + 1) + const doneLabel = builder.newLabel(baseDepth + 1) + const exprDoneLabel = builder.newLabel(baseDepth + 1) + + builder.mark(loopLabel) + builder.emit(Op.DUP, 0, 0, idx(node), 1) + builder.emit(Op.PUT_LOCAL, PutKind.INIT, kindSlot, idx(node), -1) + // YSTAR jumps to `callLabel` (`[iterator, method, recv]`) when the method exists, and falls through (`[recv]`) when missing. + builder.emitJump(Op.YSTAR, itSlot, callLabel, idx(node), 1, -1) + emitIsReturn(kindSlot, node) + builder.emitJump(Op.JUMP_IF_FALSE, 0, noThrowLabel, idx(node), -1, -1) + // Missing `return` method: await received value in async generators, then return + if (isAsync) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + exitPlan("return", null, node) + + // Missing `throw` method: close iterator, then throw TypeError + builder.mark(noThrowLabel) + builder.emit(Op.POP, 0, 0, idx(node), -1) + emitIteratorCloseNormal(itSlot, isAsync, node) + emitThrowError({ type: "TypeError", message: "The delegated iterator does not provide a throw() method." }, node) + + builder.mark(callLabel) + builder.emit(Op.CALL, 1, 0, idx(node), -2) + if (isAsync) builder.emit(Op.AWAIT, 0, 0, idx(node), 0) + builder.emitJump(Op.YSTAR_RESULT, kindSlot, doneLabel, idx(node), 0, 0) + builder.emit(Op.YIELD, 0, 0, idx(node), 1) + if (isAsync) { + const notReturnLabel = builder.newLabel(baseDepth + 2) + emitIsReturn(-1, node) + builder.emitJump(Op.JUMP_IF_FALSE, 0, notReturnLabel, idx(node), -1, -1) + builder.emit(Op.ROT, 1, 0, idx(node), 0) + builder.emit(Op.AWAIT, 1, 0, idx(node), 0) + builder.emit(Op.ROT, 1, 0, idx(node), 0) + builder.mark(notReturnLabel) + } + builder.emitJump(Op.LOOP, 0, loopLabel, idx(node), 0, null) + openSlow() + + builder.mark(doneLabel) + emitIsReturn(kindSlot, node) + builder.emitJump(Op.JUMP_IF_FALSE, 0, exprDoneLabel, idx(node), -1, -1) + exitPlan("return", null, node) + builder.mark(exprDoneLabel) + } + + type Compilers = { + readonly [K in N["type"]]?: (node: Extract, arg?: A) => void + } + + const statements: Compilers> = { + EmptyStatement: () => {}, + FunctionDeclaration: () => {}, + ImportDeclaration: (stmt) => { + if (isProgram) return + const message = "An import declaration may only appear at the top level of the program." + emitThrowError({ type: "SyntaxError", message }, stmt) + }, + ExpressionStatement: (stmt) => compileEffect(stmt.expression), + VariableDeclaration: compileVariableDeclaration, + BlockStatement: compileBlockStatement, + IfStatement: compileIfStatement, + WhileStatement: compileWhileStatement, + DoWhileStatement: compileDoWhileStatement, + ForStatement: compileForStatement, + ForInStatement: compileForInStatement, + ForOfStatement: compileForOfStatement, + SwitchStatement: compileSwitchStatement, + LabeledStatement: compileLabeledStatement, + BreakStatement: (stmt) => exitPlan("break", findExitTarget("break", stmt.label?.name), stmt), + ContinueStatement: (stmt) => exitPlan("continue", findExitTarget("continue", stmt.label?.name), stmt), + ReturnStatement: (stmt) => { + if (stmt.argument) { + compileExpression(stmt.argument) + if (isAsync && (fnInfo.flags & CodeFlags.GENERATOR) !== 0) builder.emit(Op.AWAIT, 0, 0, idx(stmt), 0) + } else builder.emit(Op.PUSH_UNDEF, 0, 0, idx(stmt), 1) + exitPlan("return", null, stmt) + }, + ThrowStatement: (stmt) => { + compileExpression(stmt.argument) + builder.emit(Op.THROW, 0, 0, idx(stmt), -1) + builder.unreachable() + }, + TryStatement: compileTryStatement, + } + + const expressions: Compilers = { + Literal: compileLiteral, + Identifier: (expr) => emitLoadRef(analysis.refs.get(expr)!, expr), + ThisExpression: (expr) => emitLoadRef(analysis.thisRefs.get(expr)!, expr), + FunctionExpression: (expr) => builder.emit(Op.CLOSURE, 0, analysis.nodeToFunction.get(expr)!.index, idx(expr), 1), + ArrowFunctionExpression: (expr) => + builder.emit(Op.CLOSURE, 0, analysis.nodeToFunction.get(expr)!.index, idx(expr), 1), + ArrayExpression: compileArrayExpression, + ObjectExpression: compileObjectExpression, + TemplateLiteral: compileTemplateLiteral, + TaggedTemplateExpression: compileTaggedTemplate, + UnaryExpression: compileUnaryExpression, + UpdateExpression: compileUpdateExpression, + BinaryExpression: compileBinaryExpression, + LogicalExpression: compileLogicalExpression, + ConditionalExpression: compileConditionalExpression, + SequenceExpression: (expr) => + expr.expressions.forEach((e, i) => { + compileExpression(e) + if (i < expr.expressions.length - 1) builder.emit(Op.POP, 0, 0, idx(e), -1) + }), + AssignmentExpression: (expr) => compileAssignmentExpression(expr), + MemberExpression: (expr, short) => compileMemberExpression(expr, false, short), + CallExpression: compileCallExpression, + NewExpression: compileNewExpression, + ChainExpression: (expr) => { + const shortLabel = builder.newLabel(builder.depth + 1) + compileExpression(expr.expression, shortLabel) + builder.mark(shortLabel) + }, + ImportExpression: compileImportExpression, + AwaitExpression: compileAwaitExpression, + YieldExpression: compileYieldExpression, + } + + // --- Emit Function / Program Body --- + // `this`, `arguments`, and a named function expression's own name, when the body uses them. + const emitImplicit = (decl: Decl | undefined, argsKind: number): void => { + if (!decl?.used) return + if (decl.captured) builder.emit(Op.NEW_CELL, 0, decl.slot) + if (isProgram) builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1) + else builder.emit(Op.ARGS, argsKind, 0, idx(fnInfo.node), 1) + emitStoreDecl(decl, true, isProgram ? undefined : fnInfo.node) + } + + if (isProgram) { + const prog = fnInfo.node as Program + emitImplicit(fnInfo.thisDecl, 3) + for (const decl of fnInfo.bodyScope.decls.values()) { + if (decl.kind !== "this" && decl.captured) builder.emit(Op.NEW_CELL, 0, decl.slot) + } + emitImports(prog.body) + for (const decl of fnInfo.bodyScope.decls.values()) { + if (decl.kind !== "var") continue + builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1) + emitStoreDecl(decl, true) + } + emitHoistedFunctions(fnInfo.bodyScope) + const last = prog.body.at(-1) + for (const s of prog.body) if (s !== last || s.type !== "ExpressionStatement") compileStatement(s) + if (last?.type === "ExpressionStatement") { + compileExpression(last.expression) + builder.emit(Op.END_PROGRAM, 0, 0, idx(last), -1) + } else { + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(prog), 1) + builder.emit(Op.END_PROGRAM, 0, 0, idx(prog), -1) + } + } else { + emitImplicit(fnInfo.selfScope?.decls.values().next().value, 2) + emitImplicit(fnInfo.thisDecl, 3) + const simple = (fnInfo.flags & CodeFlags.SIMPLE_PARAMS) !== 0 + for (const decl of fnInfo.paramScope.decls.values()) { + // A simple parameter's argument is already in its slot: it moves into the new cell. + if (decl.kind === "param" && decl.captured) + builder.emit(Op.NEW_CELL, simple ? 1 : 0, decl.slot, idx(fnInfo.node), 0) + } + emitImplicit(fnInfo.argumentsDecl, 0) + if (!simple) { + fnInfo.params.forEach((param, i) => { + if (param.type === "RestElement") { + builder.emit(Op.ARGS, 1, i, idx(param), 1) + return compilePattern(param.argument, true, true, param) + } + // Duplicate plain identifier parameter: only the last occurrence initializes the binding + const name = param.type === "Identifier" ? param.name : undefined + if (name !== undefined && fnInfo.params.findLastIndex((p) => p.type === "Identifier" && p.name === name) !== i) + return + builder.emit(Op.ARGS, 4, i, idx(param), 1) + compilePattern(param, true, true, param) + }) + } + emitScopePrologue(fnInfo.bodyScope, false) + if ((fnInfo.flags & CodeFlags.GENERATOR) !== 0) builder.emit(Op.GENERATOR_START, 0, 0, idx(fnInfo.node), 0) + + const body = fnInfo.node.body as BlockStatement | Expression + if (body.type === "BlockStatement") { + for (const s of body.body) compileStatement(s) + builder.emit(Op.PUSH_UNDEF, 0, 0, idx(body), 1) + } else compileExpression(body) + builder.emit(Op.RETURN, 0, 0, idx(body), -1) + } + + return builder.finish() +} + +const propertyName = (node: MemberExpression): string => (node.property as { name: string }).name + +const keyName = (key: Expression | { type: "PrivateIdentifier" }): string => + key.type === "Identifier" ? key.name : String((key as Literal).value) diff --git a/packages/codemode/src/vm/compiler/scope.ts b/packages/codemode/src/vm/compiler/scope.ts new file mode 100644 index 00000000000..503d07e43bf --- /dev/null +++ b/packages/codemode/src/vm/compiler/scope.ts @@ -0,0 +1,549 @@ +import type { + AnyNode, + ArrowFunctionExpression, + BlockStatement, + CatchClause, + Expression, + FunctionDeclaration, + FunctionExpression, + Identifier, + ModuleDeclaration, + Pattern, + Program, + Statement, + VariableDeclaration, +} from "acorn" +import type { Source } from "../runtime/source.js" +import { CodeFlags } from "../opcodes.js" + +type FunctionNode = Program | FunctionDeclaration | FunctionExpression | ArrowFunctionExpression + +type DeclKind = "var" | "let" | "const" | "function" | "param" | "catch" | "import" | "self" | "this" | "arguments" + +export type Decl = { + readonly name: string + readonly kind: DeclKind + readonly fnIndex: number + mutable: boolean + captured: boolean + used: boolean + slot: number + /** When a body `var` shares a name with a parameter or `arguments` in a separate `paramScope`. */ + copyFromParam?: Decl | undefined +} + +export class Scope { + readonly decls = new Map() + readonly hoistedFunctions: Array<{ readonly node: FunctionDeclaration; fnIndex: number; readonly decl: Decl }> = [] + + constructor(readonly parent: Scope | undefined) {} +} + +type UpvalueInfo = { + /** `0` = parent's local cell slot, `1` = parent's upvalue index. */ + readonly from: 0 | 1 + index: number + readonly decl: Decl +} + +export type FunctionInfo = { + readonly index: number + readonly node: FunctionNode + readonly parentIndex: number + readonly name: string + flags: number + readonly length: number + readonly params: ReadonlyArray + readonly selfScope: Scope | undefined + readonly paramScope: Scope + readonly bodyScope: Scope + readonly allDecls: Array + readonly upvalues: Array + readonly upvalueMap: Map + thisDecl: Decl | undefined + argumentsDecl: Decl | undefined + locals: number + readonly localNames: Array +} + +export type ResolvedRef = + | { readonly kind: "local"; readonly decl: Decl } + | { readonly kind: "upval"; readonly upvalIndex: number; readonly decl: Decl } + | { readonly kind: "global"; readonly name: string } + +export type ScopeAnalysis = { + readonly functions: ReadonlyArray + readonly nodeToFunction: ReadonlyMap + readonly scopeOfNode: ReadonlyMap + readonly refs: ReadonlyMap + readonly thisRefs: ReadonlyMap +} + +/** + * Pass 1: walks the AST to build lexical/var scopes, assign pre-order function indices, + * resolve all identifier and `this` references, mark captured bindings, and assign local/upvalue slots. + */ +export const analyzeScopes = (source: Source, internal = false): ScopeAnalysis => { + const functions: Array = [] + const nodeToFunction = new Map() + const scopeOfNode = new Map() + const unresolved: Array<{ readonly node: AnyNode; readonly name: string; readonly scope: Scope; fnIndex: number }> = + [] + + const addDecl = (scope: Scope, fnInfo: FunctionInfo, name: string, kind: DeclKind, mutable: boolean): Decl => { + const existing = scope.decls.get(name) + if (existing !== undefined) { + if (mutable) existing.mutable = true + return existing + } + const decl: Decl = { name, kind, fnIndex: fnInfo.index, mutable, captured: false, used: false, slot: -1 } + scope.decls.set(name, decl) + fnInfo.allDecls.push(decl) + return decl + } + + const declareLexical = (node: VariableDeclaration, scope: Scope, fnInfo: FunctionInfo): void => { + if (node.kind === "var") return + for (const d of node.declarations) { + for (const name of collectPatternNames(d.id)) + addDecl(scope, fnInfo, name, node.kind === "let" ? "let" : "const", node.kind === "let") + } + } + + const visitFunction = (node: FunctionNode, parentScope: Scope | undefined, parentFnIndex: number): FunctionInfo => { + const index = functions.length + const isProgram = node.type === "Program" + const isArrow = node.type === "ArrowFunctionExpression" + const params: ReadonlyArray = isProgram ? [] : node.params + const simple = params.every((p) => p.type === "Identifier") + + let flags = 0 + if (isArrow) flags |= CodeFlags.ARROW + if (isProgram || node.async) flags |= CodeFlags.ASYNC + if (!isProgram && node.generator) flags |= CodeFlags.GENERATOR + if (simple) flags |= CodeFlags.SIMPLE_PARAMS + if (internal) flags |= CodeFlags.INTERNAL + if (isProgram) flags |= CodeFlags.PROGRAM + + const selfScope = node.type === "FunctionExpression" && node.id ? new Scope(parentScope) : undefined + const paramScope = new Scope(selfScope ?? parentScope) + const bodyScope = simple ? paramScope : new Scope(paramScope) + const lengthEnd = params.findIndex((p) => p.type === "AssignmentPattern" || p.type === "RestElement") + + const fnInfo: FunctionInfo = { + index, + node, + parentIndex: parentFnIndex, + name: isProgram ? "" : isArrow ? "" : (node.id?.name ?? ""), + flags, + length: lengthEnd === -1 ? params.length : lengthEnd, + params, + selfScope, + paramScope, + bodyScope, + allDecls: [], + upvalues: [], + upvalueMap: new Map(), + thisDecl: undefined, + argumentsDecl: undefined, + locals: 0, + localNames: [], + } + functions.push(fnInfo) + nodeToFunction.set(node, fnInfo) + scopeOfNode.set(node, bodyScope) + + if (selfScope && node.type === "FunctionExpression") addDecl(selfScope, fnInfo, node.id!.name, "self", false) + if (!isArrow) fnInfo.thisDecl = addDecl(paramScope, fnInfo, "this", "this", false) + const paramNames = params.flatMap((param) => collectPatternNames(param)) + if (!isProgram && !isArrow && !paramNames.includes("arguments")) { + fnInfo.argumentsDecl = addDecl(paramScope, fnInfo, "arguments", "arguments", true) + } + for (const name of paramNames) addDecl(paramScope, fnInfo, name, "param", true) + for (const param of params) visitPattern(param, paramScope, fnInfo) + + if (node.type !== "Program" && node.body.type !== "BlockStatement") visitExpression(node.body, bodyScope, fnInfo) + else { + if (node.type !== "Program") scopeOfNode.set(node.body, bodyScope) + const stmts = node.type === "Program" ? node.body : (node.body as BlockStatement).body + hoistStatementList(stmts, bodyScope, bodyScope, fnInfo, isProgram) + for (const stmt of stmts) visitStatement(stmt, bodyScope, bodyScope, fnInfo) + } + + if (!simple) { + for (const [varName, decl] of bodyScope.decls) { + const paramDecl = decl.kind === "var" ? paramScope.decls.get(varName) : undefined + if (paramDecl?.kind !== "param" && paramDecl?.kind !== "arguments") continue + decl.copyFromParam = paramDecl + paramDecl.used = true + if (paramDecl.kind === "arguments") fnInfo.flags |= CodeFlags.USES_ARGUMENTS | CodeFlags.NEEDS_ARGS + } + } + return fnInfo + } + + const hoistStatementList = ( + stmts: ReadonlyArray, + lexicalScope: Scope, + varScope: Scope, + fnInfo: FunctionInfo, + isProgram: boolean, + ): void => { + if (isProgram) { + for (const stmt of stmts) { + if (stmt.type !== "ImportDeclaration") continue + for (const spec of stmt.specifiers) addDecl(lexicalScope, fnInfo, spec.local.name, "import", false) + } + } + for (const stmt of stmts) if (stmt.type === "VariableDeclaration") declareLexical(stmt, lexicalScope, fnInfo) + for (const stmt of stmts) { + if (stmt.type !== "FunctionDeclaration" || !stmt.id || (internal && isProgram)) continue + const decl = addDecl(lexicalScope, fnInfo, stmt.id.name, "function", true) + lexicalScope.hoistedFunctions.push({ node: stmt, fnIndex: -1, decl }) + } + if (lexicalScope === varScope) for (const stmt of stmts) collectVars(stmt, varScope, fnInfo) + } + + const collectVars = ( + stmt: Statement | ModuleDeclaration | null | undefined, + varScope: Scope, + fnInfo: FunctionInfo, + ) => { + switch (stmt?.type) { + case "VariableDeclaration": + if (stmt.kind !== "var") break + for (const d of stmt.declarations) { + for (const name of collectPatternNames(d.id)) { + if (!varScope.decls.has(name)) addDecl(varScope, fnInfo, name, "var", true) + } + } + break + case "BlockStatement": + for (const s of stmt.body) collectVars(s, varScope, fnInfo) + break + case "IfStatement": + collectVars(stmt.consequent, varScope, fnInfo) + collectVars(stmt.alternate, varScope, fnInfo) + break + case "WhileStatement": + case "DoWhileStatement": + case "LabeledStatement": + collectVars(stmt.body, varScope, fnInfo) + break + case "ForStatement": + if (stmt.init?.type === "VariableDeclaration") collectVars(stmt.init, varScope, fnInfo) + collectVars(stmt.body, varScope, fnInfo) + break + case "ForInStatement": + case "ForOfStatement": + if (stmt.left.type === "VariableDeclaration") collectVars(stmt.left, varScope, fnInfo) + collectVars(stmt.body, varScope, fnInfo) + break + case "SwitchStatement": + for (const c of stmt.cases) for (const s of c.consequent) collectVars(s, varScope, fnInfo) + break + case "TryStatement": + collectVars(stmt.block, varScope, fnInfo) + collectVars(stmt.handler?.body, varScope, fnInfo) + collectVars(stmt.finalizer, varScope, fnInfo) + break + } + } + + const visitStatement = ( + stmt: Statement | ModuleDeclaration, + scope: Scope, + varScope: Scope, + fnInfo: FunctionInfo, + ): void => { + switch (stmt.type) { + case "ExpressionStatement": + visitExpression(stmt.expression, scope, fnInfo) + break + case "VariableDeclaration": + for (const d of stmt.declarations) { + visitPattern(d.id, scope, fnInfo) + if (d.init) visitExpression(d.init, scope, fnInfo) + } + break + case "ReturnStatement": + case "ThrowStatement": + if (stmt.argument) visitExpression(stmt.argument, scope, fnInfo) + break + case "BlockStatement": + visitBlock(stmt, new Scope(scope), varScope, fnInfo) + break + case "IfStatement": + visitExpression(stmt.test, scope, fnInfo) + visitStatement(stmt.consequent, scope, varScope, fnInfo) + if (stmt.alternate) visitStatement(stmt.alternate, scope, varScope, fnInfo) + break + case "WhileStatement": + case "DoWhileStatement": + visitExpression(stmt.test, scope, fnInfo) + visitStatement(stmt.body, scope, varScope, fnInfo) + break + case "ForStatement": { + const forScope = new Scope(scope) + scopeOfNode.set(stmt, forScope) + if (stmt.init?.type === "VariableDeclaration") { + declareLexical(stmt.init, forScope, fnInfo) + visitStatement(stmt.init, forScope, varScope, fnInfo) + } else if (stmt.init) visitExpression(stmt.init, forScope, fnInfo) + if (stmt.test) visitExpression(stmt.test, forScope, fnInfo) + if (stmt.update) visitExpression(stmt.update, forScope, fnInfo) + visitStatement(stmt.body, forScope, varScope, fnInfo) + break + } + case "ForInStatement": + case "ForOfStatement": { + const forScope = new Scope(scope) + scopeOfNode.set(stmt, forScope) + if (stmt.left.type === "VariableDeclaration") { + declareLexical(stmt.left, forScope, fnInfo) + for (const d of stmt.left.declarations) visitPattern(d.id, forScope, fnInfo) + } else visitPattern(stmt.left, forScope, fnInfo) + visitExpression(stmt.right, forScope, fnInfo) + visitStatement(stmt.body, forScope, varScope, fnInfo) + break + } + case "SwitchStatement": { + visitExpression(stmt.discriminant, scope, fnInfo) + const switchScope = new Scope(scope) + scopeOfNode.set(stmt, switchScope) + hoistStatementList( + stmt.cases.flatMap((c) => c.consequent), + switchScope, + varScope, + fnInfo, + false, + ) + for (const c of stmt.cases) { + if (c.test) visitExpression(c.test, switchScope, fnInfo) + for (const s of c.consequent) visitStatement(s, switchScope, varScope, fnInfo) + } + break + } + case "LabeledStatement": + visitStatement(stmt.body, scope, varScope, fnInfo) + break + case "TryStatement": + visitBlock(stmt.block, new Scope(scope), varScope, fnInfo) + if (stmt.handler) visitCatchClause(stmt.handler, scope, varScope, fnInfo) + if (stmt.finalizer) visitBlock(stmt.finalizer, new Scope(scope), varScope, fnInfo) + break + case "FunctionDeclaration": { + const childFn = visitFunction(stmt, scope, fnInfo.index) + const hoisted = scope.hoistedFunctions.find((h) => h.node === stmt) + if (hoisted) hoisted.fnIndex = childFn.index + break + } + } + } + + const visitBlock = (block: BlockStatement, blockScope: Scope, varScope: Scope, fnInfo: FunctionInfo): void => { + scopeOfNode.set(block, blockScope) + hoistStatementList(block.body, blockScope, varScope, fnInfo, false) + for (const s of block.body) visitStatement(s, blockScope, varScope, fnInfo) + } + + const visitCatchClause = (handler: CatchClause, scope: Scope, varScope: Scope, fnInfo: FunctionInfo): void => { + const catchScope = new Scope(scope) + scopeOfNode.set(handler, catchScope) + if (handler.param) { + for (const name of collectPatternNames(handler.param)) addDecl(catchScope, fnInfo, name, "catch", true) + visitPattern(handler.param, catchScope, fnInfo) + } + visitBlock(handler.body, new Scope(catchScope), varScope, fnInfo) + } + + const visitPattern = (pattern: Pattern, scope: Scope, fnInfo: FunctionInfo): void => { + switch (pattern.type) { + case "Identifier": + unresolved.push({ node: pattern, name: pattern.name, scope, fnIndex: fnInfo.index }) + break + case "AssignmentPattern": + visitPattern(pattern.left, scope, fnInfo) + visitExpression(pattern.right, scope, fnInfo) + break + case "RestElement": + visitPattern(pattern.argument, scope, fnInfo) + break + case "ArrayPattern": + for (const el of pattern.elements) if (el) visitPattern(el, scope, fnInfo) + break + case "ObjectPattern": + for (const prop of pattern.properties) { + if (prop.type === "RestElement") visitPattern(prop.argument, scope, fnInfo) + else { + if (prop.computed) visitExpression(prop.key as Expression, scope, fnInfo) + visitPattern(prop.value, scope, fnInfo) + } + } + break + case "MemberExpression": + visitExpression(pattern, scope, fnInfo) + break + } + } + + const visitExpression = (expr: Expression | AnyNode, scope: Scope, fnInfo: FunctionInfo): void => { + const visit = (child: Expression | AnyNode) => visitExpression(child, scope, fnInfo) + switch (expr.type) { + case "Identifier": + unresolved.push({ node: expr, name: expr.name, scope, fnIndex: fnInfo.index }) + break + case "ThisExpression": + unresolved.push({ node: expr, name: "this", scope, fnIndex: fnInfo.index }) + break + case "FunctionExpression": + case "ArrowFunctionExpression": + visitFunction(expr, scope, fnInfo.index) + break + case "BinaryExpression": + case "LogicalExpression": + visit(expr.left) + visit(expr.right) + break + case "UnaryExpression": + case "UpdateExpression": + case "AwaitExpression": + case "SpreadElement": + visit(expr.argument) + break + case "YieldExpression": + if (expr.argument) visit(expr.argument) + break + case "AssignmentExpression": + visitPattern(expr.left, scope, fnInfo) + visit(expr.right) + break + case "SequenceExpression": + case "TemplateLiteral": + for (const e of expr.expressions) visit(e) + break + case "ConditionalExpression": + visit(expr.test) + visit(expr.consequent) + visit(expr.alternate) + break + case "CallExpression": + case "NewExpression": + if (expr.callee.type !== "Super") visit(expr.callee) + for (const arg of expr.arguments) visit(arg) + break + case "MemberExpression": + if (expr.object.type !== "Super") visit(expr.object) + if (expr.computed) visit(expr.property) + break + case "ChainExpression": + visit(expr.expression) + break + case "ArrayExpression": + for (const el of expr.elements) if (el) visit(el) + break + case "ObjectExpression": + for (const prop of expr.properties) { + if (prop.type === "SpreadElement") visit(prop.argument) + else { + if (prop.computed) visit(prop.key) + visit(prop.value) + } + } + break + case "TaggedTemplateExpression": + visit(expr.tag) + for (const e of expr.quasi.expressions) visit(e) + break + case "ImportExpression": + visit(expr.source) + if (expr.options) visit(expr.options) + break + } + } + + visitFunction(source.program, undefined, -1) + + const ensureUpvalue = (fnIndex: number, decl: Decl): number => { + const fnInfo = functions[fnIndex]! + const existing = fnInfo.upvalueMap.get(decl) + if (existing !== undefined) return existing + const upvalIndex = fnInfo.upvalues.length + if (fnInfo.parentIndex === decl.fnIndex) fnInfo.upvalues.push({ from: 0, index: -1, decl }) + else fnInfo.upvalues.push({ from: 1, index: ensureUpvalue(fnInfo.parentIndex, decl), decl }) + fnInfo.upvalueMap.set(decl, upvalIndex) + return upvalIndex + } + + const lookupDecl = (scope: Scope | undefined, name: string): Decl | undefined => + scope === undefined ? undefined : (scope.decls.get(name) ?? lookupDecl(scope.parent, name)) + + const refs = new Map() + const thisRefs = new Map() + // Identifiers resolve first, then `this`, so upvalues keep their order. + const resolve = (item: (typeof unresolved)[number]): ResolvedRef => { + const decl = lookupDecl(item.scope, item.name) + if (decl === undefined) return { kind: "global", name: item.name } + decl.used = true + const owner = functions[decl.fnIndex]! + if (decl.kind === "arguments") owner.flags |= CodeFlags.USES_ARGUMENTS | CodeFlags.NEEDS_ARGS + if (decl.kind === "this") owner.flags |= CodeFlags.USES_THIS + if (decl.fnIndex === item.fnIndex) return { kind: "local", decl } + decl.captured = true + return { kind: "upval", upvalIndex: ensureUpvalue(item.fnIndex, decl), decl } + } + for (const item of unresolved) + if (item.node.type !== "ThisExpression") refs.set(item.node as Identifier, resolve(item)) + for (const item of unresolved) if (item.node.type === "ThisExpression") thisRefs.set(item.node, resolve(item)) + + // Assign dense local slots per function. A call copies its arguments into the leading slots, so with simple + // parameters each position gets a slot; an earlier duplicate name (`function f(a, a)`) gets a dummy one. + for (const fnInfo of functions) { + const assignSlot = (decl: Decl) => { + if (decl.slot >= 0) return + decl.slot = fnInfo.localNames.length + fnInfo.localNames.push(decl.name) + } + if ((fnInfo.flags & CodeFlags.SIMPLE_PARAMS) !== 0) { + fnInfo.params.forEach((p, i) => { + const name = (p as Identifier).name + const last = fnInfo.params.findLastIndex((q) => (q as Identifier).name === name) + if (last === i) assignSlot(fnInfo.paramScope.decls.get(name)!) + else fnInfo.localNames.push(name) + }) + } else fnInfo.flags |= CodeFlags.NEEDS_ARGS + for (const decl of fnInfo.allDecls) { + if ((decl.kind === "this" || decl.kind === "arguments") && !decl.used) continue + assignSlot(decl) + } + fnInfo.locals = fnInfo.localNames.length + } + + // Patch `from === 0` upvalue indices now that parent local slots are assigned + for (const fnInfo of functions) for (const u of fnInfo.upvalues) if (u.from === 0) u.index = u.decl.slot + + return { functions, nodeToFunction, scopeOfNode, refs, thisRefs } +} + +const collectPatternNames = (pattern: Pattern, out: Array = []): Array => { + switch (pattern.type) { + case "Identifier": + out.push(pattern.name) + break + case "AssignmentPattern": + collectPatternNames(pattern.left, out) + break + case "RestElement": + collectPatternNames(pattern.argument, out) + break + case "ArrayPattern": + for (const element of pattern.elements) if (element !== null) collectPatternNames(element, out) + break + case "ObjectPattern": + for (const prop of pattern.properties) { + collectPatternNames(prop.type === "RestElement" ? prop.argument : prop.value, out) + } + break + } + return out +} diff --git a/packages/codemode/src/vm/data.ts b/packages/codemode/src/vm/data.ts new file mode 100644 index 00000000000..c211a884846 --- /dev/null +++ b/packages/codemode/src/vm/data.ts @@ -0,0 +1,20 @@ +export * as Data from "./data.js" + +import type { Json } from "../data.js" +import type { Interpreter } from "./runtime/interpreter.js" +import { Arr, define, Obj, type Value } from "./runtime/objects.js" + +export type { Json } + +/** Host JSON as program values: objects and arrays are copied, primitives pass through. */ +export const fromJson = (ctx: Interpreter, value: Json | undefined): Value => { + if (value === null || typeof value !== "object") return value + if (Array.isArray(value)) + return new Arr( + ctx.builtins.Array, + value.map((item) => fromJson(ctx, item)), + ) + const copied = new Obj(ctx.builtins.Object) + for (const [key, item] of Object.entries(value)) define(copied, key, fromJson(ctx, item)) + return copied +} diff --git a/packages/codemode/src/vm/generators.ts b/packages/codemode/src/vm/generators.ts new file mode 100644 index 00000000000..cfb49e430f7 --- /dev/null +++ b/packages/codemode/src/vm/generators.ts @@ -0,0 +1,487 @@ +import { type AstNode, type GeneratorRequestKind, Throw, typeError } from "./runtime/model.js" +import { + type Cursor, + type CustomIterator, + GeneratorObj, + get, + iteratorResult, + Obj, + Opaque, + Position, + type PromiseObj, + type Value, +} from "./runtime/objects.js" +import { readProperty, requireIteratorMethod, requireIteratorObject } from "./runtime/operations.js" +import { capability } from "./runtime/promises.js" +import { abrupt, type Completion, type GeneratorRequest, Interruption, normal, Resume } from "./runtime/signals.js" +import type { Reaction, Resolving } from "./scheduler.js" +import { internalCode, VmIntrinsic } from "./internal-prelude.js" +import { CodeFlags, Op } from "./opcodes.js" +import { CustomIter, type Frame, Mode, type Vm } from "./vm.js" + +/** + * CreateAsyncFromSyncIterator's object: an async iterator over a synchronous one, for `for await` and an async + * generator's `yield*`. Its methods are the `%AsyncFromSyncIterator%` intrinsics, so it is plain data. + */ +export class AsyncFromSyncIterator extends Opaque { + override readonly tag = "AsyncFromSyncIterator" + constructor( + proto: Obj, + /** The synchronous iterator it drives. */ + readonly sync: Cursor, + /** Where it was made, so a failure that rejects one of its promises is located there. */ + readonly site: AstNode, + ) { + super(proto) + } + override get describe() { + return "an iterator" + } +} + +const result = (vm: Vm, value: Value, done: boolean) => iteratorResult(vm.ctx.builtins.Object, value, done) + +// A request reaches a resumed body as a number on its stack (see `RESUME`): its index here. +const REQUEST_KINDS: ReadonlyArray = ["next", "throw", "return"] + +const NO_THROW_METHOD = "The delegated iterator does not provide a throw() method." + +// Hands a resumed body its request: the value, then the kind's number. +const pushRequest = (frame: Frame, request: GeneratorRequest) => { + frame.regs[frame.sp++] = request.value + frame.regs[frame.sp++] = request.kind === "next" ? 0 : request.kind === "throw" ? 1 : 2 +} + +// Takes the generator's body off the job stack, to continue after the instruction at `pc`. +const suspend = (vm: Vm, generator: GeneratorObj, frame: Frame, pc: number, sp: number) => { + generator.frame = frame + frame.pc = pc + 1 + frame.sp = sp + vm.frames.pop() + if (frame.counted) vm.depth-- +} + +// A synchronous generator's step answers the caller now on the stack: a `for...of` waiting at `ITER_STEP` takes the +// value or leaves the loop; any other caller receives an iterator result. +const answerCallerWide = ( + caller: Frame, + code: Int32Array, + pc: number, + prevW: number, + w: number, + value: Value, + done: boolean, +) => { + if (done) { + caller.pc = ((prevW >>> 16) << 16) | (w >>> 16) + return + } + const prev2W = pc > 1 ? code[pc - 2]! : 0 + const slot = + (prev2W & 0xff) === Op.EXTRA + ? ((prev2W >>> 16) << 16) | (prevW & 0xff00) | ((w >>> 8) & 0xff) + : (prevW & 0xff00) | ((w >>> 8) & 0xff) + ;(caller.regs[slot] as CustomIter).done = false + caller.regs[caller.sp++] = value + caller.pc = pc + ((code[pc + 1]! & 0xff) === Op.EXTRA ? 3 : 2) +} + +const answerCaller = (vm: Vm, value: Value, done: boolean) => { + const caller = vm.frames[vm.frames.length - 1]! + const pc = caller.pc + const code = caller.code.code + const w = code[pc]! + if ((w & 0xff) !== Op.ITER_STEP) { + caller.regs[caller.sp++] = result(vm, value, done) + caller.pc = pc + 1 + return + } + const prevW = pc > 0 ? code[pc - 1]! : 0 + if ((prevW & 0xff) === Op.EXTRA) return answerCallerWide(caller, code, pc, prevW, w, value, done) + if (done) { + caller.pc = w >>> 16 + return + } + ;(caller.regs[(w >>> 8) & 0xff] as CustomIter).done = false + caller.regs[caller.sp++] = value + caller.pc = pc + 2 +} + +/** Generator.prototype.next/return/throw: the body runs on the caller's job stack until it yields, returns, or throws. */ +export const generatorStep = ( + vm: Vm, + generator: GeneratorObj, + kind: GeneratorRequestKind, + value: Value, +): Value | Resume => { + if (generator.state === "executing") { + throw typeError( + generator.proto === vm.ctx.builtins.IteratorHelper + ? "Iterator helper is already running." + : "Generator is already running.", + ) + } + if (generator.state === "suspendedStart" && kind !== "next") generator.state = "completed" + if (generator.state === "completed") { + if (kind === "throw") throw new Throw(value) + return result(vm, kind === "return" ? value : undefined, true) + } + generator.state = "executing" + return new Resume(generator, { kind, value }) +} + +/** + * AsyncGenerator.prototype.next/return/throw: queue the request with its promise, and run the body now if it is + * waiting for one. A request that arrives while the body runs is answered when the body reaches it. + */ +export const generatorRequest = ( + vm: Vm, + thisValue: Value, + kind: GeneratorRequestKind, + value: Value, +): Value | Resume => { + const resolving = vm.scheduler.resolving(vm.scheduler.newPromise()) + const promise = resolving.promise + if (!(thisValue instanceof GeneratorObj) || !thisValue.asynchronous) { + resolving.reject(typeError(`AsyncGenerator.prototype.${kind} called on incompatible receiver.`)) + return promise + } + const generator = thisValue + const state = generator.state + if (kind === "next" && state === "completed") { + resolving.resolve(result(vm, undefined, true)) + return promise + } + if (kind === "throw") { + if (state === "suspendedStart") generator.state = "completed" + if (generator.state === "completed") { + resolving.reject(new Throw(value)) + return promise + } + } + const queued = { kind, value } + generator.queue.push({ request: queued, resolving }) + if (kind === "return" && (state === "suspendedStart" || state === "completed")) { + generator.state = "awaiting-return" + awaitReturn(vm, generator) + return promise + } + if (state !== "suspendedStart" && state !== "suspendedYield") return promise + generator.state = "executing" + return new Resume(generator, queued) +} + +// AsyncGeneratorCompleteStep: answer the oldest request. +const completeStep = (vm: Vm, generator: GeneratorObj, completion: Completion, done: boolean) => { + const next = generator.queue.shift()! + if (completion.ok) next.resolving.resolve(result(vm, completion.value as Value, done)) + else next.resolving.reject(completion.error) +} + +// AsyncGeneratorDrainQueue: once the body has ended, answer the remaining requests in order. +const drainQueue = (vm: Vm, generator: GeneratorObj) => { + generator.state = "draining" + while (true) { + const next = generator.queue[0] + if (next === undefined) { + generator.state = "completed" + return + } + if (next.request.kind === "return") { + generator.state = "awaiting-return" + return awaitReturn(vm, generator) + } + completeStep( + vm, + generator, + next.request.kind === "throw" ? abrupt(new Throw(next.request.value)) : normal(undefined), + true, + ) + } +} + +// AsyncGeneratorAwaitReturn: a `return(value)` on an ended or unstarted generator settles once `value` does. +const awaitReturn = (vm: Vm, generator: GeneratorObj) => { + try { + vm.scheduler.then(vm.scheduler.promiseResolve(generator.queue[0]!.request.value), { + kind: "awaitReturn", + generator, + }) + } catch (error) { + finishAwaitReturn(vm, generator, abrupt(error)) + } +} + +/** The `awaitReturn` reaction: answers the `return` request with how its value settled, then drains the queue. */ +export const finishAwaitReturn = (vm: Vm, generator: GeneratorObj, completion: Completion) => { + completeStep(vm, generator, completion, true) + drainQueue(vm, generator) +} + +export const execGeneratorStart = (vm: Vm, frame: Frame, pc: number, sp: number): void => { + const fn = frame.fn! + const isAsync = (frame.code.flags & CodeFlags.ASYNC) !== 0 + const proto = get(fn, "prototype") + const builtins = vm.ctx.builtins + const gen = new GeneratorObj( + proto instanceof Obj ? proto : isAsync ? builtins.AsyncGenerator : builtins.Generator, + isAsync, + ) + gen.state = "suspendedStart" + frame.mode = Mode.GENERATOR + frame.target = gen + suspend(vm, gen, frame, pc, sp) + const caller = vm.frames[vm.frames.length - 1]! + caller.regs[caller.sp++] = gen + caller.pc++ +} + +export const execYield = (vm: Vm, frame: Frame, pc: number, sp: number): boolean => { + const v = frame.regs[--sp] as Value + const gen = frame.target as GeneratorObj + if (!gen.asynchronous) { + gen.state = "suspendedYield" + suspend(vm, gen, frame, pc, sp) + answerCaller(vm, v, false) + return true + } + completeStep(vm, gen, normal(v), false) + const next = gen.queue[0] + if (next !== undefined) { + frame.sp = sp + frame.pc = pc + 1 + pushRequest(frame, next.request) + return false + } + gen.state = "suspendedYield" + suspend(vm, gen, frame, pc, sp) + vm.deliverAnswer(frame) + return true +} + +export const execResume = (frame: Frame, bx: number, pc: number, sp: number): number => { + const kind = frame.regs[--sp] as number + if (kind === 0) { + frame.sp = sp + return bx + } + if (kind === 1) { + const errVal = frame.regs[--sp] as Value + frame.pc = pc + frame.sp = sp + throw new Throw(errVal) + } + frame.sp = sp + return pc + 1 +} + +export const execAwait = (vm: Vm, frame: Frame, a: number, pc: number, sp: number): void => { + const v = frame.regs[--sp] as Value + frame.pc = pc + frame.sp = sp + const promise = vm.scheduler.promiseResolve(v) + const site = a === 1 ? undefined : frame.code.siteAt(pc) + frame.pc = pc + 1 + let bIdx = vm.frames.length - 1 + while (bIdx > 0 && vm.frames[bIdx]!.mode === Mode.CALLER) { + bIdx-- + } + const cut = vm.frames.splice(bIdx) + for (const f of cut) { + if (f.counted) vm.depth-- + } + const root = cut[0]! + const result: Value = + root.mode === Mode.ASYNC || root.mode === Mode.REJECT_ONLY + ? (root.target as Resolving).promise + : root.mode === Mode.GENERATOR + ? (root.target as GeneratorObj).queue[0]?.resolving.promise + : undefined + vm.deliverAnswer(root) + vm.scheduler.park(cut, promise, site, result) +} + +export const execYstar = (frame: Frame, itSlot: number, bx: number, pc: number, sp: number): number => { + const kind = frame.regs[sp - 1] as number + const recv = frame.regs[sp - 2] as Value + const rec = frame.regs[itSlot] as CustomIter + const site = frame.code.siteAt(pc) ?? frame.origin + if (kind === 0) { + frame.regs[sp - 2] = rec.iterator + frame.regs[sp - 1] = rec.next + frame.regs[sp] = recv + frame.sp = sp + 1 + return bx + } + const methodName = REQUEST_KINDS[kind]! + const method = readProperty(rec.iterator, methodName, site) + if (method === undefined || method === null) { + frame.sp = sp - 1 + return pc + 1 + } + const callable = requireIteratorMethod(method, `Iterator ${methodName}`, site) + frame.regs[sp - 2] = rec.iterator + frame.regs[sp - 1] = callable + frame.regs[sp] = recv + frame.sp = sp + 1 + return bx +} + +export const execYstarResult = (frame: Frame, kindSlot: number, bx: number, pc: number, sp: number): number => { + const res = frame.regs[sp - 1] as Value + const kind = frame.regs[kindSlot] as number + const site = frame.code.siteAt(pc) ?? frame.origin + const obj = requireIteratorObject(res, `Iterator ${REQUEST_KINDS[kind]}() result`, site) + const done = Boolean(readProperty(obj, "done", site)) + const value = readProperty(obj, "value", site) + frame.regs[sp - 1] = value + return done ? bx : pc + 1 +} + +/** Puts a suspended generator's body back on the job stack, handing it `request` unless it has not started yet. */ +export const resumeVmGenerator = (vm: Vm, gen: GeneratorObj, request: GeneratorRequest): void => { + const f = gen.frame! + gen.frame = undefined + if (gen.asynchronous) f.answer = gen.queue[0]!.resolving.promise + if ((f.code.code[f.pc - 1]! & 0xff) !== Op.GENERATOR_START) pushRequest(f, request) + if (f.counted) vm.depth++ + vm.frames.push(f) +} + +export const completeGeneratorReturn = (vm: Vm, frame: Frame, v: Value): void => { + const gen = frame.target as GeneratorObj + vm.frames.pop() + gen.frame = undefined + if (!gen.asynchronous) { + gen.state = "completed" + vm.release(frame) + answerCaller(vm, v, true) + return + } + vm.deliverAnswer(frame) + vm.release(frame) + completeStep(vm, gen, normal(v), true) + drainQueue(vm, gen) +} + +export const unwindGenerator = (vm: Vm, frame: Frame, error: unknown): boolean => { + const gen = frame.target as GeneratorObj + gen.frame = undefined + if (!gen.asynchronous) { + gen.state = "completed" + vm.release(frame) + return false + } + vm.deliverAnswer(frame) + vm.release(frame) + completeStep(vm, gen, abrupt(error), true) + drainQueue(vm, gen) + return true +} + +const settleAdapterStep = ( + vm: Vm, + adapter: AsyncFromSyncIterator, + value: Value, + done: boolean, + closeOnRejection: boolean, + made: Resolving, +): void => { + const reaction: Reaction = { + kind: "asyncFromSync", + made, + done, + close: closeOnRejection ? adapter.sync : undefined, + site: adapter.site, + } + let wrapper: PromiseObj + try { + wrapper = vm.scheduler.promiseResolve(value) + } catch (error) { + if (!done && closeOnRejection && adapter.sync !== undefined && !(error instanceof Interruption)) { + vm.scheduler.enqueue({ kind: "reaction", reaction, settled: abrupt(error) }) + } else { + made.reject(error) + } + return + } + vm.scheduler.then(wrapper, reaction) +} + +export const handleAdapterPush = (vm: Vm, callerFrame: Frame, slots: Array): number => { + const adapter = slots[0] as AsyncFromSyncIterator + const kind = slots[1] as GeneratorRequestKind + const args = slots[2] as Array + const site = adapter.site + const made = capability(vm.ctx, site) + const sync = adapter.sync + if (sync instanceof Position) { + if (kind === "next") { + const step = sync.step() + settleAdapterStep(vm, adapter, step.value, step.done, true, made) + } else if (kind === "return") made.resolve(result(vm, args[0], true)) + else made.reject(typeError(NO_THROW_METHOD, site)) + callerFrame.regs[callerFrame.sp++] = made.promise + return 0 + } + vm.pushInternal( + internalCode("$vmAsyncFromSync"), + [adapter, kind, args.length > 0, args[0]], + site, + Mode.REJECT_ONLY, + made, + made.promise, + ) + return 1 +} + +export const registerGeneratorIntrinsics = (map: Map, vm: Vm): void => { + const ctx = vm.ctx + const builtins = ctx.builtins + const add = (name: string, fn: (args: Array) => Value) => { + map.set(name, new VmIntrinsic(name, fn)) + } + // The `$vmAsyncFromSync` frame running now settles this. + const made = () => vm.frames[vm.frames.length - 1]!.target as Resolving + + add("$WrapAsyncFromSync", ([syncVal]) => { + const site = vm.currentSite() + const obj = requireIteratorObject(syncVal, "Iterator method result", site) + const next = requireIteratorMethod(readProperty(obj, "next", site), "Iterator next", site) + return new AsyncFromSyncIterator( + builtins.AsyncFromSyncIterator, + { iterator: obj, next, asynchronous: false }, + site!, + ) + }) + + add("$AfsGetIter", ([adapter]) => ((adapter as AsyncFromSyncIterator).sync as CustomIterator).iterator) + + add("$AfsGetNext", ([adapter]) => ((adapter as AsyncFromSyncIterator).sync as CustomIterator).next) + + add("$AfsGetMethod", ([adapter, iterator, kind]) => { + const site = (adapter as AsyncFromSyncIterator).site + const method = readProperty(iterator as Obj, kind as string, site) + if (method === undefined || method === null) return undefined + return requireIteratorMethod(method, `Iterator ${kind as string}`, site) + }) + + add("$AfsCheckObj", ([adapter, res, kind]) => + requireIteratorObject(res, `Iterator ${kind as string}() result`, (adapter as AsyncFromSyncIterator).site), + ) + + add("$AfsResolveDone", ([value]) => { + made().resolve(result(vm, value, true)) + return undefined + }) + + add("$AfsRejectNoThrow", ([adapter]) => { + made().reject(typeError(NO_THROW_METHOD, (adapter as AsyncFromSyncIterator).site)) + return undefined + }) + + add("$AfsSettle", ([adapter, value, done, close]) => { + settleAdapterStep(vm, adapter as AsyncFromSyncIterator, value, Boolean(done), Boolean(close), made()) + return undefined + }) +} diff --git a/packages/codemode/src/vm/host.ts b/packages/codemode/src/vm/host.ts new file mode 100644 index 00000000000..a821612732b --- /dev/null +++ b/packages/codemode/src/vm/host.ts @@ -0,0 +1,313 @@ +import type { Diagnostic } from "../codemode.js" +import { fromJson, type Json } from "./data.js" +import { startExtensionCall } from "./runtime/extensions.js" +import type { Interpreter } from "./runtime/interpreter.js" +import { type AstNode, Lost, lostError } from "./runtime/model.js" +import { Arr, PromiseObj, type Value } from "./runtime/objects.js" +import { promiseLike } from "./runtime/promises.js" +import type { Completion } from "./runtime/signals.js" +import { type ExtensionRequest, HostCall, type HostRequest, Resolving } from "./scheduler.js" +import type { ExtensionInvocation } from "../tool-runtime.js" +import type { Code } from "./code.js" +import { internalCode, tryFastBoundaryOrJson, VmIntrinsic } from "./internal-prelude.js" +import { type FrameMode, Mode, type Vm } from "./vm.js" + +// The VM's side of host work (plan §4.10): tool calls, `search`, and extension calls, the host calls they queue, and +// the blocking call a job may wait on. + +/** What host calls need from the promise scheduler. */ +export type Promises = { + newPromise(): PromiseObj + resolving(promise: PromiseObj, site?: AstNode): Resolving + /** Unhandled rejections, in promise creation order. */ + diagnostics(): Array +} + +/** A native's answer asking the VM to stop the job until the blocking host call it queued completes. */ +export const BLOCK: unique symbol = Symbol("vm.block") +/** A host operation pushed an internal frame that produces its result. */ +export const ENTERED: unique symbol = Symbol("vm.entered") + +export type HostResult = Value | typeof BLOCK | typeof ENTERED + +/** What the VM gives host operations: its runtime, and a way to run an internal prelude function at the call. */ +export type HostVm = { + readonly ctx: Interpreter + /** + * Runs an internal prelude function at the call `origin`; its result is the call's. In another `mode` than `CALLER`, + * it settles `target`, and the call's result is `answer`: in `REJECT_ONLY` mode, a failure in it rejects `target`. + */ + pushInternal( + code: Code, + args: ReadonlyArray, + origin: AstNode | undefined, + mode?: FrameMode, + target?: unknown, + answer?: Value, + ): unknown +} + +type Delivery = { readonly call: HostCall; readonly completion: Completion } + +/** Visited nodes a native fast path may spend before it hands the work to the prelude walk, which slices. */ +export const VISIT_BUDGET = 2048 + +/** + * The host calls of one run: queued (written ahead, for the driver to start at the next stop), running, completed and + * waiting in the inbox, and the blocking call the current job waits on. Ids count up in program order. + */ +export class HostCalls { + queued: Array = [] + readonly running = new Map() + readonly inbox: Array = [] + nextID = 0 + /** The blocking call the job on the stack waits on. */ + blocked: HostCall | undefined = undefined + /** A completed blocking call's outcome, which continues the job on the stack before anything else runs. */ + input: { readonly call: HostCall; readonly completion: Completion } | undefined = undefined + /** The synchronous extension call the program is in the middle of: see `Engine.activeExtension`. */ + extension: { readonly path: string } | undefined = undefined + extensionResume: ((completion: Completion) => HostResult) | undefined = undefined + + constructor( + private readonly ctx: Interpreter, + readonly promises: Promises, + ) {} + + /** Queues concurrent host work; `resolving` settles its promise in a later job, in completion order. */ + host( + request: HostRequest, + call: { + readonly label: string + readonly result: "json" | "value" + readonly resolving: Resolving + readonly site: AstNode | undefined + }, + ): HostCall { + return this.queue(request, call.label, false, call.result, call.resolving, call.site) + } + + /** Queues blocking `search` work: the job stops until it completes, then continues with its result. */ + blockSearch(args: Array): typeof BLOCK { + return this.block({ kind: "search", args }, "search", undefined) + } + + /** Queues blocking extension work: the job stops until it completes, then continues with `resume`. */ + blockExtension(call: ExtensionRequest, resume: (completion: Completion) => HostResult): typeof BLOCK { + return this.block({ kind: "extension", call }, "extension", resume) + } + + private block( + request: HostRequest, + label: string, + resume: ((completion: Completion) => HostResult) | undefined, + ): typeof BLOCK { + this.blocked = this.queue(request, label, true, "value", undefined, undefined) + this.extensionResume = resume + return BLOCK + } + + private queue( + request: HostRequest, + label: string, + blocking: boolean, + result: "json" | "value", + resolving: Resolving | undefined, + site: AstNode | undefined, + ): HostCall { + const id = this.nextID++ + const call = new HostCall(id, request, label, blocking, result, resolving, site) + this.queued.push(call) + return call + } + + /** Continues the job stopped at a blocking call with its outcome. */ + resume(input: { readonly call: HostCall; readonly completion: Completion }): HostResult { + const resume = this.extensionResume + if (resume !== undefined) { + this.extensionResume = undefined + return resume(input.completion) + } + if (!input.completion.ok) throw input.completion.error + return promiseLike(this.ctx, fromJson(this.ctx, input.completion.value as Json | undefined)) + } + + /** Hands the queued calls to the driver: they are running from now on. */ + handOver(): Array { + const calls = this.queued + this.queued = [] + for (const call of calls) this.running.set(call.id, call) + return calls + } + + complete(id: number, completion: Completion): void { + const call = this.running.get(id) + if (call === undefined) return + this.running.delete(id) + // Its outcome is all that is left: the host function, its arguments, and what it returned are dropped. + const request = call.request + if (request.kind === "extension") { + request.call.invocation = undefined + request.call.host = undefined + request.call.args = [] + } + if (call.blocking) { + call.status = "completed" + if (this.blocked !== call) return + this.blocked = undefined + this.input = { call, completion } + return + } + this.inbox.push({ call, completion }) + } + + /** Settles the oldest completed concurrent call's promise; false when the inbox is empty. */ + deliver(): boolean { + const delivery = this.inbox.shift() + if (delivery === undefined) return false + const call = delivery.call + const completion = delivery.completion + call.status = "completed" + const resolving = call.resolving! + if (!completion.ok) resolving.reject(completion.error) + else { + resolving.resolve( + call.result === "json" ? fromJson(this.ctx, completion.value as Json | undefined) : (completion.value as Value), + ) + } + return true + } +} + +// --- Tool calls --- + +/** + * A tool call returns its promise at once. Plain-data arguments cross on the native fast path; anything else + * (`toJSON`, a promise, a deep or shared value) crosses through `$vmCross` at the call, which queues the call with the + * same `Resolving` or rejects it. Either way the call is queued once its arguments have crossed, and the driver starts + * it before any other job runs. + */ +export const callTool = ( + vm: HostVm, + hosts: HostCalls, + path: ReadonlyArray, + args: Array, + site: AstNode | undefined, +): HostResult => { + const resolving = hosts.promises.resolving(hosts.promises.newPromise()) + const json = crossFast(vm.ctx, args) + if (json === undefined) { + // A failure while crossing rejects the promise, which the caller receives either way. + vm.pushInternal( + internalCode("$vmCross"), + [arrayOf(vm, args), arrayOf(vm, [...path]), resolving], + site, + Mode.REJECT_ONLY, + resolving, + resolving.promise, + ) + return ENTERED + } + if (json instanceof Failure) { + resolving.reject(json.error) + return resolving.promise + } + hosts.host({ kind: "tool", path, args: json }, { label: path.join("."), result: "json", resolving, site }) + return resolving.promise +} + +/** `search(...)`: blocking; its result can be used directly, or awaited and chained like a Promise. */ +export const callSearch = (vm: HostVm, hosts: HostCalls, args: Array, site: AstNode | undefined): HostResult => { + const json = crossFast(vm.ctx, args) + if (json === undefined) { + vm.pushInternal(internalCode("$vmCross"), [arrayOf(vm, args), "search"], site) + return ENTERED + } + if (json instanceof Failure) throw json.error + return hosts.blockSearch(json) +} + +class Failure { + constructor(readonly error: unknown) {} +} + +// Every argument as host JSON on the budgeted fast path; undefined when one needs the prelude walk. +const crossFast = (ctx: Interpreter, args: Array): Array | Failure | undefined => { + const json: Array = [] + try { + for (const arg of args) { + const crossed = tryFastBoundaryOrJson(ctx.builtins, arg, true, VISIT_BUDGET) + if (!crossed.ok) return undefined + json.push(crossed.json) + } + } catch (error) { + return new Failure(error) + } + return json +} + +const arrayOf = (vm: HostVm, values: Array) => new Arr(vm.ctx.builtins.Array, [...values]) + +// --- Extension calls --- + +/** + * A synchronous extension function's call, staged here so its host values never enter a frame: the `extension.before` + * hook (blocking), then the host function; what it returns settles now (blocking) unless it is an Effect or a Promise, + * which the program holds a promise for. The call is active, holding live host values, until its result is a program + * value or its concurrent call is queued. + */ +export const callExtension = ( + ctx: Interpreter, + hosts: HostCalls, + slots: ReadonlyArray, + site: AstNode | undefined, +): HostResult => { + const label = slots[2] as string + const call = { + host: slots[0], + args: slots[1] as ReadonlyArray, + label, + invocation: slots[3] as ExtensionInvocation | undefined, + } + if (call.host instanceof Lost) throw lostError(`The function ${label}`) + hosts.extension = { path: label } + const run = (): HostResult => { + const started = startExtensionCall(ctx, call.host as Function, call.args, call, site) + if (started instanceof PromiseObj) { + hosts.extension = undefined + return started + } + return hosts.blockExtension(started, (completion) => { + hosts.extension = undefined + if (!completion.ok) throw completion.error + return completion.value as Value + }) + } + if (call.invocation === undefined || ctx.tools.hooks["extension.before"] === undefined) return run() + const before: ExtensionRequest = { stage: "before", ...call, host: undefined, threw: false } + return hosts.blockExtension(before, (completion) => { + if (!completion.ok) { + hosts.extension = undefined + throw completion.error + } + return run() + }) +} + +/** The intrinsics the boundary walk calls to queue a tool or search host call. */ +export const registerHostIntrinsics = (map: Map, vm: Vm): void => { + const json = (value: Value) => (value as Arr).items as Array + map.set( + "$HostTool", + new VmIntrinsic("$HostTool", ([path, args, resolving]) => { + const keys = (path as Arr).items as Array + const site = vm.frames[vm.frames.length - 1]?.origin + vm.hosts.host( + { kind: "tool", path: keys, args: json(args) }, + { label: keys.join("."), result: "json", resolving: resolving as unknown as Resolving, site }, + ) + return undefined + }), + ) + map.set("$HostSearch", new VmIntrinsic("$HostSearch", ([args]) => vm.hosts.blockSearch(json(args)) as never)) +} diff --git a/packages/codemode/src/vm/index.ts b/packages/codemode/src/vm/index.ts new file mode 100644 index 00000000000..63878aa4af0 --- /dev/null +++ b/packages/codemode/src/vm/index.ts @@ -0,0 +1,4 @@ +export * as VmEngine from "./index.js" + +export { execute } from "./runtime/execute.js" +export { globalNames } from "./runtime/globals.js" diff --git a/packages/codemode/src/vm/internal-prelude.ts b/packages/codemode/src/vm/internal-prelude.ts new file mode 100644 index 00000000000..305fc669d95 --- /dev/null +++ b/packages/codemode/src/vm/internal-prelude.ts @@ -0,0 +1,534 @@ +import { fromJson, type Json } from "./data.js" +import { conversions } from "./runtime/callback.js" +import type { Builtins } from "./runtime/intrinsics.js" +import { checkStringLength, MAX_VALUE_DEPTH } from "./runtime/limits.js" +import { invalidData, rangeError, typeError } from "./runtime/model.js" +import { + Arr, + Builtin, + Bytes, + Callable, + coerceToNumber, + coerceToString, + DateObj, + ErrorObj, + get, + HeadersObj, + keys, + type NativeResult, + Obj, + PromiseObj, + SetObj, + URLSearchParamsObj, + type Value, +} from "./runtime/objects.js" +import { applyBinaryOperator, operatorConversion, propertyKey } from "./runtime/operations.js" +import { isOpaque, typeofValue } from "./runtime/references.js" +import { Convert } from "./runtime/signals.js" +import type { Code } from "./code.js" +import { builtinCodes } from "./prelude/bytecode.js" +import { UnaryOp } from "./opcodes.js" +import { type Vm, VmInternalFn } from "./vm.js" + +/** + * Lightweight internal callable for VM-only `$` intrinsics. Does not extend `Obj` or `Native`, + * so it never touches `builtins.Function`, `nativeRule.sealed`, or static intrinsic numbering. + */ +export class VmIntrinsic { + constructor( + readonly name: string, + readonly call: (args: Array) => NativeResult, + ) {} +} + +export const INTERNAL_SOURCE = String.raw` +function $vmToPrimitive(v, hint) { + if (!$IsDataObj(v)) return v + const first = hint === "string" || (hint === "default" && $IsDate(v)) ? "toString" : "valueOf" + const second = first === "toString" ? "valueOf" : "toString" + const m1 = $GetMethod(v, first) + if (m1 !== undefined) { + const r1 = $Call(m1, v) + if (!$IsObj(r1)) return r1 + } + const m2 = $GetMethod(v, second) + if (m2 !== undefined) { + const r2 = $Call(m2, v) + if (!$IsObj(r2)) return r2 + } + $Throw("TypeError", "Cannot convert object to primitive value.") +} + +function $vmToKey(v) { + return $PropKey($vmToPrimitive(v, "string")) +} + +function $vmToString(v) { + return $CoerceToString($vmToPrimitive(v, "string")) +} + +function $vmToNumeric(incFlag, v) { + return $ApplyToNumeric(incFlag, $vmToPrimitive(v, "number")) +} + +function $vmInc(incFlag, v) { + const n = $ApplyToNumeric(incFlag, $vmToPrimitive(v, "number")) + return incFlag === 0 ? n + 1 : n - 1 +} + +function $vmUnary(sub, v) { + return $ApplyUnary(sub, $vmToPrimitive(v, "number")) +} + +function $vmBinary(op, lhs, rhs) { + const kind = $OpConvKind(op, lhs, rhs) + if (kind === 0) return $ApplyBinary(op, lhs, rhs) + if (kind === 1) { + const key = $vmToPrimitive(lhs, "string") + return $ApplyBinary(op, key, rhs) + } + const hint = kind === 2 ? "default" : "number" + const l = $vmToPrimitive(lhs, hint) + const r = $vmToPrimitive(rhs, hint) + return $ApplyBinary(op, l, r) +} + +function $vmGetElem(o, k) { + return o[$vmToKey(k)] +} + +function $vmDeleteElem(o, k) { + return delete o[$vmToKey(k)] +} + +function $vmConvert(callable, target, args, constructing, replay, req) { + while (true) { + const values = $ConvValues(req) + const coerce = $ConvFlag(req, 0) + const ordinal = $ConvFlag(req, 1) + const len = values.length + const converted = [] + let i = 0 + while (i < len) { + const val = values[i] + const hint = $ConvHint(req, i) + const prim = $IsObj(val) ? $vmToPrimitive(val, hint) : val + converted[i] = coerce ? $ConvCoerce(hint, prim) : prim + i++ + } + replay[ordinal] = converted + const res = $ConvReplay(callable, target, args, constructing, replay) + if (!$IsConvert(res)) return res + req = res + } +} + +// A JSON walk's step for one value, holder[key]: its JSON as program data, or undefined when it is left out. A promise +// is yielded at the host boundary, which resumes the walk with its value. +function $vmJsonVisitor(boundary, replacer, properties) { + const ancestors = [] + function* visit(holder, key, depth) { + if (depth > 100) $Throw("RangeError", "Value exceeds the maximum depth of 100.") + let val = holder[key] + if ($IsPromise(val)) { + if (!boundary) { + $Throw("InvalidData", "JSON.stringify received an un-awaited Promise; await it first - e.g. \x60const result = await tools.ns.tool(...)\x60.") + } + val = yield val + } + if ($IsObj(val)) { + const toJSON = val.toJSON + if ($IsCallableAny(toJSON)) val = $Call(toJSON, val, key) + } + if (replacer !== undefined) val = $Call(replacer, holder, key, val) + const kind = $JsonClassify(val, boundary, ancestors) + if (kind === 0) return undefined + if (kind === 1) return val + if (kind === 2) return null + if (kind === 3) return $JsonSpecial(val, boundary) + const target = kind === 4 ? $SetToArray(val) : val + ancestors[ancestors.length] = target + let out + if ($IsArray(target)) { + out = [] + const len = target.length + let i = 0 + while (i < len) { + const elem = yield* visit(target, String(i), depth + 1) + out[i] = elem === undefined ? null : elem + i++ + } + } else { + out = $NewNullProtoObj() + const names = $JsonNames(target, boundary, properties) + let i = 0 + while (i < names.length) { + const k = names[i] + const propVal = yield* visit(target, k, depth + 1) + if (propVal !== undefined) $Define(out, k, propVal, true) + i++ + } + } + $ArrayPop(ancestors) + return out + } + return visit +} + +function $vmJsonWalk(replacer, value, properties, indent) { + const json = $vmJsonVisitor(false, replacer, properties)({ "": value }, "", 0).next().value + return $JsonFinish(false, json, properties, indent) +} + +function $vmSettleCall(callable, thisVal, args) { + return $Apply(callable, thisVal, args) +} + +function $vmCombine(name, items) { + const c = $CombineStart(name, items) + for (const item of items) { + $CombineAdd(c, item) + } + $CombineEnd(c) +} + +function $vmOpenSyncForAsync(v, method) { + return $WrapAsyncFromSync($Call(method, v)) +} + +function $vmAsyncFromSync(adapter, kind, hasArg, val) { + const iter = $AfsGetIter(adapter) + const method = kind === "next" ? $AfsGetNext(adapter) : $AfsGetMethod(adapter, iter, kind) + if (method === undefined || method === null) { + if (kind === "return") { + $AfsResolveDone(val) + return + } + const ret = $AfsGetMethod(adapter, iter, "return") + if (ret !== undefined && ret !== null) { + $AfsCheckObj(adapter, $Call(ret, iter), "return") + } + $AfsRejectNoThrow(adapter) + return + } + const res = hasArg ? $Call(method, iter, val) : $Call(method, iter) + $AfsCheckObj(adapter, res, kind) + const done = !!res.done + const value = res.value + $AfsSettle(adapter, value, done, kind !== "return") +} + +function $vmAsyncFromSyncClose(iter) { + const ret = iter.return + if (ret !== undefined && ret !== null) $Call(ret, iter) +} + +async function $vmCross(roots, path, resolving) { + const crossed = [] + let r = 0 + while (r < roots.length) { + let root = roots[r] + if (path === undefined && $IsThenable(root)) root = await root + const walk = $vmJsonVisitor(true, undefined, null)({ "": root }, "", 0) + let step = walk.next() + while (!step.done) step = walk.next(await step.value) + crossed[r] = $JsonFinish(true, step.value, null, undefined) + r++ + } + if (path === undefined) return crossed[0] + if (path === "search") return $HostSearch(crossed) + $HostTool(path, crossed, resolving) +} +` + +let compiledInternal: ReturnType | undefined + +/** The VM's internal helpers' bytecode, and their functions' code by name. */ +export const getInternalPrelude = () => (compiledInternal ??= builtinCodes("@vm-internal", INTERNAL_SOURCE)) + +/** The code of the internal helper `name`. */ +export const internalCode = (name: string): Code => getInternalPrelude().byName.get(name)! + +/** + * Adds the intrinsics available to `CodeFlags.INTERNAL` functions (both `@prelude` and `@vm-internal`) to `map`: the + * internal helpers, and the `$` natives. Uses `VmIntrinsic` rather than `Native` so it never touches + * `builtins.Function` or `nativeRule.sealed`. + */ +export const registerVmIntrinsics = (map: Map, vm: Vm): void => { + const ctx = vm.ctx + const builtins = ctx.builtins + const currentSite = () => vm.currentSite() + for (const [name, code] of getInternalPrelude().byName) map.set(name, new VmInternalFn(code)) + + const add = (name: string, fn: (args: Array) => NativeResult) => { + map.set(name, new VmIntrinsic(name, fn)) + } + + add("$IsObj", ([v]) => v instanceof Obj) + add("$IsDataObj", ([v]) => v instanceof Obj && !isOpaque(v)) + add("$IsDate", ([v]) => v instanceof DateObj) + add("$IsPromise", ([v]) => v instanceof PromiseObj) + add("$IsArray", ([v]) => v instanceof Arr) + add("$IsCallableAny", ([v]) => v instanceof Callable) + add("$GetMethod", ([v, name]) => { + const m = get(v as Obj, name as string) + return m instanceof Callable ? m : undefined + }) + add("$PropKey", ([v]) => propertyKey(v) as Value) + add("$CoerceToString", ([v]) => coerceToString(v)) + add("$ApplyToNumeric", ([incFlag, v]) => { + if (isOpaque(v)) { + const op = incFlag === 0 ? "++" : "--" + throw invalidData(`'${op}' requires a data value.`, currentSite()) + } + return coerceToNumber(v) + }) + add("$ApplyUnary", ([sub, v]) => { + if (isOpaque(v)) throw invalidData("Unary operators require data values.", currentSite()) + const n = coerceToNumber(v) + return sub === UnaryOp.NEG ? -n : sub === UnaryOp.PLUS ? +n : ~n + }) + add("$OpConvKind", ([op, lhs, rhs]) => { + const conv = operatorConversion(op as string, lhs, rhs) + if (conv === undefined) return 0 + if (conv.kind === "key") return 1 + return conv.hint === "default" ? 2 : 3 + }) + add("$ApplyBinary", ([op, lhs, rhs]) => applyBinaryOperator(op as string, lhs, rhs, currentSite()!)) + + // Convert helpers + add("$ConvValues", ([req]) => new Arr(builtins.Array, [...(req as unknown as Convert).values])) + add("$ConvFlag", ([req, which]) => { + const c = req as unknown as Convert + return which === 0 ? c.coerce : c.ordinal + }) + add("$ConvHint", ([req, idx]) => { + const hints = (req as unknown as Convert).hints + return typeof hints === "string" ? hints : hints[idx as number]! + }) + add("$ConvCoerce", ([hint, val]) => (hint === "string" ? coerceToString(val) : coerceToNumber(val))) + add("$IsConvert", ([res]) => res instanceof Convert) + add("$ConvReplay", ([callable, target, args, constructing, replay]) => { + const rep = (replay as Arr).items.map((item) => (item instanceof Arr ? item.items : undefined)) + conversions.count = 0 + conversions.replay = rep + try { + const fnObj = callable as Builtin + const argList = (args as Arr).items + return constructing + ? fnObj.construct!(argList, target as Builtin, currentSite()) + : fnObj.invoke(target, argList, currentSite()) + } finally { + conversions.replay = undefined + } + }) + + // JSON walk helpers + add("$JsonClassify", ([val, boundary, ancestors]) => { + if (val === undefined || typeofValue(val) === "function") return 0 + if (typeof val === "number") return Number.isFinite(val) ? 1 : 2 + if (val === null || typeof val === "string" || typeof val === "boolean") return 1 + if (!(val instanceof Obj)) return 3 + if (boundary && val instanceof Bytes) { + throw invalidData( + "A Uint8Array cannot cross to the host; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.", + ) + } + if ((boundary && val instanceof URLSearchParamsObj) || val instanceof HeadersObj) return 3 + if ((ancestors as Arr).items.includes(val)) throw typeError("Converting circular structure to JSON.") + if (boundary && val instanceof SetObj) return 4 + return 5 + }) + add("$JsonSpecial", ([val, boundary]) => { + if (!(val instanceof Obj)) return new Obj(builtins.Object) + if (boundary && val instanceof URLSearchParamsObj) return val.params.toString() + if (val instanceof HeadersObj) return fromJson(ctx, Object.fromEntries(val.headers)) + return new Obj(builtins.Object) + }) + add("$SetToArray", ([val]) => new Arr(builtins.Array, [...(val as SetObj).set.liveKeys()])) + add("$NewNullProtoObj", () => new Obj(null)) + // The keys a JSON walk writes, in order: an error's name and message come first, then its other own keys. + add("$JsonNames", ([target, boundary, properties]) => { + const error = target instanceof ErrorObj + const propFilter = properties instanceof Arr ? new Set(properties.items as Array) : undefined + const list = keys(target as Obj).filter( + (k) => + !(error && (k === "name" || k === "message")) && + !(boundary && k === "__proto__") && + (propFilter === undefined || propFilter.has(k)), + ) + return new Arr(builtins.Array, error ? ["name", "message", ...list] : list) + }) + add("$ArrayPop", ([arr]) => (arr as Arr).items.pop()) + add("$JsonFinish", ([boundary, root, properties, indent]) => { + const plain = toPlainJson(root) + if (boundary) return plain as Value + const props = properties instanceof Arr ? (properties.items as Array) : null + const text = JSON.stringify(plain, props, indent as string | number | undefined) + if (text !== undefined) checkStringLength(text.length) + return text + }) +} + +const toPlainJson = (val: Value): Json | undefined => { + if (val === undefined) return undefined + if (val === null || typeof val === "string" || typeof val === "number" || typeof val === "boolean") return val + if (val instanceof Arr) return val.items.map((item) => toPlainJson(item) ?? null) + if (val instanceof Obj) { + const out: Record = {} + for (const [k, slot] of val.props) { + if (typeof k !== "string" || !slot.enumerable || !("value" in slot)) continue + const v = toPlainJson(slot.value) + if (v !== undefined) { + if (k === "__proto__") { + Object.defineProperty(out, k, { value: v, enumerable: true, writable: true, configurable: true }) + } else { + out[k] = v + } + } + } + return out + } + return {} +} + +/** + * Budgeted native fast path for copying plain data at the host boundary (`boundary`) or for `JSON.stringify`. Returns + * `{ ok: true, json }` if the tree has no custom `toJSON`, promises, or shared references revisited more than `budget` + * times; otherwise the prelude walk (`$vmJsonWalk`) does the work, ticking fuel. + * + * Both give the text `JSON.stringify` would: `toJSON` is honored, functions and `undefined` vanish, non-finite numbers + * become null, and everything else is copied. Two departures from JS so a mistake is not a silent `{}`: an Error + * serializes as `{ name, message, ...own }`, and a promise throws. At the host boundary a promise is awaited, a Set + * crosses as an array, a URLSearchParams as its query string, a Uint8Array asks to be encoded as text first, and a + * `__proto__` key is dropped so host code can never receive one. + */ +export const tryFastBoundaryOrJson = ( + builtins: Builtins, + value: Value, + boundary: boolean, + revisitBudget = 2048, +): { readonly ok: true; readonly json: Json | undefined } | { readonly ok: false } => { + if (value === undefined) return { ok: true, json: undefined } + if (value === null || typeof value === "string" || typeof value === "boolean") return { ok: true, json: value } + if (typeof value === "number") return { ok: true, json: Number.isFinite(value) ? value : null } + if ( + value instanceof Obj && + value.constructor === Obj && + value.proto === builtins.Object && + value.proto.intrinsic > 0 && + value.props.size <= 4 && + !value.props.has("toJSON") + ) { + const flatOut: Record = {} + let flatOk = true + for (const [k, slot] of value.props) { + if (typeof k !== "string" || !slot.enumerable) continue + if (k === "__proto__") { + if (!boundary) flatOk = false + continue + } + if (!("value" in slot)) { + flatOk = false + break + } + const pv = slot.value + if (pv === undefined) continue + if (pv === null || typeof pv === "string" || typeof pv === "boolean") { + flatOut[k] = pv + } else if (typeof pv === "number") { + flatOut[k] = Number.isFinite(pv) ? pv : null + } else { + flatOk = false + break + } + } + if (flatOk) return { ok: true, json: flatOut } + } + let remainingRevisits = revisitBudget + const ancestors = new Set() + const seen = new Set() + const objectProto = builtins.Object + let dateProtoToJSON: Value | undefined + let urlProtoToJSON: Value | undefined + + const visit = ( + cur: Value, + depth: number, + ): { readonly ok: true; readonly json: Json | undefined } | { readonly ok: false } => { + if (depth > MAX_VALUE_DEPTH) throw rangeError(`Value exceeds the maximum depth of ${MAX_VALUE_DEPTH}.`) + if (cur === undefined || typeofValue(cur) === "function") return { ok: true, json: undefined } + if (typeof cur === "number") return { ok: true, json: Number.isFinite(cur) ? cur : null } + if (cur === null || typeof cur === "string" || typeof cur === "boolean") return { ok: true, json: cur } + if (cur instanceof PromiseObj) return { ok: false } + if (!(cur instanceof Obj)) return { ok: true, json: {} } + if (seen.has(cur)) { + if (--remainingRevisits <= 0) return { ok: false } + } else { + seen.add(cur) + } + const plain = cur.constructor === Obj + const toJSON = + plain && cur.proto === objectProto && objectProto.intrinsic > 0 && !cur.props.has("toJSON") + ? undefined + : get(cur, "toJSON") + if (toJSON instanceof Callable) { + if (cur instanceof DateObj && toJSON === (dateProtoToJSON ??= get(builtins.Date, "toJSON"))) { + return { ok: true, json: Number.isFinite(cur.time) ? new Date(cur.time).toISOString() : null } + } + if (cur.tag === "URL" && toJSON === (urlProtoToJSON ??= get(builtins.URL, "toJSON"))) { + return { ok: true, json: cur.toString() } + } + return { ok: false } + } + if (boundary && cur instanceof Bytes) { + throw invalidData( + "A Uint8Array cannot cross to the host; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.", + ) + } + if (boundary && cur instanceof URLSearchParamsObj) return { ok: true, json: cur.params.toString() } + if (cur instanceof HeadersObj) return { ok: true, json: Object.fromEntries(cur.headers) } + if (ancestors.has(cur)) throw typeError("Converting circular structure to JSON.") + ancestors.add(cur) + try { + if (cur instanceof Arr || (boundary && cur instanceof SetObj)) { + const items = cur instanceof Arr ? cur.items : [...cur.set.liveKeys()] + const out: Array = new Array(items.length) + for (let i = 0; i < items.length; i++) { + const r = visit(items[i], depth + 1) + if (!r.ok) return r + out[i] = r.json ?? null + } + return { ok: true, json: out } + } + const out: Record = {} + if (cur instanceof ErrorObj) { + for (const k of ["name", "message"]) { + const r = visit(get(cur, k), depth + 1) + if (!r.ok) return r + if (r.json !== undefined) out[k] = r.json + } + } + // A plain object's own data is its property table; a wrapper (bytes, say) presents its own keys. + for (const [k, slot] of plain ? cur.props : keys(cur).map((key) => [key, undefined] as const)) { + if (typeof k !== "string" || (slot !== undefined && !slot.enumerable)) continue + if (boundary && k === "__proto__") continue + const propVal = slot === undefined ? get(cur, k) : "value" in slot ? slot.value : slot.get?.(cur) + const r = visit(propVal, depth + 1) + if (!r.ok) return r + if (r.json !== undefined) { + if (k === "__proto__") { + Object.defineProperty(out, k, { value: r.json, enumerable: true, writable: true, configurable: true }) + } else { + out[k] = r.json + } + } + } + return { ok: true, json: out } + } finally { + ancestors.delete(cur) + } + } + + return visit(value, 0) +} diff --git a/packages/codemode/src/vm/opcodes.ts b/packages/codemode/src/vm/opcodes.ts new file mode 100644 index 00000000000..0f84a541fe9 --- /dev/null +++ b/packages/codemode/src/vm/opcodes.ts @@ -0,0 +1,288 @@ +import type { DiagnosticKind } from "../codemode.js" +import type { ErrorType } from "./runtime/intrinsics.js" + +/** Uninitialized lexical binding sentinel (temporal dead zone). */ +export const HOLE: unique symbol = Symbol("codemode.hole") + +/** Mutable box for a binding captured by a closure. */ +export class Cell { + constructor(public v: unknown) {} +} + +const initCell = new Cell(0) +initCell.v = 1.5 +initCell.v = "" +initCell.v = true +initCell.v = null +initCell.v = undefined +initCell.v = HOLE + +const makeEmptyCells = (): ReadonlyArray => { + const arr: Array = [] + arr.push(initCell) + arr.length = 0 + return arr as Array +} + +export const EMPTY_CELLS: ReadonlyArray = makeEmptyCells() + +/** Raised when the VM compiler or runtime encounters a feature scheduled for a later phase. */ +export class NotYetImplemented extends Error { + constructor(readonly feature: string) { + super(`Not yet implemented in VM: ${feature}`) + this.name = "NotYetImplemented" + } +} + +/** + * Bytecode opcodes (1 word per instruction: `op:8 | a:8 | bx:16`, widened by `EXTRA` prefixes for `a > 0xff` or `bx > 0xffff`). + */ +export const Op = { + // Stack and constants (10) + PUSH_UNDEF: 0, + PUSH_NULL: 1, + PUSH_BOOL: 2, + PUSH_INT: 3, + PUSH_CONST: 4, + POP: 5, + POPN: 6, + DUP: 7, + PICK: 8, + ROT: 9, + + // Variables (15) + GET_LOCAL: 10, + PUT_LOCAL: 11, + HOLE_LOCAL: 12, + NEW_CELL: 13, + COPY_CELL: 14, + GET_CELL: 15, + PUT_CELL: 16, + GET_UPVAL: 17, + PUT_UPVAL: 18, + GET_GLOBAL: 19, + GET_INTRINSIC: 20, + ARGS: 21, + CLOSURE: 22, + SET_NAME: 23, + IMPORT: 24, + + // Properties (10) + GET_PROP: 25, + GET_ELEM: 26, + PUT_PROP: 27, + PUT_ELEM: 28, + DELETE_PROP: 29, + DELETE_ELEM: 30, + CHECK_BASE: 31, + TO_KEY: 32, + IN: 33, + INSTANCEOF: 34, + + // Literals (8) + NEW_OBJECT: 35, + NEW_ARRAY: 36, + ARRAY_APPEND: 37, + DEFINE_FIELD: 38, + COPY_DATA_PROPS: 39, + COPY_REST: 40, + TEMPLATE: 41, + REGEXP: 42, + + // Operators (16) + ADD: 43, + SUB: 44, + MUL: 45, + LT: 46, + LE: 47, + GT: 48, + GE: 49, + SEQ: 50, + SNE: 51, + BINOP: 52, + UNARY: 53, + NOT: 54, + TO_NUMERIC: 55, + INC: 56, + TO_STRING: 57, + CONCAT: 58, + + // Control (11) + JUMP: 59, + LOOP: 60, + JUMP_IF_FALSE: 61, + JUMP_IF_TRUE: 62, + JUMP_COND: 63, + THROW: 64, + THROW_ERROR: 65, + END_FINALLY: 66, + RETURN: 67, + NOP: 68, + EXTRA: 69, + + // Calls (4) + CALL: 70, + CALL_SPREAD: 71, + NEW: 72, + NEW_SPREAD: 73, + + // Iteration (7) + ITER_OPEN: 74, + ITER_INIT: 75, + ITER_STEP: 76, + ITER_RESULT: 77, + ITER_CLOSE: 78, + ITER_APPEND_ALL: 79, + FOR_IN: 80, + + // Generators and async (6) + GENERATOR_START: 81, + YIELD: 82, + RESUME: 83, + AWAIT: 84, + YSTAR: 85, + YSTAR_RESULT: 86, + + // Program (1) + END_PROGRAM: 87, +} as const + +export type OpCode = (typeof Op)[keyof typeof Op] + +export const OpNames: ReadonlyArray = Object.entries(Op).reduce>((acc, [name, code]) => { + acc[code] = name + return acc +}, []) + +/** Sub-ops for `Op.BINOP` (`a` operand). */ +export const BinOp = { + DIV: 0, + MOD: 1, + EXP: 2, + BAND: 3, + BOR: 4, + BXOR: 5, + SHL: 6, + SAR: 7, + SHR: 8, + EQ: 9, + NE: 10, +} as const + +export const BinOpSymbols: ReadonlyArray = ["/", "%", "**", "&", "|", "^", "<<", ">>", ">>>", "==", "!="] + +/** Sub-ops for `Op.UNARY` (`a` operand). */ +export const UnaryOp = { + NEG: 0, + PLUS: 1, + BNOT: 2, + TYPEOF: 3, + VOID: 4, +} as const + +export const UnaryOpSymbols: ReadonlyArray = ["-", "+", "~", "typeof", "void"] + +/** Sub-ops for `Op.JUMP_COND` (`a` operand). */ +export const CondKind = { + /** If top is truthy, keep it and jump; else pop and fall through (`||`, `||=`). */ + TRUTHY_KEEP: 0, + /** If top is falsy, keep it and jump; else pop and fall through (`&&`, `&&=`). */ + FALSY_KEEP: 1, + /** If top is not null/undefined, keep it and jump; else pop and fall through (`??`, `??=`). */ + NOT_NULLISH_KEEP: 2, + /** If top !== undefined, keep it and jump; else pop and fall through (defaults). */ + DEFINED: 3, + /** If top == null, replace top with undefined and jump; else keep top and fall through (`?.`). */ + NULLISH: 4, + /** For `[thisValue, callable]`: if callable == null, pop callable, set top to undefined, and jump (`o.m?.()`). */ + NULLISH_METHOD: 5, +} as const + +/** Sub-ops for `Op.PUT_LOCAL`, `Op.PUT_CELL`, `Op.PUT_UPVAL` (`a` operand). */ +export const PutKind = { + /** Normal assignment: checks TDZ (`HOLE`). */ + ASSIGN: 0, + /** Declaration initialization or internal slot write: skips TDZ check. */ + INIT: 1, + /** Assignment to a `const` or read-only binding: checks TDZ first, then throws `TypeError`. */ + CONST: 2, +} as const + +/** Sub-ops for `Op.CHECK_BASE` (`a` operand). */ +export const CheckBaseKind = { + /** `o[k]`: checks `o` is not null/undefined before converting `k` (`[o, k] -> [o, k]`). */ + READ: 0, + /** `o.x = ...`: checks `o.x` is a writable reference (`[o] -> [o]`, `bx = k`). */ + WRITE_PROP: 1, + /** `o[k] = ...`: checks `o[k]` is a writable reference after `TO_KEY` (`[o, key] -> [o, key]`). */ + WRITE_ELEM: 2, + /** Destructuring target check: throws if top is null or undefined (`[v] -> [v]`). */ + DESTRUCTURE: 3, +} as const + +/** Sub-ops for `Op.ITER_OPEN` and `Op.ITER_INIT` (`a` operand). */ +export const IterContext = { + FOR_OF: 0, + DESTRUCTURE: 1, + SPREAD: 2, + DELEGATE: 3, + FOR_AWAIT: 4, + ASYNC_DELEGATE: 5, + CHECK_OBJECT: 6, +} as const + +/** Sub-ops for `Op.IMPORT` (`a` operand). */ +export const ImportKind = { + DEFAULT: 0, + NAMESPACE: 1, + NAMED: 2, + SIDE_EFFECT: 3, + DYNAMIC: 4, +} as const + +/** Bit flags on `Code.flags`. */ +export const CodeFlags = { + ARROW: 1 << 0, + ASYNC: 1 << 1, + GENERATOR: 1 << 2, + SIMPLE_PARAMS: 1 << 3, + USES_THIS: 1 << 4, + USES_ARGUMENTS: 1 << 5, + INTERNAL: 1 << 6, + PROGRAM: 1 << 7, + NEEDS_ARGS: 1 << 9, +} as const + +/** Constant pool descriptors. */ +export type TemplateSite = { + readonly cooked: ReadonlyArray + readonly raw: ReadonlyArray + readonly nodeIndex: number +} + +export type RegExpSite = { + readonly pattern: string + readonly flags: string +} + +export type ErrorSite = { + readonly type: ErrorType + readonly message: string + readonly kind?: DiagnosticKind + readonly suggestions?: ReadonlyArray +} + +export type ImportSite = { + readonly specifier: string + readonly imported?: string +} + +export type ConstValue = string | number | TemplateSite | RegExpSite | ErrorSite | ImportSite + +/** Packs one instruction word: `op:8 | a:8 | bx:16`. Throws if any field does not fit its width. */ +export const packWord = (op: OpCode, a = 0, bx = 0): number => { + if ((op & 0xff) !== op || (a & 0xff) !== a || (bx & 0xffff) !== bx) { + throw new RangeError(`Instruction operand out of single-word range: op=${op} a=${a} bx=${bx}`) + } + return op | (a << 8) | (bx << 16) +} diff --git a/packages/codemode/src/vm/prelude/bytecode.json b/packages/codemode/src/vm/prelude/bytecode.json new file mode 100644 index 00000000000..086ea5d6e5c --- /dev/null +++ b/packages/codemode/src/vm/prelude/bytecode.json @@ -0,0 +1 @@ +{"@prelude":{"text":"f135511dc7fc0839","codes":[{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":1,"name":"arrayFromIterable","flags":72,"length":3,"params":3,"locals":8,"maxStack":6,"code":[36,196875,3,262411,327692,10,2293834,2293819,393483,3212876,2490427,327692,327947,196618,262154,31,32,543,65546,0,50,1572925,327690,1900603,131082,65546,327690,262154,582,28,5,262154,56,262155,589884,70,75,524347,70,3212877,720955,459019,2950734,70,5,458762,64,5,2949179,196618,67],"consts":[],"captures":[],"handlers":[11,35,41,0,42,45,47,0],"finallies":[],"finallyOffsets":[],"sites":[0,10,1,8,2,14,3,12,5,19,6,15,12,16,13,24,14,25,15,23,16,25,17,23,18,28,19,29,20,27,21,26,22,30,23,26,24,34,25,33,26,35,27,36,28,31,29,23,30,22,31,39,32,38,33,39,34,15,49,41,50,40],"localNames":["items","mapper","thisArg","result","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":2,"name":"arrayFromLike","flags":72,"length":4,"params":4,"locals":6,"maxStack":6,"code":[20,65352,65546,328,262411,3,327947,327690,65546,46,1900605,262154,327690,31,32,543,196618,131082,10,327690,26,327690,582,28,5,327690,56,327691,458812,262154,67],"consts":["Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,53,1,52,2,54,3,52,4,50,5,58,6,56,7,61,8,62,9,60,10,59,11,67,12,68,13,66,14,68,15,66,16,72,17,71,18,74,19,75,20,73,21,76,22,69,23,66,24,65,25,79,26,78,27,79,28,59,29,81,30,80],"localNames":["source","length","mapper","thisArg","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":3,"name":"Array$prototype$map","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["mapItems","$Receiver","Array","Array.prototype.map","Array.map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,82,2,84,4,85,6,87,7,85,8,90,9,91,10,92,11,93,12,94,13,95,14,96,15,92,16,97,17,98,18,99,19,90,20,89],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":4,"name":"Uint8Array$prototype$map","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.map","$Bytes","mapItems","Uint8Array.map","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,100,2,102,4,103,6,105,7,103,8,110,9,111,10,112,11,113,12,114,13,110,14,108,15,116,16,117,17,118,18,119,19,120,20,121,21,122,22,123,23,118,24,125,25,124,26,127,27,116,28,115],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":5,"name":"mapItems","flags":72,"length":4,"params":4,"locals":7,"maxStack":7,"code":[0,20,65546,196618,582,5,10,65561,262411,131092,65352,262154,328,327947,3,393483,393226,262154,46,2818109,393226,10,33,2555965,327690,393226,31,32,543,131082,65546,10,393226,26,393226,10,838,28,5,393226,56,393227,1048636,327690,67],"consts":["$Callback","length","Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,136,1,137,2,138,3,139,4,136,6,144,7,143,8,141,9,150,10,149,11,151,12,149,13,147,14,155,15,153,16,158,17,159,18,157,19,156,20,163,21,164,22,162,23,161,24,168,25,169,26,167,27,169,28,167,29,173,30,172,31,175,32,176,33,174,34,177,35,178,36,170,37,167,38,166,39,181,40,180,41,181,42,156,43,183,44,182],"localNames":["array","callback","thisArg","label","length","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":6,"name":"Array$prototype$filter","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["filterItems","$Receiver","Array","Array.prototype.filter","Array.filter"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,184,2,186,4,187,6,189,7,187,8,192,9,193,10,194,11,195,12,196,13,197,14,198,15,194,16,199,17,200,18,201,19,192,20,191],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":7,"name":"Uint8Array$prototype$filter","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.filter","$Bytes","filterItems","Uint8Array.filter","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,202,2,204,4,205,6,207,7,205,8,212,9,213,10,214,11,215,12,216,13,212,14,210,15,218,16,219,17,220,18,221,19,222,20,223,21,224,22,225,23,220,24,227,25,226,26,229,27,218,28,217],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":8,"name":"filterItems","flags":72,"length":4,"params":4,"locals":9,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,36,327947,3,393483,3,459019,0,524555,458762,262154,46,3407933,458762,10,33,3145789,10,458762,26,524299,131082,65546,524298,458762,10,838,3145789,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,1114172,327690,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,238,1,239,2,240,3,241,4,238,6,246,7,245,8,243,9,251,10,249,11,255,12,253,13,259,14,257,15,261,17,265,18,266,19,264,20,263,21,270,22,271,23,269,24,268,25,277,26,278,27,276,28,275,29,283,30,282,31,284,32,285,33,286,34,280,35,279,36,290,37,292,39,291,41,292,42,289,43,291,44,289,45,293,46,289,47,288,48,296,49,295,50,296,51,263,52,298,53,297],"localNames":["array","callback","thisArg","label","length","result","count","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":9,"name":"Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Array","Array.prototype.forEach","Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,299,2,301,4,302,6,304,7,302,8,307,9,308,10,309,11,310,12,311,13,312,14,313,15,309,16,314,17,315,18,316,19,307,21,305],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":10,"name":"Uint8Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Uint8Array","Uint8Array.prototype.forEach","Uint8Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,317,2,319,4,320,6,322,7,320,8,325,9,326,10,327,11,328,12,329,13,330,14,331,15,327,16,332,17,333,18,334,19,325,21,323],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":11,"name":"forEachItem","flags":72,"length":4,"params":4,"locals":6,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,3,327947,327690,262154,46,2097213,327690,10,33,1835069,131082,65546,10,327690,26,327690,10,838,5,327690,56,327691,720956,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,343,1,344,2,345,3,346,4,343,6,351,7,350,8,348,9,356,10,354,11,359,12,360,13,358,14,357,15,364,16,365,17,363,18,362,19,370,20,369,21,372,22,373,23,371,24,374,25,375,26,367,28,378,29,377,30,378,31,357,32,341],"localNames":["array","callback","thisArg","label","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":12,"name":"Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.some","Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,379,2,381,4,382,6,384,7,382,8,387,9,388,10,389,11,390,12,391,13,392,14,393,15,389,16,394,17,395,18,396,19,397,20,387,21,386],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":13,"name":"Uint8Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.some","Uint8Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,398,2,400,4,401,6,403,7,401,8,406,9,407,10,408,11,409,12,410,13,411,14,412,15,408,16,413,17,414,18,415,19,416,20,406,21,405],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":14,"name":"Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.every","Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,417,2,419,4,420,6,422,7,420,8,425,9,426,10,427,11,428,12,429,13,430,14,431,15,427,16,432,17,433,18,434,19,435,20,425,21,424],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":15,"name":"Uint8Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.every","Uint8Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,436,2,438,4,439,6,441,7,439,8,444,9,445,10,446,11,447,12,448,13,449,14,450,15,446,16,451,17,452,18,453,19,454,20,444,21,443],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":16,"name":"testItems","flags":72,"length":5,"params":5,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,327947,3,393483,393226,327690,46,2490429,393226,10,33,1966399,131082,65546,10,393226,26,393226,10,838,54,262154,50,2228285,262154,54,67,393226,56,393227,720956,262154,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,464,1,465,2,466,3,467,4,464,6,472,7,471,8,469,9,477,10,475,11,480,12,481,13,479,14,478,15,486,16,487,17,485,18,484,19,493,20,492,21,495,22,496,23,494,24,497,25,498,26,490,27,489,28,499,29,488,30,483,31,502,32,501,33,500,34,505,35,504,36,505,37,478,38,507,39,506],"localNames":["array","callback","thisArg","label","every","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":17,"name":"Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.find","Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,508,2,510,4,511,6,513,7,511,8,516,9,517,10,518,11,519,12,520,13,521,14,522,15,518,16,523,17,524,18,525,19,526,20,527,21,516,22,515],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":18,"name":"Uint8Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.find","Uint8Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,528,2,530,4,531,6,533,7,531,8,536,9,537,10,538,11,539,12,540,13,541,14,542,15,538,16,543,17,544,18,545,19,546,20,547,21,536,22,535],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":19,"name":"Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findIndex","Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,548,2,550,4,551,6,553,7,551,8,556,9,557,10,558,11,559,12,560,13,561,14,562,15,558,16,563,17,564,18,565,19,566,20,567,21,556,22,555],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":20,"name":"Uint8Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findIndex","Uint8Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,568,2,570,4,571,6,573,7,571,8,576,9,577,10,578,11,579,12,580,13,581,14,582,15,578,16,583,17,584,18,585,19,586,20,587,21,576,22,575],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":21,"name":"Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLast","Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,588,2,590,4,591,6,593,7,591,8,596,9,597,10,598,11,599,12,600,13,601,14,602,15,598,16,603,17,604,18,605,19,606,20,607,21,596,22,595],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":22,"name":"Uint8Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLast","Uint8Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,608,2,610,4,611,6,613,7,611,8,616,9,617,10,618,11,619,12,620,13,621,14,622,15,618,16,623,17,624,18,625,19,626,20,627,21,616,22,615],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":23,"name":"Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLastIndex","Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,628,2,630,4,631,6,633,7,631,8,636,9,637,10,638,11,639,12,640,13,641,14,642,15,638,16,643,17,644,18,645,19,646,20,647,21,636,22,635],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":24,"name":"Uint8Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLastIndex","Uint8Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,648,2,650,4,651,6,653,7,651,8,656,9,657,10,658,11,659,12,660,13,661,14,662,15,658,16,663,17,664,18,665,19,666,20,667,21,656,22,655],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":25,"name":"findItem","flags":72,"length":6,"params":6,"locals":10,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,393483,262154,917565,65539,53,983099,65539,459019,262154,1441853,393226,65539,44,1507387,3,524555,0,590091,524298,3,49,2163007,524298,393226,46,3670077,10,524298,26,589835,131082,65546,589834,524298,10,838,3342397,327690,3211325,524298,3276859,589834,67,524298,458762,43,524299,1703996,327690,3997757,65539,53,4063291,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,678,1,679,2,680,3,681,4,678,6,686,7,685,8,683,9,692,10,691,11,694,12,693,13,691,14,695,15,689,16,700,17,699,18,702,19,703,20,701,21,699,22,704,23,697,24,706,26,711,27,712,28,710,29,709,30,714,31,715,32,713,33,708,34,721,35,722,36,720,37,719,38,727,39,726,40,728,41,729,42,730,43,724,44,723,45,733,46,732,47,734,48,732,49,735,50,731,51,738,52,739,53,737,54,738,55,708,56,742,57,741,58,744,59,743,60,741,61,745,62,740],"localNames":["array","callback","thisArg","label","last","wantIndex","length","step","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":26,"name":"Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduce","Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,746,2,748,4,749,6,753,7,754,8,755,9,756,10,757,11,758,12,759,13,755,14,760,15,761,16,762,17,763,18,753,19,752],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":27,"name":"Uint8Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduce","Uint8Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,764,2,766,4,767,6,771,7,772,8,773,9,774,10,775,11,776,12,777,13,773,14,778,15,779,16,780,17,781,18,771,19,770],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":28,"name":"Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduceRight","Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,782,2,784,4,785,6,789,7,790,8,791,9,792,10,793,11,794,12,795,13,791,14,796,15,797,16,798,17,799,18,789,19,788],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":29,"name":"Uint8Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduceRight","Uint8Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,800,2,802,4,803,6,807,7,808,8,809,9,810,10,811,11,812,12,813,13,809,14,814,15,815,16,816,17,817,18,807,19,806],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":30,"name":"reduceItems","flags":72,"length":5,"params":5,"locals":9,"maxStack":6,"code":[0,20,65546,196618,582,5,10,65561,327947,262154,917565,65539,53,983099,65539,393483,262154,1441853,327690,65539,44,1507387,3,459019,0,524555,131082,65561,3,48,2359357,131082,3,26,524299,5111867,458762,3,49,2818367,458762,327690,46,3146047,458762,10,33,54,3539005,458762,393226,43,458763,2359356,458762,3,46,3997759,458762,327690,49,4587581,0,131092,196612,196618,262148,43,582,5,10,458762,26,524299,458762,393226,43,458763,458762,3,49,5570879,458762,327690,46,6881341,458762,10,33,6553661,0,65546,524298,10,458762,26,458762,10,1094,524299,458762,393226,43,458763,5111868,524298,67],"consts":["$Callback","length","$Throw","TypeError"," of an empty array with no initial value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,827,1,828,2,829,3,830,4,827,6,835,7,834,8,832,9,841,10,840,11,843,12,842,13,840,14,844,15,838,16,849,17,848,18,851,19,852,20,850,21,848,22,853,23,846,24,855,26,860,27,859,28,862,29,858,30,857,31,867,32,868,33,866,34,865,35,857,36,874,37,875,38,873,39,872,40,877,41,878,42,876,43,871,44,881,45,882,46,880,47,879,48,870,49,885,50,886,51,884,52,885,53,870,54,890,55,891,56,889,57,888,58,893,59,894,60,892,61,887,62,896,63,897,64,898,65,900,66,901,67,899,68,896,70,906,71,907,72,905,73,904,74,910,75,911,76,909,77,910,78,915,79,916,80,914,81,913,82,918,83,919,84,917,85,912,86,923,87,924,88,922,89,921,90,931,91,930,92,932,93,934,94,935,95,933,96,936,97,937,98,928,99,927,100,940,101,941,102,939,103,940,104,912,105,943,106,942],"localNames":["array","callback","initial","label","right","length","step","index","accumulator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":31,"name":"Array$prototype$flatMap","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,65546,262148,582,5,196618,327705,262411,36,327947,3,393483,3,459019,0,524555,0,590091,458762,262154,46,6684733,458762,196618,33,6422589,131082,65546,196618,458762,26,458762,196618,838,524299,0,393236,524298,326,5636157,3,589835,589834,524298,327705,46,5570621,589834,524298,33,5308477,327690,393226,55,7,56,393227,31,32,543,524298,589834,26,28,5,589834,56,589835,3801148,6422587,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,2228284,327690,67],"consts":["$Receiver","Array","Array.prototype.flatMap","$Callback","Array.flatMap","length","$IsArray"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,944,2,946,4,947,6,949,7,947,8,954,9,955,10,956,11,957,12,958,13,954,14,952,15,960,16,961,17,962,18,963,19,960,21,968,22,967,23,965,24,973,25,971,26,977,27,975,28,981,29,979,30,983,32,986,34,990,35,991,36,989,37,988,38,995,39,996,40,994,41,993,42,1004,43,1003,44,1006,45,1007,46,1005,47,1008,48,1009,49,1001,50,1000,51,1011,52,1012,53,1013,54,1011,55,1010,56,1018,57,1017,58,1021,59,1023,60,1022,61,1020,62,1019,63,1028,64,1029,65,1027,66,1026,67,1033,68,1035,70,1034,72,1035,73,1032,74,1034,75,1032,76,1037,77,1038,78,1036,79,1032,80,1031,81,1041,82,1040,83,1041,84,1019,85,1010,86,1045,87,1047,89,1046,91,1047,92,1044,93,1046,94,1044,95,1048,96,1044,97,1043,98,1051,99,1050,100,1051,101,988,102,1053,103,1052],"localNames":["this","callback","thisArg","array","length","result","count","index","inner","at"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":32,"name":"sortWith","flags":72,"length":7,"params":7,"locals":15,"maxStack":11,"code":[36,459019,3,524555,0,590091,0,655627,0,721163,0,786699,0,852235,0,917771,524298,393226,25,46,7733309,393226,524298,26,720907,393226,524298,65539,43,26,589835,393226,524298,131075,43,26,655371,589834,786443,3,851979,720906,589834,46,3146047,786442,655370,46,6488125,0,196618,262154,720906,26,262154,786442,26,582,309,917515,917514,3,47,4390975,917514,917514,51,5374013,458762,851978,31,32,543,262154,720906,26,28,5,720906,56,720907,6225979,458762,851978,31,32,543,262154,786442,26,28,5,786442,56,786443,851978,56,851979,2687036,0,65556,262154,458762,851978,720906,589834,786442,655370,393226,524298,26,2118,5,524298,196611,43,524299,1048636,0,131092,10,65546,131082,262154,327690,1350,67],"consts":["length","$MergeRest","$Sorted"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1067,1,1065,2,1071,3,1069,4,1073,6,1076,8,1079,10,1082,12,1085,14,1088,16,1092,17,1094,18,1093,19,1091,20,1090,21,1101,22,1102,23,1100,24,1099,25,1107,26,1109,27,1110,28,1108,29,1106,30,1105,31,1115,32,1117,33,1118,34,1116,35,1114,36,1113,37,1122,38,1121,39,1126,40,1125,41,1130,42,1131,43,1129,44,1128,45,1133,46,1134,47,1132,48,1127,49,1143,50,1142,51,1145,52,1146,53,1144,54,1148,55,1149,56,1147,57,1140,58,1139,59,1138,60,1153,61,1154,62,1152,63,1151,64,1156,65,1157,66,1155,67,1150,68,1162,69,1163,70,1161,71,1163,72,1161,73,1165,74,1166,75,1164,76,1161,77,1160,78,1169,79,1168,80,1169,81,1150,82,1174,83,1175,84,1173,85,1175,86,1173,87,1177,88,1178,89,1176,90,1173,91,1172,92,1181,93,1180,94,1181,95,1184,96,1183,97,1184,98,1127,99,1186,100,1187,101,1188,102,1189,103,1190,104,1191,105,1192,106,1193,107,1194,108,1196,109,1197,110,1195,111,1186,113,1200,114,1201,115,1199,116,1200,117,1090,118,1203,119,1204,120,1205,121,1206,122,1207,123,1208,124,1209,125,1203,126,1202],"localNames":["target","into","holes","comparator","work","undefs","merges","merged","merge","middle","high","left","right","count","order"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":33,"name":"sortDefault","flags":72,"length":5,"params":5,"locals":6,"maxStack":9,"code":[0,20,196618,326,327947,0,65556,327690,326,720957,327740,0,131092,10,65546,131082,0,196628,327690,196618,582,262154,1350,67],"consts":["$SortState","$SortStep","$Sorted","$SortResult"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1221,1,1222,2,1223,3,1221,4,1219,5,1225,6,1226,7,1227,8,1225,9,1224,11,1230,12,1231,13,1232,14,1233,15,1234,16,1235,17,1236,18,1237,19,1238,20,1235,21,1239,22,1230,23,1229],"localNames":["target","into","holes","values","undefs","state"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":34,"name":"Array$prototype$toLocaleString","flags":88,"length":0,"params":0,"locals":5,"maxStack":5,"code":[789,267,0,20,10,65540,131076,838,65803,196612,131339,3,196875,0,262411,196618,65546,262169,46,3473469,196618,3,48,1835069,131082,327684,43,131083,65546,196618,26,262155,262154,1,51,2556223,262154,0,51,3211325,131082,0,393236,262154,459033,70,326,43,131083,196618,56,196619,983100,131082,67],"consts":["$Receiver","Array","Array.prototype.toLocaleString","","length",",","String","toLocaleString"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1240,2,1246,3,1247,4,1248,5,1249,6,1250,7,1246,8,1244,9,1254,10,1252,11,1258,12,1256,13,1260,15,1264,16,1266,17,1265,18,1263,19,1262,20,1271,21,1272,22,1270,23,1269,24,1275,25,1276,26,1274,27,1275,28,1281,29,1282,30,1280,31,1279,32,1286,33,1287,34,1285,35,1284,36,1289,37,1290,38,1288,39,1283,40,1293,41,1294,42,1295,43,1298,44,1297,45,1296,46,1294,47,1292,48,1293,49,1302,50,1301,51,1302,52,1262,53,1304,54,1303],"localNames":["this","array","result","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":35,"name":"fillCollection","flags":72,"length":3,"params":3,"locals":7,"maxStack":7,"code":[131082,4,50,458815,131082,65540,50,196875,262156,65546,3801162,3801147,327947,4719948,3997755,262156,262411,196618,54,1835069,0,131092,196612,10,262154,838,5,3735611,0,262164,262154,326,54,2949181,0,327700,393220,458756,131082,43,524292,43,582,5,3735611,0,131092,196612,10,262154,3,26,262154,65539,26,1094,5,852028,70,75,786491,70,4719949,983099,393483,4457806,70,5,393226,64,5,4456507,10,67],"consts":["Map","WeakMap","$Table","add","$IsObject","$Throw","TypeError","new ","(...) expects [key, value] pairs as entry objects."],"captures":[],"handlers":[15,58,64,0,65,68,70,0],"finallies":[],"finallyOffsets":[],"sites":[0,1316,1,1317,2,1315,3,1314,4,1319,5,1320,6,1318,7,1312,9,1325,10,1321,16,1322,17,1329,18,1328,19,1327,20,1331,21,1332,22,1333,23,1334,24,1335,25,1331,27,1327,28,1338,29,1339,30,1340,31,1338,32,1337,33,1336,34,1342,35,1343,36,1344,37,1347,38,1348,39,1346,40,1349,41,1345,42,1342,44,1336,45,1351,46,1352,47,1353,48,1354,49,1356,50,1357,51,1355,52,1359,53,1360,54,1358,55,1351,57,1321,72,1362,73,1361],"localNames":["target","items","kind","pairs","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":36,"name":"Map$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1369,1,1370,2,1371,3,1372,4,1373,5,1369,6,1368],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":37,"name":"Object$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Object"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1380,1,1381,2,1382,3,1383,4,1384,5,1380,6,1379],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":38,"name":"groupBy","flags":72,"length":3,"params":3,"locals":9,"maxStack":6,"code":[10,1,50,458815,10,0,50,1048637,0,20,65540,131082,131076,43,582,5,0,196628,65546,131082,262148,43,582,5,0,327700,393220,131082,582,196875,3,262411,0,327947,393228,10,4915274,4915259,459019,5834572,5111867,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,458756,50,4325437,0,524308,0,327700,589828,327690,582,326,327691,0,327700,655364,196618,327690,393226,1094,5,2555964,70,75,2490427,70,5834573,2687035,524555,5572430,70,5,524298,64,5,5570619,196618,67],"consts":["$Throw","TypeError",".groupBy expects an iterable collection.","$Callback",".groupBy","$Table","groups","Object","String","groupKey","group"],"captures":[],"handlers":[41,75,81,0,82,85,87,0],"finallies":[],"finallyOffsets":[],"sites":[0,1394,1,1395,2,1393,3,1392,4,1397,5,1398,6,1396,7,1391,8,1400,9,1401,10,1402,11,1404,12,1405,13,1403,14,1400,16,1407,17,1408,18,1409,19,1411,20,1412,21,1410,22,1407,24,1416,25,1417,26,1418,27,1419,28,1416,29,1414,30,1423,31,1421,32,1425,35,1431,36,1427,42,1428,43,1436,44,1437,45,1438,46,1440,48,1439,50,1440,51,1436,52,1435,53,1443,54,1444,55,1442,56,1441,57,1448,58,1449,59,1450,60,1451,61,1452,62,1453,63,1450,64,1448,65,1447,66,1455,67,1456,68,1457,69,1458,70,1459,71,1460,72,1455,74,1427,89,1462,90,1461],"localNames":["items","callback","kind","groups","index","key","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":39,"name":"insertComputed","flags":72,"length":3,"params":3,"locals":4,"maxStack":6,"code":[0,131082,65546,326,196875,0,20,65540,10,65546,196618,1094,5,196618,67],"consts":["$Table","add"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1472,1,1473,2,1474,3,1472,4,1470,5,1476,6,1477,7,1478,8,1479,9,1480,10,1481,11,1476,13,1483,14,1482],"localNames":["target","key","callback","value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":40,"name":"Map$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Map","Map.prototype.forEach","Map.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1484,2,1486,4,1487,6,1489,7,1487,8,1492,9,1493,10,1494,11,1495,12,1496,13,1497,14,1498,15,1494,16,1499,17,1500,18,1501,19,1492,21,1490],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":41,"name":"Set$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Set","Set.prototype.forEach","Set.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1502,2,1504,4,1505,6,1507,7,1505,8,1510,9,1511,10,1512,11,1513,12,1514,13,1515,14,1516,15,1512,16,1517,17,1518,18,1519,19,1510,21,1508],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":42,"name":"collectionForEach","flags":72,"length":4,"params":4,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,262156,0,65556,131076,10,582,2031690,2031675,327947,2950476,2228283,262156,262411,131082,65546,262154,65539,26,262154,3,26,10,838,5,983100,70,75,917563,70,2950477,1114171,393483,2688334,70,5,393226,64,5,2687035,0,67],"consts":["$Callback","$Table","entries"],"captures":[],"handlers":[17,31,37,0,38,41,43,0],"finallies":[],"finallyOffsets":[],"sites":[0,1528,1,1529,2,1530,3,1531,4,1528,7,1536,8,1537,9,1538,10,1539,11,1536,12,1532,18,1533,19,1544,20,1543,21,1546,22,1547,23,1545,24,1549,25,1550,26,1548,27,1551,28,1541,30,1532,45,1526],"localNames":["target","callback","thisArg","label","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":43,"name":"Set$prototype$union","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.union","union"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1552,2,1557,3,1558,4,1559,5,1560,6,1561,7,1562,8,1563,9,1559,10,1564,11,1565,12,1557,13,1556],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":44,"name":"Set$prototype$intersection","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.intersection","intersection"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1566,2,1571,3,1572,4,1573,5,1574,6,1575,7,1576,8,1577,9,1573,10,1578,11,1579,12,1571,13,1570],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":45,"name":"Set$prototype$difference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.difference","difference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1580,2,1585,3,1586,4,1587,5,1588,6,1589,7,1590,8,1591,9,1587,10,1592,11,1593,12,1585,13,1584],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":46,"name":"Set$prototype$symmetricDifference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.symmetricDifference","symmetricDifference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1594,2,1599,3,1600,4,1601,5,1602,6,1603,7,1604,8,1605,9,1601,10,1606,11,1607,12,1599,13,1598],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":47,"name":"Set$prototype$isSubsetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSubsetOf","isSubsetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1608,2,1613,3,1614,4,1615,5,1616,6,1617,7,1618,8,1619,9,1615,10,1620,11,1621,12,1613,13,1612],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":48,"name":"Set$prototype$isSupersetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSupersetOf","isSupersetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1622,2,1627,3,1628,4,1629,5,1630,6,1631,7,1632,8,1633,9,1629,10,1634,11,1635,12,1627,13,1626],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":49,"name":"Set$prototype$isDisjointFrom","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isDisjointFrom","isDisjointFrom"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1636,2,1641,3,1642,4,1643,5,1644,6,1645,7,1646,8,1647,9,1643,10,1648,11,1649,12,1641,13,1640],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":50,"name":"setMethod","flags":72,"length":3,"params":3,"locals":18,"maxStack":5,"code":[0,20,65546,326,54,1048637,0,65556,131076,196612,131082,43,262148,43,582,5,65546,327705,309,196875,196618,196618,51,2228285,0,65556,131076,196612,131082,43,393220,43,582,5,196618,3,46,3145789,0,65556,458756,196612,131082,43,524292,43,582,5,65546,589849,262411,65546,655385,327947,0,720916,262154,326,54,4259903,0,720916,327690,326,54,4980797,0,65556,131076,196612,131082,43,786436,43,582,5,0,851988,327684,10,582,393483,131082,917508,50,5833023,393226,196618,48,6029373,2,67,131082,983044,50,6488383,393226,196618,46,6684733,2,67,131082,1048580,50,7209021,1114132,65352,72,8454203,131082,1179652,50,7667775,131082,1245188,50,7929919,131082,1310724,50,8388669,0,851988,1376260,10,582,8454203,0,459019,131082,917508,50,9764927,131082,1048580,50,9240639,131082,1245188,50,9502783,131082,1441796,50,9765183,393226,196618,47,524555,0,590091,524298,16056381,720908,0,851988,1507332,131082,1245188,50,10747965,458762,10813499,10,582,14614602,14614587,852235,15535436,14811195,720908,721163,65546,262154,720906,326,54,54,589835,131082,1048580,50,12714045,589834,12648509,0,851988,1572868,458762,720906,838,5,14549051,131082,1245188,50,13631549,589834,13566013,0,851988,1638404,458762,720906,838,5,14549051,589834,131082,1441796,50,50,14549053,2,983307,14421326,70,1611,5,983050,67,11075644,70,75,11010107,70,15535437,11206715,917771,15273294,70,5,917514,64,5,15269947,458762,0,50,15925309,258,15990843,458762,67,65546,327690,70,655627,0,20,655370,326,54,17367101,0,65556,131076,196612,131082,43,1703940,43,582,5,786444,0,1769492,655370,258,582,23199818,23199803,1048843,24121420,23396411,786444,786699,0,851988,589828,10,786442,838,589835,131082,1179652,50,19464253,0,851988,1572868,458762,786442,838,5,23134267,131082,1048580,50,20381757,589834,20316221,0,851988,1572868,458762,786442,838,5,23134267,131082,1245188,50,21168189,0,851988,1638404,458762,786442,838,5,23134267,131082,1310724,50,22216765,0,851988,589834,21823549,1638404,21889083,1572868,458762,786442,838,5,23134267,589834,131082,1441796,50,50,23134269,2,983307,23007310,70,1611,5,983050,67,17956924,70,75,17891387,70,24121421,18087995,1114379,23859278,70,5,1114122,64,5,23855163,458762,0,50,24510525,258,24576059,458762,67],"consts":["$IsObject","$Throw","TypeError","Set."," expects a Set-like object.","size"," received a Set-like object with an invalid size.","RangeError"," received a Set-like object with a negative size.","has","keys","$IsCallable"," expects callable 'has' and 'keys' methods.","$Table","isSubsetOf","isSupersetOf","intersection","Set","union","difference","symmetricDifference","copy","isDisjointFrom","values","add","delete"," expects 'keys' to return an iterator object.","$Direct"],"captures":[],"handlers":[171,223,229,0,230,233,235,0,276,354,360,0,361,364,366,0],"finallies":[],"finallyOffsets":[],"sites":[0,1658,1,1659,2,1660,3,1658,4,1657,5,1656,6,1662,7,1663,8,1664,9,1667,10,1668,11,1666,12,1669,13,1665,14,1662,16,1675,17,1674,18,1673,19,1671,20,1679,21,1680,22,1678,23,1677,24,1682,25,1683,26,1684,27,1687,28,1688,29,1686,30,1689,31,1685,32,1682,34,1692,35,1693,36,1691,37,1690,38,1695,39,1696,40,1697,41,1700,42,1701,43,1699,44,1702,45,1698,46,1695,48,1707,49,1706,50,1704,51,1713,52,1712,53,1710,54,1718,55,1719,56,1720,57,1718,58,1717,59,1716,60,1722,61,1723,62,1724,63,1722,64,1721,65,1715,66,1726,67,1727,68,1728,69,1731,70,1732,71,1730,72,1733,73,1729,74,1726,76,1737,77,1738,78,1739,79,1740,80,1737,81,1735,82,1744,83,1745,84,1743,85,1742,86,1747,87,1748,88,1746,89,1741,90,1750,91,1749,92,1754,93,1755,94,1753,95,1752,96,1757,97,1758,98,1756,99,1751,100,1760,101,1759,102,1766,103,1767,104,1765,105,1764,106,1769,107,1768,109,1764,110,1774,111,1775,112,1773,113,1772,114,1777,115,1778,116,1776,117,1771,118,1780,119,1781,120,1779,121,1770,122,1782,123,1783,124,1784,125,1785,126,1782,127,1770,128,1786,129,1762,130,1792,131,1793,132,1791,133,1790,134,1798,135,1799,136,1797,137,1796,138,1801,139,1802,140,1800,141,1795,142,1804,143,1805,144,1803,145,1794,146,1807,147,1808,148,1806,149,1788,150,1810,152,1813,153,1812,155,1819,156,1820,157,1821,158,1824,159,1825,160,1823,161,1822,162,1826,163,1822,164,1827,165,1819,166,1815,172,1816,173,1837,174,1836,175,1838,176,1834,177,1833,178,1832,179,1831,180,1841,181,1842,182,1840,183,1839,184,1845,185,1844,186,1847,187,1848,188,1849,189,1850,190,1851,191,1847,193,1839,194,1854,195,1855,196,1853,197,1852,198,1858,199,1857,200,1860,201,1861,202,1862,203,1863,204,1864,205,1860,207,1852,208,1867,209,1869,210,1870,211,1868,212,1866,213,1865,214,1872,215,1871,222,1815,237,1876,238,1877,239,1875,240,1874,241,1878,242,1874,243,1879,244,1873,245,1886,246,1885,247,1883,248,1881,249,1889,250,1890,251,1891,252,1889,253,1888,254,1887,255,1893,256,1894,257,1895,258,1898,259,1899,260,1897,261,1900,262,1896,263,1893,266,1905,267,1906,268,1907,269,1908,270,1905,271,1901,277,1902,278,1913,279,1914,280,1915,281,1916,282,1917,283,1913,284,1912,285,1920,286,1921,287,1919,288,1918,289,1923,290,1924,291,1925,292,1926,293,1927,294,1923,296,1918,297,1930,298,1931,299,1929,300,1928,301,1934,302,1933,303,1936,304,1937,305,1938,306,1939,307,1940,308,1936,310,1928,311,1943,312,1944,313,1942,314,1941,315,1946,316,1947,317,1948,318,1949,319,1950,320,1946,322,1941,323,1953,324,1954,325,1952,326,1951,327,1956,328,1957,329,1959,330,1958,331,1960,332,1958,333,1961,334,1962,335,1963,336,1956,338,1951,339,1966,340,1968,341,1969,342,1967,343,1965,344,1964,345,1971,346,1970,353,1901,368,1975,369,1976,370,1974,371,1973,372,1977,373,1973,374,1978,375,1972],"localNames":["target","other","name","size","has","keys","own","result","asks","found","iterator","item","item","%it","%e","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":51,"name":"Object$fromEntries","flags":72,"length":1,"params":1,"locals":5,"maxStack":7,"code":[35,65803,131084,10,2621514,2621499,196875,3539788,2818107,131084,131339,0,20,131082,258,582,54,1572925,0,65556,131076,196612,582,5,0,262164,65546,0,327700,131082,3,26,326,131082,65539,26,258,1094,5,458812,70,75,393275,70,3539789,589883,262411,3277646,70,5,262154,64,5,3276859,65546,67],"consts":["$IsObject","$Throw","TypeError","Object.fromEntries expects [key, value] entry objects.","$Define","String"],"captures":[],"handlers":[9,40,46,0,47,50,52,0],"finallies":[],"finallyOffsets":[],"sites":[0,1986,1,1984,3,1991,4,1987,10,1988,11,1995,12,1996,13,1997,14,1998,15,1995,16,1994,17,1993,18,2000,19,2001,20,2002,21,2003,22,2000,24,2005,25,2006,26,2007,27,2008,28,2009,29,2011,30,2012,31,2010,32,2008,33,2014,34,2015,35,2013,36,2016,37,2005,39,1987,54,2018,55,2017],"localNames":["entries","result","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":52,"name":"Math$sumPrecise","flags":72,"length":1,"params":1,"locals":5,"maxStack":4,"code":[36,65803,131084,10,2097226,2097211,196875,3015500,2293819,131084,131339,131082,821,4,51,1441853,0,65556,131076,196612,582,5,65546,65546,262169,31,32,543,131082,28,5,458812,70,75,393275,70,3015501,589883,262411,2753358,70,5,262154,64,5,2752571,0,327700,65546,326,67],"consts":["number","$Throw","TypeError","Math.sumPrecise expects an iterable of numbers.","length","$SumPrecise"],"captures":[],"handlers":[9,32,38,0,39,42,44,0],"finallies":[],"finallyOffsets":[],"sites":[0,2026,1,2024,3,2031,4,2027,10,2028,11,2036,12,2035,13,2037,14,2034,15,2033,16,2039,17,2040,18,2041,19,2042,20,2039,22,2046,23,2048,24,2047,25,2045,26,2047,27,2045,28,2050,29,2045,30,2044,31,2027,46,2052,47,2053,48,2054,49,2052,50,2051],"localNames":["items","numbers","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":53,"name":"aggregateError","flags":72,"length":2,"params":2,"locals":6,"maxStack":6,"code":[36,131339,196620,65546,1376330,1376315,262411,2294860,1572923,196620,196875,131082,131082,25,31,32,543,196618,28,5,458812,70,75,393275,70,2294861,589883,327947,2032718,70,5,327690,64,5,2031675,0,65556,10,131076,131082,2,1094,67],"consts":["length","$Define","errors"],"captures":[],"handlers":[9,21,27,0,28,31,33,0],"finallies":[],"finallyOffsets":[],"sites":[0,2063,1,2061,3,2068,4,2064,10,2065,11,2072,12,2074,13,2073,14,2071,15,2073,16,2071,17,2076,18,2071,19,2070,20,2064,35,2078,36,2079,37,2080,38,2081,39,2082,40,2083,41,2078,42,2077],"localNames":["error","errors","list","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":54,"name":"collectBytes","flags":72,"length":6,"params":6,"locals":11,"maxStack":7,"code":[0,20,10,326,54,1376317,0,65556,131076,65546,196612,43,0,262164,10,326,43,327684,43,582,5,36,393483,524300,10,2752586,2752571,590091,3672396,2949179,524300,524555,393226,393226,393241,31,32,543,524298,28,5,1835068,70,75,1769531,70,3672397,1966139,655627,3410254,70,5,655370,64,5,3407931,3,459019,131082,0,51,5570621,458762,393226,393241,46,5570621,393226,458762,31,32,543,196618,131082,393226,458762,26,458762,582,28,5,458762,56,458763,4063292,0,458772,393226,262154,327690,838,67],"consts":["$IsIterable","$Throw","TypeError"," expects a Uint8Array, an array, or an iterable of numbers, received ","$Describe",".","length","$Bytes"],"captures":[],"handlers":[30,42,48,0,49,52,54,0],"finallies":[],"finallyOffsets":[],"sites":[0,2095,1,2096,2,2097,3,2095,4,2094,5,2093,6,2099,7,2100,8,2101,9,2105,10,2106,11,2104,12,2107,13,2108,14,2109,15,2107,16,2103,17,2110,18,2102,19,2099,21,2114,22,2112,24,2119,25,2115,31,2116,32,2123,33,2125,34,2124,35,2122,36,2124,37,2122,38,2127,39,2122,40,2121,41,2115,56,2131,57,2129,58,2134,59,2135,60,2133,61,2132,62,2139,63,2141,64,2140,65,2138,66,2137,67,2147,68,2148,69,2146,70,2148,71,2146,72,2152,73,2151,74,2154,75,2155,76,2153,77,2156,78,2149,79,2146,80,2145,81,2159,82,2158,83,2159,84,2137,85,2161,86,2162,87,2163,88,2164,89,2165,90,2161,91,2160],"localNames":["source","name","mapper","thisArg","into","offset","values","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":55,"name":"readPairs","flags":72,"length":4,"params":4,"locals":12,"maxStack":6,"code":[0,20,10,326,54,917565,0,65556,131082,196618,10,65546,1094,67,36,262411,0,327947,393228,10,5832778,5832763,524555,6752332,6029371,393228,393483,0,20,393226,326,54,2687037,0,131092,196612,65546,262148,43,582,5,36,327691,458764,393226,4259914,4259899,655627,5179980,4456507,458764,459019,327690,327690,327705,31,32,543,0,393236,458762,326,28,5,3145788,70,75,3080251,70,5179981,3276859,721163,4917838,70,5,720906,64,5,4915259,262154,262154,327705,31,32,543,327690,28,5,1507388,70,75,1441851,70,6752333,1638459,590091,6490190,70,5,589834,64,5,6488123,0,65556,131082,196618,262154,65546,1094,67],"consts":["$IsIterable","$Pairs","$Throw","TypeError"," expects iterable [name, value] pairs.","length","String"],"captures":[],"handlers":[50,65,71,0,72,75,77,0,25,89,95,0,96,99,101,0],"finallies":[],"finallyOffsets":[],"sites":[0,2175,1,2176,2,2177,3,2175,4,2174,5,2173,6,2179,7,2180,8,2181,9,2182,10,2183,11,2184,12,2179,13,2178,14,2188,15,2186,16,2190,19,2196,20,2192,26,2193,27,2200,28,2201,29,2202,30,2200,31,2199,32,2198,33,2204,34,2205,35,2206,36,2208,37,2209,38,2207,39,2204,41,2213,42,2212,44,2218,45,2214,51,2215,52,2222,53,2224,54,2223,55,2221,56,2223,57,2221,58,2226,59,2227,60,2228,61,2226,62,2221,63,2220,64,2214,79,2232,80,2234,81,2233,82,2231,83,2233,84,2231,85,2236,86,2231,87,2230,88,2192,103,2238,104,2239,105,2240,106,2241,107,2242,108,2243,109,2238,110,2237],"localNames":["init","label","kind","proto","pairs","strings","pair","item","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":56,"name":"pairsForEach","flags":72,"length":4,"params":4,"locals":5,"maxStack":5,"code":[3,262411,262154,65546,25,46,1703997,196618,131082,65546,262154,26,65539,26,65546,262154,26,3,26,10,838,5,262154,56,262155,131132,0,67],"consts":["length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2254,1,2252,2,2257,3,2259,4,2258,5,2256,6,2255,7,2266,8,2265,9,2269,10,2270,11,2268,12,2271,13,2267,14,2274,15,2275,16,2273,17,2276,18,2272,19,2277,20,2263,22,2280,23,2279,24,2280,25,2255,26,2250],"localNames":["target","pairs","callback","thisArg","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":57,"name":"Iterator$from","flags":72,"length":1,"params":1,"locals":1,"maxStack":10,"code":[0,20,10,326,721215,0,65556,10,131092,196633,582,917565,10,67,0,262164,0,327700,10,0,393236,10,258,458756,838,582,326,67],"consts":["$IsObject","$Inherits","Iterator","prototype","prime","wrapHelper","flattenable","Iterator.from"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2287,1,2288,2,2289,3,2287,4,2286,5,2290,6,2291,7,2292,8,2294,9,2293,10,2290,11,2285,12,2297,13,2296,14,2299,15,2300,16,2301,17,2302,18,2303,19,2304,20,2305,21,2306,22,2307,23,2308,24,2304,25,2301,26,2299,27,2298],"localNames":["value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":58,"name":"wrapHelper","flags":76,"length":2,"params":2,"locals":9,"maxStack":3,"code":[81,2,131339,0,82,786515,393483,393226,327947,131075,262411,3866683,5,258,131083,196620,65546,2555978,2555963,459019,3475276,2752571,196620,196875,196618,82,2424915,393483,2099022,70,1611,5,393226,327947,131075,262411,3866683,5,1310780,70,75,1245243,70,3475277,1441851,524555,3213134,70,5,524298,64,5,3211323,3,262411,3866683,327947,65539,262411,131082,54,4390973,0,20,10,326,5,66,327690,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,39,45,0,46,49,51,0,3,53,56,0],"finallies":[4,5,0,3,70,-1,68],"finallyOffsets":[0],"sites":[0,2309,1,2317,2,2315,3,2321,13,2325,14,2324,16,2330,17,2326,23,2327,24,2333,25,2332,38,2326,53,2318,59,2337,60,2336,61,2335,62,2339,63,2340,64,2341,65,2339,67,2318,70,2313],"localNames":["source","iterator","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":59,"name":"Iterator$prototype$map","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.map","prime","mapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2342,2,2349,3,2350,4,2351,5,2352,6,2349,7,2347,8,2354,9,2355,10,2356,11,2357,12,2354,14,2359,15,2360,16,2361,17,2362,18,2363,19,2364,20,2365,21,2361,22,2359,23,2358],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":60,"name":"mapHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4587579,5,258,196619,3,262411,327692,65546,3276874,3276859,590091,4196684,3473467,327692,327947,0,131082,327690,262154,55,7,56,262155,582,82,3145811,524555,2820430,70,1611,5,524298,459019,131075,393483,4587579,5,1507388,70,75,1441851,70,4196685,1638459,655627,3934542,70,5,655370,64,5,3932219,3,393483,4587579,459019,65539,393483,196618,54,5111869,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,50,56,0,57,60,62,0,3,64,67,0],"finallies":[6,7,0,3,81,-1,79],"finallyOffsets":[0],"sites":[0,2366,1,2375,2,2373,4,2379,14,2383,15,2382,16,2387,17,2385,19,2392,20,2388,26,2389,27,2395,28,2396,29,2397,30,2399,32,2398,34,2399,35,2395,36,2394,49,2388,64,2376,70,2403,71,2402,72,2401,73,2405,74,2406,75,2407,76,2405,78,2376,81,2371],"localNames":["source","iterator","mapper","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":61,"name":"Iterator$prototype$filter","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.filter","prime","filterHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2408,2,2415,3,2416,4,2417,5,2418,6,2415,7,2413,8,2420,9,2421,10,2422,11,2423,12,2420,14,2425,15,2426,16,2427,17,2428,18,2429,19,2430,20,2431,21,2427,22,2425,23,2424],"localNames":["predicate","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":62,"name":"filterHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4718651,5,258,196619,3,262411,327692,65546,3407946,3407931,590091,4327756,3604539,327692,327947,0,131082,327690,262154,55,7,56,262155,582,3342397,327690,82,3276883,524555,2951502,70,1611,5,524298,459019,131075,393483,4718651,5,1507388,70,75,1441851,70,4327757,1638459,655627,4065614,70,5,655370,64,5,4063291,3,393483,4718651,459019,65539,393483,196618,54,5242941,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,52,58,0,59,62,64,0,3,66,69,0],"finallies":[6,7,0,3,83,-1,81],"finallyOffsets":[0],"sites":[0,2432,1,2441,2,2439,4,2445,14,2449,15,2448,16,2453,17,2451,19,2458,20,2454,26,2455,27,2460,28,2461,29,2462,30,2464,32,2463,34,2464,35,2460,36,2459,37,2467,38,2466,51,2454,66,2442,72,2471,73,2470,74,2469,75,2473,76,2474,77,2475,78,2473,80,2442,83,2437],"localNames":["source","iterator","predicate","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":63,"name":"Iterator$prototype$take","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","takeHelper","count","take"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2476,2,2483,3,2484,4,2485,5,2486,6,2483,7,2481,8,2488,9,2489,10,2490,11,2491,12,2492,13,2493,14,2494,15,2495,16,2496,17,2497,18,2494,19,2490,20,2488,21,2487],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":64,"name":"takeHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,5570619,5,258,196619,131082,3,50,1900605,0,20,10,326,459019,458762,393483,131075,327947,5570619,262156,65546,4259914,4259899,524555,5179468,4456507,262156,262411,131082,312,131083,262154,82,3539027,459019,3213390,70,1611,5,458762,393483,131075,327947,5570619,5,131082,3,50,4194365,4130894,70,1611,5,5177403,2228284,70,75,2162747,70,5179469,2359355,590091,4917326,70,5,589834,64,5,4915259,3,327947,5570619,393483,65539,327947,196618,54,6094909,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[36,65,71,0,72,75,77,0,3,79,82,0],"finallies":[5,6,0,3,96,-1,94],"finallyOffsets":[0],"sites":[0,2498,1,2507,2,2505,3,2511,13,2515,14,2514,15,2518,16,2519,17,2517,18,2516,19,2521,20,2522,21,2523,22,2521,23,2520,30,2528,31,2524,37,2525,38,2532,39,2531,40,2532,41,2535,42,2534,55,2538,56,2539,57,2537,58,2536,59,2540,64,2524,79,2508,85,2544,86,2543,87,2542,88,2546,89,2547,90,2548,91,2546,93,2508,96,2503],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":65,"name":"Iterator$prototype$drop","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","dropHelper","count","drop"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2549,2,2556,3,2557,4,2558,5,2559,6,2556,7,2554,8,2561,9,2562,10,2563,11,2564,12,2565,13,2566,14,2567,15,2568,16,2569,17,2570,18,2567,19,2563,20,2561,21,2560],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":66,"name":"dropHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,4390971,5,258,196619,262156,65546,3080266,3080251,524555,3999820,3276859,262156,262411,131082,3,48,2097213,131082,312,131083,3014715,262154,82,2949203,459019,2623566,70,1611,5,458762,393483,131075,327947,4390971,5,1310780,70,75,1245243,70,3999821,1441851,590091,3737678,70,5,589834,64,5,3735611,3,327947,4390971,393483,65539,327947,196618,54,4915261,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,47,53,0,54,57,59,0,3,61,64,0],"finallies":[5,6,0,3,78,-1,76],"finallyOffsets":[0],"sites":[0,2571,1,2580,2,2578,3,2584,13,2588,14,2587,16,2593,17,2589,23,2590,24,2597,25,2598,26,2596,27,2595,28,2601,29,2600,30,2601,31,2595,32,2604,33,2603,46,2589,61,2581,67,2608,68,2607,69,2606,70,2610,71,2611,72,2612,73,2610,75,2581,78,2576],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":67,"name":"Iterator$prototype$flatMap","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.flatMap","prime","flatMapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2613,2,2620,3,2621,4,2622,5,2623,6,2620,7,2618,8,2625,9,2626,10,2627,11,2628,12,2625,14,2630,15,2631,16,2632,17,2633,18,2634,19,2635,20,2636,21,2632,22,2630,23,2629],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":68,"name":"flatMapHelper","flags":76,"length":3,"params":3,"locals":14,"maxStack":7,"code":[81,2,196875,262156,0,82,852051,590091,589834,524555,131075,459019,6750267,5,258,196619,3,262411,327692,65546,5439562,5439547,655627,6359628,5636155,327692,327947,393228,0,20,0,131082,327690,262154,55,7,56,262155,582,2,65540,838,4456522,4456507,786699,5377100,4653115,393228,393483,393226,82,4325459,590091,3738702,70,1611,5,4000334,70,1611,5,589834,524555,131075,459019,6750267,5,2949180,70,75,2883643,70,5377101,3080251,852235,5114958,70,5,851978,64,5,5111867,1507388,70,75,1441851,70,6359629,1638459,721163,6097486,70,5,720906,64,5,6094907,3,459019,6750267,524555,65539,459019,196618,54,7274557,0,131092,10,326,5,66,524298,67,0,67],"consts":["flattenable","Iterator.prototype.flatMap","closeIterator"],"captures":[],"handlers":[47,68,74,0,75,78,80,0,25,83,89,0,90,93,95,0,3,97,100,0],"finallies":[7,8,0,3,114,-1,112],"finallyOffsets":[0],"sites":[0,2637,1,2646,2,2644,4,2650,14,2654,15,2653,16,2658,17,2656,19,2663,20,2659,26,2660,28,2669,29,2670,30,2671,31,2672,32,2673,33,2675,35,2674,37,2675,38,2671,39,2676,40,2677,41,2669,42,2665,48,2666,49,2680,50,2679,67,2665,82,2659,97,2647,103,2684,104,2683,105,2682,106,2686,107,2687,108,2688,109,2686,111,2647,114,2642],"localNames":["source","iterator","mapper","started","counter","item","inner","%ftag0","%fval0","%ret","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":69,"name":"prime","flags":72,"length":1,"params":1,"locals":1,"maxStack":2,"code":[10,281,70,5,10,67],"consts":["next"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2696,1,2695,2,2694,4,2699,5,2698],"localNames":["helper"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":70,"name":"count","flags":72,"length":2,"params":2,"locals":4,"maxStack":5,"code":[10,309,131339,131082,20,50,720959,131082,20,53,50,917565,131082,1245243,131082,131082,65539,308,44,196875,131082,131082,51,1769535,196618,3,46,2752573,0,65556,131076,196612,65546,43,262148,43,131082,43,327684,43,582,5,196618,67],"consts":["Infinity","$Throw","RangeError","Iterator.prototype."," expects a non-negative count, received ","."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2709,1,2708,2,2706,3,2716,4,2717,5,2715,6,2714,7,2719,8,2721,9,2720,10,2718,11,2713,12,2722,13,2713,14,2724,15,2726,16,2727,17,2725,18,2723,19,2711,20,2731,21,2732,22,2730,23,2729,24,2734,25,2735,26,2733,27,2728,28,2737,29,2738,30,2739,31,2744,32,2745,33,2743,34,2746,35,2742,36,2747,37,2741,38,2748,39,2740,40,2737,42,2750,43,2749],"localNames":["limit","name","number","integer"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":71,"name":"flattenable","flags":72,"length":3,"params":3,"locals":4,"maxStack":7,"code":[10,821,4,50,524349,65546,54,852027,0,65556,10,326,54,1900605,0,131092,196612,131082,262148,43,0,327700,10,326,43,393220,43,582,5,10,458772,524313,26,196875,0,589844,196618,0,50,2818111,196618,1,50,3014717,10,3211323,10,196618,70,2,582,67],"consts":["string","$IsObject","$Throw","TypeError"," expects an iterable or iterator, received ","$Describe",".","Symbol","iterator","$Direct"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2761,1,2760,2,2762,3,2759,4,2758,5,2764,6,2763,7,2758,8,2766,9,2767,10,2768,11,2766,12,2765,13,2757,14,2770,15,2771,16,2772,17,2776,18,2777,19,2775,20,2778,21,2779,22,2780,23,2778,24,2774,25,2781,26,2773,27,2770,29,2786,30,2788,31,2787,32,2785,33,2783,34,2791,35,2792,36,2796,37,2797,38,2795,39,2794,40,2799,41,2800,42,2798,43,2793,44,2801,45,2793,46,2805,47,2804,48,2802,49,2806,50,2791,51,2790],"localNames":["value","strings","name","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":72,"name":"closeIterator","flags":72,"length":1,"params":1,"locals":2,"maxStack":4,"code":[10,25,65803,65546,0,51,655679,65546,1,51,1179967,0,65556,10,65546,70,326,54,1638461,0,131092,196612,262148,582,5,0,67],"consts":["return","$IsObject","$Throw","TypeError","Iterator return() must return an object."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2815,1,2814,2,2812,3,2821,4,2822,5,2820,6,2819,7,2824,8,2825,9,2823,10,2818,11,2827,12,2828,13,2832,14,2831,15,2829,16,2827,17,2826,18,2817,19,2834,20,2835,21,2836,22,2837,23,2834,25,2810],"localNames":["iterator","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":73,"name":"Iterator$prototype$toArray","flags":88,"length":0,"params":0,"locals":5,"maxStack":4,"code":[789,267,36,65803,131084,0,20,10,2,582,1769546,1769531,196875,2687820,1966139,131084,131339,65546,65546,65561,31,32,543,131082,28,5,852028,70,75,786491,70,2687821,983099,262411,2425678,70,5,262154,64,5,2424891,65546,67],"consts":["$Direct","length"],"captures":[],"handlers":[15,27,33,0,34,37,39,0],"finallies":[],"finallyOffsets":[],"sites":[0,2838,2,2844,3,2842,5,2849,6,2850,7,2851,8,2852,9,2849,10,2845,16,2846,17,2856,18,2858,19,2857,20,2855,21,2857,22,2855,23,2860,24,2855,25,2854,26,2845,41,2862,42,2861],"localNames":["this","result","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":74,"name":"Iterator$prototype$reduce","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,65813,131339,0,20,10,2,582,196875,0,65556,65546,131076,582,5,131082,3,26,262411,131082,196633,3,48,327947,3,393483,458764,196618,3670090,3670075,524555,4589644,3866683,458764,459019,327690,3145789,0,65546,262154,458762,393226,838,262155,3276859,458762,262155,258,327691,393226,56,393227,2228284,70,75,2162747,70,4589645,2359355,590091,4327502,70,5,589834,64,5,4325435,327690,54,5177405,0,262164,327684,393220,582,5,262154,67],"consts":["$Direct","$Callback","Iterator.prototype.reduce","length","$Throw","TypeError","Iterator.prototype.reduce of an empty iterator with no initial value."],"captures":[],"handlers":[36,56,62,0,63,66,68,0],"finallies":[],"finallyOffsets":[],"sites":[0,2863,2,2865,4,2866,6,2872,7,2873,8,2874,9,2875,10,2872,11,2870,12,2877,13,2878,14,2879,15,2880,16,2877,18,2885,19,2886,20,2884,21,2882,22,2892,23,2891,24,2894,25,2890,26,2888,27,2898,28,2896,30,2903,31,2899,37,2900,38,2906,39,2905,40,2910,41,2911,42,2912,43,2913,44,2914,45,2910,46,2909,47,2905,48,2918,49,2917,50,2922,51,2921,52,2925,53,2924,54,2925,55,2899,70,2928,71,2927,72,2926,73,2930,74,2931,75,2932,76,2933,77,2930,79,2935,80,2934],"localNames":["this","callback","initial","iterator","accumulator","started","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":75,"name":"Iterator$prototype$forEach","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,5,0,67],"consts":["iteratorTest","forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2936,2,2941,3,2942,4,2943,5,2944,6,2945,7,2941,9,2939],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":76,"name":"Iterator$prototype$some","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2946,2,2951,3,2952,4,2953,5,2954,6,2955,7,2951,8,2950],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":77,"name":"Iterator$prototype$every","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2956,2,2961,3,2962,4,2963,5,2964,6,2965,7,2961,8,2960],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":78,"name":"Iterator$prototype$find","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2966,2,2971,3,2972,4,2973,5,2974,6,2975,7,2971,8,2970],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":79,"name":"iteratorTest","flags":72,"length":3,"params":3,"locals":10,"maxStack":5,"code":[0,20,10,2,582,196875,0,65556,65546,131076,131082,43,582,5,3,262411,0,327947,393228,196618,5308490,5308475,459019,6227788,5505083,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,196612,50,2752831,327690,3342397,258,590091,3213134,70,1611,5,589834,67,131082,262148,50,3735871,327690,54,4325437,2,590091,4196174,70,1611,5,589834,67,131082,327684,50,4653375,327690,5242941,393226,590091,5113678,70,1611,5,589834,67,1507388,70,75,1441851,70,6227789,1638459,524555,5965646,70,5,524298,64,5,5963835,131082,262148,50,6619197,258,7077947,131082,196612,50,7012413,2,7077947,0,67],"consts":["$Direct","$Callback","Iterator.prototype.","some","every","find"],"captures":[],"handlers":[25,81,87,0,88,91,93,0],"finallies":[],"finallyOffsets":[],"sites":[0,2985,1,2986,2,2987,3,2988,4,2985,5,2983,6,2990,7,2991,8,2992,9,2994,10,2995,11,2993,12,2990,14,2999,15,2997,16,3001,19,3007,20,3003,26,3004,27,3012,28,3013,29,3014,30,3016,32,3015,34,3016,35,3012,36,3011,37,3020,38,3021,39,3019,40,3018,41,3022,42,3017,43,3024,44,3023,51,3028,52,3029,53,3027,54,3026,55,3031,56,3030,57,3025,58,3033,59,3032,66,3037,67,3038,68,3036,69,3035,70,3039,71,3034,72,3041,73,3040,80,3003,95,3045,96,3046,97,3044,98,3043,99,3047,100,3043,101,3050,102,3051,103,3049,104,3048,105,3052,106,3048,107,3053,108,3042],"localNames":["self","callback","name","iterator","index","result","item","%it","%e","%ret"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":80,"name":"revive","flags":72,"length":3,"params":3,"locals":7,"maxStack":5,"code":[10,65546,26,196875,0,20,196618,326,3932221,262156,327692,393228,0,65556,196618,326,262411,3,327947,0,393483,327690,262154,131097,46,3932221,0,196628,196618,262154,327690,26,131082,838,393227,393226,0,50,3014717,196618,262154,327690,26,30,5,3670075,196618,262154,327690,26,31,32,543,393226,28,5,327690,56,327691,1376316,10,131082,65546,196618,582,67],"consts":["$IsObject","$Keys","length","revive"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3064,1,3065,2,3063,3,3061,4,3067,5,3068,6,3069,7,3067,8,3066,12,3074,13,3075,14,3076,15,3074,16,3072,17,3080,18,3078,19,3082,21,3086,22,3088,23,3087,24,3085,25,3084,26,3094,27,3095,28,3096,29,3098,30,3099,31,3097,32,3100,33,3094,34,3093,35,3103,36,3104,37,3102,38,3101,39,3108,40,3110,41,3111,42,3109,43,3107,44,3106,45,3101,46,3115,47,3117,48,3118,49,3116,50,3114,51,3116,52,3114,53,3119,54,3114,55,3113,56,3122,57,3121,58,3122,59,3084,60,3127,61,3126,62,3128,63,3129,64,3124,65,3123],"localNames":["holder","key","reviver","value","names","index","revived"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":81,"name":"replaceWith","flags":72,"length":3,"params":3,"locals":6,"maxStack":9,"code":[4,196875,3,262411,262154,10,65561,46,3211325,196618,10,262154,26,3,26,0,131092,0,131082,36,10,262154,26,65539,26,2228810,2228283,327947,1967439,2621499,2622796,2424891,37,1966139,70,587,1769531,70,2622797,2097211,71,326,43,43,196619,262154,56,262155,262204,196618,65546,43,67],"consts":["","length","String"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3139,1,3137,2,3143,3,3141,4,3146,5,3148,6,3147,7,3145,8,3144,9,3153,10,3157,11,3158,12,3156,13,3159,14,3155,15,3160,16,3161,17,3162,18,3163,19,3162,20,3167,21,3168,22,3166,23,3169,24,3165,25,3164,40,3162,41,3160,42,3154,43,3152,44,3153,45,3172,46,3171,47,3172,48,3144,49,3175,50,3176,51,3174,52,3173],"localNames":["matches","rest","callback","result","index","%it"],"upvalNames":[]}]},"@vm-internal":{"text":"8cec7c4116877903","codes":[{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":1,"name":"$vmToPrimitive","flags":72,"length":2,"params":2,"locals":8,"maxStack":4,"code":[0,20,10,326,54,524349,10,67,65546,65540,50,1310783,65546,131076,50,1311039,0,196628,10,326,1507389,262148,1572923,327684,131339,131082,262148,50,2031677,327684,2097211,262148,196875,0,393236,10,131082,582,262411,262154,0,51,3670077,393228,10,262154,70,393483,0,458772,393226,326,54,3670077,393226,67,0,393236,10,196618,582,327947,327690,0,51,5177405,458764,10,327690,70,459019,0,458772,458762,326,54,5177405,458762,67,0,524308,589828,655364,582,5,0,67],"consts":["$IsDataObj","string","default","$IsDate","toString","valueOf","$GetMethod","$IsObj","$Throw","TypeError","Cannot convert object to primitive value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,8,1,9,2,10,3,8,4,7,5,6,6,12,7,11,8,19,9,20,10,18,11,17,12,23,13,24,14,22,15,21,16,25,17,26,18,27,19,25,20,16,21,28,22,16,23,29,24,14,25,35,26,36,27,34,28,33,29,37,30,33,31,38,32,31,33,42,34,43,35,44,36,45,37,42,38,40,39,48,40,49,41,47,42,46,44,57,45,56,46,54,47,52,48,60,49,61,50,62,51,60,52,59,53,58,54,64,55,63,56,68,57,69,58,70,59,71,60,68,61,66,62,74,63,75,64,73,65,72,67,83,68,82,69,80,70,78,71,86,72,87,73,88,74,86,75,85,76,84,77,90,78,89,79,92,80,93,81,94,82,95,83,92,85,5],"localNames":["v","hint","first","second","m1","m2","r1","r2"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":2,"name":"$vmToKey","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$PropKey","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,101,1,102,2,103,3,104,4,105,5,106,6,103,7,101,8,100],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":3,"name":"$vmToString","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$CoerceToString","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,112,1,113,2,114,3,115,4,116,5,117,6,114,7,112,8,111],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":4,"name":"$vmToNumeric","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,124,1,125,2,126,3,127,4,128,5,129,6,130,7,127,8,124,9,123],"localNames":["incFlag","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":5,"name":"$vmInc","flags":72,"length":2,"params":2,"locals":3,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,131339,10,3,50,1179709,131082,65539,43,1376315,131082,65539,44,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,139,1,140,2,141,3,142,4,143,5,144,6,145,7,142,8,139,9,137,10,149,11,150,12,148,13,147,14,152,15,153,16,151,17,147,18,155,19,156,20,154,21,146],"localNames":["incFlag","v","n"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":6,"name":"$vmUnary","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyUnary","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,163,1,164,2,165,3,166,4,167,5,168,6,169,7,166,8,163,9,162],"localNames":["sub","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":7,"name":"$vmBinary","flags":72,"length":3,"params":3,"locals":8,"maxStack":5,"code":[0,20,10,65546,131082,838,196875,196618,3,50,1179709,0,65556,10,65546,131082,838,67,196618,65539,50,2359357,458764,0,131092,65546,196612,582,459019,0,65556,10,458762,131082,838,67,196618,131075,50,2752573,262148,2818107,327684,262411,0,131092,65546,262154,582,327947,0,131092,131082,262154,582,393483,0,65556,10,327690,393226,838,67],"consts":["$OpConvKind","$ApplyBinary","$vmToPrimitive","string","default","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,179,1,180,2,181,3,182,4,183,5,179,6,177,7,186,8,187,9,185,10,184,11,189,12,190,13,191,14,192,15,193,16,189,17,188,18,196,19,197,20,195,21,194,23,202,24,203,25,204,26,205,27,202,28,200,29,207,30,208,31,209,32,210,33,211,34,207,35,206,36,217,37,218,38,216,39,215,40,219,41,215,42,220,43,213,44,224,45,225,46,226,47,227,48,224,49,222,50,231,51,232,52,233,53,234,54,231,55,229,56,236,57,237,58,238,59,239,60,240,61,236,62,235],"localNames":["op","lhs","rhs","kind","hint","l","r","key"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":8,"name":"$vmGetElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,26,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,248,1,249,2,250,3,251,4,249,5,247,6,246],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":9,"name":"$vmDeleteElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,30,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,260,1,261,2,262,3,263,4,261,5,259,6,257],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":10,"name":"$vmConvert","flags":72,"length":6,"params":6,"locals":16,"maxStack":7,"code":[258,7274557,393228,458764,524300,589836,655372,720908,786444,0,20,327690,326,393483,0,65556,327690,3,582,459019,0,65556,327690,65539,582,524555,393226,131097,590091,36,655627,3,721163,720906,589834,46,5439549,851980,917516,983052,393226,720906,26,852235,0,196628,327690,720906,582,917771,0,262164,851978,326,3997757,0,327700,851978,917514,582,4063291,851978,983307,655370,720906,31,32,543,458762,4980797,0,393236,917514,983050,582,5046331,983050,28,5,720906,56,720907,2162748,262154,524298,31,32,543,655370,28,5,0,458772,10,65546,131082,196618,262154,1350,786699,0,524308,786442,326,54,7077949,786442,67,786442,327691,60,0,67],"consts":["$ConvValues","$ConvFlag","length","$ConvHint","$IsObj","$vmToPrimitive","$ConvCoerce","$ConvReplay","$IsConvert"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,274,1,273,9,279,10,280,11,281,12,279,13,277,14,285,15,286,16,287,17,288,18,285,19,283,20,292,21,293,22,294,23,295,24,292,25,290,26,300,27,299,28,297,29,305,30,303,31,309,32,307,33,312,34,313,35,311,36,310,40,319,41,320,42,318,43,316,44,324,45,325,46,326,47,327,48,324,49,322,50,332,51,333,52,334,53,332,54,331,55,335,56,336,57,337,58,338,59,335,60,331,61,339,62,329,63,343,64,344,65,342,66,344,67,342,68,346,69,345,70,347,71,348,72,349,73,350,74,347,75,345,76,351,77,342,78,341,79,354,80,353,81,354,82,310,83,358,84,359,85,357,86,359,87,357,88,360,89,357,90,356,91,364,92,365,93,366,94,367,95,368,96,369,97,370,98,364,99,362,100,373,101,374,102,375,103,373,104,372,105,371,106,377,107,376,108,381,109,380,110,273,111,272],"localNames":["callable","target","args","constructing","replay","req","values","coerce","ordinal","len","converted","i","res","val","hint","prim"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":11,"name":"$vmJsonVisitor","flags":72,"length":3,"params":3,"locals":5,"maxStack":1,"code":[269,65805,131341,196621,262157,786454,262416,36,196880,262159,67],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,382,5,392,7,391,8,389,9,639,10,638],"localNames":["boundary","replacer","properties","ancestors","visit"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":12,"name":"visit","flags":76,"length":3,"params":3,"locals":19,"maxStack":6,"code":[81,131082,6553603,48,720957,0,20,65540,131076,582,5,10,65546,26,196875,0,196628,196618,326,2228285,17,54,1900605,0,20,262148,327684,582,5,196618,82,2162771,67,196619,0,393236,196618,326,3473469,458764,196618,458777,459019,0,524308,458762,326,3473469,196618,458762,65546,326,196619,65553,0,51,4128829,10,65553,65546,196618,582,196619,0,589844,196618,17,131089,838,262411,262154,3,50,4980797,0,67,262154,65539,50,5374013,196618,67,262154,131075,50,5767229,1,67,262154,196611,50,6422589,0,655380,196618,17,582,67,262154,262147,50,7012413,0,720916,196618,326,7077947,196618,327947,131089,131089,786457,31,32,543,327690,28,5,0,393483,0,851988,327690,326,13172797,524300,589836,36,393227,327690,786457,524555,3,590091,589834,524298,46,13107261,655372,0,196625,327690,0,917524,589834,326,131082,65539,43,838,11338570,11337787,983307,0,3,7,1048843,11079509,1048586,131075,50,10682429,67,5,11013966,70,1611,5,983105,326,11538518,82,10158140,70,843,9961531,1048586,131075,50,11862077,67,655627,393226,589834,31,32,543,655370,0,50,12648509,1,12714043,655370,28,5,589834,56,589835,8781884,18546747,720908,786444,0,1048596,70,393227,0,1114132,327690,17,262161,838,721163,3,786699,786442,720906,786457,46,18546749,851980,917516,720906,786442,26,852235,0,196625,327690,851978,131082,65539,43,838,16909130,16908347,1114379,0,3,7,1179915,16650581,1179658,131075,50,16252989,67,5,16585038,70,1611,5,1179713,326,17109590,82,15728700,70,843,15532091,1179658,131075,50,17432637,67,917771,917514,0,51,18284605,0,1245204,393226,851978,917514,258,1094,5,786442,56,786443,14155836,0,1310740,131089,326,5,393226,67],"consts":["$Throw","RangeError","Value exceeds the maximum depth of 100.","$IsPromise","InvalidData","JSON.stringify received an un-awaited Promise; await it first - e.g. `const result = await tools.ns.tool(...)`.","$IsObj","toJSON","$IsCallableAny","$JsonClassify","$JsonSpecial","$SetToArray","length","$IsArray","String",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$NewNullProtoObj","$JsonNames",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$Define","$ArrayPop"],"captures":[0,0,0,1,0,3,0,4,0,2],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,392,1,400,2,401,3,399,4,398,5,403,6,404,7,405,8,406,9,403,11,411,12,412,13,410,14,408,15,414,16,415,17,416,18,414,19,413,20,420,21,419,22,418,23,423,24,424,25,425,26,426,27,423,29,431,30,430,33,429,34,433,35,434,36,435,37,433,38,432,40,441,41,440,42,438,43,444,44,445,45,446,46,444,47,443,48,453,49,452,50,454,51,450,52,449,53,457,54,458,55,456,56,455,57,465,58,464,59,466,60,467,61,462,62,461,63,471,64,472,65,473,66,474,67,475,68,471,69,469,70,478,71,479,72,477,73,476,74,481,75,480,76,484,77,485,78,483,79,482,80,487,81,486,82,490,83,491,84,489,85,488,86,493,87,492,88,496,89,497,90,495,91,494,92,499,93,500,94,501,95,502,96,499,97,498,98,508,99,509,100,507,101,506,102,510,103,511,104,512,105,510,106,506,107,513,108,504,109,517,110,519,111,518,112,516,113,518,114,516,115,521,116,516,117,515,118,523,120,526,121,527,122,528,123,526,124,525,127,533,128,532,129,538,130,537,131,535,132,543,133,541,134,546,135,547,136,545,137,544,139,553,140,554,141,555,142,556,143,557,144,558,145,556,146,560,147,561,148,559,149,553,150,552,181,550,182,565,183,566,184,564,185,566,186,564,187,569,188,570,189,568,190,567,191,571,192,567,193,572,194,564,195,563,196,575,197,574,198,575,199,544,200,525,203,580,204,581,205,580,206,579,207,585,208,586,209,587,210,588,211,589,212,585,213,583,214,593,215,591,216,596,217,598,218,597,219,595,220,594,223,605,224,606,225,604,226,602,227,611,228,612,229,613,230,614,231,616,232,617,233,615,234,611,235,610,266,608,267,620,268,621,269,619,270,618,271,623,272,624,273,625,274,626,275,627,276,628,277,623,279,631,280,630,281,631,282,594,283,633,284,634,285,635,286,633,288,637,289,636],"localNames":["holder","key","depth","val","kind","target","out","toJSON","len","i","elem","names","i","k","propVal","%it","%kind","%it","%kind"],"upvalNames":["boundary","replacer","ancestors","visit","properties"]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":13,"name":"$vmJsonWalk","flags":72,"length":4,"params":4,"locals":5,"maxStack":6,"code":[0,0,20,2,10,131082,838,35,65546,65574,65540,3,838,131353,70,196633,262411,0,262164,2,262154,131082,196618,1094,67],"consts":["$vmJsonVisitor","","next","value","$JsonFinish"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,653,1,654,2,655,3,656,4,657,5,658,6,654,7,659,8,662,9,660,10,663,11,664,12,653,13,652,14,651,15,650,16,648,17,668,18,669,19,670,20,671,21,672,22,673,23,668,24,667],"localNames":["replacer","value","properties","indent","json"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":14,"name":"$vmSettleCall","flags":72,"length":3,"params":3,"locals":3,"maxStack":5,"code":[0,20,10,65546,131082,838,67],"consts":["$Apply"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,681,1,682,2,683,3,684,4,685,5,681,6,680],"localNames":["callable","thisVal","args"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":15,"name":"$vmCombine","flags":72,"length":2,"params":2,"locals":6,"maxStack":4,"code":[0,20,10,65546,582,131339,196620,65546,1441866,1441851,262411,2360396,1638459,196620,196875,0,65556,131082,196618,582,5,720956,70,75,655419,70,2360397,852027,327947,2098254,70,5,327690,64,5,2097211,0,131092,131082,326,5,0,67],"consts":["$CombineStart","$CombineAdd","$CombineEnd"],"captures":[],"handlers":[13,22,28,0,29,32,34,0],"finallies":[],"finallyOffsets":[],"sites":[0,694,1,695,2,696,3,697,4,694,5,692,7,702,8,698,14,699,15,705,16,706,17,707,18,708,19,705,21,698,36,710,37,711,38,712,39,710,41,690],"localNames":["name","items","c","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":16,"name":"$vmOpenSyncForAsync","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[0,20,10,65546,70,326,67],"consts":["$WrapAsyncFromSync"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,719,1,720,2,724,3,723,4,721,5,719,6,718],"localNames":["v","method"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":17,"name":"$vmAsyncFromSync","flags":72,"length":4,"params":4,"locals":10,"maxStack":7,"code":[0,20,10,326,262411,65546,65540,50,917565,0,131092,10,326,1310779,0,196628,10,262154,65546,838,327947,327690,0,50,1835071,327690,1,50,4718653,589836,65546,262148,50,2687037,0,327700,196618,326,5,0,67,0,196628,10,262154,262148,838,590091,589834,0,51,3604799,589834,1,51,4259901,0,393236,10,262154,589834,70,262148,838,5,0,458772,10,326,5,0,67,131082,5177405,262154,327690,196618,326,5374011,262154,327690,70,393483,0,393236,10,393226,65546,838,5,393226,524313,54,54,459019,393226,589849,524555,0,655380,10,524298,458762,65546,262148,51,1094,5,0,67],"consts":["$AfsGetIter","next","$AfsGetNext","$AfsGetMethod","return","$AfsResolveDone","$AfsCheckObj","$AfsRejectNoThrow","done","value","$AfsSettle"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,735,1,736,2,737,3,735,4,733,5,743,6,744,7,742,8,741,9,745,10,746,11,747,12,745,13,741,14,748,15,749,16,750,17,751,18,752,19,748,20,739,21,756,22,757,23,755,24,754,25,759,26,760,27,758,28,753,30,764,31,765,32,763,33,762,34,768,35,769,36,770,37,768,39,771,41,775,42,776,43,777,44,778,45,779,46,775,47,773,48,783,49,784,50,782,51,781,52,786,53,787,54,785,55,780,56,790,57,791,58,792,59,796,60,795,61,793,62,797,63,790,65,799,66,800,67,801,68,799,70,802,72,807,73,806,74,811,75,810,76,812,77,808,78,806,79,816,80,815,81,813,82,804,83,818,84,819,85,820,86,821,87,822,88,818,90,829,91,828,92,827,93,826,94,824,95,835,96,834,97,832,98,838,99,839,100,840,101,841,102,842,103,844,104,845,105,843,106,838,108,731],"localNames":["adapter","kind","hasArg","val","iter","method","res","done","value","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":18,"name":"$vmAsyncFromSyncClose","flags":72,"length":1,"params":1,"locals":2,"maxStack":2,"code":[10,25,65803,65546,0,51,655679,65546,1,51,983101,10,65546,70,5,0,67],"consts":["return"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,854,1,853,2,851,3,859,4,860,5,858,6,857,7,862,8,863,9,861,10,856,11,868,12,867,13,865,15,849],"localNames":["iter","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":19,"name":"$vmCross","flags":74,"length":3,"params":3,"locals":8,"maxStack":8,"code":[36,196875,3,262411,262154,10,25,46,5046333,327692,393228,458764,10,262154,26,327947,65546,0,50,1573183,0,65556,327690,326,1835069,327690,340,327691,0,0,131092,258,0,1,838,35,327690,196646,196612,3,838,393483,393226,262425,70,459019,458762,327705,54,3801149,393226,262425,458762,393241,340,326,458763,3014716,196618,262154,31,32,543,0,458772,258,458762,393241,1,0,1094,28,5,262154,56,262155,262204,65546,0,50,5570621,196618,3,26,67,65546,524292,50,6160445,0,589844,196618,326,67,0,655380,65546,196618,131082,838,5,0,67],"consts":["length","$IsThenable","$vmJsonVisitor","","next","done","value","$JsonFinish","search","$HostSearch","$HostTool"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,878,1,876,2,882,3,880,4,885,5,887,6,886,7,884,8,883,12,894,13,895,14,893,15,891,16,899,17,900,18,898,19,897,20,901,21,902,22,903,23,901,24,896,25,908,26,907,27,906,28,912,29,913,30,914,31,915,32,916,33,917,34,913,35,918,36,921,37,919,38,922,39,923,40,912,41,910,42,929,43,928,44,927,45,925,46,934,47,933,48,932,49,931,50,941,51,940,52,945,53,944,54,943,55,939,56,938,57,931,58,950,59,951,60,949,61,951,62,949,63,952,64,953,65,954,66,956,67,955,68,958,69,959,70,952,71,949,72,948,73,962,74,961,75,962,76,883,77,965,78,966,79,964,80,963,81,969,82,970,83,968,84,967,85,973,86,974,87,972,88,971,89,976,90,977,91,978,92,976,93,975,94,980,95,981,96,982,97,983,98,984,99,980,101,874],"localNames":["roots","path","resolving","crossed","r","root","walk","step"],"upvalNames":[]}]}} diff --git a/packages/codemode/src/vm/prelude/bytecode.ts b/packages/codemode/src/vm/prelude/bytecode.ts new file mode 100644 index 00000000000..91d395d876f --- /dev/null +++ b/packages/codemode/src/vm/prelude/bytecode.ts @@ -0,0 +1,37 @@ +import { parse } from "acorn" +import { Source } from "../runtime/source.js" +import { hash } from "../snapshot/snapshot.js" +import { type Code, codeFromJson, type CodeJson, codeToJson } from "../code.js" +import { compileSource } from "../compiler/compile.js" +import saved from "./bytecode.json" with { type: "json" } + +/** A built-in source's bytecode as `script/prelude.ts` saves it, with a hash of the text it was compiled from. */ +export type SavedCodes = { readonly text: string; readonly codes: ReadonlyArray } + +/** + * A built-in source's bytecode (the prelude's, the VM's internal helpers'), made once per process: from the bytecode + * compiled ahead of time when it was compiled from this text, so the process neither parses nor compiles it until + * something asks for its nodes (an error's site, a saved state's); compiled now otherwise. A test checks the saved + * bytecode is what the compiler makes. Its top-level functions start at a line's start; they are found by name. + */ +export const builtinCodes = (id: string, text: string) => { + const tree = () => parse(text, { ecmaVersion: "latest", sourceType: "script" }) + const ahead = (saved as { readonly [id: string]: SavedCodes | undefined })[id] + const lazy = new Source(id, text, tree) + const codes = + ahead?.text === hash(text) + ? ahead.codes.map((code) => codeFromJson(code, lazy)) + : compileSource(new Source(id, text, tree()), { internal: true }) + const byName = new Map() + // A top-level function comes before the functions nested in it, in pre-order. + for (const [, name] of text.matchAll(/^(?:async )?function\*? ?([\w$]+)/gm)) { + byName.set(name!, codes.find((made) => made.index > 0 && made.name === name)!) + } + return { codes, byName } +} + +/** What `script/prelude.ts` saves for a built-in source. */ +export const saveCodes = (text: string, codes: ReadonlyArray): SavedCodes => ({ + text: hash(text), + codes: codes.map(codeToJson), +}) diff --git a/packages/codemode/src/vm/prelude/helpers.ts b/packages/codemode/src/vm/prelude/helpers.ts new file mode 100644 index 00000000000..f213fade8a9 --- /dev/null +++ b/packages/codemode/src/vm/prelude/helpers.ts @@ -0,0 +1,288 @@ +import { isSupportedCallback, requireCallback } from "../runtime/callback.js" +import type { Builtins } from "../runtime/intrinsics.js" +import { SORT_RUN, sorted, type SortInto } from "../stdlib/array.js" +import { finishBytes } from "../stdlib/bytes.js" +import { weakKey } from "../stdlib/collections.js" +import { buildPairs } from "../stdlib/url.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { checkArrayLength } from "../runtime/limits.js" +import { invalidData, IteratorSymbol, PendingThrow } from "../runtime/model.js" +import { native, receiver } from "../runtime/native.js" +import { + Arr, + Bytes, + coerceToString, + data, + define, + hidden, + hasPrototype, + keys, + getOwn, + IteratorObj, + positionIterator, + coerceToNumber, + get, + MapObj, + type NativeResult, + Obj, + PromiseObj, + SetObj, + type Value, + WeakMapObj, + WeakSetObj, +} from "../runtime/objects.js" +import { builtinCursor, requireIteratorObject } from "../runtime/operations.js" +import { containsOpaqueReference, describeValue, isOpaque } from "../runtime/references.js" +import { compareText } from "../../tool-runtime.js" +import { Invoke } from "../runtime/signals.js" + +// The prelude's privileged helpers: leaf built-ins (they never call program code) bound only in the prelude's scope. + +type Helper = readonly [length: number, call: (args: Array) => NativeResult] + +const classes = { Array: Arr, Uint8Array: Bytes, Map: MapObj, Set: SetObj } as const + +// Bun exposes ES2026 Math.sumPrecise before TypeScript's standard library types. +declare global { + interface Math { + sumPrecise(values: Iterable): number + } +} + +export const helpers = (ctx: Interpreter): Array => { + const builtins = ctx.builtins + const helpers: Record = { + /** Throws a program error of `type` ("TypeError", "RangeError", or "InvalidData") with `message`. */ + $Throw: [ + 2, + ([type, message]) => { + if (type === "InvalidData") throw invalidData(String(message)) + throw new PendingThrow(type as "TypeError", String(message)) + }, + ], + /** The callback itself, or the TypeError for one a callback site doesn't admit. */ + $Callback: [2, ([value, label]) => requireCallback(value, String(label))], + /** Calls `fn` with a receiver and arguments, as `Function.prototype.call` would, without reading `call`. */ + $Call: [2, ([fn, thisArg, ...args]) => new Invoke(fn, thisArg, args, undefined)], + /** The receiver, when it is the built-in kind `kind` names, or the TypeError for any other. */ + $Receiver: [ + 3, + ([value, kind, method]) => receiver(classes[kind as keyof typeof classes], value, String(method)), + ], + /** Whether `value` is an object; with `data`, one holding no opaque reference. */ + $IsObject: [2, ([value, data]) => value instanceof Obj && !(data === true && containsOpaqueReference(value))], + /** Whether `for...of` can iterate `value`. */ + $IsIterable: [ + 1, + ([value]) => { + if (builtinCursor(builtins, value) !== undefined) return true + if (!(value instanceof Obj)) return false + const method = get(value, IteratorSymbol) + return method !== undefined && method !== null + }, + ], + $Describe: [1, ([value]) => describeValue(value)], + $Inherits: [2, ([value, proto]) => hasPrototype(value, proto as Obj)], + /** An object's own enumerable string keys. */ + $Keys: [1, ([value]) => new Arr(builtins.Array, keys(value as Obj))], + /** Defines an own data property, enumerable or hidden, and returns the object. */ + $Define: [ + 4, + ([target, key, value, enumerable]) => { + define(target as Obj, key as string, value, enumerable === true ? data : hidden) + return target + }, + ], + $SumPrecise: [1, ([numbers]) => Math.sumPrecise((numbers as Arr).items as Array)], + /** A Uint8Array of the values (see `collectBytes`). */ + $Bytes: [ + 3, + ([values, into, offset]) => { + const items = (values as Arr).items + checkArrayLength(items.length) + return finishBytes(Uint8Array.from(items, coerceToNumber), into as Obj, offset as number) + }, + ], + /** Headers or URLSearchParams from the pairs `readPairs` read, or from a record. */ + $Pairs: [ + 4, + ([kind, proto, value, label]) => + buildPairs({ kind: kind as "Headers", proto: proto as Obj }, value, String(label)), + ], + $IsCallable: [1, ([value]) => isSupportedCallback(value)], + /** + * An iterator object as `for...of` steps it, reading `next` once and calling it only then (GetIteratorDirect); + * with `arrays`, an array's values. + */ + $Direct: [ + 2, + ([value, arrays]) => + arrays === true && value instanceof Arr + ? positionIterator(builtins, value, "values") + : value instanceof IteratorObj + ? value + : new IteratorObj(builtins.Iterator, { + iterator: requireIteratorObject(value, "An iterator"), + next: get(value as Obj, "next"), + asynchronous: false, + }), + ], + /** A sort's result written where it goes (see `sortWith`): the defined values in order, then `undefs` undefined. */ + $Sorted: [ + 5, + ([target, into, holes, values, undefs]) => + sorted(builtins, target as Arr | Bytes, into as SortInto, holes as number, [ + ...(values as Arr).items, + ...Array(undefs as number).fill(undefined), + ]), + ], + /** Ends one merge of `sortWith`: the runs' remaining values after the merged ones, then all of them back in `work`. */ + $MergeRest: [ + 8, + ([work, merged, count, left, middle, right, high, low]) => { + const items = (work as Arr).items + const out = (merged as Arr).items + out.length = count as number + for (let at = left as number; at < (middle as number); at++) out.push(items[at]) + for (let at = right as number; at < (high as number); at++) out.push(items[at]) + for (let at = 0; at < out.length; at++) items[(low as number) + at] = out[at] + return undefined + }, + ], + $SortState: [1, ([values]) => sortState(builtins, values as Arr)], + $SortStep: [1, ([state]) => sortStep(state as Arr)], + $SortResult: [ + 2, + ([state, values]) => { + const items = (values as Arr).items + return new Arr( + builtins.Array, + ((state as Arr).items[1] as Arr).items.map((at) => items[at as number]), + ) + }, + ], + /** What the collection built-ins need from a Map, Set, WeakMap, or WeakSet's table; see `table`. */ + $Table: [4, ([op, target, key, value]) => table(builtins, String(op), target, key, value)], + } + return Object.entries(helpers).map(([name, [length, call]]) => [ + name, + native(builtins, { name, length, call: (_, args) => call(args), callback: false }), + ]) +} + +const table = (builtins: Builtins, op: string, target: Value, key: Value, value: Value): Value => { + switch (op) { + case "add": + if (target instanceof MapObj) target.map.set(key, value) + else if (target instanceof SetObj) target.set.add(key) + else if (target instanceof WeakMapObj) target.map.set(weakKey(key, "as weak map key"), value) + else (target as WeakSetObj).set.add(weakKey(key, "in weak set")) + return undefined + case "delete": + return (target as SetObj).set.delete(key) + case "has": + return (target as SetObj).set.has(key) + case "size": + return (target as SetObj).set.size + case "copy": { + const copy = new SetObj(builtins.Set) + for (const item of (target as SetObj).set.liveKeys()) copy.set.add(item) + return copy + } + case "values": + return positionIterator(builtins, target as SetObj, "values") + case "entries": + return positionIterator(builtins, target as MapObj | SetObj, "entries") + case "groups": + return target === "Map" ? new MapObj(builtins.Map) : new Obj(null) + case "groupKey": + if (target instanceof PromiseObj) return coerceToString(target) + if (isOpaque(target)) { + throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(target)}.`) + } + return target + case "group": { + if (target instanceof MapObj) { + const group = target.map.get(key) + if (group === undefined) target.map.set(key, new Arr(builtins.Array, [value])) + else (group as Arr).items.push(value) + return undefined + } + const group = getOwn(target as Obj, key as string) + if (group === undefined) define(target as Obj, key as string, new Arr(builtins.Array, [value])) + else (group as Arr).items.push(value) + return undefined + } + } + throw new Error(`Unknown table operation ${op}.`) +} + +// The most values one merge step writes. +const MERGE_STEP = 65_536 + +/** + * A default sort's state, as data: the values' string keys, the order so far and a buffer (indices into the values), + * the next run to sort, and the merge pass's run width, output position, and positions in its two runs. Runs of + * SORT_RUN are sorted first, then merged in passes of doubling width from the order into the buffer, which swap. + */ +const sortState = (builtins: Builtins, values: Arr): Arr => { + const keys = values.items.map(coerceToString) + const order = keys.map((_, at) => at) + return new Arr(builtins.Array, [ + new Arr(builtins.Array, keys), + new Arr(builtins.Array, order), + new Arr(builtins.Array, [...order]), + 0, + SORT_RUN, + 0, + 0, + 0, + ]) +} + +// One bounded step of a default sort: one run sorted, or up to MERGE_STEP values merged. Whether work remains. +const sortStep = (state: Arr): boolean => { + const slots = state.items + const keys = (slots[0] as Arr).items as Array + const from = (slots[1] as Arr).items as Array + const length = keys.length + const run = slots[3] as number + const width = slots[4] as number + const compare = (left: number, right: number) => compareText(keys[left]!, keys[right]!) + if (run < length) { + const end = Math.min(run + SORT_RUN, length) + const part = from.slice(run, end).sort(compare) + for (let at = 0; at < part.length; at++) from[run + at] = part[at]! + slots[3] = end + return end < length || width < length + } + const to = (slots[2] as Arr).items as Array + let out = slots[5] as number + let left = slots[6] as number + let right = slots[7] as number + const stop = Math.min(out + MERGE_STEP, length) + while (out < stop) { + const low = out - (out % (2 * width)) + const middle = Math.min(low + width, length) + const high = Math.min(low + 2 * width, length) + if (out === low) { + left = low + right = middle + } + while (out < high && out < stop) { + to[out++] = + left < middle && (right >= high || compare(from[left]!, from[right]!) <= 0) ? from[left++]! : from[right++]! + } + } + if (out === length) { + const merged = slots[2] + slots[2] = slots[1] + slots[1] = merged + slots[4] = width * 2 + out = 0 + } + slots[5] = out + slots[6] = left + slots[7] = right + return (slots[4] as number) < length +} diff --git a/packages/codemode/src/vm/prelude/prelude.ts b/packages/codemode/src/vm/prelude/prelude.ts new file mode 100644 index 00000000000..aa06268b2a8 --- /dev/null +++ b/packages/codemode/src/vm/prelude/prelude.ts @@ -0,0 +1,57 @@ +import type { Interpreter } from "../runtime/interpreter.js" +import { define, Fn, get, hidden, Obj, type Value } from "../runtime/objects.js" +import { builtinCodes } from "./bytecode.js" +import { helpers } from "./helpers.js" +import { code } from "./source.js" + +// The prelude: built-ins that call program code, written in Code Mode itself and compiled to bytecode ahead of time +// (see bytecode.ts). Each runtime makes its top-level functions as `Fn`s; their free names are the intrinsic globals, the `$` +// helpers, and the prelude's own functions (`GET_INTRINSIC`), so programs can't reach the helpers, and replacing a +// global can't change what a built-in does. A function named `Array$prototype$map` is installed as +// `Array.prototype.map`; one without a `$` is internal, and a generator makes iterator helpers. Snapshots name prelude +// functions as intrinsics and never copy them; the prelude's bytecode hash is part of the build id. Natives reach a +// prelude function with `fn` and hand their work on to it (`new Invoke(...)`). + +let compiled: ReturnType | undefined + +/** + * The prelude's bytecode, and its top-level functions' code by name. It is parsed without locations: a failure raised + * inside it has none, so it is located at the program's call instead (see `locate`). + */ +export const compiledPrelude = () => (compiled ??= builtinCodes("@prelude", code)) + +/** One runtime's prelude: what its functions' free names see, and its functions and helpers, by name. */ +export class Prelude { + readonly scope = new Map() + /** Every prelude function (installed or internal), as an object the intrinsic names reach. */ + readonly functions: Obj + /** The `$` helpers, as an object the intrinsic names reach. */ + readonly helpers: Obj + + constructor(ctx: Interpreter, globals: ReadonlyArray) { + const builtins = ctx.builtins + this.functions = new Obj(null) + this.helpers = new Obj(null) + for (const [name, value] of globals) this.scope.set(name, value) + for (const [name, value] of helpers(ctx)) { + define(this.helpers, name, value, hidden) + this.scope.set(name, value) + } + for (const [name, made] of compiledPrelude().byName) { + const path = name.split("$") + const fn = new Fn(builtins.Function, made, [], path.at(-1)) + // The prelude's generators are iterator helpers. + if (fn.generator) define(fn, "prototype", builtins.IteratorHelper, hidden) + this.scope.set(name, fn) + define(this.functions, name, fn, hidden) + if (path.length === 1) continue + const owner = path.slice(1, -1).reduce((value, key) => get(value as Obj, key), this.scope.get(path[0]!)) + define(owner as Obj, path.at(-1)!, fn, hidden) + } + } + + /** A prelude function, for a native that hands its work on to it. */ + fn(name: string): Fn { + return get(this.functions, name) as Fn + } +} diff --git a/packages/codemode/src/vm/prelude/source.ts b/packages/codemode/src/vm/prelude/source.ts new file mode 100644 index 00000000000..7992d6bf48c --- /dev/null +++ b/packages/codemode/src/vm/prelude/source.ts @@ -0,0 +1,652 @@ +// The prelude's source (see prelude.ts): Code Mode JavaScript, parsed once per process. Only top-level function +// declarations; `$` names are the privileged helpers (helpers.ts). Keep `${` and backticks out of it. +export const code = String.raw` +function arrayFromIterable(items, mapper, thisArg) { + const result = [] + let index = 0 + for (const item of items) { + result[index] = mapper === undefined ? item : $Call(mapper, thisArg, item, index) + index++ + } + return result +} + +// Array.from over an array-like with a mapper: every index is read, holes included. +function arrayFromLike(source, length, mapper, thisArg) { + const result = new Array(length) + let index = 0 + while (index < length) { + result[index] = $Call(mapper, thisArg, source[index], index) + index++ + } + return result +} + +// The callback methods Array and Uint8Array share. They fix the iteration length when they start and read elements +// live; they skip holes, but the find methods visit them as undefined. map and filter return the receiver's kind of +// collection. +function Array$prototype$map(callback, thisArg = undefined) { + return mapItems($Receiver(this, "Array", "Array.prototype.map"), callback, thisArg, "Array.map") +} +function Uint8Array$prototype$map(callback, thisArg = undefined) { + const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.map") + return $Bytes(mapItems(bytes, callback, thisArg, "Uint8Array.map"), Uint8Array.prototype, 0) +} +function mapItems(array, callback, thisArg, label) { + $Callback(callback, label) + const length = array.length + const result = new Array(length) + let index = 0 + while (index < length) { + if (index in array) result[index] = $Call(callback, thisArg, array[index], index, array) + index++ + } + return result +} + +function Array$prototype$filter(callback, thisArg = undefined) { + return filterItems($Receiver(this, "Array", "Array.prototype.filter"), callback, thisArg, "Array.filter") +} +function Uint8Array$prototype$filter(callback, thisArg = undefined) { + const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.filter") + return $Bytes(filterItems(bytes, callback, thisArg, "Uint8Array.filter"), Uint8Array.prototype, 0) +} +function filterItems(array, callback, thisArg, label) { + $Callback(callback, label) + const length = array.length + const result = [] + let count = 0 + let index = 0 + let item + while (index < length) { + if (index in array) { + item = array[index] + if ($Call(callback, thisArg, item, index, array)) result[count++] = item + } + index++ + } + return result +} + +function Array$prototype$forEach(callback, thisArg = undefined) { + forEachItem($Receiver(this, "Array", "Array.prototype.forEach"), callback, thisArg, "Array.forEach") +} +function Uint8Array$prototype$forEach(callback, thisArg = undefined) { + forEachItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.forEach"), callback, thisArg, "Uint8Array.forEach") +} +function forEachItem(array, callback, thisArg, label) { + $Callback(callback, label) + const length = array.length + let index = 0 + while (index < length) { + if (index in array) $Call(callback, thisArg, array[index], index, array) + index++ + } +} + +function Array$prototype$some(callback, thisArg = undefined) { + return testItems($Receiver(this, "Array", "Array.prototype.some"), callback, thisArg, "Array.some", false) +} +function Uint8Array$prototype$some(callback, thisArg = undefined) { + return testItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.some"), callback, thisArg, "Uint8Array.some", false) +} +function Array$prototype$every(callback, thisArg = undefined) { + return testItems($Receiver(this, "Array", "Array.prototype.every"), callback, thisArg, "Array.every", true) +} +function Uint8Array$prototype$every(callback, thisArg = undefined) { + return testItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.every"), callback, thisArg, "Uint8Array.every", true) +} +// some stops at the first truthy result, every at the first falsy one. +function testItems(array, callback, thisArg, label, every) { + $Callback(callback, label) + const length = array.length + let index = 0 + while (index < length) { + if (index in array && !$Call(callback, thisArg, array[index], index, array) === every) return !every + index++ + } + return every +} + +function Array$prototype$find(callback, thisArg = undefined) { + return findItem($Receiver(this, "Array", "Array.prototype.find"), callback, thisArg, "Array.find", false, false) +} +function Uint8Array$prototype$find(callback, thisArg = undefined) { + return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.find"), callback, thisArg, "Uint8Array.find", false, false) +} +function Array$prototype$findIndex(callback, thisArg = undefined) { + return findItem($Receiver(this, "Array", "Array.prototype.findIndex"), callback, thisArg, "Array.findIndex", false, true) +} +function Uint8Array$prototype$findIndex(callback, thisArg = undefined) { + return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findIndex"), callback, thisArg, "Uint8Array.findIndex", false, true) +} +function Array$prototype$findLast(callback, thisArg = undefined) { + return findItem($Receiver(this, "Array", "Array.prototype.findLast"), callback, thisArg, "Array.findLast", true, false) +} +function Uint8Array$prototype$findLast(callback, thisArg = undefined) { + return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findLast"), callback, thisArg, "Uint8Array.findLast", true, false) +} +function Array$prototype$findLastIndex(callback, thisArg = undefined) { + return findItem($Receiver(this, "Array", "Array.prototype.findLastIndex"), callback, thisArg, "Array.findLastIndex", true, true) +} +function Uint8Array$prototype$findLastIndex(callback, thisArg = undefined) { + return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findLastIndex"), callback, thisArg, "Uint8Array.findLastIndex", true, true) +} +function findItem(array, callback, thisArg, label, last, wantIndex) { + $Callback(callback, label) + const length = array.length + const step = last ? -1 : 1 + let index = last ? length - 1 : 0 + let item + while (index >= 0 && index < length) { + item = array[index] + if ($Call(callback, thisArg, item, index, array)) return wantIndex ? index : item + index += step + } + return wantIndex ? -1 : undefined +} + +// reduce and reduceRight take an initial value where the others take a thisArg. +function Array$prototype$reduce(callback, ...initial) { + return reduceItems($Receiver(this, "Array", "Array.prototype.reduce"), callback, initial, "Array.reduce", false) +} +function Uint8Array$prototype$reduce(callback, ...initial) { + return reduceItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.reduce"), callback, initial, "Uint8Array.reduce", false) +} +function Array$prototype$reduceRight(callback, ...initial) { + return reduceItems($Receiver(this, "Array", "Array.prototype.reduceRight"), callback, initial, "Array.reduceRight", true) +} +function Uint8Array$prototype$reduceRight(callback, ...initial) { + return reduceItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.reduceRight"), callback, initial, "Uint8Array.reduceRight", true) +} +function reduceItems(array, callback, initial, label, right) { + $Callback(callback, label) + const length = array.length + const step = right ? -1 : 1 + let index = right ? length - 1 : 0 + let accumulator + if (initial.length > 0) accumulator = initial[0] + else { + while (index >= 0 && index < length && !(index in array)) index += step + if (index < 0 || index >= length) $Throw("TypeError", label + " of an empty array with no initial value.") + accumulator = array[index] + index += step + } + while (index >= 0 && index < length) { + if (index in array) accumulator = $Call(callback, undefined, accumulator, array[index], index, array) + index += step + } + return accumulator +} + +// forEach semantics: a returned array's holes are skipped, as flattening does. +function Array$prototype$flatMap(callback, thisArg = undefined) { + const array = $Receiver(this, "Array", "Array.prototype.flatMap") + $Callback(callback, "Array.flatMap") + const length = array.length + const result = [] + let count = 0 + let index = 0 + let inner + let at + while (index < length) { + if (index in array) { + inner = $Call(callback, thisArg, array[index], index, array) + if ($IsArray(inner)) { + at = 0 + while (at < inner.length) { + if (at in inner) result[count++] = inner[at] + at++ + } + } else result[count++] = inner + } + index++ + } + return result +} + +// A sort with a comparator (see sortArray): a stable top-down merge sort that compares in the same order as the +// recursive one, running the merges (low, middle, high) the native planned in post-order over one working copy. +// Holes and undefined values sort last without being compared. +function sortWith(target, into, holes, comparator, work, undefs, merges) { + const merged = [] + let merge = 0 + let middle + let high + let left + let right + let count + let order + while (merge < merges.length) { + left = merges[merge] + middle = merges[merge + 1] + high = merges[merge + 2] + right = middle + count = 0 + while (left < middle && right < high) { + order = +$Call(comparator, undefined, work[left], work[right]) + // A NaN result counts as equal, which keeps the order stable. + if (order <= 0 || order !== order) { + merged[count] = work[left] + left++ + } else { + merged[count] = work[right] + right++ + } + count++ + } + $MergeRest(work, merged, count, left, middle, right, high, merges[merge]) + merge += 3 + } + return $Sorted(target, into, holes, work, undefs) +} + +// The default sort over more values than one native sort may take at once: native steps of bounded size (see +// $SortStep), so the run can pause between them like any loop. +function sortDefault(target, into, holes, values, undefs) { + const state = $SortState(values) + while ($SortStep(state)) {} + return $Sorted(target, into, holes, $SortResult(state, values), undefs) +} + +function Array$prototype$toLocaleString() { + const array = $Receiver(this, "Array", "Array.prototype.toLocaleString") + let result = "" + let index = 0 + let item + while (index < array.length) { + if (index > 0) result += "," + item = array[index] + if (item !== null && item !== undefined) result += String(item.toLocaleString()) + index++ + } + return result +} +// new Map, Set, WeakMap, and WeakSet from an iterable. +function fillCollection(target, items, kind) { + const pairs = kind === "Map" || kind === "WeakMap" + for (const item of items) { + if (!pairs) $Table("add", target, item) + else if (!$IsObject(item)) $Throw("TypeError", "new " + kind + "(...) expects [key, value] pairs as entry objects.") + else $Table("add", target, item[0], item[1]) + } + return target +} + +function Map$groupBy(items, callback) { + return groupBy(items, callback, "Map") +} +function Object$groupBy(items, callback) { + return groupBy(items, callback, "Object") +} +// Object.groupBy's groups have a null prototype, so group names never collide with inherited methods. +function groupBy(items, callback, kind) { + if (items === null || items === undefined) $Throw("TypeError", kind + ".groupBy expects an iterable collection.") + $Callback(callback, kind + ".groupBy") + const groups = $Table("groups", kind) + let index = 0 + let key + for (const item of items) { + key = callback(item, index++) + if (kind === "Object") key = String($Table("groupKey", key)) + $Table("group", groups, key, item) + } + return groups +} + +// getOrInsertComputed when the key is missing: the callback's result wins over anything it inserted itself. +function insertComputed(target, key, callback) { + const value = callback(key) + $Table("add", target, key, value) + return value +} + +function Map$prototype$forEach(callback, thisArg = undefined) { + collectionForEach($Receiver(this, "Map", "Map.prototype.forEach"), callback, thisArg, "Map.forEach") +} +function Set$prototype$forEach(callback, thisArg = undefined) { + collectionForEach($Receiver(this, "Set", "Set.prototype.forEach"), callback, thisArg, "Set.forEach") +} +// Visits entries live, as in JS. +function collectionForEach(target, callback, thisArg, label) { + $Callback(callback, label) + for (const entry of $Table("entries", target)) $Call(callback, thisArg, entry[1], entry[0], target) +} + +function Set$prototype$union(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.union"), other, "union") +} +function Set$prototype$intersection(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.intersection"), other, "intersection") +} +function Set$prototype$difference(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.difference"), other, "difference") +} +function Set$prototype$symmetricDifference(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.symmetricDifference"), other, "symmetricDifference") +} +function Set$prototype$isSubsetOf(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.isSubsetOf"), other, "isSubsetOf") +} +function Set$prototype$isSupersetOf(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.isSupersetOf"), other, "isSupersetOf") +} +function Set$prototype$isDisjointFrom(other) { + return setMethod($Receiver(this, "Set", "Set.prototype.isDisjointFrom"), other, "isDisjointFrom") +} +// A Set method over the other set's size, has, and keys (GetSetRecord): it either asks the other set whether it has +// each of this set's values, or takes all of the other set's values, whichever is smaller. +function setMethod(target, other, name) { + if (!$IsObject(other)) $Throw("TypeError", "Set." + name + " expects a Set-like object.") + const size = +other.size + if (size !== size) $Throw("TypeError", "Set." + name + " received a Set-like object with an invalid size.") + if (size < 0) $Throw("RangeError", "Set." + name + " received a Set-like object with a negative size.") + const has = other.has + const keys = other.keys + if (!$IsCallable(has) || !$IsCallable(keys)) $Throw("TypeError", "Set." + name + " expects callable 'has' and 'keys' methods.") + const own = $Table("size", target) + if (name === "isSubsetOf" && own > size) return false + if (name === "isSupersetOf" && own < size) return false + const result = name === "intersection" ? new Set() : name === "union" || name === "difference" || name === "symmetricDifference" ? $Table("copy", target) : undefined + const asks = name === "isSubsetOf" || ((name === "intersection" || name === "difference" || name === "isDisjointFrom") && own <= size) + let found + if (asks) { + for (const item of $Table("values", name === "difference" ? result : target)) { + found = !!$Call(has, other, item) + if (name === "intersection") { + if (found) $Table("add", result, item) + } else if (name === "difference") { + if (found) $Table("delete", result, item) + } else if (found === (name === "isDisjointFrom")) return false + } + return result === undefined ? true : result + } + const iterator = $Call(keys, other) + if (!$IsObject(iterator)) $Throw("TypeError", "Set." + name + " expects 'keys' to return an iterator object.") + for (const item of $Direct(iterator, true)) { + found = $Table("has", target, item) + if (name === "union") $Table("add", result, item) + else if (name === "intersection") { + if (found) $Table("add", result, item) + } else if (name === "difference") $Table("delete", result, item) + else if (name === "symmetricDifference") $Table(found ? "delete" : "add", result, item) + else if (found === (name === "isDisjointFrom")) return false + } + return result === undefined ? true : result +} +function Object$fromEntries(entries) { + const result = {} + for (const entry of entries) { + if (!$IsObject(entry, true)) $Throw("TypeError", "Object.fromEntries expects [key, value] entry objects.") + $Define(result, String(entry[0]), entry[1], true) + } + return result +} + +function Math$sumPrecise(items) { + const numbers = [] + for (const item of items) { + if (typeof item !== "number") $Throw("TypeError", "Math.sumPrecise expects an iterable of numbers.") + numbers[numbers.length] = item + } + return $SumPrecise(numbers) +} + +// new AggregateError: the error is made (message and cause) before its errors are read. +function aggregateError(error, errors) { + const list = [] + for (const item of errors) list[list.length] = item + return $Define(error, "errors", list, false) +} + +// A Uint8Array's bytes from an iterable: a new one with prototype into, or set into the Uint8Array into at +// offset. As in TypedArray.from, a mapper sees the original values, after the source is consumed in full. +function collectBytes(source, name, mapper, thisArg, into, offset) { + if (!$IsIterable(source)) $Throw("TypeError", name + " expects a Uint8Array, an array, or an iterable of numbers, received " + $Describe(source) + ".") + const values = [] + for (const item of source) values[values.length] = item + let index = 0 + if (mapper !== undefined) { + while (index < values.length) { + values[index] = $Call(mapper, thisArg, values[index], index) + index++ + } + } + return $Bytes(values, into, offset) +} + +// Headers and URLSearchParams from iterable [name, value] pairs, read in full as strings before any is checked; from a +// record of names to values when init is not iterable. +function readPairs(init, label, kind, proto) { + if (!$IsIterable(init)) return $Pairs(kind, proto, init, label) + const pairs = [] + let strings + for (const pair of init) { + if (!$IsIterable(pair)) $Throw("TypeError", label + " expects iterable [name, value] pairs.") + strings = [] + for (const item of pair) strings[strings.length] = String(item) + pairs[pairs.length] = strings + } + return $Pairs(kind, proto, pairs, label) +} + +// Headers and URLSearchParams forEach: the callback sees each pair as listed when it started. +function pairsForEach(target, pairs, callback, thisArg) { + let index = 0 + while (index < pairs.length) { + $Call(callback, thisArg, pairs[index][1], pairs[index][0], target) + index++ + } +} +// Iterator helpers are prelude generators, made with %IteratorHelper% as their prototype. Each is primed to its first +// yield, so a return() before the first next() still closes the source, from its finally. +function Iterator$from(value) { + if ($IsObject(value) && $Inherits(value, Iterator.prototype)) return value + return prime(wrapHelper(value, flattenable(value, true, "Iterator.from"))) +} +function* wrapHelper(source, iterator) { + let started = false + try { + yield + started = true + for (const item of iterator) yield item + } finally { + if (!started) closeIterator(source) + } +} + +function Iterator$prototype$map(mapper) { + const iterator = $Direct(this, false) + $Callback(mapper, "Iterator.prototype.map") + return prime(mapHelper(this, iterator, mapper)) +} +function* mapHelper(source, iterator, mapper) { + let started = false + try { + yield + started = true + let counter = 0 + for (const item of iterator) yield mapper(item, counter++) + } finally { + if (!started) closeIterator(source) + } +} + +function Iterator$prototype$filter(predicate) { + const iterator = $Direct(this, false) + $Callback(predicate, "Iterator.prototype.filter") + return prime(filterHelper(this, iterator, predicate)) +} +function* filterHelper(source, iterator, predicate) { + let started = false + try { + yield + started = true + let counter = 0 + for (const item of iterator) if (predicate(item, counter++)) yield item + } finally { + if (!started) closeIterator(source) + } +} + +function Iterator$prototype$take(limit) { + const iterator = $Direct(this, false) + return prime(takeHelper(this, iterator, count(limit, "take"))) +} +function* takeHelper(source, iterator, remaining) { + let started = false + try { + yield + started = true + if (remaining === 0) return closeIterator(source) + for (const item of iterator) { + remaining-- + yield item + if (remaining === 0) break + } + } finally { + if (!started) closeIterator(source) + } +} + +function Iterator$prototype$drop(limit) { + const iterator = $Direct(this, false) + return prime(dropHelper(this, iterator, count(limit, "drop"))) +} +function* dropHelper(source, iterator, remaining) { + let started = false + try { + yield + started = true + for (const item of iterator) { + if (remaining > 0) remaining-- + else yield item + } + } finally { + if (!started) closeIterator(source) + } +} + +function Iterator$prototype$flatMap(mapper) { + const iterator = $Direct(this, false) + $Callback(mapper, "Iterator.prototype.flatMap") + return prime(flatMapHelper(this, iterator, mapper)) +} +function* flatMapHelper(source, iterator, mapper) { + let started = false + try { + yield + started = true + let counter = 0 + for (const item of iterator) { + for (const inner of flattenable(mapper(item, counter++), false, "Iterator.prototype.flatMap")) yield inner + } + } finally { + if (!started) closeIterator(source) + } +} + +function prime(helper) { + helper.next() + return helper +} + +// A take or drop count: a number that is not NaN or negative. +function count(limit, name) { + const number = +limit + const integer = number === Infinity || number === -Infinity ? number : number - (number % 1) + if (number !== number || integer < 0) $Throw("RangeError", "Iterator.prototype." + name + " expects a non-negative count, received " + number + ".") + return integer +} + +// GetIteratorFlattenable: an iterable by its [Symbol.iterator], otherwise the object itself as an iterator. +function flattenable(value, strings, name) { + if (typeof value === "string" ? !strings : !$IsObject(value)) $Throw("TypeError", name + " expects an iterable or iterator, received " + $Describe(value) + ".") + const method = value[Symbol.iterator] + return $Direct(method === undefined || method === null ? value : $Call(method, value), false) +} + +function closeIterator(iterator) { + const method = iterator.return + if (method !== undefined && method !== null && !$IsObject($Call(method, iterator))) $Throw("TypeError", "Iterator return() must return an object.") +} + +function Iterator$prototype$toArray() { + const result = [] + for (const item of $Direct(this, false)) result[result.length] = item + return result +} + +function Iterator$prototype$reduce(callback, ...initial) { + const iterator = $Direct(this, false) + $Callback(callback, "Iterator.prototype.reduce") + let accumulator = initial[0] + let started = initial.length > 0 + let index = 0 + for (const item of iterator) { + if (started) accumulator = callback(accumulator, item, index) + else accumulator = item + started = true + index++ + } + if (!started) $Throw("TypeError", "Iterator.prototype.reduce of an empty iterator with no initial value.") + return accumulator +} + +function Iterator$prototype$forEach(callback) { + iteratorTest(this, callback, "forEach") +} +function Iterator$prototype$some(callback) { + return iteratorTest(this, callback, "some") +} +function Iterator$prototype$every(callback) { + return iteratorTest(this, callback, "every") +} +function Iterator$prototype$find(callback) { + return iteratorTest(this, callback, "find") +} +// Calls the callback with each value and its index until its result decides; leaving early closes the iterator. +function iteratorTest(self, callback, name) { + const iterator = $Direct(self, false) + $Callback(callback, "Iterator.prototype." + name) + let index = 0 + let result + for (const item of iterator) { + result = callback(item, index++) + if (name === "some" && result) return true + if (name === "every" && !result) return false + if (name === "find" && result) return item + } + return name === "every" ? true : name === "some" ? false : undefined +} + +// JSON.parse's reviver: children are revived first, each replacing (or, when the reviver gives undefined, removing) +// its property; then the reviver sees the value itself. +function revive(holder, key, reviver) { + const value = holder[key] + if ($IsObject(value)) { + const names = $Keys(value) + let index = 0 + let revived + while (index < names.length) { + revived = revive(value, names[index], reviver) + if (revived === undefined) delete value[names[index]] + else value[names[index]] = revived + index++ + } + } + return $Call(reviver, holder, key, value) +} + +// String replace with a callback, once the matches are found: each is the text before it and the callback's +// arguments; the text after the last one follows. +function replaceWith(matches, rest, callback) { + let result = "" + let index = 0 + while (index < matches.length) { + result += matches[index][0] + String(callback(...matches[index][1])) + index++ + } + return result + rest +} +` diff --git a/packages/codemode/src/vm/runtime/callback.ts b/packages/codemode/src/vm/runtime/callback.ts new file mode 100644 index 00000000000..a5a241f8bfe --- /dev/null +++ b/packages/codemode/src/vm/runtime/callback.ts @@ -0,0 +1,60 @@ +import { type Builtins, primitivePrototype } from "./intrinsics.js" +import { typeError } from "./model.js" +import { Callable, get, Builtin, Obj, type NativeResult, type Value } from "./objects.js" +import { typeofValue } from "./references.js" +import { Convert, type Hints, Invoke } from "./signals.js" + +/** Invoke(value, name), as the call to make: the method the value would find through its prototype. */ +export const invokeMethod = (builtins: Builtins, value: Value, name: string, label: string): Invoke => { + const target = value instanceof Obj ? value : primitivePrototype(builtins, value) + if (target === undefined) throw typeError(`${label} called on null or undefined.`) + return new Invoke(get(target, name), value, [], undefined) +} + +/** + * The conversions a built-in received, while it runs again after them: by ordinal, the order its `withPrimitives` + * calls run in. The VM resets `count` before every built-in call. + */ +export const conversions: { count: number; replay: Array | undefined> | undefined } = { + count: 0, + replay: undefined, +} + +/** + * Runs a native body on values after ToPrimitive, in order, with one hint for all positions or one per position. + * Primitive values skip the conversion entirely. Otherwise the built-in asks the VM for the conversions (they + * call program code, so they run as prelude code) and runs again from the start once they are done; this call then + * receives them. So everything a built-in does before its last conversion must be repeatable: it reads, checks, and + * throws, but changes nothing. State the body needs as it was before the conversions (a Date's time) goes in `values`. + * With `coerce`, each value is also converted to a number or string (by its hint) before the next one is. + */ +export const withPrimitives = ( + hints: Hints, + values: Array, + body: (primitives: Array) => NativeResult, + coerce = false, +): NativeResult => { + const ordinal = conversions.count++ + const replayed = conversions.replay?.[ordinal] + if (replayed !== undefined) return body(replayed) + if (!values.some((value) => value instanceof Obj)) return body(values) + return new Convert(hints, values, ordinal, coerce) +} + +/** Checks a callback argument: the callable it is, or the TypeError for one a callback site does not admit. */ +export const requireCallback = (callback: Value, name: string): Callable => { + if (isSupportedCallback(callback)) return callback + if (typeofValue(callback) === "function") { + throw typeError( + `${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, + ) + } + throw typeError(`${name} expects a function callback.`) +} + +// The single acceptance list for callbacks: collections, sort, string replacers, +// Array.from mappers, and promise reactions all admit exactly these callables. +// Admission means dispatchable, not necessarily invocable: new-requiring +// constructors pass the gate and throw a TypeError on call, like JS. +export const isSupportedCallback = (value: Value): value is Callable => + value instanceof Callable && !(value instanceof Builtin && !value.callback) diff --git a/packages/codemode/src/vm/runtime/driver.ts b/packages/codemode/src/vm/runtime/driver.ts new file mode 100644 index 00000000000..c57838e92da --- /dev/null +++ b/packages/codemode/src/vm/runtime/driver.ts @@ -0,0 +1,146 @@ +import { Cause, type Context, Effect, Exit, Fiber } from "effect" +import type { HostCall } from "../scheduler.js" +import type { ToolRuntime } from "../../tool-runtime.js" +import { locate } from "./errors.js" +import { PendingThrow } from "./model.js" +import type { Vm } from "../vm.js" +import { abrupt, type Completion, Interruption, normal } from "./signals.js" + +/** + * Runs the VM (see `Vm`) in Effect: the only place program execution meets the host. It calls `vm.run` + * until the program ends, and does what each stop asks: yields to the Effect scheduler for a slice, and starts the host + * calls the VM queued, as fibers with the caller's services, reporting each outcome back with `vm.complete`. + * Fibers live only here, by host call id, and never become part of the VM's state. + * + * The VM and tool bridge can be swapped for restored ones between stops (the round-trip test mode does); a fiber's + * outcome goes to whichever VM is current, by host call id. + */ +export class Driver { + private readonly fibers = new Map>() + private wake: (() => void) | undefined + private stopping = false + // Blocking host work interrupted on its own, which interrupts the execution. + private interrupted: Cause.Cause | undefined + /** Called at every slice and host stop, before queued calls start: the round-trip test mode swaps the VM. */ + checkpoint: Effect.Effect | undefined + + constructor( + public vm: Vm, + public tools: ToolRuntime, + /** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */ + private readonly services: Context.Context, + ) {} + + /** + * Runs the VM until the program's body completes, then returns how it ended. Host work it started may still be + * running; `interrupt` stops it. Host work interrupted on its own that reaches the program's body, or blocking host + * work that is interrupted, interrupts the execution. + */ + run(): Effect.Effect { + const self = this + return Effect.gen(function* () { + while (true) { + if (self.interrupted !== undefined) return yield* Effect.failCause(self.interrupted) + const stop = self.vm.run() + if (stop.kind === "slice") { + if (self.checkpoint !== undefined) yield* self.checkpoint + yield* Effect.yieldNow + continue + } + if (stop.kind === "host") { + if (stop.calls.length === 0) { + yield* self.settled + continue + } + if (self.checkpoint !== undefined) yield* self.checkpoint + for (const handed of stop.calls) self.launch(self.vm.call(handed.id) ?? handed) + continue + } + if (stop.kind === "stuck") { + return abrupt( + new PendingThrow( + "Error", + "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.", + stop.node, + ), + ) + } + const outcome = stop.completion + if (!outcome.ok && outcome.error instanceof Interruption) return yield* Effect.failCause(outcome.error.cause) + return outcome + } + }) + } + + /** Whether the run holds live host values: an extension call in progress. */ + get holdsHostValues(): boolean { + return this.vm.calls().some((call) => call.request.kind === "extension") || this.vm.activeExtension !== undefined + } + + /** Stops host work still running, waiting for its cleanup. */ + interrupt(): Effect.Effect { + const self = this + return Effect.gen(function* () { + self.stopping = true + while (self.fibers.size > 0) yield* Fiber.interruptAll([...self.fibers.values()]) + }) + } + + // Starts host work now; its outcome goes back to the VM: a blocking call's continues the job that made it, a + // concurrent call's waits in the inbox and settles its promise in a later job, in completion order. + private launch(call: HostCall): void { + call.status = "running" + const request = call.request + if (request.kind === "tool") return this.fork(call.id, this.tools.execute(request.path, request.args)) + if (request.kind === "search") return this.fork(call.id, this.tools.search(request.args)) + // An extension's work, made by the extension bridge with the program's services in its requirements. + return this.fork(call.id, this.vm.ctx.extensionWork!(request.call) as Effect.Effect) + } + + // Forks `work` with the caller's services; it starts synchronously, so admission and hooks happen before the VM + // runs another job. + private fork(id: number, work: Effect.Effect): void { + const fiber = Effect.runForkWith(this.services)(work) + this.fibers.set(id, fiber) + fiber.addObserver((exit) => { + if (this.fibers.get(id) === fiber) this.fibers.delete(id) + // Interrupted to end the run: nothing is left to settle. + if (this.stopping) return + const call = this.vm.call(id) + if (call === undefined) return + if (call.blocking) { + // Blocking work interrupted on its own interrupts the execution. + if (Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) this.interrupted = exit.cause + else this.vm.complete(id, Exit.isSuccess(exit) ? normal(exit.value) : abrupt(Cause.squash(exit.cause))) + this.wake?.() + return + } + this.vm.complete( + id, + Exit.isSuccess(exit) + ? normal(exit.value) + : abrupt( + Cause.hasInterruptsOnly(exit.cause) + ? new Interruption(exit.cause) + : Cause.hasDies(exit.cause) + ? locate(Cause.squash(exit.cause), call.site) + : Cause.squash(exit.cause), + ), + ) + this.wake?.() + }) + } + + private get settled(): Effect.Effect { + return Effect.callback((resume) => { + if (this.vm.ready || this.interrupted !== undefined) return resume(Effect.void) + this.wake = () => { + this.wake = undefined + resume(Effect.void) + } + return Effect.sync(() => { + this.wake = undefined + }) + }) + } +} diff --git a/packages/codemode/src/vm/runtime/errors.ts b/packages/codemode/src/vm/runtime/errors.ts new file mode 100644 index 00000000000..83748ec9a1b --- /dev/null +++ b/packages/codemode/src/vm/runtime/errors.ts @@ -0,0 +1,208 @@ +import type { Diagnostic } from "../../codemode.js" +import { ToolError } from "../../tool-error.js" +import { ToolRuntimeError } from "../../tool-runtime.js" +import { type AstNode, formatLocation, PendingThrow, Throw, sourceLocation } from "./model.js" +import { containsRuntimeReference } from "./references.js" +import { createErrorValue, type ErrorType, isErrorType } from "./intrinsics.js" +import { constructor, methods, prototypeFrom, receiver } from "./native.js" +import { + type Callable, + define, + get, + has, + hidden, + type Native, + Arr, + ErrorObj, + Obj, + coerceToString, + type Value, +} from "./objects.js" +import type { Interpreter } from "./interpreter.js" +import { Invoke } from "./signals.js" +import { withPrimitives } from "./callback.js" +import { formatValue } from "../stdlib/console.js" +import { inPrelude } from "./source.js" + +/** + * A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it + * into text would run its own methods. + */ +export const errorMessage = (error: Error): string => { + const message: unknown = error.message + if (typeof message === "string") return message + if (message === undefined || typeof message === "function" || (typeof message === "object" && message !== null)) { + return "" + } + return String(message) +} + +/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */ +export const errorType = (error: Error): ErrorType => { + const name: unknown = error.name + return typeof name === "string" && isErrorType(name) ? name : "Error" +} + +export const normalizeError = (error: unknown): Diagnostic => { + if (error instanceof PendingThrow) { + const location = error.node?.loc ? sourceLocation(error.node) : undefined + return { + kind: error.kind, + message: `${error.type}: ${error.message}${formatLocation(location)}`, + ...(location ? { location } : {}), + ...(error.suggestions ? { suggestions: error.suggestions } : {}), + } + } + + if (error instanceof ToolRuntimeError) { + return { + kind: error.kind, + message: error.message, + ...(error.suggestions.length > 0 ? { suggestions: error.suggestions } : {}), + } + } + + if (error instanceof ToolError) { + return { kind: "ToolFailure", message: error.message } + } + + if (error instanceof Throw) { + const value = error.value + if (value instanceof ErrorObj) { + return value.failure ? normalizeError(value.failure) : { kind: "ExecutionFailure", message: errorToString(value) } + } + let message: string + if (containsRuntimeReference(value)) { + // Never expose runtime reference internals through thrown values. + message = "a non-data value" + } else if (typeof value === "string") { + message = value + } else { + message = formatValue(value) + } + return { kind: "ExecutionFailure", message: `Uncaught: ${message}` } + } + + if (error instanceof RangeError && /call stack|recursion/i.test(errorMessage(error))) { + return { + kind: "ExecutionFailure", + message: "Execution exceeded the maximum nesting depth.", + } + } + + if (error instanceof Error) { + return { + kind: error.name === "SyntaxError" ? "ParseError" : "ExecutionFailure", + message: errorMessage(error), + } + } + + return { + kind: "ExecutionFailure", + message: String(error), + } +} + +/** + * Gives a failure the source location of the expression that raised it, keeping the first one attached. Host errors + * that escape a built-in become the equivalent program error here. + */ +export const locate = (error: unknown, node?: AstNode): unknown => { + if (error instanceof PendingThrow) { + if ((error.node === undefined || inPrelude(error.node)) && node) error.node = node + return error + } + if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) { + return new PendingThrow(errorType(error), errorMessage(error), node) + } + return error +} + +/** The program value a handler receives for a failure; one failure always yields the same value. */ +export const materialize = (ctx: Interpreter, thrown: unknown): Value => { + if (thrown instanceof Throw) return thrown.value + const builtins = ctx.builtins + if (thrown instanceof PendingThrow) { + if (thrown.value === undefined) { + thrown.value = createErrorValue(builtins[thrown.type], thrown.message) + thrown.value.failure = thrown + } + return thrown.value + } + return createErrorValue( + builtins[thrown instanceof Error ? errorType(thrown) : "Error"], + normalizeError(thrown).message, + ) +} + +/** Error.prototype.toString: `name: message`, omitting whichever side is empty. */ +const errorToString = (self: Obj): string => { + const name = get(self, "name") + const message = get(self, "message") + const shownName = name === undefined ? "Error" : coerceToString(name) + const shownMessage = message === undefined ? "" : coerceToString(message) + if (shownMessage === "") return shownName + if (shownName === "") return shownMessage + return `${shownName}: ${shownMessage}` +} + +export const createAggregateErrorValue = ( + ctx: Interpreter, + errors: Array, + message: string, + proto: Obj = ctx.builtins.AggregateError, +) => { + const value = createErrorValue(proto, message) + define(value, "errors", new Arr(ctx.builtins.Array, errors), { ...hidden }) + return value +} + +// ES2022 `new Error(message, { cause })`: installed only when the options object has the property at all. +const withCause = (created: ErrorObj, options: Value) => { + if (options instanceof Obj && has(options, "cause")) define(created, "cause", get(options, "cause"), hidden) + return created +} + +/** An error constructor such as `Error` or `TypeError`; callable with or without `new`, like JS. */ +export const errorGlobal = (type: ErrorType, ctx: Interpreter) => { + const builtins = ctx.builtins + const prototype = builtins[type] + const construct = (args: Array, newTarget: Callable) => { + const proto = prototypeFrom(newTarget, prototype) + if (type === "AggregateError") { + const message = args[1] + return withPrimitives( + "string", + message === undefined ? [] : [message], + ([text]) => { + const created = withCause(createErrorValue(proto, message === undefined ? "" : coerceToString(text)), args[2]) + return new Invoke(ctx.prelude.fn("aggregateError"), undefined, [created, args[0]], undefined) + }, + true, + ) + } + const message = args[0] + return withPrimitives( + "string", + message === undefined ? [] : [message], + ([text]) => withCause(createErrorValue(proto, message === undefined ? undefined : coerceToString(text)), args[1]), + true, + ) + } + const ctor: Native = constructor(builtins, prototype, { + name: type, + length: type === "AggregateError" ? 2 : 1, + call: (_, args) => construct(args, ctor), + construct, + }) + if (type === "Error") { + methods(builtins, prototype, [ + ["toString", 0, (thisValue) => errorToString(receiver(Obj, thisValue, "Error.prototype.toString"))], + ]) + methods(builtins, ctor, [["isError", 1, (_, args) => args[0] instanceof ErrorObj]]) + return ctor + } + // Derived constructors extend Error itself, so its statics are inherited. The globals table creates Error first. + ctor.proto = get(builtins.Error, "constructor") as Obj + return ctor +} diff --git a/packages/codemode/src/vm/runtime/execute.ts b/packages/codemode/src/vm/runtime/execute.ts new file mode 100644 index 00000000000..9b21abccbe6 --- /dev/null +++ b/packages/codemode/src/vm/runtime/execute.ts @@ -0,0 +1,339 @@ +import { parse, type Program } from "acorn" +import { Cause, Effect, Option, Schema } from "effect" +import type { DataValue, Diagnostic, ResolvedExecutionLimits, Result } from "../../codemode.js" +import type { Extension } from "../../extension.js" +import { fromJson } from "../data.js" +import { fromBytes, SnapshotError, toBytes } from "../snapshot/snapshot.js" +import { ToolRuntime } from "../../tool-runtime.js" +import { normalizeError } from "./errors.js" +import { extensionGlobals } from "./extensions.js" +import { createBuiltins } from "./intrinsics.js" +import { Interpreter } from "./interpreter.js" +import type { Value } from "./objects.js" +import { formatLocation, sourceLocation } from "./model.js" +import { Driver } from "./driver.js" +import { Interruption } from "./signals.js" +import { Source } from "./source.js" +import type { Vm } from "../vm.js" + +type Globals = (ctx: Interpreter) => ReadonlyArray + +/** How a runtime runs programs: its tools, limits, hooks, and host globals. */ +type Setup = { + readonly prepared: ToolRuntime.Prepared + readonly limits: ResolvedExecutionLimits + readonly hooks: ToolRuntime.Hooks + /** The host's globals, made again for a restored run; see `Interpreter`. */ + readonly globals: Globals | undefined + /** Whether the host globals can hand the program values with no data form (extensions can). */ + readonly lossy: boolean +} + +/** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */ +export const execute = ( + code: string, + prepared: ToolRuntime.Prepared, + limits: ResolvedExecutionLimits, + hooks: ToolRuntime.Hooks, + extensions: ReadonlyArray, + bindings: Readonly> | undefined, +): Effect.Effect => + executeProgram( + code, + prepared, + limits, + hooks, + extensions.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, extensions), + bindings === undefined + ? undefined + : (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)] as const), + extensions.length > 0, + ) + +export const executeProgram = ( + code: string, + prepared: ToolRuntime.Prepared, + limits: ResolvedExecutionLimits, + hooks: ToolRuntime.Hooks, + globals?: Globals, + bindings?: Globals, + lossy = false, +): Effect.Effect => + Effect.suspend(() => { + if (code.trim().length === 0) { + return Effect.succeed({ + ok: false, + error: { kind: "ParseError", message: "Code cannot be empty." }, + toolCalls: [], + }) + } + const parsed = parseProgram(code) + if (!parsed.ok) return Effect.succeed({ ok: false, error: parsed.error, toolCalls: [] }) + const setup: Setup = { prepared, limits, hooks, globals, lossy } + const ctx = instantiate(setup, bindings) + ctx.vm.start(parsed.source) + return new Running(setup, ctx).run() + }) + +// Sources restored recently, by text: the AST is never changed, so runs restored from the same program share it. +const restoredSources = new Map() + +const restoreInto = (saved: unknown, vm: Vm): void => { + vm.restore(saved, { + parse: (code) => { + const known = restoredSources.get(code) + if (known !== undefined) return known + const parsed = parseProgram(code) + if (!parsed.ok) throw new SnapshotError(`A saved source no longer parses: ${parsed.error.message}`) + if (restoredSources.size >= 16) restoredSources.delete(restoredSources.keys().next().value!) + restoredSources.set(code, parsed.source) + return parsed.source + }, + interruption: () => new Interruption(Cause.interrupt()), + }) +} + +// A new runtime: intrinsics, the host globals, the program's bindings, a fresh tool bridge, and its VM. +const instantiate = (setup: Setup, bindings?: Globals): Interpreter => { + const ctx = new Interpreter({ + tools: ToolRuntime.make(setup.prepared, setup.limits.maxToolCalls, setup.hooks), + builtins: createBuiltins(), + logs: [], + globals: setup.globals, + bindings, + }) + if (roundTrip) ctx.vm.tripEvery = ROUND_TRIP_TICKS + return ctx +} + +/** + * The round-trip test mode: at every host stop and every slice, which come every few loop back-edges and calls, a run's + * state is saved to bytes and restored into a brand-new runtime, which continues the run. It proves the VM's state is + * plain data at every step. Internal: only the test suite turns it on (`bun run test:roundtrip`). + */ +let roundTrip = false +export const setRoundTrip = (enabled: boolean) => { + roundTrip = enabled +} +const ROUND_TRIP_TICKS = 2 +const ROUND_TRIP_MAX = 200 + +/** One run: the runtime it is in now (a round trip swaps it). */ +class Running { + private trips = 0 + // Checkpoints to let pass before the next round trip, so a large state is not saved at every one. + private skip = 0 + + constructor( + private readonly setup: Setup, + private ctx: Interpreter, + ) {} + + run(): Effect.Effect { + const self = this + return Effect.gen(function* () { + const driver = new Driver(self.ctx.vm, self.ctx.tools, yield* Effect.context()) + if (roundTrip) driver.checkpoint = Effect.sync(() => self.trip(driver)) + return yield* self.execute(driver) + }) + } + + private execute(driver: Driver): Effect.Effect { + const self = this + const limits = this.setup.limits + const logged = () => { + const logs = self.ctx.logs + return logs.length > 0 ? { logs: [...logs] } : {} + } + const toolCalls = () => self.ctx.tools.calls + const failed = (error: Diagnostic): Result => ({ ok: false, error, ...logged(), toolCalls: toolCalls() }) + // Set only after copy-out so timeouts cannot report invalid values as completed. + let returned: { value: DataValue; warnings: () => Array } | undefined + + const base = Effect.gen(function* () { + const outcome = yield* driver.run() + if (!outcome.ok) return failed(normalizeError(outcome.error)) + const result = (outcome.value as DataValue | undefined) ?? null + const vm = self.ctx.vm + returned = { value: result, warnings: () => vm.diagnostics() } + // Host work the program left running is interrupted, not awaited. + yield* driver.interrupt() + const warnings = vm.diagnostics() + return { + ok: true, + value: result, + ...(warnings.length > 0 ? { warnings } : {}), + ...logged(), + toolCalls: toolCalls(), + } satisfies Result + }).pipe(Effect.ensuring(Effect.suspend(() => driver.interrupt()))) + const timeoutMs = limits.timeoutMs + const operation = + timeoutMs === undefined + ? base + : base.pipe( + Effect.timeoutOrElse({ + duration: timeoutMs, + orElse: () => + Effect.sync((): Result => { + if (returned === undefined) { + return failed({ kind: "TimeoutExceeded", message: `Execution timed out after ${timeoutMs}ms.` }) + } + // Keep the timeout warning first so truncation preserves it. + return { + ok: true, + value: returned.value, + warnings: [ + { + kind: "TimeoutExceeded", + message: `The program returned, but background work was still running at the ${timeoutMs}ms timeout and was interrupted. Await all started promises.`, + }, + ...returned.warnings(), + ], + ...logged(), + toolCalls: toolCalls(), + } satisfies Result + }), + }), + ) + + return operation.pipe( + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeed(failed(normalizeError(Cause.squash(cause)))), + ), + Effect.map((result) => + limits.maxOutputBytes === undefined ? result : boundOutput(result, limits.maxOutputBytes), + ), + ) + } + + // The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime. States that + // hold live host values (an extension call in flight, a function an extension returned) can't round-trip, so they + // are skipped; a lost value anywhere else is a codec gap and fails the run. + private trip(driver: Driver): void { + if (this.trips >= ROUND_TRIP_MAX) { + this.ctx.vm.tripEvery = 0 + return + } + if (this.skip > 0) { + this.skip-- + return + } + if (driver.holdsHostValues) return + this.trips++ + const saved = this.ctx.vm.capture({ strict: !this.setup.lossy }) + const bytes = toBytes(saved.snapshot) + this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000) + if (saved.lost.length > 0) return + const fresh = instantiate(this.setup) + restoreInto(fromBytes(bytes), fresh.vm) + this.ctx = fresh + driver.vm = fresh.vm + driver.tools = fresh.tools + } +} + +export const parseProgram = ( + code: string, +): { ok: true; program: Program; source: Source } | { ok: false; error: Diagnostic } => { + try { + const program = parse(code, { + ecmaVersion: "latest", + sourceType: "script", + allowReturnOutsideFunction: true, + allowAwaitOutsideFunction: true, + // Imports bind globals by module name; scripts keep sloppy-mode semantics, which module source would make strict. + allowImportExportEverywhere: true, + locations: true, + }) + const exported = program.body.find((statement) => statement.type.startsWith("Export")) + if (exported) { + const location = sourceLocation(exported) + return { + ok: false, + error: { + kind: "ParseError", + message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`, + location, + }, + } + } + return { ok: true, program, source: new Source("main", code, program) } + } catch (error) { + // Only acorn's own syntax errors become a located ParseError; anything else keeps the generic failure path. + const acorn = error instanceof SyntaxError ? decodeAcornError(error) : Option.none() + if (Option.isNone(acorn)) throw error + return { ok: false, error: parseDiagnostic(acorn.value) } + } +} + +// Acorn appends ` (line:column)` to its messages; its lines are 1-based and its columns 0-based. +const AcornError = Schema.Struct({ + message: Schema.String, + loc: Schema.Struct({ line: Schema.Number, column: Schema.Number }), +}) +const decodeAcornError = Schema.decodeUnknownOption(AcornError) + +const parseDiagnostic = (error: typeof AcornError.Type): Diagnostic => { + const location = { line: error.loc.line, column: error.loc.column + 1 } + const reason = error.message.replace(/ \(\d+:\d+\)$/, "") + const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.` + return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location } +} + +const utf8ByteLength = (value: string): number => new TextEncoder().encode(value).byteLength + +// Drop a replacement character produced by truncating inside a UTF-8 sequence. +const utf8Truncate = (value: string, maxBytes: number): string => { + const bytes = new TextEncoder().encode(value) + if (bytes.byteLength <= maxBytes) return value + const text = new TextDecoder("utf-8").decode(bytes.slice(0, Math.max(0, maxBytes))) + return text.endsWith("\uFFFD") ? text.slice(0, -1) : text +} + +// The longest prefix of `items` whose sizes add up to at most `budget` bytes. +const fitting = (items: ReadonlyArray, budget: number, size: (item: T) => number): Array => { + const kept: Array = [] + let used = 0 + for (const item of items) { + used += size(item) + if (used > budget) break + kept.push(item) + } + return kept +} + +// Warnings have a separate budget so result data cannot starve diagnostics. +const boundOutput = (result: Result, maxOutputBytes: number): Result => { + const serialized = result.ok ? (JSON.stringify(result.value) ?? "null") : "" + const valueBytes = result.ok ? utf8ByteLength(serialized) : 0 + const valueTruncated = valueBytes > maxOutputBytes + const value: DataValue = !result.ok + ? null + : valueTruncated + ? `${utf8Truncate(serialized, maxOutputBytes)} [result truncated: ${valueBytes} bytes exceeds the ${maxOutputBytes}-byte output limit; return a smaller value]` + : result.value + + const warnings = result.ok ? (result.warnings ?? []) : [] + const keptWarnings = fitting(warnings, maxOutputBytes, (warning) => utf8ByteLength(JSON.stringify(warning)) + 1) + const warningsCut = keptWarnings.length < warnings.length + if (warningsCut) { + keptWarnings.push({ + kind: "Truncated", + message: `${warnings.length - keptWarnings.length} additional warnings omitted by the output limit.`, + }) + } + + const logs = result.logs ?? [] + const logBudget = Math.max(0, maxOutputBytes - Math.min(valueBytes, maxOutputBytes)) + const kept = fitting(logs, logBudget, (line) => utf8ByteLength(line) + 1) + const logsCut = kept.length < logs.length + if (logsCut) kept.push(`[logs truncated: showing ${kept.length} of ${logs.length} lines]`) + + if (!valueTruncated && !warningsCut && !logsCut) return result + const warningsPart = keptWarnings.length > 0 ? { warnings: keptWarnings } : {} + const logsPart = kept.length > 0 ? { logs: kept } : {} + return result.ok + ? { ok: true, value, ...warningsPart, ...logsPart, truncated: true, toolCalls: result.toolCalls } + : { ok: false, error: result.error, ...logsPart, truncated: true, toolCalls: result.toolCalls } +} diff --git a/packages/codemode/src/vm/runtime/extensions.ts b/packages/codemode/src/vm/runtime/extensions.ts new file mode 100644 index 00000000000..22a830b1026 --- /dev/null +++ b/packages/codemode/src/vm/runtime/extensions.ts @@ -0,0 +1,294 @@ +import { Effect } from "effect" +import { type Extension, isAsync } from "../../extension.js" +import { hooked } from "../../tool-runtime.js" +import { errorMessage, errorType } from "./errors.js" +import type { Interpreter } from "./interpreter.js" +import { createErrorValue } from "./intrinsics.js" +import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js" +import { type AstNode, PendingThrow, rangeError, Throw, typeError } from "./model.js" +import { native } from "./native.js" +import { + define, + get, + has, + hidden, + keys, + Arr, + Bytes, + DateObj, + ErrorObj, + HeadersObj, + PromiseObj, + MapObj, + Obj, + RegExpObj, + SetObj, + URLObj, + URLSearchParamsObj, + coerceToString, + type Value, +} from "./objects.js" +import { describeValue, isOpaque } from "./references.js" +import { Push } from "./signals.js" +import type { ExtensionRequest } from "../scheduler.js" + +/** + * The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in + * wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise + * or Effect becomes a program promise. + */ +export const extensionGlobals = ( + ctx: Interpreter, + extensions: ReadonlyArray, +): ReadonlyArray => { + const builtins = ctx.builtins + + const toHost = (value: Value, label: string, depth = 0, seen = new Set()): unknown => { + if (depth > MAX_VALUE_DEPTH) throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`) + if (isPrimitive(value)) return value + if (!(value instanceof Obj) || isOpaque(value)) { + throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to an extension.`) + } + if (seen.has(value)) throw typeError(`${label} contains a circular value.`) + seen.add(value) + const next = (item: Value) => toHost(item, label, depth + 1, seen) + if (value instanceof ErrorObj) { + const name = coerceToString(get(value, "name")) + const message = get(value, "message") + const text = message === undefined ? "" : coerceToString(message) + const copied = + name === "AggregateError" ? new AggregateError([], text) : new (hostErrors.get(name) ?? Error)(text) + for (const key of new Set(["cause", ...keys(value)])) { + if (uncrossed.has(key) || !has(value, key)) continue + const item = crossing(() => next(get(value, key))) + if (item === left) continue + Object.defineProperty(copied, key, { + value: item, + writable: true, + configurable: true, + enumerable: key !== "cause", + }) + } + seen.delete(value) + return copied + } + const copied = value.toHost(next) + seen.delete(value) + return copied + } + + // `extension` is set under an extension's global object, whose functions are entry points that fire the hooks. + const fromHost = (value: unknown, label: string, depth = 0, seen = new Set(), extension?: string): Value => { + if (depth > MAX_VALUE_DEPTH) throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`) + if (isPrimitive(value)) return value + if (typeof value === "function") return wrap(value, label, extension) + if (value !== null && typeof value === "object") { + const next = (item: unknown, path: string) => fromHost(item, path, depth + 1, seen, extension) + // Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own + // method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read. + if (value instanceof Date) return new DateObj(builtins.Date, Date.prototype.getTime.call(value)) + if (value instanceof RegExp) return new RegExpObj(builtins.RegExp, value.source, value.flags) + if (value instanceof Uint8Array) return new Bytes(builtins.Uint8Array, new Uint8Array(value)) + if (value instanceof ArrayBuffer) { + const view = new Uint8Array(value) + // Only a real buffer has bytes to view; anything else would be read as array-like and lose them silently. + if (view.buffer !== value) + throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`) + return new Bytes(builtins.Uint8Array, new Uint8Array(view)) + } + if (value instanceof Error) { + if (seen.has(value)) throw typeError(`${label} produced a circular value.`) + seen.add(value) + const copied = createErrorValue(builtins[errorType(value)], errorMessage(value)) + const fields = value as unknown as Record + for (const key of new Set(["cause", ...Object.keys(value)])) { + if (uncrossed.has(key) || !(key in value)) continue + // Read once: a getter could pass the check and then hand back a function. + const field = fields[key] + if (typeof field === "function") continue + const item = crossing(() => next(field, `${label}.${key}`)) + if (item === left) continue + define(copied, key, item, key === "cause" ? hidden : undefined) + } + seen.delete(value) + return copied + } + if (value instanceof URL) return new URLObj(builtins.URL, builtins.URLSearchParams, new URL(value.href)) + if (value instanceof URLSearchParams) { + return new URLSearchParamsObj(builtins.URLSearchParams, new URLSearchParams(value)) + } + if (value instanceof Headers) return new HeadersObj(builtins.Headers, new Headers(value)) + if (value instanceof Map) { + const wrapped = new MapObj(builtins.Map) + for (const [key, item] of value) wrapped.map.set(next(key, label), next(item, label)) + return wrapped + } + if (value instanceof Set) { + const wrapped = new SetObj(builtins.Set) + for (const item of value) wrapped.set.add(next(item, label)) + return wrapped + } + if (seen.has(value)) throw typeError(`${label} produced a circular value.`) + seen.add(value) + if (Array.isArray(value)) { + const length = value.length + if (length > MAX_ARRAY_LENGTH) { + throw rangeError(`${label} produced an array of ${length} items; at most ${MAX_ARRAY_LENGTH} can cross.`) + } + // Filled in order so the copy stays a dense array; holes stay holes, as a copy made with `map` would leave them. + const items: Array = [] + for (let index = 0; index < length; index += 1) { + if (index in value) items[index] = next(value[index], `${label}[${index}]`) + } + items.length = length + seen.delete(value) + return new Arr(builtins.Array, items) + } + const prototype = Object.getPrototypeOf(value) + if (prototype === Object.prototype || prototype === null) { + const copied = new Obj(builtins.Object) + for (const [key, item] of Object.entries(value)) define(copied, key, next(item, `${label}.${key}`)) + seen.delete(value) + return copied + } + } + throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`) + } + + // A host function as a program function. Arguments cross in; for a global or a global object's function, the + // `extension.before` hook runs before the host function is called, so a call it denies never reaches the host. The + // host's result, or what it threw, crosses out so the program catches a copy, and `extension.after` sees how the + // call ended. Functions inside results are part of a value's API and skip the hooks. + const wrap = (value: Function, label: string, extension: string | undefined) => { + // The host function's own `name` and `length` can be anything, even getters: each is read once, and only text + // and a count cross. + const name: unknown = value.name + const length: unknown = value.length + return native(builtins, { + runtime: "extension", + name: typeof name === "string" ? name : "", + length: typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0, + call: (_, values, site) => { + const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`)) + const invocation = extension === undefined ? undefined : { extension, name: label, args } + // A denied call fails the way the global would: an async global rejects its promise, a sync one throws. + if (isAsync(value)) { + return pending(ctx, { stage: "run", label, invocation, host: value, args, threw: false }, site) + } + return new Push("extension call", [value, args, label, invocation]) + }, + }) + } + + // The Effect each step of an extension call runs as, with the program's services in its requirements. + ctx.extensionWork = (request: ExtensionRequest) => { + const label = request.label + const call = request.invocation + const cross = (reason: unknown) => new Throw(fromHost(reason, label)) + const hooks = ctx.tools.hooks + const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross)) + if (request.stage === "before") return before ?? Effect.void + const after = call && hooks["extension.after"] + const settle = (effect: Effect.Effect) => + (after ? hooked(call, undefined, after, effect) : effect).pipe( + Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }), + ) + if (request.stage === "settle") return settle(outcome(request.host, request.threw)) + return Effect.andThen( + before ?? Effect.void, + settle(Effect.suspend(() => outcome(...callHost(request.host as Function, request.args)))), + ) + } + + return extensions.flatMap((extension) => + Object.entries(extension.globals).map( + ([name, value]) => [name, fromHost(value, name, 0, new Set(), extension.name)] as const, + ), + ) +} + +// An asynchronous host call: the program holds a promise that settles when the host's work does. +const pending = (ctx: Interpreter, call: ExtensionRequest, site: AstNode | undefined) => { + const resolving = ctx.vm.scheduler.resolving(ctx.vm.scheduler.newPromise(), site) + ctx.vm.hosts.host({ kind: "extension", call }, { label: call.label, result: "value", resolving, site }) + return resolving.promise +} + +/** + * Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for + * a result that settles later (an Effect or a Promise), or the request that settles what it returned or threw now. + */ +export const startExtensionCall = ( + ctx: Interpreter, + host: Function, + args: ReadonlyArray, + call: Pick, + site: AstNode | undefined, +): PromiseObj | ExtensionRequest => { + const [result, threw] = callHost(host, args) + const request = { stage: "settle", ...call, host: result, args, threw } as const + if (!threw && (Effect.isEffect(result) || result instanceof Promise)) return pending(ctx, request, site) + return request +} + +/** Calls a host function: what it returned, or what it threw. */ +const callHost = (value: Function, args: ReadonlyArray): [result: unknown, threw: boolean] => { + try { + return [value.apply(undefined, args), false] + } catch (reason) { + return [reason, true] + } +} + +/** + * What a host function's call settles as: a returned Effect or Promise settles later; a returned value or a throw + * settles now. + */ +const outcome = (result: unknown, threw: boolean): Effect.Effect => { + if (threw) return Effect.fail(result) + // Extension.Services adds an Effect global's requirements to R. + if (Effect.isEffect(result)) return result as Effect.Effect + if (result instanceof Promise) return Effect.tryPromise({ try: () => result, catch: (reason) => reason }) + return Effect.succeed(result) +} + +/** + * An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`, + * `syscall`, and `path`. `stack` stays on its own side, `message` crosses once as text, and no field may shadow an + * Error method. A field that cannot cross (a socket, a handle, a function) is left behind so the error itself always + * arrives. + */ +const uncrossed = new Set(["stack", "message", "constructor", "toString", "__proto__"]) +const left = Symbol("left behind") +const crossing = (convert: () => T): T | typeof left => { + try { + return convert() + } catch (reason) { + if (reason instanceof PendingThrow) return left + throw reason + } +} + +const hostErrors = new Map([ + ["TypeError", TypeError], + ["RangeError", RangeError], + ["SyntaxError", SyntaxError], + ["ReferenceError", ReferenceError], + ["EvalError", EvalError], + ["URIError", URIError], +]) + +// The primitives the interpreter operates on; symbols and BigInts are not among them. +const isPrimitive = (value: unknown): value is string | number | boolean | null | undefined => + value === null || + value === undefined || + typeof value === "string" || + typeof value === "number" || + typeof value === "boolean" + +const describeHost = (value: unknown): string => { + if (typeof value !== "object" || value === null) return `a ${typeof value}` + const name = (value as { constructor?: { name?: string } }).constructor?.name + if (name === undefined || name === "") return "an object" + return `${/^[AEIO]/.test(name) ? "an" : "a"} ${name}` +} diff --git a/packages/codemode/src/vm/runtime/generators.ts b/packages/codemode/src/vm/runtime/generators.ts new file mode 100644 index 00000000000..4031adc2616 --- /dev/null +++ b/packages/codemode/src/vm/runtime/generators.ts @@ -0,0 +1,56 @@ +import { fn, type Method, methods, receiver } from "./native.js" +import { AsyncIteratorSymbol, type GeneratorRequestKind, IteratorSymbol, typeError } from "./model.js" +import { define, hidden, GeneratorObj } from "./objects.js" +import type { Interpreter } from "./interpreter.js" +import { Push } from "./signals.js" +import { AsyncFromSyncIterator, generatorRequest, generatorStep } from "../generators.js" + +/** + * `next`/`return`/`throw` on the generator prototypes (async generators answer with promises), on iterator helpers, + * and on the async-from-sync iterator. A generator's suspended body runs on the caller's job stack. + */ +export const generatorGlobals = (ctx: Interpreter): void => { + const builtins = ctx.builtins + const sync = (kind: GeneratorRequestKind, owner = "Generator.prototype"): Method => [ + kind, + 1, + (thisValue, args) => { + const method = `${owner}.${kind}` + const generator = receiver(GeneratorObj, thisValue, method) + if (generator.asynchronous) throw typeError(`${method} called on incompatible receiver an async generator.`) + return generatorStep(ctx.vm, generator, kind, args[0]) + }, + ] + const async = (kind: GeneratorRequestKind): Method => [ + kind, + 1, + (thisValue, args) => generatorRequest(ctx.vm, thisValue, kind, args[0]), + ] + const adapter = (kind: GeneratorRequestKind): Method => [ + kind, + 1, + (thisValue, args) => + new Push("AsyncFromSyncIterator", [ + receiver(AsyncFromSyncIterator, thisValue, `AsyncFromSyncIterator.${kind}`), + kind, + args, + ]), + ] + methods(builtins, builtins.Generator, [sync("next"), sync("return"), sync("throw")]) + // Iterator helpers are the prelude's generators (see prelude.ts). + methods(builtins, builtins.IteratorHelper, [sync("next", "%IteratorHelper%"), sync("return", "%IteratorHelper%")]) + methods(builtins, builtins.AsyncGenerator, [async("next"), async("return"), async("throw")]) + methods(builtins, builtins.AsyncFromSyncIterator, [adapter("next"), adapter("return"), adapter("throw")]) + define( + builtins.Iterator, + IteratorSymbol, + fn(builtins, "[Symbol.iterator]", 0, (thisValue) => thisValue), + hidden, + ) + define( + builtins.AsyncIterator, + AsyncIteratorSymbol, + fn(builtins, "[Symbol.asyncIterator]", 0, (thisValue) => thisValue), + hidden, + ) +} diff --git a/packages/codemode/src/vm/runtime/globals.ts b/packages/codemode/src/vm/runtime/globals.ts new file mode 100644 index 00000000000..6e5c3a31893 --- /dev/null +++ b/packages/codemode/src/vm/runtime/globals.ts @@ -0,0 +1,143 @@ +import { Arr, BoundFn, Callable, coerceToInteger, get, Obj, type Value } from "./objects.js" +import { arrayGlobal } from "../stdlib/array.js" +import { textDecoderGlobal, textEncoderGlobal, uint8ArrayGlobal } from "../stdlib/bytes.js" +import { mapGlobal, setGlobal, weakMapGlobal, weakSetGlobal } from "../stdlib/collections.js" +import { consoleGlobal } from "../stdlib/console.js" +import { dateGlobal } from "../stdlib/date.js" +import { jsonGlobal } from "../stdlib/json.js" +import { mathGlobal } from "../stdlib/math.js" +import { booleanGlobal, numberGlobal } from "../stdlib/number.js" +import { objectGlobal } from "../stdlib/object.js" +import { regexpGlobal } from "../stdlib/regexp.js" +import { stringGlobal } from "../stdlib/string.js" +import { uriGlobal, urlGlobal, urlSearchParamsGlobal } from "../stdlib/url.js" +import { headersGlobal } from "../stdlib/headers.js" +import { iteratorGlobals } from "../stdlib/iterator.js" +import { coercion } from "../stdlib/value.js" +import { base64Global, cryptoGlobal, structuredCloneGlobal } from "../stdlib/web.js" +import { ToolReference } from "../../tool-runtime.js" +import { errorGlobal } from "./errors.js" +import { errorTypes } from "./intrinsics.js" +import { constants, constructor, methods, native, receiver } from "./native.js" +import { AsyncIteratorSymbol, IteratorSymbol, typeError } from "./model.js" +import { checkArrayLength } from "./limits.js" +import { generatorGlobals } from "./generators.js" +import { promiseGlobal } from "./promises.js" +import type { Interpreter } from "./interpreter.js" +import { Invoke } from "./signals.js" + +// Function.prototype.constructor exists so `fn.constructor === Function` holds; dynamic code is unsupported. +const functionGlobal = (ctx: Interpreter) => { + const reject = () => { + throw typeError("The Function constructor is not supported; write the function inline.") + } + const target = (thisValue: Value, method: string) => receiver(Callable, thisValue, `Function.prototype.${method}`) + methods(ctx.builtins, ctx.builtins.Function, [ + ["call", 1, (thisValue, args, site) => new Invoke(target(thisValue, "call"), args[0], args.slice(1), site)], + [ + "apply", + 2, + (thisValue, args, site) => new Invoke(target(thisValue, "apply"), args[0], listFromArrayLike(args[1]), site), + ], + [ + "bind", + 1, + (thisValue, args) => new BoundFn(ctx.builtins.Function, target(thisValue, "bind"), args[0], args.slice(1)), + ], + ]) + return constructor(ctx.builtins, ctx.builtins.Function, { + name: "Function", + length: 1, + call: reject, + construct: reject, + }) +} + +// CreateListFromArrayLike: `apply` reads `length` and the indexed properties of any object. +const listFromArrayLike = (value: Value): Array => { + if (value === undefined || value === null) return [] + if (value instanceof Arr) return [...value.items] + if (!(value instanceof Obj)) throw typeError("Function.prototype.apply expects an array-like argument list.") + const length = Math.max(0, coerceToInteger(get(value, "length"))) + checkArrayLength(length) + return Array.from({ length }, (_, index) => get(value, String(index))) +} + +const symbolGlobal = (ctx: Interpreter) => { + const symbol = native(ctx.builtins, { + name: "Symbol", + call: () => { + throw typeError("Symbol is not callable; only Symbol.asyncIterator and Symbol.iterator are available.") + }, + callback: false, + }) + constants(symbol, { asyncIterator: AsyncIteratorSymbol, iterator: IteratorSymbol }) + return symbol +} + +type Factory = (ctx: Interpreter) => Value + +// A table rather than a list so the names are known before any runtime exists. +const table: Record = { + tools: () => new ToolReference([]), + search: (ctx) => native(ctx.builtins, { name: "search", call: (_, args) => ctx.search(args), callback: false }), + require: (ctx) => + native(ctx.builtins, { + name: "require", + length: 1, + call: (_, args, site) => { + const specifier = args[0] + if (typeof specifier !== "string") throw typeError("require expects a module name string.") + return ctx.module(specifier, site) + }, + }), + undefined: () => undefined, + NaN: () => NaN, + Infinity: () => Infinity, + Object: (ctx) => objectGlobal(ctx), + Function: (ctx) => functionGlobal(ctx), + Array: (ctx) => arrayGlobal(ctx), + Math: (ctx) => mathGlobal(ctx), + JSON: (ctx) => jsonGlobal(ctx), + console: (ctx) => consoleGlobal(ctx), + Promise: (ctx) => promiseGlobal(ctx), + Symbol: (ctx) => symbolGlobal(ctx), + Iterator: (ctx) => iteratorGlobals(ctx), + Number: (ctx) => numberGlobal(ctx), + String: (ctx) => stringGlobal(ctx), + Boolean: (ctx) => booleanGlobal(ctx), + parseInt: (ctx) => coercion(ctx, "parseInt", 2), + parseFloat: (ctx) => coercion(ctx, "parseFloat"), + isFinite: (ctx) => coercion(ctx, "isFinite"), + isNaN: (ctx) => coercion(ctx, "isNaN"), + Date: (ctx) => dateGlobal(ctx), + RegExp: (ctx) => regexpGlobal(ctx), + Map: (ctx) => mapGlobal(ctx), + Set: (ctx) => setGlobal(ctx), + WeakMap: (ctx) => weakMapGlobal(ctx), + WeakSet: (ctx) => weakSetGlobal(ctx), + URL: (ctx) => urlGlobal(ctx), + URLSearchParams: (ctx) => urlSearchParamsGlobal(ctx), + Headers: (ctx) => headersGlobal(ctx), + Uint8Array: (ctx) => uint8ArrayGlobal(ctx), + TextEncoder: (ctx) => textEncoderGlobal(ctx), + TextDecoder: (ctx) => textDecoderGlobal(ctx), + encodeURI: (ctx) => uriGlobal(ctx, "encodeURI"), + encodeURIComponent: (ctx) => uriGlobal(ctx, "encodeURIComponent"), + decodeURI: (ctx) => uriGlobal(ctx, "decodeURI"), + decodeURIComponent: (ctx) => uriGlobal(ctx, "decodeURIComponent"), + atob: (ctx) => base64Global(ctx, "atob"), + btoa: (ctx) => base64Global(ctx, "btoa"), + crypto: (ctx) => cryptoGlobal(ctx), + structuredClone: (ctx) => structuredCloneGlobal(ctx), + ...Object.fromEntries(errorTypes.map((type) => [type, (ctx: Interpreter) => errorGlobal(type, ctx)])), +} + +/** Names bound in every program before extensions apply. */ +export const globalNames: ReadonlySet = new Set(Object.keys(table)) + +/** The immutable global bindings of every program, in declaration order. */ +export const globals = (ctx: Interpreter): ReadonlyArray => { + generatorGlobals(ctx) + return Object.entries(table).map(([name, factory]) => [name, factory(ctx)] as const) +} diff --git a/packages/codemode/src/vm/runtime/interpreter.ts b/packages/codemode/src/vm/runtime/interpreter.ts new file mode 100644 index 00000000000..03851cf64e3 --- /dev/null +++ b/packages/codemode/src/vm/runtime/interpreter.ts @@ -0,0 +1,116 @@ +import type { ToolRuntime } from "../../tool-runtime.js" +import type { ExtensionRequest } from "../scheduler.js" +import { Vm } from "../vm.js" +import { globals } from "./globals.js" +import type { Builtins } from "./intrinsics.js" +import { IntrinsicNames } from "./intrinsic-names.js" +import { type AstNode, type Binding, PendingThrow } from "./model.js" +import { moduleName, resolveModule } from "./modules.js" +import { define, entries, makingIntrinsics, nativeRule, Obj, type Value } from "./objects.js" +import { Prelude } from "../prelude/prelude.js" +import { Push } from "./signals.js" +import type { NodeId, Source } from "./source.js" + +/** One program execution: the tool bridge, the VM that runs it, captured logs, and the global scope built once. */ +export class Interpreter { + readonly tools: ToolRuntime + readonly builtins: Builtins + readonly logs: Array + /** Runs program code, and keeps the job queue, promises, and host calls the program waits on. */ + readonly vm: Vm + /** Names for this runtime's intrinsics, taken before any program code can change a built-in. */ + readonly intrinsics: IntrinsicNames + /** The built-ins written in Code Mode, and the scope they close over. */ + readonly prelude: Prelude + /** Makes the Effect an extension function's host work runs as; set by the extension bridge. */ + extensionWork: ((request: ExtensionRequest) => unknown) | undefined + /** The parsed sources this execution runs, by id, so a `NodeId` resolves to its node. */ + readonly sources = new Map() + /** The global bindings: the intrinsics, the host's globals, and the program's bindings. */ + readonly globalScope: Map + private readonly modules = new Map() + private readonly namespaces = new Map() + + constructor(options: { + readonly tools: ToolRuntime + readonly builtins: Builtins + readonly logs?: Array + /** The host's globals (extensions, a test harness): made again, the same way, when a saved run is restored. */ + readonly globals?: (ctx: Interpreter) => ReadonlyArray + /** Data bound for this program, part of its saved state; host globals of the same name win. */ + readonly bindings?: (ctx: Interpreter) => ReadonlyArray + }) { + this.tools = options.tools + this.builtins = options.builtins + this.logs = options.logs ?? [] + // Program code has no receiver: top-level `this` is undefined, as in a module. + this.globalScope = new Map([["this", { value: undefined }]]) + // Everything made from here to the host globals is an intrinsic, with a static id (see `IntrinsicNames`). + const [[intrinsics, prelude], made] = makingIntrinsics(Object.values(this.builtins), () => { + const intrinsics = globals(this) + return [intrinsics, new Prelude(this as Interpreter, intrinsics)] as const + }) + this.prelude = prelude + const hosts = options.globals?.(this) ?? [] + this.intrinsics = new IntrinsicNames( + made, + { + globals: intrinsics, + builtins: this.builtins, + internals: [ + ["Prelude", this.prelude.functions], + ["PreludeHelpers", this.prelude.helpers], + ], + }, + hosts, + ) + for (const [name, value] of [...intrinsics, ...(options.bindings?.(this) ?? []), ...hosts]) { + this.globalScope.set(name, { value }) + } + nativeRule.sealed.add(this.builtins.Function) + // The VM never runs host work itself, so it does not need the services that work runs with. + this.vm = new Vm(this as Interpreter) + } + + /** A module, as `require` returns it and a default import binds it: a global, by its Node module name. */ + module(specifier: string, site?: AstNode): Value { + const name = moduleName(specifier) + const cached = this.modules.get(name) + if (cached !== undefined) return cached + const module = resolveModule(name, (global) => this.globalScope.get(global)?.value) + if (module === undefined) + throw new PendingThrow( + "Error", + `Cannot import "${specifier}": it isn't available in Code Mode, which can't load packages or files. Use the provided globals, or call tools through \`tools\`.`, + site, + ) + this.modules.set(name, module) + return module + } + + /** + * What `import * as` and `import()` see: the module's own properties, and the module itself as `default`, the way + * Node presents a CommonJS module. + */ + namespace(specifier: string, site?: AstNode): Obj { + const module = this.module(specifier, site) + const name = moduleName(specifier) + const cached = this.namespaces.get(name) + if (cached) return cached + const namespace = new Obj(null) + if (module instanceof Obj) for (const [key, value] of entries(module)) define(namespace, key, value) + define(namespace, "default", module) + this.namespaces.set(name, namespace) + return namespace + } + + /** The node a `NodeId` names, in one of this execution's sources. */ + node(id: NodeId): AstNode | undefined { + return this.sources.get(id.source)?.node(id) + } + + /** The built-in search: its result is usable directly, and can also be awaited or chained like a Promise. */ + search(args: Array): Push { + return new Push("search", [args]) + } +} diff --git a/packages/codemode/src/vm/runtime/intrinsic-names.ts b/packages/codemode/src/vm/runtime/intrinsic-names.ts new file mode 100644 index 00000000000..eb8c8a7093d --- /dev/null +++ b/packages/codemode/src/vm/runtime/intrinsic-names.ts @@ -0,0 +1,188 @@ +import type { Builtins } from "./intrinsics.js" +import { AsyncIteratorSymbol, IteratorSymbol } from "./model.js" +import { Arr, Fn, Native, Obj, type Value } from "./objects.js" + +/** + * Deterministic names for one runtime's intrinsic objects, so saved state can refer to a built-in by name instead of + * copying it, and whether each one changed since the runtime was made. + * + * - **Static ids.** Every object made with the intrinsics (`createBuiltins`, the globals, the prelude) has an id: its + * creation index, which is the same in every runtime because making them is deterministic (see `makingIntrinsics`). + * The ids are named once per process, by a breadth-first walk of the first runtime made, before any program runs: + * from the global names (`Array.prototype.map`), then `%Name%` for built-in prototypes the walk cannot reach + * (`%Generator%`) and the prelude's internals, and what is reachable from them. The first path found names a value. + * No later runtime walks anything. + * - **`@host` names.** The host's globals (extensions, a test harness) differ between runtimes, so each runtime names + * them itself, `@name`, by a walk over only the objects they reach that have no static id. Host lists are small. + * - **Dirty bits.** `Obj.intrinsic` is the id plus one while the object is as made, negated once it changes. Every + * write to an object's properties, prototype, extensibility or elements marks it (`changed` in objects.ts), and so + * does restoring it from a saved record, so a run suspended twice keeps what changed before the first suspend. + */ +export class IntrinsicNames { + private readonly hostValues: Array = [] + private readonly hostNames: Array = [] + private readonly hostIndex = new Map() + private named: Array | undefined + + constructor( + /** The objects made with the intrinsics, by id. */ + private readonly made: ReadonlyArray, + roots: Roots, + hosts: ReadonlyArray = [], + ) { + made.forEach((value, id) => { + value.intrinsic = id + 1 + }) + table ??= walk(made, roots) + if (made.length !== table.count) { + throw new Error(`This runtime made ${made.length} intrinsics, and the first one made ${table.count}.`) + } + const visit = (value: Value, name: () => string) => { + if (!(value instanceof Obj) || this.hostIndex.has(value) || this.isStatic(value)) return + this.hostIndex.set(value, this.hostValues.length) + this.hostValues.push(value) + this.hostNames.push(name()) + value.intrinsic = table!.count + this.hostValues.length + } + let next = 0 + for (const [name, value] of hosts) { + visit(value, () => `@${name}`) + for (; next < this.hostValues.length; next++) { + const item = this.hostValues[next]! + const base = this.hostNames[next]! + for (const [key, slot] of item.props) if ("value" in slot) visit(slot.value, () => member(base, key)) + if (item.proto !== null) visit(item.proto, () => member(base, prototypeKey)) + } + } + } + + /** The named intrinsics, in name order (see `all`). */ + get values(): ReadonlyArray { + return (this.named ??= [...table!.order.map((id) => this.made[id]!), ...this.hostValues]) + } + + /** The name of an intrinsic, or undefined for any other object. */ + nameOf(value: Obj): string | undefined { + if (this.isStatic(value)) { + const at = table!.position[Math.abs(value.intrinsic) - 1]! + return at < 0 ? undefined : table!.names[at] + } + const host = this.hostIndex.get(value) + return host === undefined ? undefined : this.hostNames[host] + } + + /** The intrinsic a name refers to. */ + valueOf(name: string): Obj | undefined { + const id = staticIds().get(name) + if (id !== undefined) return this.made[id] + const host = this.hostNames.indexOf(name) + return host < 0 ? undefined : this.hostValues[host] + } + + /** Every name: the static ones in walk order, then the host globals'. */ + all(): ReadonlyArray { + return [...table!.names, ...this.hostNames] + } + + /** The host globals' names, `@name`, in walk order. */ + hosts(): ReadonlyArray { + return this.hostNames + } + + /** + * The named intrinsics a saved state has to record: those changed since the runtime was made, and host values of + * classes whose state is not their properties (an extension's Map), which count as changed. + */ + changed(): Array { + return this.values.filter((value) => !unchanged(value)) + } + + private isStatic(value: Obj): boolean { + const id = Math.abs(value.intrinsic) - 1 + return id >= 0 && id < this.made.length && this.made[id] === value + } +} + +/** Whether a named object is still as the runtime made it, so its name alone restores it. */ +export const unchanged = (value: Obj): boolean => { + if (value.intrinsic <= 0) return false + const ctor = value.constructor + return ctor === Obj || ctor === Arr || ctor === Native || ctor === Fn +} + +/** The static names, in walk order, once a runtime has been made in this process. */ +export const staticNames = (): ReadonlyArray => { + if (table === undefined) throw new Error("No runtime has been made yet, so the intrinsics have no names.") + return table.names +} + +/** Where the walk that names the static intrinsics starts. */ +export type Roots = { + readonly globals: ReadonlyArray + readonly builtins: Builtins + /** Internal objects no global reaches (the prelude's functions and helpers), named like the builtins. */ + readonly internals: ReadonlyArray +} + +type Table = { + readonly count: number + /** Static ids in walk order, and their names. */ + readonly order: ReadonlyArray + readonly names: ReadonlyArray + /** By id: the position in `order`, or -1 for an object the walk does not reach. */ + readonly position: ReadonlyArray +} + +let table: Table | undefined +let ids: Map | undefined + +const staticIds = () => (ids ??= new Map(table!.names.map((name, at) => [name, table!.order[at]!]))) + +const walk = (made: ReadonlyArray, roots: Roots): Table => { + const order: Array = [] + const paths: Array = [] + const position = made.map(() => -1) + const visit = (value: Value, path: string | readonly [number, string | symbol]) => { + if (!(value instanceof Obj)) return + const id = value.intrinsic - 1 + if (id < 0 || made[id] !== value) { + throw new Error(`The intrinsic at ${describe(path)} was not made with this runtime's intrinsics.`) + } + if (position[id] !== -1) return + position[id] = order.length + order.push(id) + paths.push(path) + } + const describe = (path: string | readonly [number, string | symbol]): string => + typeof path === "string" ? path : member(describe(paths[path[0]]!), path[1]) + let next = 0 + const drain = () => { + for (; next < order.length; next++) { + const item = made[order[next]!]! + for (const [key, slot] of item.props) if ("value" in slot) visit(slot.value, [next, key]) + if (item.proto !== null) visit(item.proto, [next, prototypeKey]) + } + } + for (const [name, value] of roots.globals) visit(value, name) + drain() + for (const [name, value] of [...Object.entries(roots.builtins), ...roots.internals]) { + visit(value, `%${name}%`) + drain() + } + const names: Array = [] + for (const path of paths) names.push(typeof path === "string" ? path : member(names[path[0]]!, path[1])) + return { count: made.length, order, names, position } +} + +const prototypeKey: unique symbol = Symbol("[[Prototype]]") + +const symbolNames = new Map([ + [IteratorSymbol, "[Symbol.iterator]"], + [AsyncIteratorSymbol, "[Symbol.asyncIterator]"], + [prototypeKey, ".[[Prototype]]"], +]) + +const member = (base: string, key: string | symbol): string => { + if (typeof key === "symbol") return `${base}${symbolNames.get(key) ?? `[${String(key)}]`}` + return /^[A-Za-z_$][\w$]*$/.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]` +} diff --git a/packages/codemode/src/vm/runtime/intrinsics.ts b/packages/codemode/src/vm/runtime/intrinsics.ts new file mode 100644 index 00000000000..24845254681 --- /dev/null +++ b/packages/codemode/src/vm/runtime/intrinsics.ts @@ -0,0 +1,121 @@ +import { define, hidden, Native, Arr, ErrorObj, Obj, type Value } from "./objects.js" + +export const errorTypes = [ + "Error", + "TypeError", + "RangeError", + "SyntaxError", + "ReferenceError", + "EvalError", + "URIError", + "AggregateError", +] as const + +export type ErrorType = (typeof errorTypes)[number] + +export const isErrorType = (name: string): name is ErrorType => (errorTypes as ReadonlyArray).includes(name) + +const builtins = [ + "Object", + "Function", + "Array", + "String", + "Number", + "Boolean", + "Date", + "RegExp", + "Map", + "Set", + "WeakMap", + "WeakSet", + "URL", + "URLSearchParams", + "Headers", + "Uint8Array", + "TextEncoder", + "TextDecoder", + "Promise", + "PromiseLike", + "Iterator", + "IteratorHelper", + "AsyncIterator", + "Generator", + "AsyncGenerator", + "AsyncFromSyncIterator", +] as const + +/** + * The built-in prototype objects of one runtime, allocated empty in dependency order. The globals populate them + * and attach their constructors when the runtime is built. + */ +export type Builtins = Readonly> + +export const createErrorValue = (prototype: Obj, message: string | undefined): ErrorObj => { + const value = new ErrorObj(prototype) + if (message !== undefined) define(value, "message", message, hidden) + return value +} + +/** The prototype a primitive reads its methods from without being boxed; none for null, undefined, and symbols. */ +export const primitivePrototype = (builtins: Builtins, value: Value): Obj | undefined => { + if (typeof value === "string") return builtins.String + if (typeof value === "number") return builtins.Number + if (typeof value === "boolean") return builtins.Boolean + return undefined +} + +export const createBuiltins = (): Builtins => { + const object = new Obj(null) + // Function.prototype is itself callable and returns undefined. + const fn = new Native(object, { name: "", call: () => undefined }) + const plain = () => new Obj(object) + const error = plain() + define(error, "name", "Error", hidden) + define(error, "message", "", hidden) + const derived = (type: ErrorType) => { + const proto = new Obj(error) + define(proto, "name", type, hidden) + define(proto, "message", "", hidden) + return proto + } + const iterator = plain() + const asyncIterator = plain() + return { + Object: object, + Function: fn, + Array: new Arr(object), + String: plain(), + Number: plain(), + Boolean: plain(), + Date: plain(), + RegExp: plain(), + Map: plain(), + Set: plain(), + WeakMap: plain(), + WeakSet: plain(), + URL: plain(), + URLSearchParams: plain(), + Headers: plain(), + Uint8Array: plain(), + TextEncoder: plain(), + TextDecoder: plain(), + Promise: plain(), + /** What a synchronous result that can also be awaited inherits: then/catch/finally over a plain copy. */ + PromiseLike: plain(), + Iterator: iterator, + IteratorHelper: new Obj(iterator), + AsyncIterator: asyncIterator, + Generator: new Obj(iterator), + AsyncGenerator: new Obj(asyncIterator), + /** CreateAsyncFromSyncIterator's objects; programs never see them. */ + AsyncFromSyncIterator: new Obj(asyncIterator), + Error: error, + TypeError: derived("TypeError"), + RangeError: derived("RangeError"), + SyntaxError: derived("SyntaxError"), + ReferenceError: derived("ReferenceError"), + EvalError: derived("EvalError"), + URIError: derived("URIError"), + AggregateError: derived("AggregateError"), + } +} diff --git a/packages/codemode/src/vm/runtime/limits.ts b/packages/codemode/src/vm/runtime/limits.ts new file mode 100644 index 00000000000..18bada5f301 --- /dev/null +++ b/packages/codemode/src/vm/runtime/limits.ts @@ -0,0 +1,21 @@ +import { rangeError } from "./model.js" + +// The timeout only fires between interpreter steps, so a single built-in must not be able to materialize an +// unbounded value. These bound what one call may build; programs cannot reach such sizes any other way. + +/** Longest string a built-in or operator may produce. */ +export const MAX_STRING_LENGTH = 1 << 24 +/** Longest array a built-in may create or grow to. */ +export const MAX_ARRAY_LENGTH = 10_000_000 +/** Most promises that may be pending at once. */ +export const MAX_PENDING_PROMISES = 10_000 +/** Deepest nesting a value may have when it crosses to or from the host. */ +export const MAX_VALUE_DEPTH = 32 + +export const checkStringLength = (length: number): void => { + if (length > MAX_STRING_LENGTH) throw rangeError("Invalid string length") +} + +export const checkArrayLength = (length: number): void => { + if (length > MAX_ARRAY_LENGTH) throw rangeError("Invalid array length") +} diff --git a/packages/codemode/src/vm/runtime/model.ts b/packages/codemode/src/vm/runtime/model.ts new file mode 100644 index 00000000000..bbdccc41ccf --- /dev/null +++ b/packages/codemode/src/vm/runtime/model.ts @@ -0,0 +1,78 @@ +import type { Node } from "acorn" +import type { ErrorType } from "./intrinsics.js" +import type { DiagnosticKind } from "../../codemode.js" +import type { ErrorObj, Value } from "./objects.js" + +/** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */ +export type AstNode = Node + +/** A global binding: globals are immutable per run, and top-level declarations are the program's own locals. */ +export type Binding = { readonly value: Value } + +export type GeneratorRequestKind = "next" | "return" | "throw" + +export const AsyncIteratorSymbol: unique symbol = Symbol("codemode.async-iterator") +export const IteratorSymbol: unique symbol = Symbol("codemode.iterator") + +export class Throw { + constructor(readonly value: Value) {} +} + +/** + * A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object + * the first time a handler observes it, so every observer of the same failure sees the same value. + */ +export class PendingThrow { + node?: AstNode + value?: ErrorObj + + constructor( + /** The JS error class a program sees when it catches this failure. */ + readonly type: ErrorType, + readonly message: string, + node?: AstNode, + readonly kind: DiagnosticKind = "ExecutionFailure", + readonly suggestions?: ReadonlyArray, + ) { + if (node) this.node = node + } +} + +/** + * A host value a restored run cannot have back: a function an extension returned, or what a host function returned, + * which have no data form. Using it fails with `lostError`. + */ +export class Lost { + constructor(readonly description: string) {} +} + +/** What using a value lost to a suspend throws. */ +export const lostError = (description: string, node?: AstNode): PendingThrow => + new PendingThrow( + "TypeError", + `${description} came from the host and was lost when the run was suspended; get it again after resuming.`, + node, + ) + +const failure = (type: ErrorType, kind?: DiagnosticKind) => (message: string, node?: AstNode) => + new PendingThrow(type, message, node, kind) + +export const typeError = failure("TypeError") +export const invalidData = failure("TypeError", "InvalidDataValue") +export const rangeError = failure("RangeError") +export const referenceError = failure("ReferenceError") +export const syntaxError = failure("SyntaxError") +export const uriError = failure("URIError") + +// Orient the agent rather than enumerate JavaScript; interpreter-support.md is the full matrix. +export const supportedSyntaxMessage = + "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead." + +// Acorn lines are 1-based and its columns are 0-based. Diagnostics use 1-based columns of the submitted source. +export const sourceLocation = (node: AstNode): { readonly line: number; readonly column: number } => ({ + line: node.loc?.start.line ?? 1, + column: (node.loc?.start.column ?? 0) + 1, +}) + +export const formatLocation = (location?: { readonly line: number; readonly column: number }): string => + location ? ` (line ${location.line}, col ${location.column})` : "" diff --git a/packages/codemode/src/vm/runtime/modules.ts b/packages/codemode/src/vm/runtime/modules.ts new file mode 100644 index 00000000000..d68704d0f1b --- /dev/null +++ b/packages/codemode/src/vm/runtime/modules.ts @@ -0,0 +1,35 @@ +import { define, get, Obj, type Value } from "./objects.js" + +/** + * What `import` and `require` load: a global, by the Node module name that provides it. `fs` loads the `fs` global + * and `crypto` the `crypto` global; the names below come from globals with different names. A module that would be + * empty isn't available. + */ +const aliases: Record Value) => Value> = { + "fs/promises": (global) => { + const fs = global("fs") + return fs instanceof Obj ? get(fs, "promises") : undefined + }, + url: (global) => pick(global, ["URL", "URLSearchParams"]), + util: (global) => pick(global, ["TextEncoder", "TextDecoder"]), + buffer: (global) => pick(global, ["Buffer", "atob", "btoa"]), +} + +/** `node:fs` names the same module as `fs`. */ +export const moduleName = (specifier: string) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier) + +export const resolveModule = (specifier: string, global: (name: string) => Value): Value => { + const name = moduleName(specifier) + return global(name) ?? aliases[name]?.(global) +} + +function pick(global: (name: string) => Value, names: ReadonlyArray): Value { + const found = names.flatMap((name) => { + const value = global(name) + return value === undefined ? [] : [[name, value] as const] + }) + if (found.length === 0) return undefined + const module = new Obj(null) + for (const [name, value] of found) define(module, name, value) + return module +} diff --git a/packages/codemode/src/vm/runtime/native.ts b/packages/codemode/src/vm/runtime/native.ts new file mode 100644 index 00000000000..421405c10c1 --- /dev/null +++ b/packages/codemode/src/vm/runtime/native.ts @@ -0,0 +1,74 @@ +import type { Builtins } from "./intrinsics.js" +import { typeError } from "./model.js" +import { + type Callable, + define, + frozen, + hidden, + Native, + type NativeCall, + type NativeOptions, + Obj, + positionIterator, + type PositionSource, + type Value, +} from "./objects.js" +import { describeValue } from "./references.js" + +/** A native function body: a value, a thrown `PendingThrow`, or what the VM carries out as the call. */ +export type Impl = NativeCall + +export const native = (builtins: Builtins, options: NativeOptions): Native => new Native(builtins.Function, options) + +export const fn = (builtins: Builtins, name: string, length: number, impl: Impl): Native => + native(builtins, { name, length, call: impl }) + +export type Method = readonly [name: string, length: number, impl: Impl] + +export const methods = (builtins: Builtins, target: Obj, table: ReadonlyArray): void => { + for (const [name, length, impl] of table) define(target, name, fn(builtins, name, length, impl), hidden) +} + +/** `keys`, `values`, and `entries`: built-in iterators over the receiver `self` checks. */ +export const iteratorMethods = ( + builtins: Builtins, + self: (thisValue: Value, name: string) => PositionSource, +): Array => + (["keys", "values", "entries"] as const).map((kind) => [ + kind, + 0, + (thisValue) => positionIterator(builtins, self(thisValue, kind), kind), + ]) + +export const constants = (target: Obj, table: Record): void => { + for (const [name, value] of Object.entries(table)) define(target, name, value, frozen) +} + +/** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */ +export const constructor = (builtins: Builtins, proto: Obj, options: NativeOptions): Native => { + const ctor = native(builtins, options) + define(ctor, "prototype", proto, frozen) + define(proto, "constructor", ctor, hidden) + return ctor +} + +/** The `call` of a constructor that JS requires to be invoked with `new`. */ +export const requiresNew = (name: string) => (): never => { + throw typeError(`Constructor ${name} requires 'new'.`) +} + +/** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */ +export const prototypeFrom = (newTarget: Callable, fallback: Obj): Obj => { + const proto = newTarget.props.get("prototype") + return proto !== undefined && "value" in proto && proto.value instanceof Obj ? proto.value : fallback +} + +/** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */ +export const receiver = ( + cls: abstract new (...args: never) => T, + thisValue: Value, + method: string, +): T => { + if (thisValue instanceof cls) return thisValue + throw typeError(`${method} called on incompatible receiver ${describeValue(thisValue)}.`) +} diff --git a/packages/codemode/src/vm/runtime/objects.ts b/packages/codemode/src/vm/runtime/objects.ts new file mode 100644 index 00000000000..cef4d7ae3af --- /dev/null +++ b/packages/codemode/src/vm/runtime/objects.ts @@ -0,0 +1,944 @@ +import { ToolReference } from "../../tool-runtime.js" +import type { Builtins } from "./intrinsics.js" +import { checkArrayLength } from "./limits.js" +import { + type AstNode, + AsyncIteratorSymbol, + IteratorSymbol, + type PendingThrow, + rangeError, + Throw, + typeError, +} from "./model.js" +import { type Convert, type GeneratorRequest, Invoke, type Push, type Resume } from "./signals.js" +import type { Code } from "../code.js" +import { type Cell, CodeFlags } from "../opcodes.js" +import type { Reaction, Resolving } from "../scheduler.js" +import type { Frame } from "../vm.js" +import { OrderedTable, type TableCursor } from "./table.js" + +/** Property attributes, as in a JS property descriptor. */ +export type Attributes = { + readonly writable: boolean + readonly enumerable: boolean + readonly configurable: boolean +} + +export type Getter = (receiver: Value) => Value +export type Setter = (receiver: Value, value: Value) => void + +/** One own property: a data slot or a native accessor pair. */ +export type Slot = + | { value: Value; writable: boolean; enumerable: boolean; configurable: boolean } + | { get: Getter | undefined; set: Setter | undefined; enumerable: boolean; configurable: boolean } + +// The objects made while a runtime's intrinsics are being made, in creation order; see `makingIntrinsics`. +let making: Array | undefined + +/** + * Runs `make`, which makes a runtime's intrinsics, and returns every object made meanwhile, in creation order, after + * `first` (the built-in prototypes, made before). An object's index in the list is its static intrinsic id. + */ +export const makingIntrinsics = (first: ReadonlyArray, make: () => A): readonly [A, Array] => { + const made = [...first] + making = made + try { + return [make(), made] + } finally { + making = undefined + } +} + +/** Marks an intrinsic as changed by the program, so saving a run records it; any other object is left alone. */ +export const changed = (target: Obj): void => { + if (target.intrinsic > 0) target.intrinsic = -target.intrinsic +} + +/** Ordinary assignment: writable, enumerable, configurable. */ +export const data: Attributes = { writable: true, enumerable: true, configurable: true } +/** Built-in methods and `constructor`: writable and configurable but hidden from enumeration. */ +export const hidden: Attributes = { writable: true, enumerable: false, configurable: true } +/** Function `name` and `length`: read-only but deletable. */ +export const readonly: Attributes = { writable: false, enumerable: false, configurable: true } +/** Constants such as `Math.PI` and a constructor's `prototype`. */ +export const frozen: Attributes = { writable: false, enumerable: false, configurable: false } + +/** + * An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a + * built-in kind of object prints, coerces, iterates, and crosses to the host. + */ +export class Obj { + readonly props = new Map() + /** [[Extensible]]: cleared by `Object.preventExtensions`, `seal`, and `freeze`. */ + extensible = true + /** + * 0 for an object the program made. For an intrinsic, its id plus one while it is as the runtime made it, negated + * once it changes (see `changed` and `IntrinsicNames`). + */ + intrinsic = 0 + constructor(public proto: Obj | null) { + if (making !== undefined) making.push(this) + } + + /** The class name `Object.prototype.toString` reports: `[object Map]`. */ + readonly tag: string = "Object" + + /** How diagnostics refer to a value of this kind. */ + get describe(): string { + if (this.tag === "Object") return "a data object" + return `${/^[AEIO]/.test(this.tag) ? "an" : "a"} ${this.tag}` + } + + /** ToString without consulting program-defined methods. */ + toString(): string { + return `[object ${this.tag}]` + } + + /** ToPrimitive without consulting program-defined methods: only a Date answers a number hint differently. */ + toPrimitive(hint: "default" | "number" | "string"): string | number { + return this.toString() + } + + /** ToNumber without consulting program-defined methods. */ + toNumber(): number { + return Number(this.toPrimitive("number")) + } + + /** How `console.log` shows the value; `item` formats a child with cycle and depth tracking. */ + inspect(item: (value: Value) => string): string { + return `{${entries(this) + .map(([key, value]) => `${JSON.stringify(key)}:${item(value)}`) + .join(",")}}` + } + + /** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */ + toHost(item: (value: Value) => unknown): unknown { + return Object.fromEntries( + entries(this) + .filter(([key]) => key !== "__proto__") + .map(([key, value]) => [key, item(value)]), + ) + } + + /** The built-in iteration `for...of` and spread use, when this kind of object has one. */ + iterator(builtins: Builtins): Cursor | undefined { + return undefined + } +} + +export class Arr extends Obj { + override readonly tag = "Array" + /** The attributes every live element shares; `seal` and `freeze` narrow them since elements have no slots. */ + elements: Attributes = data + constructor( + proto: Obj, + readonly items: Array = [], + ) { + super(proto) + } + override get describe() { + return "an array" + } + override toString() { + return this.items.map((item) => (item === null || item === undefined ? "" : coerceToString(item))).join(",") + } + override inspect(item: (value: Value) => string) { + return `[${this.items.map(item).join(",")}]` + } + override toHost(item: (value: Value) => unknown) { + return this.items.map(item) + } + override iterator(builtins: Builtins) { + return new Position(this, "values", builtins.Array) + } +} + +/** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */ +export class ErrorObj extends Obj { + override readonly tag = "Error" + /** + * The interpreter failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. It + * is plain data (type, message, kind, suggestions, and the node it is located at), shared with the failure so a + * location attached later is seen here too. + */ + failure?: PendingThrow + /** Error.prototype.toString: "name: message", or just one when the other is empty. */ + override toString() { + const name = get(this, "name") + const message = get(this, "message") + const shownName = typeof name === "string" ? name : "Error" + const shownMessage = typeof message === "string" ? message : "" + if (shownMessage === "") return shownName + if (shownName === "") return shownMessage + return `${shownName}: ${shownMessage}` + } + override inspect() { + return this.toString() + } +} + +/** Interpreter machinery a program can hold but never inspect, serialize, or hand to the host. */ +export abstract class Opaque extends Obj { + override inspect() { + return "[opaque reference]" + } +} + +export abstract class Callable extends Opaque { + override readonly tag = "Function" + constructor( + proto: Obj, + name: string, + readonly length: number, + ) { + super(proto) + define(this, "length", length, readonly) + define(this, "name", name, readonly) + } + override get describe() { + return "a function" + } +} + +/** A program or prelude function: its bytecode, and the cells of the bindings it captured. */ +export class Fn extends Callable { + constructor( + proto: Obj, + readonly code: Code, + readonly cells: ReadonlyArray, + name = code.name, + ) { + super(proto, name, code.length) + } + get generator(): boolean { + return (this.code.flags & CodeFlags.GENERATOR) !== 0 + } +} + +/** The strict `arguments` object: an ordinary object with indexed own properties and a hidden `length`. */ +export class Arguments extends Obj { + override readonly tag = "Arguments" + constructor(proto: Obj, args: Array) { + super(proto) + args.forEach((arg, index) => define(this, String(index), arg)) + define(this, "length", args.length, hidden) + } + override iterator(builtins: Builtins) { + return new Position( + keys(this).map((key) => get(this, key)), + "values", + builtins.Array, + ) + } +} + +/** + * A native function body: a value, or what the VM carries out as the call: work it hands over by name (`Push`), a + * suspended generator to continue (`Resume`), a call it hands on (`Invoke`), or conversions to run before it runs again + * (`Convert`). A native never runs program code itself. + */ +export type NativeResult = Value | Push | Resume | Invoke | Convert +/** `site` is the call running the built-in, where a failure it reports later (a rejection) is located. */ +export type NativeCall = (thisValue: Value, args: Array, site: AstNode | undefined) => NativeResult +export type NativeConstruct = (args: Array, newTarget: Callable, site: AstNode | undefined) => NativeResult + +export type NativeOptions = { + /** + * Why this native may be made while the program runs, for the few kinds that still capture state in a host closure. + * Every other native is made with the intrinsics; see `nativeRule`. + */ + readonly runtime?: RuntimeNative + readonly name: string + readonly length?: number + readonly call: NativeCall + /** `new name(...)`; without it the function is not a constructor. */ + readonly construct?: NativeConstruct + /** Whether callback sites (array methods, replacers, promise reactions) admit this function. Defaults to true. */ + readonly callback?: boolean +} + +/** The natives still made at run time, each holding state in a host closure that cannot be saved yet. */ +export type RuntimeNative = "extension" + +/** + * Natives are made only with the intrinsics, so each one has an intrinsic name and run state never holds a host + * closure. When `check` is on (tests turn it on), making a native for a runtime whose intrinsics are `sealed` (keyed by + * its `Function.prototype`) throws, unless it declares one of the `RuntimeNative` exceptions. + */ +export const nativeRule: { check: boolean; sealed: WeakSet; observe?: (native: Native) => void } = { + check: false, + sealed: new WeakSet(), +} + +/** + * A built-in function: a `Native` made with the intrinsics, or a data kind such as `BoundFn` or `Resolver` whose state + * is plain fields and whose behaviour lives on its class. + */ +export abstract class Builtin extends Callable { + /** `new name(...)`; without it the function is not a constructor. */ + abstract readonly construct: NativeConstruct | undefined + /** Whether callback sites (array methods, replacers, promise reactions) admit this function. */ + abstract readonly callback: boolean + abstract invoke(thisValue: Value, args: Array, site: AstNode | undefined): NativeResult +} + +export class Native extends Builtin { + readonly call: NativeCall + readonly construct: NativeConstruct | undefined + readonly callback: boolean + + constructor(proto: Obj, options: NativeOptions) { + if (nativeRule.check && options.runtime === undefined && nativeRule.sealed.has(proto)) { + throw new Error(`Native "${options.name}" was made after the intrinsics; make it a data kind instead.`) + } + super(proto, options.name, options.length ?? 0) + nativeRule.observe?.(this) + this.call = options.call + this.construct = options.construct + this.callback = options.callback ?? true + } + + invoke(thisValue: Value, args: Array, site: AstNode | undefined): NativeResult { + return this.call(thisValue, args, site) + } +} + +/** `Function.prototype.bind`'s result: calls `target` with the bound receiver and leading arguments. */ +export class BoundFn extends Builtin { + readonly construct = undefined + readonly callback = true + constructor( + proto: Obj, + readonly target: Callable, + readonly boundThis: Value, + readonly boundArgs: ReadonlyArray, + ) { + super(proto, `bound ${coerceToString(get(target, "name"))}`, Math.max(0, target.length - boundArgs.length)) + } + + invoke(_: Value, args: Array, site: AstNode | undefined): NativeResult { + return new Invoke(this.target, this.boundThis, [...this.boundArgs, ...args], site) + } +} + +/** A `resolve` or `reject` function handed to an executor or thenable: settles its shared `Resolving` once. */ +export class Resolver extends Builtin { + readonly construct = undefined + readonly callback = true + constructor( + proto: Obj, + name: string, + readonly resolving: Resolving, + readonly kind: "resolve" | "reject", + ) { + super(proto, name, 1) + } + + invoke(_: Value, args: Array): NativeResult { + if (this.kind === "resolve") this.resolving.resolve(args[0]) + else this.resolving.reject(new Throw(args[0])) + return undefined + } +} + +export class PromiseObj extends Opaque { + override readonly tag = "Promise" + state: "pending" | "fulfilled" | "rejected" = "pending" + /** The value, or the failure as thrown; a handler receives it materialized. */ + result: unknown = undefined + reactions: Array = [] + /** [[PromiseIsHandled]]: a reaction was attached, so a rejection is not reported as unhandled. */ + handled = false + constructor( + proto: Obj, + /** Creation order, for reporting unhandled rejections in the order their promises were made. */ + readonly id: number, + ) { + super(proto) + } + override get describe() { + return "an un-awaited Promise" + } + override inspect() { + return "[Promise (await it to get its value)]" + } +} + +export type GeneratorState = + | "suspendedStart" + | "suspendedYield" + | "executing" + | "awaiting-return" + | "draining" + | "completed" + +export class GeneratorObj extends Opaque { + override readonly tag = "Generator" + state: GeneratorState = "suspendedStart" + /** The suspended body's frame, kept while it waits for the next request, which puts it back on the job stack. */ + frame: Frame | undefined = undefined + /** An async generator's requests, oldest first, each with what settles its promise. */ + readonly queue: Array<{ readonly request: GeneratorRequest; readonly resolving: Resolving }> = [] + constructor( + proto: Obj, + readonly asynchronous: boolean, + ) { + super(proto) + } + override get describe() { + return "a generator" + } +} + +/** One pull from an iterator, as `for...of` sees it. */ +export type Step = { readonly done: boolean; readonly value: Value } + +/** + * An iterator the interpreter drives, as data: a built-in cursor over a collection (`Position`), or a program + * iterator driven by its own `next` method (`CustomIterator`). + */ +export type Cursor = Position | CustomIterator + +/** A program iterator record: the iterator, its `next` method, and whether its results are awaited. */ +export type CustomIterator = { + readonly iterator: Obj + readonly next: Value + readonly asynchronous: boolean +} + +const finished: Step = { done: true, value: undefined } + +/** What a built-in iterator yields for each entry: `keys()`, `values()`, or `entries()`. */ +export type IterationKind = "keys" | "values" | "entries" + +/** What a built-in iterator walks: a collection, a string, or a fixed list of values. */ +export type PositionSource = + | Arr + | Bytes + | MapObj + | SetObj + | HeadersObj + | URLSearchParamsObj + | string + | ReadonlyArray + +/** + * A built-in iterator's place in its source, as data: what it walks, what it yields, and the next index. Iteration is + * live, as in JS: an array iterator reads the length on every step, and a Map or Set iterator is kept in place by its + * table. Once done it stays done. There is nothing to close. + */ +export class Position implements TableCursor { + index = 0 + done = false + constructor( + readonly source: PositionSource, + readonly kind: IterationKind, + /** The prototype of the `[key, value]` arrays `entries()` yields. */ + readonly arrays: Obj, + ) { + if (source instanceof MapObj) source.map.track(this) + if (source instanceof SetObj) source.set.track(this) + } + + step(): Step { + if (this.done) return finished + const source = this.source + if (typeof source === "string") { + if (this.index >= source.length) return this.finish() + const point = source.codePointAt(this.index)! + const value = source.slice(this.index, this.index + (point > 0xffff ? 2 : 1)) + this.index += value.length + return { done: false, value } + } + if (source instanceof MapObj || source instanceof SetObj) { + const table = source instanceof MapObj ? source.map : source.set + const at = table.advance(this) + if (at < 0) return finished + const key = table.keys[at] as Value + return this.yield(key, source instanceof MapObj ? table.values[at] : key) + } + if (source instanceof HeadersObj || source instanceof URLSearchParamsObj) { + // The host recomputes the sorted list on every step, as the spec's pair iterators do. + const list = Array.from(source instanceof HeadersObj ? source.headers.entries() : source.params.entries()) + const entry = list[this.index] + if (entry === undefined) return this.finish() + this.index++ + return this.yield(entry[0], entry[1]) + } + const items = source instanceof Arr ? source.items : source instanceof Bytes ? source.bytes : source + if (this.index >= items.length) return this.finish() + const at = this.index++ + return this.yield(at, items[at]) + } + + private yield(key: Value, value: Value): Step { + if (this.kind === "keys") return { done: false, value: key } + if (this.kind === "values") return { done: false, value } + return { done: false, value: new Arr(this.arrays, [key, value]) } + } + + private finish(): Step { + this.done = true + return finished + } +} + +/** A built-in iterator: a live cursor over a host collection or an iterator helper, yielding program values. */ +export class IteratorObj extends Opaque { + override readonly tag = "Iterator" + constructor( + proto: Obj, + readonly cursor: Cursor, + ) { + super(proto) + } + override get describe() { + return "an iterator" + } +} + +/** A built-in iterator over a collection, e.g. `array.values()`. */ +export const positionIterator = (builtins: Builtins, source: PositionSource, kind: IterationKind): IteratorObj => + new IteratorObj(builtins.Iterator, new Position(source, kind, builtins.Array)) + +/** A built-in object around a host value: data-like, so it prints as itself and crosses to extensions as a copy. */ +export abstract class Wrapper extends Obj { + override inspect(item: (value: Value) => string) { + return this.toString() + } +} + +export class DateObj extends Wrapper { + override readonly tag = "Date" + constructor( + proto: Obj, + public time: number, + ) { + super(proto) + } + override toString() { + return Number.isFinite(this.time) ? new Date(this.time).toISOString() : "Invalid Date" + } + override toPrimitive(hint: "default" | "number" | "string") { + return hint === "number" ? this.time : this.toString() + } + override toHost() { + return new Date(this.time) + } +} + +/** A RegExp as data: its source, flags, and `lastIndex`; the host regex is only a compiled cache of the first two. */ +export class RegExpObj extends Wrapper { + override readonly tag = "RegExp" + readonly source: string + readonly flags: string + /** Shared by `exec`/`test` and the String methods, as the host regex's own `lastIndex` would be. */ + lastIndex = 0 + private compiled: RegExp | undefined + constructor(proto: Obj, pattern: string, flags: string) { + super(proto) + // Compiling validates the pattern and flags, and normalizes both as the host spells them. + this.compiled = new RegExp(pattern, flags) + this.source = this.compiled.source + this.flags = this.compiled.flags + } + /** The compiled regex, for reading what it is; anything that matches goes through `use`. */ + get regex(): RegExp { + this.compiled ??= new RegExp(this.source, this.flags) + return this.compiled + } + /** Runs host regex work starting from this object's `lastIndex`, and keeps the `lastIndex` the work leaves. */ + use(work: (regex: RegExp) => T): T { + const regex = this.regex + regex.lastIndex = this.lastIndex + try { + return work(regex) + } finally { + this.lastIndex = regex.lastIndex + } + } + override toString() { + return `/${this.source}/${this.flags}` + } + override toHost() { + return new RegExp(this.source, this.flags) + } +} + +export class MapObj extends Wrapper { + override readonly tag = "Map" + readonly map = new OrderedTable() + override inspect(item: (value: Value) => string) { + return `Map(${this.map.size}) [${[...this.map].map(([key, value]) => `[${item(key)},${item(value)}]`).join(",")}]` + } + override toHost(item: (value: Value) => unknown) { + return new Map([...this.map].map(([key, value]) => [item(key), item(value)])) + } + override iterator(builtins: Builtins) { + return new Position(this, "entries", builtins.Array) + } +} + +export class SetObj extends Wrapper { + override readonly tag = "Set" + readonly set = new OrderedTable() + override inspect(item: (value: Value) => string) { + return `Set(${this.set.size}) [${[...this.set.liveKeys()].map(item).join(",")}]` + } + override toHost(item: (value: Value) => unknown) { + return new Set([...this.set.liveKeys()].map(item)) + } + override iterator(builtins: Builtins) { + return new Position(this, "values", builtins.Array) + } +} + +/** + * Keys are held strongly for the run, which is short-lived; garbage collection is not observable in Code Mode, and a + * plain table keeps the state as data. + */ +export class WeakMapObj extends Wrapper { + override readonly tag = "WeakMap" + readonly map = new Map() + override inspect() { + return "WeakMap { }" + } +} + +export class WeakSetObj extends Wrapper { + override readonly tag = "WeakSet" + readonly set = new Set() + override inspect() { + return "WeakSet { }" + } +} + +export class URLSearchParamsObj extends Wrapper { + override readonly tag = "URLSearchParams" + constructor( + proto: Obj, + readonly params: URLSearchParams, + ) { + super(proto) + } + override toString() { + return this.params.toString() + } + override toHost() { + return new URLSearchParams(this.params) + } + override iterator(builtins: Builtins) { + return new Position(this, "entries", builtins.Array) + } +} + +export class HeadersObj extends Wrapper { + override readonly tag = "Headers" + constructor( + proto: Obj, + readonly headers: Headers, + ) { + super(proto) + } + override inspect() { + return `Headers ${JSON.stringify(Object.fromEntries(this.headers))}` + } + override toHost() { + return new Headers(this.headers) + } + override iterator(builtins: Builtins) { + return new Position(this, "entries", builtins.Array) + } +} + +export class URLObj extends Wrapper { + override readonly tag = "URL" + readonly searchParams: URLSearchParamsObj + constructor( + proto: Obj, + searchParamsProto: Obj, + readonly url: URL, + ) { + super(proto) + this.searchParams = new URLSearchParamsObj(searchParamsProto, url.searchParams) + } + override toString() { + return this.url.href + } + override toHost() { + return new URL(this.url.href) + } +} + +/** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */ +export class Bytes extends Wrapper { + override readonly tag = "Uint8Array" + constructor( + proto: Obj, + readonly bytes: Uint8Array, + ) { + super(proto) + } + override toString() { + return this.bytes.join(",") + } + override inspect() { + return `Uint8Array(${this.bytes.length}) [${this.bytes.join(",")}]` + } + override toHost() { + return new Uint8Array(this.bytes) + } + override iterator(builtins: Builtins) { + return new Position(this, "values", builtins.Array) + } +} + +/** Every value a program can hold. Host values never appear here; they are copied in at the boundaries. */ +export type Value = string | number | boolean | null | undefined | symbol | Obj | ToolReference + +/** ToString without consulting program-defined methods. */ +export const coerceToString = (value: Value): string => (value instanceof Obj ? value.toString() : String(value)) + +/** ToNumber without consulting program-defined methods; tool references are not numbers. */ +export const coerceToNumber = (value: Value): number => { + if (value instanceof Obj) return value.toNumber() + return value instanceof ToolReference ? Number.NaN : Number(value) +} + +/** ToIntegerOrInfinity: NaN is 0, fractions truncate. */ +export const coerceToInteger = (value: Value): number => { + const number = coerceToNumber(value) + return Number.isNaN(number) ? 0 : Math.trunc(number) +} + +/** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */ +export const isRuntimeReference = (value: Value): boolean => + value instanceof Opaque || value instanceof Wrapper || value instanceof ToolReference + +const MAX_ARRAY_INDEX = 4_294_967_295 + +export const parseArrayIndex = (key: string | number): number | undefined => { + if (typeof key === "number") return Number.isInteger(key) && key >= 0 && key < MAX_ARRAY_INDEX ? key : undefined + const property = String(key) + if (!/^(0|[1-9]\d*)$/.test(property)) return undefined + const index = Number(property) + return index < MAX_ARRAY_INDEX ? index : undefined +} + +const canonical = (key: PropertyKey): string | symbol => (typeof key === "symbol" ? key : String(key)) + +/** Objects whose integer keys are live elements rather than own property slots. */ +type Indexed = Arr | Bytes + +const isIndexed = (target: Obj): target is Indexed => target instanceof Arr || target instanceof Bytes + +const elements = (target: Indexed): Array | Uint8Array => (target instanceof Arr ? target.items : target.bytes) + +const index = (target: Obj, key: string | number | symbol): number | undefined => + isIndexed(target) && typeof key !== "symbol" ? parseArrayIndex(key) : undefined + +/** The own property under `key`, including an array's live indexes and `length`. */ +export const own = (target: Obj, key: PropertyKey): Slot | undefined => { + // Most lookups are a name on a plain object or a prototype. + if (typeof key === "string" && !isIndexed(target)) return target.props.get(key) + const name = typeof key === "number" && isIndexed(target) ? key : canonical(key) + if (isIndexed(target)) { + const at = index(target, name) + if (at !== undefined) { + const items = elements(target) + if (!(at in items)) return undefined + return { value: items[at], ...(target instanceof Arr ? target.elements : data) } + } + if (target instanceof Arr && name === "length") { + return { value: target.items.length, writable: target.elements.writable, enumerable: false, configurable: false } + } + } + return target.props.get(typeof name === "number" ? String(name) : name) +} + +const read = (slot: Slot, receiver: Value): Value => + "value" in slot ? slot.value : slot.get === undefined ? undefined : slot.get(receiver) + +export const hasOwn = (target: Obj, key: PropertyKey): boolean => own(target, key) !== undefined + +export const getOwn = (target: Obj, key: PropertyKey): Value => { + const slot = own(target, key) + return slot === undefined ? undefined : read(slot, target) +} + +/** [[Get]]: walks the prototype chain; accessors see `receiver`, which is the primitive for wrapper prototypes. */ +export const get = (target: Obj, key: PropertyKey, receiver: Value = target): Value => { + // An array's present element, without making its property slot. + if (typeof key === "number" && target instanceof Arr && key in target.items && parseArrayIndex(key) !== undefined) + return target.items[key] + for (let current: Obj | null = target; current !== null; current = current.proto) { + const slot = own(current, key) + if (slot !== undefined) return read(slot, receiver) + } + return undefined +} + +export const has = (target: Obj, key: PropertyKey): boolean => { + for (let current: Obj | null = target; current !== null; current = current.proto) { + if (own(current, key) !== undefined) return true + } + return false +} + +export const hasPrototype = (value: Value, proto: Obj): boolean => { + for (let current = value instanceof Obj ? value.proto : null; current !== null; current = current.proto) { + if (current === proto) return true + } + return false +} + +const writeElement = (target: Indexed, name: string | number | symbol, value: Value): boolean | undefined => { + const at = index(target, name) + if (at !== undefined) { + if (target instanceof Bytes) { + target.bytes[at] = typeof value === "number" ? value : Number(value) + return true + } + if (!(at in target.items)) rejectAddition(target, at) + changed(target) + target.items[at] = value + return true + } + if (!(target instanceof Arr) || name !== "length") return undefined + const length = typeof value === "number" ? value : Number(value) + if (!Number.isInteger(length) || length < 0) throw rangeError("Invalid array length") + // Shrinking deletes elements, which a sealed array forbids. + if (length < target.items.length && !target.elements.configurable) return false + checkArrayLength(length) + changed(target) + target.items.length = length + return true +} + +/** [[Set]]: an inherited setter or read-only property decides before an own data property is created. */ +export const set = (target: Obj, key: PropertyKey, value: Value): boolean => { + // Overwriting an array's present, writable element: no setter or read-only slot can decide first. + if (typeof key === "number" && target instanceof Arr && target.elements.writable && key in target.items) { + if (parseArrayIndex(key) !== undefined) { + changed(target) + target.items[key] = value + return true + } + } + const name = canonical(key) + for (let current: Obj | null = target; current !== null; current = current.proto) { + const slot = own(current, key) + if (slot === undefined) continue + if (!("value" in slot)) { + if (slot.set === undefined) return false + slot.set(target, value) + return true + } + if (!slot.writable) return false + if (current !== target) break + if (isIndexed(target)) { + const written = writeElement(target, typeof key === "number" ? key : name, value) + if (written !== undefined) return written + } + changed(target) + slot.value = value + return true + } + if (isIndexed(target)) { + const written = writeElement(target, typeof key === "number" ? key : name, value) + if (written !== undefined) return written + } + rejectAddition(target, name) + changed(target) + target.props.set(name, { value, ...data }) + return true +} + +/** Creating a property on a non-extensible object is the one [[Set]] failure with its own message. */ +export const rejectAddition = (target: Obj, key: string | symbol | number): void => { + if (target.extensible) return + throw typeError(`Cannot add property ${String(key)}, object is not extensible.`) +} + +/** [[DefineOwnProperty]] for a data property, ignoring the chain. */ +export const define = (target: Obj, key: PropertyKey, value: Value, attrs: Attributes = data): void => { + const name = canonical(key) + if (isIndexed(target) && writeElement(target, name, value) !== undefined) return + changed(target) + target.props.set(name, { value, ...attrs }) +} + +export const defineAccessor = (target: Obj, key: PropertyKey, get: Getter | undefined, set?: Setter): void => { + changed(target) + target.props.set(canonical(key), { get, set, enumerable: false, configurable: true }) +} + +export const remove = (target: Obj, key: PropertyKey): boolean => { + const name = canonical(key) + if (isIndexed(target)) { + const at = index(target, name) + if (at !== undefined) { + if (target instanceof Bytes) return !(at in target.bytes) + if (at in target.items && !target.elements.configurable) return false + changed(target) + return delete target.items[at] + } + if (target instanceof Arr && name === "length") return false + } + const slot = target.props.get(name) + if (slot === undefined) return true + if (!slot.configurable) return false + changed(target) + target.props.delete(name) + return true +} + +// JS order: array indexes, integer-like keys ascending, other strings, then symbols. +export const ownKeys = (target: Obj): Array => { + const strings = [...target.props.keys()].filter((key): key is string => typeof key === "string") + const symbols = [...target.props.keys()].filter((key): key is symbol => typeof key === "symbol") + return [ + ...(isIndexed(target) ? Object.keys(elements(target)) : []), + ...(target instanceof Arr ? ["length"] : []), + ...strings.filter((key) => parseArrayIndex(key) !== undefined).sort((a, b) => Number(a) - Number(b)), + ...strings.filter((key) => parseArrayIndex(key) === undefined), + ...symbols, + ] +} + +const enumerable = (target: Obj, key: string | symbol): boolean => own(target, key)?.enumerable === true + +/** Own enumerable keys, including the iterator symbols; what spread and `Object.assign` copy. */ +export const enumerableKeys = (target: Obj): Array => + ownKeys(target).filter( + (key) => + (typeof key === "string" || key === IteratorSymbol || key === AsyncIteratorSymbol) && enumerable(target, key), + ) + +/** Own enumerable string keys: `Object.keys`. */ +export const keys = (target: Obj): Array => + ownKeys(target).filter((key): key is string => typeof key === "string" && enumerable(target, key)) + +/** Own enumerable string entries: `Object.entries` and serialization. */ +export const entries = (target: Obj): Array<[string, Value]> => keys(target).map((key) => [key, getOwn(target, key)]) + +/** An iterator result `{ value, done }`, built directly since every iteration step makes one. */ +export const iteratorResult = (proto: Obj, value: Value, done: boolean): Obj => { + const target = new Obj(proto) + target.props.set("value", { value, writable: true, enumerable: true, configurable: true }) + target.props.set("done", { value: done, writable: true, enumerable: true, configurable: true }) + return target +} + +export const record = (proto: Obj, fields: Record): Obj => { + const target = new Obj(proto) + for (const [key, value] of Object.entries(fields)) define(target, key, value) + return target +} + +export const assign = (target: Obj, source: Obj, skip?: ReadonlySet): void => { + for (const key of enumerableKeys(source)) { + if (skip?.has(key)) continue + set(target, key, getOwn(source, key)) + } +} diff --git a/packages/codemode/src/vm/runtime/operations.ts b/packages/codemode/src/vm/runtime/operations.ts new file mode 100644 index 00000000000..01cf75ca039 --- /dev/null +++ b/packages/codemode/src/vm/runtime/operations.ts @@ -0,0 +1,349 @@ +import type { Expression, MemberExpression, NewExpression, Super } from "acorn" +import { ToolReference } from "../../tool-runtime.js" +import { type AstNode, invalidData, typeError } from "./model.js" +import { checkStringLength } from "./limits.js" +import { locate } from "./errors.js" +import { type Builtins, primitivePrototype } from "./intrinsics.js" +import { + Arr, + Builtin, + Callable, + type Cursor, + Fn, + get, + has, + hasOwn, + hasPrototype, + IteratorObj, + keys, + Obj, + parseArrayIndex, + Position, + PromiseObj, + set, + coerceToString, + type Value, +} from "./objects.js" +import { isOpaque, rejectCircularInsertion, typeofValue } from "./references.js" +import type { Interpreter } from "./interpreter.js" +import { enumerableSource } from "../stdlib/object.js" + +// The synchronous semantics the VM's ops share: operators, property references, and the checks around them. Nothing +// here runs program code. + +/** The binary operators that convert object operands through ToPrimitive before acting on primitives. */ +const primitiveOperators = new Set([ + "+", + "-", + "*", + "/", + "%", + "**", + "<", + "<=", + ">", + ">=", + "&", + "|", + "^", + "<<", + ">>", + ">>>", +]) + +/** ToPropertyKey on a primitive (or an opaque value, which keeps its built-in string form). */ +export const propertyKey = (value: Value): PropertyKey => + typeof value === "string" || typeof value === "number" || typeof value === "symbol" ? value : coerceToString(value) + +// Native engines name the callee (`search(...).catch is not a function`), including call chains. Returns +// undefined when a link cannot be named, so a chain is either named completely or not at all. +const calleeDescription = (node: Expression | Super | undefined): string | undefined => { + if (node?.type === "Identifier") return node.name + if (node?.type === "CallExpression") { + const target = calleeDescription(node.callee) + return target === undefined ? undefined : `${target}(...)` + } + if (node?.type !== "MemberExpression") return undefined + const property = node.property + const key = + !node.computed && property.type === "Identifier" + ? property.name + : property.type === "Literal" && typeof property.value === "string" + ? property.value + : undefined + if (key === undefined) return undefined + const object = calleeDescription(node.object) + return object === undefined ? undefined : `${object}.${key}` +} + +/** The TypeError for calling a value that is not a function. */ +export const notAFunction = (callee: Expression | Super | undefined, node: AstNode | undefined) => + typeError(`${calleeDescription(callee) ?? "The called value"} is not a function.`, callee ?? node) + +/** The TypeError for `new` on a callee that cannot be constructed. */ +export const notConstructible = (callee: Value, node: NewExpression) => { + // `new` itself is supported, so a non-constructible callee is a TypeError like JS rather than + // unsupported syntax. Built-ins like Number are real constructors in JS, so do not claim + // otherwise; say `new` is unsupported for them and point at the plain call. + const name = calleeDescription(node.callee) ?? "The called value" + const message = + callee instanceof Fn + ? `${name} cannot be constructed: user-defined constructors and classes are not supported. Call it as a function that returns a plain object instead.` + : callee instanceof Builtin + ? `new ${name}(...) is not supported; call ${name}(...) without new instead.` + : `${name} is not a constructor.` + return typeError(message, node) +} + +// OrdinaryHasInstance: walk the left operand's chain looking for the constructor's `prototype`. +export const instanceofValue = (lhs: Value, rhs: Value, node: AstNode): boolean => { + if (!(rhs instanceof Callable)) { + throw typeError("The right-hand side of 'instanceof' is not callable.", node) + } + const prototype = get(rhs, "prototype") + if (!(prototype instanceof Obj)) { + throw typeError("The right-hand side of 'instanceof' has no 'prototype' object.", node) + } + return hasPrototype(lhs, prototype) +} + +export const applyBinaryOperator = (operator: string, lhs: Value, rhs: Value, node: AstNode): Value => { + if (operator === "===") return lhs === rhs + if (operator === "!==") return lhs !== rhs + if (operator === "==") return looselyEqual(lhs, rhs, node) + if (operator === "!=") return !looselyEqual(lhs, rhs, node) + if (operator === "in" && rhs instanceof Obj && !isOpaque(lhs)) return has(rhs, propertyKey(lhs)) + if (isOpaque(lhs) || isOpaque(rhs)) { + throw invalidData("Binary operators require data values.", node) + } + // Object operands were already converted through ToPrimitive; only primitives reach the arithmetic below. + switch (operator) { + case "+": { + const sum = (lhs as string) + (rhs as string) + if (typeof sum === "string") checkStringLength(sum.length) + return sum + } + case "-": + return (lhs as number) - (rhs as number) + case "*": + return (lhs as number) * (rhs as number) + case "/": + return (lhs as number) / (rhs as number) + case "%": + return (lhs as number) % (rhs as number) + case "**": + return (lhs as number) ** (rhs as number) + case "<": + return (lhs as string) < (rhs as string) + case "<=": + return (lhs as string) <= (rhs as string) + case ">": + return (lhs as string) > (rhs as string) + case ">=": + return (lhs as string) >= (rhs as string) + case "&": + return (lhs as number) & (rhs as number) + case "|": + return (lhs as number) | (rhs as number) + case "^": + return (lhs as number) ^ (rhs as number) + case "<<": + return (lhs as number) << (rhs as number) + case ">>": + return (lhs as number) >> (rhs as number) + case ">>>": + return (lhs as number) >>> (rhs as number) + case "in": + throw typeError("The 'in' operator requires a data object on the right-hand side.", node) + default: + throw typeError(`Unsupported binary operator '${operator}'.`, node) + } +} + +// IsLooselyEqual: objects (including functions and tool references) compare by identity, and a nullish +// primitive never equals an object. +const looselyEqual = (lhs: Value, rhs: Value, node: AstNode): boolean => { + const lhsObject = lhs !== null && typeof lhs === "object" + const rhsObject = rhs !== null && typeof rhs === "object" + if (lhsObject === rhsObject) return lhsObject ? lhs === rhs : lhs == rhs + const primitive = lhsObject ? rhs : lhs + if (primitive === null || primitive === undefined) return false + // Data objects were converted through ToPrimitive, so only an opaque reference facing a primitive gets here. + throw invalidData("Binary operators require data values.", node) +} + +/** + * How an operator with an object operand converts: `in` with a data object on the left asks ToPropertyKey of it, + * a converting operator asks ToPrimitive of both operands with `hint`, and anything else needs no conversion. + */ +export const operatorConversion = ( + operator: string, + lhs: Value, + rhs: Value, +): { readonly kind: "key" } | { readonly kind: "primitive"; readonly hint: "default" | "number" } | undefined => { + // `in` checks the right operand before ToPropertyKey on the left, so a bad right side wins over a bad key. + if (operator === "in" && lhs instanceof Obj && !isOpaque(lhs) && rhs instanceof Obj) return { kind: "key" } + // IsLooselyEqual converts only an object facing a non-nullish primitive; two objects (including tool + // references, which are not Obj) compare by identity. + const equality = operator === "==" || operator === "!=" + const other = lhs instanceof Obj ? rhs : lhs + const converts = + primitiveOperators.has(operator) || (equality && other !== null && other !== undefined && typeof other !== "object") + if (!converts) return undefined + return { kind: "primitive", hint: operator === "+" || equality ? "default" : "number" } +} + +export type { CustomIterator } from "./objects.js" + +/** A resolved member: the object to read through and the receiver that inherited accessors see. */ +type MemberReference = { + target: Obj + key: PropertyKey + receiver: Value +} + +// A thenable the program's implicit async body adopts before copy-out. +export const isThenable = (value: Value): boolean => + value instanceof PromiseObj || (value instanceof Obj && typeofValue(get(value, "then")) === "function") + +/** A built-in iterator over `value` that needs no program code to start: an iterator object, a string, a collection. */ +export const builtinCursor = (builtins: Builtins, value: Value): Cursor | undefined => { + if (value instanceof IteratorObj) return value.cursor + if (typeof value === "string") return new Position(value, "values", builtins.Array) + return value instanceof Obj ? value.iterator(builtins) : undefined +} + +export const requireIteratorObject = (value: Value, context: string, node?: AstNode): Obj => { + if (value instanceof Obj) return value + throw typeError(`${context} must be an object.`, node) +} + +export const requireIteratorMethod = (value: Value, context: string, node?: AstNode): Value => { + if (typeofValue(value) === "function") return value + throw typeError(`${context} must be a function.`, node) +} + +// for...in over null/undefined iterates nothing, like JS. After the own keys come each prototype's enumerable +// string keys, skipping any key a nearer level has, even as a non-enumerable property. +export const forInKeys = (ctx: Interpreter, value: Value, node: AstNode): Array => { + if (value instanceof ToolReference) return [...ctx.tools.keys(value.path)] + if (value === null || value === undefined) return [] + const source = enumerableSource(ctx, "for...in", value, node) + const chain = [source] + // A string's characters stand in an array, but its prototype is String.prototype. + let proto = primitivePrototype(ctx.builtins, value) ?? source.proto + while (proto !== null) { + chain.push(proto) + proto = proto.proto + } + return chain.flatMap((target, depth) => { + const nearer = chain.slice(0, depth) + return keys(target).filter((key) => !nearer.some((level) => hasOwn(level, key))) + }) +} + +// GetValue applies ToObject to the base before ToPropertyKey, so a nullish base throws before the key's own +// toString runs. +export const requireMemberBase = (node: MemberExpression, objectValue: Value, keyValue: Value): void => { + if (objectValue === null || objectValue === undefined) { + throw typeError(`Cannot read properties of ${objectValue} (reading '${coerceToString(keyValue)}').`, node.object) + } +} + +export const resolveProperty = ( + builtins: Builtins, + objectValue: Value, + key: PropertyKey, + objectNode: AstNode, + propertyNode: AstNode, +): MemberReference | ToolReference | { value: Value } => { + if (objectValue instanceof ToolReference) { + if (typeof key !== "string") { + throw typeError("Tool paths must use string property names.", propertyNode) + } + return new ToolReference([...objectValue.path, key]) + } + + if (objectValue instanceof Obj) return { target: objectValue, key, receiver: objectValue } + + // Strings own length and indexes; every other primitive property reads through the wrapper prototype. + if (typeof objectValue === "string") { + if (key === "length") return { value: objectValue.length } + const index = typeof key === "symbol" ? undefined : parseArrayIndex(key) + if (index !== undefined) return { value: objectValue[index] } + } + const proto = primitivePrototype(builtins, objectValue) + if (proto !== undefined) return { target: proto, key, receiver: objectValue } + + if (objectValue === null || objectValue === undefined) { + throw typeError(`Cannot read properties of ${objectValue} (reading '${String(key)}').`, objectNode) + } + throw typeError("Cannot access a property on a non-object value.", objectNode) +} + +// One destructured property, read the way a member expression would read it (primitives use their prototype). +export const destructuredProperty = (builtins: Builtins, source: Value, key: PropertyKey, node: AstNode): Value => { + const reference = resolveProperty(builtins, source, key, node, node) + if (reference instanceof ToolReference) return reference + if ("value" in reference) return reference.value + return readProperty(reference.target, reference.key, node, reference.receiver) +} + +export const readReference = (reference: MemberReference, node: MemberExpression): Value => { + // Reject unknown promise properties so a missing await cannot hide. + if (reference.target instanceof PromiseObj && !has(reference.target, reference.key)) { + throw invalidData( + "This value is an un-awaited Promise; await it first - e.g. `const result = await tools.ns.tool(...)`.", + node.object, + ) + } + return readProperty(reference.target, reference.key, node, reference.receiver) +} + +// Accessors throw without a location; the member or pattern that read them supplies it. +export const readProperty = ( + target: Obj, + key: PropertyKey, + node: AstNode | undefined, + receiver: Value = target, +): Value => { + try { + return get(target, key, receiver) + } catch (error) { + throw locate(error, node) + } +} + +/** The reference an assignment or update writes through: only a data field of an object. */ +export const writableReference = ( + reference: MemberReference | ToolReference | { value: Value } | symbol, + node: MemberExpression, +): MemberReference => { + if (typeof reference === "symbol" || reference instanceof ToolReference || "value" in reference) { + throw typeError("Only data fields may be assigned.", node) + } + if (reference.receiver !== reference.target) { + throw typeError( + `Cannot create property '${String(reference.key)}' on ${typeof reference.receiver} '${String(reference.receiver)}'.`, + node, + ) + } + return reference +} + +export const assignToReference = (target: Obj, key: PropertyKey, next: Value, node: AstNode): void => { + const written = (() => { + try { + rejectCircularInsertion( + target, + next, + target instanceof Arr ? "Array assignment result" : "Object assignment result", + ) + return set(target, key, next) + } catch (error) { + throw locate(error, node) + } + })() + if (written) return + throw typeError(`Cannot assign to read only property '${String(key)}'.`, node) +} diff --git a/packages/codemode/src/vm/runtime/promises.ts b/packages/codemode/src/vm/runtime/promises.ts new file mode 100644 index 00000000000..0b91eebb91d --- /dev/null +++ b/packages/codemode/src/vm/runtime/promises.ts @@ -0,0 +1,325 @@ +import { type AstNode, Throw, typeError } from "./model.js" +import { Callable, changed, define, hidden, Arr, Fn, Obj, PromiseObj, record, Resolver, type Value } from "./objects.js" +import { constructor, methods, native, receiver, requiresNew } from "./native.js" +import { createAggregateErrorValue, materialize } from "./errors.js" +import { describeValue, typeofValue } from "./references.js" +import { isSupportedCallback } from "./callback.js" +import type { Interpreter } from "./interpreter.js" +import { type Completion, Interruption, Push } from "./signals.js" +import type { Combination, Reaction, Resolving } from "../scheduler.js" +import { finishAwaitReturn } from "../generators.js" + +// A `resolve`/`reject` handed to an executor or thenable: calling it settles the capability. +const resolvingFunctions = (ctx: Interpreter, resolving: Resolving) => ({ + resolve: new Resolver(ctx.builtins.Function, "resolve", resolving, "resolve"), + reject: new Resolver(ctx.builtins.Function, "reject", resolving, "reject"), +}) + +/** + * NewPromiseCapability(%Promise%): a pending promise with what settles it once. A built-in's own failure that rejects + * it is located at `site`, the call that created it. + */ +export const capability = (ctx: Interpreter, site: AstNode | undefined): Resolving => + ctx.vm.scheduler.resolving(ctx.vm.scheduler.newPromise(), site) + +/** Settles `resolving` with a completion: a fulfillment value is resolved, so a thenable is adopted. */ +const settleWith = (resolving: Resolving, completion: Completion) => + completion.ok ? resolving.resolve(completion.value as Value) : resolving.reject(completion.error) + +/** + * Runs a settled promise's reaction as a job: it finishes at once, or returns the call into program code the VM makes + * for it. + */ +export const react = ( + ctx: Interpreter, + reaction: Exclude, + settled: Completion, +): JobCall | void => { + switch (reaction.kind) { + case "settle": + return settleWith(reaction.target, settled) + case "handler": + return handle(ctx, reaction, settled) + case "combinator": + return combine(ctx, reaction, settled) + case "finally": + return cleanUp(ctx, reaction, settled) + case "finallyCleaned": + return settled.ok ? reaction.target.resolve(undefined) : reaction.target.reject(settled.error) + case "finallyOutcome": + return settleWith(reaction.target, settled.ok ? reaction.outcome : settled) + case "awaitReturn": + return finishAwaitReturn(ctx.vm, reaction.generator, settled) + } +} + +/** + * A job that calls program code once, which the VM runs (`VmScheduler.beginJob`), counted toward the depth limit: + * - `thenable` (PromiseResolveThenableJob): calls the thenable's `then` with the resolving functions; a throw rejects + * `resolving`. + * - `handler` (PromiseReactionJob): a `then`/`catch` handler; its result or failure settles `derived`. + * - `finally` (PromiseFinallyJob): a `finally` cleanup; once its own result settles, the original outcome passes on to + * `made` (the spec chains `then(valueThunk)` onto the cleanup's promise: one more job), and a failing cleanup wins. + */ +export type JobCall = + | { + readonly kind: "thenable" + readonly resolving: Resolving + readonly then: Value + readonly thenable: Obj + readonly functions: Array + } + | { readonly kind: "handler"; readonly derived: Resolving; readonly handler: Callable; readonly argument: Value } + | { + readonly kind: "finally" + readonly made: Resolving + readonly cleanup: Callable + readonly site: AstNode | undefined + readonly outcome: Completion + } + +/** + * NewPromiseResolveThenableJob: the work that calls the thenable's `then`. Adopting a native promise through the + * intrinsic `then` takes the same jobs as calling it, and keeps the rejection as thrown so its diagnostic survives. + */ +export const adoptThenable = ( + ctx: Interpreter, + promise: PromiseObj, + thenable: Obj, + then: Value, +): JobCall | void => { + const vm = ctx.vm + const resolving = vm.scheduler.resolving(promise) + if (then === vm.scheduler.intrinsicThen && thenable instanceof PromiseObj) { + vm.scheduler.then(thenable, { kind: "settle", target: resolving }) + return + } + const proto = ctx.builtins.Function + const resolve = new Resolver(proto, "", resolving, "resolve") + const reject = new Resolver(proto, "", resolving, "reject") + return { kind: "thenable", resolving, then, thenable, functions: [resolve, reject] } +} + +// A `then`/`catch` reaction calls program code: the handler runs as a job, and its result or failure settles the +// derived promise. A missing handler passes the outcome through. +const handle = ( + ctx: Interpreter, + reaction: Extract, + settled: Completion, +): JobCall | void => { + const handler = settled.ok ? reaction.onFulfilled : reaction.onRejected + const derived = reaction.derived + if (handler === undefined || (!settled.ok && settled.error instanceof Interruption)) { + return settleWith(derived, settled) + } + const argument = settled.ok ? (settled.value as Value) : materialize(ctx, settled.error) + return { kind: "handler", derived, handler, argument } +} + +const promiseStatics = ["all", "allSettled", "race", "any", "resolve", "reject", "withResolvers", "try"] as const + +const invokePromiseMethod = ( + ctx: Interpreter, + name: (typeof promiseStatics)[number], + args: Array, + site: AstNode | undefined, +) => { + if (name === "resolve") return ctx.vm.scheduler.promiseResolve(args[0]) + if (name === "reject") { + const made = capability(ctx, site) + made.reject(new Throw(args[0])) + return made.promise + } + if (name === "withResolvers") { + const made = capability(ctx, site) + const functions = resolvingFunctions(ctx, made) + return record(ctx.builtins.Object, { promise: made.promise, resolve: functions.resolve, reject: functions.reject }) + } + if (name === "try") { + if (typeofValue(args[0]) !== "function") { + throw typeError(`Promise.try expects a function, received ${describeValue(args[0])}.`) + } + return new Push("Promise.try", [capability(ctx, site), args[0], args.slice(1)]) + } + return new Push("Promise combinator", [capability(ctx, site), name, args[0]]) +} + +// Promise.all, allSettled, race, and any: the prelude iterates the input (`$vmCombine`), resolving each element to a +// promise observed through a reaction, so the result settles in the job where the deciding element's reaction runs, +// or at once for an empty input. A failure while iterating rejects the result. + +// One member settled. Once the result settles early, the remaining members' reactions can do nothing: they are +// detached so a member that stays pending for the rest of the program does not keep them. +const combine = ( + ctx: Interpreter, + reaction: Extract, + settled: Completion, +) => { + if (reaction.called) return + reaction.called = true + const combination = reaction.combination + const made = combination.made + const name = combination.name + if (name === "race" || (!settled.ok && settled.error instanceof Interruption)) { + settleWith(made, settled) + return detachAll(ctx, combination) + } + if (name === "all") { + if (!settled.ok) { + made.reject(settled.error) + return detachAll(ctx, combination) + } + combination.values[reaction.index] = settled.value as Value + return finishCombination(ctx, combination) + } + if (name === "any") { + if (settled.ok) { + made.resolve(settled.value as Value) + return detachAll(ctx, combination) + } + combination.reasons[reaction.index] = materialize(ctx, settled.error) + return finishCombination(ctx, combination) + } + combination.values[reaction.index] = settled.ok + ? record(ctx.builtins.Object, { status: "fulfilled", value: settled.value as Value }) + : record(ctx.builtins.Object, { status: "rejected", reason: materialize(ctx, settled.error) }) + finishCombination(ctx, combination) +} + +const detachAll = (ctx: Interpreter, combination: Combination) => { + for (const [item, reaction] of combination.attached) ctx.vm.scheduler.detach(item, reaction) +} + +export const finishCombination = (ctx: Interpreter, combination: Combination) => { + if (--combination.remaining > 0) return + if (combination.name === "any") { + combination.made.reject( + new Throw(createAggregateErrorValue(ctx, combination.reasons, "All promises were rejected")), + ) + return + } + combination.made.resolve(new Arr(ctx.builtins.Array, combination.values)) +} + +const instanceMethod = ( + ctx: Interpreter, + name: "then" | "catch" | "finally", + thisValue: Value, + args: Array, + site: AstNode | undefined, +): Value => { + const method = `Promise.prototype.${name}` + const promise = receiver(PromiseObj, thisValue, method) + if (name === "finally") return chainFinally(ctx, promise, reactionHandler(args[0], method), site) + const onFulfilled = name === "then" ? reactionHandler(args[0], method) : undefined + const onRejected = reactionHandler(name === "then" ? args[1] : args[0], method) + const made = capability(ctx, site) + ctx.vm.scheduler.then(promise, { kind: "handler", onFulfilled, onRejected, derived: made }) + return made.promise +} + +// Promise.prototype.finally: the cleanup runs as a reaction, then the original outcome passes through once the +// cleanup's own result settles; a failing cleanup wins. +const chainFinally = ( + ctx: Interpreter, + source: PromiseObj, + cleanup: Callable | undefined, + site: AstNode | undefined, +): PromiseObj => { + const made = capability(ctx, site) + ctx.vm.scheduler.then( + source, + cleanup === undefined ? { kind: "settle", target: made } : { kind: "finally", cleanup, target: made, site }, + ) + return made.promise +} + +const cleanUp = ( + _ctx: Interpreter, + reaction: Extract, + settled: Completion, +): JobCall | void => { + const made = reaction.target + if (!settled.ok && settled.error instanceof Interruption) return made.reject(settled.error) + return { kind: "finally", made, cleanup: reaction.cleanup, site: reaction.site, outcome: settled } +} + +const reactionHandler = (value: Value, method: string): Callable | undefined => { + if (isSupportedCallback(value)) return value + if (typeofValue(value) === "function") { + throw typeError( + `${method} cannot use this callable as a handler; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, + ) + } + return undefined +} + +const constructPromise = (ctx: Interpreter, executor: Value, site: AstNode | undefined) => { + if (!(executor instanceof Fn)) { + throw typeError("new Promise(...) expects an executor function (e.g. new Promise((resolve, reject) => { ... })).") + } + const made = capability(ctx, site) + const functions = resolvingFunctions(ctx, made) + return new Push("Promise executor", [made, executor, [functions.resolve, functions.reject]]) +} + +/** + * Lets a synchronous object result also be awaited or chained like a Promise. It inherits non-enumerable + * then/catch/finally that settle with a plain copy, so data views never show them and awaiting does not recurse. + */ +export const promiseLike = (ctx: Interpreter, value: Value): Value => { + if (!(value instanceof Obj) || value.proto !== ctx.builtins.Object) return value + changed(value) + value.proto = ctx.builtins.PromiseLike + return value +} + +const promiseLikeMethods = (ctx: Interpreter) => { + const settled = (thisValue: Value, name: string) => { + const source = receiver(Obj, thisValue, name) + const copy = new Obj(ctx.builtins.Object) + for (const [key, slot] of source.props) copy.props.set(key, { ...slot }) + return ctx.vm.scheduler.promiseResolve(copy) + } + methods( + ctx.builtins, + ctx.builtins.PromiseLike, + (["then", "catch", "finally"] as const).map((name) => [ + name, + name === "then" ? 2 : 1, + (thisValue, args, site) => instanceMethod(ctx, name, settled(thisValue, name), args, site), + ]), + ) +} + +export const promiseGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Promise + const promise = constructor(builtins, proto, { + name: "Promise", + length: 1, + call: requiresNew("Promise"), + construct: (args, _, site) => constructPromise(ctx, args[0], site), + }) + // Combinators are not callbacks: `[p].map(Promise.resolve)` must ask for an arrow function. + for (const name of promiseStatics) { + define( + promise, + name, + native(builtins, { + name, + length: 1, + call: (_, args, site) => invokePromiseMethod(ctx, name, args, site), + callback: false, + }), + hidden, + ) + } + methods(builtins, proto, [ + ["then", 2, (thisValue, args, site) => instanceMethod(ctx, "then", thisValue, args, site)], + ["catch", 1, (thisValue, args, site) => instanceMethod(ctx, "catch", thisValue, args, site)], + ["finally", 1, (thisValue, args, site) => instanceMethod(ctx, "finally", thisValue, args, site)], + ]) + promiseLikeMethods(ctx) + return promise +} diff --git a/packages/codemode/src/vm/runtime/references.ts b/packages/codemode/src/vm/runtime/references.ts new file mode 100644 index 00000000000..05cf8427f6e --- /dev/null +++ b/packages/codemode/src/vm/runtime/references.ts @@ -0,0 +1,65 @@ +import { ToolReference } from "../../tool-runtime.js" +import { invalidData } from "./model.js" +import { Callable, getOwn, isRuntimeReference, Obj, Opaque, ownKeys, type Value } from "./objects.js" + +/** Interpreter machinery that is never data, unlike a Date or Map, which cross some boundaries as copies. */ +export const isOpaque = (value: Value): boolean => value instanceof Opaque || value instanceof ToolReference + +// Depth-first search over a value tree. `match` stops the walk; `skip` prunes a subtree without matching it. +const find = ( + value: Value, + match: (current: Value) => boolean, + skip: (current: Value) => boolean, + seen: Set, +): boolean => { + const pending: Array> = [[value].values()] + while (pending.length > 0) { + const next = pending.at(-1)!.next() + if (next.done) { + pending.pop() + continue + } + const current = next.value + if (match(current)) return true + if (!(current instanceof Obj) || skip(current) || seen.has(current)) continue + seen.add(current) + pending.push( + ownKeys(current) + .map((key) => getOwn(current, key)) + .values(), + ) + } + return false +} + +const never = () => false + +export const containsRuntimeReference = (value: Value): boolean => find(value, isRuntimeReference, never, new Set()) + +export const containsOpaqueReference = (value: Value): boolean => + find(value, isOpaque, (current) => isRuntimeReference(current) && !isOpaque(current), new Set()) + +// Reject cycles before mutation so later boundary walks remain safe. +export const rejectCircularInsertion = ( + container: Obj, + value: Value, + label: string, + seen = new Set(), +): void => { + if (find(value, (current) => current === container, isRuntimeReference, seen)) { + throw invalidData(`${label} contains a circular value.`) + } +} + +export const describeValue = (value: Value): string => { + if (value === null || value === undefined) return String(value) + if (value instanceof Obj) return value.describe + if (value instanceof ToolReference) return "a tool reference" + return `a ${typeof value}` +} + +export const typeofValue = (value: Value): string => { + if (value instanceof Callable) return "function" + if (value instanceof ToolReference) return value.path.length > 0 ? "function" : "object" + return typeof value +} diff --git a/packages/codemode/src/vm/runtime/signals.ts b/packages/codemode/src/vm/runtime/signals.ts new file mode 100644 index 00000000000..01af24897cf --- /dev/null +++ b/packages/codemode/src/vm/runtime/signals.ts @@ -0,0 +1,84 @@ +import type { Cause } from "effect" +import type { AstNode, GeneratorRequestKind } from "./model.js" +import type { GeneratorObj, Value } from "./objects.js" + +// What built-ins return when their result is not a plain value (work for the VM, a generator to resume, a call to +// make, or conversions to run first), and how work completes. All of it is data; nothing here needs Effect at run time. + +export type Completion = + | { readonly ok: true; readonly value: unknown } + | { readonly ok: false; readonly error: unknown } + +export const normal = (value: unknown): Completion => ({ ok: true, value }) +export const abrupt = (error: unknown): Completion => ({ ok: false, error }) + +/** + * Work a built-in hands to the VM, by name, with its arguments: VM-side TS or a prelude function runs it at the call, + * and its result is the call's (see `Vm.settleNativeResult`). + */ +export class Push { + constructor( + readonly kind: PushKind, + readonly slots: Array, + ) {} +} + +export type PushKind = + | "JSON.stringify" + | "AsyncFromSyncIterator" + | "Promise executor" + | "Promise.try" + | "Promise combinator" + | "search" + | "extension call" + +/** Continue a suspended generator's frame with `request`: what `next`/`return`/`throw` return once it can run. */ +export class Resume { + constructor( + readonly generator: GeneratorObj, + readonly request: GeneratorRequest, + ) {} +} + +/** + * A built-in needs `values` converted to primitives (ToPrimitive with `hints`, in order) before it can go on: the + * conversions run as prelude code, then the built-in runs again from the start, and its `ordinal`-th conversion + * receives them. See `withPrimitives`. + */ +export class Convert { + constructor( + readonly hints: Hints, + readonly values: ReadonlyArray, + readonly ordinal: number, + /** Also apply ToNumber or ToString to each one before converting the next. */ + readonly coerce: boolean, + ) {} +} + +export type Hint = "number" | "string" | "default" +export type Hints = Hint | ReadonlyArray + +/** + * Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch + * it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the + * program's body interrupts the execution. + */ +export class Interruption { + constructor(readonly cause: Cause.Cause) {} +} + +/** A call a built-in hands on (`Function.prototype.call`, say): the VM makes it as the built-in's own result. */ +export class Invoke { + constructor( + readonly callable: Value, + readonly thisValue: Value, + readonly args: Array, + readonly site: AstNode | undefined, + ) {} +} + +/** What a generator body continues with: the `next`, `return`, or `throw` call that resumed it. */ +export type GeneratorRequest = { readonly kind: GeneratorRequestKind; readonly value: Value } + +/** Most activations one stack may hold, as the call-stack limit. */ +export const MAX_CALL_DEPTH = 10_000 diff --git a/packages/codemode/src/vm/runtime/source.ts b/packages/codemode/src/vm/runtime/source.ts new file mode 100644 index 00000000000..40b31deb323 --- /dev/null +++ b/packages/codemode/src/vm/runtime/source.ts @@ -0,0 +1,74 @@ +import type { Program } from "acorn" +import type { AstNode } from "./model.js" + +/** A stable reference to one AST node: its source and its index in a pre-order walk of that source's tree. */ +export type NodeId = { readonly source: string; readonly index: number } + +/** + * One parsed program text with every node numbered in pre-order, so a node can be named and found again. A built-in + * source whose bytecode was compiled ahead of time is parsed only once something asks for its nodes. + */ +export class Source { + private tree: Program | (() => Program) + private numbered: ReadonlyArray | undefined + + constructor( + readonly id: string, + readonly code: string, + program: Program | (() => Program), + ) { + this.tree = program + if (typeof program !== "function") this.numbered = numberNodes(program, this) + } + + get program(): Program { + if (typeof this.tree === "function") this.tree = this.tree() + return this.tree + } + + /** Nodes by pre-order index. */ + get nodes(): ReadonlyArray { + return (this.numbered ??= numberNodes(this.program, this)) + } + + /** The node `id` names, when it belongs to this source. */ + node(id: NodeId): AstNode | undefined { + return id.source === this.id ? this.nodes[id.index] : undefined + } +} + +const numbering = new WeakMap() + +/** The id of a node from a numbered source; nodes built outside one (none today) have none. */ +export const nodeId = (node: AstNode): NodeId | undefined => { + const entry = numbering.get(node) + return entry === undefined ? undefined : { source: entry.source.id, index: entry.index } +} + +/** The source a numbered node belongs to. */ +export const sourceOf = (node: AstNode): Source | undefined => numbering.get(node)?.source + +const isNode = (value: unknown): value is AstNode => + typeof value === "object" && value !== null && typeof (value as { readonly type?: unknown }).type === "string" + +// Acorn creates node fields in a fixed order, so walking own keys in order gives the same numbering on every parse. +const numberNodes = (root: AstNode, source: Source): Array => { + const nodes: Array = [] + const pending = [root] + for (let node = pending.pop(); node !== undefined; node = pending.pop()) { + numbering.set(node, { source, index: nodes.length }) + nodes.push(node) + const children: Array = [] + for (const key in node) { + if (key === "loc") continue + const child: unknown = (node as unknown as Record)[key] + if (Array.isArray(child)) children.push(...child.filter(isNode)) + else if (isNode(child)) children.push(child) + } + for (let index = children.length - 1; index >= 0; index--) pending.push(children[index]!) + } + return nodes +} + +/** Whether `node` is the prelude's: it is parsed without locations, so a failure there is located at the program's call. */ +export const inPrelude = (node: AstNode): boolean => node.loc === undefined || node.loc === null diff --git a/packages/codemode/src/vm/runtime/table.ts b/packages/codemode/src/vm/runtime/table.ts new file mode 100644 index 00000000000..b6b21ac455e --- /dev/null +++ b/packages/codemode/src/vm/runtime/table.ts @@ -0,0 +1,143 @@ +import type { Value } from "./objects.js" + +/** The key of a deleted entry. */ +export const deleted: unique symbol = Symbol("codemode.deleted") + +/** A position in a table's entries, kept by the table so compaction can move it. */ +export type TableCursor = { index: number; done: boolean } + +/** + * The storage behind Map and Set: entries in insertion order with tombstones, as V8's OrderedHashTable. An entry keeps + * its index until the table compacts, and the table moves every live cursor when it does, so an iterator's position is + * a plain integer. Iteration is live: entries added before a cursor reaches the end are visited, deleted ones skipped, + * and after `clear()` a cursor continues with whatever is added next. Keys compare by SameValueZero. + */ +export class OrderedTable { + readonly keys: Array = [] + readonly values: Array = [] + // Derived from `keys`: where each live key is. + private readonly index = new Map() + private cursors: Array = [] + private tombstones = 0 + private pruneAt = 16 + + get size(): number { + return this.index.size + } + + has(key: Value): boolean { + return this.index.has(key) + } + + get(key: Value): Value { + const at = this.index.get(key) + return at === undefined ? undefined : this.values[at] + } + + set(key: Value, value: Value): void { + const at = this.index.get(key) + if (at !== undefined) { + this.values[at] = value + return + } + // -0 is stored as +0, as Map and Set do. + const stored = key === 0 ? 0 : key + this.index.set(stored, this.keys.length) + this.keys.push(stored) + this.values.push(value) + } + + add(key: Value): void { + if (!this.index.has(key)) this.set(key, undefined) + } + + delete(key: Value): boolean { + const at = this.index.get(key) + if (at === undefined) return false + this.index.delete(key) + this.keys[at] = deleted + this.values[at] = undefined + this.tombstones++ + if (this.tombstones >= 16 && this.tombstones * 2 >= this.keys.length) this.compact() + return true + } + + clear(): void { + this.keys.fill(deleted) + this.values.fill(undefined) + this.index.clear() + this.tombstones = this.keys.length + this.compact() + } + + /** A cursor at the first entry, moved along by `advance`. */ + cursor(): TableCursor { + return this.track({ index: 0, done: false }) + } + + /** Starts keeping `cursor` in place across compaction. */ + track(cursor: C): C { + if (this.cursors.length >= this.pruneAt) { + this.cursors = this.cursors.filter((item) => !item.done) + this.pruneAt = Math.max(16, this.cursors.length * 2) + } + this.cursors.push(cursor) + return cursor + } + + /** The index of the cursor's next live entry, moving past it; -1 once the cursor is done, which is for good. */ + advance(cursor: TableCursor): number { + if (cursor.done) return -1 + for (let at = cursor.index; at < this.keys.length; at++) { + if (this.keys[at] === deleted) continue + cursor.index = at + 1 + return at + } + cursor.done = true + return -1 + } + + /** + * Live entries in order, through a tracked cursor, so host code may change the table (or run program code that does) + * between steps. Stopping early releases the cursor. + */ + *entries(): Generator<[Value, Value]> { + const cursor = this.cursor() + try { + for (let at = this.advance(cursor); at >= 0; at = this.advance(cursor)) { + yield [this.keys[at] as Value, this.values[at]] + } + } finally { + cursor.done = true + } + } + + [Symbol.iterator](): Generator<[Value, Value]> { + return this.entries() + } + + *liveKeys(): Generator { + for (const [key] of this.entries()) yield key + } + + // Drops the tombstones; a cursor moves to the count of live entries before it, which is where it now points. + private compact(): void { + const before: Array = [] + let live = 0 + for (let at = 0; at < this.keys.length; at++) { + before.push(live) + const key = this.keys[at] + if (key === deleted) continue + this.keys[live] = key + this.values[live] = this.values[at] + this.index.set(key, live) + live++ + } + before.push(live) + this.keys.length = live + this.values.length = live + this.tombstones = 0 + this.cursors = this.cursors.filter((cursor) => !cursor.done) + for (const cursor of this.cursors) cursor.index = before[Math.min(cursor.index, before.length - 1)]! + } +} diff --git a/packages/codemode/src/vm/scheduler.ts b/packages/codemode/src/vm/scheduler.ts new file mode 100644 index 00000000000..6b24eb98179 --- /dev/null +++ b/packages/codemode/src/vm/scheduler.ts @@ -0,0 +1,159 @@ +import type { Json } from "./data.js" +import type { ExtensionInvocation } from "../tool-runtime.js" +import { locate } from "./runtime/errors.js" +import type { AstNode } from "./runtime/model.js" +import type { Callable, Cursor, GeneratorObj, Obj, PromiseObj, Value } from "./runtime/objects.js" +import type { Completion } from "./runtime/signals.js" +import type { Frame } from "./vm.js" + +// The scheduler's records: jobs, reactions, resolvers, and host calls. All of them are data the VM keeps, so a run's +// queue and the work it waits on can be read (and saved) between `run()`s. + +/** What the member reactions of one `Promise.all`/`allSettled`/`race`/`any` share. */ +export type Combination = { + readonly name: "all" | "allSettled" | "race" | "any" + /** Settles the combinator's result. */ + readonly made: Resolving + readonly values: Array + readonly reasons: Array + /** Members still to settle, plus one until iteration ends. */ + remaining: number + /** Every member's reaction, detached once the result is decided. */ + readonly attached: Array +} + +/** + * A settled promise's reaction, as data. It runs as a job (see `react` in promises.ts) and either finishes at once or + * starts a frame that calls program code. The kinds: + * - `await`: resume the frames parked on the promise, cut off the job stack at an `await`. + * - `settle`: pass the outcome on to `target`. + * - `handler`: `then`/`catch` handlers; the handler's result settles `derived`. + * - `combinator`: one member of a `Promise` combinator. + * - `finally`, `finallyCleaned`, `finallyOutcome`: the steps of `Promise.prototype.finally` with a cleanup. + * - `awaitReturn`: an async generator's `return(value)` once `value` settles. + * - `asyncFromSync`: an async-from-sync iterator method's result value settled: answer `made` with it, closing the + * synchronous iterator `close` first on a rejection when one is given. + */ +export type Reaction = + | { readonly kind: "await"; readonly frames: Array } + | { readonly kind: "settle"; readonly target: Resolving } + | { + readonly kind: "handler" + readonly onFulfilled: Callable | undefined + readonly onRejected: Callable | undefined + readonly derived: Resolving + } + | { readonly kind: "combinator"; readonly combination: Combination; readonly index: number; called: boolean } + | { + readonly kind: "finally" + readonly cleanup: Callable + readonly target: Resolving + readonly site: AstNode | undefined + } + | { readonly kind: "finallyCleaned"; readonly target: Resolving } + | { readonly kind: "finallyOutcome"; readonly target: Resolving; readonly outcome: Completion } + | { readonly kind: "awaitReturn"; readonly generator: GeneratorObj } + | { + readonly kind: "asyncFromSync" + readonly made: Resolving + readonly done: boolean + readonly close: Cursor | undefined + readonly site: AstNode + } + +/** One queued job, as data: run a settled promise's reaction (resuming parked frames, say), or adopt a thenable. */ +export type Job = + | { readonly kind: "reaction"; readonly reaction: Reaction; readonly settled: Completion } + | { readonly kind: "thenable"; readonly promise: PromiseObj; readonly thenable: Obj; readonly then: Value } + +/** + * What host work a call asks for, as data the driver turns into an Effect: a tool call, a search, or a step of an + * extension function's call (see `ExtensionRequest`). + */ +export type HostRequest = + | { readonly kind: "tool"; readonly path: ReadonlyArray; readonly args: Array } + | { readonly kind: "search"; readonly args: Array } + | { readonly kind: "extension"; readonly call: ExtensionRequest } + +/** + * One step of a call to an extension function: its `extension.before` hook (`before`), settling what the host function + * returned or threw (`settle`: `host` is that), or calling an async host function and settling its result (`run`: + * `host` is the function). `invocation` is what the hooks see, for a global or a global object's function. The host + * function and what it returns are live host values with no data form, dropped once the call completes. + */ +export type ExtensionRequest = { + readonly stage: "before" | "settle" | "run" + readonly label: string + invocation: ExtensionInvocation | undefined + host: unknown + args: ReadonlyArray + readonly threw: boolean +} + +/** + * Host work the program waits on. The VM queues it and hands it to the driver at its next stop; the driver runs it and + * reports how it ended with `Vm.complete`. A concurrent call's outcome waits in the inbox and settles its promise in a + * later job; a blocking call stops the VM, and no other job runs until it ends. + */ +export class HostCall { + /** Where the call is: `queued` (written ahead, not started), `running`, or `completed`. */ + status: "queued" | "running" | "completed" = "queued" + constructor( + readonly id: number, + readonly request: HostRequest, + /** What runs: a tool path, or an extension function's label. */ + readonly label: string, + /** The running job waits for it: its outcome continues the frame that asked, before any other job. */ + readonly blocking: boolean, + /** How a concurrent call's result becomes a program value: a tool's crosses as JSON, an extension's is one. */ + readonly result: "json" | "value", + /** Settles a concurrent call's promise. */ + readonly resolving: Resolving | undefined, + /** The call that started the work, where a defect it raises is located. */ + readonly site: AstNode | undefined, + ) {} +} + +/** + * CreateResolvingFunctions as data: settles one promise at most once, however many `Resolver`s share it. `reject` takes + * the failure as thrown; one without a location is located at `site`, the call that created the promise. + */ +export class Resolving { + done = false + constructor( + private readonly scheduler: Settles, + readonly promise: PromiseObj, + readonly site: AstNode | undefined, + ) {} + + resolve(resolution: Value): void { + if (this.done) return + this.done = true + this.scheduler.resolveWith(this.promise, resolution, this.site) + } + + reject(reason: unknown): void { + if (this.done) return + this.done = true + this.scheduler.reject(this.promise, locate(reason, this.site)) + } +} + +type Settles = { + resolveWith(promise: PromiseObj, resolution: Value, site?: AstNode): void + reject(promise: PromiseObj, reason: unknown): void +} + +/** + * Why `Vm.run` stopped: + * - `done`: the program's body ended, with its completion. + * - `host`: start `calls`, newly queued host work, in order (a blocking call is last, and the VM continues only once + * it completes). With no calls, the VM waits for host work already running to complete. + * - `slice`: the step budget is used up; let the host run, then continue. + * - `stuck`: nothing can run or settle any more; `node` is the `await` that waits forever, when one is known. + */ +export type Stop = + | { readonly kind: "done"; readonly completion: Completion } + | { readonly kind: "host"; readonly calls: ReadonlyArray } + | { readonly kind: "slice" } + | { readonly kind: "stuck"; readonly node: AstNode | undefined } diff --git a/packages/codemode/src/vm/snapshot.ts b/packages/codemode/src/vm/snapshot.ts new file mode 100644 index 00000000000..41c396b8845 --- /dev/null +++ b/packages/codemode/src/vm/snapshot.ts @@ -0,0 +1,333 @@ +import packageJson from "../../package.json" with { type: "json" } +import type { Interruption } from "./runtime/signals.js" +import { staticNames, unchanged } from "./runtime/intrinsic-names.js" +import { Fn, Obj } from "./runtime/objects.js" +import { nodeId, type Source } from "./runtime/source.js" +import { type ClassSpec, codecShape, decode, encode, type Json, SnapshotError } from "./snapshot/codec.js" +import { FORMAT, hash, hostsId, type Snapshot, validate } from "./snapshot/snapshot.js" +import { AsyncFromSyncCloseTarget, FinallyTarget, VmScheduler } from "./async.js" +import { Code, hashOf } from "./code.js" +import { COMPILER_VERSION, compileSource } from "./compiler/compile.js" +import { HostCalls } from "./host.js" +import { getInternalPrelude } from "./internal-prelude.js" +import { compiledPrelude } from "./prelude/prelude.js" +import { Cell, CodeFlags, EMPTY_CELLS, HOLE, Op } from "./opcodes.js" +import { CustomIter, Frame, Mode, Vm, VmInternalFn } from "./vm.js" + +// Snapshots of the bytecode VM (plan §6): the generic graph codec, plus records for the VM's own classes. +// +// - **Code is never saved.** A function's code is `["x", ["code", source, index]]`; restore recompiles every saved +// source eagerly and refuses one whose bytecode hash differs from the saved one, so a pc always means the same +// instruction. The prelude and the VM's internal helpers are named by the build id, which hashes their bytecode. +// - **A frame** saves its registers up to `sp` (locals, then the live operand stack), its function, pc, mode and the +// promise or generator it settles. Restore checks it against its code before anything runs (§6.5). +// - **The VM's internal callables** (`$` intrinsics, internal helpers) are named, never copied. +// - New VM state must be listed in the `Vm` spec: an unknown own field is a codec gap, so nothing is silently dropped. + +export type CaptureOptions = { + /** Throw on the first value with no data form, instead of reporting it in `lost`. */ + readonly strict: boolean +} + +export type RestoreOptions = { + /** Parses a saved program source (cached by text). */ + readonly parse: (code: string) => Source + readonly interruption: () => Interruption +} + +let specs: ReadonlyArray | undefined + +// Built on first use: this module and vm.ts import each other, so the classes aren't there at load time. +const vmSpecs = (): ReadonlyArray => + (specs ??= [ + { + name: "Vm", + ctor: Vm, + anchor: "vm", + fields: ["frames", "templates", "scheduler", "hosts", "depth", "sliced", "outcome"], + skip: ["ctx", "pool", "sourceCodes", "intrinsics", "fuel", "sliceStart", "tripEvery"], + reset: restoreStack, + }, + { + name: "VmScheduler", + ctor: VmScheduler, + anchor: "scheduler", + fields: ["intrinsicThen", "parked", "queue", "head", "pending", "nextID", "unhandled"], + skip: ["vm"], + }, + { + name: "HostCalls", + ctor: HostCalls, + anchor: "hosts", + fields: ["queued", "running", "inbox", "nextID", "blocked", "input"], + skip: ["ctx", "promises", "extension", "extensionResume"], + }, + { + name: "Frame", + ctor: Frame, + fields: ["fn", "code", "pc", "sp", "regs", "thisValue", "args", "mode", "target", "answer", "origin", "counted"], + view: (frame: Frame) => [ + frame.fn, + frame.code, + frame.pc, + frame.sp, + frame.regs.slice(0, frame.sp), + frame.thisValue, + frame.args, + frame.mode, + frame.target, + frame.answer, + frame.origin, + frame.counted, + ], + reset: restoreFrame, + }, + { + name: "Fn", + ctor: Fn, + fields: ["code", "cells"], + skip: ["length"], + obj: true, + constants: { tag: "Function" }, + reset: restoreFn, + }, + { name: "VmInternalFn", ctor: VmInternalFn, fields: ["code", "boundArgs"] }, + { name: "FinallyTarget", ctor: FinallyTarget, fields: ["vm", "made", "site", "outcome"] }, + { name: "AsyncFromSyncCloseTarget", ctor: AsyncFromSyncCloseTarget, fields: ["made", "reason"] }, + { name: "Cell", ctor: Cell, fields: ["v"] }, + { name: "CustomIter", ctor: CustomIter, fields: ["done", "rawGen", "iterator", "next"] }, + ]) + +let build: string | undefined + +/** + * The VM build a snapshot belongs to: the package version, and a hash of the format, the opcode table, the frame + * modes, the compiler version, the prelude's and the internal helpers' bytecode, the static intrinsic names, and the + * codec's shape. A runtime must have been made first (the intrinsic names come from it). + */ +export const buildId = (): string => + (build ??= `${packageJson.version}+${hash( + [ + FORMAT, + JSON.stringify(Object.entries(Op)), + JSON.stringify(Object.entries(Mode)), + COMPILER_VERSION, + hashOf(compiledPrelude().codes), + hashOf(getInternalPrelude().codes), + staticNames().join("\n"), + codecShape(vmSpecs()), + ].join("\n"), + )}`) + +/** Saves a VM's state as it is now, with every built-in or host global the run changed. Changes nothing. */ +export const captureVm = ( + vm: Vm, + options: CaptureOptions, +): { readonly snapshot: Snapshot; readonly lost: ReadonlyArray } => { + vm.compact() + const ctx = vm.ctx + const lost: Array = [] + const names = ctx.intrinsics + const programs = [...ctx.sources.values()] + const sources = sourcesOf(vm) + const anchors = new Map(anchorsOf(vm)) + const internals = new Map() + for (const [name, value] of vm.intrinsics) if (!(value instanceof Obj)) internals.set(value, name) + const encoded = encode([vm, ctx, ctx.globalScope, ...names.changed()], { + nameOf: (value) => names.nameOf(value), + unchanged, + anchorOf: (value) => anchors.get(value), + nodeId: (node) => { + const id = nodeId(node) + return id !== undefined && sources.get(id.source)?.nodes[id.index] === node ? [id.source, id.index] : undefined + }, + lost, + strict: options.strict, + specs: vmSpecs(), + external: (value): Json | undefined => { + if (value === HOLE) return "hole" + if (typeof value === "symbol" || value instanceof Obj) return undefined + const internal = internals.get(value) + if (internal !== undefined) return ["intrinsic", internal] + return codeRef(vm, value) + }, + }) + const snapshot: Snapshot = { + format: FORMAT, + header: { + build: buildId(), + hosts: hostsId(ctx), + sources: programs.map((source) => ({ + id: source.id, + code: source.code, + hash: hash(source.code), + bytecode: vm.sourceCodes.get(source.id)![0]!.source.hash, + })), + toolCalls: ctx.tools.calls.map((call) => ({ name: call.name })), + }, + roots: encoded.roots, + records: encoded.records, + } + return { snapshot, lost } +} + +/** + * Restores a saved state into `vm`, a fresh one made for a new runtime with the same host globals and no bindings, + * after checking the snapshot belongs to this build, these host globals, its own sources and their bytecode. + */ +export const restoreVm = (vm: Vm, saved: unknown, options: RestoreOptions): Snapshot => { + const snapshot = validate(saved) + const header = snapshot.header + const ctx = vm.ctx + const expected = buildId() + if (snapshot.format !== FORMAT || header.build !== expected) { + throw new SnapshotError( + `This state was saved by another Code Mode build (${header.build}); this runtime is ${expected}, so it can't resume it.`, + ) + } + if (header.hosts !== hostsId(ctx)) { + throw new SnapshotError( + "This state was saved by a runtime with other host globals (extensions); resume it with the same ones.", + ) + } + for (const source of header.sources) { + if (hash(source.code) !== source.hash) throw new SnapshotError(`Source ${source.id} does not match its hash.`) + const parsed = options.parse(source.code) + if (parsed.id !== source.id || vm.sourceCodes.has(source.id)) { + throw new SnapshotError(`Source ${source.id} is not a program source.`) + } + const codes = compileSource(parsed) + if (codes[0]!.source.hash !== source.bytecode) { + throw new SnapshotError(`Source ${source.id} compiles to other bytecode in this build, so it can't resume.`) + } + ctx.sources.set(source.id, parsed) + vm.sourceCodes.set(source.id, codes) + } + const sources = sourcesOf(vm) + const anchors = new Map(anchorsOf(vm).map(([object, name]) => [name, object])) + decode(snapshot, { + named: (name) => ctx.intrinsics.valueOf(name), + anchor: (name) => anchors.get(name), + node: (source, index) => sources.get(source)?.nodes[index], + interruption: options.interruption, + specs: vmSpecs(), + external: (data) => { + if (data === "hole") return HOLE + if (!Array.isArray(data)) return undefined + if (data[0] === "intrinsic" && typeof data[1] === "string") { + const value = vm.intrinsics.get(data[1]) + return value instanceof Obj ? undefined : value + } + if (data[0] === "code" && typeof data[1] === "string" && typeof data[2] === "number") { + return vm.sourceCodes.get(data[1])?.[data[2]] + } + return undefined + }, + }) + ctx.tools.calls.push(...header.toolCalls.map((call) => ({ name: call.name }))) + return snapshot +} + +// --- Helpers --- + +// The objects a fresh runtime already has, saved by name and filled in place on restore. +const anchorsOf = (vm: Vm): ReadonlyArray => [ + [vm, "vm"], + [vm.scheduler, "scheduler"], + [vm.hosts, "hosts"], + [vm.ctx, "ctx"], + [vm.ctx.globalScope, "globals"], +] + +// Every source a saved node can belong to: the programs, and the prelude's and the internal helpers' sources. +const sourcesOf = (vm: Vm): Map => { + const sources = new Map(vm.ctx.sources) + for (const codes of vm.sourceCodes.values()) { + const source = codes[0]?.sourceObj + if (source !== undefined) sources.set(source.id, source) + } + return sources +} + +const codeRef = (vm: Vm, code: object): Json | undefined => { + if (!(code instanceof Code)) return undefined + if (vm.sourceCodes.get(code.source.id)?.[code.index] !== code) { + throw new SnapshotError(`Codec gap: function ${code.index} of ${code.source.id} is not this run's code.`) + } + return ["code", code.source.id, code.index] +} + +const invalid = (message: string): never => { + throw new SnapshotError(`Invalid snapshot: ${message}`) +} + +// A frame as saved, checked against its code before anything runs (§6.5): pc on an instruction, sp within the +// frame's registers, holes only in locals, finally tags in range, a mode it can complete by. Then its registers are +// padded to the code's size and its cells taken from its function. +function restoreFrame(frame: Frame): void { + const code = frame.code as unknown + if (!(code instanceof Code)) return invalid("a frame has no code.") + const regs = frame.regs as unknown + if (!Array.isArray(regs)) return invalid("a frame's registers are not a list.") + const pc = frame.pc + if (!Number.isSafeInteger(pc) || pc < 0 || pc >= code.code.length) + return invalid(`a frame's pc ${pc} is out of range.`) + const sp = frame.sp + if (!Number.isSafeInteger(sp) || sp < code.locals || sp > code.locals + code.maxStack || regs.length !== sp) { + return invalid(`a frame's stack pointer ${sp} is out of range.`) + } + for (let at = code.locals; at < sp; at++) if (regs[at] === HOLE) invalid("a hole is on a frame's operand stack.") + for (let level = 0; level < code.finallyOffsets.length; level++) { + const offset = code.finallyOffsets[level]! + const tag = regs[code.finallies[offset]!] + const targets = code.finallies[offset + 3]! + if ( + tag !== HOLE && + tag !== undefined && + !(Number.isSafeInteger(tag) && (tag as number) >= 0 && (tag as number) < targets) + ) { + invalid(`a frame's finally tag ${String(tag)} is out of range.`) + } + } + if (!(Object.values(Mode) as Array).includes(frame.mode)) + invalid(`a frame's mode ${String(frame.mode)} is unknown.`) + if (frame.mode === Mode.PROGRAM && (code.flags & CodeFlags.PROGRAM) === 0) + invalid("a frame's mode does not fit its code.") + const settles = frame.mode !== Mode.CALLER && frame.mode !== Mode.PROGRAM && frame.mode !== Mode.BOUNDARY_OUT + if (!settles && frame.target !== undefined) invalid("a frame that settles nothing has a target.") + const fn = frame.fn as unknown + if (fn !== null && !(fn instanceof Fn)) invalid("a frame's function is not a function.") + if (fn instanceof Fn && fn.code !== code) invalid("a frame's function does not run its code.") + if (typeof frame.counted !== "boolean") invalid("a frame's call depth flag is not a boolean.") + if (frame.args !== undefined && !Array.isArray(frame.args)) invalid("a frame's arguments are not a list.") + for (let at = sp, size = code.locals + code.maxStack; at < size; at++) regs.push(HOLE) + frame.cells = fn instanceof Fn ? fn.cells : EMPTY_CELLS +} + +// The job stack, checked: frames only, the program's frame only at the bottom, and a caller under every `CALLER` one. +function restoreStack(vm: Vm): void { + const frames = vm.frames as unknown + if (!Array.isArray(frames) || frames.some((frame) => !(frame instanceof Frame))) + invalid("the job stack is not frames.") + vm.frames.forEach((frame, at) => { + const bottomOnly = frame.mode === Mode.PROGRAM || frame.mode === Mode.BOUNDARY_OUT + if ((at === 0 && frame.mode === Mode.CALLER) || (at > 0 && bottomOnly)) + invalid("a frame is out of place on the job stack.") + }) + if (!Number.isSafeInteger(vm.depth) || vm.depth < 0) invalid("the call depth is not a count.") +} + +// A closure's code and cells, checked; its length comes from its code. +function restoreFn(fn: Fn): void { + const code = fn.code as unknown + if (!(code instanceof Code)) return invalid("a closure has no code.") + const cells = fn.cells as unknown + if ( + !Array.isArray(cells) || + cells.length !== code.captures.length >>> 1 || + cells.some((cell) => !(cell instanceof Cell)) + ) { + invalid("a closure's cells do not match its captures.") + } + Object.assign(fn, { length: code.length }) +} diff --git a/packages/codemode/src/vm/snapshot/codec.ts b/packages/codemode/src/vm/snapshot/codec.ts new file mode 100644 index 00000000000..a6356ab8882 --- /dev/null +++ b/packages/codemode/src/vm/snapshot/codec.ts @@ -0,0 +1,870 @@ +import { Interpreter } from "../runtime/interpreter.js" +import { + type AstNode, + AsyncIteratorSymbol, + IteratorSymbol, + Lost, + lostError, + PendingThrow, + Throw, +} from "../runtime/model.js" +import { + Arguments, + Arr, + BoundFn, + Bytes, + changed, + DateObj, + ErrorObj, + GeneratorObj, + HeadersObj, + IteratorObj, + MapObj, + Native, + Obj, + Position, + PromiseObj, + RegExpObj, + Resolver, + SetObj, + type Slot, + URLObj, + type Value, + URLSearchParamsObj, + WeakMapObj, + WeakSetObj, +} from "../runtime/objects.js" +import { Convert, Interruption, Invoke } from "../runtime/signals.js" +import { deleted, OrderedTable } from "../runtime/table.js" +import { AsyncFromSyncIterator } from "../generators.js" +import { HostCall, Resolving } from "../scheduler.js" +import { TextDecoderObj } from "../stdlib/bytes.js" +import { ToolError, toolError } from "../../tool-error.js" +import { ToolReference, ToolRuntimeError } from "../../tool-runtime.js" + +// A graph codec for a run's state: every object reachable from the roots becomes one record, so identity, sharing, +// and cycles survive, and every value inside a record is tagged JSON. Built-ins, AST nodes, bytecode, and the VM's +// own objects are named rather than copied; a value with no data form (a live host function, say) becomes a `Lost` +// placeholder. Nothing here evaluates anything: decoding only allocates and assigns. + +/** A plain JSON value, as a snapshot holds it. */ +export type Json = null | boolean | number | string | ReadonlyArray | { readonly [key: string]: Json } + +/** + * A value inside a record: a JSON primitive, or a tag (an array whose first item says what it is): + * - `["u"]` undefined, `["n", "NaN" | "Infinity" | "-Infinity" | "-0"]` a number JSON cannot spell, `["-"]` an absent + * field, `["h"]` an array hole; + * - `["s", name]` an interpreter symbol; + * - `["i", name]` a built-in or host global, unchanged since the runtime was made; + * - `["a", source, index]` an AST node (a call site, an error's location); + * - `["x", data]` something the engine names itself (the VM's compiled code, by source and function index); + * - `["r", id]` another record. + */ +export type Encoded = string | number | boolean | null | ReadonlyArray + +/** One object of the graph. `t` says how to read the rest. */ +export type Record = { readonly t: string; readonly [field: string]: Json } + +/** A saved state can't be read, or doesn't fit this runtime. */ +export class SnapshotError extends Error { + override readonly name = "SnapshotError" +} + +/** What the encoder asks of the runtime whose state it saves. */ +export type EncodeEnv = { + /** The name of a built-in or host global object. */ + readonly nameOf: (value: Obj) => string | undefined + /** Whether a named object is still as the runtime made it, so its name alone restores it. */ + readonly unchanged: (value: Obj) => boolean + /** The anchors: objects the restored runtime already has, filled in place (the VM, its scheduler, ...). */ + readonly anchorOf: (value: object) => string | undefined + readonly nodeId: (node: AstNode) => readonly [source: string, index: number] | undefined + /** Values with no data form are reported here; strict encoding throws on the first instead. */ + readonly lost: Array + readonly strict: boolean + /** More classes to save, an engine's own (the VM's frames and closures), after the built-in ones. */ + readonly specs?: ReadonlyArray | undefined + /** The data an engine names an object or symbol by, saved as `["x", data]`, instead of copying it. */ + readonly external?: ((value: object | symbol) => Json | undefined) | undefined +} + +/** What the decoder asks of the fresh runtime it restores into. */ +export type DecodeEnv = { + readonly named: (name: string) => Obj | undefined + readonly anchor: (name: string) => object | undefined + readonly node: (source: string, index: number) => AstNode | undefined + /** An `Interruption` (it holds an Effect cause, which this module does not make). */ + readonly interruption: () => Interruption + readonly specs?: ReadonlyArray | undefined + /** What `EncodeEnv.external` named. */ + readonly external?: ((data: Json) => unknown) | undefined +} + +// --- Registries --- + +const symbols = new Map([ + ["iterator", IteratorSymbol], + ["asyncIterator", AsyncIteratorSymbol], + ["deleted", deleted], +]) +const symbolNames = new Map([...symbols].map(([name, symbol]) => [symbol, name])) + +/** How one class saves: its own fields in order, and how a restored instance is made whole. */ +export type ClassSpec = { + readonly name: string + readonly ctor: Function + /** Saved fields, in the order a constructor makes them. */ + readonly fields: ReadonlyArray + /** Own fields that are not saved: caches and runtime configuration, set to `reset`'s values. */ + readonly skip?: ReadonlyArray + /** Own fields with the same value on every instance. */ + readonly constants?: Readonly<{ [field: string]: unknown }> + /** An `Obj`: its prototype, extensibility, and properties are saved too. */ + readonly obj?: boolean + /** The restored runtime already has this object; it is filled in place. */ + readonly anchor?: string + /** Sets fields that are not saved, once the saved ones are in. */ + readonly reset?: (value: never) => void + /** The saved fields' values, when they are not the own fields as they are (a frame saves its live registers only). */ + readonly view?: (value: never) => ReadonlyArray +} + +// `intrinsic` is not saved: a restored object is the program's, or a named intrinsic that keeps its own. +const objFields = ["props", "extensible", "proto", "intrinsic"] +const callable = { tag: "Function" } + +// Built on first use: the VM's modules and this one import each other, so the classes aren't there at load time. +const builtinSpecs = (): ReadonlyArray => [ + { name: "Obj", ctor: Obj, fields: [], obj: true, constants: { tag: "Object" } }, + { name: "Arr", ctor: Arr, fields: ["items", "elements"], obj: true, constants: { tag: "Array" } }, + { name: "ErrorObj", ctor: ErrorObj, fields: ["failure"], obj: true, constants: { tag: "Error" } }, + { name: "Arguments", ctor: Arguments, fields: [], obj: true, constants: { tag: "Arguments" } }, + // Only named natives are saved (by name); an unnamed one is a host function, saved as `LostNative`. + { name: "Native", ctor: Native, fields: [], skip: ["length", "call", "construct", "callback"], obj: true }, + { + name: "BoundFn", + ctor: BoundFn, + fields: ["length", "target", "boundThis", "boundArgs"], + obj: true, + constants: { ...callable, construct: undefined, callback: true }, + }, + { + name: "Resolver", + ctor: Resolver, + fields: ["length", "resolving", "kind"], + obj: true, + constants: { ...callable, construct: undefined, callback: true }, + }, + { + name: "PromiseObj", + ctor: PromiseObj, + fields: ["state", "result", "reactions", "handled", "id"], + obj: true, + constants: { tag: "Promise" }, + }, + { + name: "GeneratorObj", + ctor: GeneratorObj, + fields: ["state", "frame", "queue", "asynchronous"], + obj: true, + constants: { tag: "Generator" }, + }, + { name: "IteratorObj", ctor: IteratorObj, fields: ["cursor"], obj: true, constants: { tag: "Iterator" } }, + { name: "DateObj", ctor: DateObj, fields: ["time"], obj: true, constants: { tag: "Date" } }, + { + name: "RegExpObj", + ctor: RegExpObj, + fields: ["source", "flags", "lastIndex"], + skip: ["compiled"], + obj: true, + constants: { tag: "RegExp" }, + reset: (value: { compiled: unknown }) => { + value.compiled = undefined + }, + }, + { name: "MapObj", ctor: MapObj, fields: ["map"], obj: true, constants: { tag: "Map" } }, + { name: "SetObj", ctor: SetObj, fields: ["set"], obj: true, constants: { tag: "Set" } }, + { name: "WeakMapObj", ctor: WeakMapObj, fields: ["map"], obj: true, constants: { tag: "WeakMap" } }, + { name: "WeakSetObj", ctor: WeakSetObj, fields: ["set"], obj: true, constants: { tag: "WeakSet" } }, + { + name: "URLSearchParamsObj", + ctor: URLSearchParamsObj, + fields: ["params"], + obj: true, + constants: { tag: "URLSearchParams" }, + }, + { name: "HeadersObj", ctor: HeadersObj, fields: ["headers"], obj: true, constants: { tag: "Headers" } }, + { name: "URLObj", ctor: URLObj, fields: ["searchParams", "url"], obj: true, constants: { tag: "URL" } }, + { name: "Bytes", ctor: Bytes, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } }, + { name: "TextDecoderObj", ctor: TextDecoderObj, fields: ["decoder"], obj: true, constants: { tag: "Object" } }, + { + name: "AsyncFromSyncIterator", + ctor: AsyncFromSyncIterator, + fields: ["sync", "site"], + obj: true, + constants: { tag: "AsyncFromSyncIterator" }, + }, + { name: "Position", ctor: Position, fields: ["index", "done", "source", "kind", "arrays"] }, + { + name: "OrderedTable", + ctor: OrderedTable, + fields: ["keys", "values", "cursors", "tombstones", "pruneAt"], + skip: ["index"], + // The index is derived from the keys. + reset: (table: { keys: Array; index: Map }) => { + table.index = new Map() + table.keys.forEach((key, at) => { + if (key !== deleted) table.index.set(key, at) + }) + }, + }, + { + name: "HostCall", + ctor: HostCall, + fields: ["status", "id", "request", "label", "blocking", "result", "resolving", "site"], + }, + { name: "Resolving", ctor: Resolving, fields: ["done", "scheduler", "promise", "site"] }, + { name: "Throw", ctor: Throw, fields: ["value"] }, + { name: "PendingThrow", ctor: PendingThrow, fields: ["type", "message", "kind", "suggestions", "node", "value"] }, + { name: "Convert", ctor: Convert, fields: ["hints", "values", "ordinal", "coerce"] }, + { name: "Invoke", ctor: Invoke, fields: ["callable", "thisValue", "args", "site"] }, + { name: "ToolReference", ctor: ToolReference, fields: ["path"] }, + { name: "Lost", ctor: Lost, fields: ["description"] }, + { + name: "Interpreter", + ctor: Interpreter, + anchor: "ctx", + fields: ["logs", "globalScope", "modules", "namespaces"], + skip: ["tools", "builtins", "vm", "intrinsics", "extensionWork", "sources", "prelude"], + }, +] + +let builtins: { readonly byCtor: Map; readonly byName: Map } | undefined + +const builtinRegistry = () => { + if (builtins !== undefined) return builtins + const specs = builtinSpecs() + builtins = { + byCtor: new Map(specs.map((spec) => [spec.ctor, spec])), + byName: new Map(specs.map((spec) => [spec.name, spec])), + } + return builtins +} + +// A function an extension made while the program ran, which has no data form: saved as `LostNative`. +const hostFunction: ClassSpec = { + name: "LostNative", + ctor: Native, + fields: ["length"], + obj: true, + constants: callable, +} + +/** Host error classes that can travel as a failure, by name. */ +const errorClasses = new Map([ + ["Error", Error], + ["TypeError", TypeError], + ["RangeError", RangeError], + ["SyntaxError", SyntaxError], + ["ReferenceError", ReferenceError], + ["EvalError", EvalError], + ["URIError", URIError], + ["ToolRuntimeError", ToolRuntimeError], + ["ToolError", ToolError], +]) +const errorNames = new Map([...errorClasses].map(([name, ctor]) => [ctor, name])) + +/** The names of every class with a codec: a build that saves a different set cannot read this one's snapshots. */ +export const codecShape = (extra: ReadonlyArray = []): string => + JSON.stringify([[...builtinSpecs(), ...extra].map((spec) => [spec.name, spec.fields]), [...symbols.keys()]]) + +const registry = (extra: ReadonlyArray | undefined) => { + const { byCtor, byName } = builtinRegistry() + return extra === undefined || extra.length === 0 + ? { byCtor, byName } + : { + byCtor: new Map([...byCtor, ...extra.map((spec) => [spec.ctor, spec] as const)]), + byName: new Map([...byName, ...extra.map((spec) => [spec.name, spec] as const)]), + } +} + +// --- Encoding --- + +/** Encodes everything reachable from `roots`: the roots' values and the records they refer to. */ +export const encode = ( + roots: ReadonlyArray, + env: EncodeEnv, +): { readonly roots: Array; readonly records: Array } => { + const encoder = new Encoder(env) + const encoded = roots.map((root) => encoder.value(root)) + encoder.drain() + return { roots: encoded, records: encoder.records as Array } +} + +class Encoder { + readonly records: Array = [] + private readonly ids = new Map() + private readonly work: Array = [] + // A URL's `searchParams` is the same host object its URLSearchParams wraps; it is encoded once every URL is known. + private readonly owners = new Map() + private readonly params: Array = [] + private readonly specs: { readonly byCtor: Map; readonly byName: Map } + + constructor(private readonly env: EncodeEnv) { + this.specs = registry(env.specs) + } + + value(value: unknown): Encoded { + switch (typeof value) { + case "string": + case "boolean": + return value + case "undefined": + return ["u"] + case "number": + if (Number.isFinite(value) && !Object.is(value, -0)) return value + return ["n", Object.is(value, -0) ? "-0" : String(value)] + case "symbol": { + const name = symbolNames.get(value) + const external = name === undefined ? this.env.external?.(value) : undefined + if (external !== undefined) return ["x", external] + if (name === undefined) throw new SnapshotError(`Codec gap: the symbol ${String(value)} has no name.`) + return ["s", name] + } + case "bigint": + throw new SnapshotError("Codec gap: a bigint has no data form.") + } + if (value === null) return null + const object = value as object + const external = this.env.external?.(object) + if (external !== undefined) return ["x", external] + if (object instanceof Obj) { + const name = this.env.nameOf(object) + if (name !== undefined && this.env.unchanged(object)) return ["i", name] + } else if (!Array.isArray(object)) { + const node = this.env.nodeId(object as AstNode) + if (node !== undefined) return ["a", node[0], node[1]] + } + return this.ref(object) + } + + drain(): void { + for (let object = this.work.shift(); object !== undefined; object = this.work.shift()) { + const id = this.ids.get(object)! + this.records[id] = this.record(object, id) + } + for (const [id, params] of this.params) { + const owner = this.owners.get(params) + this.records[id] = + owner === undefined + ? { t: "URLSearchParams", s: params.toString() } + : { t: "URLSearchParams", o: this.ref(owner) } + } + } + + private ref(object: object): Encoded { + const known = this.ids.get(object) + if (known !== undefined) return ["r", known] + const id = this.records.length + this.ids.set(object, id) + this.records.push(undefined) + this.work.push(object) + return ["r", id] + } + + private record(object: object, id: number): Record | undefined { + if (object instanceof Obj) { + const name = this.env.nameOf(object) + if (name !== undefined) { + const spec = this.spec(object) + return { t: "@", n: name, c: spec.name, ...this.fields(object, spec) } + } + } + if (Array.isArray(object)) { + // A sparse array (`length` can be ten million with nothing in it) saves only the indexes it has. + if (object.length > 1024) { + const present = Object.keys(object) + if (present.length * 2 < object.length) { + const entries: Array = [] + for (const key of present) entries.push(Number(key), this.value(object[Number(key)])) + return { t: "Array", l: object.length, s: entries } + } + } + const items: Array = [] + for (let at = 0; at < object.length; at++) items.push(at in object ? this.value(object[at]) : ["h"]) + return { t: "Array", v: items } + } + if (object instanceof Map) { + const entries: Array = [] + for (const [key, item] of object) entries.push(this.value(key), this.value(item)) + const anchor = this.env.anchorOf(object) + return anchor === undefined ? { t: "Map", e: entries } : { t: "Map", e: entries, a: anchor } + } + if (object instanceof Set) return { t: "Set", e: [...object].map((item) => this.value(item)) } + const spec = this.specs.byCtor.get(object.constructor) + if (spec !== undefined && spec.name !== "Native") { + if (spec.anchor !== undefined && this.env.anchorOf(object) !== spec.anchor) { + throw new SnapshotError(`Codec gap: a second ${spec.name} is reachable from the state.`) + } + return { t: spec.name, ...this.fields(object, spec) } + } + if (object instanceof Native) { + // A host function made while the program ran, inside a value an extension returned: its name says what was lost. + this.lose(`the function ${functionName(object)}`) + return { t: "LostNative", ...this.fields(object, hostFunction, hostFunction.fields) } + } + const proto = Object.getPrototypeOf(object) + if (proto === Object.prototype || proto === null) { + const entries: Array = [] + for (const key of Object.keys(object)) entries.push(key, this.value((object as { [key: string]: unknown })[key])) + return proto === null ? { t: "Object", e: entries, n: 1 } : { t: "Object", e: entries } + } + if (object instanceof Error) return this.error(object) + if (object instanceof Interruption) return { t: "Interruption" } + if (object instanceof URL) { + this.owners.set(object.searchParams, object) + return { t: "URL", h: object.href } + } + if (object instanceof URLSearchParams) { + this.params.push([id, object]) + return undefined + } + if (object instanceof Headers) return { t: "Headers", e: [...object].map(([key, item]) => [key, item]) } + if (object instanceof Uint8Array) { + return { t: "Uint8Array", b: this.ref(object.buffer), o: object.byteOffset, l: object.length } + } + if (object instanceof ArrayBuffer) return { t: "ArrayBuffer", d: base64(new Uint8Array(object)) } + if (object instanceof TextDecoder) return { t: "TextDecoder", f: object.fatal, i: object.ignoreBOM } + const what = typeof object === "function" ? "a host function" : `a host ${object.constructor?.name ?? "object"}` + this.lose(what) + return { t: "Lost", f: [what] } + } + + private lose(what: string): void { + if (this.env.strict) throw new SnapshotError(`Codec gap: ${what} has no data form.`) + this.env.lost.push(what) + } + + private spec(object: object): ClassSpec { + const spec = this.specs.byCtor.get(object.constructor) + if (spec === undefined) throw new SnapshotError(`Codec gap: ${object.constructor.name} has no codec.`) + return spec + } + + // A class instance's saved fields, positional, with its `Obj` header when it is one. Fields the spec does not know + // are a codec gap: they would be silently dropped. + private fields(object: object, spec: ClassSpec, only?: ReadonlyArray): { [field: string]: Json } { + const record = object as { [field: string]: unknown } + const fields = only ?? spec.fields + if (only === undefined && spec.view === undefined) { + for (const key of Object.keys(object)) { + if (fields.includes(key) || spec.skip?.includes(key) || (spec.constants !== undefined && key in spec.constants)) + continue + if (spec.obj && (objFields.includes(key) || key === "tag")) continue + throw new SnapshotError(`Codec gap: ${spec.name}.${key} is not saved.`) + } + } + const values = + only === undefined && spec.view !== undefined + ? spec.view(object as never).map((item) => this.value(item)) + : fields.map((field) => (Object.hasOwn(object, field) ? this.value(record[field]) : ["-"])) + if (!(object instanceof Obj)) return { f: values } + const props: Array = [] + for (const [key, slot] of object.props) { + props.push(typeof key === "symbol" ? this.value(key) : key) + if ("value" in slot) { + props.push( + this.value(slot.value), + (slot.writable ? 1 : 0) | (slot.enumerable ? 2 : 0) | (slot.configurable ? 4 : 0), + ) + } else props.push(null, 8 | (slot.enumerable ? 2 : 0) | (slot.configurable ? 4 : 0)) + } + return { f: values, p: object.proto === null ? null : this.value(object.proto), x: object.extensible, o: props } + } + + private error(error: Error): Record { + const name = errorNames.get(error.constructor) + if (name === undefined) { + // A failure only ever reaches the program when it is thrown, so it is saved as what throwing a lost value does. + this.lose(`a host ${error.constructor.name}`) + return { + t: "PendingThrow", + ...this.fields( + lostError(`An error of class ${error.constructor.name}`), + this.specs.byName.get("PendingThrow")!, + ), + } + } + const fields: Array = [] + for (const key of Object.keys(error)) { + if (key === "message" || key === "stack") continue + fields.push(key, this.value((error as unknown as { [key: string]: unknown })[key])) + } + return { t: "Error", c: name, m: error.message, e: fields } + } +} + +// --- Decoding --- + +/** Rebuilds the graph `encode` saved, filling anchors in place: the decoded roots. */ +export const decode = ( + saved: { readonly roots: ReadonlyArray; readonly records: ReadonlyArray }, + env: DecodeEnv, +): Array => { + const decoder = new Decoder(saved.records, env) + for (let id = 0; id < saved.records.length; id++) decoder.shell(id) + for (let id = 0; id < saved.records.length; id++) decoder.fill(id) + decoder.finish() + return saved.roots.map((root) => decoder.value(root)) +} + +const invalid = (message: string): never => { + throw new SnapshotError(`Invalid snapshot: ${message}`) +} + +const isRecord = (value: unknown): value is { readonly t: string; readonly [field: string]: unknown } => + typeof value === "object" && + value !== null && + !Array.isArray(value) && + typeof (value as { t?: unknown }).t === "string" + +const list = (value: unknown, what: string): ReadonlyArray => + Array.isArray(value) ? value : invalid(`${what} is not a list.`) + +const text = (value: unknown, what: string): string => + typeof value === "string" ? value : invalid(`${what} is not text.`) + +const count = (value: unknown, what: string): number => + Number.isSafeInteger(value) && (value as number) >= 0 ? (value as number) : invalid(`${what} is not a count.`) + +const forbidden = new Set(["__proto__", "constructor", "prototype"]) + +class Decoder { + private readonly shells: Array + private readonly filled: Array + // Derived state is rebuilt once every record is filled in, since it reads other records. + private readonly resets: Array<() => void> = [] + private readonly specs: { readonly byCtor: Map; readonly byName: Map } + + constructor( + private readonly records: ReadonlyArray, + private readonly env: DecodeEnv, + ) { + this.specs = registry(env.specs) + this.shells = new Array(records.length) + this.filled = new Array(records.length).fill(false) + } + + private at(id: number): { readonly t: string; readonly [field: string]: unknown } { + const record = this.records[id] + return isRecord(record) ? record : invalid(`record ${id} is malformed.`) + } + + // Allocates the object a record restores, before anything is filled in, so references and cycles resolve. + shell(id: number): object { + const known = this.shells[id] + if (known !== undefined) return known + const record = this.at(id) + const made = this.make(record, id) + this.shells[id] = made + return made + } + + private make(record: { readonly t: string; readonly [field: string]: unknown }, id: number): object { + switch (record.t) { + case "Array": + return [] + case "Object": + return record.n === 1 ? Object.create(null) : {} + case "Map": + return record.a === undefined ? new Map() : this.anchor(text(record.a, "an anchor"), Map) + case "Set": + return new Set() + case "@": { + const name = text(record.n, "a built-in name") + const named = this.env.named(name) + if (named === undefined) return invalid(`this runtime has no built-in or host global named ${name}.`) + const spec = this.classSpec(record.c) + if (named.constructor !== spec.ctor) return invalid(`${name} is not a ${spec.name} in this runtime.`) + return named + } + case "LostNative": + return Object.create(Native.prototype) + case "Interruption": + return this.env.interruption() + case "Error": { + const name = text(record.c, "an error class") + const ctor = errorClasses.get(name) ?? invalid(`unknown error class ${name}.`) + const message = text(record.m, "an error message") + if (ctor === ToolError) return toolError(message) + const made = Object.create(ctor.prototype) as Error + Object.defineProperty(made, "message", { + value: message, + writable: true, + configurable: true, + enumerable: false, + }) + return made + } + case "URL": + return new URL(text(record.h, "a URL")) + case "URLSearchParams": { + if (record.o === undefined) return new URLSearchParams(text(record.s, "search params")) + const owner = this.value(record.o) + return owner instanceof URL ? owner.searchParams : invalid(`record ${id} names no URL.`) + } + case "Headers": { + const entries = list(record.e, "headers").map((entry) => { + const pair = list(entry, "a header") + return [text(pair[0], "a header name"), text(pair[1], "a header value")] as [string, string] + }) + return new Headers(entries) + } + case "ArrayBuffer": + return fromBase64(text(record.d, "bytes")).buffer + case "Uint8Array": { + const buffer = this.value(record.b) + if (!(buffer instanceof ArrayBuffer)) return invalid(`record ${id} names no buffer.`) + const offset = count(record.o, "a byte offset") + const length = count(record.l, "a byte length") + if (offset + length > buffer.byteLength) return invalid(`record ${id} reaches past its buffer.`) + return new Uint8Array(buffer, offset, length) + } + case "TextDecoder": + return new TextDecoder("utf-8", { fatal: record.f === true, ignoreBOM: record.i === true }) + } + const spec = this.classSpec(record.t) + if (spec.name === "Native") return invalid("a native is saved only by name.") + if (spec.anchor !== undefined) return this.anchor(spec.anchor, spec.ctor) + return Object.create(spec.ctor.prototype) + } + + private anchor(name: string, ctor: Function): object { + const anchor = this.env.anchor(name) + if (anchor === undefined || anchor.constructor !== ctor) return invalid(`unknown anchor ${name}.`) + return anchor + } + + private classSpec(name: unknown): ClassSpec { + return this.specs.byName.get(text(name, "a class")) ?? invalid(`unknown record kind ${String(name)}.`) + } + + fill(id: number): void { + if (this.filled[id]) return + this.filled[id] = true + const record = this.at(id) + const target = this.shell(id) as { [field: string]: unknown } + switch (record.t) { + case "Array": { + const array = target as unknown as Array + if (record.v === undefined) { + const length = count(record.l, "an array length") + if (length > 4_294_967_295) invalid("an array is too long.") + array.length = length + const entries = list(record.s, "a sparse array") + for (let at = 0; at < entries.length; at += 2) { + const index = count(entries[at], "an array index") + if (index >= length) invalid("an array index is past its length.") + array[index] = this.value(entries[at + 1]) + } + return + } + const items = list(record.v, "an array") + items.forEach((item, at) => { + if (Array.isArray(item) && item[0] === "h") return + array[at] = this.value(item) + }) + array.length = items.length + return + } + case "Object": + case "Error": { + const entries = list(record.e, record.t === "Error" ? "an error" : "an object") + for (let at = 0; at < entries.length; at += 2) { + const key = text(entries[at], "a key") + if (record.t === "Error" && forbidden.has(key)) invalid(`an error field is named ${key}.`) + Object.defineProperty(target, key, { + value: this.value(entries[at + 1]), + writable: true, + enumerable: true, + configurable: true, + }) + } + return + } + case "Map": { + const map = target as unknown as Map + if (record.a !== undefined) map.clear() + const entries = list(record.e, "a map") + for (let at = 0; at < entries.length; at += 2) map.set(this.value(entries[at]), this.value(entries[at + 1])) + return + } + case "Set": { + const set = target as unknown as Set + for (const item of list(record.e, "a set")) set.add(this.value(item)) + return + } + case "@": + this.fillClass(target, record, this.classSpec(record.c), true) + // Changed before it was saved, so it stays changed: a later save records it again. + return changed(target as unknown as Obj) + case "LostNative": { + this.fillClass(target, record, hostFunction, false) + const native = target as unknown as { tag: string; call: unknown; construct: unknown; callback: boolean } + const name = functionName(target as unknown as Obj) + native.tag = "Function" + native.call = (_: unknown, __: unknown, site: AstNode | undefined) => { + throw lostError(`The function ${name}`, site) + } + native.construct = undefined + native.callback = true + return + } + case "Interruption": + case "URL": + case "URLSearchParams": + case "Headers": + case "ArrayBuffer": + case "Uint8Array": + case "TextDecoder": + return + } + const spec = this.classSpec(record.t) + this.fillClass(target, record, spec, false) + } + + private fillClass( + target: { [field: string]: unknown }, + record: { readonly [field: string]: unknown }, + spec: ClassSpec, + named: boolean, + only?: ReadonlyArray, + ): void { + const fields = only ?? spec.fields + const values = list(record.f, `the fields of a ${spec.name}`) + if (values.length !== fields.length) invalid(`a ${spec.name} has ${values.length} fields, not ${fields.length}.`) + if (spec.obj || named) this.fillObj(target as unknown as Obj, record) + if (spec.constants !== undefined) Object.assign(target, spec.constants) + fields.forEach((field, at) => { + const value = values[at] + if (Array.isArray(value) && value[0] === "-") { + delete target[field] + return + } + target[field] = this.value(value) + }) + const reset = spec.reset + if (reset !== undefined) this.resets.push(() => reset(target as never)) + } + + finish(): void { + for (const reset of this.resets) reset() + } + + // An object's prototype, extensibility, and properties. A named object's accessors stay the ones it was made with. + private fillObj(target: Obj, record: { readonly [field: string]: unknown }): void { + const proto = record.p === null ? null : this.value(record.p) + if (proto !== null && !(proto instanceof Obj)) invalid("a prototype is not an object.") + if (typeof record.x !== "boolean") invalid("an object's extensibility is not a boolean.") + const entries = list(record.o, "an object's properties") + const props = new Map() + for (let at = 0; at < entries.length; at += 3) { + const raw = entries[at] + const key = typeof raw === "string" ? raw : this.value(raw) + if (typeof key !== "string" && typeof key !== "symbol") invalid("a property key is not a string or symbol.") + const flags = count(entries[at + 2], "property flags") + const enumerable = (flags & 2) !== 0 + const configurable = (flags & 4) !== 0 + if ((flags & 8) !== 0) { + const original = target.props?.get(key as string | symbol) + if (original === undefined || "value" in original) invalid(`the accessor ${String(key)} has no original.`) + const accessor = original as Extract + props.set(key as string | symbol, { get: accessor.get, set: accessor.set, enumerable, configurable }) + continue + } + props.set(key as string | symbol, { + value: this.value(entries[at + 1]) as Value, + writable: (flags & 1) !== 0, + enumerable, + configurable, + }) + } + const writable = target as unknown as { + props: Map + proto: Obj | null + extensible: boolean + intrinsic: number + } + writable.intrinsic ??= 0 + writable.props = props + writable.extensible = record.x as boolean + writable.proto = proto as Obj | null + } + + value(value: unknown): unknown { + switch (typeof value) { + case "string": + case "boolean": + return value + case "number": + return Number.isFinite(value) ? value : invalid("a number is not finite.") + } + if (value === null) return null + if (!Array.isArray(value)) return invalid("a value is neither JSON data nor a tag.") + switch (value[0]) { + case "u": + return undefined + case "n": + switch (value[1]) { + case "NaN": + return Number.NaN + case "Infinity": + return Number.POSITIVE_INFINITY + case "-Infinity": + return Number.NEGATIVE_INFINITY + case "-0": + return -0 + } + return invalid(`unknown number ${String(value[1])}.`) + case "s": + return symbols.get(text(value[1], "a symbol")) ?? invalid(`unknown symbol ${String(value[1])}.`) + case "x": + return this.env.external?.(value[1] as Json) ?? invalid(`unknown external value ${JSON.stringify(value[1])}.`) + case "i": { + const name = text(value[1], "a built-in name") + return this.env.named(name) ?? invalid(`this runtime has no built-in or host global named ${name}.`) + } + case "a": + return ( + this.env.node(text(value[1], "a source"), count(value[2], "a node index")) ?? + invalid(`no node ${String(value[2])} in source ${String(value[1])}.`) + ) + case "r": { + const id = count(value[1], "a record id") + if (id >= this.records.length) invalid(`record ${id} does not exist.`) + return this.shell(id) + } + } + return invalid(`unknown tag ${String(value[0])}.`) + } +} + +const functionName = (fn: Obj): string => { + const slot = fn.props.get("name") + return slot !== undefined && "value" in slot && typeof slot.value === "string" && slot.value !== "" + ? slot.value + : "(anonymous)" +} + +const base64 = (bytes: Uint8Array): string => { + let binary = "" + for (let at = 0; at < bytes.length; at += 0x8000) binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000)) + return btoa(binary) +} + +const fromBase64 = (encoded: string): Uint8Array => { + const binary = atob(encoded) + const bytes = new Uint8Array(binary.length) + for (let at = 0; at < binary.length; at++) bytes[at] = binary.charCodeAt(at) + return bytes +} diff --git a/packages/codemode/src/vm/snapshot/snapshot.ts b/packages/codemode/src/vm/snapshot/snapshot.ts new file mode 100644 index 00000000000..16d1339a97d --- /dev/null +++ b/packages/codemode/src/vm/snapshot/snapshot.ts @@ -0,0 +1,112 @@ +import type { Interpreter } from "../runtime/interpreter.js" +import type { ToolCall } from "../../tool-runtime.js" +import type { Encoded, Record } from "./codec.js" +import { SnapshotError } from "./codec.js" + +export { SnapshotError } + +/** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */ +export const FORMAT = 2 + +/** + * A run's whole state as JSON: a header that says which build and sources it belongs to, and the graph of records (see codec.ts) rooted at the VM, the interpreter, and the global scope, then + * every built-in or host global the run changed. + */ +export type Snapshot = { + readonly format: typeof FORMAT + readonly header: Header + readonly roots: ReadonlyArray + readonly records: ReadonlyArray +} + +export type Header = { + /** The build that wrote it: the package version, and a hash of the opcodes, the prelude's bytecode, codecs, and built-ins. */ + readonly build: string + /** A hash of the host's globals (extensions, a test harness), which the restoring runtime must have too. */ + readonly hosts: string + /** The program texts, re-parsed and recompiled on restore, and checked against their hashes and bytecode hashes. */ + readonly sources: ReadonlyArray<{ + readonly id: string + readonly code: string + readonly hash: string + readonly bytecode: string + }> + /** The tool calls admitted so far. */ + readonly toolCalls: ReadonlyArray +} + +export const hostsId = (ctx: Interpreter): string => hash(ctx.intrinsics.hosts().join("\n")) + +/** A snapshot as bytes: UTF-8 JSON. */ +export const toBytes = (snapshot: Snapshot): Uint8Array => new TextEncoder().encode(JSON.stringify(snapshot)) + +/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */ +export const fromBytes = (bytes: Uint8Array): unknown => { + try { + return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)) + } catch (error) { + throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`) + } +} + +// --- Validation --- + +const fail = (message: string): never => { + throw new SnapshotError(`Invalid snapshot: ${message}`) +} + +const object = (value: unknown, what: string): { readonly [key: string]: unknown } => + typeof value === "object" && value !== null && !Array.isArray(value) + ? (value as { readonly [key: string]: unknown }) + : fail(`${what} is not an object.`) + +const array = (value: unknown, what: string): ReadonlyArray => + Array.isArray(value) ? value : fail(`${what} is not a list.`) + +const string = (value: unknown, what: string): string => + typeof value === "string" ? value : fail(`${what} is not text.`) + +// The header is checked in full; records are checked as they are decoded. +export const validate = (saved: unknown): Snapshot => { + const root = object(saved, "the snapshot") + if (root.format !== FORMAT) fail(`format ${String(root.format)} is not ${FORMAT}.`) + const header = object(root.header, "the header") + const checked: Header = { + build: string(header.build, "the build"), + hosts: string(header.hosts, "the host globals"), + sources: array(header.sources, "the sources").map((item) => { + const source = object(item, "a source") + return { + id: string(source.id, "a source id"), + code: string(source.code, "a source"), + hash: string(source.hash, "a source hash"), + bytecode: string(source.bytecode, "a bytecode hash"), + } + }), + toolCalls: array(header.toolCalls, "the tool calls").map((item) => ({ + name: string(object(item, "a tool call").name, "a tool name"), + })), + } + const roots = array(root.roots, "the roots") + if (roots.length < 3) fail("there are fewer than three roots.") + return { + format: FORMAT, + header: checked, + roots: roots as ReadonlyArray, + records: array(root.records, "the records") as ReadonlyArray, + } +} + +// --- Helpers --- + +// FNV-1a over UTF-16 code units, twice with different offsets: a fingerprint, not a security check. +export const hash = (text: string): string => { + let a = 0x811c9dc5 + let b = 0x01000193 ^ 0x9e3779b9 + for (let at = 0; at < text.length; at++) { + const code = text.charCodeAt(at) + a = Math.imul(a ^ code, 0x01000193) + b = Math.imul(b ^ code, 0x01000193) ^ (b >>> 13) + } + return (a >>> 0).toString(16).padStart(8, "0") + (b >>> 0).toString(16).padStart(8, "0") +} diff --git a/packages/codemode/src/vm/stdlib/array.ts b/packages/codemode/src/vm/stdlib/array.ts new file mode 100644 index 00000000000..1799066113f --- /dev/null +++ b/packages/codemode/src/vm/stdlib/array.ts @@ -0,0 +1,358 @@ +import { Invoke } from "../runtime/signals.js" +import type { Builtins } from "../runtime/intrinsics.js" +import { builtinCursor } from "../runtime/operations.js" +import { constructor, methods, prototypeFrom, receiver, iteratorMethods } from "../runtime/native.js" +import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../runtime/limits.js" +import { invalidData, IteratorSymbol, rangeError, typeError } from "../runtime/model.js" +import { + changed, + define, + get, + hidden, + Arr, + Bytes, + GeneratorObj, + Obj, + PromiseObj, + coerceToInteger, + coerceToNumber, + coerceToString, + rejectAddition, + type Value, +} from "../runtime/objects.js" +import { describeValue, rejectCircularInsertion } from "../runtime/references.js" +import { requireCallback, withPrimitives } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { compareText } from "../../tool-runtime.js" + +const arrayLikeSource = (source: Value): { readonly length: number; readonly source: Obj } => { + // JS would treat a promise as an empty array-like; that would hide a missing `await`. + if (source instanceof Obj && !(source instanceof PromiseObj)) { + const length = Math.max(0, coerceToInteger(get(source, "length"))) + checkArrayLength(length) + return { length, source } + } + throw invalidData(`Array.from expects an iterable or array-like value, received ${describeValue(source)}.`) +} + +const iterable = (builtins: Builtins, value: Value) => { + if (builtinCursor(builtins, value) !== undefined) return true + if (!(value instanceof Obj)) return false + const method = get(value, IteratorSymbol) + return method !== undefined && method !== null +} + +const arrayFrom = (ctx: Interpreter, args: Array): Value | Invoke => { + const source = args[0] + const mapper = args.length < 2 || args[1] === undefined ? undefined : requireCallback(args[1], "Array.from") + if (iterable(ctx.builtins, source)) { + return new Invoke(ctx.prelude.fn("arrayFromIterable"), undefined, [source, mapper, args[2]], undefined) + } + if (source instanceof GeneratorObj) { + throw typeError(`Array.from expects a synchronous iterable or array-like value, received ${describeValue(source)}.`) + } + const arrayLike = arrayLikeSource(source) + if (mapper === undefined) { + return new Arr( + ctx.builtins.Array, + Array.from({ length: arrayLike.length }, (_, index) => get(arrayLike.source, index)), + ) + } + return new Invoke( + ctx.prelude.fn("arrayFromLike"), + undefined, + [arrayLike.source, arrayLike.length, mapper, args[2]], + undefined, + ) +} + +/** Where a sort puts its result: back into an array or bytes, or into a new array. */ +export type SortInto = "Array.sort" | "Array.toSorted" | "Uint8Array.sort" + +/** The most values the default sort sorts in one native call; more sort in bounded steps (see `sortDefault`). */ +export const SORT_RUN = 4096 + +/** + * Sorts `target`'s values: at once without a comparator, unless there are many, otherwise in the prelude + * (`sortWith`), which calls it. The merges of a top-down merge sort are planned here, in post-order, so the prelude + * compares in the same order as the recursive sort. Holes and undefined values sort last without being compared. + */ +export const sortArray = ( + ctx: Interpreter, + target: Arr | Bytes, + comparator: Value, + into: SortInto, +): Value | Invoke => { + const builtins = ctx.builtins + const items = target instanceof Arr ? [...target.items] : [...target.bytes] + // Counted before any comparator runs; a comparator that changes the array does not change where holes go. + let holes = 0 + if (target instanceof Arr) for (let index = 0; index < items.length; index++) if (!(index in target.items)) holes++ + const callback = comparator === undefined ? undefined : requireCallback(comparator, into) + if (callback === undefined && items.length <= SORT_RUN) { + return sorted( + builtins, + target, + into, + holes, + items.sort((a, b) => compareText(coerceToString(a), coerceToString(b))), + ) + } + const defined = new Arr( + builtins.Array, + items.filter((item) => item !== undefined), + ) + const undefs = items.length - defined.items.length + if (callback === undefined) { + return new Invoke(ctx.prelude.fn("sortDefault"), undefined, [target, into, holes, defined, undefs], undefined) + } + const merges: Array = [] + const plan = (low: number, high: number) => { + if (high - low <= 1) return + const middle = low + Math.floor((high - low) / 2) + plan(low, middle) + plan(middle, high) + merges.push(low, middle, high) + } + plan(0, defined.items.length) + const args = [target, into, holes, callback, defined, undefs, new Arr(builtins.Array, merges)] + return new Invoke(ctx.prelude.fn("sortWith"), undefined, args, undefined) +} + +export const sorted = ( + builtins: Builtins, + target: Arr | Bytes, + into: SortInto, + holes: number, + result: Array, +) => { + if (target instanceof Bytes) { + target.bytes.set(Uint8Array.from(result, coerceToNumber)) + return target + } + if (into === "Array.toSorted") return new Arr(builtins.Array, result) + changed(target) + const items = target.items + const count = result.length - holes + result.slice(0, count).forEach((item, index) => { + items[index] = item + }) + Array.from({ length: holes }, (_, index) => count + index).forEach((index) => { + delete items[index] + }) + return target +} + +// Array constructs identically with or without new, like JS. +export const arrayGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Array + const wrap = (items: Array) => new Arr(proto, items) + const construct = (args: Array, into: Obj): Arr => { + if (args.length !== 1) return new Arr(into, [...args]) + const first = args[0] + if (typeof first !== "number") return new Arr(into, [first]) + if (!Number.isInteger(first) || first < 0 || first > MAX_ARRAY_LENGTH) throw rangeError("Invalid array length.") + // Sparse like JS: Array(3) has holes, and combinator loops already skip them. + return new Arr(into, new Array(first)) + } + const array = constructor(builtins, proto, { + name: "Array", + length: 1, + call: (_, args) => construct(args, proto), + construct: (args, newTarget) => construct(args, prototypeFrom(newTarget, proto)), + }) + methods(builtins, array, [ + ["isArray", 1, (_, args) => args[0] instanceof Arr], + ["of", 0, (_, args) => wrap([...args])], + ["from", 1, (_, args) => arrayFrom(ctx, args)], + ]) + + const self = (thisValue: Value, name: string) => receiver(Arr, thisValue, `Array.prototype.${name}`) + // A mutating method fails where its first element write, delete, or `length` write would on a frozen, sealed, or + // non-extensible array. `growth` is given by the methods that always write `length`, even when it is 0; holes a + // method would fill on a non-extensible array are not checked. + const mutable = (target: Arr, growth?: number): Arr => { + const length = target.items.length + if ((growth ?? 0) > 0) rejectAddition(target, length) + if ((growth ?? 0) < 0 && length > 0 && !target.elements.configurable) { + throw typeError(`Cannot delete property '${length - 1}'.`) + } + if (!target.elements.writable && (length > 0 || growth !== undefined)) { + throw typeError(`Cannot assign to read only property '${length > 0 ? 0 : "length"}'.`) + } + changed(target) + return target + } + const optNumber = (value: Value): number | undefined => (value === undefined ? undefined : coerceToInteger(value)) + + methods(builtins, proto, [ + [ + "join", + 1, + (thisValue, args) => { + // .map would keep holes, which the conversion would then hand to the body as undefined. + const parts = Array.from(self(thisValue, "join").items, (item) => item ?? "") + return withPrimitives("string", [args[0] === undefined ? "," : args[0], ...parts], ([separator, ...items]) => { + const joined = items.map(coerceToString).join(coerceToString(separator)) + checkStringLength(joined.length) + return joined + }) + }, + ], + [ + "toString", + 0, + (thisValue) => + withPrimitives( + "string", + Array.from(self(thisValue, "toString").items, (item) => item ?? ""), + (items) => items.map(coerceToString).join(","), + ), + ], + [ + "includes", + 1, + (thisValue, args) => { + return self(thisValue, "includes").items.includes(args[0], optNumber(args[1])) + }, + ], + ["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").items.indexOf(args[0], optNumber(args[1]))], + [ + "lastIndexOf", + 1, + (thisValue, args) => { + const target = self(thisValue, "lastIndexOf").items + // An explicit undefined is a fromIndex of 0, unlike omitting it. + return args.length < 2 ? target.lastIndexOf(args[0]) : target.lastIndexOf(args[0], optNumber(args[1])) + }, + ], + ["at", 1, (thisValue, args) => self(thisValue, "at").items.at(optNumber(args[0]) ?? 0)], + [ + "slice", + 2, + (thisValue, args) => wrap(self(thisValue, "slice").items.slice(optNumber(args[0]), optNumber(args[1]))), + ], + [ + "concat", + 1, + (thisValue, args) => { + const joined = self(thisValue, "concat").items.concat( + ...args.map((item) => (item instanceof Arr ? item.items : item)), + ) + checkArrayLength(joined.length) + return wrap(joined) + }, + ], + [ + "flat", + 0, + (thisValue, args) => { + const flatten = (items: Array, depth: number): Array => + items.flatMap((item) => (item instanceof Arr && depth > 0 ? flatten(item.items, depth - 1) : [item])) + const flattened = flatten(self(thisValue, "flat").items, optNumber(args[0]) ?? 1) + checkArrayLength(flattened.length) + return wrap(flattened) + }, + ], + [ + "reverse", + 0, + (thisValue) => { + const target = self(thisValue, "reverse") + // Fewer than two elements means no writes at all, so a frozen one-element array reverses fine. + if (target.items.length > 1) mutable(target) + target.items.reverse() + return target + }, + ], + ["sort", 1, (thisValue, args) => sortArray(ctx, mutable(self(thisValue, "sort")), args[0], "Array.sort")], + ["toSorted", 1, (thisValue, args) => sortArray(ctx, self(thisValue, "toSorted"), args[0], "Array.toSorted")], + ["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())], + [ + "with", + 2, + (thisValue, args) => { + const target = self(thisValue, "with").items + const index = optNumber(args[0]) ?? 0 + const resolved = index < 0 ? target.length + index : index + if (resolved < 0 || resolved >= target.length) throw rangeError("Array.with index is out of range.") + const copied = [...target] + copied[resolved] = args[1] + return wrap(copied) + }, + ], + [ + "push", + 1, + (thisValue, args) => { + const target = mutable(self(thisValue, "push"), args.length) + // Validate all insertions before mutating to avoid partial cyclic updates. + for (const item of args) rejectCircularInsertion(target, item, "Array.push result") + return target.items.push(...args) + }, + ], + [ + "unshift", + 1, + (thisValue, args) => { + const target = mutable(self(thisValue, "unshift"), args.length) + for (const item of args) rejectCircularInsertion(target, item, "Array.unshift result") + return target.items.unshift(...args) + }, + ], + ["pop", 0, (thisValue) => mutable(self(thisValue, "pop"), -1).items.pop()], + ["shift", 0, (thisValue) => mutable(self(thisValue, "shift"), -1).items.shift()], + [ + "splice", + 2, + (thisValue, args) => { + const target = self(thisValue, "splice") + const length = target.items.length + const start = optNumber(args[0]) ?? 0 + const from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length) + const deleteCount = + args.length === 1 ? length - from : Math.min(Math.max(optNumber(args[1]) ?? 0, 0), length - from) + const inserted = args.slice(2) + for (const item of inserted) rejectCircularInsertion(target, item, "Array.splice result") + mutable(target, inserted.length - deleteCount) + return wrap(target.items.splice(from, deleteCount, ...inserted)) + }, + ], + [ + "toSpliced", + 2, + (thisValue, args) => { + const copied = [...self(thisValue, "toSpliced").items] + if (args.length === 0) return wrap(copied) + const start = optNumber(args[0]) ?? 0 + if (args.length === 1) copied.splice(start) + else copied.splice(start, optNumber(args[1]) ?? 0, ...args.slice(2)) + return wrap(copied) + }, + ], + [ + "fill", + 1, + (thisValue, args) => { + const target = mutable(self(thisValue, "fill")) + rejectCircularInsertion(target, args[0], "Array.fill result") + target.items.fill(args[0], optNumber(args[1]), optNumber(args[2])) + return target + }, + ], + [ + "copyWithin", + 2, + (thisValue, args) => { + const target = mutable(self(thisValue, "copyWithin")) + target.items.copyWithin(optNumber(args[0]) ?? 0, optNumber(args[1]) ?? 0, optNumber(args[2])) + return target + }, + ], + ...iteratorMethods(builtins, self), + // The callback methods are the prelude's. + ]) + define(proto, IteratorSymbol, get(proto, "values"), hidden) + return array +} diff --git a/packages/codemode/src/vm/stdlib/bytes.ts b/packages/codemode/src/vm/stdlib/bytes.ts new file mode 100644 index 00000000000..815074471c4 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/bytes.ts @@ -0,0 +1,251 @@ +import { Invoke } from "../runtime/signals.js" +import { checkArrayLength, checkStringLength } from "../runtime/limits.js" +import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js" +import { IteratorSymbol, rangeError, syntaxError, typeError } from "../runtime/model.js" +import { + define, + defineAccessor, + get, + hidden, + Bytes, + Obj, + coerceToInteger, + coerceToNumber, + coerceToString, + type Value, +} from "../runtime/objects.js" +import { describeValue } from "../runtime/references.js" +import { requireCallback } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { sortArray } from "./array.js" + +/** + * The bytes a Uint8Array, array, or other iterable of numbers describes, read in the prelude: a new Uint8Array with + * prototype `into`, or set into the Uint8Array `into` at `offset`. The host array clamps each value. + */ +const collectBytes = ( + ctx: Interpreter, + source: Value, + name: string, + into: Obj, + offset: number, + mapper?: Value, + thisArg?: Value, +) => { + const callback = mapper === undefined ? undefined : requireCallback(mapper, name) + if (source instanceof Bytes && callback === undefined) return finishBytes(new Uint8Array(source.bytes), into, offset) + return new Invoke( + ctx.prelude.fn("collectBytes"), + undefined, + [source, name, callback, thisArg, into, offset], + undefined, + ) +} + +export const finishBytes = (bytes: Uint8Array, into: Obj, offset: number): Value => { + if (!(into instanceof Bytes)) return new Bytes(into, bytes) + if (!Number.isInteger(offset) || offset < 0 || bytes.length + offset > into.bytes.length) { + throw rangeError("Uint8Array.set: the source does not fit at that offset.") + } + into.bytes.set(bytes, offset) + return undefined +} + +const constructBytes = (ctx: Interpreter, args: Array, proto: Obj) => { + const source = args[0] + if (source !== null && typeof source === "object") return collectBytes(ctx, source, "new Uint8Array(...)", proto, 0) + const length = source === undefined ? 0 : coerceToNumber(source) + if (!Number.isInteger(length) || length < 0) throw rangeError(`Invalid typed array length: ${coerceToString(source)}`) + checkArrayLength(length) + return new Bytes(proto, new Uint8Array(length)) +} + +export const uint8ArrayGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Uint8Array + const wrap = (bytes: Uint8Array) => new Bytes(proto, bytes) + const uint8Array = constructor(builtins, proto, { + name: "Uint8Array", + length: 3, + call: requiresNew("Uint8Array"), + construct: (args, newTarget) => constructBytes(ctx, args, prototypeFrom(newTarget, proto)), + }) + const decode = (name: string, args: Array, from: (text: string) => Uint8Array) => { + if (typeof args[0] !== "string") throw typeError(`Uint8Array.${name} expects a string.`) + try { + return wrap(from(args[0])) + } catch { + throw syntaxError(`Uint8Array.${name}: the string is not valid ${name === "fromHex" ? "hex" : "base64"}.`) + } + } + methods(builtins, uint8Array, [ + ["from", 1, (_, args) => collectBytes(ctx, args[0], "Uint8Array.from", proto, 0, args[1], args[2])], + ["of", 0, (_, args) => wrap(Uint8Array.from(args, coerceToNumber))], + ["fromBase64", 1, (_, args) => decode("fromBase64", args, (text) => Uint8Array.fromBase64(text))], + ["fromHex", 1, (_, args) => decode("fromHex", args, (text) => Uint8Array.fromHex(text))], + ]) + + const self = (thisValue: Value, name: string) => receiver(Bytes, thisValue, `Uint8Array.prototype.${name}`) + const optNumber = (value: Value): number | undefined => (value === undefined ? undefined : coerceToInteger(value)) + // Uint8Array cannot contain NaN, so non-number search values never match a byte. + const search = (value: Value) => (typeof value === "number" ? value : NaN) + defineAccessor(proto, "length", (thisValue) => self(thisValue, "length").bytes.length) + methods(builtins, proto, [ + ["at", 1, (thisValue, args) => self(thisValue, "at").bytes.at(optNumber(args[0]) ?? 0)], + [ + "slice", + 2, + (thisValue, args) => wrap(self(thisValue, "slice").bytes.slice(optNumber(args[0]), optNumber(args[1]))), + ], + // A view on the same bytes, as in JS: writes through one are visible through the other. + [ + "subarray", + 2, + (thisValue, args) => wrap(self(thisValue, "subarray").bytes.subarray(optNumber(args[0]), optNumber(args[1]))), + ], + [ + "set", + 1, + (thisValue, args) => { + const target = self(thisValue, "set") + return collectBytes(ctx, args[0], "Uint8Array.set", target, optNumber(args[1]) ?? 0) + }, + ], + [ + "fill", + 1, + (thisValue, args) => { + const target = self(thisValue, "fill") + target.bytes.fill(coerceToNumber(args[0]), optNumber(args[1]), optNumber(args[2])) + return target + }, + ], + [ + "reverse", + 0, + (thisValue) => { + const target = self(thisValue, "reverse") + target.bytes.reverse() + return target + }, + ], + [ + "sort", + 1, + (thisValue, args) => { + const target = self(thisValue, "sort") + // Without a comparator, typed arrays sort numerically rather than by string. + if (args[0] === undefined) { + target.bytes.sort() + return target + } + return sortArray(ctx, target, args[0], "Uint8Array.sort") + }, + ], + ["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").bytes.indexOf(search(args[0]), optNumber(args[1]))], + [ + "lastIndexOf", + 1, + (thisValue, args) => { + const target = self(thisValue, "lastIndexOf").bytes + return args.length < 2 + ? target.lastIndexOf(search(args[0])) + : target.lastIndexOf(search(args[0]), optNumber(args[1])) + }, + ], + [ + "includes", + 1, + (thisValue, args) => self(thisValue, "includes").bytes.includes(search(args[0]), optNumber(args[1])), + ], + [ + "join", + 1, + (thisValue, args) => { + const joined = self(thisValue, "join").bytes.join(args[0] === undefined ? "," : coerceToString(args[0])) + checkStringLength(joined.length) + return joined + }, + ], + ["toString", 0, (thisValue) => self(thisValue, "toString").bytes.join(",")], + ["toBase64", 0, (thisValue) => self(thisValue, "toBase64").bytes.toBase64()], + ["toHex", 0, (thisValue) => self(thisValue, "toHex").bytes.toHex()], + ...iteratorMethods(builtins, self), + // The callback methods are the prelude's. + ]) + define(proto, IteratorSymbol, get(proto, "values"), hidden) + return uint8Array +} + +export const textEncoderGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.TextEncoder + const encoder = new TextEncoder() + defineAccessor(proto, "encoding", () => "utf-8") + methods(builtins, proto, [ + [ + "encode", + 0, + (_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] === undefined ? "" : args[0]))), + ], + ]) + return constructor(builtins, proto, { + name: "TextEncoder", + call: requiresNew("TextEncoder"), + construct: (_, newTarget) => new Obj(prototypeFrom(newTarget, proto)), + }) +} + +/** A `TextDecoder` holding the host decoder its label and options configured. */ +export class TextDecoderObj extends Obj { + constructor( + proto: Obj, + readonly decoder: TextDecoder, + ) { + super(proto) + } +} + +// The WHATWG labels for UTF-8, the only encoding CodeMode decodes. +const utf8Labels = new Set(["unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "utf-8", "utf8", "x-unicode20utf8"]) + +export const textDecoderGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.TextDecoder + const self = (thisValue: Value, name: string) => receiver(TextDecoderObj, thisValue, `TextDecoder.prototype.${name}`) + defineAccessor(proto, "encoding", (thisValue) => self(thisValue, "encoding").decoder.encoding) + defineAccessor(proto, "fatal", (thisValue) => self(thisValue, "fatal").decoder.fatal) + defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").decoder.ignoreBOM) + methods(builtins, proto, [ + [ + "decode", + 0, + (thisValue, args) => { + const decoder = self(thisValue, "decode").decoder + if (args[0] === undefined) return "" + if (!(args[0] instanceof Bytes)) { + throw typeError(`TextDecoder.decode expects a Uint8Array, received ${describeValue(args[0])}.`) + } + try { + return decoder.decode(args[0].bytes) + } catch { + throw typeError(`TextDecoder.decode: the input is not valid ${decoder.encoding}.`) + } + }, + ], + ]) + return constructor(builtins, proto, { + name: "TextDecoder", + call: requiresNew("TextDecoder"), + construct: (args, newTarget) => { + const label = args[0] === undefined ? "utf-8" : coerceToString(args[0]).trim().toLowerCase() + if (!utf8Labels.has(label)) throw rangeError(`The "${label}" encoding is not supported; only UTF-8 is.`) + const options = args[1] instanceof Obj ? args[1] : undefined + const flag = (name: string) => options !== undefined && Boolean(get(options, name)) + return new TextDecoderObj( + prototypeFrom(newTarget, proto), + new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: flag("ignoreBOM") }), + ) + }, + }) +} diff --git a/packages/codemode/src/vm/stdlib/collections.ts b/packages/codemode/src/vm/stdlib/collections.ts new file mode 100644 index 00000000000..7458be07529 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/collections.ts @@ -0,0 +1,210 @@ +import { Invoke } from "../runtime/signals.js" +import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js" +import { IteratorSymbol, typeError } from "../runtime/model.js" +import { + define, + defineAccessor, + get, + hidden, + type Callable, + MapObj, + Obj, + SetObj, + type Value, + WeakMapObj, + WeakSetObj, +} from "../runtime/objects.js" +import { describeValue } from "../runtime/references.js" +import { requireCallback } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" + +// A collection from an iterable is filled by the prelude, which iterates it. +const filled = (ctx: Interpreter, target: Obj, init: Value, kind: string) => + init === undefined || init === null + ? target + : new Invoke(ctx.prelude.fn("fillCollection"), undefined, [target, init, kind], undefined) + +// The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself. +const getOrInsertComputed = ( + ctx: Interpreter, + owner: MapObj | WeakMapObj, + key: Value, + callback: Callable, +): Value | Invoke => { + const table = owner.map as Map + if (table.has(key)) return table.get(key) + return new Invoke(ctx.prelude.fn("insertComputed"), undefined, [owner, key === 0 ? 0 : key, callback], undefined) +} + +export const mapGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Map + const map = constructor(builtins, proto, { + name: "Map", + call: requiresNew("Map"), + construct: (args, newTarget) => filled(ctx, new MapObj(prototypeFrom(newTarget, proto)), args[0], "Map"), + }) + const self = (thisValue: Value, name: string) => receiver(MapObj, thisValue, `Map.prototype.${name}`) + defineAccessor(proto, "size", (thisValue) => receiver(MapObj, thisValue, "Map.prototype.size").map.size) + methods(builtins, proto, [ + ["get", 1, (thisValue, args) => self(thisValue, "get").map.get(args[0])], + ["has", 1, (thisValue, args) => self(thisValue, "has").map.has(args[0])], + [ + "set", + 2, + (thisValue, args) => { + const target = self(thisValue, "set") + target.map.set(args[0], args[1]) + return target + }, + ], + [ + "getOrInsert", + 2, + (thisValue, args) => { + const target = self(thisValue, "getOrInsert").map + if (!target.has(args[0])) target.set(args[0], args[1]) + return target.get(args[0]) + }, + ], + [ + "getOrInsertComputed", + 2, + (thisValue, args) => { + const target = self(thisValue, "getOrInsertComputed") + return getOrInsertComputed(ctx, target, args[0], requireCallback(args[1], "Map.getOrInsertComputed")) + }, + ], + ["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])], + [ + "clear", + 0, + (thisValue) => { + self(thisValue, "clear").map.clear() + return undefined + }, + ], + ...iteratorMethods(builtins, self), + ]) + define(proto, IteratorSymbol, get(proto, "entries"), hidden) + return map +} + +export const setGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Set + const set = constructor(builtins, proto, { + name: "Set", + call: requiresNew("Set"), + construct: (args, newTarget) => filled(ctx, new SetObj(prototypeFrom(newTarget, proto)), args[0], "Set"), + }) + const self = (thisValue: Value, name: string) => receiver(SetObj, thisValue, `Set.prototype.${name}`) + defineAccessor(proto, "size", (thisValue) => receiver(SetObj, thisValue, "Set.prototype.size").set.size) + methods(builtins, proto, [ + ["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])], + [ + "add", + 1, + (thisValue, args) => { + const target = self(thisValue, "add") + target.set.add(args[0]) + return target + }, + ], + ["delete", 1, (thisValue, args) => self(thisValue, "delete").set.delete(args[0])], + [ + "clear", + 0, + (thisValue) => { + self(thisValue, "clear").set.clear() + return undefined + }, + ], + ...iteratorMethods(builtins, self), + ]) + define(proto, IteratorSymbol, get(proto, "values"), hidden) + return set +} + +// CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again. +export const weakKey = (value: Value, label: string) => { + if (value instanceof Obj) return value + throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`) +} + +export const weakMapGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.WeakMap + const weakMap = constructor(builtins, proto, { + name: "WeakMap", + call: requiresNew("WeakMap"), + construct: (args, newTarget) => filled(ctx, new WeakMapObj(prototypeFrom(newTarget, proto)), args[0], "WeakMap"), + }) + const self = (thisValue: Value, name: string) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map + // Lookups pass any key through: the host collection answers false for a non-object, as the spec requires. + const key = (value: Value) => weakKey(value, "as weak map key") + methods(builtins, proto, [ + [ + "get", + 1, + (thisValue, args) => { + const target = self(thisValue, "get") + return args[0] instanceof Obj ? target.get(args[0]) : undefined + }, + ], + ["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0] as Obj)], + ["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0] as Obj)], + [ + "set", + 2, + (thisValue, args) => { + self(thisValue, "set").set(key(args[0]), args[1]) + return thisValue + }, + ], + [ + "getOrInsert", + 2, + (thisValue, args) => { + const target = self(thisValue, "getOrInsert") + const k = key(args[0]) + if (!target.has(k)) target.set(k, args[1]) + return target.get(k) + }, + ], + [ + "getOrInsertComputed", + 2, + (thisValue, args) => { + const target = receiver(WeakMapObj, thisValue, "WeakMap.prototype.getOrInsertComputed") + const k = key(args[0]) + return getOrInsertComputed(ctx, target, k, requireCallback(args[1], "WeakMap.getOrInsertComputed")) + }, + ], + ]) + return weakMap +} + +export const weakSetGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.WeakSet + const weakSet = constructor(builtins, proto, { + name: "WeakSet", + call: requiresNew("WeakSet"), + construct: (args, newTarget) => filled(ctx, new WeakSetObj(prototypeFrom(newTarget, proto)), args[0], "WeakSet"), + }) + const self = (thisValue: Value, name: string) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set + methods(builtins, proto, [ + ["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0] as Obj)], + ["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0] as Obj)], + [ + "add", + 1, + (thisValue, args) => { + self(thisValue, "add").add(weakKey(args[0], "in weak set")) + return thisValue + }, + ], + ]) + return weakSet +} diff --git a/packages/codemode/src/vm/stdlib/console.ts b/packages/codemode/src/vm/stdlib/console.ts new file mode 100644 index 00000000000..c4e4f7fd68c --- /dev/null +++ b/packages/codemode/src/vm/stdlib/console.ts @@ -0,0 +1,98 @@ +import { type Method, methods } from "../runtime/native.js" +import { entries, get, Arr, Obj, type Value } from "../runtime/objects.js" +import { ToolReference } from "../../tool-runtime.js" +import { containsOpaqueReference } from "../runtime/references.js" +import type { Interpreter } from "../runtime/interpreter.js" + +const consoleMethods = ["log", "info", "debug", "warn", "error", "dir", "table"] + +/** Captured console: every method appends one formatted line to `logs`. */ +export const consoleGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const console = new Obj(builtins.Object) + methods( + builtins, + console, + consoleMethods.map( + (name): Method => [ + name, + 0, + (_, args) => { + ctx.logs.push(formatConsoleMessage(name, args)) + return undefined + }, + ], + ), + ) + return console +} + +const MAX_CONSOLE_DEPTH = 32 + +const formatConsoleMessage = (name: string, args: Array): string => { + if (name === "dir") return args.length === 0 ? "undefined" : formatValue(args[0]) + if (name === "table") return formatConsoleTable(args[0], args[1]) + const prefix = name === "warn" ? "[warn] " : name === "error" ? "[error] " : name === "debug" ? "[debug] " : "" + return `${prefix}${args.map((arg) => formatValue(arg)).join(" ")}` +} + +/** One value as `console.log` shows it. */ +export const formatValue = (value: Value): string => { + if (value === undefined) return "undefined" + if (typeof value === "string") return value + return formatConsoleValue(value, new Set(), 0) +} + +const formatConsoleValue = (value: Value, seen: Set, depth: number): string => { + if (value === null || value === undefined) return "null" + if (typeof value === "string") return JSON.stringify(value) + if (!(value instanceof Obj)) return value instanceof ToolReference ? "[opaque reference]" : String(value) + if (depth > MAX_CONSOLE_DEPTH) return "..." + if (seen.has(value)) return "[Circular]" + seen.add(value) + try { + return value.inspect((item) => formatConsoleValue(item, seen, depth + 1)) + } finally { + seen.delete(value) + } +} + +const formatConsoleTable = (value: Value, columnsArgument: Value): string => { + if (value === undefined) return "undefined" + if (containsOpaqueReference(value)) return "[opaque reference]" + const columns = columnsArgument instanceof Arr ? columnsArgument.items.map(String) : undefined + const rows = consoleTableRows(value, columns) + const keys = columns ?? Array.from(new Set(rows.flatMap((row) => Object.keys(row.values)))) + const header = ["(index)", ...keys].join("\t") + return [ + header, + ...rows.map((row) => [row.index, ...keys.map((key) => formatConsoleTableCell(row.values[key]))].join("\t")), + ].join("\n") +} + +const consoleTableRows = ( + data: Value, + columns: ReadonlyArray | undefined, +): Array<{ readonly index: string; readonly values: Record }> => { + if (data instanceof Arr) { + return data.items.map((item, index) => ({ index: String(index), values: consoleTableValues(item, columns) })) + } + if (data instanceof Obj) { + return entries(data).map(([index, item]) => ({ index, values: consoleTableValues(item, columns) })) + } + return [{ index: "0", values: { Value: data } }] +} + +const consoleTableValues = (value: Value, columns: ReadonlyArray | undefined): Record => { + if (value instanceof Obj && !(value instanceof Arr)) { + if (columns !== undefined) return Object.fromEntries(columns.map((column) => [column, get(value, column)])) + return Object.fromEntries(entries(value)) + } + return { Value: value } +} + +const formatConsoleTableCell = (value: Value): string => { + if (value === undefined) return "" + if (typeof value === "string") return value + return formatConsoleValue(value, new Set(), 0) +} diff --git a/packages/codemode/src/vm/stdlib/date.ts b/packages/codemode/src/vm/stdlib/date.ts new file mode 100644 index 00000000000..386399c1d92 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/date.ts @@ -0,0 +1,141 @@ +import { constructor, type Method, methods, prototypeFrom, receiver } from "../runtime/native.js" +import { rangeError } from "../runtime/model.js" +import { DateObj, Obj, coerceToNumber, coerceToString, type Value } from "../runtime/objects.js" +import { withPrimitives } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" + +const constructDate = (args: Array, proto: Obj) => { + if (args.length === 0) return new DateObj(proto, Date.now()) + if (args.length === 1) { + const arg = args[0] + if (arg instanceof DateObj) return new DateObj(proto, arg.time) + return withPrimitives("default", [arg], ([value]) => + typeof value === "string" + ? new DateObj(proto, Date.parse(value)) + : new DateObj(proto, new Date(coerceToNumber(value)).getTime()), + ) + } + // The spec converts at most seven components, in order, so extra arguments never run program code. + return toNumbers(args.slice(0, 7), (parts) => new DateObj(proto, new Date(...(parts as [number, number])).getTime())) +} + +// ToNumber on each value in turn, so a symbol throws before any later value's conversion runs program code. +const toNumbers = (values: Array, body: (numbers: Array) => Value) => + withPrimitives("number", values, (primitives) => body(primitives.map(coerceToNumber)), true) + +type Getter = keyof { + [K in keyof Date as Date[K] extends () => number ? K : never]: never +} + +const getters: ReadonlyArray = [ + "getFullYear", + "getMonth", + "getDate", + "getDay", + "getHours", + "getMinutes", + "getSeconds", + "getMilliseconds", + "getUTCFullYear", + "getUTCMonth", + "getUTCDate", + "getUTCDay", + "getUTCHours", + "getUTCMinutes", + "getUTCSeconds", + "getUTCMilliseconds", + "getTimezoneOffset", +] + +type Setter = keyof { + [K in keyof Date as Date[K] extends (value: number, ...rest: Array) => number ? K : never]: never +} + +const setters: ReadonlyArray = [ + ["setTime", 1], + ["setMilliseconds", 1], + ["setUTCMilliseconds", 1], + ["setSeconds", 2], + ["setUTCSeconds", 2], + ["setMinutes", 3], + ["setUTCMinutes", 3], + ["setHours", 4], + ["setUTCHours", 4], + ["setDate", 1], + ["setUTCDate", 1], + ["setMonth", 2], + ["setUTCMonth", 2], + ["setFullYear", 3], + ["setUTCFullYear", 3], +] + +export const dateGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Date + const date = constructor(builtins, proto, { + name: "Date", + length: 7, + // ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone. + call: () => new Date().toISOString(), + construct: (args, newTarget) => constructDate(args, prototypeFrom(newTarget, proto)), + }) + methods(builtins, date, [ + ["now", 0, () => Date.now()], + ["parse", 1, (_, args) => Date.parse(coerceToString(args[0]))], + [ + "UTC", + 7, + (_, args) => toNumbers(args.slice(0, 7), (parts) => Date.UTC(...(parts as Parameters))), + ], + ]) + + const self = (thisValue: Value, name: string) => receiver(DateObj, thisValue, `Date.prototype.${name}`) + const iso = (value: DateObj) => { + if (!Number.isFinite(value.time)) throw rangeError("Invalid time value.") + return new Date(value.time).toISOString() + } + methods(builtins, proto, [ + ["getTime", 0, (thisValue) => self(thisValue, "getTime").time], + ["valueOf", 0, (thisValue) => self(thisValue, "valueOf").time], + ["toISOString", 0, (thisValue) => iso(self(thisValue, "toISOString"))], + [ + "toJSON", + 1, + (thisValue) => { + const value = self(thisValue, "toJSON") + return Number.isFinite(value.time) ? iso(value) : null + }, + ], + ["toString", 0, (thisValue) => coerceToString(self(thisValue, "toString"))], + ["toDateString", 0, (thisValue) => new Date(self(thisValue, "toDateString").time).toDateString()], + ["toTimeString", 0, (thisValue) => new Date(self(thisValue, "toTimeString").time).toTimeString()], + ["toUTCString", 0, (thisValue) => new Date(self(thisValue, "toUTCString").time).toUTCString()], + ["toGMTString", 0, (thisValue) => new Date(self(thisValue, "toGMTString").time).toUTCString()], + ...(["toLocaleString", "toLocaleDateString", "toLocaleTimeString"] as const).map( + (name): Method => [ + name, + 0, + (thisValue) => new Date(self(thisValue, name).time)[name]("en-US", { timeZone: "UTC" }), + ], + ), + ...getters.map((name): Method => [name, 0, (thisValue) => new Date(self(thisValue, name).time)[name]()]), + ...setters.map( + ([name, length]): Method => [ + name, + length, + (thisValue, args) => { + const target = self(thisValue, name) + // Native setters read the current time before argument coercion, whose callbacks may mutate the Date. + return toNumbers([target.time, ...args.slice(0, length)], ([time, ...values]) => { + const hosted = new Date(time!) + // Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN. + if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear")) return NaN + target.time = hosted[name](...(values as [number, number, number, number])) + return target.time + }) + }, + ], + ), + ]) + return date +} diff --git a/packages/codemode/src/vm/stdlib/headers.ts b/packages/codemode/src/vm/stdlib/headers.ts new file mode 100644 index 00000000000..b817946533c --- /dev/null +++ b/packages/codemode/src/vm/stdlib/headers.ts @@ -0,0 +1,92 @@ +import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js" +import { IteratorSymbol, typeError } from "../runtime/model.js" +import { define, get, hidden, Arr, HeadersObj, Obj, coerceToString, type Value } from "../runtime/objects.js" +import { requireCallback } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { attempt, pairsForEach, readPairs } from "./url.js" + +const constructHeaders = (ctx: Interpreter, init: Value, proto: Obj) => { + if (init === undefined) return new HeadersObj(proto, new Headers()) + if (init instanceof Obj) return readPairs(ctx, init, "new Headers(...)", { kind: "Headers", proto }) + throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.") +} + +export const headersGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Headers + const headers = constructor(builtins, proto, { + name: "Headers", + call: requiresNew("Headers"), + construct: (args, newTarget) => constructHeaders(ctx, args[0], prototypeFrom(newTarget, proto)), + }) + const self = (thisValue: Value, name: string) => receiver(HeadersObj, thisValue, `Headers.prototype.${name}`) + const wrap = (items: Array) => new Arr(builtins.Array, items) + const arg = (args: Array, index: number): string => coerceToString(args[index]) + const requireArgs = (name: string, args: Array, count: number): void => { + if (args.length < count) throw typeError(`Headers.${name} requires ${count} argument${count === 1 ? "" : "s"}.`) + } + methods(builtins, proto, [ + [ + "append", + 2, + (thisValue, args) => { + requireArgs("append", args, 2) + const target = self(thisValue, "append").headers + attempt(() => target.append(arg(args, 0), arg(args, 1))) + return undefined + }, + ], + [ + "delete", + 1, + (thisValue, args) => { + requireArgs("delete", args, 1) + const target = self(thisValue, "delete").headers + attempt(() => target.delete(arg(args, 0))) + return undefined + }, + ], + [ + "get", + 1, + (thisValue, args) => { + requireArgs("get", args, 1) + const target = self(thisValue, "get").headers + return attempt(() => target.get(arg(args, 0))) + }, + ], + ["getSetCookie", 0, (thisValue) => wrap(self(thisValue, "getSetCookie").headers.getSetCookie())], + [ + "has", + 1, + (thisValue, args) => { + requireArgs("has", args, 1) + const target = self(thisValue, "has").headers + return attempt(() => target.has(arg(args, 0))) + }, + ], + [ + "set", + 2, + (thisValue, args) => { + requireArgs("set", args, 2) + const target = self(thisValue, "set").headers + attempt(() => target.set(arg(args, 0), arg(args, 1))) + return undefined + }, + ], + ...iteratorMethods(builtins, self), + [ + "forEach", + 1, + (thisValue, args) => { + requireArgs("forEach", args, 1) + const target = self(thisValue, "forEach") + const callback = requireCallback(args[0], "Headers.forEach") + return pairsForEach(ctx, target, Array.from(target.headers.entries()), callback, args[1]) + }, + ], + ]) + define(proto, IteratorSymbol, get(proto, "entries"), hidden) + return headers +} diff --git a/packages/codemode/src/vm/stdlib/iterator.ts b/packages/codemode/src/vm/stdlib/iterator.ts new file mode 100644 index 00000000000..4dd2206b874 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/iterator.ts @@ -0,0 +1,31 @@ +import type { Interpreter } from "../runtime/interpreter.js" +import { typeError } from "../runtime/model.js" +import { constructor, methods, receiver, requiresNew } from "../runtime/native.js" +import { iteratorResult, IteratorObj, Position } from "../runtime/objects.js" + +// Every built-in iterator and helper shares the Iterator prototype; JS gives each collection its own, which is only +// observable through getPrototypeOf. The helpers (`map`, `take`, `Iterator.from`, ...) are the prelude's. +export const iteratorGlobals = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.Iterator + methods(builtins, proto, [ + [ + "next", + 0, + (thisValue) => { + const cursor = receiver(IteratorObj, thisValue, "Iterator.prototype.next").cursor + if (!(cursor instanceof Position)) throw typeError("Iterator.prototype.next called on an internal iterator.") + const step = cursor.step() + return iteratorResult(builtins.Object, step.value, step.done) + }, + ], + ]) + return constructor(builtins, proto, { + name: "Iterator", + length: 0, + call: requiresNew("Iterator"), + construct: () => { + throw typeError("Iterator is abstract; use Iterator.from(...) or a built-in iterator.") + }, + }) +} diff --git a/packages/codemode/src/vm/stdlib/json.ts b/packages/codemode/src/vm/stdlib/json.ts new file mode 100644 index 00000000000..e6c9c286eb0 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/json.ts @@ -0,0 +1,51 @@ +import { methods } from "../runtime/native.js" +import { requireCallback } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { syntaxError } from "../runtime/model.js" +import { typeofValue } from "../runtime/references.js" +import { fromJson } from "../data.js" +import { Arr, Obj, coerceToString, record, type Value } from "../runtime/objects.js" +import { Invoke, Push } from "../runtime/signals.js" + +export const jsonGlobal = (ctx: Interpreter) => { + const json = new Obj(ctx.builtins.Object) + methods(ctx.builtins, json, [ + ["parse", 2, (_, args) => parse(ctx, args)], + ["stringify", 3, (_, args) => stringify(args)], + ]) + return json +} + +const parse = (ctx: Interpreter, args: Array) => { + const text = coerceToString(args[0]) + + const parsed = (() => { + try { + return fromJson(ctx, JSON.parse(text)) + } catch (error) { + throw syntaxError(`JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}`) + } + })() + if (typeofValue(args[1]) !== "function") return parsed + const reviver = requireCallback(args[1], "JSON.parse") + return new Invoke( + ctx.prelude.fn("revive"), + undefined, + [record(ctx.builtins.Object, { "": parsed }), "", reviver], + undefined, + ) +} + +const stringify = (args: Array) => { + const space = args[2] + const indent = typeof space === "number" || typeof space === "string" ? space : undefined + const replacer = args[1] + const properties = + replacer instanceof Arr + ? replacer.items + .filter((item): item is string | number => typeof item === "string" || typeof item === "number") + .map(String) + : undefined + const callback = typeofValue(replacer) === "function" ? requireCallback(replacer, "JSON.stringify") : undefined + return new Push("JSON.stringify", [args[0], callback, properties, indent]) +} diff --git a/packages/codemode/src/vm/stdlib/math.ts b/packages/codemode/src/vm/stdlib/math.ts new file mode 100644 index 00000000000..e5030dd98ad --- /dev/null +++ b/packages/codemode/src/vm/stdlib/math.ts @@ -0,0 +1,75 @@ +import { constants, type Method, methods } from "../runtime/native.js" +import { Obj, coerceToNumber } from "../runtime/objects.js" +import { withPrimitives } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" + +export const mathGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const math = new Obj(builtins.Object) + // Convert only the arguments a method consumes; like JS, extras are ignored + // (so built-ins work as callbacks receiving (element, index, array)). + const unary = (name: string, op: (a: number) => number): Method => [ + name, + 1, + (_, args) => withPrimitives("number", [args[0]], ([a]) => op(coerceToNumber(a))), + ] + const binary = (name: string, op: (a: number, b: number) => number): Method => [ + name, + 2, + (_, args) => withPrimitives("number", [args[0], args[1]], ([a, b]) => op(coerceToNumber(a), coerceToNumber(b))), + ] + const variadic = (name: string, op: (...values: Array) => number): Method => [ + name, + 2, + (_, args) => withPrimitives("number", args, (values) => op(...values.map(coerceToNumber))), + ] + constants(math, { + PI: Math.PI, + E: Math.E, + LN2: Math.LN2, + LN10: Math.LN10, + LOG2E: Math.LOG2E, + LOG10E: Math.LOG10E, + SQRT2: Math.SQRT2, + SQRT1_2: Math.SQRT1_2, + }) + methods(builtins, math, [ + ["random", 0, () => Math.random()], + variadic("max", Math.max), + variadic("min", Math.min), + variadic("hypot", Math.hypot), + unary("abs", Math.abs), + unary("acos", Math.acos), + unary("acosh", Math.acosh), + unary("asin", Math.asin), + unary("asinh", Math.asinh), + unary("atan", Math.atan), + binary("atan2", Math.atan2), + unary("atanh", Math.atanh), + unary("floor", Math.floor), + unary("ceil", Math.ceil), + unary("round", Math.round), + unary("trunc", Math.trunc), + unary("sign", Math.sign), + unary("sqrt", Math.sqrt), + unary("cbrt", Math.cbrt), + binary("pow", Math.pow), + unary("cos", Math.cos), + unary("cosh", Math.cosh), + unary("sin", Math.sin), + unary("sinh", Math.sinh), + unary("tan", Math.tan), + unary("tanh", Math.tanh), + unary("log", Math.log), + unary("log2", Math.log2), + unary("log10", Math.log10), + unary("log1p", Math.log1p), + unary("exp", Math.exp), + unary("expm1", Math.expm1), + unary("f16round", Math.f16round), + unary("fround", Math.fround), + unary("clz32", Math.clz32), + binary("imul", Math.imul), + ]) + return math +} diff --git a/packages/codemode/src/vm/stdlib/number.ts b/packages/codemode/src/vm/stdlib/number.ts new file mode 100644 index 00000000000..c21ec30990a --- /dev/null +++ b/packages/codemode/src/vm/stdlib/number.ts @@ -0,0 +1,76 @@ +import { constructor, constants, type Method, methods } from "../runtime/native.js" +import { coerceToNumber, type Value } from "../runtime/objects.js" +import { typeError } from "../runtime/model.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { withPrimitives } from "../runtime/callback.js" +import { coerce } from "./value.js" + +export const numberGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const number = constructor(builtins, builtins.Number, { + name: "Number", + length: 1, + call: (_, args) => coerce(ctx, "Number", args), + }) + constants(number, { + MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER, + MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER, + MAX_VALUE: Number.MAX_VALUE, + MIN_VALUE: Number.MIN_VALUE, + EPSILON: Number.EPSILON, + NaN: Number.NaN, + POSITIVE_INFINITY: Number.POSITIVE_INFINITY, + NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY, + }) + methods(builtins, number, [ + ["isInteger", 1, (_, args) => Number.isInteger(args[0])], + ["isFinite", 1, (_, args) => Number.isFinite(args[0])], + ["isNaN", 1, (_, args) => Number.isNaN(args[0])], + ["isSafeInteger", 1, (_, args) => Number.isSafeInteger(args[0])], + ["parseInt", 2, (_, args) => coerce(ctx, "parseInt", args)], + ["parseFloat", 1, (_, args) => coerce(ctx, "parseFloat", args)], + ]) + + const self = (thisValue: Value, name: string): number => { + if (typeof thisValue === "number") return thisValue + throw typeError(`Number.prototype.${name} requires that 'this' be a Number.`) + } + // The receiver is checked first, then the one argument converts through ToPrimitive with the number hint. + const formatting = (name: string, op: (value: number, digits: number | undefined) => string): Method => [ + name, + 1, + (thisValue, args) => { + const value = self(thisValue, name) + return withPrimitives("number", [args[0]], ([digits]) => + op(value, digits === undefined ? undefined : coerceToNumber(digits)), + ) + }, + ] + methods(builtins, builtins.Number, [ + formatting("toFixed", (value, digits) => value.toFixed(digits)), + ["toLocaleString", 0, (thisValue) => self(thisValue, "toLocaleString").toLocaleString("en-US")], + formatting("toExponential", (value, digits) => value.toExponential(digits)), + formatting("toPrecision", (value, digits) => (digits === undefined ? value.toString() : value.toPrecision(digits))), + formatting("toString", (value, radix) => value.toString(radix)), + ["valueOf", 0, (thisValue) => self(thisValue, "valueOf")], + ]) + return number +} + +export const booleanGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const boolean = constructor(builtins, builtins.Boolean, { + name: "Boolean", + length: 1, + call: (_, args) => coerce(ctx, "Boolean", args), + }) + const self = (thisValue: Value, name: string): boolean => { + if (typeof thisValue === "boolean") return thisValue + throw typeError(`Boolean.prototype.${name} requires that 'this' be a Boolean.`) + } + methods(builtins, builtins.Boolean, [ + ["toString", 0, (thisValue) => String(self(thisValue, "toString"))], + ["valueOf", 0, (thisValue) => self(thisValue, "valueOf")], + ]) + return boolean +} diff --git a/packages/codemode/src/vm/stdlib/object.ts b/packages/codemode/src/vm/stdlib/object.ts new file mode 100644 index 00000000000..09e8482a6a1 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/object.ts @@ -0,0 +1,227 @@ +import { constructor, methods, receiver } from "../runtime/native.js" +import { type AstNode, AsyncIteratorSymbol, invalidData, IteratorSymbol, typeError } from "../runtime/model.js" +import { + changed, + entries, + enumerableKeys, + getOwn, + hasOwn, + hasPrototype, + keys, + own, + ownKeys, + Arr, + Bytes, + Obj, + PromiseObj, + set, + coerceToString, + type Value, +} from "../runtime/objects.js" +import { primitivePrototype } from "../runtime/intrinsics.js" +import { describeValue, rejectCircularInsertion } from "../runtime/references.js" +import { invokeMethod } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { ToolReference } from "../../tool-runtime.js" + +// ToObject for enumeration. +export const enumerableSource = (ctx: Interpreter, label: string, value: Value, node?: AstNode): Obj => { + if (value === null || value === undefined) { + throw typeError(`${label} cannot convert ${describeValue(value)} to an object.`, node) + } + if (value instanceof PromiseObj) { + throw invalidData(`${label} received an un-awaited Promise; await it before inspecting the result.`, node) + } + if (value instanceof ToolReference) { + throw invalidData( + `${label} cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, + node, + ) + } + if (typeof value === "string") return new Arr(ctx.builtins.Array, [...value]) + if (value instanceof Obj) return value + return new Obj(ctx.builtins.Object) +} + +export const objectAssign = (ctx: Interpreter, args: Array): Value => { + const target = args[0] + // JS would box a primitive target; wrappers and primitives cannot hold fields here. + if (!(target instanceof Obj)) { + throw typeError(`Object.assign expects a data object or array target, received ${describeValue(target)}.`) + } + const seen = new Set() + for (const source of args.slice(1)) { + if (source === null || source === undefined) continue + const from = enumerableSource(ctx, "Object.assign(...)", source) + for (const key of enumerableKeys(from)) { + rejectCircularInsertion(target, getOwn(from, key), "Object.assign result", seen) + if (!set(target, key, getOwn(from, key))) { + throw typeError(`Cannot assign to read only property '${String(key)}'.`) + } + } + } + return target +} + +const classTag = (value: Value): string => { + if (value === null) return "Null" + if (value === undefined) return "Undefined" + if (value instanceof Obj) return value.tag + if (typeof value === "string") return "String" + if (typeof value === "number") return "Number" + if (typeof value === "boolean") return "Boolean" + return "Object" +} + +const propertyKey = (value: Value): PropertyKey => + value === AsyncIteratorSymbol || value === IteratorSymbol ? value : coerceToString(value) + +// SetIntegrityLevel: primitives pass through. A typed array's bytes cannot carry attributes, so JS throws after +// already making it non-extensible. +const restrict = (level: "freeze" | "seal" | "preventExtensions", value: Value): Value => { + if (!(value instanceof Obj)) return value + changed(value) + value.extensible = false + if (level === "preventExtensions") return value + if (value instanceof Bytes && value.bytes.length > 0) { + throw typeError(`Cannot ${level} array buffer views with elements.`) + } + for (const slot of value.props.values()) { + slot.configurable = false + if (level === "freeze" && "value" in slot) slot.writable = false + } + if (value instanceof Arr) value.elements = { writable: level === "seal", enumerable: true, configurable: false } + return value +} + +// TestIntegrityLevel: array elements and `length` answer through `own`, so a non-extensible empty array is sealed +// but not frozen, as in JS. +const integrity = (value: Value, frozen: boolean): boolean => { + if (!(value instanceof Obj)) return true + if (value.extensible) return false + return ownKeys(value).every((key) => { + const slot = own(value, key) + return slot !== undefined && !slot.configurable && !(frozen && "value" in slot && slot.writable) + }) +} + +// Object constructs identically with or without new, like JS. Only `keys` copies its result into the +// program; `values`, `entries`, `assign`, and `fromEntries` hand back the program's own values. +export const objectGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const construct = (args: Array): Value => { + const first = args[0] + if (first === null || first === undefined) return new Obj(builtins.Object) + if (first instanceof Obj) return first + throw typeError(`Object(${typeof first}) wrapper objects are not supported; use the primitive value directly.`) + } + const object = constructor(builtins, builtins.Object, { + name: "Object", + length: 1, + call: (_, args) => construct(args), + construct: (args) => construct(args), + }) + methods(builtins, object, [ + [ + "keys", + 1, + (_, args) => + new Arr( + builtins.Array, + args[0] instanceof ToolReference + ? [...ctx.tools.keys(args[0].path)] + : keys(enumerableSource(ctx, "Object.keys(...)", args[0])), + ), + ], + [ + "values", + 1, + (_, args) => + new Arr( + builtins.Array, + entries(enumerableSource(ctx, "Object.values(...)", args[0])).map((entry) => entry[1]), + ), + ], + [ + "entries", + 1, + (_, args) => + new Arr( + builtins.Array, + entries(enumerableSource(ctx, "Object.entries(...)", args[0])).map((entry) => new Arr(builtins.Array, entry)), + ), + ], + ["hasOwn", 2, (_, args) => hasOwn(enumerableSource(ctx, "Object.hasOwn(...)", args[0]), propertyKey(args[1]))], + ["is", 2, (_, args) => Object.is(args[0], args[1])], + ["assign", 2, (_, args) => objectAssign(ctx, args)], + ["freeze", 1, (_, args) => restrict("freeze", args[0])], + ["seal", 1, (_, args) => restrict("seal", args[0])], + ["preventExtensions", 1, (_, args) => restrict("preventExtensions", args[0])], + ["isFrozen", 1, (_, args) => integrity(args[0], true)], + ["isSealed", 1, (_, args) => integrity(args[0], false)], + ["isExtensible", 1, (_, args) => args[0] instanceof Obj && args[0].extensible], + [ + "getPrototypeOf", + 1, + (_, args) => { + if (args[0] instanceof Obj) return args[0].proto + const proto = primitivePrototype(builtins, args[0]) + if (proto === undefined) { + throw typeError(`Object.getPrototypeOf cannot convert ${describeValue(args[0])} to an object.`) + } + return proto + }, + ], + [ + "create", + 2, + (_, args) => { + if (args[0] !== null && !(args[0] instanceof Obj)) { + throw typeError("Object prototype may only be an Object or null.") + } + if (args[1] !== undefined) { + throw typeError( + "Object.create property descriptors are not supported; assign the fields after creating the object.", + ) + } + return new Obj(args[0]) + }, + ], + ]) + methods(builtins, builtins.Object, [ + [ + "hasOwnProperty", + 1, + (thisValue, args) => hasOwn(receiver(Obj, thisValue, "Object.prototype.hasOwnProperty"), propertyKey(args[0])), + ], + [ + "isPrototypeOf", + 1, + (thisValue, args) => hasPrototype(args[0], receiver(Obj, thisValue, "Object.prototype.isPrototypeOf")), + ], + [ + "propertyIsEnumerable", + 1, + (thisValue, args) => + own(receiver(Obj, thisValue, "Object.prototype.propertyIsEnumerable"), propertyKey(args[0]))?.enumerable === + true, + ], + ["toString", 0, (thisValue) => `[object ${classTag(thisValue)}]`], + [ + "toLocaleString", + 0, + (thisValue) => invokeMethod(ctx.builtins, thisValue, "toString", "Object.prototype.toLocaleString"), + ], + [ + "valueOf", + 0, + (thisValue) => { + if (thisValue === null || thisValue === undefined) { + throw typeError("Object.prototype.valueOf called on null or undefined.") + } + return thisValue + }, + ], + ]) + return object +} diff --git a/packages/codemode/src/vm/stdlib/regexp.ts b/packages/codemode/src/vm/stdlib/regexp.ts new file mode 100644 index 00000000000..5a8fac99eb4 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/regexp.ts @@ -0,0 +1,152 @@ +import type { Builtins } from "../runtime/intrinsics.js" +import { constructor, type Method, methods, prototypeFrom, receiver } from "../runtime/native.js" +import { syntaxError, typeError } from "../runtime/model.js" +import { + define, + defineAccessor, + Arr, + Obj, + RegExpObj, + record, + coerceToNumber, + coerceToString, + type Value, +} from "../runtime/objects.js" +import type { Interpreter } from "../runtime/interpreter.js" + +const flagProperties = [ + "hasIndices", + "global", + "ignoreCase", + "multiline", + "sticky", + "unicode", + "unicodeSets", + "dotAll", +] as const + +const regexFailureReason = (error: unknown): string => + (error instanceof Error ? error.message : String(error)).replace(/^Invalid regular expression:\s*/i, "") + +const escapeRegexHint = + 'To match special characters like ( ) [ ] { } + * ? . literally, escape them with a backslash (e.g. "\\\\(") or test for them with String.includes instead.' + +/** Runs host regex work on a RegExp argument, sharing its `lastIndex`, or on a pattern string compiled for the call. */ +export const withHostRegex = (arg: Value, method: string, extraFlags: string, work: (regex: RegExp) => T): T => { + if (arg instanceof RegExpObj) return arg.use(work) + // Anything else is a pattern string, as `new RegExp(arg)` would read it: undefined is the empty pattern. + const source = arg === undefined ? "" : coerceToString(arg) + return work(compilePattern(source, method, extraFlags)) +} + +const compilePattern = (source: string, method: string, extraFlags: string): RegExp => { + try { + return new RegExp(source, extraFlags) + } catch (error) { + throw syntaxError( + `String.${method} received the string ${JSON.stringify(source)}, which is not a valid regular expression pattern (${regexFailureReason(error)}). ${escapeRegexHint}`, + ) + } +} + +export const matchToValue = (builtins: Builtins, match: RegExpMatchArray): Arr => { + const result = new Arr( + builtins.Array, + Array.from(match, (group) => group), + ) + if (match.index !== undefined) define(result, "index", match.index) + if (match.input !== undefined) define(result, "input", match.input) + if (match.groups) define(result, "groups", record(builtins.Object, match.groups)) + if (match.indices) define(result, "indices", indicesToValue(builtins, match.indices)) + return result +} + +export const constructRegExp = (builtins: Builtins, args: Array, proto: Obj = builtins.RegExp): RegExpObj => { + const first = args[0] + const pattern = first instanceof RegExpObj ? first.source : first === undefined ? "" : coerceToString(first) + const flagsArg = args[1] + if (flagsArg !== undefined && typeof flagsArg !== "string") { + throw syntaxError( + `RegExp flags must be a string of flag characters (e.g. "g", "gi"), not ${flagsArg === null ? "null" : typeof flagsArg}.`, + ) + } + const flags = flagsArg ?? (first instanceof RegExpObj ? first.flags : "") + try { + return new RegExpObj(proto, pattern, flags) + } catch (error) { + const reason = regexFailureReason(error) + throw syntaxError( + /flag/i.test(reason) + ? `new RegExp(...) received invalid flags ${JSON.stringify(flags)} (${reason}). Valid flags are d, g, i, m, s, u, v, and y.` + : `new RegExp(...) received ${JSON.stringify(pattern)}, which is not a valid regular expression pattern (${reason}). ${escapeRegexHint}`, + ) + } +} + +// RegExp constructs identically with or without new, like JS. +export const regexpGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.RegExp + const regexp = constructor(builtins, proto, { + name: "RegExp", + length: 2, + call: (_, args) => constructRegExp(builtins, args), + construct: (args, newTarget) => constructRegExp(builtins, args, prototypeFrom(newTarget, proto)), + }) + methods(builtins, regexp, [ + [ + "escape", + 1, + (_, args) => { + if (typeof args[0] !== "string") throw typeError("RegExp.escape expects a string.") + return RegExp.escape(args[0]) + }, + ], + ]) + + const self = (thisValue: Value, name: string) => receiver(RegExpObj, thisValue, `RegExp.prototype.${name}`) + defineAccessor(proto, "source", (thisValue) => self(thisValue, "source").source) + defineAccessor(proto, "flags", (thisValue) => self(thisValue, "flags").flags) + // One lastIndex per RegExp, so exec/test and the String methods share one counter. + defineAccessor( + proto, + "lastIndex", + (thisValue) => self(thisValue, "lastIndex").lastIndex, + (thisValue, value) => { + self(thisValue, "lastIndex").lastIndex = coerceToNumber(value) + }, + ) + for (const name of flagProperties) defineAccessor(proto, name, (thisValue) => self(thisValue, name).regex[name]) + const run = (name: "exec" | "test"): Method => [ + name, + 1, + (thisValue, args) => { + const value = self(thisValue, name) + const input = coerceToString(args[0]) + const matched = value.use((regex) => regex.exec(input)) + if (name === "test") return matched !== null + return matched === null ? null : matchToValue(builtins, matched) + }, + ] + methods(builtins, proto, [ + run("exec"), + run("test"), + ["toString", 0, (thisValue) => coerceToString(self(thisValue, "toString"))], + ]) + return regexp +} + +const indicesToValue = (builtins: Builtins, indices: RegExpIndicesArray): Arr => { + const range = (pair: [number, number] | undefined) => + pair === undefined ? undefined : new Arr(builtins.Array, [...pair]) + const result = new Arr(builtins.Array, Array.from(indices, range)) + const groups = indices.groups + define( + result, + "groups", + groups === undefined + ? undefined + : record(builtins.Object, Object.fromEntries(Object.entries(groups).map(([key, pair]) => [key, range(pair)]))), + ) + return result +} diff --git a/packages/codemode/src/vm/stdlib/string.ts b/packages/codemode/src/vm/stdlib/string.ts new file mode 100644 index 00000000000..e5da527e2de --- /dev/null +++ b/packages/codemode/src/vm/stdlib/string.ts @@ -0,0 +1,386 @@ +import { type Hint, Invoke } from "../runtime/signals.js" +import { constructor, fn, type Method, methods } from "../runtime/native.js" +import { checkArrayLength, checkStringLength } from "../runtime/limits.js" +import { invalidData, IteratorSymbol, rangeError, typeError } from "../runtime/model.js" +import { + define, + get, + hidden, + Arr, + positionIterator, + Obj, + RegExpObj, + record, + coerceToInteger, + coerceToNumber, + coerceToString, + type NativeResult, + type Value, +} from "../runtime/objects.js" +import { containsOpaqueReference, typeofValue } from "../runtime/references.js" +import { isSupportedCallback, requireCallback, withPrimitives } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { matchToValue, withHostRegex } from "./regexp.js" +import { coerce } from "./value.js" + +// console is intercepted by the interpreter before reaching here. +const requireDataArgument = (name: string, index: number, arg: Value): Value => { + if (containsOpaqueReference(arg)) { + throw invalidData(`String.${name} expects argument ${index + 1} to be a data value.`) + } + return arg +} + +const replaceAllNeedsGlobal = (pattern: RegExp) => { + if (!pattern.global) { + throw typeError( + `String.replaceAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.replace to replace only the first match.`, + ) + } +} + +// The matches are found first, as the host replaces; then the callback runs for each in order. +const replaceWithCallback = ( + ctx: Interpreter, + value: string, + name: "replace" | "replaceAll", + args: Array, +): Invoke => { + const builtins = ctx.builtins + const callback = requireCallback(args[1], `String.${name}`) + const matches: Array = [] + // The host calls back with (match, ...captures, offset, string, groups?); only groups is not already a Value. + const collect = ( + ...callbackArgs: Array> + ): string => { + const match = callbackArgs[0] + const hasGroups = typeof callbackArgs.at(-1) === "object" + const offset = callbackArgs[callbackArgs.length - (hasGroups ? 3 : 2)] + if (typeof match !== "string" || typeof offset !== "number") { + throw typeError(`String.${name} produced an invalid replacement match.`) + } + const args = callbackArgs.map((arg) => (typeof arg === "object" ? record(builtins.Object, arg) : arg)) + matches.push({ match, offset, args }) + return match + } + + const pattern = args[0] + if (pattern instanceof RegExpObj) { + pattern.use((regex) => { + if (name === "replaceAll") replaceAllNeedsGlobal(regex) + if (name === "replace") value.replace(regex, collect) + else value.replaceAll(regex, collect) + }) + } else { + const search = coerceToString(requireDataArgument(name, 0, pattern)) + if (name === "replace") value.replace(search, collect) + else value.replaceAll(search, collect) + } + const pieces = matches.map( + (match, index) => + new Arr(builtins.Array, [ + value.slice(index === 0 ? 0 : matches[index - 1]!.offset + matches[index - 1]!.match.length, match.offset), + new Arr(builtins.Array, match.args), + ]), + ) + const last = matches.at(-1) + const rest = value.slice(last === undefined ? 0 : last.offset + last.match.length) + return new Invoke(ctx.prelude.fn("replaceWith"), undefined, [new Arr(builtins.Array, pieces), rest, callback], undefined) +} + +type Match = { readonly match: string; readonly offset: number; readonly args: Array } + +export const stringGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const string = constructor(builtins, builtins.String, { + name: "String", + length: 1, + call: (_, args) => coerce(ctx, "String", args), + }) + const codeUnits = (name: string, op: (...codes: Array) => string): Method => [ + name, + 1, + (_, args) => op(...args.map(coerceToNumber)), + ] + methods(builtins, string, [ + codeUnits("fromCharCode", String.fromCharCode), + codeUnits("fromCodePoint", String.fromCodePoint), + [ + "raw", + 1, + (_, args) => { + const template = args[0] + const raw = template instanceof Obj ? get(template, "raw") : undefined + if (!(raw instanceof Obj)) throw typeError("String.raw expects a template object with a raw array.") + const count = Math.max(0, coerceToInteger(get(raw, "length"))) + checkArrayLength(count) + // Each literal is followed by its substitution, except the last literal, or when substitutions run out. + const parts = Array.from({ length: count }, (_, index) => + index + 1 < count && index + 1 < args.length ? [get(raw, index), args[index + 1]] : [get(raw, index)], + ).flat() + return withPrimitives( + "string", + parts, + (strings) => { + const output = strings.map(coerceToString).join("") + checkStringLength(output.length) + return output + }, + true, + ) + }, + ], + ]) + + const self = (thisValue: Value, name: string): string => { + if (typeof thisValue === "string") return thisValue + if (thisValue === null || thisValue === undefined) { + throw typeError(`String.prototype.${name} called on null or undefined.`) + } + return coerceToString(thisValue) + } + // Coerce arguments like native JS; opaque runtime references still reject. + const str = (name: string, args: Array, index: number): string => + coerceToString(requireDataArgument(name, index, args[index])) + const num = (name: string, args: Array, index: number): number => + coerceToNumber(requireDataArgument(name, index, args[index])) + const optNum = (name: string, args: Array, index: number): number | undefined => + args[index] === undefined ? undefined : num(name, args, index) + const optStr = (name: string, args: Array, index: number): string | undefined => + args[index] === undefined ? undefined : str(name, args, index) + // ToPrimitive in spec order: the receiver, then the arguments the method consumes, one hint per position; the + // rest pass through as they are. + const simple = ( + name: string, + length: number, + op: (value: string, args: Array) => NativeResult, + hints: ReadonlyArray = [], + ): Method => [ + name, + length, + (thisValue, args) => { + if (thisValue === null || thisValue === undefined) { + throw typeError(`String.prototype.${name} called on null or undefined.`) + } + return withPrimitives( + ["string", ...hints], + [thisValue, ...args.slice(0, hints.length)], + ([value, ...primitives]) => op(coerceToString(value), [...primitives, ...args.slice(hints.length)]), + ) + }, + ] + // includes, startsWith, and endsWith reject a RegExp before converting their search string and position. + const searching = (name: string, op: (value: string, search: string, position: number | undefined) => boolean) => + simple(name, 1, (value, args) => { + if (args[0] instanceof RegExpObj) { + throw typeError( + `String.${name} cannot take a regular expression; use regex.test(string) or String.search instead.`, + ) + } + return withPrimitives(["string", "number"], args.slice(0, 2), (primitives) => + op(value, str(name, primitives, 0), optNum(name, primitives, 1)), + ) + }) + // match, matchAll, and search read a RegExp as is and convert anything else to its pattern text. + const withPattern = (args: Array, op: (pattern: Value) => Value) => + args[0] instanceof RegExpObj ? op(args[0]) : withPrimitives("string", [args[0]], ([text]) => op(text)) + const replace = (name: "replace" | "replaceAll") => + simple(name, 2, (value, args) => { + const pattern = args[0] + const replacer = args[1] + // A RegExp pattern is used as is; a plain one converts to its search string, then a non-callable replacement. + return withPrimitives( + "string", + [pattern instanceof RegExpObj ? undefined : pattern, isSupportedCallback(replacer) ? undefined : replacer], + ([search, replacement]) => { + if (isSupportedCallback(replacer)) { + return replaceWithCallback(ctx, value, name, [pattern instanceof RegExpObj ? pattern : search, replacer]) + } + if (typeofValue(replacer) === "function") { + throw typeError( + `String.${name} cannot use this callable as a replacer; wrap it in an arrow function, e.g. (match) => tools.ns.tool(match).`, + ) + } + const primitives = [search, replacement] + if (pattern instanceof RegExpObj) { + const text = str(name, primitives, 1) + return pattern.use((regex) => { + if (name === "replaceAll") replaceAllNeedsGlobal(regex) + return name === "replace" ? value.replace(regex, text) : value.replaceAll(regex, text) + }) + } + if (name === "replace") return value.replace(str(name, primitives, 0), str(name, primitives, 1)) + return value.replaceAll(str(name, primitives, 0), str(name, primitives, 1)) + }, + ) + }) + + methods(builtins, builtins.String, [ + simple("toString", 0, (value) => value), + simple("valueOf", 0, (value) => value), + simple("toLowerCase", 0, (value) => value.toLowerCase()), + simple("toUpperCase", 0, (value) => value.toUpperCase()), + simple("toLocaleLowerCase", 0, (value) => value.toLowerCase()), + simple("toLocaleUpperCase", 0, (value) => value.toUpperCase()), + simple("trim", 0, (value) => value.trim()), + simple("trimStart", 0, (value) => value.trimStart()), + simple("trimLeft", 0, (value) => value.trimStart()), + simple("trimEnd", 0, (value) => value.trimEnd()), + simple("trimRight", 0, (value) => value.trimEnd()), + // Locale/options are deliberately unsupported; comparison uses the host default locale. + simple("localeCompare", 1, (value, args) => value.localeCompare(str("localeCompare", args, 0)), ["string"]), + simple( + "normalize", + 0, + (value, args) => { + const form = optStr("normalize", args, 0) + try { + return value.normalize(form) + } catch { + throw rangeError( + `String.normalize expects the form "NFC", "NFD", "NFKC", or "NFKD" (got ${JSON.stringify(form)}).`, + ) + } + }, + ["string"], + ), + simple("split", 2, (value, args) => { + const separator = args[0] + // A RegExp separator is used as is; the limit converts before a plain separator does, as in the spec. + return withPrimitives( + ["number", "string"], + [args[1], separator instanceof RegExpObj ? undefined : separator], + ([limit, pattern]) => { + const wrap = (parts: Array) => new Arr(builtins.Array, parts) + // Native: an undefined separator returns the whole string, not a split on "undefined", + // unless the limit truncates to zero. + const requestedLimit = args[1] === undefined ? undefined : num("split", [pattern, limit], 1) + if (separator === undefined) { + return wrap(requestedLimit !== undefined && requestedLimit >>> 0 === 0 ? [] : [value]) + } + const parts = + separator instanceof RegExpObj + ? separator.use((regex) => value.split(regex, requestedLimit)) + : value.split(str("split", [pattern], 0), requestedLimit === undefined ? undefined : requestedLimit >>> 0) + checkArrayLength(parts.length) + return wrap(parts) + }, + ) + }), + simple("slice", 2, (value, args) => value.slice(optNum("slice", args, 0), optNum("slice", args, 1)), [ + "number", + "number", + ]), + searching("includes", (value, search, position) => value.includes(search, position)), + searching("startsWith", (value, search, position) => value.startsWith(search, position)), + searching("endsWith", (value, search, position) => value.endsWith(search, position)), + simple("indexOf", 1, (value, args) => value.indexOf(str("indexOf", args, 0), optNum("indexOf", args, 1)), [ + "string", + "number", + ]), + simple( + "lastIndexOf", + 1, + (value, args) => value.lastIndexOf(str("lastIndexOf", args, 0), optNum("lastIndexOf", args, 1)), + ["string", "number"], + ), + replace("replace"), + replace("replaceAll"), + simple("match", 1, (value, args) => + withPattern(args, (arg) => + withHostRegex(arg, "match", "", (regex) => { + const matched = value.match(regex) + if (matched === null) return null + // Preserve the own `index` and `groups` properties on non-global matches. + if (regex.global) return new Arr(builtins.Array, [...matched]) + return matchToValue(builtins, matched) + }), + ), + ), + simple("matchAll", 1, (value, args) => + withPattern(args, (arg) => + withHostRegex(arg, "matchAll", "g", (regex) => { + if (!regex.global) { + throw typeError( + `String.matchAll requires a regular expression with the global (g) flag: write /${regex.source}/${regex.flags}g, or use String.match for a single match.`, + ) + } + const matches: Array = [] + for (const match of value.matchAll(regex)) { + checkArrayLength(matches.length + 1) + matches.push(matchToValue(builtins, match)) + } + return new Arr(builtins.Array, matches) + }), + ), + ), + simple("search", 1, (value, args) => + withPattern(args, (arg) => withHostRegex(arg, "search", "", (regex) => value.search(regex))), + ), + simple( + "repeat", + 1, + (value, args) => { + const count = num("repeat", args, 0) + if (!Number.isFinite(count) || count < 0) { + throw rangeError("String.repeat expects a finite non-negative count.") + } + checkStringLength(value.length * count) + return value.repeat(count) + }, + ["number"], + ), + simple( + "padStart", + 1, + (value, args) => { + const length = num("padStart", args, 0) + checkStringLength(length) + return value.padStart(length, optStr("padStart", args, 1)) + }, + ["number", "string"], + ), + simple( + "padEnd", + 1, + (value, args) => { + const length = num("padEnd", args, 0) + checkStringLength(length) + return value.padEnd(length, optStr("padEnd", args, 1)) + }, + ["number", "string"], + ), + simple("charAt", 1, (value, args) => value.charAt(optNum("charAt", args, 0) ?? 0), ["number"]), + simple("at", 1, (value, args) => value.at(optNum("at", args, 0) ?? 0), ["number"]), + simple( + "substring", + 2, + (value, args) => value.substring(optNum("substring", args, 0) ?? 0, optNum("substring", args, 1)), + ["number", "number"], + ), + simple("substr", 2, (value, args) => value.substr(optNum("substr", args, 0) ?? 0, optNum("substr", args, 1)), [ + "number", + "number", + ]), + simple("isWellFormed", 0, (value) => value.isWellFormed()), + simple("toWellFormed", 0, (value) => value.toWellFormed()), + simple("charCodeAt", 1, (value, args) => value.charCodeAt(optNum("charCodeAt", args, 0) ?? 0), ["number"]), + simple("codePointAt", 1, (value, args) => value.codePointAt(optNum("codePointAt", args, 0) ?? 0), ["number"]), + simple("concat", 1, (value, args) => + withPrimitives("string", args, (parts) => { + const joined = value.concat(...parts.map((_, index) => str("concat", parts, index))) + checkStringLength(joined.length) + return joined + }), + ), + ]) + define( + builtins.String, + IteratorSymbol, + fn(builtins, "[Symbol.iterator]", 0, (thisValue) => + positionIterator(builtins, self(thisValue, "[Symbol.iterator]"), "values"), + ), + hidden, + ) + return string +} diff --git a/packages/codemode/src/vm/stdlib/url.ts b/packages/codemode/src/vm/stdlib/url.ts new file mode 100644 index 00000000000..9efe74aa8a1 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/url.ts @@ -0,0 +1,310 @@ +import { Invoke } from "../runtime/signals.js" +import { + constructor, + fn, + type Method, + methods, + prototypeFrom, + receiver, + requiresNew, + iteratorMethods, +} from "../runtime/native.js" +import { IteratorSymbol, PendingThrow, typeError, uriError } from "../runtime/model.js" +import { + define, + defineAccessor, + entries, + get, + hidden, + Arr, + Obj, + URLObj, + URLSearchParamsObj, + HeadersObj, + type Callable, + coerceToString, + isRuntimeReference, + type Value, +} from "../runtime/objects.js" +import { requireCallback } from "../runtime/callback.js" +import type { Interpreter } from "../runtime/interpreter.js" + +const urlProperties = [ + "href", + "origin", + "protocol", + "username", + "password", + "host", + "hostname", + "port", + "pathname", + "search", + "hash", +] as const + +type UriFunction = "encodeURI" | "encodeURIComponent" | "decodeURI" | "decodeURIComponent" + +const uriFunctions: Record string> = { + encodeURI, + encodeURIComponent, + decodeURI, + decodeURIComponent, +} + +export const uriGlobal = (ctx: Interpreter, name: UriFunction) => + fn(ctx.builtins, name, 1, (_, args) => { + const value = coerceToString(args[0]) + try { + return uriFunctions[name](value) + } catch (error) { + throw uriError(`${name} received malformed URI data: ${error instanceof Error ? error.message : String(error)}`) + } + }) + +const urlArgument = (value: Value): string => (value instanceof URLObj ? value.url.href : coerceToString(value)) + +export const urlGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.URL + const construct = (args: Array, into: Obj): URLObj => { + if (args.length === 0) { + throw typeError("new URL(...) requires a URL string and an optional base URL.") + } + const input = urlArgument(args[0]) + const base = args[1] === undefined ? undefined : urlArgument(args[1]) + try { + return new URLObj(into, builtins.URLSearchParams, new URL(input, base)) + } catch { + throw typeError(`new URL(...) received an invalid URL${base === undefined ? "" : " or base URL"}.`) + } + } + const url = constructor(builtins, proto, { + name: "URL", + length: 1, + call: requiresNew("URL"), + construct: (args, newTarget) => construct(args, prototypeFrom(newTarget, proto)), + }) + const parse = (name: "canParse" | "parse"): Method => [ + name, + 1, + (_, args) => { + if (args.length === 0) throw typeError(`URL.${name} requires a URL argument.`) + const input = urlArgument(args[0]) + const base = args[1] === undefined ? undefined : urlArgument(args[1]) + try { + const parsed = new URL(input, base) + return name === "canParse" ? true : new URLObj(proto, builtins.URLSearchParams, parsed) + } catch { + return name === "canParse" ? false : null + } + }, + ] + methods(builtins, url, [parse("canParse"), parse("parse")]) + + const self = (thisValue: Value, name: string) => receiver(URLObj, thisValue, `URL.prototype.${name}`) + for (const name of urlProperties) { + defineAccessor( + proto, + name, + (thisValue) => self(thisValue, name).url[name], + name === "origin" + ? undefined + : (thisValue, value) => { + const target = self(thisValue, name) + try { + target.url[name] = coerceToString(value) + } catch (error) { + if (error instanceof PendingThrow) throw error + throw typeError(`URL.${name} received an invalid value.`) + } + }, + ) + } + defineAccessor(proto, "searchParams", (thisValue) => self(thisValue, "searchParams").searchParams) + methods(builtins, proto, [ + ["toString", 0, (thisValue) => self(thisValue, "toString").url.href], + ["toJSON", 0, (thisValue) => self(thisValue, "toJSON").url.href], + ]) + return url +} + +/** What `readPairs` builds from `[name, value]` pairs, or from a record of names to values when `init` is no iterable. */ +export type PairsInto = { readonly kind: "Headers" | "URLSearchParams"; readonly proto: Obj } + +/** + * Reads a synchronous iterable of `[name, value]` pairs as strings (in the prelude), then builds `into` from them; a + * record of names to values when `init` is not iterable. As in WebIDL, the whole sequence is converted before any + * pair's length is checked. + */ +export const readPairs = (ctx: Interpreter, init: Value, label: string, into: PairsInto) => + new Invoke(ctx.prelude.fn("readPairs"), undefined, [init, label, into.kind, into.proto], undefined) + +/** What `readPairs` makes of the pairs it read (an array of arrays of strings), or of a record. */ +export const buildPairs = (into: PairsInto, value: Value, label: string): Value => { + if (!(value instanceof Arr)) return fromRecord(into, value) + const pairs = value.items.map((pair) => (pair as Arr).items as Array) + if (pairs.some((entry) => entry.length !== 2)) throw typeError(`${label} expects iterable [name, value] pairs.`) + if (into.kind === "URLSearchParams") { + return new URLSearchParamsObj(into.proto, new URLSearchParams(pairs as Array<[string, string]>)) + } + return new HeadersObj( + into.proto, + attempt(() => new Headers(pairs as Array<[string, string]>)), + ) +} + +const fromRecord = (into: PairsInto, init: Value): Value => { + if (into.kind === "Headers") { + if (!(init instanceof Obj) || isRuntimeReference(init)) { + throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.") + } + return new HeadersObj( + into.proto, + attempt(() => new Headers(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)])))), + ) + } + if (isRuntimeReference(init)) { + throw typeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.") + } + if (!(init instanceof Obj)) { + throw typeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.") + } + return new URLSearchParamsObj( + into.proto, + new URLSearchParams(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)]))), + ) +} + +/** The host validates header names and values and throws its own TypeError; the program gets one of its own. */ +export const attempt = (run: () => T): T => { + try { + return run() + } catch (error) { + throw typeError(error instanceof Error ? error.message : String(error)) + } +} + +/** Headers and URLSearchParams `forEach`: the callback sees each `[name, value]` pair as listed when it started. */ +export const pairsForEach = ( + ctx: Interpreter, + target: Value, + pairs: Array<[string, string]>, + callback: Callable, + thisArg: Value, +) => { + const list = new Arr( + ctx.builtins.Array, + pairs.map((pair) => new Arr(ctx.builtins.Array, pair)), + ) + return new Invoke(ctx.prelude.fn("pairsForEach"), undefined, [target, list, callback, thisArg], undefined) +} + +const constructURLSearchParams = (ctx: Interpreter, init: Value, proto: Obj) => { + const wrap = (params: URLSearchParams) => new URLSearchParamsObj(proto, params) + if (init === undefined) return wrap(new URLSearchParams()) + if (init instanceof URLSearchParamsObj) return wrap(new URLSearchParams(init.params)) + if (typeof init === "string") return wrap(new URLSearchParams(init)) + if (init === null || typeof init === "number" || typeof init === "boolean") { + return wrap(new URLSearchParams(coerceToString(init))) + } + return readPairs(ctx, init, "new URLSearchParams(...)", { kind: "URLSearchParams", proto }) +} + +export const urlSearchParamsGlobal = (ctx: Interpreter) => { + const builtins = ctx.builtins + const proto = builtins.URLSearchParams + const searchParams = constructor(builtins, proto, { + name: "URLSearchParams", + call: requiresNew("URLSearchParams"), + construct: (args, newTarget) => constructURLSearchParams(ctx, args[0], prototypeFrom(newTarget, proto)), + }) + const self = (thisValue: Value, name: string) => + receiver(URLSearchParamsObj, thisValue, `URLSearchParams.prototype.${name}`) + const wrap = (items: Array) => new Arr(builtins.Array, items) + const arg = (args: Array, index: number): string => coerceToString(args[index]) + const requireArgs = (name: string, args: Array, count: number): void => { + if (args.length < count) { + throw typeError(`URLSearchParams.${name} requires ${count} argument${count === 1 ? "" : "s"}.`) + } + } + defineAccessor(proto, "size", (thisValue) => self(thisValue, "size").params.size) + methods(builtins, proto, [ + [ + "append", + 2, + (thisValue, args) => { + requireArgs("append", args, 2) + self(thisValue, "append").params.append(arg(args, 0), arg(args, 1)) + return undefined + }, + ], + [ + "delete", + 1, + (thisValue, args) => { + requireArgs("delete", args, 1) + const params = self(thisValue, "delete").params + if (args[1] !== undefined) params.delete(arg(args, 0), arg(args, 1)) + else params.delete(arg(args, 0)) + return undefined + }, + ], + [ + "get", + 1, + (thisValue, args) => { + requireArgs("get", args, 1) + return self(thisValue, "get").params.get(arg(args, 0)) + }, + ], + [ + "getAll", + 1, + (thisValue, args) => { + requireArgs("getAll", args, 1) + return wrap(self(thisValue, "getAll").params.getAll(arg(args, 0))) + }, + ], + [ + "has", + 1, + (thisValue, args) => { + requireArgs("has", args, 1) + const params = self(thisValue, "has").params + return args[1] !== undefined ? params.has(arg(args, 0), arg(args, 1)) : params.has(arg(args, 0)) + }, + ], + [ + "set", + 2, + (thisValue, args) => { + requireArgs("set", args, 2) + self(thisValue, "set").params.set(arg(args, 0), arg(args, 1)) + return undefined + }, + ], + [ + "sort", + 0, + (thisValue) => { + self(thisValue, "sort").params.sort() + return undefined + }, + ], + ...iteratorMethods(builtins, self), + ["toString", 0, (thisValue) => self(thisValue, "toString").params.toString()], + [ + "forEach", + 1, + (thisValue, args) => { + requireArgs("forEach", args, 1) + const target = self(thisValue, "forEach") + const callback = requireCallback(args[0], "URLSearchParams.forEach") + return pairsForEach(ctx, target, Array.from(target.params.entries()), callback, args[1]) + }, + ], + ]) + define(proto, IteratorSymbol, get(proto, "entries"), hidden) + return searchParams +} diff --git a/packages/codemode/src/vm/stdlib/value.ts b/packages/codemode/src/vm/stdlib/value.ts new file mode 100644 index 00000000000..8fb96a45fc7 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/value.ts @@ -0,0 +1,33 @@ +import { fn } from "../runtime/native.js" +import { coerceToNumber, coerceToString, type Native, type Value } from "../runtime/objects.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { withPrimitives } from "../runtime/callback.js" + +export type Coercion = "Number" | "String" | "Boolean" | "parseInt" | "parseFloat" | "isFinite" | "isNaN" + +export const coerce = (ctx: Interpreter, name: Coercion, args: Array) => { + // Native: Number() is 0 and String() is "", unlike their undefined-argument forms; the + // other coercers match native through the undefined-argument path below. + if (args.length === 0) { + if (name === "Number") return 0 + if (name === "String") return "" + } + const raw = args[0] + if (name === "Boolean") return Boolean(raw) + if (name === "parseInt") { + return withPrimitives(["string", "number"], [raw, args[1]], ([text, radix]) => + parseInt(coerceToString(text), coerceToNumber(radix)), + ) + } + return withPrimitives(name === "String" || name === "parseFloat" ? "string" : "number", [raw], ([value]) => { + if (name === "Number") return coerceToNumber(value) + if (name === "isFinite") return Number.isFinite(coerceToNumber(value)) + if (name === "isNaN") return Number.isNaN(coerceToNumber(value)) + if (name === "parseFloat") return parseFloat(coerceToString(value)) + return coerceToString(value) + }) +} + +/** A global coercion function such as `Number` or `parseInt`. */ +export const coercion = (ctx: Interpreter, name: Coercion, length = 1): Native => + fn(ctx.builtins, name, length, (_, args) => coerce(ctx, name, args)) diff --git a/packages/codemode/src/vm/stdlib/web.ts b/packages/codemode/src/vm/stdlib/web.ts new file mode 100644 index 00000000000..4c18668d973 --- /dev/null +++ b/packages/codemode/src/vm/stdlib/web.ts @@ -0,0 +1,118 @@ +import { fn, methods } from "../runtime/native.js" +import { typeError } from "../runtime/model.js" +import { + define, + entries, + get, + getOwn, + hasOwn, + hidden, + isRuntimeReference, + Arr, + Bytes, + DateObj, + ErrorObj, + MapObj, + Obj, + RegExpObj, + SetObj, + WeakMapObj, + WeakSetObj, + coerceToString, + type Value, +} from "../runtime/objects.js" +import { createErrorValue, isErrorType } from "../runtime/intrinsics.js" +import { describeValue } from "../runtime/references.js" +import type { Interpreter } from "../runtime/interpreter.js" +import { ToolReference } from "../../tool-runtime.js" + +// WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies. Invalid input is a +// TypeError as well; browsers throw a DOMException named InvalidCharacterError, which CodeMode does not have. +export const base64Global = (ctx: Interpreter, name: "atob" | "btoa") => + fn(ctx.builtins, name, 1, (_, args) => { + if (args.length === 0) throw typeError(`${name} requires 1 argument (a string)`) + const input = coerceToString(args[0]) + try { + return name === "atob" ? atob(input) : btoa(input) + } catch { + throw typeError("The string contains invalid characters.") + } + }) + +// HTML structured clone over CodeMode's data kinds. Shared references survive through the memo; prototypes and +// frozen state do not, as in JS. Without a DOMException, DataCloneError is a TypeError carrying that prefix. +export const structuredCloneGlobal = (ctx: Interpreter) => + fn(ctx.builtins, "structuredClone", 1, (_, args) => { + if (args.length === 0) throw typeError("structuredClone requires 1 argument") + const builtins = ctx.builtins + const memo = new Map() + const clone = (value: Value): Value => { + if (typeof value === "symbol" || value instanceof ToolReference) { + throw typeError(`DataCloneError: ${describeValue(value)} could not be cloned.`) + } + if (!(value instanceof Obj)) return value + const seen = memo.get(value) + if (seen !== undefined) return seen + const remember = (copy: T): T => { + memo.set(value, copy) + return copy + } + if (value instanceof MapObj) { + const copy = remember(new MapObj(builtins.Map)) + for (const [key, item] of value.map) copy.map.set(clone(key), clone(item)) + return copy + } + if (value instanceof SetObj) { + const copy = remember(new SetObj(builtins.Set)) + for (const item of value.set.liveKeys()) copy.set.add(clone(item)) + return copy + } + if (value instanceof DateObj) return remember(new DateObj(builtins.Date, value.time)) + if (value instanceof RegExpObj) { + return remember(new RegExpObj(builtins.RegExp, value.source, value.flags)) + } + if (value instanceof Bytes) return remember(new Bytes(builtins.Uint8Array, new Uint8Array(value.bytes))) + if (value instanceof ErrorObj) { + const name = get(value, "name") + const message = get(value, "message") + const copy = remember( + createErrorValue( + builtins[typeof name === "string" && isErrorType(name) ? name : "Error"], + message === undefined ? undefined : coerceToString(message), + ), + ) + const stack = getOwn(value, "stack") + if (typeof stack === "string") define(copy, "stack", stack, hidden) + if (hasOwn(value, "cause")) define(copy, "cause", clone(getOwn(value, "cause")), hidden) + return copy + } + if (isRuntimeReference(value) || value instanceof WeakMapObj || value instanceof WeakSetObj) { + throw typeError(`DataCloneError: ${describeValue(value)} could not be cloned.`) + } + const copy = remember( + value instanceof Arr ? new Arr(builtins.Array, new Array(value.items.length)) : new Obj(builtins.Object), + ) + for (const [key, item] of entries(value)) define(copy, key, clone(item)) + return copy + } + return clone(args[0]) + }) + +export const cryptoGlobal = (ctx: Interpreter) => { + const object = new Obj(ctx.builtins.Object) + methods(ctx.builtins, object, [ + ["randomUUID", 0, () => crypto.randomUUID()], + [ + "getRandomValues", + 1, + (_, args) => { + if (!(args[0] instanceof Bytes)) { + throw typeError(`crypto.getRandomValues expects a Uint8Array, received ${describeValue(args[0])}.`) + } + crypto.getRandomValues(args[0].bytes) + return args[0] + }, + ], + ]) + return object +} diff --git a/packages/codemode/src/vm/vm.ts b/packages/codemode/src/vm/vm.ts new file mode 100644 index 00000000000..6f9e875dd0f --- /dev/null +++ b/packages/codemode/src/vm/vm.ts @@ -0,0 +1,3092 @@ +import { parse, type Expression, type MemberExpression, type NewExpression } from "acorn" +import type { Diagnostic } from "../codemode.js" +import { conversions } from "./runtime/callback.js" +import { locate, materialize } from "./runtime/errors.js" +import type { Interpreter } from "./runtime/interpreter.js" +import { checkArrayLength, checkStringLength } from "./runtime/limits.js" +import { + AsyncIteratorSymbol, + type AstNode, + invalidData, + IteratorSymbol, + PendingThrow, + rangeError, + referenceError, + syntaxError, + Throw, + typeError, +} from "./runtime/model.js" +import { + Arguments, + Arr, + assign, + BoundFn, + Builtin, + type Callable, + coerceToNumber, + coerceToString, + define, + Fn, + frozen, + GeneratorObj, + get, + getOwn, + hasOwn, + IteratorObj, + Native, + type NativeResult, + Obj, + Position, + PromiseObj, + readonly, + remove, + type Value, +} from "./runtime/objects.js" +import { + applyBinaryOperator, + assignToReference, + builtinCursor, + destructuredProperty, + forInKeys, + instanceofValue, + isThenable, + notAFunction, + notConstructible, + operatorConversion, + propertyKey, + readProperty, + readReference, + requireIteratorMethod, + requireIteratorObject, + requireMemberBase, + resolveProperty, + writableReference, +} from "./runtime/operations.js" +import { capability } from "./runtime/promises.js" +import { describeValue, isOpaque, typeofValue } from "./runtime/references.js" +import { + abrupt, + type Completion, + Convert, + type GeneratorRequest, + Interruption, + Invoke, + MAX_CALL_DEPTH, + normal, + Push, + Resume, +} from "./runtime/signals.js" +import { Source } from "./runtime/source.js" +import type { HostCall, Stop } from "./scheduler.js" +import { compiledPrelude } from "./prelude/prelude.js" +import { constructRegExp } from "./stdlib/regexp.js" +import { enumerableSource } from "./stdlib/object.js" +import { ToolReference } from "../tool-runtime.js" +import { registerAsyncIntrinsics, type SettlingTarget, VmScheduler } from "./async.js" +import type { Code } from "./code.js" +import { type CaptureOptions, captureVm, type RestoreOptions, restoreVm } from "./snapshot.js" +import { compileSource } from "./compiler/compile.js" +import { + AsyncFromSyncIterator, + completeGeneratorReturn, + execAwait, + execGeneratorStart, + execResume, + execYield, + execYstar, + execYstarResult, + handleAdapterPush, + registerGeneratorIntrinsics, + resumeVmGenerator, + unwindGenerator, +} from "./generators.js" +import { + BLOCK, + callExtension, + callSearch, + callTool, + ENTERED, + type HostResult, + HostCalls, + registerHostIntrinsics, + VISIT_BUDGET, +} from "./host.js" +import { + getInternalPrelude, + internalCode, + registerVmIntrinsics, + tryFastBoundaryOrJson, + VmIntrinsic, +} from "./internal-prelude.js" +import { + BinOpSymbols, + Cell, + CheckBaseKind, + CodeFlags, + CondKind, + type ConstValue, + EMPTY_CELLS, + type ErrorSite, + HOLE, + ImportKind, + type ImportSite, + IterContext, + NotYetImplemented, + Op, + PutKind, + type RegExpSite, + type TemplateSite, + UnaryOp, +} from "./opcodes.js" +import { hidden } from "./runtime/objects.js" + +const contiguous = (): Array => { + const a: Array = [""] + a.pop() + return a as Array +} + +const INITIAL_FUEL = 10_000 +/** + * What a call to a built-in costs in fuel: one may run far longer than an instruction (describing a rejection, copying + * a value), so a loop made only of built-in calls still checks the clock every few dozen calls. + */ +const NATIVE_FUEL = 50 +const SLICE_MS = 1 + +export const Mode = { + CALLER: 0, + ASYNC: 1, + GENERATOR: 2, + SETTLE: 3, + REJECT_ONLY: 4, + PROGRAM: 5, + BOUNDARY_OUT: 6, +} as const + +export type FrameMode = (typeof Mode)[keyof typeof Mode] + +/** Custom synchronous/async iterator state stored in a frame's hidden `%it` slot. */ +export class CustomIter { + done = false + rawGen = false + constructor( + readonly iterator: Obj, + readonly next: Value, + ) {} +} + +/** Single uniform activation record for the bytecode VM. */ +export class Frame { + fn: Fn | null = null + cells: ReadonlyArray = EMPTY_CELLS + code: Code + pc = 0 + sp = 0 + regs: Array + thisValue: Value = undefined + args: Array | undefined = undefined + mode: FrameMode = Mode.CALLER + target: unknown = undefined + answer: unknown = undefined + origin: AstNode | undefined = undefined + counted = false + + constructor(code: Code, regs: Array) { + this.code = code + this.regs = regs + } +} + +const EXEC_DONE = 0 +const EXEC_FUEL = 1 + +const allocRegs = (size: number): Array => { + const regs: Array = [] + for (let i = 0; i < size; i++) regs.push(HOLE) + return regs +} + +/** Internal closure wrapper that does not touch `builtins.Function` or `nativeRule.sealed`. */ +export class VmInternalFn { + constructor( + readonly code: Code, + readonly boundArgs?: ReadonlyArray, + ) {} +} + +/** + * Runs a runtime's program code: the job stack, the job queue, promises, and host calls. `execute.ts` and the driver + * use only its host surface: + * + * - **Starting.** Exactly one of `start(source)` (a new run) or `restore(saved, …)` (a saved one), once. Host call + * ids count up in program order. + * - **Running.** `run()` is synchronous, runs no host work, and returns a `Stop`: + * - `host(calls)`: start these queued calls now (they are `queued`; the driver marks them `running`), + * then call `run()` again; `host([])` means wait until `ready` (a blocking call or the inbox has input) and call + * `run()` again; + * - `slice`: the time slice is over; yield to the host scheduler and call `run()` again; + * - `done(completion)`: the program's body ended; host calls may still be running (`diagnostics()` reports + * unhandled rejections); + * - `stuck(node)`: nothing is queued or running, and the body awaits something that can never settle. + * - **Host calls.** `complete(id, completion)` reports how a handed-over call ended, at any time between `run()`s: a + * blocking call's outcome continues the job that made it; a concurrent call's waits in the inbox and settles its + * promise in a later job, in completion order. Unknown or completed ids are ignored. `call(id)`/`calls()` list the + * calls handed over and not completed, in the order they were made. + * - **Snapshots.** `capture` and `restore` run only between `run()`s, synchronously. A capture changes nothing a + * later `run()` sees; it fails (throws) only on a codec gap. A state holding live host values (`calls()` with an + * extension call, or `activeExtension`) can't be restored faithfully. + */ +export class Vm { + readonly frames: Array = contiguous() + private readonly pool: Array = contiguous() + readonly sourceCodes = new Map>() + readonly templates = new Map() + readonly intrinsics = new Map() + readonly scheduler: VmScheduler + /** Host calls: queued, running, completed, and the blocking one the job on the stack waits on. */ + readonly hosts: HostCalls + + depth = 0 + fuel = INITIAL_FUEL + sliceStart = 0 + sliced = false + outcome: Completion | undefined = undefined + /** Round-trip test mode (task C): slice after this many fuel ticks, so the run is saved and restored all through. */ + tripEvery = 0 + + constructor(readonly ctx: Interpreter) { + this.scheduler = new VmScheduler(this) + this.scheduler.intrinsicThen = get(ctx.builtins.Promise, "then") as Callable + this.hosts = new HostCalls(ctx, this.scheduler) + const internal = getInternalPrelude() + const prelude = compiledPrelude() + this.sourceCodes.set("@vm-internal", internal.codes) + this.sourceCodes.set("@prelude", prelude.codes) + for (const val of ctx.prelude.scope.values()) { + if (val instanceof Fn && val.cells.length === 0 && val.cells !== EMPTY_CELLS) { + ;(val as { cells: ReadonlyArray }).cells = EMPTY_CELLS + } + } + } + + private initIntrinsics(): void { + registerVmIntrinsics(this.intrinsics, this) + registerAsyncIntrinsics(this.intrinsics, this) + registerGeneratorIntrinsics(this.intrinsics, this) + registerHostIntrinsics(this.intrinsics, this) + } + + private getIntrinsic(name: string): unknown { + const p = this.ctx.prelude.scope.get(name) + if (p !== undefined) return p + if (this.intrinsics.size === 0) this.initIntrinsics() + return this.intrinsics.get(name) + } + + /** Queues the program's body as the root frame. */ + start(source: Source): void { + if (!warmed) warmupVm(this) + this.ctx.sources.set(source.id, source) + let codes: ReadonlyArray + try { + codes = compileSource(source) + } catch (error) { + // A program nested deeper than the compiler's host stack fails the run, as running it would. + if (!(error instanceof RangeError)) throw error + this.outcome = abrupt(error) + return + } + this.sourceCodes.set(source.id, codes) + const progCode = codes[0]! + const frame = this.acquire(progCode) + frame.fn = null + frame.cells = EMPTY_CELLS + frame.pc = 0 + frame.sp = progCode.locals + frame.thisValue = undefined + frame.args = undefined + frame.mode = Mode.PROGRAM + frame.origin = undefined + frame.counted = true + this.depth = 1 + this.frames.push(frame) + } + + /** + * Runs until it has to stop (`Stop`), in this order: hand over queued host calls; a due slice; continue the job a + * completed blocking call waits on; wait while the job on the stack is blocked; continue a sliced job before any + * other; the outcome; the next job; one inbox completion; wait for running calls; stuck. + */ + run(): Stop { + this.sliceStart = performance.now() + if (this.tripEvery > 0) this.fuel = this.tripEvery + const sched = this.scheduler + const hosts = this.hosts + while (true) { + if (hosts.queued.length > 0) return { kind: "host", calls: hosts.handOver() } + if (this.sliced) { + this.sliced = false + return { kind: "slice" } + } + const input = hosts.input + if (input !== undefined) { + hosts.input = undefined + this.resumeBlocked(input) + continue + } + if (this.frames.length > 0) { + if (hosts.blocked !== undefined) return { kind: "host", calls: [] } + if (this.fuel <= 0) this.fuel = this.tripEvery || INITIAL_FUEL + let signal = EXEC_DONE + try { + signal = this.exec() + } catch (error) { + if (error instanceof NotYetImplemented) throw error + this.unwind(error) + continue + } + if (signal === EXEC_FUEL) { + this.fuel = this.tripEvery || INITIAL_FUEL + if (this.tripEvery > 0 || performance.now() - this.sliceStart >= SLICE_MS) { + this.sliced = true + } + } + continue + } + if (this.outcome !== undefined) return { kind: "done", completion: this.outcome } + const job = sched.dequeue() + if (job !== undefined) { + sched.beginJob(job) + if (--this.fuel <= 0) { + this.fuel = this.tripEvery || INITIAL_FUEL + if (this.tripEvery > 0 || performance.now() - this.sliceStart >= SLICE_MS) { + this.sliced = true + } + } + continue + } + if (hosts.deliver()) continue + if (hosts.running.size > 0) return { kind: "host", calls: [] } + return { kind: "stuck", node: sched.stuckAwait() } + } + } + + // Continues the job stopped at a blocking call with the call's result, or throws its failure at the call. + private resumeBlocked(input: { readonly call: HostCall; readonly completion: Completion }): void { + const frame = this.frames[this.frames.length - 1]! + try { + const result = this.hosts.resume(input) + if (result === BLOCK || result === ENTERED) return + frame.regs[frame.sp++] = result + frame.pc++ + } catch (error) { + this.unwind(error) + } + } + + /** Reports how host work handed over by a `host` stop ended. */ + complete(id: number, completion: Completion): void { + this.hosts.complete(id, completion) + } + + /** Whether `run` has something to do: a blocking call's outcome, or one in the inbox while no job runs. */ + get ready(): boolean { + const hosts = this.hosts + return hosts.input !== undefined || (this.frames.length === 0 && (hosts.inbox.length > 0 || this.scheduler.hasJobs)) + } + + /** A host call handed over and not yet completed, by id. */ + call(id: number): HostCall | undefined { + return this.hosts.running.get(id) + } + + /** The host calls handed over and not yet completed, in the order they were made. */ + calls(): Array { + return [...this.hosts.running.values()] + } + + compact(): void { + this.scheduler.compact() + } + + /** Unhandled rejections, in promise creation order, once inbox completions are delivered. */ + diagnostics(): Array { + while (this.hosts.deliver()); + return this.scheduler.diagnostics() + } + + /** + * A synchronous extension call the program is in the middle of, from its `before` hook until its result is a + * program value or its concurrent call is running: it holds live host values. + */ + get activeExtension(): { readonly path: string } | undefined { + return this.hosts.extension + } + + /** Saves the run's state as it is now. */ + capture(options: CaptureOptions) { + if (this.intrinsics.size === 0) this.initIntrinsics() + return captureVm(this, options) + } + + /** Restores a saved state into this fresh VM, after checking it belongs to this build and runtime. */ + restore(saved: unknown, options: RestoreOptions) { + if (this.intrinsics.size === 0) this.initIntrinsics() + return restoreVm(this, saved, options) + } + + deliverAnswer(frame: Frame): void { + if (frame.answer !== undefined) { + const answer = frame.answer as Value + frame.answer = undefined + const caller = this.frames[this.frames.length - 1] + if (caller !== undefined) { + caller.regs[caller.sp++] = answer + caller.pc++ + } + } + } + + acquire(code: Code): Frame { + const size = code.locals + code.maxStack + const pool = this.pool + if (pool.length === 0) { + return new Frame(code, allocRegs(size)) + } + const f = pool.pop()! + f.code = code + const regs = f.regs + if (regs.length < size) { + f.regs = allocRegs(size) + } else { + for (let i = 0, n = code.locals; i < n; i++) regs[i] = HOLE + } + return f + } + + release(frame: Frame): void { + if (frame.counted) this.depth-- + frame.fn = null + frame.cells = EMPTY_CELLS + frame.thisValue = undefined + frame.args = undefined + frame.target = undefined + frame.answer = undefined + frame.origin = undefined + frame.counted = false + if (this.pool.length < 128) this.pool.push(frame) + } + + private codesOf(code: Code): ReadonlyArray { + return this.sourceCodes.get(code.source.id)! + } + + /** + * Nearest non-internal frame's AST site, or the bottom internal frame's `origin` (§3.6). A failure the host + * boundary's own walk raises (copying out the result, or a tool call's arguments) has no location: the call that + * crosses reports it. + */ + currentSite(): AstNode | undefined { + for (let i = this.frames.length - 1; i >= 0; i--) { + const f = this.frames[i]! + if ((f.code.flags & CodeFlags.INTERNAL) === 0) { + return f.code.siteAt(f.pc) + } + if (f.mode === Mode.BOUNDARY_OUT || f.mode === Mode.REJECT_ONLY) return undefined + if (f.origin !== undefined) return f.origin + } + return undefined + } + + private exec(): number { + const frames = this.frames + const hole = HOLE + const CellCtor = Cell + const ArrCtor = Arr + const ObjCtor = Obj + const FnCtor = Fn + const PositionCtor = Position + const globalScope = this.ctx.globalScope + const objectProto = this.ctx.builtins.Object + let frame!: Frame + let fcode!: Code + let code!: Int32Array + let regs!: Array + let consts!: ReadonlyArray + let cells!: ReadonlyArray + let pc = 0 + let sp = 0 + outer: for (;;) { + if (frames.length === 0) return 0 + frame = frames[frames.length - 1]! + fcode = frame.code + code = fcode.code + regs = frame.regs + consts = fcode.consts + pc = frame.pc | 0 + cells = frame.cells + sp = frame.sp | 0 + for (;;) { + const w = code[pc]! + switch (w & 0xff) { + case 0: // Op.PUSH_UNDEF + regs[sp++] = undefined + pc++ + continue + case 1: // Op.PUSH_NULL + regs[sp++] = null + pc++ + continue + case 2: // Op.PUSH_BOOL + regs[sp++] = (w & 0xff00) === 0x0100 + pc++ + continue + case 3: // Op.PUSH_INT + regs[sp++] = w >> 16 + pc++ + continue + case 4: // Op.PUSH_CONST + regs[sp++] = consts[w >>> 16] + pc++ + continue + case 5: // Op.POP + sp-- + pc++ + continue + case 6: // Op.POPN + sp -= w >>> 16 + pc++ + continue + case 7: // Op.DUP + regs[sp] = regs[sp - 1] + sp++ + pc++ + continue + case 8: // Op.PICK + regs[sp] = regs[sp - 1 - ((w >>> 8) & 0xff)] + sp++ + pc++ + continue + case 9: // Op.ROT + this.execRot(regs, (w >>> 8) & 0xff, sp) + pc++ + continue + case 10: { + // Op.GET_LOCAL + const slot = w >>> 16 + const v = regs[slot] + if (v === hole) this.failTdz(frame, fcode.localNames[slot]!, pc, sp) + regs[sp++] = v + pc++ + continue + } + case 11: { + // Op.PUT_LOCAL + const slot = w >>> 16 + const kind = (w >>> 8) & 0xff + const v = regs[--sp] + if (kind !== 1 && (regs[slot] === hole || kind === 2)) { + this.failPut(frame, fcode.localNames[slot]!, regs[slot], pc, sp) + } + regs[slot] = v + pc++ + continue + } + case 12: // Op.HOLE_LOCAL + regs[w >>> 16] = hole + pc++ + continue + case 13: { + // Op.NEW_CELL + const slot = w >>> 16 + regs[slot] = new CellCtor((w & 0xff00) === 0x0100 ? regs[slot] : hole) + pc++ + continue + } + case 14: { + // Op.COPY_CELL + const slot = w >>> 16 + regs[slot] = new CellCtor((regs[slot] as Cell).v) + pc++ + continue + } + case 15: { + // Op.GET_CELL + const slot = w >>> 16 + const v = (regs[slot] as Cell).v + if (v === hole) this.failTdz(frame, fcode.localNames[slot]!, pc, sp) + regs[sp++] = v + pc++ + continue + } + case 16: { + // Op.PUT_CELL + const slot = w >>> 16 + const kind = (w >>> 8) & 0xff + const v = regs[--sp] + const cell = regs[slot] as Cell + if (kind !== 1 && (cell.v === hole || kind === 2)) { + this.failPut(frame, fcode.localNames[slot]!, cell.v, pc, sp) + } + cell.v = v + pc++ + continue + } + case 17: { + // Op.GET_UPVAL + const idx = w >>> 16 + const v = cells[idx]!.v + if (v === hole) this.failTdz(frame, fcode.upvalNames[idx]!, pc, sp) + regs[sp++] = v + pc++ + continue + } + case 18: { + // Op.PUT_UPVAL + const idx = w >>> 16 + const kind = (w >>> 8) & 0xff + const v = regs[--sp] + const cell = cells[idx]! + if (kind !== 1 && (cell.v === hole || kind === 2)) { + this.failPut(frame, fcode.upvalNames[idx]!, cell.v, pc, sp) + } + cell.v = v + pc++ + continue + } + case 19: { + // Op.GET_GLOBAL + const name = consts[w >>> 16] as string + if ((w & 0xff00) === 0) { + const binding = globalScope.get(name) + if (binding !== undefined) { + regs[sp++] = binding.value + pc++ + continue + } + } + regs[sp++] = this.execGetGlobal(frame, (w >>> 8) & 0xff, name, pc, sp) + pc++ + continue + } + case 20: // Op.GET_INTRINSIC + regs[sp++] = this.getIntrinsic(consts[w >>> 16] as string) + pc++ + continue + case 21: // Op.ARGS + regs[sp++] = this.execArgs(frame, (w >>> 8) & 0xff, w >>> 16) + pc++ + continue + case 22: // Op.CLOSURE + regs[sp++] = this.makeClosure(fcode, w >>> 16, regs, cells) + pc++ + continue + case 23: // Op.SET_NAME + this.execSetName(regs, consts, (w >>> 8) & 0xff, w >>> 16, sp) + pc++ + continue + case 24: // Op.IMPORT + frame.pc = pc + frame.sp = sp + sp = this.execImport(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + pc++ + continue + case 25: { + // Op.GET_PROP + const o = regs[sp - 1] as Value + const key = consts[w >>> 16] as string + const mode = (w >>> 8) & 0xff + if (key === "length" && mode === 0) { + if (typeof o === "string") { + regs[sp - 1] = o.length + pc++ + continue + } + if (o instanceof ArrCtor) { + regs[sp - 1] = o.items.length + pc++ + continue + } + } + frame.pc = pc + frame.sp = sp + sp = this.execGetProp(frame, o, key, mode, pc, sp) + pc++ + continue + } + case 26: { + // Op.GET_ELEM + const keyVal = regs[sp - 1] as Value + const o = regs[sp - 2] as Value + const mode = (w >>> 8) & 0xff + if ( + mode === 0 && + typeof keyVal === "number" && + keyVal >= 0 && + (keyVal | 0) === keyVal && + o instanceof ArrCtor && + keyVal in o.items + ) { + sp-- + regs[sp - 1] = o.items[keyVal] + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execGetElem(frame, o, keyVal, mode, pc, sp)) continue outer + sp = frame.sp + pc++ + continue + } + case 27: { + // Op.PUT_PROP + const v = regs[--sp] as Value + const o = regs[sp - 1] as Value + frame.pc = pc + frame.sp = sp + this.execPutProp(fcode, o, consts[w >>> 16] as string, v, pc) + regs[sp - 1] = v + pc++ + continue + } + case 28: { + // Op.PUT_ELEM + const v = regs[--sp] as Value + const keyVal = regs[--sp] as Value + const o = regs[sp - 1] as Value + if ( + typeof keyVal === "number" && + keyVal >= 0 && + (keyVal | 0) === keyVal && + (typeof v !== "object" || v === null) && + o instanceof ArrCtor && + o.elements.writable && + o.extensible + ) { + const items = o.items + if (keyVal < items.length) { + if (keyVal === 0) items[0] = "" + items[keyVal] = v + regs[sp - 1] = v + pc++ + continue + } + if (keyVal === items.length && keyVal < 10_000_000) { + items.push(v) + regs[sp - 1] = v + pc++ + continue + } + } + frame.pc = pc + frame.sp = sp + this.execPutElem(fcode, o, keyVal, v, pc) + regs[sp - 1] = v + pc++ + continue + } + case 29: // Op.DELETE_PROP + frame.pc = pc + frame.sp = sp + regs[sp - 1] = this.execDeleteProp(fcode, regs[sp - 1] as Value, consts[w >>> 16] as string, pc) + pc++ + continue + case 30: { + // Op.DELETE_ELEM + const keyVal = regs[--sp] as Value + const o = regs[sp - 1] as Value + frame.pc = pc + frame.sp = sp + if (this.execDeleteElem(frame, o, keyVal, pc, sp)) continue outer + sp = frame.sp + pc++ + continue + } + case 31: // Op.CHECK_BASE + frame.pc = pc + frame.sp = sp + this.execCheckBase(fcode, (w >>> 8) & 0xff, w >>> 16, regs, sp, pc) + pc++ + continue + case 32: { + // Op.TO_KEY + const v = regs[sp - 1] as Value + if (typeof v === "string" || typeof v === "number" || typeof v === "symbol") { + pc++ + continue + } + if (this.execToKey(frame, v, pc, sp)) continue outer + pc++ + continue + } + case 33: { + // Op.IN + const rhs = regs[--sp] as Value + const lhs = regs[sp - 1] as Value + if (typeof lhs === "number" && lhs >= 0 && (lhs | 0) === lhs && rhs instanceof ArrCtor) { + regs[sp - 1] = lhs in rhs.items + pc++ + continue + } + if (this.execIn(frame, lhs, rhs, pc, sp)) continue outer + pc++ + continue + } + case 34: { + // Op.INSTANCEOF + const rhs = regs[--sp] as Value + frame.pc = pc + frame.sp = sp + regs[sp - 1] = this.execInstanceof(fcode, regs[sp - 1] as Value, rhs, pc) + pc++ + continue + } + case 35: // Op.NEW_OBJECT + regs[sp++] = new ObjCtor(objectProto) + pc++ + continue + case 36: // Op.NEW_ARRAY + sp = this.execNewArray(regs, w >>> 16, sp) + pc++ + continue + case 37: // Op.ARRAY_APPEND + frame.pc = pc + frame.sp = sp + sp = this.execArrayAppend(regs, (w >>> 8) & 0xff, sp) + pc++ + continue + case 38: // Op.DEFINE_FIELD + sp = this.execDefineField(regs, consts, (w >>> 8) & 0xff, w >>> 16, sp) + pc++ + continue + case 39: // Op.COPY_DATA_PROPS + frame.pc = pc + frame.sp = --sp + this.execCopyDataProps(fcode, regs[sp - 1] as Obj, regs[sp] as Value, pc) + pc++ + continue + case 40: // Op.COPY_REST + frame.pc = pc + frame.sp = sp + sp = this.execCopyRest(fcode, regs, w >>> 16, pc, sp) + pc++ + continue + case 41: // Op.TEMPLATE + regs[sp++] = this.getTemplateObject(fcode, consts[w >>> 16] as TemplateSite) + pc++ + continue + case 42: // Op.REGEXP + frame.pc = pc + frame.sp = sp + regs[sp++] = this.execRegExp(fcode, consts[w >>> 16] as RegExpSite, pc) + pc++ + continue + case 43: { + // Op.ADD + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a + b + pc++ + continue + } + if ( + (typeof a === "string" || typeof b === "string") && + (a === null || (typeof a !== "object" && typeof a !== "symbol")) && + (b === null || (typeof b !== "object" && typeof b !== "symbol")) + ) { + const s = (a as string) + (b as string) + if (s.length > 16_777_216) { + frame.pc = pc + frame.sp = sp + this.failStringLength(s.length) + } + regs[sp - 1] = s + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, "+", a, b, pc, sp)) continue outer + pc++ + continue + } + case 44: { + // Op.SUB + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a - b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, "-", a, b, pc, sp)) continue outer + pc++ + continue + } + case 45: { + // Op.MUL + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a * b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, "*", a, b, pc, sp)) continue outer + pc++ + continue + } + case 46: { + // Op.LT + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a < b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, "<", a, b, pc, sp)) continue outer + pc++ + continue + } + case 47: { + // Op.LE + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a <= b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, "<=", a, b, pc, sp)) continue outer + pc++ + continue + } + case 48: { + // Op.GT + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a > b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, ">", a, b, pc, sp)) continue outer + pc++ + continue + } + case 49: { + // Op.GE + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = a >= b + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinarySlow(frame, ">=", a, b, pc, sp)) continue outer + pc++ + continue + } + case 50: { + // Op.SEQ + const b = regs[--sp] + regs[sp - 1] = regs[sp - 1] === b + pc++ + continue + } + case 51: { + // Op.SNE + const b = regs[--sp] + regs[sp - 1] = regs[sp - 1] !== b + pc++ + continue + } + case 52: { + // Op.BINOP + const b = regs[--sp] as Value + const a = regs[sp - 1] as Value + const sub = (w >>> 8) & 0xff + if (typeof a === "number" && typeof b === "number") { + regs[sp - 1] = sub === 1 ? a % b : this.execBinopNum(sub, a, b) + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execBinopSlow(frame, sub, a, b, pc, sp)) continue outer + pc++ + continue + } + case 53: { + // Op.UNARY + const sub = (w >>> 8) & 0xff + const v = regs[sp - 1] as Value + if (typeof v === "number" && sub < 3) { + regs[sp - 1] = sub === 0 ? -v : sub === 1 ? +v : ~v + pc++ + continue + } + frame.pc = pc + frame.sp = sp + if (this.execUnarySlow(frame, sub, v, pc, sp)) continue outer + pc++ + continue + } + case 54: // Op.NOT + regs[sp - 1] = !regs[sp - 1] + pc++ + continue + case 55: { + // Op.TO_NUMERIC + const v = regs[sp - 1] as Value + if (typeof v === "number") { + pc++ + continue + } + if (this.execToNumericSlow(frame, (w >>> 8) & 0xff, v, pc, sp)) continue outer + pc++ + continue + } + case 56: { + // Op.INC + const v = regs[sp - 1] as Value + if (typeof v === "number") { + regs[sp - 1] = (w & 0xff00) === 0 ? v + 1 : v - 1 + pc++ + continue + } + if (this.execIncSlow(frame, (w >>> 8) & 0xff, v, pc, sp)) continue outer + pc++ + continue + } + case 57: { + // Op.TO_STRING + const v = regs[sp - 1] as Value + if (typeof v === "string") { + pc++ + continue + } + if (typeof v === "number") { + regs[sp - 1] = "" + v + pc++ + continue + } + if (this.execToStringSlow(frame, (w >>> 8) & 0xff, v, pc, sp)) continue outer + pc++ + continue + } + case 58: { + // Op.CONCAT + const n = w >>> 16 + if (n === 2) { + const b = regs[--sp] as string + const s = (regs[sp - 1] as string) + b + if (s.length > 16_777_216) { + frame.pc = pc + frame.sp = sp + this.failStringLength(s.length) + } + regs[sp - 1] = s + pc++ + continue + } + frame.pc = pc + frame.sp = sp + sp = this.execConcat(regs, n, sp) + pc++ + continue + } + case 59: // Op.JUMP + pc = w >>> 16 + continue + case 60: // Op.LOOP + pc = w >>> 16 + if (--this.fuel <= 0) { + frame.pc = pc + frame.sp = sp + return 1 + } + continue + case 61: // Op.JUMP_IF_FALSE + if (regs[--sp]) pc++ + else pc = w >>> 16 + continue + case 62: // Op.JUMP_IF_TRUE + if (regs[--sp]) pc = w >>> 16 + else pc++ + continue + case 63: { + // Op.JUMP_COND + const v = regs[sp - 1] + const sub = (w >>> 8) & 0xff + if (sub >= 4) { + if (v === null || v === undefined) { + if (sub === 5) sp-- + regs[sp - 1] = undefined + pc = w >>> 16 + } else { + pc++ + } + continue + } + const jump = sub === 0 ? !!v : sub === 1 ? !v : sub === 2 ? v !== null && v !== undefined : v !== undefined + if (jump) { + pc = w >>> 16 + } else { + sp-- + pc++ + } + continue + } + case 64: // Op.THROW + this.execThrow(frame, regs[--sp] as Value, pc, sp) + case 65: // Op.THROW_ERROR + this.execThrowError(frame, w >>> 16, pc, sp) + case 66: // Op.END_FINALLY + pc = this.execEndFinally(frame, w >>> 16, pc, sp) + continue + case 67: // Op.RETURN + case 87: { + // Op.END_PROGRAM + const v = regs[sp - 1] as Value + if (frame.mode === 0) { + frames.pop() + if (frame.counted) this.depth-- + if (this.pool.length < 128) this.pool.push(frame) + const caller = frames[frames.length - 1]! + caller.regs[caller.sp++] = v + caller.pc++ + continue outer + } + if (this.execReturnSlow(frame, v, pc, sp)) continue outer + return 0 + } + case 68: // Op.NOP + pc++ + continue + case 69: { + // Op.EXTRA + const nextPc = this.execExtra(frame, w, pc, sp) + if (nextPc < 0) { + if (nextPc === -1) continue outer + return -2 - nextPc + } + sp = frame.sp + pc = nextPc + continue + } + case 70: { + // Op.CALL + const n = (w >>> 8) & 0xff + const base = sp - n + const callee = regs[base - 1] + if (callee instanceof FnCtor) { + const c = callee.code + if ((c.flags & 518) === 0) { + const counted = (c.flags & 64) === 0 + if (counted && this.depth > 10_000) { + this.failCallDepth(frame, fcode, pc, sp) + } + if (counted) this.depth++ + const f = this.acquire(c) + f.fn = callee + f.cells = callee.cells + f.counted = counted + const r = f.regs + const p = c.params + if (p === 1) { + r[0] = n > 0 ? regs[base] : undefined + } else if (p === 2) { + r[0] = n > 0 ? regs[base] : undefined + r[1] = n > 1 ? regs[base + 1] : undefined + } else if (p > 2) { + this.copyParams(r, regs, base, n, p) + } + f.thisValue = regs[base - 2] as Value + f.origin = counted ? undefined : (fcode.flags & 64) === 0 ? fcode.siteAt(pc) : frame.origin + frame.pc = pc + frame.sp = base - 2 + f.pc = 0 + f.sp = c.locals + f.mode = 0 + frames.push(f) + if (--this.fuel <= 0) return 1 + continue outer + } + } + frame.pc = pc + frame.sp = sp + const called = this.execCallSlow(frame, n, pc, sp) + if (called === 1) { + if (this.fuel <= 0) return 1 + continue outer + } + if (called === 2) return 2 + sp = frame.sp + pc++ + frame.pc = pc + if (this.fuel <= 0) return 1 + continue + } + case 71: { + // Op.CALL_SPREAD + const called = this.execCallSpread(frame, pc, sp) + if (called === 1) { + if (this.fuel <= 0) return 1 + continue outer + } + if (called === 2) return 2 + sp = frame.sp + pc++ + frame.pc = pc + if (this.fuel <= 0) return 1 + continue + } + case 72: // Op.NEW + if (this.execNew(frame, (w >>> 8) & 0xff, pc, sp)) continue outer + sp = frame.sp + pc++ + continue + case 73: // Op.NEW_SPREAD + if (this.execNewSpread(frame, pc, sp)) continue outer + sp = frame.sp + pc++ + continue + case 74: // Op.ITER_OPEN + frame.pc = pc + frame.sp = sp + pc = this.execIterOpen(frame, (w >>> 8) & 0xff, pc, sp) + sp = frame.sp + continue + case 75: // Op.ITER_INIT + frame.pc = pc + frame.sp = sp + this.execIterInit(frame, (w >>> 8) & 0xff, pc, sp) + pc++ + continue + case 76: { + // Op.ITER_STEP + const rec = regs[(w >>> 8) & 0xff] + if (rec instanceof PositionCtor) { + const step = rec.step() + if (!step.done) { + regs[sp++] = step.value + pc += 2 + } else { + pc = w >>> 16 + } + continue + } + if (this.execIterStepSlow(frame, rec as CustomIter, w >>> 16, pc, sp)) continue outer + sp = frame.sp + pc = frame.pc + continue + } + case 77: // Op.ITER_RESULT + pc = this.execIterResult(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + sp = frame.sp + continue + case 78: // Op.ITER_CLOSE + pc = this.execIterClose(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + sp = frame.sp + continue + case 79: // Op.ITER_APPEND_ALL + pc = this.execIterAppendAll(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + continue + case 80: // Op.FOR_IN + frame.pc = pc + frame.sp = sp + regs[sp - 1] = this.execForIn(fcode, regs[sp - 1] as Value, pc) + pc++ + continue + case 81: // Op.GENERATOR_START + this.onGeneratorStart(frame, pc, sp) + continue outer + case 82: // Op.YIELD + if (this.onYield(frame, pc, sp)) continue outer + sp = frame.sp + pc = frame.pc + continue + case 83: // Op.RESUME + if (regs[sp - 1] === 0) { + sp-- + pc = w >>> 16 + continue + } + pc = this.onResume(frame, w >>> 16, pc, sp) + sp = frame.sp + continue + case 84: // Op.AWAIT + this.onAwait(frame, (w >>> 8) & 0xff, pc, sp) + continue outer + case 85: // Op.YSTAR + frame.pc = pc + frame.sp = sp + pc = this.onYstar(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + sp = frame.sp + continue + case 86: // Op.YSTAR_RESULT + frame.pc = pc + frame.sp = sp + pc = this.onYstarResult(frame, (w >>> 8) & 0xff, w >>> 16, pc, sp) + sp = frame.sp + continue + default: + this.failOpcode(w & 0xff) + } + } + } + } + + private copyParams(r: Array, regs: Array, base: number, n: number, p: number): void { + for (let i = 0; i < p; i++) r[i] = i < n ? regs[base + i] : undefined + } + + private failCallDepth(frame: Frame, fcode: Code, pc: number, sp: number): never { + frame.pc = pc + frame.sp = sp + throw rangeError("Maximum call stack size exceeded", fcode.siteAt(pc)) + } + + private failStringLength(len: number): void { + checkStringLength(len) + } + + private execInstanceof(fcode: Code, lhs: Value, rhs: Value, pc: number): boolean { + return instanceofValue(lhs, rhs, fcode.siteAt(pc)!) + } + + private execBinopSlow(frame: Frame, sub: number, a: Value, b: Value, pc: number, sp: number): boolean { + return this.execBinarySlow(frame, BinOpSymbols[sub]!, a, b, pc, sp) + } + + private execThrow(frame: Frame, val: Value, pc: number, sp: number): never { + frame.pc = pc + frame.sp = sp + throw new Throw(val) + } + + private execForIn(fcode: Code, val: Value, pc: number): Position { + return new Position(forInKeys(this.ctx, val, fcode.siteAt(pc)!), "values", this.ctx.builtins.Array) + } + + private onGeneratorStart(frame: Frame, pc: number, sp: number): void { + execGeneratorStart(this, frame, pc, sp) + } + + private onYield(frame: Frame, pc: number, sp: number): boolean { + return execYield(this, frame, pc, sp) + } + + private onResume(frame: Frame, bx: number, pc: number, sp: number): number { + return execResume(frame, bx, pc, sp) + } + + private onAwait(frame: Frame, flag: number, pc: number, sp: number): void { + execAwait(this, frame, flag, pc, sp) + } + + private onYstar(frame: Frame, slot: number, bx: number, pc: number, sp: number): number { + return execYstar(frame, slot, bx, pc, sp) + } + + private onYstarResult(frame: Frame, slot: number, bx: number, pc: number, sp: number): number { + return execYstarResult(frame, slot, bx, pc, sp) + } + + private failOpcode(op: number): never { + throw new NotYetImplemented(`opcode ${op}`) + } + + private execRot(regs: Array, n: number, sp: number): void { + const top = regs[sp - 1] + for (let i = sp - 1; i > sp - 1 - n; i--) regs[i] = regs[i - 1] + regs[sp - 1 - n] = top + } + + private failTdz(frame: Frame, name: string, pc: number, sp: number): never { + frame.pc = pc + frame.sp = sp + throw referenceError(`Cannot access '${name}' before initialization.`, frame.code.siteAt(pc)) + } + + private failPut(frame: Frame, name: string, current: unknown, pc: number, sp: number): never { + frame.pc = pc + frame.sp = sp + if (current === HOLE) { + throw referenceError(`Cannot access '${name}' before initialization.`, frame.code.siteAt(pc)) + } + throw typeError(`Cannot assign to constant '${name}'.`, frame.code.siteAt(pc)) + } + + private execGetGlobal(frame: Frame, mode: number, name: string, pc: number, sp: number): Value { + const fcode = frame.code + const binding = this.ctx.globalScope.get(name) + if (mode === 2) { + frame.pc = pc + frame.sp = sp - 1 + if (binding === undefined) throw referenceError(`Unknown identifier '${name}'.`, fcode.siteAt(pc)) + throw typeError(`Cannot assign to constant '${name}'.`, fcode.siteAt(pc)) + } + if (binding === undefined) { + if (mode === 1) return "undefined" + frame.pc = pc + frame.sp = sp + throw referenceError(`Unknown identifier '${name}'.`, fcode.siteAt(pc)) + } + return mode === 1 ? typeofValue(binding.value) : binding.value + } + + private execArgs(frame: Frame, kind: number, bx: number): unknown { + if (kind === 0) return new Arguments(this.ctx.builtins.Object, frame.args ?? []) + if (kind === 1) return new Arr(this.ctx.builtins.Array, (frame.args ?? []).slice(bx)) + if (kind === 2) return frame.fn + if (kind === 3) return frame.thisValue + return frame.args?.[bx] + } + + private execSetName( + regs: Array, + consts: ReadonlyArray, + mode: number, + bx: number, + sp: number, + ): void { + const fn = regs[sp - 1] + if (!(fn instanceof Fn)) return + if (mode === 0) { + define(fn, "name", consts[bx] as string, readonly) + return + } + const key = regs[sp - 2] + const name = + key === IteratorSymbol + ? "[Symbol.iterator]" + : key === AsyncIteratorSymbol + ? "[Symbol.asyncIterator]" + : String(key) + define(fn, "name", name, readonly) + } + + private execToKey(frame: Frame, v: Value, pc: number, sp: number): boolean { + if (!(v instanceof Obj) || isOpaque(v)) { + frame.regs[sp - 1] = coerceToString(v) + return false + } + frame.pc = pc + frame.sp = sp - 1 + this.pushInternalCall("$vmToKey", [v], frame.code.siteAt(pc) ?? frame.origin) + return true + } + + private execIn(frame: Frame, lhs: Value, rhs: Value, pc: number, sp: number): boolean { + frame.pc = pc + frame.sp = sp + return this.execBinarySlow(frame, "in", lhs, rhs, pc, sp) + } + + private execNewArray(regs: Array, n: number, sp: number): number { + if (n === 0) { + regs[sp] = new Arr(this.ctx.builtins.Array, contiguous()) + return sp + 1 + } + const items = regs.slice(sp - n, sp) as Array + sp -= n + regs[sp] = new Arr(this.ctx.builtins.Array, items) + return sp + 1 + } + + private execArrayAppend(regs: Array, isHole: number, sp: number): number { + if (isHole === 1) { + const arr = regs[sp - 1] as Arr + checkArrayLength(arr.items.length + 1) + arr.items.length += 1 + return sp + } + const v = regs[--sp] as Value + const arr = regs[sp - 1] as Arr + checkArrayLength(arr.items.length + 1) + arr.items.push(v) + return sp + } + + private execDefineField( + regs: Array, + consts: ReadonlyArray, + computed: number, + bx: number, + sp: number, + ): number { + if (computed === 0) { + const v = regs[--sp] as Value + define(regs[sp - 1] as Obj, consts[bx] as string, v) + return sp + } + const v = regs[--sp] as Value + const k = regs[--sp] as PropertyKey + define(regs[sp - 1] as Obj, k, v) + return sp + } + + private execCopyDataProps(fcode: Code, o: Obj, src: Value, pc: number): void { + if (src !== null && src !== undefined) { + assign(o, enumerableSource(this.ctx, "Object spread", src, fcode.siteAt(pc)!)) + } + } + + private execCopyRest(fcode: Code, regs: Array, n: number, pc: number, sp: number): number { + const consumed = new Set() + for (let i = sp - n; i < sp; i++) { + const k = regs[i] as PropertyKey + consumed.add(typeof k === "symbol" ? k : String(k)) + } + sp -= n + const src = regs[sp - 1] as Value + const rest = new Obj(this.ctx.builtins.Object) + assign(rest, enumerableSource(this.ctx, "Object destructuring", src, fcode.siteAt(pc)!), consumed) + regs[sp - 1] = rest + return sp + } + + private execRegExp(fcode: Code, site: RegExpSite, pc: number): Value { + try { + return constructRegExp(this.ctx.builtins, [site.pattern, site.flags]) + } catch (err) { + throw locate(err, fcode.siteAt(pc)) + } + } + + private execBinopNum(sub: number, a: number, b: number): Value { + switch (sub) { + case 0: + return a / b + case 1: + return a % b + case 2: + return a ** b + case 3: + return a & b + case 4: + return a | b + case 5: + return a ^ b + case 6: + return a << b + case 7: + return a >> b + case 8: + return a >>> b + case 9: + return a == b + default: + return a != b + } + } + + // `helper` converts an object in the prelude: `$vmToNumeric`, or `$vmInc` for `INC`. + private execToNumericSlow(frame: Frame, flag: number, v: Value, pc: number, sp: number, helper = "$vmToNumeric") { + const fcode = frame.code + frame.pc = pc + frame.sp = sp + if (v instanceof Obj && !isOpaque(v)) { + frame.sp = sp - 1 + this.pushInternalCall(helper, [flag, v], fcode.siteAt(pc) ?? frame.origin) + return true + } + if (isOpaque(v)) { + throw invalidData(`'${flag === 0 ? "++" : "--"}' requires a data value.`, fcode.siteAt(pc)) + } + frame.regs[sp - 1] = coerceToNumber(v) + return false + } + + private execIncSlow(frame: Frame, flag: number, v: Value, pc: number, sp: number): boolean { + if (this.execToNumericSlow(frame, flag, v, pc, sp, "$vmInc")) return true + const n = frame.regs[sp - 1] as number + frame.regs[sp - 1] = flag === 0 ? n + 1 : n - 1 + return false + } + + private execToStringSlow(frame: Frame, flag: number, v: Value, pc: number, sp: number): boolean { + if (flag === 1 || !(v instanceof Obj) || isOpaque(v)) { + frame.regs[sp - 1] = coerceToString(v) + return false + } + frame.pc = pc + frame.sp = sp - 1 + this.pushInternalCall("$vmToString", [v], frame.code.siteAt(pc) ?? frame.origin) + return true + } + + private execConcat(regs: Array, n: number, sp: number): number { + let out = "" + for (let i = sp - n; i < sp; i++) { + out += regs[i] as string + if (out.length > 16_777_216) checkStringLength(out.length) + } + sp -= n + regs[sp] = out + return sp + 1 + } + + private execThrowError(frame: Frame, bx: number, pc: number, sp: number): never { + frame.pc = pc + frame.sp = sp + const site = frame.code.consts[bx] as ErrorSite + throw new PendingThrow(site.type, site.message, frame.code.siteAt(pc), site.kind, site.suggestions) + } + + private execEndFinally(frame: Frame, level: number, pc: number, sp: number): number { + const fcode = frame.code + const offset = fcode.finallyOffsets[level]! + const fin = fcode.finallies + const tag = frame.regs[fin[offset]!] as number + if (tag === 1) { + frame.pc = pc + frame.sp = sp + throw new Throw(frame.regs[fin[offset + 1]!] as Value) + } + return fin[offset + 4 + tag]! + } + + private execReturnSlow(frame: Frame, v: Value, pc: number, sp: number): boolean { + const frames = this.frames + if (frame.mode === Mode.ASYNC || frame.mode === Mode.SETTLE) { + frames.pop() + const target = frame.target as SettlingTarget + this.deliverAnswer(frame) + this.release(frame) + target.resolve(v) + return true + } + if (frame.mode === Mode.GENERATOR) { + completeGeneratorReturn(this, frame, v) + return true + } + if (frame.mode === Mode.REJECT_ONLY) { + frames.pop() + this.deliverAnswer(frame) + this.release(frame) + return true + } + if (frame.mode === Mode.BOUNDARY_OUT) { + frames.pop() + this.release(frame) + this.outcome = normal(v) + return false + } + if (frame.mode === Mode.PROGRAM) { + frame.pc = pc + frame.sp = sp + return this.finishProgram(frame, v) + } + frames.pop() + this.release(frame) + return true + } + + private execCallSpread(frame: Frame, pc: number, sp: number): number { + const regs = frame.regs + const argsArr = regs[--sp] as Arr + frame.pc = pc + const callee = regs[sp - 1] as Value + const thisVal = regs[sp - 2] as Value + frame.sp = sp - 2 + return this.invokeCallable(frame, callee, thisVal, [...argsArr.items], callSiteOf(frame, pc), pc) + } + + private execNew(frame: Frame, n: number, pc: number, sp: number): boolean { + const fcode = frame.code + const regs = frame.regs + if (n === 255) { + const callee = regs[sp - 1] as Value + if (!(callee instanceof Builtin) || callee.construct === undefined) { + frame.pc = pc + frame.sp = sp + throw notConstructible(callee, fcode.siteAt(pc) as NewExpression) + } + frame.sp = sp + return false + } + frame.pc = pc + const base = sp - n + const callee = regs[base - 1] as Builtin + const args = regs.slice(base, sp) as Array + frame.sp = base - 1 + return this.execConstruct(frame, callee, args, callSiteOf(frame, pc)) + } + + private execNewSpread(frame: Frame, pc: number, sp: number): boolean { + const regs = frame.regs + const argsArr = regs[--sp] as Arr + const callee = regs[--sp] as Builtin + frame.pc = pc + frame.sp = sp + return this.execConstruct(frame, callee, [...argsArr.items], callSiteOf(frame, pc)) + } + + private execIterStepSlow(frame: Frame, custom: CustomIter, bx: number, pc: number, sp: number): boolean { + const regs = frame.regs + custom.done = true + if (custom.rawGen) { + const gen = custom.iterator as GeneratorObj + if (gen.state === "completed") { + frame.sp = sp + frame.pc = bx + return false + } + if (gen.state === "suspendedStart" || gen.state === "suspendedYield") { + gen.state = "executing" + frame.pc = pc + frame.sp = sp + resumeVmGenerator(this, gen, NEXT) + return true + } + } + regs[sp++] = custom.iterator + regs[sp++] = custom.next + frame.sp = sp + frame.pc = pc + 1 + return false + } + + private execIterResult(frame: Frame, slot: number, bx: number, pc: number, sp: number): number { + const regs = frame.regs + const res = regs[--sp] as Value + frame.pc = pc + frame.sp = sp + if (!(res instanceof Obj)) { + throw typeError("Iterator next() result must be an object.", frame.code.siteAt(pc)) + } + const done = Boolean(this.readObjProp(res, "done", frame.code, pc)) + if (done) return bx + ;(regs[slot] as CustomIter).done = false + regs[sp] = this.readObjProp(res, "value", frame.code, pc) + frame.sp = sp + 1 + return pc + 1 + } + + private execIterClose(frame: Frame, slot: number, bx: number, pc: number, sp: number): number { + const regs = frame.regs + const rec = regs[slot] + if (!(rec instanceof CustomIter) || rec.done) { + frame.sp = sp + return bx + } + frame.pc = pc + frame.sp = sp + rec.done = true + const ret = this.readObjProp(rec.iterator, "return", frame.code, pc) + if (ret === undefined || ret === null) return bx + const method = requireIteratorMethod(ret, "Iterator return", frame.code.siteAt(pc)) + regs[sp++] = rec.iterator + regs[sp++] = method + frame.sp = sp + return pc + 1 + } + + private execIterAppendAll(frame: Frame, slot: number, bx: number, pc: number, sp: number): number { + const rec = frame.regs[slot] + if (!(rec instanceof Position)) return bx + const arr = frame.regs[sp - 1] as Arr + frame.pc = pc + frame.sp = sp + for (let step = rec.step(); !step.done; step = rec.step()) { + checkArrayLength(arr.items.length + 1) + arr.items.push(step.value) + } + return pc + 1 + } + + private readObjProp(target: Obj, key: string | symbol, fcode: Code, pc: number): Value { + for (let cur: Obj | null = target; cur !== null; cur = cur.proto) { + if (cur.props.size === 0) continue + const slot = cur.props.get(key) + if (slot !== undefined) { + if ("value" in slot) return slot.value + if (slot.get === undefined) return undefined + try { + return slot.get(target) + } catch (err) { + throw locate(err, fcode.siteAt(pc)) + } + } + } + return undefined + } + + unwind(rawError: unknown): boolean { + // An interruption skips every handler, but still completes each frame by its mode: a frame that settles a + // promise rejects it, so whatever awaits that promise is interrupted in turn. + const interrupted = rawError instanceof Interruption + const error = interrupted ? rawError : locate(rawError, this.currentSite()) + + if (this.frames.length === 0) { + this.outcome = abrupt(error) + return false + } + + while (this.frames.length > 0) { + const frame = this.frames[this.frames.length - 1]! + const fcode = frame.code + const handlers = fcode.handlers + const faultPc = frame.pc + for (let i = 0; !interrupted && i < handlers.length; i += 4) { + const start = handlers[i]! + const end = handlers[i + 1]! + if (faultPc >= start && faultPc < end) { + const target = handlers[i + 2]! + const depth = handlers[i + 3]! + const sp = fcode.locals + depth + frame.regs[sp] = materialize(this.ctx, error) + frame.sp = sp + 1 + frame.pc = target + return true + } + } + + this.frames.pop() + const mode = frame.mode + if (mode === Mode.CALLER) { + this.release(frame) + continue + } + if (mode === Mode.GENERATOR) { + if (unwindGenerator(this, frame, error)) { + return this.frames.length > 0 + } + continue + } + if (mode === Mode.ASYNC || mode === Mode.SETTLE || mode === Mode.REJECT_ONLY) { + const target = frame.target as SettlingTarget + this.deliverAnswer(frame) + this.release(frame) + target.reject(error) + return this.frames.length > 0 + } + this.release(frame) + if (mode === Mode.PROGRAM || mode === Mode.BOUNDARY_OUT) { + this.outcome = abrupt(error) + return false + } + } + return false + } + + private finishProgram(frame: Frame, value: Value): boolean { + if (!isThenable(value)) { + let fast: ReturnType + try { + fast = tryFastBoundaryOrJson(this.ctx.builtins, value, true, VISIT_BUDGET) + } catch (error) { + this.frames.pop() + this.release(frame) + this.outcome = abrupt(error) + return false + } + if (fast.ok) { + this.frames.pop() + this.release(frame) + this.outcome = normal(fast.json) + return false + } + } + this.frames.pop() + this.release(frame) + this.pushInternal( + internalCode("$vmCross"), + [new Arr(this.ctx.builtins.Array, [value])], + undefined, + Mode.BOUNDARY_OUT, + ) + return true + } + + private makeClosure(parentCode: Code, fnIndex: number, regs: Array, cells: ReadonlyArray): Fn { + const childCode = this.codesOf(parentCode)[fnIndex]! + const caps = childCode.captures + let childCells: ReadonlyArray + if (caps.length === 0) { + childCells = EMPTY_CELLS + } else { + const arr = contiguous() + for (let i = 0; i < caps.length; i += 2) { + arr.push(caps[i] === 0 ? (regs[caps[i + 1]!] as Cell) : cells[caps[i + 1]!]!) + } + childCells = arr + } + const fn = new Fn(this.ctx.builtins.Function, childCode, childCells) + if ((childCode.flags & CodeFlags.GENERATOR) !== 0) { + const isAsync = (childCode.flags & CodeFlags.ASYNC) !== 0 + define(fn, "prototype", new Obj(isAsync ? this.ctx.builtins.AsyncGenerator : this.ctx.builtins.Generator), hidden) + } + return fn + } + + private getTemplateObject(fcode: Code, site: TemplateSite): Arr { + const key = `${fcode.source.id}:${site.nodeIndex}` + const cached = this.templates.get(key) + if (cached !== undefined) return cached + const arrayProto = this.ctx.builtins.Array + const strings = new Arr(arrayProto, [...site.cooked]) + define(strings, "raw", new Arr(arrayProto, [...site.raw]), frozen) + this.templates.set(key, strings) + return strings + } + + private execImport(frame: Frame, kind: number, k: number, pc: number, sp: number): number { + const fcode = frame.code + const siteNode = fcode.siteAt(pc) + if (kind === ImportKind.DYNAMIC) { + const specifier = frame.regs[sp - 1] as string + const promise = capability(this.ctx, siteNode) + try { + promise.resolve(this.ctx.namespace(specifier, siteNode)) + } catch (err) { + if (!(err instanceof PendingThrow)) throw err + promise.reject(err) + } + frame.regs[sp - 1] = promise.promise + return sp + } + const site = fcode.consts[k] as ImportSite + if (kind === ImportKind.SIDE_EFFECT) { + this.ctx.module(site.specifier, siteNode) + return sp + } + if (kind === ImportKind.DEFAULT) { + frame.regs[sp] = this.ctx.module(site.specifier, siteNode) + return sp + 1 + } + const ns = this.ctx.namespace(site.specifier, siteNode) + if (kind === ImportKind.NAMESPACE) { + frame.regs[sp] = ns + return sp + 1 + } + const imported = site.imported! + if (!hasOwn(ns, imported)) { + throw syntaxError(`"${site.specifier}" has no export named "${imported}".`, siteNode) + } + frame.regs[sp] = getOwn(ns, imported) + return sp + 1 + } + + private execGetProp(frame: Frame, o: Value, key: string, mode: number, pc: number, sp: number): number { + const regs = frame.regs + if (mode !== 2 && o instanceof Obj && !(o instanceof PromiseObj)) { + const v = key === "length" && o instanceof Arr ? o.items.length : this.readObjProp(o, key, frame.code, pc) + return writeRead(regs, sp - 1, mode, o, v) + } + if (mode !== 2 && typeof o === "string") { + const v = key === "length" ? o.length : this.readObjProp(this.ctx.builtins.String, key, frame.code, pc) + return writeRead(regs, sp - 1, mode, o, v) + } + if (o instanceof ToolReference) return writeRead(regs, sp - 1, mode, undefined, new ToolReference([...o.path, key])) + const site = frame.code.siteAt(pc)! + if (mode === 2) { + regs[sp - 1] = destructuredProperty(this.ctx.builtins, o, key, site) + return sp + } + return this.readMember(regs, sp - 1, mode, o, key, site) + } + + private execGetElem(frame: Frame, o: Value, keyVal: Value, mode: number, pc: number, sp: number): boolean { + if (mode === 0) { + if (typeof o === "string" && typeof keyVal === "number" && keyVal >= 0 && (keyVal | 0) === keyVal) { + frame.regs[sp - 2] = o[keyVal] + frame.sp = sp - 1 + return false + } + if (o instanceof Obj && !(o instanceof PromiseObj)) { + if (typeof keyVal === "number" && !(o instanceof Arr)) { + if ( + o.constructor === Obj && + (o.props.size === 0 || (o.props.size === 1 && o.props.has("length"))) && + o.proto === this.ctx.builtins.Object && + o.proto.intrinsic > 0 + ) { + frame.regs[sp - 2] = undefined + frame.sp = sp - 1 + return false + } + } + if (typeof keyVal === "string" && o.constructor === Obj) { + frame.regs[sp - 2] = this.readObjProp(o, keyVal, frame.code, pc) + frame.sp = sp - 1 + return false + } + } + } + const site = frame.code.siteAt(pc)! + const memberNode = site as MemberExpression + if (mode !== 2) requireMemberBase(memberNode, o, keyVal) + if (keyVal instanceof Obj && !isOpaque(keyVal)) { + frame.sp = sp - 2 + this.pushInternalCall("$vmGetElem", [o, keyVal], memberNode.property ?? site) + return true + } + const key = propertyKey(keyVal) + if (mode === 2) { + frame.regs[sp - 2] = destructuredProperty(this.ctx.builtins, o, key, site) + frame.sp = sp - 1 + return false + } + frame.sp = this.readMember(frame.regs, sp - 2, mode, o, key, site) + return false + } + + // Reads `o[key]` for the member expression at `site` into `regs` at `at`; see `writeRead`. + private readMember( + regs: Array, + at: number, + mode: number, + o: Value, + key: PropertyKey, + site: AstNode, + ): number { + const ref = this.resolveMember(o, key, site) + if (ref instanceof ToolReference) return writeRead(regs, at, mode, undefined, ref) + if ("value" in ref) return writeRead(regs, at, mode, undefined, ref.value) + return writeRead(regs, at, mode, ref.receiver, readReference(ref, site as MemberExpression)) + } + + // The reference `o[key]` that the member expression at `site` names. + private resolveMember(o: Value, key: PropertyKey, site: AstNode) { + const member = site as MemberExpression + return resolveProperty(this.ctx.builtins, o, key, member.object ?? site, member.property ?? site) + } + + private execPutProp(fcode: Code, o: Value, key: PropertyKey, v: Value, pc: number): void { + if (!(o instanceof Obj)) { + const site = fcode.siteAt(pc)! + const ref = resolveProperty(this.ctx.builtins, o, key, site, site) + writableReference(ref, site as MemberExpression) + return + } + try { + assignToReference(o, key, v, undefined!) + } catch (err) { + throw locate(err, fcode.siteAt(pc)) + } + } + + private execPutElem(fcode: Code, o: Value, keyVal: Value, v: Value, pc: number): void { + this.execPutProp(fcode, o, propertyKey(keyVal), v, pc) + } + + private execDeleteProp(fcode: Code, o: Value, key: PropertyKey, pc: number): boolean { + const site = fcode.siteAt(pc)! + const ref = this.resolveMember(o, key, site) + if (ref instanceof ToolReference || "value" in ref || ref.receiver !== ref.target) { + throw invalidData("Only data fields may be deleted.", site) + } + if (remove(ref.target, ref.key)) return true + throw typeError(`Cannot delete property '${String(ref.key)}'.`, site) + } + + private execDeleteElem(frame: Frame, o: Value, keyVal: Value, pc: number, sp: number): boolean { + const site = frame.code.siteAt(pc)! + requireMemberBase(site as MemberExpression, o, keyVal) + if (keyVal instanceof Obj && !isOpaque(keyVal)) { + frame.sp = sp - 1 + this.pushInternalCall("$vmDeleteElem", [o, keyVal], site) + return true + } + frame.regs[sp - 1] = this.execDeleteProp(frame.code, o, propertyKey(keyVal), pc) + frame.sp = sp + return false + } + + private execCheckBase(fcode: Code, kind: number, bx: number, regs: Array, sp: number, pc: number): void { + const base = regs[kind === CheckBaseKind.READ || kind === CheckBaseKind.WRITE_ELEM ? sp - 2 : sp - 1] as Value + if (base instanceof Obj && !(base instanceof PromiseObj)) return + const site = fcode.siteAt(pc)! + if (kind === CheckBaseKind.DESTRUCTURE) { + if (base === null || base === undefined) { + throw typeError(`Cannot destructure ${describeValue(base)} as it is ${base}.`, site) + } + return + } + const memberNode = site as MemberExpression + if (kind === CheckBaseKind.READ) { + requireMemberBase(memberNode, base, regs[sp - 1] as Value) + return + } + const key = kind === CheckBaseKind.WRITE_PROP ? (fcode.consts[bx] as string) : propertyKey(regs[sp - 1] as Value) + readReference(writableReference(this.resolveMember(base, key, site), memberNode), memberNode) + } + + private execBinarySlow(frame: Frame, op: string, a: Value, b: Value, pc: number, sp: number): boolean { + const fcode = frame.code + const site = fcode.siteAt(pc) ?? frame.origin + if (a instanceof Obj || b instanceof Obj) { + const conv = operatorConversion(op, a, b) + if (conv !== undefined) { + frame.sp = sp - 1 + this.pushInternalCall("$vmBinary", [op, a, b], site) + return true + } + } + frame.regs[sp - 1] = applyBinaryOperator(op, a, b, site!) + return false + } + + private execUnarySlow(frame: Frame, sub: number, v: Value, pc: number, sp: number): boolean { + if (sub === 3) { + frame.regs[sp - 1] = typeofValue(v) + return false + } + if (sub === 4) { + frame.regs[sp - 1] = undefined + return false + } + const fcode = frame.code + const site = fcode.siteAt(pc) ?? frame.origin + if (v instanceof Obj && !isOpaque(v)) { + frame.sp = sp - 1 + this.pushInternalCall("$vmUnary", [sub, v], site) + return true + } + if (isOpaque(v)) throw invalidData("Unary operators require data values.", site) + const n = coerceToNumber(v) + frame.regs[sp - 1] = sub === UnaryOp.NEG ? -n : sub === UnaryOp.PLUS ? +n : ~n + return false + } + + private execCallSlow(frame: Frame, n: number, pc: number, sp: number): number { + const regs = frame.regs + const base = sp - n + const callee = regs[base - 1] as Value + const thisVal = regs[base - 2] as Value + if (callee instanceof Native) { + conversions.count = 0 + const args = + n === 0 + ? [] + : n === 1 + ? [regs[base] as Value] + : n === 2 + ? [regs[base] as Value, regs[base + 1] as Value] + : (regs.slice(base, sp) as Array) + frame.pc = pc + frame.sp = base - 2 + const site = callee.callback && callee.call.length < 3 ? undefined : callSiteOf(frame, pc) + const res = callee.call(thisVal, args, site) + if (this.tripEvery === 0) this.fuel -= NATIVE_FUEL + if (res === null || typeof res !== "object" || res instanceof Obj) { + normalizeItems(res) + regs[base - 2] = res + frame.sp = base - 1 + return 0 + } + return this.settleNativeResult(frame, res, callee, thisVal, args, false, site ?? callSiteOf(frame, pc), pc) + } + const args = regs.slice(base, sp) as Array + frame.sp = base - 2 + return this.invokeCallable(frame, callee, thisVal, args, callSiteOf(frame, pc), pc) + } + + /** + * Dispatches a call to `callee`. Returns `0` if synchronous value pushed, `1` if a frame was entered, `2` if the job + * blocks on a host call. + */ + private invokeCallable( + callerFrame: Frame, + callee: Value | VmIntrinsic | VmInternalFn, + thisVal: Value, + args: Array, + site: AstNode | undefined, + pc: number, + ): number { + while (callee instanceof BoundFn) { + thisVal = callee.boundThis + args = [...callee.boundArgs, ...args] + callee = callee.target + } + + if (callee instanceof VmIntrinsic) { + let res: NativeResult + try { + res = callee.call(args) + } catch (err) { + throw locate(err, site ?? this.currentSite()) + } + if (res instanceof Convert) { + callerFrame.regs[callerFrame.sp++] = res as unknown as Value + return 0 + } + return this.settleNativeResult(callerFrame, res, undefined, thisVal, args, false, site, pc) + } + + if (callee instanceof VmInternalFn) { + const callArgs = callee.boundArgs !== undefined ? [...callee.boundArgs, ...args] : args + const f = this.pushInternal(callee.code, callArgs, site ?? callerFrame.origin) + if ((callee.code.flags & CodeFlags.NEEDS_ARGS) !== 0) f.args = callArgs + f.thisValue = thisVal + this.fuel-- + return 1 + } + + if (callee instanceof Fn) { + const code = callee.code + const counted = (code.flags & CodeFlags.INTERNAL) === 0 + if (counted && this.depth > MAX_CALL_DEPTH) { + throw rangeError("Maximum call stack size exceeded", site ?? this.currentSite()) + } + const isGenerator = (code.flags & CodeFlags.GENERATOR) !== 0 + const isAsync = (code.flags & CodeFlags.ASYNC) !== 0 + const origin = site ?? callerFrame.origin + if (isAsync && !isGenerator) { + const resolving = this.scheduler.resolving(this.scheduler.newPromise(), site ?? this.currentSite()) + this.pushFn(callee, thisVal, args, origin, Mode.ASYNC, resolving, resolving.promise) + } else this.pushFn(callee, thisVal, args, origin, Mode.CALLER) + this.fuel-- + return 1 + } + + if (callee instanceof Builtin) { + conversions.count = 0 + let res: NativeResult + try { + res = callee.invoke(thisVal, args, site ?? this.currentSite()) + } catch (err) { + throw locate(err, site ?? this.currentSite()) + } + if (this.tripEvery === 0) this.fuel -= NATIVE_FUEL + return this.settleNativeResult(callerFrame, res, callee, thisVal, args, false, site, pc) + } + + if (callee instanceof ToolReference) { + if (callee.path.length === 0) { + const callNode = site as { readonly callee?: Expression } | undefined + throw typeError("The tools root is not callable.", callNode?.callee ?? site) + } + return this.hostResult(callerFrame, callTool(this, this.hosts, callee.path, args, site ?? this.currentSite())) + } + + const callNode = site as { readonly callee?: Expression } | undefined + throw notAFunction(callNode?.callee, site ?? this.currentSite()) + } + + private execConstruct(callerFrame: Frame, ctor: Builtin, args: Array, site: AstNode | undefined): boolean { + conversions.count = 0 + let res: NativeResult + try { + res = ctor.construct!(args, ctor, site ?? this.currentSite()) + } catch (err) { + throw locate(err, site ?? this.currentSite()) + } + return this.settleNativeResult(callerFrame, res, ctor, ctor, args, true, site, callerFrame.pc) === 1 + } + + private settleNativeResult( + callerFrame: Frame, + res: NativeResult, + callable: Builtin | undefined, + thisOrNewTarget: Value, + args: Array, + constructing: boolean, + site: AstNode | undefined, + pc: number, + ): number { + if (res instanceof Invoke) { + normalizeItems(res.thisValue) + for (let i = 0; i < res.args.length; i++) normalizeItems(res.args[i]) + return this.invokeCallable(callerFrame, res.callable, res.thisValue, res.args, res.site ?? site, pc) + } + if (res instanceof Resume) { + resumeVmGenerator(this, res.generator, res.request) + return 1 + } + if ((res as unknown) === BLOCK) return 2 + if (res instanceof Convert) { + const builtins = this.ctx.builtins + this.pushInternalCall( + "$vmConvert", + [ + callable!, + thisOrNewTarget, + new Arr(builtins.Array, args), + constructing, + new Arr(builtins.Array, []), + res as unknown as Value, + ], + site ?? callerFrame.origin, + ) + return 1 + } + if (res instanceof Push) { + const kindName = res.kind + const slots = res.slots + const builtins = this.ctx.builtins + if (kindName === "JSON.stringify") { + const [value, replacer, properties, indent] = slots as [ + Value, + Callable | undefined, + Array | undefined, + string | number | undefined, + ] + if (replacer === undefined && properties === undefined) { + const fast = tryFastBoundaryOrJson(builtins, value, false, VISIT_BUDGET) + if (fast.ok) { + const text = JSON.stringify(fast.json, null, indent) + if (text !== undefined) checkStringLength(text.length) + callerFrame.regs[callerFrame.sp++] = text + return 0 + } + } + this.pushInternalCall( + "$vmJsonWalk", + [replacer, value, properties !== undefined ? new Arr(builtins.Array, properties) : null, indent], + site ?? callerFrame.origin, + ) + return 1 + } + if (kindName === "AsyncFromSyncIterator") { + return handleAdapterPush(this, callerFrame, slots) + } + const promiseHandled = this.scheduler.handlePromisePush(callerFrame, kindName, slots, site ?? callerFrame.origin) + if (promiseHandled !== undefined) return promiseHandled + if (kindName === "search") { + return this.hostResult( + callerFrame, + callSearch(this, this.hosts, slots[0] as Array, site ?? callerFrame.origin), + ) + } + if (kindName === "extension call") { + return this.hostResult(callerFrame, callExtension(this.ctx, this.hosts, slots, site ?? callerFrame.origin)) + } + throw new NotYetImplemented(`Push kind: ${kindName}`) + } + callerFrame.regs[callerFrame.sp++] = res as Value + return 0 + } + + // A host operation's answer, by `invokeCallable`'s convention: a value is pushed (0), a frame entered (1), or the job + // blocks (2). + private hostResult(callerFrame: Frame, result: HostResult): number { + if (result === ENTERED) return 1 + if (result === BLOCK) return 2 + callerFrame.regs[callerFrame.sp++] = result + return 0 + } + + /** + * Enters the program function `fn`, from the call at `origin`. A frame in another `mode` than `CALLER` settles + * `target`, and hands the caller `answer` when it ends. + */ + pushFn( + fn: Fn, + thisValue: Value, + args: Array, + origin: AstNode | undefined, + mode: FrameMode, + target?: unknown, + answer?: Value, + ): void { + const code = fn.code + const counted = (code.flags & CodeFlags.INTERNAL) === 0 + if (counted) this.depth++ + const f = this.acquire(code) + f.fn = fn + f.cells = fn.cells.length === 0 ? EMPTY_CELLS : fn.cells + f.counted = counted + if ((code.flags & CodeFlags.SIMPLE_PARAMS) !== 0) { + for (let i = 0; i < code.params; i++) f.regs[i] = i < args.length ? args[i] : undefined + } + f.args = (code.flags & CodeFlags.NEEDS_ARGS) !== 0 ? args : undefined + f.thisValue = thisValue + f.origin = counted ? undefined : origin + f.pc = 0 + f.sp = code.locals + f.mode = mode + f.target = target + f.answer = answer + this.frames.push(f) + } + + private pushInternalCall(name: string, args: Array, origin: AstNode | undefined): void { + this.pushInternal(internalCode(name), args, origin) + } + + /** Runs an internal prelude function at the call `origin`; see `HostVm.pushInternal`. */ + pushInternal( + code: Code, + args: ReadonlyArray, + origin: AstNode | undefined, + mode: FrameMode = Mode.CALLER, + target?: unknown, + answer?: Value, + ): Frame { + const f = this.acquire(code) + f.fn = null + f.cells = EMPTY_CELLS + for (let i = 0; i < code.params; i++) f.regs[i] = i < args.length ? args[i] : undefined + f.thisValue = undefined + f.args = undefined + f.origin = origin + f.pc = 0 + f.sp = code.locals + f.mode = mode + f.target = target + f.answer = answer + f.counted = false + this.frames.push(f) + return f + } + + private execIterOpen(frame: Frame, context: number, pc: number, sp: number): number { + const v = frame.regs[sp - 1] as Value + const fcode = frame.code + if ( + context !== IterContext.FOR_AWAIT && + context !== IterContext.ASYNC_DELEGATE && + context !== IterContext.DELEGATE + ) { + const cursor = builtinCursor(this.ctx.builtins, v) + if (cursor !== undefined) { + if (cursor instanceof Position) { + frame.regs[sp - 1] = cursor + } else { + frame.regs[sp - 1] = this.customIter(cursor.iterator, cursor.next) + } + return skipJump(fcode.code, pc) + } + } + const site = fcode.siteAt(pc) ?? frame.origin + if (context === IterContext.FOR_AWAIT || context === IterContext.ASYNC_DELEGATE) { + if (this.openWithMethod(frame, v, AsyncIteratorSymbol, site, sp)) return pc + 1 + const cursor = builtinCursor(this.ctx.builtins, v) + if (cursor !== undefined) { + const adapter = new AsyncFromSyncIterator(this.ctx.builtins.AsyncFromSyncIterator, cursor, site!) + frame.regs[sp - 1] = new CustomIter(adapter, get(adapter, "next")) + return skipJump(fcode.code, pc) + } + if (v instanceof Obj) { + const syncMethod = readProperty(v, IteratorSymbol, site) + if (syncMethod !== undefined && syncMethod !== null) { + const callable = requireIteratorMethod(syncMethod, "Iterator method", site) + const openCode = internalCode("$vmOpenSyncForAsync") + frame.regs[sp - 1] = undefined + frame.regs[sp] = new VmInternalFn(openCode, [v, callable]) + frame.sp = sp + 1 + return pc + 1 + } + } + if (context === IterContext.FOR_AWAIT) { + throw invalidData(`for await...of requires an iterable value, received ${describeValue(v)}.`, site) + } + throw typeError("yield* requires a compatible iterable value.", site) + } + + if (context === IterContext.DELEGATE) { + if (this.openWithMethod(frame, v, IteratorSymbol, site, sp)) return pc + 1 + const cursor = builtinCursor(this.ctx.builtins, v) + if (cursor !== undefined) { + const iterObj = + cursor instanceof Position ? new IteratorObj(this.ctx.builtins.Iterator, cursor) : cursor.iterator + frame.regs[sp - 1] = new CustomIter(iterObj, get(iterObj, "next")) + return skipJump(fcode.code, pc) + } + throw typeError("yield* requires a compatible iterable value.", site) + } + + if (this.openWithMethod(frame, v, IteratorSymbol, site, sp)) return pc + 1 + if (context === IterContext.FOR_OF) { + throw invalidData(`for...of requires an iterable value, received ${describeValue(v)}.`, site) + } + if (context === IterContext.DESTRUCTURE) { + throw typeError("Array destructuring requires a supported iterable value.", site) + } + throw typeError( + site?.type === "SpreadElement" && (fcode.sourceObj?.nodes.includes(site) ?? false) + ? "Spread requires a synchronous iterable." + : "Value is not iterable.", + site, + ) + } + + private execIterInit(frame: Frame, context: number, pc: number, sp: number): void { + const site = frame.code.siteAt(pc) ?? frame.origin + const v = frame.regs[sp - 1] as Value + if (context === IterContext.CHECK_OBJECT) { + requireIteratorObject(v, "Iterator return() result", site) + return + } + const obj = requireIteratorObject(v, "Iterator method result", site) + const next = requireIteratorMethod(readProperty(obj, "next", site), "Iterator next", site) + frame.regs[sp - 1] = this.customIter(obj, next) + } + + // Opens `v` with its own `symbol` method, when it has one: the next instruction calls it. + private openWithMethod(frame: Frame, v: Value, symbol: symbol, site: AstNode | undefined, sp: number): boolean { + if (!(v instanceof Obj)) return false + const method = readProperty(v, symbol, site) + if (method === undefined || method === null) return false + frame.regs[sp - 1] = v + frame.regs[sp] = requireIteratorMethod(method, "Iterator method", site) + frame.sp = sp + 1 + return true + } + + // A synchronous generator's (or iterator helper's) own `next` is stepped without a call to it. + private customIter(iterator: Obj, next: Value): CustomIter { + const custom = new CustomIter(iterator, next) + const builtins = this.ctx.builtins + custom.rawGen = + iterator instanceof GeneratorObj && + !iterator.asynchronous && + (next === get(builtins.Generator, "next") || next === get(builtins.IteratorHelper, "next")) + return custom + } + + // Decodes one or two `EXTRA` prefix words and dispatches the widened instruction. + // Negative returns encode outer control: `-1` = `continue outer`, `-2` = `return 0`, `-3` = `return 1`, `-4` = `return 2`. + private execExtra(frame: Frame, w: number, pc: number, sp: number): number { + const code = frame.code.code + const w1 = code[pc + 1]! + const op1 = w1 & 0xff + if (op1 === Op.EXTRA) { + const w2 = code[pc + 2]! + const wideA = ((w >>> 16) << 16) | (w1 & 0xff00) | ((w2 >>> 8) & 0xff) + const wideBx = ((w1 >>> 16) << 16) | (w2 >>> 16) + frame.pc = pc + 2 + frame.sp = sp + return this.execWide(frame, w2 & 0xff, wideA, wideBx, pc + 2) + } + const wideA = (w & 0xff00) | ((w1 >>> 8) & 0xff) + const wideBx = ((w >>> 16) << 16) | (w1 >>> 16) + frame.pc = pc + 1 + frame.sp = sp + return this.execWide(frame, op1, wideA, wideBx, pc + 1) + } + + // An instruction after its `EXTRA` prefix: the same work as in `exec`, with wide `a` and `bx`. + private execWide(frame: Frame, op: number, a: number, bx: number, pc: number): number { + const fcode = frame.code + const regs = frame.regs + const consts = fcode.consts + const cells = frame.cells + let sp = frame.sp + switch (op) { + case Op.PUSH_UNDEF: + regs[sp++] = undefined + frame.sp = sp + return pc + 1 + case Op.PUSH_NULL: + regs[sp++] = null + frame.sp = sp + return pc + 1 + case Op.PUSH_BOOL: + regs[sp++] = a === 1 + frame.sp = sp + return pc + 1 + case Op.PUSH_CONST: + regs[sp++] = consts[bx] + frame.sp = sp + return pc + 1 + case Op.POP: + frame.sp = sp - 1 + return pc + 1 + case Op.POPN: + frame.sp = sp - bx + return pc + 1 + case Op.DUP: + regs[sp] = regs[sp - 1] + frame.sp = sp + 1 + return pc + 1 + case Op.PICK: + regs[sp] = regs[sp - 1 - a] + frame.sp = sp + 1 + return pc + 1 + case Op.ROT: + this.execRot(regs, a, sp) + return pc + 1 + case Op.GET_LOCAL: + case Op.GET_CELL: { + const v = op === Op.GET_LOCAL ? regs[bx] : (regs[bx] as Cell).v + if (v === HOLE) this.failTdz(frame, fcode.localNames[bx]!, pc, sp) + regs[sp++] = v + frame.sp = sp + return pc + 1 + } + case Op.PUT_LOCAL: { + const v = regs[--sp] + frame.sp = sp + if (a !== PutKind.INIT && (regs[bx] === HOLE || a === PutKind.CONST)) { + this.failPut(frame, fcode.localNames[bx]!, regs[bx], pc, sp) + } + regs[bx] = v + return pc + 1 + } + case Op.PUT_CELL: + case Op.PUT_UPVAL: { + const v = regs[--sp] + frame.sp = sp + const cell = op === Op.PUT_CELL ? (regs[bx] as Cell) : cells[bx]! + if (a !== PutKind.INIT && (cell.v === HOLE || a === PutKind.CONST)) { + this.failPut(frame, (op === Op.PUT_CELL ? fcode.localNames : fcode.upvalNames)[bx]!, cell.v, pc, sp) + } + cell.v = v + return pc + 1 + } + case Op.HOLE_LOCAL: + regs[bx] = HOLE + return pc + 1 + case Op.NEW_CELL: + regs[bx] = new Cell(a === 1 ? regs[bx] : HOLE) + return pc + 1 + case Op.COPY_CELL: + regs[bx] = new Cell((regs[bx] as Cell).v) + return pc + 1 + case Op.GET_UPVAL: { + const v = cells[bx]!.v + if (v === HOLE) this.failTdz(frame, fcode.upvalNames[bx]!, pc, sp) + regs[sp++] = v + frame.sp = sp + return pc + 1 + } + case Op.GET_GLOBAL: + regs[sp] = this.execGetGlobal(frame, a, consts[bx] as string, pc, sp) + frame.sp = sp + 1 + return pc + 1 + case Op.GET_INTRINSIC: + regs[sp++] = this.getIntrinsic(consts[bx] as string) + frame.sp = sp + return pc + 1 + case Op.ARGS: + regs[sp++] = this.execArgs(frame, a, bx) + frame.sp = sp + return pc + 1 + case Op.CLOSURE: + regs[sp++] = this.makeClosure(fcode, bx, regs, cells) + frame.sp = sp + return pc + 1 + case Op.SET_NAME: + this.execSetName(regs, consts, a, bx, sp) + return pc + 1 + case Op.IMPORT: + frame.sp = this.execImport(frame, a, bx, pc, sp) + return pc + 1 + case Op.GET_PROP: + frame.sp = this.execGetProp(frame, regs[sp - 1] as Value, consts[bx] as string, a, pc, sp) + return pc + 1 + case Op.GET_ELEM: + if (this.execGetElem(frame, regs[sp - 2] as Value, regs[sp - 1] as Value, a, pc, sp)) return -1 + return pc + 1 + case Op.PUT_PROP: { + const v = regs[--sp] as Value + frame.sp = sp + this.execPutProp(fcode, regs[sp - 1] as Value, consts[bx] as string, v, pc) + regs[sp - 1] = v + return pc + 1 + } + case Op.PUT_ELEM: { + const v = regs[--sp] as Value + const keyVal = regs[--sp] as Value + frame.sp = sp + this.execPutElem(fcode, regs[sp - 1] as Value, keyVal, v, pc) + regs[sp - 1] = v + return pc + 1 + } + case Op.DELETE_PROP: + regs[sp - 1] = this.execDeleteProp(fcode, regs[sp - 1] as Value, consts[bx] as string, pc) + return pc + 1 + case Op.DELETE_ELEM: { + const keyVal = regs[--sp] as Value + frame.sp = sp + if (this.execDeleteElem(frame, regs[sp - 1] as Value, keyVal, pc, sp)) return -1 + return pc + 1 + } + case Op.CHECK_BASE: + this.execCheckBase(fcode, a, bx, regs, sp, pc) + return pc + 1 + case Op.TO_KEY: { + const v = regs[sp - 1] as Value + if (typeof v !== "string" && typeof v !== "number" && typeof v !== "symbol") { + if (this.execToKey(frame, v, pc, sp)) return -1 + } + return pc + 1 + } + case Op.IN: { + const rhs = regs[--sp] as Value + frame.sp = sp + if (this.execIn(frame, regs[sp - 1] as Value, rhs, pc, sp)) return -1 + return pc + 1 + } + case Op.INSTANCEOF: { + const rhs = regs[--sp] as Value + frame.sp = sp + regs[sp - 1] = this.execInstanceof(fcode, regs[sp - 1] as Value, rhs, pc) + return pc + 1 + } + case Op.NEW_OBJECT: + regs[sp++] = new Obj(this.ctx.builtins.Object) + frame.sp = sp + return pc + 1 + case Op.NEW_ARRAY: + frame.sp = this.execNewArray(regs, bx, sp) + return pc + 1 + case Op.ARRAY_APPEND: + frame.sp = this.execArrayAppend(regs, a, sp) + return pc + 1 + case Op.DEFINE_FIELD: + frame.sp = this.execDefineField(regs, consts, a, bx, sp) + return pc + 1 + case Op.COPY_DATA_PROPS: + frame.sp = --sp + this.execCopyDataProps(fcode, regs[sp - 1] as Obj, regs[sp] as Value, pc) + return pc + 1 + case Op.COPY_REST: + frame.sp = this.execCopyRest(fcode, regs, bx, pc, sp) + return pc + 1 + case Op.TEMPLATE: + regs[sp++] = this.getTemplateObject(fcode, consts[bx] as TemplateSite) + frame.sp = sp + return pc + 1 + case Op.REGEXP: + regs[sp] = this.execRegExp(fcode, consts[bx] as RegExpSite, pc) + frame.sp = sp + 1 + return pc + 1 + case Op.ADD: + case Op.SUB: + case Op.MUL: + case Op.LT: + case Op.LE: + case Op.GT: + case Op.GE: { + const b = regs[--sp] as Value + const lhs = regs[sp - 1] as Value + frame.sp = sp + const sym = + op === Op.ADD + ? "+" + : op === Op.SUB + ? "-" + : op === Op.MUL + ? "*" + : op === Op.LT + ? "<" + : op === Op.LE + ? "<=" + : op === Op.GT + ? ">" + : ">=" + if (this.execBinarySlow(frame, sym, lhs, b, pc, sp)) return -1 + return pc + 1 + } + case Op.SEQ: { + const b = regs[--sp] + frame.sp = sp + regs[sp - 1] = regs[sp - 1] === b + return pc + 1 + } + case Op.SNE: { + const b = regs[--sp] + frame.sp = sp + regs[sp - 1] = regs[sp - 1] !== b + return pc + 1 + } + case Op.BINOP: { + const b = regs[--sp] as Value + const lhs = regs[sp - 1] as Value + frame.sp = sp + if (typeof lhs === "number" && typeof b === "number") { + regs[sp - 1] = this.execBinopNum(a, lhs, b) + return pc + 1 + } + if (this.execBinopSlow(frame, a, lhs, b, pc, sp)) return -1 + return pc + 1 + } + case Op.UNARY: { + const v = regs[sp - 1] as Value + if (typeof v === "number" && a < 3) { + regs[sp - 1] = a === 0 ? -v : a === 1 ? +v : ~v + return pc + 1 + } + if (this.execUnarySlow(frame, a, v, pc, sp)) return -1 + return pc + 1 + } + case Op.NOT: + regs[sp - 1] = !regs[sp - 1] + return pc + 1 + case Op.TO_NUMERIC: { + const v = regs[sp - 1] as Value + if (typeof v !== "number" && this.execToNumericSlow(frame, a, v, pc, sp)) return -1 + return pc + 1 + } + case Op.INC: { + const v = regs[sp - 1] as Value + if (typeof v === "number") { + regs[sp - 1] = a === 0 ? v + 1 : v - 1 + return pc + 1 + } + if (this.execIncSlow(frame, a, v, pc, sp)) return -1 + return pc + 1 + } + case Op.TO_STRING: { + const v = regs[sp - 1] as Value + if (typeof v !== "string" && this.execToStringSlow(frame, a, v, pc, sp)) return -1 + return pc + 1 + } + case Op.CONCAT: + frame.sp = this.execConcat(regs, bx, sp) + return pc + 1 + case Op.JUMP: + return bx + case Op.LOOP: + frame.pc = bx + if (--this.fuel <= 0) return -3 + return bx + case Op.JUMP_IF_FALSE: + frame.sp = sp - 1 + return regs[sp - 1] ? pc + 1 : bx + case Op.JUMP_IF_TRUE: + frame.sp = sp - 1 + return regs[sp - 1] ? bx : pc + 1 + case Op.JUMP_COND: { + const v = regs[sp - 1] + if (a === CondKind.NULLISH) { + if (v === null || v === undefined) { + regs[sp - 1] = undefined + return bx + } + return pc + 1 + } + if (a === CondKind.NULLISH_METHOD) { + if (v === null || v === undefined) { + frame.sp = sp - 1 + regs[sp - 2] = undefined + return bx + } + return pc + 1 + } + const jump = + a === CondKind.TRUTHY_KEEP + ? Boolean(v) + : a === CondKind.FALSY_KEEP + ? !v + : a === CondKind.NOT_NULLISH_KEEP + ? v !== null && v !== undefined + : v !== undefined + if (jump) return bx + frame.sp = sp - 1 + return pc + 1 + } + case Op.THROW: + return this.execThrow(frame, regs[--sp] as Value, pc, sp) + case Op.THROW_ERROR: + return this.execThrowError(frame, bx, pc, sp) + case Op.END_FINALLY: + return this.execEndFinally(frame, bx, pc, sp) + case Op.RETURN: + case Op.END_PROGRAM: { + const v = regs[sp - 1] as Value + if (frame.mode === 0) { + this.frames.pop() + if (frame.counted) this.depth-- + if (this.pool.length < 128) this.pool.push(frame) + const caller = this.frames[this.frames.length - 1]! + caller.regs[caller.sp++] = v + caller.pc++ + return -1 + } + return this.execReturnSlow(frame, v, pc, sp) ? -1 : -2 + } + case Op.NOP: + return pc + 1 + case Op.CALL: { + const called = this.execCallSlow(frame, a, pc, sp) + if (called === 1) return this.fuel <= 0 ? -3 : -1 + if (called === 2) return -4 + frame.pc = pc + 1 + return this.fuel <= 0 ? -3 : pc + 1 + } + case Op.CALL_SPREAD: { + const called = this.execCallSpread(frame, pc, sp) + if (called === 1) return this.fuel <= 0 ? -3 : -1 + if (called === 2) return -4 + frame.pc = pc + 1 + return this.fuel <= 0 ? -3 : pc + 1 + } + case Op.NEW: + if (this.execNew(frame, a, pc, sp)) return -1 + return pc + 1 + case Op.NEW_SPREAD: + if (this.execNewSpread(frame, pc, sp)) return -1 + return pc + 1 + case Op.ITER_OPEN: + return this.execIterOpen(frame, a, pc, sp) + case Op.ITER_INIT: + this.execIterInit(frame, a, pc, sp) + return pc + 1 + case Op.ITER_STEP: { + const rec = regs[a] + if (rec instanceof Position) { + const step = rec.step() + if (!step.done) { + regs[sp++] = step.value + frame.sp = sp + return skipJump(fcode.code, pc) + } + return bx + } + if (this.execIterStepSlow(frame, rec as CustomIter, bx, pc, sp)) return -1 + return frame.pc + } + case Op.ITER_RESULT: + return this.execIterResult(frame, a, bx, pc, sp) + case Op.ITER_CLOSE: + return this.execIterClose(frame, a, bx, pc, sp) + case Op.ITER_APPEND_ALL: + return this.execIterAppendAll(frame, a, bx, pc, sp) + case Op.FOR_IN: + regs[sp - 1] = this.execForIn(fcode, regs[sp - 1] as Value, pc) + return pc + 1 + case Op.GENERATOR_START: + execGeneratorStart(this, frame, pc, sp) + return -1 + case Op.YIELD: + if (execYield(this, frame, pc, sp)) return -1 + return frame.pc + case Op.RESUME: + return execResume(frame, bx, pc, sp) + case Op.AWAIT: + execAwait(this, frame, a, pc, sp) + return -1 + case Op.YSTAR: + return execYstar(frame, a, bx, pc, sp) + case Op.YSTAR_RESULT: + return execYstarResult(frame, a, bx, pc, sp) + default: + throw new Error(`Unexpected wide op ${op}`) + } + } +} + +// Skips the `JUMP slow` instruction following a test-and-skip `ITER_OPEN` or `ITER_STEP`. +const skipJump = (code: Int32Array, pc: number): number => ((code[pc + 1]! & 0xff) === Op.EXTRA ? pc + 3 : pc + 2) + +// A member read's result replaces its base at `at`; a method read (mode 1) keeps the receiver under the value. +const writeRead = (regs: Array, at: number, mode: number, receiver: Value, value: Value): number => { + if (mode !== 1) { + regs[at] = value + return at + 1 + } + regs[at] = receiver + regs[at + 1] = value + return at + 2 +} + +const NEXT: GeneratorRequest = { kind: "next", value: undefined } + +// Where the call at `pc` is located: its own node, or inside the VM's internal code, the call that entered it. +const callSiteOf = (frame: Frame, pc: number) => + (frame.code.flags & CodeFlags.INTERNAL) === 0 ? frame.code.siteAt(pc) : frame.origin + +// Rewrites an array's first item in place, so arrays a native returns share the item shape JSC sees in the VM's own. +const normalizeItems = (value: unknown) => { + if (!(value instanceof Arr) || !(0 in value.items)) return + const first = value.items[0] + value.items[0] = "" + value.items[0] = first +} + +let warmed = false + +const WARMUP_SOURCE = [ + "return (async () => {", + " let totalSum = 0", + " const fib = (n) => (n < 2 ? n : fib(n - 1) + fib(n - 2))", + " const restFn = (a, b, ...r) => r.length", + " const fiveFn = (a, b, c, d, e) => (a === b ? c : d) !== e ? 1 : 0", + " const work = async (i) => { await null; return i * 2 }", + " function* range(n) { for (let i = 0; i < n; i++) yield i }", + " for (let r = 0; r < 12; r++) {", + " let sum = 0", + " for (let i = 0; i < 6; i++) sum = (sum + i * 3 + (i === 2 ? 1 : 0)) % 1000003", + " sum += fib(3) + restFn(1, 2, 3) + fiveFn(r & 1, 1, 2, work, fib)", + " for (const x of range(3)) sum += x", + ' let out = ""', + " for (let i = 0; i < 3; i++) out += String.fromCharCode(97 + (i % 26))", + " const parts = []", + " for (let i = 0; i < 3; i++) parts.push(`item-${i}`)", + " const map = new Map(), set = new Set()", + ' for (let i = 0; i < 4; i++) { map.set("k" + (i % 2), i); set.add(i % 2) }', + " for (const [k, v] of map) sum += v", + " for (const v of set) sum += v", + ' map.delete("k0"); set.delete(0)', + " const items = Array.from({ length: 4 }, (_, i) => (i * 7919) % 4)", + " const mapped = items.map((x) => x * 2).filter((x) => x % 3 !== 0)", + " const total = mapped.reduce((a, b) => a + b, 0)", + " const sorted = mapped.slice(0, 4).sort((a, b) => a - b)", + ' const obj = { a: 1, b: "two", c: [1, r], d: null, e: sorted }', + " if (r >= 2) sum += JSON.stringify(JSON.parse(JSON.stringify(obj))).length", + " const pres = await Promise.all(Array.from({ length: 2 }, (_, i) => work(i)))", + " const tres = await Promise.all(Array.from({ length: 2 }, (_, i) => tools.echo({ id: i })))", + ' totalSum += sum + total + sorted.length + pres.length + tres.length + out.length + parts.join(",").length + map.size + set.size', + " }", + " return totalSum", + "})()", +].join("\n") + +const warmupVm = (vm: Vm): void => { + warmed = true + const savedTripEvery = vm.tripEvery + vm.tripEvery = 0 + try { + const ast = parse(WARMUP_SOURCE, { + ecmaVersion: "latest", + sourceType: "script", + allowReturnOutsideFunction: true, + }) + const source = new Source("", WARMUP_SOURCE, ast) + vm.start(source) + for (;;) { + const stop = vm.run() + if (stop.kind === "done" || stop.kind === "stuck") break + if (stop.kind === "host") { + for (const call of stop.calls) vm.complete(call.id, normal(1)) + } + } + vm.sourceCodes.delete(source.id) + vm.ctx.sources.delete(source.id) + } catch { + } finally { + vm.frames.length = 0 + vm.templates.clear() + vm.scheduler.parked.clear() + vm.scheduler.queue.length = 0 + vm.scheduler.head = 0 + vm.scheduler.pending = 0 + vm.scheduler.nextID = 0 + vm.scheduler.unhandled.clear() + vm.hosts.queued.length = 0 + vm.hosts.running.clear() + vm.hosts.inbox.length = 0 + vm.hosts.nextID = 0 + vm.hosts.blocked = undefined + vm.hosts.input = undefined + vm.depth = 0 + vm.fuel = INITIAL_FUEL + vm.sliced = false + vm.outcome = undefined + vm.tripEvery = savedTripEvery + } +} diff --git a/packages/codemode/test/array-callbacks-test262.test.ts b/packages/codemode/test/array-callbacks-test262.test.ts index 4dd98009516..a538072bfe6 100644 --- a/packages/codemode/test/array-callbacks-test262.test.ts +++ b/packages/codemode/test/array-callbacks-test262.test.ts @@ -56,7 +56,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/array-core-test262.test.ts b/packages/codemode/test/array-core-test262.test.ts index b1c3d67447a..1b436b5f3da 100644 --- a/packages/codemode/test/array-core-test262.test.ts +++ b/packages/codemode/test/array-core-test262.test.ts @@ -69,7 +69,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/callbacks.test.ts b/packages/codemode/test/callbacks.test.ts index 9722ae19a1a..5b736205f8f 100644 --- a/packages/codemode/test/callbacks.test.ts +++ b/packages/codemode/test/callbacks.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Schema } from "effect" -import { CodeMode, Tool } from "../src/index.js" +import { CodeMode, Tool } from "./lib/index.js" // Callback acceptance is one gate shared by array methods, sort, string replacers, // Array.from mappers, Map/Set/URLSearchParams forEach, and promise reactions: diff --git a/packages/codemode/test/codemode.test.ts b/packages/codemode/test/codemode.test.ts index 225805e403f..8392ccf2376 100644 --- a/packages/codemode/test/codemode.test.ts +++ b/packages/codemode/test/codemode.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Cause, Effect, Schema } from "effect" -import { CodeMode, Extension, Tool, toolError } from "../src/index.js" +import { CodeMode, Extension, Tool, toolError } from "./lib/index.js" const run = (tool: Tool.Tool) => Effect.runPromise(CodeMode.make({ tools: { host: { call: tool } } }).execute("return await tools.host.call({})")) diff --git a/packages/codemode/test/constructor-test262.test.ts b/packages/codemode/test/constructor-test262.test.ts index c038f3f21c3..368e71a883d 100644 --- a/packages/codemode/test/constructor-test262.test.ts +++ b/packages/codemode/test/constructor-test262.test.ts @@ -14,7 +14,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/date-test262.test.ts b/packages/codemode/test/date-test262.test.ts index ffda8cef4bf..04c5256c417 100644 --- a/packages/codemode/test/date-test262.test.ts +++ b/packages/codemode/test/date-test262.test.ts @@ -44,7 +44,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/engines.test.ts b/packages/codemode/test/engines.test.ts new file mode 100644 index 00000000000..879c496bbc2 --- /dev/null +++ b/packages/codemode/test/engines.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, test } from "bun:test" +import path from "node:path" +import { globalNames } from "../src/interpreter/globals.js" +import { VmEngine } from "../src/vm/index.js" + +const fixture = path.join(import.meta.dir, "fixtures/engine-modules.ts") + +const run = async (...args: Array) => { + const child = Bun.spawn([process.execPath, fixture, ...args], { stdout: "pipe", stderr: "inherit" }) + const output = await new Response(child.stdout).text() + expect(await child.exited).toBe(0) + return JSON.parse(output) as { result: unknown; vm: Array } +} + +describe("engines", () => { + // `CodeMode.make` checks extension globals against one list, so both engines must bind the same names. + test("bind the same global names", () => { + expect([...VmEngine.globalNames].sort()).toEqual([...globalNames].sort()) + }) + + test("the default engine loads no VM code", async () => { + const output = await run() + expect(output.result).toEqual({ ok: true, value: [2, 4], toolCalls: [] }) + expect(output.vm).toEqual([]) + }) + + test("the VM engine loads the VM on first use", async () => { + const output = await run("vm") + expect(output.result).toEqual({ ok: true, value: [2, 4], toolCalls: [] }) + expect(output.vm.length).toBeGreaterThan(0) + }) +}) diff --git a/packages/codemode/test/enumeration.test.ts b/packages/codemode/test/enumeration.test.ts index c46b6048d5b..e5feef46d5c 100644 --- a/packages/codemode/test/enumeration.test.ts +++ b/packages/codemode/test/enumeration.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Schema } from "effect" -import { CodeMode, Tool } from "../src/index.js" +import { CodeMode, Tool } from "./lib/index.js" // Key enumeration: Object.keys and for...in share one surface over plain objects, arrays // (index strings), and tool references (namespace/tool names from the supplied tools), so a diff --git a/packages/codemode/test/errors.test.ts b/packages/codemode/test/errors.test.ts index 8cbb483c247..4aa1636c288 100644 --- a/packages/codemode/test/errors.test.ts +++ b/packages/codemode/test/errors.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" // One failure is one program error object, and rethrowing it keeps the diagnostic it started with. const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/extensions.test.ts b/packages/codemode/test/extensions.test.ts index 5e66727559e..1dda1501431 100644 --- a/packages/codemode/test/extensions.test.ts +++ b/packages/codemode/test/extensions.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Context, Effect, Schema } from "effect" -import { CodeMode, Extension, Tool } from "../src/index.js" +import { CodeMode, Extension, Tool } from "./lib/index.js" const held: Array = [] const config = { retries: 3, nested: { deep: true } } diff --git a/packages/codemode/test/fixtures/build-id.ts b/packages/codemode/test/fixtures/build-id.ts new file mode 100644 index 00000000000..47a2a0f4a35 --- /dev/null +++ b/packages/codemode/test/fixtures/build-id.ts @@ -0,0 +1,9 @@ +// Prints this process's snapshot build id, from a runtime made with as few of the package's modules as it takes, so +// the id can be compared across entry points (`bun --preload build-id.ts`). +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { buildId } from "../../src/vm/snapshot.js" +import { ToolRuntime } from "../../src/tool-runtime.js" + +new Interpreter({ tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins() }) +console.log(buildId()) diff --git a/packages/codemode/test/fixtures/engine-modules.ts b/packages/codemode/test/fixtures/engine-modules.ts new file mode 100644 index 00000000000..44a10bdf12b --- /dev/null +++ b/packages/codemode/test/fixtures/engine-modules.ts @@ -0,0 +1,9 @@ +// Runs one program on the engine named in argv (default when absent) and prints which `src/vm` modules were loaded. +import path from "node:path" +import { Effect } from "effect" +import { CodeMode } from "../../src/index.js" + +const engine = process.argv[2] === "vm" ? "vm" : undefined +const result = await Effect.runPromise(CodeMode.execute({ code: "return [1, 2].map((n) => n * 2)", tools: {}, engine })) +const vm = path.join(import.meta.dir, "../../src/vm") + path.sep +console.log(JSON.stringify({ result, vm: Object.keys(require.cache).filter((key) => key.startsWith(vm)) })) diff --git a/packages/codemode/test/fixtures/prelude-roundtrip.ts b/packages/codemode/test/fixtures/prelude-roundtrip.ts new file mode 100644 index 00000000000..3e8a4eb0676 --- /dev/null +++ b/packages/codemode/test/fixtures/prelude-roundtrip.ts @@ -0,0 +1,28 @@ +// Runs prelude built-ins in the round-trip mode and prints the result and how many round trips were taken while a +// prelude frame was on the stack. +import { Effect } from "effect" +import { CodeMode } from "../../src/index.js" +import { CodeFlags } from "../../src/vm/opcodes.js" +import { setRoundTrip } from "../../src/vm/runtime/execute.js" +import { Vm } from "../../src/vm/vm.js" + +setRoundTrip(true) + +let saved = 0 +const capture = Vm.prototype.capture +Vm.prototype.capture = function (this: Vm, options) { + if (this.frames.some((frame) => (frame.code.flags & CodeFlags.INTERNAL) !== 0)) saved++ + return capture.call(this, options) +} +const result = await Effect.runPromise( + CodeMode.execute({ + tools: {}, + engine: "vm", + code: ` + const groups = Object.groupBy([1, 2, 3, 4, 5, 6], (x) => (x % 2 ? "odd" : "even")) + const union = new Set([1, 2, 3]).union({ size: 2, has: () => true, keys: () => [7, 8][Symbol.iterator]() }) + const parsed = JSON.parse('{"a":[1,2,{"b":3}]}', (k, v) => (typeof v === "number" ? v * 10 : v)) + return [groups, [...union], parsed, [1, 2, 3].values().reduce((a, b) => a + b)]`, + }), +) +console.log(JSON.stringify({ result, saved })) diff --git a/packages/codemode/test/for-await-test262.test.ts b/packages/codemode/test/for-await-test262.test.ts index fcbbcb85ccb..0863b1827c7 100644 --- a/packages/codemode/test/for-await-test262.test.ts +++ b/packages/codemode/test/for-await-test262.test.ts @@ -14,7 +14,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/generator-test262.test.ts b/packages/codemode/test/generator-test262.test.ts index 9afa501c215..b8569713ddb 100644 --- a/packages/codemode/test/generator-test262.test.ts +++ b/packages/codemode/test/generator-test262.test.ts @@ -12,7 +12,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/groupby-test262.test.ts b/packages/codemode/test/groupby-test262.test.ts index bf27ad846e9..152ec86bede 100644 --- a/packages/codemode/test/groupby-test262.test.ts +++ b/packages/codemode/test/groupby-test262.test.ts @@ -30,7 +30,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/json-callbacks-test262.test.ts b/packages/codemode/test/json-callbacks-test262.test.ts index 384ec6ece4b..8a0e16efa34 100644 --- a/packages/codemode/test/json-callbacks-test262.test.ts +++ b/packages/codemode/test/json-callbacks-test262.test.ts @@ -28,7 +28,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/labeled-control-test262.test.ts b/packages/codemode/test/labeled-control-test262.test.ts index 37866b17cb4..8d7196ca06e 100644 --- a/packages/codemode/test/labeled-control-test262.test.ts +++ b/packages/codemode/test/labeled-control-test262.test.ts @@ -12,7 +12,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/lexical-test262.test.ts b/packages/codemode/test/lexical-test262.test.ts index 6f67a9acb9b..0666e6da650 100644 --- a/packages/codemode/test/lexical-test262.test.ts +++ b/packages/codemode/test/lexical-test262.test.ts @@ -26,7 +26,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/lib/codemode.ts b/packages/codemode/test/lib/codemode.ts new file mode 100644 index 00000000000..4a688323f22 --- /dev/null +++ b/packages/codemode/test/lib/codemode.ts @@ -0,0 +1,16 @@ +import { CodeMode } from "../../src/index.js" + +export * from "../../src/codemode.js" + +// The suite runs on the engine the environment selects (`bun run test:vm`, `test:roundtrip`), unless a test picks one. +export const engine: "legacy" | "vm" = + process.env.CODEMODE_ENGINE === "vm" || process.env.CODEMODE_ROUNDTRIP === "1" ? "vm" : "legacy" + +if (process.env.CODEMODE_ROUNDTRIP === "1") { + const { setRoundTrip } = await import("../../src/vm/runtime/execute.js") + setRoundTrip(true) +} + +export const make: typeof CodeMode.make = (options = {}) => CodeMode.make({ engine, ...options }) + +export const execute: typeof CodeMode.execute = (options) => CodeMode.execute({ engine, ...options }) diff --git a/packages/codemode/test/lib/index.ts b/packages/codemode/test/lib/index.ts new file mode 100644 index 00000000000..512810aced2 --- /dev/null +++ b/packages/codemode/test/lib/index.ts @@ -0,0 +1,2 @@ +export * from "../../src/index.js" +export * as CodeMode from "./codemode.js" diff --git a/packages/codemode/test/limits.test.ts b/packages/codemode/test/limits.test.ts index db5175379d5..5e6cbcce973 100644 --- a/packages/codemode/test/limits.test.ts +++ b/packages/codemode/test/limits.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Fiber } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" import { MAX_ARRAY_LENGTH, MAX_PENDING_PROMISES, MAX_STRING_LENGTH } from "../src/interpreter/limits.js" const runtime = CodeMode.make({ tools: {} }) diff --git a/packages/codemode/test/new-expression.test.ts b/packages/codemode/test/new-expression.test.ts index 0beafad48fb..1be0c5cd337 100644 --- a/packages/codemode/test/new-expression.test.ts +++ b/packages/codemode/test/new-expression.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Schema } from "effect" -import { CodeMode, Tool } from "../src/index.js" +import { CodeMode, Tool } from "./lib/index.js" // `new` is supported syntax; only the callee decides whether construction succeeds. A callee without // construction support is a TypeError naming it, like JS, rather than an unsupported-syntax diagnostic diff --git a/packages/codemode/test/object-coercion-test262.test.ts b/packages/codemode/test/object-coercion-test262.test.ts index adba9e8bd62..d89133ae5a5 100644 --- a/packages/codemode/test/object-coercion-test262.test.ts +++ b/packages/codemode/test/object-coercion-test262.test.ts @@ -37,7 +37,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/openapi.test.ts b/packages/codemode/test/openapi.test.ts index 5a70ae71e72..6482ba4be65 100644 --- a/packages/codemode/test/openapi.test.ts +++ b/packages/codemode/test/openapi.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from "bun:test" import { Effect, Layer, Option } from "effect" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" -import { CodeMode, OpenAPI, Tool } from "../src/index.js" +import { CodeMode, OpenAPI, Tool } from "./lib/index.js" import { inputTypeScript, outputTypeScript } from "../src/tool-schema.js" const baseUrl = "http://localhost:4096" diff --git a/packages/codemode/test/parity.test.ts b/packages/codemode/test/parity.test.ts index 72b00a447a5..4d7bd129f26 100644 --- a/packages/codemode/test/parity.test.ts +++ b/packages/codemode/test/parity.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" // Runs a CodeMode program with no host tools and returns the CodeMode.Result. These tests pin the // JS-parity behaviors for the "99% of ordinary defensive JavaScript just works" goal: cases where diff --git a/packages/codemode/test/preload.ts b/packages/codemode/test/preload.ts new file mode 100644 index 00000000000..a4c53eccc6d --- /dev/null +++ b/packages/codemode/test/preload.ts @@ -0,0 +1,4 @@ +import { nativeRule } from "../src/vm/runtime/objects.js" + +// Every test run checks that natives are made only with the intrinsics. +nativeRule.check = true diff --git a/packages/codemode/test/promise-test262.test.ts b/packages/codemode/test/promise-test262.test.ts index d2b60659b2d..130c85770bf 100644 --- a/packages/codemode/test/promise-test262.test.ts +++ b/packages/codemode/test/promise-test262.test.ts @@ -14,7 +14,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {}, limits: { timeoutMs: 1_000 } })) diff --git a/packages/codemode/test/promise.test.ts b/packages/codemode/test/promise.test.ts index 947e3a50d69..4d84c77ec94 100644 --- a/packages/codemode/test/promise.test.ts +++ b/packages/codemode/test/promise.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Cause, Deferred, Effect, Exit, Schema } from "effect" -import { CodeMode, Tool, toolError } from "../src/index.js" +import { CodeMode, Tool, toolError } from "./lib/index.js" // Wave 5 acceptance suite: first-class promise values. Un-awaited tool calls start eagerly as // host work, `await` settles them, Promise.all/allSettled/race/resolve/reject are diff --git a/packages/codemode/test/regexp-math-test262.test.ts b/packages/codemode/test/regexp-math-test262.test.ts index bd85d3ef2db..d12562d10b2 100644 --- a/packages/codemode/test/regexp-math-test262.test.ts +++ b/packages/codemode/test/regexp-math-test262.test.ts @@ -41,7 +41,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/scan.test.ts b/packages/codemode/test/scan.test.ts index f6a876901b1..e4327f019e6 100644 --- a/packages/codemode/test/scan.test.ts +++ b/packages/codemode/test/scan.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Result } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" describe("CodeMode.scanTools", () => { test("extracts dotted, bracketed and nested static tool calls in first-seen order", () => { diff --git a/packages/codemode/test/set-methods-test262.test.ts b/packages/codemode/test/set-methods-test262.test.ts index 7d94aac1406..2aa857ed864 100644 --- a/packages/codemode/test/set-methods-test262.test.ts +++ b/packages/codemode/test/set-methods-test262.test.ts @@ -71,7 +71,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/signature.test.ts b/packages/codemode/test/signature.test.ts index ed41e9b02c2..2594ebc160f 100644 --- a/packages/codemode/test/signature.test.ts +++ b/packages/codemode/test/signature.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Schema, SchemaGetter } from "effect" -import { CodeMode, Tool } from "../src/index.js" +import { CodeMode, Tool } from "./lib/index.js" import { decodeInput, inputProperties, diff --git a/packages/codemode/test/stdlib.test.ts b/packages/codemode/test/stdlib.test.ts index 9c0d01672cc..9212fe34a04 100644 --- a/packages/codemode/test/stdlib.test.ts +++ b/packages/codemode/test/stdlib.test.ts @@ -16,7 +16,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect, Schema } from "effect" -import { CodeMode, Tool } from "../src/index.js" +import { CodeMode, Tool } from "./lib/index.js" // Standard-library value types: Date, RegExp, Map, Set. Programs use them as ordinary JS; // intra-CodeMode checkpoints (Object.* helpers, spread, coercion inputs) preserve the live diff --git a/packages/codemode/test/string-core-test262.test.ts b/packages/codemode/test/string-core-test262.test.ts index 9173fe352da..30400130715 100644 --- a/packages/codemode/test/string-core-test262.test.ts +++ b/packages/codemode/test/string-core-test262.test.ts @@ -151,7 +151,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" type Argument = string | number | undefined type Outcome = "undefined" | "length" | "RangeError" diff --git a/packages/codemode/test/string-date-methods-test262.test.ts b/packages/codemode/test/string-date-methods-test262.test.ts index 41757052756..24a5013c8ec 100644 --- a/packages/codemode/test/string-date-methods-test262.test.ts +++ b/packages/codemode/test/string-date-methods-test262.test.ts @@ -24,7 +24,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/string-regexp-test262.test.ts b/packages/codemode/test/string-regexp-test262.test.ts index fa0d6f709d6..513de982739 100644 --- a/packages/codemode/test/string-regexp-test262.test.ts +++ b/packages/codemode/test/string-regexp-test262.test.ts @@ -82,7 +82,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" type Vector = { readonly path: string diff --git a/packages/codemode/test/string-search-test262.test.ts b/packages/codemode/test/string-search-test262.test.ts index b596fc7bf9a..6abf045fe71 100644 --- a/packages/codemode/test/string-search-test262.test.ts +++ b/packages/codemode/test/string-search-test262.test.ts @@ -102,7 +102,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/test262/built-ins b/packages/codemode/test/test262/built-ins new file mode 120000 index 00000000000..b3ac649cc0b --- /dev/null +++ b/packages/codemode/test/test262/built-ins @@ -0,0 +1 @@ +/root/projects/opencode-dev-codemode/packages/codemode/test/test262/built-ins \ No newline at end of file diff --git a/packages/codemode/test/test262/language b/packages/codemode/test/test262/language new file mode 120000 index 00000000000..b517ecdff13 --- /dev/null +++ b/packages/codemode/test/test262/language @@ -0,0 +1 @@ +/root/projects/opencode-dev-codemode/packages/codemode/test/test262/language \ No newline at end of file diff --git a/packages/codemode/test/test262/run-vm.ts b/packages/codemode/test/test262/run-vm.ts new file mode 100644 index 00000000000..cacbb0208f3 --- /dev/null +++ b/packages/codemode/test/test262/run-vm.ts @@ -0,0 +1,171 @@ +// Runs one vendored test262 file verbatim on the bytecode VM. The harness (assert, Test262Error, compareArray, $DONE, +// $DONOTEVALUATE) is host-provided, except `assert` itself: a program function made by the prelude, so that +// `assert.throws` can call the function it tests and catch what it throws as plain program code. Its checks and +// failure messages are the host's (`$assert`). +import path from "node:path" +import { Effect } from "effect" +import { executeProgram } from "../../src/vm/runtime/execute.js" +import type { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { Throw } from "../../src/vm/runtime/model.js" +import { createErrorValue } from "../../src/vm/runtime/intrinsics.js" +import { constructor, fn, methods } from "../../src/vm/runtime/native.js" +import { Callable, define, get, hidden, Arr, Fn, Obj, type Value } from "../../src/vm/runtime/objects.js" +import { ToolRuntime } from "../../src/tool-runtime.js" +import type { Outcome } from "./run.js" + +const root = import.meta.dir + +type Frontmatter = { + flags?: Array + negative?: { phase: "parse" | "resolution" | "runtime"; type: string } +} + +const limits = { timeoutMs: 5000, maxToolCalls: undefined, maxOutputBytes: undefined } +const prepared = ToolRuntime.prepare({}) + +// One line, on the line of the "use strict" directive, so the test's own line numbers stay as they are. +const prelude = [ + "function assert(mustBeTrue, message) { $assert(mustBeTrue, message) }", + "assert.sameValue = $assert.sameValue; assert.notSameValue = $assert.notSameValue;", + "assert.compareArray = $assert.compareArray;", + "assert.throws = function (expected, func, message) {", + "try { func() } catch (thrown) { $assert.thrown(expected, message, thrown); return }", + "$assert.notThrown(expected, message) };", +].join(" ") + +export const runVm = async (file: string): Promise => { + const source = await Bun.file(path.join(root, file)).text() + const start = source.indexOf("/*---") + const meta = Bun.YAML.parse(source.slice(start + 5, source.indexOf("---*/", start))) as Frontmatter + let done: { error: unknown } | undefined + // Sloppy-only tests are never vendored, so every file runs as the strict half of test262's two-mode run. + // A host drains the job queue after an async test's script ends; the program end interrupts un-awaited + // work instead, so drain explicitly. + const drain = meta.flags?.includes("async") ? "\nfor (let i = 0; i < 100; i++) await null" : "" + const result = await Effect.runPromise( + executeProgram(`"use strict"; ${prelude}\n${source}${drain}`, prepared, limits, {}, (ctx) => + harness(ctx, (error) => { + done ??= { error } + }), + ), + ) + if (meta.negative !== undefined) { + // Runtime negatives only check that execution failed: diagnostics do not carry the error name. + if (result.ok) return { status: "fail", reason: `expected ${meta.negative.type} but completed` } + if (meta.negative.phase === "runtime" || result.error.kind === "ParseError") return { status: "pass" } + return { + status: "fail", + reason: `expected ${meta.negative.type} but got ${result.error.kind}: ${result.error.message}`, + } + } + if (!result.ok) return { status: "fail", reason: `${result.error.kind}: ${result.error.message}` } + if (!meta.flags?.includes("async")) return { status: "pass" } + if (done === undefined) return { status: "fail", reason: "$DONE was never called" } + if (done.error !== undefined) return { status: "fail", reason: `$DONE: ${show(done.error)}` } + return { status: "pass" } +} + +const harness = (ctx: Interpreter, onDone: (error: Value) => void): ReadonlyArray => { + const builtins = ctx.builtins + const test262Prototype = new Obj(builtins.Object) + define(test262Prototype, "name", "Test262Error", hidden) + const test262 = (args: Array) => + createErrorValue(test262Prototype, args[0] === undefined ? "" : String(args[0])) + const fail = (message: string) => { + throw new Throw(test262([message])) + } + const prefix = (message: unknown) => (message === undefined ? "" : `${String(message)} `) + const expectedName = (ctor: unknown) => (ctor instanceof Callable ? String(get(ctor, "name")) : show(ctor)) + const compare = (a: unknown, b: unknown) => + a instanceof Arr && + b instanceof Arr && + a.items.length === b.items.length && + a.items.every((value, i) => Object.is(value, b.items[i])) + const test262Error = constructor(builtins, test262Prototype, { + name: "Test262Error", + call: (_, args) => test262(args), + construct: (args) => test262(args), + }) + methods(builtins, test262Error, [["thrower", 1, (_, args) => fail(String(args[0]))]]) + const compareArray = fn(builtins, "compareArray", 2, (_, args) => compare(args[0], args[1])) + methods(builtins, compareArray, [["format", 1, (_, args) => show(args[0])]]) + const assert = fn(builtins, "$assert", 2, (_, args) => + args[0] === true + ? undefined + : fail(args[1] === undefined ? `Expected true but got ${show(args[0])}` : String(args[1])), + ) + methods(builtins, assert, [ + [ + "sameValue", + 3, + (_, args) => + Object.is(args[0], args[1]) + ? undefined + : fail(`${prefix(args[2])}Expected SameValue(«${show(args[0])}», «${show(args[1])}») to be true`), + ], + [ + "notSameValue", + 3, + (_, args) => + Object.is(args[0], args[1]) + ? fail(`${prefix(args[2])}Expected SameValue(«${show(args[0])}», «${show(args[1])}») to be false`) + : undefined, + ], + [ + "compareArray", + 3, + (_, args) => + compare(args[0], args[1]) + ? undefined + : fail( + `Actual ${show(args[0])} and expected ${show(args[1])} should have the same contents. ${prefix(args[2])}`, + ), + ], + [ + "notThrown", + 2, + (_, args) => + fail(`${prefix(args[1])}Expected a ${expectedName(args[0])} to be thrown but no exception was thrown at all`), + ], + [ + "thrown", + 3, + (_, args) => { + if (!(args[2] instanceof Obj)) return fail(`${prefix(args[1])}Thrown value was not an object!`) + const actual = get(args[2], "constructor") + return actual === args[0] + ? undefined + : fail(`${prefix(args[1])}Expected a ${expectedName(args[0])} but got a ${show(actual)}`) + }, + ], + ]) + return [ + ["$assert", assert], + ["compareArray", compareArray], + ["Test262Error", test262Error], + [ + "$DONE", + fn(builtins, "$DONE", 1, (_, args) => { + onDone(args[0]) + return undefined + }), + ], + [ + "$DONOTEVALUATE", + fn(builtins, "$DONOTEVALUATE", 0, () => { + throw new Throw("Test262: This statement should not be evaluated.") + }), + ], + ] +} + +const show = (value: unknown): string => { + if (typeof value === "string") return JSON.stringify(value) + if (Object.is(value, -0)) return "-0" + if (value instanceof Arr) return `[${value.items.map(show).join(", ")}]` + if (value instanceof Fn) return "program function" + if (value instanceof Callable) return String(get(value, "name")) + if (!(value instanceof Obj)) return String(value) + const message = get(value, "message") + return typeof message === "string" ? `${String(get(value, "name") ?? "object")}: ${message}` : "object" +} diff --git a/packages/codemode/test/test262/run.ts b/packages/codemode/test/test262/run.ts index 5cee42d0e86..9b7293598f1 100644 --- a/packages/codemode/test/test262/run.ts +++ b/packages/codemode/test/test262/run.ts @@ -11,6 +11,7 @@ import { createErrorValue } from "../../src/interpreter/intrinsics.js" import { constructor, fn, methods } from "../../src/interpreter/native.js" import { Callable, define, get, hidden, Arr, Fn, Obj, type Value } from "../../src/interpreter/objects.js" import { ToolRuntime } from "../../src/tool-runtime.js" +import { runVm } from "./run-vm.js" export const root = import.meta.dir @@ -33,6 +34,12 @@ const limits = { timeoutMs: 5000, maxToolCalls: undefined, maxOutputBytes: undef const prepared = ToolRuntime.prepare({}) export const run = async (file: string): Promise => { + if ( + typeof process !== "undefined" && + (process.env.CODEMODE_ENGINE === "vm" || process.env.CODEMODE_ROUNDTRIP === "1") + ) { + return runVm(file) + } const source = await Bun.file(path.join(root, file)).text() const start = source.indexOf("/*---") const meta = Bun.YAML.parse(source.slice(start + 5, source.indexOf("---*/", start))) as Frontmatter diff --git a/packages/codemode/test/tool-paths.test.ts b/packages/codemode/test/tool-paths.test.ts index a739d651510..76c4a875c44 100644 --- a/packages/codemode/test/tool-paths.test.ts +++ b/packages/codemode/test/tool-paths.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { Effect, Schema } from "effect" -import { CodeMode, Namespace, Tool } from "../src/index.js" +import { CodeMode, Namespace, Tool } from "./lib/index.js" const echo = (description: string, result: string) => Tool.make({ diff --git a/packages/codemode/test/var-hoisting-test262.test.ts b/packages/codemode/test/var-hoisting-test262.test.ts index 667603f5232..a1729841f49 100644 --- a/packages/codemode/test/var-hoisting-test262.test.ts +++ b/packages/codemode/test/var-hoisting-test262.test.ts @@ -21,7 +21,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const value = async (code: string) => { const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) diff --git a/packages/codemode/test/vm/build-id.test.ts b/packages/codemode/test/vm/build-id.test.ts new file mode 100644 index 00000000000..c5a005ef8d3 --- /dev/null +++ b/packages/codemode/test/vm/build-id.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, test } from "bun:test" +import path from "node:path" + +// The snapshot build id hashes only the package's own bytecode, codecs, and built-ins, so it is the same in every +// process, whatever it imported first. + +const root = path.join(import.meta.dir, "../..") + +const output = async (command: Array) => { + const child = Bun.spawn(command, { cwd: root, stdout: "pipe", stderr: "pipe" }) + const [stdout, stderr] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text()]) + expect({ code: await child.exited, stderr }).toMatchObject({ code: 0 }) + return stdout +} + +describe("build id", () => { + test("the build id is the same whatever a process imported first", async () => { + const fixture = path.join(import.meta.dir, "../fixtures/build-id.ts") + const ids = await Promise.all( + [[], ["--preload", "./test/test262/run.ts"], ["--preload", "./src/index.ts"]].map((preload) => + output(["bun", ...preload, fixture]), + ), + ) + expect(ids[0]).toMatch(/\+[0-9a-f]+\n$/) + expect(ids[1]).toBe(ids[0]!) + expect(ids[2]).toBe(ids[0]!) + }) +}) diff --git a/packages/codemode/test/vm/limits.test.ts b/packages/codemode/test/vm/limits.test.ts new file mode 100644 index 00000000000..bcb129767b2 --- /dev/null +++ b/packages/codemode/test/vm/limits.test.ts @@ -0,0 +1,606 @@ +import { describe, expect, test } from "bun:test" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import type { ToolRuntime } from "../../src/tool-runtime.js" +import { CodeBuilder, CodeSizeOverflow, hashOf } from "../../src/vm/code.js" +import { compileSource } from "../../src/vm/compiler/compile.js" +import { CodeFlags, EMPTY_CELLS, Op, packWord, PutKind } from "../../src/vm/opcodes.js" +import { Mode } from "../../src/vm/vm.js" +import { parseVmSource, type RunOptions, runProgram } from "./run.js" +import { verifyAll, verifyCode } from "./verify.js" + +const emptyTools = { + calls: [], + keys: () => [], +} as unknown as ToolRuntime + +const compileVerified = (code: string) => { + const parsed = parseVmSource(code) + expect(parsed.ok).toBe(true) + if (!parsed.ok) throw new Error(parsed.error.message) + const codes = compileSource(parsed.source, { noCache: true }) + expect(verifyAll(codes)).toEqual([]) + return { source: parsed.source, codes } +} + +const runVerified = (code: string, options?: RunOptions) => { + const compiled = compileVerified(code) + const res = runProgram(code, options) + if (!res.ok) throw new Error(`${res.error.kind}: ${res.error.message}`) + return { value: res.value, codes: compiled.codes } +} + +// Emits ~27 words per line in wide mode (~25 in narrow mode) with shallow AST depth, reaching >65,535 words in ~2,800 lines. +const makePaddingBlock = (count: number, accVar = "padAcc", condVar = "padCond"): string => { + const lines = Array.from( + { length: count }, + (_, i) => + `if (${condVar}) { ${accVar} += ${(i % 7) + 1} + 1 + 1 + 1 + 1 + 1 + 1 + 1 } else { ${accVar} -= 1 }`, + ) + return lines.join("\n") +} + +describe("CodeBuilder and packWord encoding bounds", () => { + test("packWord and CodeBuilder reject out-of-range operands instead of masking", () => { + expect(() => packWord(Op.NOP, -1, 0)).toThrow(RangeError) + expect(() => packWord(Op.NOP, 256, 0)).toThrow(RangeError) + expect(() => packWord(Op.NOP, 0, -1)).toThrow(RangeError) + expect(() => packWord(Op.NOP, 0, 65536)).toThrow(RangeError) + expect(() => packWord(256 as never, 0, 0)).toThrow(RangeError) + + const builder = new CodeBuilder( + { id: "test", hash: "" }, + undefined, + 0, + "test", + CodeFlags.PROGRAM, + 0, + 0, + 2, + ["a", "b"], + new Int32Array(0), + ) + expect(() => builder.emit(Op.EXTRA, 0, 0)).toThrow(RangeError) + expect(() => builder.emit(Op.GET_LOCAL, -1, 0)).toThrow(RangeError) + expect(() => builder.emit(Op.GET_LOCAL, 0, -1)).toThrow(RangeError) + expect(() => builder.emit(Op.GET_LOCAL, 0x80000000, 0)).toThrow(RangeError) + expect(() => builder.emit(Op.GET_LOCAL, 0, 0x80000000)).toThrow(RangeError) + expect(() => builder.emit(Op.NEW, 256, 0)).toThrow(RangeError) + expect(() => builder.emit(Op.PUSH_INT, 1, 0)).toThrow(RangeError) + expect(() => builder.emit(Op.PUSH_INT, 0, 0x10000)).toThrow(RangeError) + }) + + test("narrow CodeBuilder throws CodeSizeOverflow past 65,535 words, while wideJumps supports 31-bit jumps and double EXTRA", () => { + const narrow = new CodeBuilder( + { id: "test", hash: "" }, + undefined, + 0, + "narrow", + CodeFlags.PROGRAM, + 0, + 0, + 0, + [], + new Int32Array(0), + ) + for (let i = 0; i < 0xffff; i++) narrow.emit(Op.NOP) + expect(() => narrow.emit(Op.NOP)).toThrow(CodeSizeOverflow) + + const localCount = 70_000 + const wide = new CodeBuilder( + { id: "test", hash: "" }, + undefined, + 0, + "wide", + CodeFlags.PROGRAM, + 0, + 0, + localCount, + Array.from({ length: localCount }, (_, i) => `v${i}`), + new Int32Array(0), + ) + wide.wideJumps = true + const slow = wide.newLabel(2) + const join = wide.newLabel(1) + const stepSlow = wide.newLabel(2) + const got = wide.newLabel(1) + const done = wide.newLabel(0) + // Build `[42]` on stack, ITER_OPEN into slot 69,999, ITER_STEP (with a = 69,999 > 0xffff -> double EXTRA) + // jumping past 66,000 NOPs on done, and return the stepped item. + wide.emitNumber(42) + wide.emit(Op.NEW_ARRAY, 0, 1, -1, 0) + wide.emitJump(Op.ITER_OPEN, 0, slow, -1, 0, 1) + wide.emitJump(Op.JUMP, 0, slow, -1, 0, null) + wide.mark(join) + wide.emit(Op.PUT_LOCAL, PutKind.INIT, 69_999, -1, -1) + wide.emitJump(Op.ITER_STEP, 69_999, done, -1, 0, 2) + wide.emitJump(Op.JUMP, 0, stepSlow, -1, 0, null) + wide.mark(got) + wide.emit(Op.PUT_LOCAL, PutKind.INIT, 69_998, -1, -1) + for (let i = 0; i < 66_000; i++) wide.emit(Op.NOP) + // Second ITER_STEP hits done -> jumps past slow handlers to `done` (target > 66,000) + wide.emitJump(Op.ITER_STEP, 69_999, done, -1, 0, 2) + wide.emitJump(Op.JUMP, 0, stepSlow, -1, 0, null) + wide.setDepth(1) + wide.emit(Op.POP, 0, 0, -1, -1) + wide.emitJump(Op.JUMP, 0, done, -1, 0, null) + wide.mark(slow) + wide.emit(Op.CALL, 0, 0, -1, -1) + wide.emit(Op.ITER_INIT, 0, 0, -1, 0) + wide.emitJump(Op.JUMP, 0, join, -1, 0, null) + wide.mark(stepSlow) + wide.emit(Op.CALL, 0, 0, -1, -1) + wide.emitJump(Op.ITER_RESULT, 69_999, done, -1, -1, 0) + wide.emitJump(Op.JUMP, 0, got, -1, 0, null) + wide.mark(done) + wide.emit(Op.GET_LOCAL, 0, 69_998, -1, 1) + wide.emit(Op.END_PROGRAM, 0, 0, -1, -1) + const finished = wide.finish() + expect(finished.code.length).toBeGreaterThan(66_000) + expect(verifyCode(finished)).toEqual([]) + + const ctx = new Interpreter({ tools: emptyTools, builtins: createBuiltins(), logs: [] }) + const vm = ctx.vm + vm.sourceCodes.set("test", [finished]) + const root = vm.acquire(finished) + root.fn = null + root.cells = EMPTY_CELLS + root.pc = 0 + root.sp = finished.locals + root.mode = Mode.PROGRAM + root.counted = true + vm.depth = 1 + vm.frames.push(root) + const stop = vm.run() + expect(stop).toEqual({ kind: "done", completion: { ok: true, value: 42 } }) + }) +}) + +describe("wide `a` operands and `execWide` coverage", () => { + test("executes a multi-feature program compiled entirely with forceExtra (every opcode widened)", () => { + const src = ` + function helper(a, b = 3, ...rest) { + let local = a + b + rest.length + arguments.length + const box = { v: local, [ "k" + 1 ]: 10 } + box.v++ + ++box.k1 + const { k1, ...rem } = { ...box, extra: 7 } + return rem.v + k1 + (typeof unknownGlobal === "undefined" ? 1 : 0) + (-2 >>> 0 > 0 ? 1 : 0) + } + function* gen(xs) { + yield* xs + const r = yield 99 + return r + } + async function run() { + const arr = [1, , ...[2, 3]] + let sum = 0 + for (const x of arr) sum += x ?? 0 + const custom = { + closed: 0, + [Symbol.iterator]() { + let i = 0 + return { + next: () => (i++ < 3 ? { value: i * 10, done: false } : { value: undefined, done: true }), + return: () => { custom.closed++; return {} }, + } + }, + } + for (const v of custom) { + sum += v + if (v === 20) break + } + const g = gen([4, 5]) + sum += g.next().value + g.next().value + g.next().value + g.next(6).value + const d = new Date(0) + return [sum, custom.closed, helper(2, undefined, 8, 9), d instanceof Date, \`\${sum}-\${custom.closed}\`] + } + return await run() + ` + const parsed = parseVmSource(src) + expect(parsed.ok).toBe(true) + if (!parsed.ok) return + const baseline = runProgram(src) + expect(baseline).toEqual({ ok: true, value: [150, 1, 25, true, "150-1"], toolCalls: [] }) + const wideCodes = compileSource(parsed.source, { forceExtra: true, noCache: true }) + expect(verifyAll(wideCodes)).toEqual([]) + + const ctx = new Interpreter({ + tools: emptyTools, + builtins: createBuiltins(), + logs: [], + }) + const vm = ctx.vm + ctx.sources.set(parsed.source.id, parsed.source) + vm.sourceCodes.set(parsed.source.id, wideCodes) + const root = vm.acquire(wideCodes[0]!) + root.fn = null + root.cells = EMPTY_CELLS + root.pc = 0 + root.sp = wideCodes[0]!.locals + root.mode = Mode.PROGRAM + root.counted = true + vm.depth = 1 + vm.frames.push(root) + while (true) { + const stop = vm.run() + if (stop.kind === "slice") continue + expect(stop.kind).toBe("done") + if (stop.kind === "done") { + expect(stop.completion).toEqual({ + ok: true, + value: [150, 1, 25, true, "150-1"], + }) + } + break + } + }) + + test("280+ locals with hoisted functions, for...of (array, custom, generator), spread, yield*, and destructuring", () => { + const localDecls = Array.from({ length: 280 }, (_, i) => + i % 25 === 0 ? `let v${i} = ${i}; const c${i} = () => v${i}` : `let v${i} = ${i}`, + ).join("\n") + const localReads = Array.from({ length: 280 }, (_, i) => (i % 25 === 0 ? `c${i}()` : `v${i}`)).join(" + ") + + const src = ` + function hoisted0(x) { return x + 100 } + function hoisted1(x) { return x * 2 } + function* hoistedGen(base) { + yield base + 1 + yield base + 2 + yield base + 3 + } + + async function exercise() { + function innerHoisted(z) { return hoisted0(z) + hoisted1(z) } + ${localDecls} + + let acc = ${localReads} + + // Array for...of (Position fast path with itSlot > 280) + for (const item of [10, 20, 30]) { + acc += innerHoisted(item) + } + + // Sync generator for...of (answerCaller with wide itSlot > 280, plus early break) + for (const item of hoistedGen(5)) { + acc += item + } + for (const item of hoistedGen(50)) { + acc += item + if (item === 52) break + } + + // Custom iterator with ITER_RESULT and ITER_CLOSE (break + throw) at itSlot > 280 + let closedCount = 0 + const makeCustom = (vals) => ({ + [Symbol.iterator]() { + let idx = 0 + return { + next() { + return idx < vals.length ? { value: vals[idx++], done: false } : { value: undefined, done: true } + }, + return() { + closedCount++ + return { done: true } + }, + } + }, + }) + for (const item of makeCustom([7, 8, 9])) { + acc += item + if (item === 8) break + } + try { + for (const item of makeCustom([11, 12])) { + if (item === 11) throw new Error("boom") + } + } catch (e) { + acc += e.message.length + } + + // Spread and array destructuring with itSlot > 280 + const spreadArr = [...[1, 2], ...hoistedGen(10), ...makeCustom([30, 40])] + const [d0, , d2, ...dRest] = hoistedGen(100) + acc += spreadArr.reduce((a, b) => a + b, 0) + d0 + d2 + dRest.length + + // Sync and async yield* with itSlot/kindSlot > 280 (including .return() and .throw()) + function* wideDelegate() { + ${Array.from({ length: 265 }, (_, i) => `const dLoc${i} = ${i}`).join("; ")} + const res = yield* { + [Symbol.iterator]() { + let step = 0 + return { + next(v) { + step++ + return step === 1 ? { value: dLoc264 + 1, done: false } : { value: (v ?? 0) + 5, done: true } + }, + return(v) { + return { value: v * 10, done: true } + }, + } + }, + } + return res + dLoc0 + } + const dg1 = wideDelegate() + const y1 = dg1.next().value + const y2 = dg1.next(4).value + const dg2 = wideDelegate() + dg2.next() + const yRet = dg2.return(7).value + + async function* wideAsyncGen() { + ${Array.from({ length: 265 }, (_, i) => `const aLoc${i} = ${i}`).join("; ")} + yield await Promise.resolve(aLoc260 + 2) + yield* hoistedGen(200) + } + let asyncSum = 0 + for await (const x of wideAsyncGen()) asyncSum += x + + // Hoisted functions in low slots must still be intact + return [acc, closedCount, y1, y2, yRet, asyncSum, hoisted0(1), hoisted1(3), innerHoisted(2)] + } + + return await exercise() + ` + + const { value, codes } = runVerified(src) + expect(codes.some((c) => c.locals > 280)).toBe(true) + const expectedTriangular = (279 * 280) / 2 + // innerHoisted(10)=130, (20)=160, (30)=190 => 480 + // hoistedGen(5): 6+7+8 = 21; hoistedGen(50) break at 52: 51+52 = 103 + // custom break: 7+8 = 15; custom throw "boom".length = 4 + // spreadArr: 1+2 + 11+12+13 + 30+40 = 109; destructure: d0=101, d2=103, dRest=[] (0) => 204 + const expectedAcc = expectedTriangular + 480 + 21 + 103 + 15 + 4 + 109 + 204 + expect(value).toEqual([expectedAcc, 2, 265, 9, 70, 262 + 201 + 202 + 203, 101, 6, 106]) + }) +}) + +describe("tagged templates, calls, and object rest past 254 items", () => { + test("tagged templates with 300 substitutions (plain and method tags) and 300+ call/new arguments", () => { + const parts = Array.from({ length: 300 }, (_, i) => `s${i}\${${i + 1}}`).join("") + "end" + const callArgs = Array.from({ length: 300 }, (_, i) => `${i}`).join(", ") + const src = ` + function plainTag(strings, ...vals) { + return [strings.length, strings.raw.length, strings[0], strings[300], vals.length, vals[0], vals[299], vals.reduce((a, b) => a + b, 0)] + } + const receiver = { + bias: 1000, + methodTag(strings, ...vals) { + return this.bias + strings.length + vals.length + vals[299] + }, + } + const sumFn = (...xs) => xs.length * 10000 + xs[0] + xs[299] + const constructed = new Array(${callArgs}) + return [ + plainTag\`${parts}\`, + receiver.methodTag\`${parts}\`, + sumFn(${callArgs}), + [constructed.length, constructed[0], constructed[299]], + ] + ` + const { value } = runVerified(src) + expect(value).toEqual([ + [301, 301, "s0", "end", 300, 1, 300, (300 * 301) / 2], + 1000 + 301 + 300 + 300, + 300 * 10000 + 0 + 299, + [300, 0, 299], + ]) + }) + + test("object rest destructuring after 305 static and computed keys", () => { + const objEntries = Array.from({ length: 310 }, (_, i) => `k${i}: ${i}`).join(", ") + const patternEntries = Array.from({ length: 305 }, (_, i) => + i % 40 === 0 ? `["k" + ${i}]: p${i}` : `k${i}: p${i}`, + ).join(", ") + const sumExtracted = Array.from({ length: 305 }, (_, i) => `p${i}`).join(" + ") + const src = ` + const srcObj = { ${objEntries} } + const { ${patternEntries}, ...rest } = srcObj + return [${sumExtracted}, Object.keys(rest), rest.k305, rest.k309] + ` + const { value } = runVerified(src) + expect(value).toEqual([(304 * 305) / 2, ["k305", "k306", "k307", "k308", "k309"], 305, 309]) + }) +}) + +describe("per-function wide jumps past 65,535 words and snapshot round-trips", () => { + test("a single function >65,535 words compiles with wide jumps (leaving sibling functions narrow) and runs all control flow", () => { + const padA = makePaddingBlock(1500, "padAcc", "padCond") + const padB = makePaddingBlock(1500, "padAcc", "padCond") + + const src = ` + function smallSibling(x) { + let s = 0 + for (let i = 0; i < x; i++) s += i + return s + } + + function* smallGen() { + yield 10 + yield 20 + } + + async function hugeControlFlow(mode) { + function hoistedInsideHuge(v) { return v * 3 } + let padAcc = 0 + let padCond = true + let trace = 0 + let finallyRuns = 0 + + // Outer labelled loop spanning >75,000 words from top to bottom + outer: for (let pass = 0; pass < 3; pass++) { + try { + // Switch whose dispatch jumps across the 65,535-word boundary + switch (pass) { + case 0: + ${padA} + trace += 1 + break + case 1: + ${padB} + // Continue outer across finally from past word 65,535 back to top of function + trace += 10 + continue outer + default: + // Break outer across finally and open for...of iterator past word 65,535 + for (const item of [100, 200, 300]) { + trace += item + if (item === 200) break outer + } + } + } catch (err) { + trace += 1000 + } finally { + finallyRuns++ + } + } + + // Code past word 75,000: while, do...while, for...in, for...of (array + generator), try/catch/finally, await + let w = 0 + while (w < 3) w++ + do { w += 2 } while (w < 7) + for (const k in { a: 1, b: 2 }) trace += k.length + for (const gVal of smallGen()) trace += gVal + + let caughtLate = "" + try { + await Promise.reject("late-reject") + } catch (e) { + caughtLate = e + } finally { + finallyRuns += 10 + } + + async function* lateAsyncGen() { + yield await Promise.resolve(5) + yield* smallGen() + } + for await (const av of lateAsyncGen()) trace += av + + return [ + hoistedInsideHuge(7), + smallSibling(5), + padAcc, + trace, + finallyRuns, + w, + caughtLate, + mode, + ] + } + + function* hugeGenerator() { + let padAcc = 0 + let padCond = true + const first = yield 1 + ${makePaddingBlock(2800, "padAcc", "padCond")} + yield* smallGen() + const second = yield padAcc + first + return second * 2 + } + + const mainRes = await hugeControlFlow("ok") + const hg = hugeGenerator() + const gSteps = [hg.next().value, hg.next(4).value, hg.next().value, hg.next().value, hg.next(9).value] + return { mainRes, gSteps } + ` + + const { value, codes } = runVerified(src) + const smallCode = codes.find((c) => c.name === "smallSibling")! + const hugeCode = codes.find((c) => c.name === "hugeControlFlow")! + const hugeGenCode = codes.find((c) => c.name === "hugeGenerator")! + expect(hugeCode.code.length).toBeGreaterThan(75_000) + expect(hugeGenCode.code.length).toBeGreaterThan(70_000) + // Sibling small function must NOT have any EXTRA prefixes: per-function wide mode keeps it byte-identical. + expect([...smallCode.code].some((w) => (w & 0xff) === Op.EXTRA)).toBe(false) + + // Determinism: recompiling produces identical hash + const parsed2 = parseVmSource(src) + expect(parsed2.ok).toBe(true) + if (parsed2.ok) { + expect(hashOf(compileSource(parsed2.source, { noCache: true }))).toBe(hashOf(codes)) + } + + // padA on pass 0: 1500 iterations; sum_{i=0..1499} ((i % 7) + 8) + let expectedPadA = 0 + for (let i = 0; i < 1500; i++) expectedPadA += (i % 7) + 8 + let expectedGenPad = 0 + for (let i = 0; i < 2800; i++) expectedGenPad += (i % 7) + 8 + + // trace: pass 0 (+1), pass 1 (+10), pass 2 (+100+200=300), for-in (+2), smallGen (+30), lateAsyncGen (+5+10+20=35) => 378 + expect(value).toEqual({ + mainRes: [21, 10, expectedPadA * 2, 378, 13, 7, "late-reject", "ok"], + gSteps: [1, 10, 20, expectedGenPad + 4, 18], + }) + }) + + test("top-level script >65,535 words runs correctly and survives snapshot round-trips at pc > 65,535", () => { + const src = ` + function hoistedTop(n) { return n + 42 } + function* topGen() { + yield 3 + yield 4 + } + let padAcc = 0 + let padCond = true + let total = 0 + + // Jump forward over >70,000 words on iteration 0, execute padding on iteration 1, then run post-65,535 loops + for (let round = 0; round < 2; round++) { + if (round === 0) { + ${makePaddingBlock(2800, "padAcc", "padCond")} + } else { + total += hoistedTop(8) + } + } + + // Now at pc > 70,000 in top-level : exercise loop, try/finally, generator for...of, and top-level await + try { + for (let k = 0; k < 25; k++) { + total += k + } + for (const v of topGen()) { + total += v + } + total += await Promise.resolve(100) + } finally { + total += 1 + } + + return [padAcc, total] + ` + + const compiled = compileVerified(src) + expect(compiled.codes[0]!.code.length).toBeGreaterThan(70_000) + + let expectedPad = 0 + for (let i = 0; i < 2800; i++) expectedPad += (i % 7) + 8 + // hoistedTop(8)=50, sum(0..24)=300, topGen=7, await=100, finally=1 => 458 + const expected = [expectedPad, 458] + + // First verify normal execution + expect(runProgram(src)).toEqual({ ok: true, value: expected, toolCalls: [] }) + + // Now run through snapshot round-trip mode, slicing inside both the padding loop and the >70,000-pc tail loop + let trips = 0 + let sawHighPc = false + const roundTripped = runProgram(src, { + roundTrip: { every: 6, max: 8 }, + onTrip: (snap) => { + trips++ + const text = JSON.stringify(snap) + for (const rec of snap.records) { + if (rec.t === "Frame" && Array.isArray(rec.f) && typeof rec.f[2] === "number" && rec.f[2] > 65_535) { + sawHighPc = true + } + } + expect(text.length).toBeGreaterThan(0) + }, + }) + expect(roundTripped).toEqual({ ok: true, value: expected, toolCalls: [] }) + expect(trips).toBeGreaterThan(2) + expect(sawHighPc).toBe(true) + }) +}) diff --git a/packages/codemode/test/vm/prelude-bytecode.test.ts b/packages/codemode/test/vm/prelude-bytecode.test.ts new file mode 100644 index 00000000000..04d466f1588 --- /dev/null +++ b/packages/codemode/test/vm/prelude-bytecode.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, test } from "bun:test" +import { compileBuiltins } from "../../script/prelude.js" +import saved from "../../src/vm/prelude/bytecode.json" with { type: "json" } +import { compiledPrelude } from "../../src/vm/prelude/prelude.js" +import { hashOf } from "../../src/vm/code.js" +import { getInternalPrelude } from "../../src/vm/internal-prelude.js" +import { moduleUrl, runProgram } from "./run.js" + +// The built-in sources are compiled ahead of time (script/prelude.ts); a process loads that bytecode instead. + +describe("precompiled built-ins", () => { + test("the saved bytecode is what the compiler makes now: run `bun run script/prelude.ts` after a change", () => { + expect(JSON.parse(JSON.stringify(compileBuiltins()))).toEqual(saved) + }) + + test("are loaded without parsing their sources, and have the compiled bytecode's hash", async () => { + const fresh = compileBuiltins() + for (const [built, id] of [ + [compiledPrelude(), "@prelude"], + [getInternalPrelude(), "@vm-internal"], + ] as const) { + const code = built.codes[0]! + expect(hashOf(built.codes)).toBe(code.source.hash) + expect(code.source).toEqual(fresh[id]!.codes[0]!.source as never) + } + // A fresh process runs a program that calls into the prelude, and its source is still unparsed. + const script = ` + import { runProgram } from ${moduleUrl("run.js")} + import { compiledPrelude } from ${moduleUrl("../../src/vm/prelude/prelude.js")} + const result = runProgram("return [3, 1, 2].map((x) => x * 2).sort((a, b) => a - b)") + console.log(JSON.stringify([result.ok && result.value, typeof compiledPrelude().codes[0].sourceObj.tree]))` + const child = Bun.spawn(["bun", "-e", script], { stdout: "pipe", stderr: "inherit" }) + expect(JSON.parse(await new Response(child.stdout).text())).toEqual([[2, 4, 6], "function"]) + expect(await child.exited).toBe(0) + }) + + test("a failure inside the prelude parses its source for the site, and is located at the program's call", () => { + const result = runProgram(`const xs = [1, 2]\nreturn xs.map((x) => x.y.z)`) + expect(result).toMatchObject({ ok: false, error: { location: { line: 2, column: 22 } } }) + }) +}) diff --git a/packages/codemode/test/vm/resumable.test.ts b/packages/codemode/test/vm/resumable.test.ts new file mode 100644 index 00000000000..86dd59c4392 --- /dev/null +++ b/packages/codemode/test/vm/resumable.test.ts @@ -0,0 +1,364 @@ +import { describe, expect, test } from "bun:test" +import path from "node:path" +import { parseProgram } from "../../src/vm/runtime/execute.js" +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { getOwn, Native, nativeRule, Obj, remove, set } from "../../src/vm/runtime/objects.js" +import { CodeMode } from "../lib/index.js" +import { decode, encode } from "../../src/vm/snapshot/codec.js" +import { Lost, PendingThrow } from "../../src/vm/runtime/model.js" +import { normalizeError } from "../../src/vm/runtime/errors.js" +import { Effect } from "effect" +import { ToolRuntime } from "../../src/tool-runtime.js" +import { nodeId, sourceOf } from "../../src/vm/runtime/source.js" + +// Groundwork for suspending and resuming a run: everything the state refers to has a stable name. + +const interpreter = () => + new Interpreter({ + tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), + builtins: createBuiltins(), + }) + +const parse = (code: string) => { + const parsed = parseProgram(code) + if (!parsed.ok) throw new Error(parsed.error.message) + return parsed +} + +describe("node ids", () => { + const code = `const add = (a, b) => a + b\nfor (const x of [1, 2]) { if (x) add(x, \`t\${x}\`) }\nreturn /a/g` + + test("number every node in pre-order and resolve back to it", () => { + const parsed = parse(code) + const nodes = parsed.source.nodes + expect(nodes[0]).toBe(parsed.program) + expect(nodes.map((node) => node.type).slice(0, 6)).toEqual([ + "Program", + "VariableDeclaration", + "VariableDeclarator", + "Identifier", + "ArrowFunctionExpression", + "Identifier", + ]) + nodes.forEach((node, index) => { + expect(nodeId(node)).toEqual({ source: "main", index }) + expect(parsed.source.node({ source: "main", index })).toBe(node) + expect(sourceOf(node)).toBe(parsed.source) + }) + }) + + test("give the same ids when the same code is parsed again", () => { + const first = parse(code).source.nodes + const second = parse(code).source.nodes + expect(second.map((node) => [node.type, node.start, node.end])).toEqual( + first.map((node) => [node.type, node.start, node.end]), + ) + }) + + test("do not resolve ids from another source", () => { + expect(parse(code).source.node({ source: "other", index: 0 })).toBeUndefined() + }) +}) + +describe("intrinsic names", () => { + test("name every intrinsic once, from the globals first", () => { + const names = interpreter().intrinsics + const all = names.all() + expect(new Set(all).size).toBe(all.length) + expect(new Set(names.values).size).toBe(names.values.length) + for (const name of [ + "Array.prototype.map", + "Promise.prototype.then", + "String.prototype[Symbol.iterator]", + "JSON.parse", + ]) { + expect(all).toContain(name) + } + for (const name of ["%Generator%", "%Generator%.next", "%AsyncGenerator%.return", "%IteratorHelper%.return"]) { + expect(all).toContain(name) + } + all.forEach((name, index) => { + expect(names.valueOf(name)).toBe(names.values[index]!) + expect(names.nameOf(names.values[index]!)).toBe(name) + }) + }) + + test("cover everything reachable from a named intrinsic", () => { + const names = interpreter().intrinsics + for (const value of names.values) { + for (const slot of value.props.values()) { + if ("value" in slot && slot.value instanceof Obj) expect(names.nameOf(slot.value)).toBeDefined() + } + if (value.proto !== null) expect(names.nameOf(value.proto)).toBeDefined() + } + expect(names.values.filter((value) => value instanceof Native).length).toBeGreaterThan(300) + }) + + test("are identical across fresh runtimes", () => { + const shape = (names: Interpreter["intrinsics"]) => + names.all().map((name, index) => { + const value = names.values[index]! + return [name, value.constructor.name, [...value.props.keys()].map(String)] + }) + expect(shape(interpreter().intrinsics)).toEqual(shape(interpreter().intrinsics)) + }) + + test("are taken before the program can change a built-in", () => { + const ctx = interpreter() + const map = ctx.intrinsics.valueOf("Array.prototype.map")! + const array = ctx.intrinsics.valueOf("Array.prototype")! + array.props.delete("map") + expect(ctx.intrinsics.nameOf(map)).toBe("Array.prototype.map") + }) + + test("only the intrinsics a write reaches are changed", () => { + const ctx = interpreter() + expect(ctx.intrinsics.changed()).toEqual([]) + const array = ctx.intrinsics.valueOf("Array.prototype")! + const math = ctx.intrinsics.valueOf("Math")! + set(array, "extra", 1) + remove(math, "max") + set(new Obj(array), "own", 1) + expect(ctx.intrinsics.changed().map((value) => ctx.intrinsics.nameOf(value))).toEqual(["Math", "Array.prototype"]) + }) +}) + +describe("natives only at intrinsic creation", () => { + test("every native made with the intrinsics has a name", () => { + const made: Array = [] + nativeRule.observe = (native) => made.push(native) + const ctx = (() => { + try { + return interpreter() + } finally { + nativeRule.observe = undefined + } + })() + expect(made.length).toBeGreaterThan(300) + expect( + made.filter((native) => ctx.intrinsics.nameOf(native) === undefined).map((native) => getOwn(native, "name")), + ).toEqual([]) + }) + + test("making a native once the program runs breaks the rule", () => { + const ctx = interpreter() + expect(() => new Native(ctx.builtins.Function, { name: "late", call: () => undefined })).toThrow( + 'Native "late" was made after the intrinsics', + ) + }) + + const value = async (code: string) => { + const result = await Effect.runPromise(CodeMode.execute({ code, tools: {}, engine: "vm" })) + if (!result.ok) throw new Error(result.error.message) + return result.value + } + + test("bound functions and resolvers behave as functions", async () => { + expect( + await value(` + function add(a, b, c) { return [this?.k, a, b, c] } + const bound = add.bind({ k: 1 }, 2) + let resolve, reject + const promise = new Promise((res, rej) => { resolve = res; reject = rej }) + const thenable = await new Promise((res) => res({ then(a, b) { a([a.name, a.length, typeof b]) } })) + resolve(5) + reject(6) + return [bound.name, bound.length, typeof bound, bound(3, 4), bound.bind(null, 3).name, resolve.name, resolve.length, + reject.name, typeof resolve, await promise, thenable] + `), + ).toEqual([ + "bound add", + 2, + "function", + [1, 2, 3, 4], + "bound bound add", + "resolve", + 1, + "reject", + "function", + 5, + ["", 1, "function"], + ]) + }) +}) + +describe("heap as data", () => { + // Each program's result must match the host engine's. + const programs = { + "map iteration is live": ` + const m = new Map([[1, 1], [2, 2], [3, 3]]) + const out = [] + for (const [k] of m) { + out.push(k) + if (k === 1) { m.delete(2); m.set(4, 4) } + if (k === 3) { m.clear(); m.set(5, 5) } + } + return [out, [...m]]`, + "a live iterator survives compaction": ` + const m = new Map(Array.from({ length: 100 }, (_, i) => [i, i])) + const it = m.keys() + const seen = [] + for (let i = 0; i < 10; i++) seen.push(it.next().value) + for (let i = 0; i < 80; i++) m.delete(i) + seen.push(...it) + const s = new Set(Array.from({ length: 100 }, (_, i) => i)) + const values = s.values() + for (let i = 0; i < 50; i++) values.next() + for (let i = 50; i < 90; i++) s.delete(i) + for (let i = 0; i < 40; i++) s.delete(i) + return [seen, values.next().value, [...s].length, m.size]`, + "clear mid-iteration continues with new entries": ` + const s = new Set([1, 2, 3]) + const it = s.values() + const out = [it.next().value] + s.clear() + s.add(7) + out.push(...it) + s.add(8) + return [out, it.next().done, [...s.entries()]]`, + "keys compare by SameValueZero": ` + const s = new Set([-0, NaN, NaN, 0]) + const m = new Map([[-0, "a"]]) + return [s.size, 1 / [...s][0], 1 / [...m.keys()][0], m.get(0), s.has(NaN)]`, + "forEach visits entries added during the walk": ` + const m = new Map([["a", 1]]) + const out = [] + m.forEach((v, k) => { out.push(k); if (m.size < 4) m.set(k + "x", v + 1) }) + const s = new Set([1, 2, 3]) + const seen = [] + s.forEach((v) => { seen.push(v); s.delete(v + 1) }) + return [out, seen]`, + "set operations read the live table": ` + const a = new Set([1, 2, 3, 4]) + const other = { size: 10, has(v) { a.delete(v + 1); return true }, keys() { return [][Symbol.iterator]() } } + return [[...a.intersection(other)], [...a]]`, + "array, string and bytes iterators keep a position": ` + const a = [1, 2] + const it = a.values() + it.next() + a.push(3) + const rest = [...it] + a.push(4) + const done = it.next().done + const entries = [...[5, , 6].entries()] + return [rest, done, entries, [..."a😀b"], [...new Uint8Array([1, 2]).entries()], [...[7, 8].keys()]]`, + "headers and search params iterate the live sorted list": ` + const h = new Headers({ b: "1", d: "2" }) + const hk = h.keys() + const first = hk.next().value + h.append("a", "x") + h.append("c", "y") + const p = new URLSearchParams("b=1&d=2") + const pk = p.keys() + const pFirst = pk.next().value + p.append("a", "x") + p.delete("b") + return [first, [...hk], pFirst, [...pk], [...h.entries()], [...p]]`, + "a RegExp keeps lastIndex as a field shared with the String methods": ` + const r = /a/g + const out = [r.exec("aaa").index, r.lastIndex] + out.push("xaxa".replace(r, "b"), r.lastIndex) + r.lastIndex = 1 + out.push(r.test("ab"), r.lastIndex, r.test("ab"), r.lastIndex) + const y = /a/y + y.lastIndex = 1 + out.push("ba".search(y), y.lastIndex, "ba".match(y)?.[0], y.lastIndex, "ba".split(y), y.lastIndex) + r.lastIndex = 2 + out.push([..."aaaa".matchAll(r)].length, r.lastIndex, new RegExp(r).lastIndex, String(new RegExp(r, "i"))) + return out`, + } + + for (const [name, code] of Object.entries(programs)) { + test(name, async () => { + const expected: unknown = await new Function(`return (async () => {${code}})()`)() + const result = await Effect.runPromise(CodeMode.execute({ code, tools: {}, engine: "vm" })) + if (!result.ok) throw new Error(result.error.message) + expect(result.value).toEqual(JSON.parse(JSON.stringify(expected))) + }) + } +}) + +describe("prelude", () => { + test("a run saved and restored while prelude built-ins run continues in their frames", async () => { + const child = Bun.spawn(["bun", path.join(import.meta.dir, "../fixtures/prelude-roundtrip.ts")], { + stdout: "pipe", + stderr: "inherit", + }) + const output = JSON.parse(await new Response(child.stdout).text()) + expect(await child.exited).toBe(0) + expect(output.result).toMatchObject({ + ok: true, + value: [{ odd: [1, 3, 5], even: [2, 4, 6] }, [1, 2, 3, 7, 8], { a: [10, 20, { b: 30 }] }, 6], + }) + expect(output.saved).toBeGreaterThan(0) + }) + + test("prelude functions look like built-ins and are named by their path", () => { + const names = interpreter().intrinsics + const groupBy = names.valueOf("Object.groupBy")! + expect(getOwn(groupBy, "name")).toBe("groupBy") + expect(getOwn(groupBy, "length")).toBe(2) + expect(names.all()).toContain("%Prelude%.fillCollection") + expect(names.all()).toContain("%PreludeHelpers%.$Call") + }) + test("programs can't see the helpers or internal functions, and failures inside are located at the call", async () => { + const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {}, engine: "vm" })) + expect(await run("return [typeof $Call, typeof $Throw, typeof fillCollection]")).toMatchObject({ + value: ["undefined", "undefined", "undefined"], + }) + expect(await run("const x = 1\nreturn Object.groupBy(null, (v) => v)")).toMatchObject({ + ok: false, + error: { location: { line: 2, column: 8 } }, + }) + }) +}) + +describe("lost values", () => { + const encodeEnv = (lost: Array) => ({ + nameOf: () => undefined, + unchanged: () => false, + anchorOf: () => undefined, + nodeId: () => undefined, + astArray: () => undefined, + lost, + strict: false, + }) + const decodeEnv = { + named: () => undefined, + anchor: () => undefined, + node: () => undefined, + astArray: () => undefined, + interruption: () => { + throw new Error("no interruption is saved") + }, + } + + test("a host function, a host object, and an unknown host error decode to what using a lost value throws", () => { + class Handle {} + class SocketError extends Error {} + const lost: Array = [] + const saved = encode( + [{ data: [1, "two", { three: 3 }], fn: () => 1, host: new Handle(), error: new SocketError("boom") }], + encodeEnv(lost), + ) + expect(lost).toEqual(["a host function", "a host Handle", "a host SocketError"]) + const [root] = decode(JSON.parse(JSON.stringify(saved)), decodeEnv) as [Record] + expect(root.data).toEqual([1, "two", { three: 3 }]) + expect(root.fn).toBeInstanceOf(Lost) + expect((root.fn as Lost).description).toBe("a host function") + expect(root.host).toBeInstanceOf(Lost) + expect((root.host as Lost).description).toBe("a host Handle") + expect(root.error).toBeInstanceOf(PendingThrow) + expect(normalizeError(root.error)).toEqual({ + kind: "ExecutionFailure", + message: + "TypeError: An error of class SocketError came from the host and was lost when the run was suspended; get it again after resuming.", + }) + // A restored placeholder saves and restores again the same way. + const again = decode(JSON.parse(JSON.stringify(encode([root], encodeEnv([])))), decodeEnv) as [ + Record, + ] + expect((again[0].fn as Lost).description).toBe("a host function") + }) +}) diff --git a/packages/codemode/test/vm/run.ts b/packages/codemode/test/vm/run.ts new file mode 100644 index 00000000000..933358ef328 --- /dev/null +++ b/packages/codemode/test/vm/run.ts @@ -0,0 +1,221 @@ +import path from "node:path" +import { pathToFileURL } from "node:url" +import { parse, type Program } from "acorn" +import type { DataValue, Diagnostic, Result } from "../../src/codemode.js" +import { normalizeError } from "../../src/vm/runtime/errors.js" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { formatLocation, PendingThrow, sourceLocation } from "../../src/vm/runtime/model.js" +import type { Value } from "../../src/vm/runtime/objects.js" +import { Interruption } from "../../src/vm/runtime/signals.js" +import { Source } from "../../src/vm/runtime/source.js" +import type { ToolRuntime } from "../../src/tool-runtime.js" +import type { Snapshot } from "../../src/vm/snapshot/snapshot.js" + +/** A quoted `file:` URL for a module relative to `test/vm`, to import from a script run with `bun -e` on any platform. */ +export const moduleUrl = (relative: string) => + JSON.stringify(pathToFileURL(path.join(import.meta.dir, relative)).href) + +const emptyTools = { + calls: [], + keys: () => [], +} as unknown as ToolRuntime + +export type RunOptions = { + readonly globals?: (ctx: Interpreter) => ReadonlyArray + readonly bindings?: (ctx: Interpreter) => ReadonlyArray + readonly maxSlices?: number + /** + * Round-trip mode: slice after every `every` fuel ticks, then save the state as JSON and continue in a fresh runtime + * restored from it, at most `max` times (default 200). + */ + readonly roundTrip?: { readonly every: number; readonly max?: number } + /** Called with each snapshot round-trip mode takes. */ + readonly onTrip?: (snapshot: Snapshot) => void +} + +export const parseVmSource = ( + code: string, + id = "main", +): + | { readonly ok: true; readonly program: Program; readonly source: Source } + | { readonly ok: false; readonly error: Diagnostic } => { + if (code.trim().length === 0) { + return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." } } + } + try { + const program = parse(code, { + ecmaVersion: "latest", + sourceType: "script", + allowReturnOutsideFunction: true, + allowAwaitOutsideFunction: true, + allowImportExportEverywhere: true, + locations: true, + }) + const exported = program.body.find((statement) => statement.type.startsWith("Export")) + if (exported) { + const location = sourceLocation(exported) + return { + ok: false, + error: { + kind: "ParseError", + message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`, + location, + }, + } + } + return { ok: true, program, source: new Source(id, code, program) } + } catch (error) { + if (error instanceof SyntaxError && "loc" in error) { + const loc = (error as SyntaxError & { readonly loc: { readonly line: number; readonly column: number } }).loc + const location = { line: loc.line, column: loc.column + 1 } + const reason = error.message.replace(/ \(\d+:\d+\)$/, "") + const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.` + return { + ok: false, + error: { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location }, + } + } + if (error instanceof RangeError) { + return { ok: false, error: normalizeError(error) } + } + throw error + } +} + +/** Runs a program on the VM synchronously, with no Effect and no tools: the VM tests' runner. */ +export const runProgram = (code: string, options?: RunOptions): Result => { + const parsed = parseVmSource(code) + if (!parsed.ok) { + return { ok: false, error: parsed.error, toolCalls: [] } + } + const runtime = (bindings: RunOptions["bindings"]) => + new Interpreter({ + tools: emptyTools, + builtins: createBuiltins(), + logs: [], + globals: options?.globals, + bindings, + }) + let ctx = runtime(options?.bindings) + let vm = ctx.vm + vm.start(parsed.source) + const roundTrip = options?.roundTrip + let trips = 0 + if (roundTrip !== undefined) vm.tripEvery = roundTrip.every + const maxSlices = options?.maxSlices ?? 5000 + let slices = 0 + while (true) { + const stop = vm.run() + if (stop.kind === "slice") { + if (roundTrip !== undefined && vm.tripEvery > 0) { + const { snapshot } = vm.capture({ strict: true }) + options?.onTrip?.(snapshot) + ctx = runtime(undefined) + vm = ctx.vm + vm.restore(JSON.parse(JSON.stringify(snapshot)), { + parse: (code) => { + const again = parseVmSource(code) + if (!again.ok) throw new Error(again.error.message) + return again.source + }, + interruption: () => { + throw new Error("No interruption is saved in round-trip mode.") + }, + }) + vm.tripEvery = ++trips < (roundTrip.max ?? 200) ? roundTrip.every : 0 + continue + } + if (++slices > maxSlices) { + return { + ok: false, + error: { kind: "TimeoutExceeded", message: "Execution timed out." }, + ...(ctx.logs.length > 0 ? { logs: [...ctx.logs] } : {}), + toolCalls: [], + } + } + continue + } + if (stop.kind === "done") { + const outcome = stop.completion + const logsPart = ctx.logs.length > 0 ? { logs: [...ctx.logs] } : {} + if (!outcome.ok) { + if (outcome.error instanceof Interruption) throw outcome.error + return { + ok: false, + error: normalizeError(outcome.error), + ...logsPart, + toolCalls: [], + } + } + const result = (outcome.value as DataValue | undefined) ?? null + const warnings = vm.diagnostics() + return { + ok: true, + value: result, + ...(warnings.length > 0 ? { warnings } : {}), + ...logsPart, + toolCalls: [], + } + } + const stuckNode = stop.kind === "stuck" ? stop.node : undefined + return { + ok: false, + error: normalizeError( + new PendingThrow( + "Error", + "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.", + stuckNode, + ), + ), + ...(ctx.logs.length > 0 ? { logs: [...ctx.logs] } : {}), + toolCalls: [], + } + } +} + +const WARMUP_SCRIPT = ` +function fib(n) { return n < 2 ? n : fib(n - 1) + fib(n - 2) } +let sum = 0 +for (let i = 0; i < 400; i++) sum = (sum + i * 3) % 10007 +const closures = [] +for (let i = 0; i < 16; i++) { + let s = "v" + i + let o = { x: i, y: s } + closures.push(() => o.x + s.length) +} +for (const fn of closures) sum += fn() +const arr = Array.from({ length: 64 }, (_, i) => (i * 17) % 64) +const mapped = arr.map((x) => x * 2).filter((x) => x % 3 !== 0) +sum += mapped.reduce((a, b) => a + b, 0) +const sorted = mapped.slice(0, 32).sort((a, b) => a - b) +sum += sorted.length + fib(8) +outer: for (let i = 0; i < 3; i++) { + try { + for (const x of [1, 2, 3]) { + try { + if (x === 2) continue + if (i === 1 && x === 3) break outer + sum += x + } finally { sum += 1 } + } + } catch (e) { sum -= 1 } finally { sum += 2 } +} +const { a = 1, b: [c = 2, ...rest] = [3, 4, 5] } = { b: [10, 20, 30] } +return sum + a + c + rest.length +` + +let warmed = false + +/** + * Startup training program (J3): exercises the VM dispatch loop with diverse types + * (numbers, strings, objects, arrays, closures, cells, try/catch/finally, iteration, destructuring) + * so JSC's DFG/FTL tiers up `Vm.prototype.exec` with polymorphic value profiles. + */ +export const warmupVm = (rounds = 8): void => { + if (warmed) return + warmed = true + for (let i = 0; i < rounds; i++) { + runProgram(WARMUP_SCRIPT) + } +} diff --git a/packages/codemode/test/vm/snapshot.test.ts b/packages/codemode/test/vm/snapshot.test.ts new file mode 100644 index 00000000000..e43f12f298d --- /dev/null +++ b/packages/codemode/test/vm/snapshot.test.ts @@ -0,0 +1,264 @@ +import { describe, expect, test } from "bun:test" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { type Snapshot, SnapshotError } from "../../src/vm/snapshot/snapshot.js" +import { ToolRuntime } from "../../src/tool-runtime.js" +import { hashOf } from "../../src/vm/code.js" +import { compileSource } from "../../src/vm/compiler/compile.js" +import { moduleUrl, parseVmSource, runProgram } from "./run.js" +import { buildId } from "../../src/vm/snapshot.js" + +// The VM's snapshots: a run saved and restored into a fresh runtime anywhere it slices goes on as if it never stopped. + +const programs: Record = { + "closures over per-iteration bindings": ` + const fns = [] + for (let i = 0; i < 5; i++) fns.push(() => i * 2) + let total = 0 + for (const f of fns) total += f() + return [fns.map((f) => f()), total]`, + "recursion several frames deep": ` + function fib(n) { return n < 2 ? n : fib(n - 1) + fib(n - 2) } + const walk = (node, depth) => node === null ? depth : walk(node.next, depth + 1) + return [fib(12), walk({ next: { next: { next: null } } }, 0)]`, + "finally levels, labels and pending returns": ` + const out = [] + function f(x) { + try { + try { if (x > 1) return "r" + x; out.push("t" + x) } finally { out.push("inner" + x) } + } finally { + out.push("outer" + x) + for (let i = 0; i < 2; i++) out.push("loop" + i) + } + return "n" + x + } + outer: for (let i = 0; i < 4; i++) { + for (let j = 0; j < 3; j++) { + try { if (j === 1) continue outer; if (i === 3) break outer } finally { out.push(i + ":" + j) } + } + } + return [f(1), f(2), out]`, + "a custom iterator in mid-iteration, and destructuring": ` + const log = [] + const it = { i: 0, [Symbol.iterator]() { return this }, next() { this.i++; return { value: this.i, done: this.i > 4 } }, return() { log.push("closed"); return {} } } + for (const x of it) { log.push(x); if (x === 3) break } + const [a, b = a * 10, ...rest] = [1, undefined, 3, 4] + const { p, q: { r = 5 } = {}, ...others } = { p: 1, s: 2, t: 3 } + return [log, a, b, rest, p, r, others]`, + "a tagged template keeps its strings object": ` + const seen = new Set() + const tag = (strings, ...values) => { seen.add(strings); return strings.raw.join("|") + values.join(",") } + let text = "" + for (let i = 0; i < 4; i++) text += tag\`a\${i}b\` + return [text, seen.size]`, + "prelude callbacks and sorts are interrupted and continue": ` + const xs = Array.from({ length: 30 }, (_, i) => (i * 7) % 30) + const sorted = [...xs].sort((a, b) => a - b) + const groups = Object.groupBy(xs, (x) => (x % 3 === 0 ? "three" : "other")) + const mapped = xs.map((x) => x * 2).filter((x) => x % 4 === 0).reduce((a, b) => a + b, 0) + return [sorted, groups.three.length, mapped, new Map(xs.map((x) => [x, x])).size]`, + "JSON with toJSON, replacers and revivers": ` + const value = { a: [1, { b: 2, toJSON() { return { c: 3 } } }], d: new Date(0) } + const text = JSON.stringify(value, (k, v) => (typeof v === "number" ? v + 1 : v), 2) + return [text, JSON.parse('{"a":[1,2,{"b":3}]}', (k, v) => (typeof v === "number" ? v * 10 : v))]`, + "errors keep their types and locations": ` + const caught = [] + for (let i = 0; i < 3; i++) { + try { if (i === 0) null.x; if (i === 1) undefinedName; if (i === 2) throw new RangeError("r" + i) } + catch (e) { caught.push(e.name + ": " + e.message) } + } + return caught`, + "implicit conversions run program code": ` + let calls = 0 + const o = { valueOf() { calls++; return 2 }, toString() { calls++; return "k" } } + const t = {} + t[o] = 1 + return [o + 1, o * o, \`\${o}\`, t.k, calls, String(o).padStart(3, "-")]`, + "logs and changed built-ins survive": ` + Array.prototype.extra = function () { return this.length } + Object.freeze(Math) + for (let i = 0; i < 3; i++) console.log("line", i) + return [[1, 2, 3].extra(), Object.isFrozen(Math)]`, + "the program's result is copied out after a slice in the boundary walk": ` + const rows = [] + for (let i = 0; i < 5; i++) rows.push({ i, toJSON() { return { row: this.i } } }) + return rows`, +} + +describe("round trips", () => { + for (const [name, code] of Object.entries(programs)) { + test(name, () => { + const straight = runProgram(code) + expect(straight.ok).toBe(true) + let trips = 0 + const tripped = runProgram(code, { roundTrip: { every: 1, max: 1000 }, onTrip: () => trips++ }) + expect(tripped).toEqual(straight) + expect(trips).toBeGreaterThan(0) + }) + } + + test("a failing program fails the same way, at the same place", () => { + const code = `let s = 0\nfor (let i = 0; i < 10; i++) s += i\nconst o = null\nreturn o.missing` + const straight = runProgram(code) + expect(straight.ok).toBe(false) + expect(runProgram(code, { roundTrip: { every: 1 } })).toEqual(straight) + }) +}) + +/** A runtime the way `execute.ts` makes one, with no tools. */ +const runtime = () => + new Interpreter({ + tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), + builtins: createBuiltins(), + }) as Interpreter + +const parse = (code: string) => { + const parsed = parseVmSource(code) + if (!parsed.ok) throw new Error(parsed.error.message) + return parsed.source +} + +const restoreOptions = { + parse, + interruption: () => { + throw new Error("no interruption") + }, +} + +/** A snapshot as JSON, to tamper with. */ +type Saved = { + readonly format: number + readonly header: Snapshot["header"] + readonly records: Array<{ readonly t: string; readonly f: Array }> +} + +/** A VM sliced once inside a loop, and its state. */ +const sliced = (code = `let n = 0\nfor (let i = 0; i < 10; i++) { try { n += i } finally { n += 0 } }\nreturn n`) => { + const vm = runtime().vm + vm.start(parse(code)) + vm.tripEvery = 3 + expect(vm.run().kind).toBe("slice") + const { snapshot, lost } = vm.capture({ strict: true }) + expect(lost).toEqual([]) + return JSON.parse(JSON.stringify(snapshot)) as Saved +} + +const frameOf = (snapshot: ReturnType) => snapshot.records.find((record) => record.t === "Frame")! + +/** A saved frame's registers, a record of their own. */ +const regsOf = (snapshot: ReturnType) => { + const ref = frameOf(snapshot).f[4] as [string, number] + return (snapshot.records[ref[1]] as unknown as { v: Array }).v +} + +const refusal = (snapshot: unknown) => { + try { + runtime().vm.restore(snapshot, restoreOptions) + } catch (error) { + expect(error).toBeInstanceOf(SnapshotError) + return (error as Error).message + } + throw new Error("the snapshot was restored") +} + +describe("snapshots", () => { + test("hold code by source and function index, with the source's bytecode hash, and never bytecode", () => { + const snapshot = sliced() + expect(snapshot.format).toBe(2) + const source = snapshot.header.sources[0]! + expect(source.id).toBe("main") + expect(source.bytecode).toBe(hashOf(compileSource(parse(source.code)))) + const frame = frameOf(snapshot) + expect(frame.f[1]).toEqual(["x", ["code", "main", 0]]) + expect(JSON.stringify(snapshot)).not.toContain('"code":[') + }) + + test("a restored VM runs to the same result", () => { + const vm = runtime().vm + vm.restore(sliced(), restoreOptions) + const stop = vm.run() + expect(stop).toEqual({ kind: "done", completion: { ok: true, value: 45 } }) + }) + + test("capturing changes nothing the run sees", () => { + const vm = runtime().vm + vm.start(parse(`let n = 0\nfor (let i = 0; i < 10; i++) n += i\nreturn n`)) + vm.tripEvery = 2 + for (let stop = vm.run(); ; stop = vm.run()) { + if (stop.kind !== "slice") { + expect(stop).toEqual({ kind: "done", completion: { ok: true, value: 45 } }) + break + } + vm.capture({ strict: true }) + } + }) + + test("the build id is stable", () => { + expect(buildId()).toBe(buildId()) + }) + + test("are refused by another build, or when a source compiles to other bytecode", () => { + expect(refusal({ ...sliced(), header: { ...sliced().header, build: "0.0.0+0" } })).toContain( + "another Code Mode build", + ) + const snapshot = sliced() + const sources = [{ ...snapshot.header.sources[0]!, bytecode: "0000000000000000" }] + expect(refusal({ ...snapshot, header: { ...snapshot.header, sources } })).toContain("other bytecode") + }) + + test("check every frame against its code before anything runs", () => { + const tamper = (change: (frame: Array) => void) => { + const snapshot = sliced() + change(frameOf(snapshot).f) + return refusal(snapshot) + } + expect(tamper((frame) => (frame[2] = 100_000))).toContain("pc") + expect(tamper((frame) => (frame[2] = 1.5))).toContain("pc") + expect(tamper((frame) => (frame[3] = 0))).toContain("stack pointer") + expect(tamper((frame) => (frame[7] = 99))).toContain("mode") + expect(tamper((frame) => (frame[1] = ["x", ["code", "main", 99]]))).toContain("unknown external value") + }) + + test("a hole on the operand stack is refused", () => { + const vm = runtime().vm + vm.start( + parse(`function f(a, b) { return a + b }\nlet s = 0\nfor (let i = 0; i < 3; i++) s = s + f(i, 1)\nreturn s`), + ) + vm.tripEvery = 1 + expect(vm.run().kind).toBe("slice") + const snapshot = JSON.parse(JSON.stringify(vm.capture({ strict: true }).snapshot)) + const regs = regsOf(snapshot) + const code = compileSource(parse(snapshot.header.sources[0].code))[0]! + expect(regs.length).toBeGreaterThan(code.locals) + regs[regs.length - 1] = ["x", "hole"] + expect(refusal(snapshot)).toContain("hole") + }) + + test("a finally tag out of range is refused", () => { + const snapshot = sliced() + const code = compileSource(parse(snapshot.header.sources[0]!.code))[0]! + regsOf(snapshot)[code.finallies[code.finallyOffsets[0]!]!] = 50 + expect(refusal(snapshot)).toContain("finally tag") + }) +}) + +describe("determinism", () => { + test("another process compiles the same bytecode and has the same build id", async () => { + const codes = Object.values(programs) + const local = { hashes: codes.map((code) => hashOf(compileSource(parse(code)))), build: (runtime(), buildId()) } + const script = ` + import { hashOf } from ${moduleUrl("../../src/vm/code.js")} + import { compileSource } from ${moduleUrl("../../src/vm/compiler/compile.js")} + import { parseVmSource } from ${moduleUrl("run.js")} + import { buildId } from ${moduleUrl("../../src/vm/snapshot.js")} + import { createBuiltins } from ${moduleUrl("../../src/vm/runtime/intrinsics.js")} + import { Interpreter } from ${moduleUrl("../../src/vm/runtime/interpreter.js")} + import { ToolRuntime } from ${moduleUrl("../../src/tool-runtime.js")} + new Interpreter({ tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins() }) + const codes = ${JSON.stringify(codes)} + console.log(JSON.stringify({ hashes: codes.map((code) => hashOf(compileSource(parseVmSource(code).source))), build: buildId() }))` + const child = Bun.spawn(["bun", "-e", script], { stdout: "pipe", stderr: "inherit" }) + expect(JSON.parse(await new Response(child.stdout).text())).toEqual(local) + expect(await child.exited).toBe(0) + }) +}) diff --git a/packages/codemode/test/vm/state.test.ts b/packages/codemode/test/vm/state.test.ts new file mode 100644 index 00000000000..176370bae4b --- /dev/null +++ b/packages/codemode/test/vm/state.test.ts @@ -0,0 +1,478 @@ +import { describe, expect, test } from "bun:test" +import { Glob } from "bun" +import path from "node:path" +import { Effect, Schema } from "effect" +import { CodeMode, Tool } from "../lib/index.js" +import { parseProgram } from "../../src/vm/runtime/execute.js" +import { Interpreter } from "../../src/vm/runtime/interpreter.js" +import { createBuiltins } from "../../src/vm/runtime/intrinsics.js" +import { GeneratorObj, type Native, nativeRule, Obj, PromiseObj, type Value } from "../../src/vm/runtime/objects.js" +import { normal } from "../../src/vm/runtime/signals.js" +import { ToolRuntime } from "../../src/tool-runtime.js" +import { Code } from "../../src/vm/code.js" +import { AsyncFromSyncIterator } from "../../src/vm/generators.js" +import { VmIntrinsic } from "../../src/vm/internal-prelude.js" +import { Cell, CodeFlags } from "../../src/vm/opcodes.js" +import type { Stop } from "../../src/vm/scheduler.js" +import { CustomIter, Frame, Mode, Vm } from "../../src/vm/vm.js" + +// The VM (`src/vm`) runs programs as bytecode frames, and keeps the jobs, promises, and host calls they wait on as +// data: a frame is its code, pc, and registers, and every suspension point is one. The Effect driver only does what +// the VM's stops ask. + +const runtimeTools = { + echo: Tool.make({ + description: "Echo a value", + input: Schema.Struct({ value: Schema.Number }), + output: Schema.Number, + execute: ({ value }) => Effect.succeed(value), + }), +} + +const runtime = CodeMode.make({ tools: runtimeTools, engine: "vm" }) + +const run = (code: string) => Effect.runPromise(runtime.execute(code)) + +const value = async (code: string) => { + const result = await run(code) + if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`) + return result.value +} + +const error = async (code: string) => { + const result = await run(code) + if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`) + return result.error +} + +// A VM for `code`, stepped by hand instead of by the Effect driver. +const vmFor = (code: string) => { + const parsed = parseProgram(code) + if (!parsed.ok) throw new Error(parsed.error.message) + const tools = ToolRuntime.make(ToolRuntime.prepare(runtimeTools), undefined, {}) + const vm = new Interpreter({ tools, builtins: createBuiltins() }).vm + vm.start(parsed.source) + return vm +} + +// The next stop that is not a slice. +const stop = (vm: Vm): Stop => { + for (;;) { + const next = vm.run() + if (next.kind !== "slice") return next + } +} + +const parked = (vm: Vm) => [...vm.scheduler.parked.keys()] + +// The value bound to `name` in the program's body, once it is parked at an `await`. +const programBinding = (vm: Vm, name: string): Value => { + const program = parked(vm).find((frame) => frame.mode === Mode.PROGRAM)! + const slot = program.code.localNames.indexOf(name) + const value = program.regs[slot] + return (value instanceof Cell ? value.v : value) as Value +} + +// Throws unless `value` is plain data: no host function or generator is reachable from it. Program values, AST +// nodes, bytecode (immutable, shared by every run), the VM's own records (back-references from what settles a +// promise), and its internal intrinsics (saved by name) are data of their own. +const requireData = (value: unknown, path: string, seen: Set): void => { + if (typeof value === "function") throw new Error(`a host function at ${path}`) + if (value === null || typeof value !== "object" || seen.has(value)) return + seen.add(value) + if (value instanceof Obj || value instanceof Code || value instanceof Vm || value instanceof VmIntrinsic) return + if (value === (value as { vm?: unknown }).vm) return + if (ArrayBuffer.isView(value)) return + const node = value as { type?: unknown; start?: unknown } + if (typeof node.type === "string" && typeof node.start === "number") return + const generator = value as { next?: unknown; throw?: unknown } + if (typeof generator.next === "function" && typeof generator.throw === "function") { + throw new Error(`a host generator at ${path}`) + } + const entries = value instanceof Map ? [...value] : Object.entries(value) + for (const [key, item] of entries) requireData(item, `${path}.${String(key)}`, seen) +} + +// Frames as saved state: every one a `Frame` whose registers, up to its stack pointer, are plain data. +const expectData = (frames: ReadonlyArray) => { + expect(frames.length).toBeGreaterThan(0) + for (const frame of frames) { + expect(frame).toBeInstanceOf(Frame) + expect(frame.pc).toBeLessThan(frame.code.code.length) + requireData({ ...frame, regs: frame.regs.slice(0, frame.sp) }, "frame", new Set()) + } +} + +describe("vm", () => { + test("runs programs as bytecode frames", async () => { + expect(await value(`const f = (n) => (n < 2 ? n : f(n - 1) + f(n - 2)); return f(12)`)).toBe(144) + }) + + test("try/catch/finally around program calls, with finally overriding the completion", async () => { + expect( + await value(` + const boom = () => { throw new Error("boom") } + const log = [] + const f = () => { try { boom() } catch (e) { log.push(e.message); return "caught" } finally { log.push("finally") } } + const g = () => { try { return f() } finally { return "override" } } + return [g(), log] + `), + ).toEqual(["override", ["boom", "finally"]]) + }) + + test("a failure in a for...of body closes the iterator and keeps the failure", async () => { + expect( + await value(` + const log = [] + const iterable = { [Symbol.iterator]() { + let i = 0 + return { next: () => ({ done: i > 3, value: i++ }), return: () => { log.push("closed"); return {} } } + } } + const check = (x) => { if (x === 2) throw new Error("at " + x) } + try { for (const x of iterable) { log.push(x); check(x) } } catch (e) { log.push(e.message) } + for (const x of iterable) { if (x === 1) break } + return log + `), + ).toEqual([0, 1, 2, "closed", "at 2", "closed"]) + }) + + test("array destructuring closes a custom iterator when a default throws", async () => { + expect( + await value(` + const log = [] + const iterable = { [Symbol.iterator]() { + return { next: () => ({ done: false, value: undefined }), return: () => { log.push("closed"); return {} } } + } } + const fail = () => { throw new Error("default") } + try { const [a = fail()] = iterable } catch (e) { log.push(e.message) } + const [b = 1, c = 2] = iterable + return [log, b, c] + `), + ).toEqual([["closed", "default", "closed"], 1, 2]) + }) + + test("labels, switch, and per-iteration bindings across program calls", async () => { + expect( + await value(` + const id = (x) => x + const out = [] + outer: for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (id(j) === 1) continue outer + switch (id(i)) { case id(0): out.push("zero"); break; case 1: out.push("one"); default: out.push("d") } + } + } + const fns = [] + for (let i = 0; i < 3; i++) fns.push(() => id(i)) + return [out, fns.map((f) => f())] + `), + ).toEqual([ + ["zero", "one", "d", "d"], + [0, 1, 2], + ]) + }) + + test("conversions call program valueOf and toString", async () => { + expect( + await value(` + const v = { valueOf: () => 2, toString: () => "key" } + const o = { [v]: 1 } + return [v + 1, \`\${v}\`, o.key, "key" in o, v * v, -v] + `), + ).toEqual([3, "key", 1, true, 4, -2]) + }) + + test("generators, async functions, and tools run on the VM", async () => { + expect( + await value(` + const log = [] + function* g() { try { yield 1; yield 2 } finally { log.push("done") } } + const it = g() + it.next() + it.return(5) + const twice = async (x) => (await tools.echo({ value: x })) * 2 + const all = await Promise.all([1, 2, 3].map(twice)) + return [log, [...g()], all] + `), + ).toEqual([ + ["done", "done"], + [1, 2], + [2, 4, 6], + ]) + }) + + test("runaway recursion stops with the location of the call that overflowed", async () => { + expect((await error(`const f = (n) => f(n + 1); return f(0)`)).message).toBe( + "RangeError: Maximum call stack size exceeded (line 1, col 18)", + ) + }) + + test("a run advances in steps: sliced at every tick, it stops many times and ends the same", () => { + const code = `let s = 0; for (let i = 0; i < 50; i++) s += [i].map((x) => x * 2)[0]; return s` + const vm = vmFor(code) + vm.tripEvery = 1 + let slices = 0 + let next = vm.run() + for (; next.kind === "slice"; next = vm.run()) slices++ + expect(slices).toBeGreaterThan(100) + expect(next).toEqual({ kind: "done", completion: normal(2450) }) + }) + + test("a large default sort runs in bounded native steps, and sorts as JS does", () => { + const words = Array.from({ length: 300_000 }, (_, i) => ((i * 7919) % 300_007).toString(36)) + const vm = vmFor(` + const words = ${JSON.stringify(words)} + const mixed = words.slice(0, 9000).concat([undefined, 10, 9, 100]) + mixed[9100] = "hole above" + const sorted = mixed.sort() + return [words.sort(), sorted.length, Array.from(sorted.slice(-6), String), 9099 in sorted, 9100 in sorted] + `) + vm.tripEvery = 1 + let slices = 0 + let next = vm.run() + for (; next.kind === "slice"; next = vm.run()) slices++ + // At least one slice per sorted run of 4,096 values, besides the merge steps. + expect(slices).toBeGreaterThan(300_000 / 4096) + const mixed: Array = words.slice(0, 9000).concat([undefined, 10, 9, 100] as Array) + mixed[9100] = "hole above" + const sorted = mixed.sort() + expect(next).toEqual({ + kind: "done", + completion: normal([ + [...words].sort(), + sorted.length, + Array.from(sorted.slice(-6), String), + 9099 in sorted, + 9100 in sorted, + ]), + }) + }) + + test("concurrent tool calls stop the VM once, with every call in one batch", () => { + const vm = vmFor(`return await Promise.all([1, 2, 3].map((value) => tools.echo({ value })))`) + const started = stop(vm) + if (started.kind !== "host") throw new Error(`expected a host stop, got ${started.kind}`) + expect( + started.calls.map((call) => ({ request: call.request, blocking: call.blocking, status: call.status })), + ).toEqual( + [1, 2, 3].map((value) => ({ + request: { kind: "tool", path: ["echo"], args: [{ value }] }, + blocking: false, + status: "queued", + })), + ) + // Nothing else can run until host work completes. + expect(stop(vm)).toEqual({ kind: "host", calls: [] }) + for (const [index, call] of [...started.calls.entries()].reverse()) vm.complete(call.id, normal((index + 1) * 10)) + expect(stop(vm)).toEqual({ kind: "done", completion: normal([10, 20, 30]) }) + expect(started.calls.map((call) => call.status)).toEqual(["completed", "completed", "completed"]) + }) + + test("a blocking host call stops the job that made it until it completes", () => { + const vm = vmFor(`const found = search({ query: "echo" }); return found.items.length`) + const started = stop(vm) + if (started.kind !== "host") throw new Error(`expected a host stop, got ${started.kind}`) + expect(started.calls.map((call) => [call.request.kind, call.blocking])).toEqual([["search", true]]) + expect(stop(vm)).toEqual({ kind: "host", calls: [] }) + vm.complete(started.calls[0]!.id, normal({ items: [1, 2], remaining: 0, next: null })) + expect(stop(vm)).toEqual({ kind: "done", completion: normal(2) }) + }) + + test("await parks the frames it cuts off the job stack as data on the promise", () => { + const vm = vmFor(` + const inner = async (x) => { const y = x + 1; return await new Promise(() => {}) } + const outer = async () => [await inner(1)] + return await outer() + `) + expect(stop(vm).kind).toBe("stuck") + const roots = parked(vm) + expect(roots.length).toBe(3) + expectData(roots) + // Each cut starts at its async boundary: the program's body, or an async function's call. + expect(roots.map((frame) => frame.mode).sort()).toEqual([Mode.ASYNC, Mode.ASYNC, Mode.PROGRAM]) + expect(vm.depth).toBe(0) + expect(vm.frames).toEqual([]) + }) + + test("a suspended generator keeps its one frame on the generator", () => { + const vm = vmFor(` + function* inner() { yield 4 } + function* g(x) { const log = []; try { log.push(yield x); yield* [2, 3]; yield* inner() } finally { log.push("f") } } + const fresh = g(0) + const array = g(1) + array.next() + array.next("a") + const nested = g(1) + for (let i = 0; i < 4; i++) nested.next() + await new Promise(() => {}) + `) + expect(stop(vm).kind).toBe("stuck") + const generator = (name: string) => { + const value = programBinding(vm, name) + if (!(value instanceof GeneratorObj)) throw new Error(`${name} is not a generator`) + return value + } + expect(generator("fresh").state).toBe("suspendedStart") + expect(generator("fresh").frame!.mode).toBe(Mode.GENERATOR) + for (const name of ["array", "nested"]) { + const frame = generator(name).frame! + expect(generator(name).state).toBe("suspendedYield") + expectData([frame]) + // `yield*` keeps the delegate's iterator record in a register, as data. + expect(frame.regs.slice(0, frame.sp).some((reg) => reg instanceof CustomIter)).toBe(true) + } + // The inner generator `nested` delegates to is suspended in its own frame. + const nested = generator("nested").frame! + const record = nested.regs + .slice(0, nested.sp) + .find((reg) => reg instanceof CustomIter && reg.iterator instanceof GeneratorObj) as CustomIter + expect((record.iterator as GeneratorObj).frame!.code.name).toBe("inner") + }) + + test("an async generator paused at an await parks its frame as data", () => { + const vm = vmFor(` + async function* ag() { const v = await new Promise(() => {}); yield v } + const it = ag() + const first = it.next() + const second = it.next() + await new Promise(() => {}) + `) + expect(stop(vm).kind).toBe("stuck") + const it = programBinding(vm, "it") + if (!(it instanceof GeneratorObj)) throw new Error("expected a generator") + expect(it.state).toBe("executing") + expect(it.queue.length).toBe(2) + expectData(parked(vm)) + expect( + parked(vm) + .map((frame) => frame.mode) + .sort(), + ).toEqual([Mode.GENERATOR, Mode.PROGRAM]) + expect(vm.frames).toEqual([]) + }) + + test("for await over a synchronous iterable keeps its adapter and reactions as data", () => { + const vm = vmFor(` + const never = new Promise(() => {}) + const log = [] + const iterable = { [Symbol.iterator]() { let i = 0; return { next: () => ({ done: false, value: i++ ? never : 1 }) } } } + for await (const x of iterable) log.push(x) + `) + // No native function is made while the program runs: the adapter's methods are intrinsics. + const made: Array = [] + nativeRule.observe = (native) => made.push(native) + try { + expect(stop(vm).kind).toBe("stuck") + } finally { + nativeRule.observe = undefined + } + expect(made).toEqual([]) + const never = programBinding(vm, "never") + if (!(never instanceof PromiseObj)) throw new Error("expected a promise") + expect(never.reactions.map((reaction) => reaction.kind)).toEqual(["asyncFromSync"]) + const reaction = never.reactions[0]! + for (const field of Object.values(reaction)) expect(typeof field).not.toBe("function") + if (reaction.kind !== "asyncFromSync") throw new Error("expected an adapter reaction") + expect(reaction.done).toBe(false) + // The iterator it would close on a rejection is the program's own, as a record. + expect(reaction.close).toMatchObject({ asynchronous: false }) + expectData(parked(vm)) + const program = parked(vm).find((frame) => frame.mode === Mode.PROGRAM)! + const loop = program.regs.find((reg) => reg instanceof CustomIter) as CustomIter + expect(loop.iterator).toBeInstanceOf(AsyncFromSyncIterator) + }) + + test("the VM does not import Effect", async () => { + const root = path.join(import.meta.dir, "../..") + // Glob paths use the platform's separator. + const files = (await Array.fromAsync(new Glob("src/vm/**/*.ts").scan({ cwd: root }))).filter( + (file) => !file.split(path.sep).join("/").startsWith("src/vm/runtime/"), + ) + expect(files.length).toBeGreaterThan(0) + for (const file of files) { + expect(await Bun.file(path.join(root, file)).text()).not.toMatch(/from "effect"/) + } + }) +}) + +describe("built-ins as data", () => { + test("every frame on the job stack is data at every step", () => { + const vm = vmFor(` + const log = [] + const box = (v) => ({ valueOf() { log.push("valueOf"); return v }, toString() { return "box" + v } }) + const xs = Array.from({ length: 6 }, (_, i) => (i * 7) % 6) + const sorted = xs.map((x) => x * 2).filter((x) => x !== 4).sort((a, b) => b - a) + const found = [xs.find((x) => x > 3), xs.findLast((x) => x < 3), xs.some((x) => x > 4), xs.every((x) => x >= 0)] + const flat = xs.flatMap((x) => [x, -x]).reduce((a, b) => a + b, 0) + xs.reduceRight((a, b) => a + b) + xs.forEach((x) => log.push(x)) + function* gen(n) { for (let i = 0; i < n; i++) yield [String(i), i] } + const map = new Map(gen(3)) + const set = new Set(Array.from(gen(3), ([k]) => k)) + const weak = new WeakMap([[log, 1]]) + map.forEach((v, k) => log.push(k)) + const setLike = { size: box(2), has: (v) => v === "1", keys: () => ["1", "9"][Symbol.iterator]() } + const ops = [set.union(setLike).size, set.intersection(setLike).size, set.isSubsetOf(setLike), set.isDisjointFrom(setLike)] + const groups = [Object.groupBy(xs, (x) => (x % 2 ? box("odd") : "even")), Map.groupBy(xs, (x) => x % 3).size] + const entries = Object.fromEntries(gen(2)) + const iter = gen(5).map((pair) => pair[1]).filter((n) => n % 2 === 0).flatMap((n) => [n, n]).take(5).drop(1) + const helpers = [iter.toArray(), gen(4).map(([, n]) => n).reduce((a, b) => a + b), Iterator.from({ next: () => ({ done: true }) }).toArray()] + const json = JSON.stringify({ a: box(1), d: { toJSON: (k) => "key " + k }, list: [1, undefined] }, (k, v) => (k === "a" ? String(v) : v), 1) + const parsed = JSON.parse('{"a":[1,2,{"b":3}]}', (k, v) => (typeof v === "number" ? v * 10 : v)) + const text = "a-b-c".replace(/-/g, (m) => "+").replaceAll("+", () => "_") + "x".padStart(box(3), "0") + const dates = [new Date(box(0)).getTime(), Date.UTC(box(2000), 0), new Date(0).setFullYear(box(1999))] + const numbers = [Math.max(box(5), 2), Number(box(7)), String(box(1)), [box(1), 2].join(box("|"))] + const calls = [Math.max.call(null, 1, 2), ((a, b) => a + b).apply(null, [1, 2]), ((a) => a).bind(null, 9)()] + const params = String(new URLSearchParams(gen(2))) + new Headers([["x", "1"]]).get("x") + const aggregate = new AggregateError(gen(2), box("many")).errors.length + const error = new Error(box("boxed")).message + const work = async (i) => { await null; return i * 2 } + const all = await Promise.all([work(1), work(2), 3]) + const settled = await Promise.allSettled([work(1), Promise.reject(new Error("no"))]) + const raced = await Promise.race([work(5), new Promise((resolve) => resolve("first"))]) + const any = await Promise.any([Promise.reject(1), work(4)]) + const tried = await Promise.try(() => work(6)) + const thenned = await work(1).then((v) => v + 1).catch(() => 0).finally(() => log.push("finally")) + const thenable = await { then: (resolve) => resolve("thenable") } + const tool = await tools.echo({ value: work(10) }) + const found2 = search({ query: "echo" }).items.length + async function* agen() { yield 1; yield await work(1) } + const collected = [] + for await (const v of agen()) collected.push(v) + return { all, raced, any, tried, thenned, tool, found2, collected } + `) + vm.tripEvery = 1 + const sources = new Set() + let steps = 0 + let next = vm.run() + for (; next.kind !== "done"; next = vm.run()) { + steps++ + if (vm.frames.length > 0) expectData(vm.frames) + for (const frame of vm.frames) { + if ((frame.code.flags & CodeFlags.INTERNAL) !== 0) sources.add(frame.code.source.id) + } + if (next.kind === "stuck") throw new Error("the run is stuck") + if (next.kind !== "host") continue + for (const call of next.calls) { + const request = call.request + if (request.kind === "tool") vm.complete(call.id, normal((request.args[0] as { value: number }).value)) + else vm.complete(call.id, normal({ items: [1], remaining: 0, next: null })) + } + } + expect(steps).toBeGreaterThan(100) + // Built-ins that call program code ran as prelude and internal bytecode frames, as data. + expect([...sources].sort()).toEqual(["@prelude", "@vm-internal"]) + expect(next).toEqual({ + kind: "done", + completion: normal({ + all: [2, 4, 3], + raced: "first", + any: 8, + tried: 12, + thenned: 3, + tool: 20, + found2: 1, + collected: [1, 2], + }), + }) + }) +}) diff --git a/packages/codemode/test/vm/verify.ts b/packages/codemode/test/vm/verify.ts new file mode 100644 index 00000000000..0dce361a801 --- /dev/null +++ b/packages/codemode/test/vm/verify.ts @@ -0,0 +1,358 @@ +import type { Code } from "../../src/vm/code.js" +import { CheckBaseKind, CondKind, ImportKind, Op, OpNames } from "../../src/vm/opcodes.js" + +/** + * Stack-depth verifier (C5): runs abstract interpretation over packed bytecode independent of `CodeBuilder`, + * proving a single static operand depth at every reachable pc, `0 <= depth <= maxStack`, valid handler and + * `END_FINALLY` entry depths, T&S successor depths, and valid local/upvalue slot bounds. + */ +export const verifyCode = (c: Code): Array => { + const errors: Array = [] + const label = `${c.source.id}#${c.index}(${c.name || ""})` + const words = c.code + const depth = new Int32Array(words.length).fill(-1) + const work: Array = [] + + const at = (pc: number, d: number, from: number) => { + if (pc < 0 || pc >= words.length) { + errors.push(`${label}: pc ${pc} out of range (from ${from})`) + return + } + if (pc > 0 && (words[pc - 1]! & 0xff) === Op.EXTRA) { + errors.push(`${label}: jump into middle of EXTRA sequence at pc ${pc} (from ${from})`) + return + } + if (d < 0) { + errors.push(`${label}: negative depth ${d} at pc ${pc} (from ${from})`) + return + } + if (d > c.maxStack) { + errors.push(`${label}: depth ${d} > maxStack ${c.maxStack} at pc ${pc}`) + } + if (depth[pc] === -1) { + depth[pc] = d + work.push(pc) + } else if (depth[pc] !== d) { + errors.push(`${label}: depth conflict at pc ${pc}: ${depth[pc]} vs ${d} (from ${from})`) + } + } + + if (words.length > 0) at(0, 0, -1) + + const h = c.handlers + for (let i = 0; i < h.length; i += 4) { + at(h[i + 2]!, h[i + 3]! + 1, -2) + } + + const checkSlot = (s: number, pc: number) => { + if (s < 0 || s >= c.locals) { + errors.push(`${label}: local slot ${s} out of [0, ${c.locals}) at pc ${pc}`) + } + } + + const checkUpval = (u: number, pc: number) => { + const count = c.captures.length >>> 1 + if (u < 0 || u >= count) { + errors.push(`${label}: upval ${u} out of [0, ${count}) at pc ${pc}`) + } + } + + while (work.length > 0) { + const pc = work.pop()! + const d = depth[pc]! + const w = words[pc]! + let op = w & 0xff + let stride = 1 + let a = (w >>> 8) & 0xff + let bx = w >>> 16 + if (op === Op.EXTRA) { + if (pc + 1 >= words.length) { + errors.push(`${label}: trailing EXTRA at pc ${pc}`) + continue + } + const w1 = words[pc + 1]! + if ((w1 & 0xff) === Op.EXTRA) { + if (pc + 2 >= words.length) { + errors.push(`${label}: trailing EXTRA at pc ${pc + 1}`) + continue + } + const w2 = words[pc + 2]! + op = w2 & 0xff + a = ((w >>> 16) << 16) | (w1 & 0xff00) | ((w2 >>> 8) & 0xff) + bx = ((w1 >>> 16) << 16) | (w2 >>> 16) + stride = 3 + } else { + op = w1 & 0xff + a = (w & 0xff00) | ((w1 >>> 8) & 0xff) + bx = ((w >>> 16) << 16) | (w1 >>> 16) + stride = 2 + } + if (op === Op.EXTRA) { + errors.push(`${label}: more than two EXTRA prefixes at pc ${pc}`) + continue + } + } + const next = (delta: number) => at(pc + stride, d + delta, pc) + + switch (op) { + case Op.PUSH_UNDEF: + case Op.PUSH_NULL: + case Op.PUSH_BOOL: + case Op.PUSH_INT: + case Op.PUSH_CONST: + case Op.DUP: + case Op.GET_INTRINSIC: + case Op.ARGS: + case Op.CLOSURE: + case Op.NEW_OBJECT: + case Op.TEMPLATE: + case Op.REGEXP: + next(1) + break + case Op.GET_GLOBAL: + next(a === 2 ? -1 : 1) + break + case Op.IMPORT: + next(a === ImportKind.SIDE_EFFECT ? 0 : a === ImportKind.DYNAMIC ? 0 : 1) + break + case Op.PICK: + if (a >= d) errors.push(`${label}: PICK ${a} at depth ${d} (pc ${pc})`) + next(1) + break + case Op.ROT: + if (a >= d) errors.push(`${label}: ROT ${a} at depth ${d} (pc ${pc})`) + next(0) + break + case Op.GET_LOCAL: + case Op.GET_CELL: + checkSlot(bx, pc) + next(1) + break + case Op.PUT_LOCAL: + case Op.PUT_CELL: + checkSlot(bx, pc) + next(-1) + break + case Op.HOLE_LOCAL: + case Op.NEW_CELL: + case Op.COPY_CELL: + checkSlot(bx, pc) + next(0) + break + case Op.GET_UPVAL: + checkUpval(bx, pc) + next(1) + break + case Op.PUT_UPVAL: + checkUpval(bx, pc) + next(-1) + break + case Op.POP: + case Op.PUT_PROP: + case Op.COPY_DATA_PROPS: + case Op.IN: + case Op.INSTANCEOF: + case Op.ADD: + case Op.SUB: + case Op.MUL: + case Op.LT: + case Op.LE: + case Op.GT: + case Op.GE: + case Op.SEQ: + case Op.SNE: + case Op.BINOP: + case Op.DELETE_ELEM: + case Op.NEW_SPREAD: + next(-1) + break + case Op.POPN: + case Op.COPY_REST: + next(-bx) + break + case Op.SET_NAME: + case Op.DELETE_PROP: + case Op.CHECK_BASE: + case Op.TO_KEY: + case Op.UNARY: + case Op.NOT: + case Op.TO_NUMERIC: + case Op.INC: + case Op.TO_STRING: + case Op.NOP: + case Op.FOR_IN: + case Op.ITER_INIT: + case Op.GENERATOR_START: + if (op === Op.CHECK_BASE && a === CheckBaseKind.READ && d < 2) { + errors.push(`${label}: CHECK_BASE.read at depth ${d} (pc ${pc})`) + } + next(0) + break + case Op.GET_PROP: + next(a === 1 ? 1 : 0) + break + case Op.GET_ELEM: + next(a === 1 ? 0 : -1) + break + case Op.PUT_ELEM: + case Op.CALL_SPREAD: + next(-2) + break + case Op.DEFINE_FIELD: + next(a === 0 ? -1 : -2) + break + case Op.NEW_ARRAY: + case Op.CONCAT: + next(1 - bx) + break + case Op.ARRAY_APPEND: + next(a === 1 ? 0 : -1) + break + case Op.JUMP: + case Op.LOOP: + at(bx, d, pc) + break + case Op.JUMP_IF_FALSE: + case Op.JUMP_IF_TRUE: + at(bx, d - 1, pc) + next(-1) + break + case Op.JUMP_COND: + if (a === CondKind.NULLISH) { + at(bx, d, pc) + next(0) + } else if (a === CondKind.NULLISH_METHOD) { + at(bx, d - 1, pc) + next(0) + } else { + at(bx, d, pc) + next(-1) + } + break + case Op.THROW: + case Op.RETURN: + case Op.END_PROGRAM: + if (d < 1) errors.push(`${label}: ${OpNames[op]} on empty stack at pc ${pc}`) + break + case Op.THROW_ERROR: + break + case Op.END_FINALLY: { + if (bx < 0 || bx >= c.finallyOffsets.length) { + errors.push(`${label}: END_FINALLY level ${bx} out of range at pc ${pc}`) + break + } + const off = c.finallyOffsets[bx]! + const fin = c.finallies + const tagSlot = fin[off]! + const valSlot = fin[off + 1]! + const entryDepth = fin[off + 2]! + const n = fin[off + 3]! + if (d !== entryDepth) { + errors.push(`${label}: END_FINALLY ${bx} at depth ${d}, expected ${entryDepth} (pc ${pc})`) + } + checkSlot(tagSlot, pc) + checkSlot(valSlot, pc) + for (let k = 0; k < n; k++) { + const target = fin[off + 4 + k]! + if (k !== 1 && target >= 0) at(target, entryDepth, pc) + } + break + } + case Op.CALL: + if (d < a + 2) errors.push(`${label}: CALL ${a} at depth ${d} (pc ${pc})`) + next(-(a + 1)) + break + case Op.NEW: + if (a === 255) { + if (d < 1) errors.push(`${label}: NEW.check at depth ${d} (pc ${pc})`) + next(0) + } else { + if (d < a + 1) errors.push(`${label}: NEW ${a} at depth ${d} (pc ${pc})`) + next(-a) + } + break + case Op.ITER_OPEN: { + const jumpPc = pc + stride + const jumpStride = jumpPc < words.length && (words[jumpPc]! & 0xff) === Op.EXTRA ? 2 : 1 + if (jumpPc + jumpStride > words.length || (words[jumpPc + jumpStride - 1]! & 0xff) !== Op.JUMP) { + errors.push(`${label}: T&S ITER_OPEN at pc ${pc} not followed by JUMP`) + break + } + at(jumpPc, d + 1, pc) + at(jumpPc + jumpStride, d, pc) + break + } + case Op.ITER_STEP: { + checkSlot(a, pc) + const jumpPc = pc + stride + const jumpStride = jumpPc < words.length && (words[jumpPc]! & 0xff) === Op.EXTRA ? 2 : 1 + if (jumpPc + jumpStride > words.length || (words[jumpPc + jumpStride - 1]! & 0xff) !== Op.JUMP) { + errors.push(`${label}: T&S ITER_STEP at pc ${pc} not followed by JUMP`) + break + } + at(jumpPc, d + 2, pc) + at(jumpPc + jumpStride, d + 1, pc) + at(bx, d, pc) + break + } + case Op.ITER_RESULT: + checkSlot(a, pc) + next(0) + at(bx, d - 1, pc) + break + case Op.ITER_CLOSE: + checkSlot(a, pc) + next(2) + at(bx, d, pc) + break + case Op.ITER_APPEND_ALL: + checkSlot(a, pc) + next(0) + at(bx, d, pc) + break + case Op.AWAIT: + if (d < 1) errors.push(`${label}: AWAIT on empty stack at pc ${pc}`) + next(0) + break + case Op.YIELD: + if (d < 1) errors.push(`${label}: YIELD on empty stack at pc ${pc}`) + next(1) + break + case Op.RESUME: + if (d < 2) errors.push(`${label}: RESUME at depth ${d} (pc ${pc})`) + at(bx, d - 1, pc) + next(-1) + break + case Op.YSTAR: + checkSlot(a, pc) + if (d < 2) errors.push(`${label}: YSTAR at depth ${d} (pc ${pc})`) + at(bx, d + 1, pc) + next(-1) + break + case Op.YSTAR_RESULT: + checkSlot(a, pc) + if (d < 1) errors.push(`${label}: YSTAR_RESULT on empty stack at pc ${pc}`) + at(bx, d, pc) + next(0) + break + default: + errors.push(`${label}: unhandled op ${OpNames[op] ?? op} at pc ${pc}`) + break + } + } + + for (let i = 0; i < h.length; i += 4) { + const start = h[i]! + const end = h[i + 1]! + const handlerDepth = h[i + 3]! + for (let pc = start; pc < end; pc++) { + if (depth[pc]! !== -1 && depth[pc]! < handlerDepth) { + errors.push(`${label}: pc ${pc} depth ${depth[pc]} < handler depth ${handlerDepth}`) + } + } + } + + return errors +} + +export const verifyAll = (codes: ReadonlyArray): Array => codes.flatMap((c) => verifyCode(c)) diff --git a/packages/codemode/test/vm/vm.test.ts b/packages/codemode/test/vm/vm.test.ts new file mode 100644 index 00000000000..5570bab289c --- /dev/null +++ b/packages/codemode/test/vm/vm.test.ts @@ -0,0 +1,585 @@ +import { describe, expect, test } from "bun:test" +import { disassembleSource } from "../../script/disasm.js" +import { hashOf } from "../../src/vm/code.js" +import { compileSource } from "../../src/vm/compiler/compile.js" +import { verifyAll } from "./verify.js" +import { parseVmSource, runProgram, warmupVm } from "./run.js" + +const val = (code: string) => { + const parsed = parseVmSource(code) + if (parsed.ok) { + const errors = verifyAll(compileSource(parsed.source)) + expect(errors).toEqual([]) + } + const res = runProgram(code) + if (!res.ok) throw new Error(`${res.error.kind}: ${res.error.message}`) + return res.value +} + +const err = (code: string) => { + const res = runProgram(code) + if (res.ok) throw new Error(`Expected failure, got ${JSON.stringify(res.value)}`) + return res.error +} + +describe("VM compiler, verifier, and determinism", () => { + test("compiles deterministically and passes the stack-depth verifier", () => { + const src = ` + function fib(n) { return n < 2 ? n : fib(n - 1) + fib(n - 2) } + const fns = [] + for (let i = 0; i < 3; i++) fns.push(() => i) + try { + return [fib(6), fns.map((f) => f())] + } catch (e) { + return null + } finally { + fns.length = 0 + } + ` + const p1 = parseVmSource(src) + const p2 = parseVmSource(src) + expect(p1.ok && p2.ok).toBe(true) + if (!p1.ok || !p2.ok) return + const c1 = compileSource(p1.source, { noCache: true }) + const c2 = compileSource(p2.source, { noCache: true }) + expect(hashOf(c1)).toBe(hashOf(c2)) + expect(verifyAll(c1)).toEqual([]) + }) + + test("disassembler formats bytecode, locals, upvalues, handlers, and finallies", () => { + const out = disassembleSource(` + const fns = [] + for (let i = 0; i < 3; i++) fns.push(() => i) + try { return fns[1]() } catch (e) { return -1 } finally { fns.pop() } + `) + expect(out).toContain("fn#0 ") + expect(out).toContain("COPY_CELL") + expect(out).toContain("CLOSURE") + expect(out).toContain("END_FINALLY") + expect(out).toContain("GET_UPVAL") + }) + + test("forceExtra mode exercises EXTRA-prefixed 32-bit operands", () => { + const src = ` + let total = 0 + const add = (a, b = 10) => a + b + for (let i = 0; i < 10; i++) total += add(i) + return total + ` + const parsed = parseVmSource(src) + expect(parsed.ok).toBe(true) + if (!parsed.ok) return + const codes = compileSource(parsed.source, { forceExtra: true }) + expect(verifyAll(codes)).toEqual([]) + expect(val(src)).toBe(145) + }) +}) + +describe("scopes, TDZ, hoisting, and closures", () => { + test("var hoisting, function hoisting, and block-scoped function declarations", () => { + expect( + val(` + const before = [typeof hoistedFn, hoistedVar] + var hoistedVar = 42 + function hoistedFn() { return "top" } + function hoistedFn() { return "second" } + let blockVal + { + blockVal = inner() + function inner() { return "inner" } + } + return [before, hoistedVar, hoistedFn(), blockVal] + `), + ).toEqual([["function", null], 42, "second", "inner"]) + }) + + test("always-checked TDZ for let and const, including hoisted functions and loop blocks", () => { + expect(err(`const x = x + 1`).message).toContain("Cannot access 'x' before initialization") + expect(err(`{ f(); let x = 1; function f() { return x } }`).message).toContain( + "Cannot access 'x' before initialization", + ) + expect(err(`{ x = 1; const x = 2 }`).message).toContain("Cannot access 'x' before initialization") + expect(err(`const x = 1; x = 2`).message).toContain("Cannot assign to constant 'x'") + }) + + test("per-iteration let bindings in for, for-in, and for-of loops", () => { + expect( + val(` + const a = [] + for (let i = 0; i < 4; i++) a.push(() => i) + const b = [] + for (const k in { x: 1, y: 2 }) b.push(() => k) + const c = [] + for (let v of [10, 20, 30]) { + c.push(() => v) + v += 1 + } + return [a.map((f) => f()), b.map((f) => f()), c.map((f) => f())] + `), + ).toEqual([ + [0, 1, 2, 3], + ["x", "y"], + [11, 21, 31], + ]) + }) + + test("parameter scopes, TDZ between defaults, and parameter -> body var copy", () => { + expect( + val(` + const outer = "outer" + function f(a = () => outer, x = 1, b = () => x) { + var outer = "inner" + var x = 2 + return [a(), x, b()] + } + return f() + `), + ).toEqual(["outer", 2, 1]) + + expect(err(`function f(a = b, b = 1) { return a }; return f()`).message).toContain( + "Cannot access 'b' before initialization", + ) + }) + + test("arguments, rest parameters, duplicate non-strict params, and named function expressions", () => { + expect( + val(` + function dup(a, a) { return a } + function withArgs(a, b = 1) { + var arguments + const arrow = () => [...arguments] + return [arguments.length, arrow()] + } + const named = function self(n) { + return n <= 0 ? "done" : self(n - 1) + } + return [dup(1, 2), withArgs(10, 20, 30), named(2)] + `), + ).toEqual([2, [3, [10, 20, 30]], "done"]) + + expect(err(`const f = function me() { me = 1 }; f()`).message).toContain("Cannot assign to constant 'me'") + }) +}) + +describe("expressions, evaluation order, and destructuring", () => { + test("member evaluation order: nullish base before key conversion, base check before rhs", () => { + expect(err(`const base = null; base[{ toString() { throw new RangeError("boom") } }]`).message).toContain( + "Cannot read properties of null", + ) + expect(err(`(1).x = (() => { throw new RangeError("rhs ran") })()`).message).toContain( + "Cannot create property 'x' on number '1'", + ) + expect( + val(` + const log = [] + const key = { toString() { log.push("key"); return "a" } } + const o = { a: 10 } + o[key] += (log.push("rhs"), 5) + const post = o[key]++ + const pre = ++o[key] + return [o.a, post, pre, log] + `), + ).toEqual([17, 15, 17, ["key", "rhs", "key", "key"]]) + }) + + test("optional chaining and short-circuiting", () => { + expect( + val(` + const o = { a: { b: (x) => ({ c: x * 2 }) } } + const n = null + return [ + o?.a.b(21).c, + n?.a.b(1).c, + o.a.missing?.(1), + delete n?.a.b, + ] + `), + ).toEqual([42, null, null, true]) + }) + + test("object and array destructuring with defaults, rest, and custom iterator close", () => { + expect( + val(` + const closed = [] + const iterable = { + [Symbol.iterator]() { + let i = 0 + return { + next() { return { value: ++i, done: false } }, + return() { closed.push(i); return {} }, + } + } + } + const [first, , third] = iterable + const { a = 1, b: { c = 2 } = {}, ...rest } = { b: { c: 3 }, x: 4, y: 5 } + return [first, third, closed, a, c, rest] + `), + ).toEqual([1, 3, [3], 1, 3, { x: 4, y: 5 }]) + }) +}) + +describe("try/catch/finally static tag routing", () => { + test("all synchronous spike finally cases pass and verify", () => { + expect( + val(` + const out = [] + function f() { try { out.push("t"); return "r" } finally { out.push("f") } } + out.push(f()) + return out.join(",") + `), + ).toBe("t,f,r") + + expect( + val(` + const out = [] + function f() { try { out.push("t"); throw "e" } finally { out.push("f") } } + try { f() } catch (e) { out.push("c:" + e) } + return out.join(",") + `), + ).toBe("t,f,c:e") + + expect( + val(` + const out = [] + outer: for (let i = 0; i < 4; i++) { + try { + for (let j = 0; j < 3; j++) { + try { + try { + if (j === 1) continue + if (i === 1 && j === 2) continue outer + if (i === 2 && j === 2) break outer + out.push(i + ":" + j) + } finally { out.push("a" + j) } + } finally { out.push("b" + j) } + } + } finally { out.push("c" + i) } + } + out.push("end") + return out.join(",") + `), + ).toBe("0:0,a0,b0,a1,b1,0:2,a2,b2,c0,1:0,a0,b0,a1,b1,a2,b2,c1,2:0,a0,b0,a1,b1,a2,b2,c2,end") + + expect( + val(` + const out = [] + function f() { try { throw "e" } finally { return "fin" } } + out.push(f()) + function g() { try { return "a" } finally { return "b" } } + out.push(g()) + return out.join(",") + `), + ).toBe("fin,b") + + expect( + val(` + const out = [] + function f() { + try { return "r" } finally { + try { throw "x" } catch (e) { out.push("c" + e) } + out.push("after") + } + } + out.push(f()) + function g(n) { + for (let i = 0; i < 3; i++) { + try { if (i === n) return "ret" + i } finally { + try { if (i === 1) throw "boom" } catch (e) { out.push("caught " + e) } finally { out.push("inner" + i) } + } + } + return "none" + } + out.push(g(1)) + out.push(g(5)) + return out.join(",") + `), + ).toBe("cx,after,r,inner0,caught boom,inner1,ret1,inner0,caught boom,inner1,inner2,none") + + expect( + val(` + const out = [] + function f() { + try { throw "outer" } finally { + try { try { throw "inner" } finally { out.push("if") } } catch (e) { out.push("caught " + e) } + out.push("of") + } + } + try { f() } catch (e) { out.push("final " + e) } + return out.join(",") + `), + ).toBe("if,caught inner,of,final outer") + + expect( + val(` + const out = [] + for (let i = 0; i < 3; i++) { try { throw "e" + i } finally { out.push("f" + i); break } } + out.push("done") + let k = 0 + while (k < 3) { k++; try { try { throw "x" } finally { out.push("in" + k); continue } } finally { out.push("out" + k) } } + out.push("k" + k) + return out.join(",") + `), + ).toBe("f0,done,in1,out1,in2,out2,in3,out3,k3") + + // Spike cases 8-12: generators + yield in try/finally + for-of + expect( + val(` + const out = [] + function* g() { try { yield 1; yield 2 } finally { out.push("cleanup") } out.push("unreached") } + const it = g() + out.push(it.next().value) + const r = it.return(9) + out.push(r.value + "/" + r.done) + out.push(String(it.next().done)) + const it2 = g() + const r2 = it2.return(3) + out.push(r2.value + "/" + r2.done) + return out.join(",") + `), + ).toBe("1,cleanup,9/true,true,3/true") + + expect( + val(` + const out = [] + function* g() { try { yield 1 } finally { yield "f"; out.push("after-f") } } + const it = g() + it.next() + let r = it.return(9) + out.push(r.value + "/" + r.done) + r = it.next() + out.push(r.value + "/" + r.done) + r = it.next() + out.push(String(r.value) + "/" + r.done) + const it2 = g() + it2.next() + it2.return(1) + r = it2.return(2) + out.push(r.value + "/" + r.done) + return out.join(",") + `), + ).toBe("f/false,after-f,9/true,undefined/true,2/true") + + expect( + val(` + const out = [] + function* g() { try { yield 1 } finally { try { yield "f" } catch (e) { out.push("caught " + e) } out.push("end-f") } } + const it = g() + it.next() + let r = it.return(5) + out.push(r.value + "/" + r.done) + r = it.throw("boom") + out.push(r.value + "/" + r.done) + function* g2() { try { yield 1 } finally { yield 2; out.push("g2-after") } } + const it2 = g2() + it2.next() + r = it2.throw("x") + out.push(r.value + "/" + r.done) + try { it2.next() } catch (e) { out.push("rethrown " + e) } + r = it2.next() + out.push(String(r.value) + "/" + r.done) + return out.join(",") + `), + ).toBe("f/false,caught boom,end-f,5/true,2/false,g2-after,rethrown x,undefined/true") + + expect( + val(` + const out = [] + function* g() { try { const v = 1 + (yield 2); out.push(v) } finally { out.push("f") } return "end" } + const it = g() + out.push(it.next().value) + let r = it.return(7) + out.push(r.value + "/" + r.done) + const it2 = g() + it2.next() + r = it2.next(10) + out.push(r.value + "/" + r.done) + function* h() { const xs = [1, 2]; try { xs.push(3 + (yield xs.length) * (yield 5)) } finally { out.push("h" + xs.join("")) } } + const it3 = h() + it3.next() + it3.next(4) + r = it3.return(8) + out.push(r.value + "/" + r.done) + return out.join(",") + `), + ).toBe("2,f,7/true,11,f,end/true,h12,8/true") + + expect( + val(` + const out = [] + function* src() { try { yield 1; yield 2; yield 3 } finally { out.push("src-closed") } } + function f() { + for (const x of src()) { + try { if (x === 2) return x * 10 } finally { out.push("f" + x) } + } + return "no" + } + out.push(f()) + function g() { + for (const x of [1, 2, 3]) { try { if (x === 2) return "arr" + x } finally { out.push("af" + x) } } + } + out.push(g()) + function* outer() { + try { for (const x of src()) { try { yield x } finally { out.push("of" + x) } } } finally { out.push("outer-f") } + } + const it = outer() + it.next() + it.next() + const r = it.return(42) + out.push(r.value + "/" + r.done) + function h() { + for (const x of src()) { for (const y of [7, 8]) { try { if (x === 1 && y === 8) break } finally { out.push("y" + y) } } if (x === 2) break } + return "h" + } + out.push(h()) + function t() { + try { for (const x of src()) { if (x === 2) throw "thrown" + x } } catch (e) { out.push("t:" + e) } + } + t() + return out.join(",") + `), + ).toBe( + "f1,f2,src-closed,20,af1,af2,arr2,of1,of2,src-closed,outer-f,42/true,y7,y8,y7,y8,src-closed,h,src-closed,t:thrown2", + ) + }) +}) + +describe("generators, async functions, async generators, yield*, and for-await", () => { + test("verifies and executes sync/async generators, yield*, for-await, and Promise combinators (including forceExtra)", () => { + const src = ` + function* innerSync() { + const x = yield 10 + return x * 2 + } + function* outerSync() { + const r = yield* innerSync() + yield r + 1 + } + async function* innerAsync() { + yield await Promise.resolve(1) + return Promise.resolve(5) + } + async function* outerAsync() { + const d = yield* innerAsync() + yield d * 10 + } + const syncOut = [] + const g = outerSync() + syncOut.push(g.next().value) + syncOut.push(g.next(3).value) + const asyncOut = [] + for await (const item of outerAsync()) { + asyncOut.push(item) + } + const all = await Promise.all([Promise.resolve("a"), "b", Promise.try(() => "c")]) + const iterHelper = [1, 2, 3, 4].values().map((x) => x * 10).filter((x) => x > 10).toArray() + return [syncOut, asyncOut, all, iterHelper] + ` + const parsed = parseVmSource(src) + expect(parsed.ok).toBe(true) + if (!parsed.ok) return + expect(verifyAll(compileSource(parsed.source, { forceExtra: true }))).toEqual([]) + const dis = disassembleSource(src) + expect(dis).toContain("GENERATOR_START") + expect(dis).toContain("YIELD") + expect(dis).toContain("RESUME") + expect(dis).toContain("AWAIT") + expect(dis).toContain("YSTAR") + expect(dis).toContain("YSTAR_RESULT") + expect(val(src)).toEqual([ + [10, 7], + [1, 50], + ["a", "b", "c"], + [20, 30, 40], + ]) + }) +}) + +describe("built-ins, prelude, and coercion", () => { + test("array callbacks, sort, Map/Set, Object.groupBy, JSON reviver/replacer, and ToPrimitive", () => { + warmupVm(2) + expect( + val(` + const arr = Array.from({ length: 6 }, (_, i) => i + 1) + const mapped = arr.map((x) => x * 2).filter((x) => x > 4) + const sum = mapped.reduce((a, b) => a + b, 0) + const sorted = [3, 1, 4, 1, 5].sort((a, b) => a - b) + const grouped = Object.groupBy(["a", "bb", "c", "dd"], (s) => s.length) + const parsed = JSON.parse('{"a":1,"b":2}', (k, v) => (typeof v === "number" ? v * 10 : v)) + const str = JSON.stringify({ x: 1, y: { toJSON() { return 99 } } }) + const coerced = { valueOf() { return 7 } } * 6 + return [mapped, sum, sorted, grouped, parsed, str, coerced] + `), + ).toEqual([ + [6, 8, 10, 12], + 36, + [1, 1, 3, 4, 5], + { "1": ["a", "c"], "2": ["bb", "dd"] }, + { a: 10, b: 20 }, + '{"x":1,"y":99}', + 42, + ]) + }) + + test("Function.prototype.call, apply, bind, and built-in callbacks", () => { + expect( + val(` + function greet(prefix, suffix) { + return prefix + ":" + this.name + ":" + suffix + } + const bound = greet.bind({ name: "vm" }, "hi") + return [ + greet.call({ name: "c" }, "a", "b"), + greet.apply({ name: "ap" }, ["x", "y"]), + bound("!"), + bound.name, + bound.length, + [-1, 2, -3].map(Math.abs), + ["1", "2"].map(Number.parseInt), + ["a", "b"].map(Error).map((e) => e.message), + ] + `), + ).toEqual(["a:c:b", "x:ap:y", "hi:vm:!", "bound greet", 1, [1, 2, 3], [1, null], ["a", "b"]]) + }) + + test("new expressions, Set composition, Uint8Array, String callbacks, and imports", () => { + expect( + val(` + import { URL as ImportedURL } from "node:url" + const s1 = new Set([1, 2, 3]) + const s2 = new Set([2, 3, 4]) + const bytes = Uint8Array.from([1, 2, 3], (x) => x * 10) + const replaced = "a1b22c".replace(/(\\d+)/g, (m) => "[" + m + "]") + const u = new ImportedURL("https://example.com/p?q=1") + const errWithCause = new TypeError("bad", { cause: "root" }) + return [ + [...s1.union(s2)], + [...s1.intersection(s2)], + [...s1.difference(s2)], + [...bytes.map((b) => b + 1)], + replaced, + u.searchParams.get("q"), + errWithCause.cause, + ] + `), + ).toEqual([[1, 2, 3, 4], [2, 3], [1], [11, 21, 31], "a[1]b[22]c", "1", "root"]) + }) + + test("error locations, rethrow preservation, and call stack depth limit", () => { + const e1 = err(`const a = 1\nconst b = null\nreturn b.foo`) + expect(e1.location).toEqual({ line: 3, column: 8 }) + + const e2 = err(`const a = 1\ntry {\n null.bar\n} catch (e) {\n throw e\n}`) + expect(e2.location).toEqual({ line: 3, column: 3 }) + + expect( + val(` + function recurse(n) { return recurse(n + 1) } + try { + recurse(0) + } catch (e) { + return [e instanceof RangeError, e.message.includes("Maximum call stack size exceeded")] + } + `), + ).toEqual([true, true]) + }) +}) diff --git a/packages/codemode/test/web-wpt.test.ts b/packages/codemode/test/web-wpt.test.ts index 05e51d06457..dc9e1a64d9a 100644 --- a/packages/codemode/test/web-wpt.test.ts +++ b/packages/codemode/test/web-wpt.test.ts @@ -13,7 +13,7 @@ */ import { describe, expect, test } from "bun:test" import { Effect } from "effect" -import { CodeMode } from "../src/index.js" +import { CodeMode } from "./lib/index.js" const base64Cases = (await Bun.file(new URL("./fixtures/wpt-base64.json", import.meta.url)).json()) as Array< [string, Array | null]