@opencode/codemode 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/codemode.d.ts +3 -0
  2. package/dist/codemode.js +12 -1
  3. package/dist/data.d.ts +14 -21
  4. package/dist/data.js +72 -140
  5. package/dist/extension.d.ts +11 -0
  6. package/dist/extension.js +9 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +1 -0
  9. package/dist/interpreter/callback.d.ts +20 -0
  10. package/dist/interpreter/{runner.js → callback.js} +14 -14
  11. package/dist/interpreter/errors.d.ts +12 -5
  12. package/dist/interpreter/errors.js +66 -54
  13. package/dist/interpreter/execute.d.ts +2 -2
  14. package/dist/interpreter/execute.js +15 -15
  15. package/dist/interpreter/extensions.d.ts +8 -0
  16. package/dist/interpreter/extensions.js +155 -0
  17. package/dist/interpreter/generators.d.ts +2 -3
  18. package/dist/interpreter/generators.js +10 -10
  19. package/dist/interpreter/globals.d.ts +4 -12
  20. package/dist/interpreter/globals.js +56 -49
  21. package/dist/interpreter/interpreter.d.ts +34 -0
  22. package/dist/interpreter/{runtime.js → interpreter.js} +250 -215
  23. package/dist/interpreter/intrinsics.d.ts +5 -5
  24. package/dist/interpreter/intrinsics.js +16 -10
  25. package/dist/interpreter/limits.d.ts +10 -0
  26. package/dist/interpreter/limits.js +19 -0
  27. package/dist/interpreter/model.d.ts +27 -14
  28. package/dist/interpreter/model.js +22 -14
  29. package/dist/interpreter/native.d.ts +12 -13
  30. package/dist/interpreter/native.js +17 -16
  31. package/dist/interpreter/objects.d.ts +55 -48
  32. package/dist/interpreter/objects.js +64 -44
  33. package/dist/interpreter/promises.d.ts +13 -15
  34. package/dist/interpreter/promises.js +89 -85
  35. package/dist/interpreter/references.d.ts +1 -2
  36. package/dist/interpreter/references.js +18 -16
  37. package/dist/interpreter/scope.js +6 -6
  38. package/dist/openapi/types.d.ts +1 -1
  39. package/dist/stdlib/array.d.ts +3 -4
  40. package/dist/stdlib/array.js +115 -110
  41. package/dist/stdlib/bytes.d.ts +10 -0
  42. package/dist/stdlib/bytes.js +228 -0
  43. package/dist/stdlib/collections.d.ts +4 -4
  44. package/dist/stdlib/collections.js +89 -89
  45. package/dist/stdlib/console.d.ts +5 -3
  46. package/dist/stdlib/console.js +30 -37
  47. package/dist/stdlib/date.d.ts +2 -2
  48. package/dist/stdlib/date.js +35 -35
  49. package/dist/stdlib/json.d.ts +3 -3
  50. package/dist/stdlib/json.js +29 -67
  51. package/dist/stdlib/math.d.ts +3 -3
  52. package/dist/stdlib/math.js +17 -21
  53. package/dist/stdlib/number.d.ts +3 -3
  54. package/dist/stdlib/number.js +32 -36
  55. package/dist/stdlib/object.d.ts +5 -5
  56. package/dist/stdlib/object.js +59 -62
  57. package/dist/stdlib/regexp.d.ts +7 -8
  58. package/dist/stdlib/regexp.js +40 -48
  59. package/dist/stdlib/string.d.ts +2 -2
  60. package/dist/stdlib/string.js +110 -88
  61. package/dist/stdlib/url.d.ts +4 -6
  62. package/dist/stdlib/url.js +86 -90
  63. package/dist/stdlib/value.d.ts +3 -3
  64. package/dist/stdlib/value.js +26 -24
  65. package/dist/stdlib/web.d.ts +4 -4
  66. package/dist/stdlib/web.js +22 -8
  67. package/dist/tool-runtime.d.ts +12 -5
  68. package/dist/tool-runtime.js +25 -11
  69. package/package.json +1 -1
  70. package/dist/interpreter/runner.d.ts +0 -28
  71. package/dist/interpreter/runtime.d.ts +0 -19
@@ -1,13 +1,13 @@
1
- import { ProgramError, ProgramObject } from "./objects.js";
1
+ import { ErrorObj, Obj } from "./objects.js";
2
2
  export declare const errorTypes: readonly ["Error", "TypeError", "RangeError", "SyntaxError", "ReferenceError", "EvalError", "URIError", "AggregateError"];
3
3
  export type ErrorType = (typeof errorTypes)[number];
4
4
  export declare const isErrorType: (name: string) => name is ErrorType;
5
- declare const builtins: readonly ["Object", "Function", "Array", "String", "Number", "Boolean", "Date", "RegExp", "Map", "Set", "URL", "URLSearchParams", "Promise", "Iterator", "AsyncIterator", "Generator", "AsyncGenerator"];
5
+ declare const builtins: readonly ["Object", "Function", "Array", "String", "Number", "Boolean", "Date", "RegExp", "Map", "Set", "URL", "URLSearchParams", "Uint8Array", "TextEncoder", "TextDecoder", "Promise", "Iterator", "AsyncIterator", "Generator", "AsyncGenerator"];
6
6
  /**
7
7
  * The built-in prototype objects of one runtime, allocated empty in dependency order. The globals populate them
8
8
  * and attach their constructors when the runtime is built.
9
9
  */
10
- export type Prototypes = Readonly<Record<(typeof builtins)[number] | ErrorType, ProgramObject>>;
11
- export declare const createErrorValue: (prototype: ProgramObject, message: string | undefined) => ProgramError;
12
- export declare const createPrototypes: () => Prototypes;
10
+ export type Builtins = Readonly<Record<(typeof builtins)[number] | ErrorType, Obj>>;
11
+ export declare const createErrorValue: (prototype: Obj, message: string | undefined) => ErrorObj;
12
+ export declare const createBuiltins: () => Builtins;
13
13
  export {};
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { define, hidden, NativeFunction, ProgramArray, ProgramError, ProgramObject } from "./objects.js";
2
+ import { define, hidden, Native, Arr, ErrorObj, Obj } from "./objects.js";
3
3
  export const errorTypes = [
4
4
  "Error",
5
5
  "TypeError",
@@ -24,6 +24,9 @@ const builtins = [
24
24
  "Set",
25
25
  "URL",
26
26
  "URLSearchParams",
27
+ "Uint8Array",
28
+ "TextEncoder",
29
+ "TextDecoder",
27
30
  "Promise",
28
31
  "Iterator",
29
32
  "AsyncIterator",
@@ -31,21 +34,21 @@ const builtins = [
31
34
  "AsyncGenerator",
32
35
  ];
33
36
  export const createErrorValue = (prototype, message) => {
34
- const value = new ProgramError(prototype);
37
+ const value = new ErrorObj(prototype);
35
38
  if (message !== undefined)
36
39
  define(value, "message", message, hidden);
37
40
  return value;
38
41
  };
39
- export const createPrototypes = () => {
40
- const object = new ProgramObject(null);
42
+ export const createBuiltins = () => {
43
+ const object = new Obj(null);
41
44
  // Function.prototype is itself callable and returns undefined.
42
- const fn = new NativeFunction(object, { name: "", call: () => Effect.undefined });
43
- const plain = () => new ProgramObject(object);
45
+ const fn = new Native(object, { name: "", call: () => Effect.undefined });
46
+ const plain = () => new Obj(object);
44
47
  const error = plain();
45
48
  define(error, "name", "Error", hidden);
46
49
  define(error, "message", "", hidden);
47
50
  const derived = (type) => {
48
- const proto = new ProgramObject(error);
51
+ const proto = new Obj(error);
49
52
  define(proto, "name", type, hidden);
50
53
  define(proto, "message", "", hidden);
51
54
  return proto;
@@ -55,7 +58,7 @@ export const createPrototypes = () => {
55
58
  return {
56
59
  Object: object,
57
60
  Function: fn,
58
- Array: new ProgramArray(object),
61
+ Array: new Arr(object),
59
62
  String: plain(),
60
63
  Number: plain(),
61
64
  Boolean: plain(),
@@ -65,11 +68,14 @@ export const createPrototypes = () => {
65
68
  Set: plain(),
66
69
  URL: plain(),
67
70
  URLSearchParams: plain(),
71
+ Uint8Array: plain(),
72
+ TextEncoder: plain(),
73
+ TextDecoder: plain(),
68
74
  Promise: plain(),
69
75
  Iterator: iterator,
70
76
  AsyncIterator: asyncIterator,
71
- Generator: new ProgramObject(iterator),
72
- AsyncGenerator: new ProgramObject(asyncIterator),
77
+ Generator: new Obj(iterator),
78
+ AsyncGenerator: new Obj(asyncIterator),
73
79
  Error: error,
74
80
  TypeError: derived("TypeError"),
75
81
  RangeError: derived("RangeError"),
@@ -0,0 +1,10 @@
1
+ /** Longest string a built-in or operator may produce. */
2
+ export declare const MAX_STRING_LENGTH: number;
3
+ /** Longest array a built-in may create or grow to. */
4
+ export declare const MAX_ARRAY_LENGTH = 10000000;
5
+ /** Most promises that may be pending at once. */
6
+ export declare const MAX_PENDING_PROMISES = 10000;
7
+ /** Deepest nesting a value may have when it crosses to or from the host. */
8
+ export declare const MAX_VALUE_DEPTH = 32;
9
+ export declare const checkStringLength: (length: number) => void;
10
+ export declare const checkArrayLength: (length: number) => void;
@@ -0,0 +1,19 @@
1
+ import { rangeError } from "./model.js";
2
+ // The timeout only fires between interpreter steps, so a single built-in must not be able to materialize an
3
+ // unbounded value. These bound what one call may build; programs cannot reach such sizes any other way.
4
+ /** Longest string a built-in or operator may produce. */
5
+ export const MAX_STRING_LENGTH = 1 << 24;
6
+ /** Longest array a built-in may create or grow to. */
7
+ export const MAX_ARRAY_LENGTH = 10_000_000;
8
+ /** Most promises that may be pending at once. */
9
+ export const MAX_PENDING_PROMISES = 10_000;
10
+ /** Deepest nesting a value may have when it crosses to or from the host. */
11
+ export const MAX_VALUE_DEPTH = 32;
12
+ export const checkStringLength = (length) => {
13
+ if (length > MAX_STRING_LENGTH)
14
+ throw rangeError("Invalid string length");
15
+ };
16
+ export const checkArrayLength = (length) => {
17
+ if (length > MAX_ARRAY_LENGTH)
18
+ throw rangeError("Invalid array length");
19
+ };
@@ -1,8 +1,15 @@
1
1
  import type { Node } from "acorn";
2
+ import { Context } from "effect";
2
3
  import type { ErrorType } from "./intrinsics.js";
3
4
  import type { DiagnosticKind } from "../codemode.js";
5
+ import type { ErrorObj } from "./objects.js";
4
6
  /** Any parsed node; the interpreter narrows on `type` and reads `loc` for diagnostics. */
5
7
  export type AstNode = Node;
8
+ /** The program call a built-in is running under: where to locate failures born inside it, and how deep the stack is there. */
9
+ export declare const CallSite: Context.Reference<{
10
+ readonly node?: AstNode;
11
+ readonly depth: number;
12
+ }>;
6
13
  export type Binding = {
7
14
  mutable: boolean;
8
15
  value: unknown;
@@ -24,7 +31,7 @@ export type GeneratorRequestKind = "next" | "return" | "throw";
24
31
  export declare const AsyncIteratorSymbol: unique symbol;
25
32
  export declare const IteratorSymbol: unique symbol;
26
33
  export declare const IteratorSymbols: readonly [typeof AsyncIteratorSymbol, typeof IteratorSymbol];
27
- export declare class ProgramThrow {
34
+ export declare class Throw {
28
35
  readonly value: unknown;
29
36
  constructor(value: unknown);
30
37
  }
@@ -33,24 +40,30 @@ export declare class GeneratorReturn {
33
40
  constructor(value: unknown);
34
41
  }
35
42
  export declare const OptionalShortCircuit: unique symbol;
36
- export declare class InterpreterRuntimeError extends Error {
37
- readonly kind: DiagnosticKind;
38
- readonly suggestions?: ReadonlyArray<string> | undefined;
43
+ /**
44
+ * A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
45
+ * the first time a handler observes it, so every observer of the same failure sees the same value.
46
+ */
47
+ export declare class PendingThrow {
39
48
  /** The JS error class a program sees when it catches this failure. */
40
49
  readonly type: ErrorType;
41
- readonly node?: AstNode;
42
- constructor(message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray<string> | undefined,
50
+ readonly message: string;
51
+ readonly kind: DiagnosticKind;
52
+ readonly suggestions?: ReadonlyArray<string> | undefined;
53
+ node?: AstNode;
54
+ value?: ErrorObj;
55
+ constructor(
43
56
  /** The JS error class a program sees when it catches this failure. */
44
- type?: ErrorType);
57
+ type: ErrorType, message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray<string> | undefined);
45
58
  }
46
- /** Attaches a source location to a failure raised where none was known, such as inside a property accessor. */
47
- export declare const locate: (error: unknown, node: AstNode) => unknown;
48
- export declare const rangeError: (message: string, node?: AstNode) => InterpreterRuntimeError;
49
- export declare const referenceError: (message: string, node?: AstNode) => InterpreterRuntimeError;
50
- export declare const syntaxError: (message: string, node?: AstNode) => InterpreterRuntimeError;
51
- export declare const uriError: (message: string, node?: AstNode) => InterpreterRuntimeError;
59
+ export declare const typeError: (message: string, node?: AstNode) => PendingThrow;
60
+ export declare const invalidData: (message: string, node?: AstNode) => PendingThrow;
61
+ export declare const rangeError: (message: string, node?: AstNode) => PendingThrow;
62
+ export declare const referenceError: (message: string, node?: AstNode) => PendingThrow;
63
+ export declare const syntaxError: (message: string, node?: AstNode) => PendingThrow;
64
+ export declare const uriError: (message: string, node?: AstNode) => PendingThrow;
52
65
  export declare 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, this, getters/setters, tagged templates, BigInt, and custom Symbols. Use plain functions and data objects instead.";
53
- export declare const unsupportedSyntax: (kind: string, node: AstNode) => InterpreterRuntimeError;
66
+ export declare const unsupportedSyntax: (kind: string, node: AstNode) => PendingThrow;
54
67
  export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
55
68
  export declare const sourceLocation: (node: AstNode) => {
56
69
  readonly line: number;
@@ -1,7 +1,12 @@
1
+ import { Context } from "effect";
2
+ /** The program call a built-in is running under: where to locate failures born inside it, and how deep the stack is there. */
3
+ export const CallSite = Context.Reference("codemode/CallSite", {
4
+ defaultValue: () => ({ depth: 0 }),
5
+ });
1
6
  export const AsyncIteratorSymbol = Symbol("codemode.async-iterator");
2
7
  export const IteratorSymbol = Symbol("codemode.iterator");
3
8
  export const IteratorSymbols = [AsyncIteratorSymbol, IteratorSymbol];
4
- export class ProgramThrow {
9
+ export class Throw {
5
10
  value;
6
11
  constructor(value) {
7
12
  this.value = value;
@@ -14,35 +19,38 @@ export class GeneratorReturn {
14
19
  }
15
20
  }
16
21
  export const OptionalShortCircuit = Symbol("codemode.optional-short-circuit");
17
- export class InterpreterRuntimeError extends Error {
22
+ /**
23
+ * A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
24
+ * the first time a handler observes it, so every observer of the same failure sees the same value.
25
+ */
26
+ export class PendingThrow {
27
+ type;
28
+ message;
18
29
  kind;
19
30
  suggestions;
20
- type;
21
31
  node;
22
- constructor(message, node, kind = "ExecutionFailure", suggestions,
32
+ value;
33
+ constructor(
23
34
  /** The JS error class a program sees when it catches this failure. */
24
- type = "TypeError") {
25
- super(message);
35
+ type, message, node, kind = "ExecutionFailure", suggestions) {
36
+ this.type = type;
37
+ this.message = message;
26
38
  this.kind = kind;
27
39
  this.suggestions = suggestions;
28
- this.type = type;
29
- this.name = "InterpreterRuntimeError";
30
40
  if (node)
31
41
  this.node = node;
32
42
  }
33
43
  }
34
- /** Attaches a source location to a failure raised where none was known, such as inside a property accessor. */
35
- export const locate = (error, node) => error instanceof InterpreterRuntimeError && error.node === undefined
36
- ? new InterpreterRuntimeError(error.message, node, error.kind, error.suggestions, error.type)
37
- : error;
38
- const failure = (type) => (message, node) => new InterpreterRuntimeError(message, node, "ExecutionFailure", undefined, type);
44
+ const failure = (type, kind) => (message, node) => new PendingThrow(type, message, node, kind);
45
+ export const typeError = failure("TypeError");
46
+ export const invalidData = failure("TypeError", "InvalidDataValue");
39
47
  export const rangeError = failure("RangeError");
40
48
  export const referenceError = failure("ReferenceError");
41
49
  export const syntaxError = failure("SyntaxError");
42
50
  export const uriError = failure("URIError");
43
51
  // Orient the agent rather than enumerate JavaScript; interpreter-support.md is the full matrix.
44
52
  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, this, getters/setters, tagged templates, BigInt, and custom Symbols. Use plain functions and data objects instead.";
45
- export const unsupportedSyntax = (kind, node) => new InterpreterRuntimeError(`Syntax '${kind}' is not supported. ${supportedSyntaxMessage}`, node, "UnsupportedSyntax", [supportedSyntaxMessage], "SyntaxError");
53
+ export const unsupportedSyntax = (kind, node) => new PendingThrow("SyntaxError", `Syntax '${kind}' is not supported. ${supportedSyntaxMessage}`, node, "UnsupportedSyntax", [supportedSyntaxMessage]);
46
54
  export const isRecord = (value) => typeof value === "object" && value !== null;
47
55
  export const sourceLocation = (node) => ({
48
56
  line: Math.max(1, (node.loc?.start.line ?? 2) - 1),
@@ -1,19 +1,18 @@
1
1
  import { Effect } from "effect";
2
- import type { Prototypes } from "./intrinsics.js";
3
- import { type AstNode } from "./model.js";
4
- import { type Callable, NativeFunction, type NativeOptions, ProgramObject } from "./objects.js";
5
- /** A native function body: a plain value, a thrown `InterpreterRuntimeError`, or an Effect. */
6
- export type Impl = (thisValue: unknown, args: Array<unknown>, node: AstNode) => unknown;
7
- export declare const native: <R>(protos: Prototypes, options: NativeOptions<R>) => NativeFunction<R>;
8
- export declare const fn: <R>(protos: Prototypes, name: string, length: number, impl: Impl) => NativeFunction<R>;
2
+ import type { Builtins } from "./intrinsics.js";
3
+ import { type Callable, Native, type NativeOptions, Obj } from "./objects.js";
4
+ /** A native function body: a plain value, a thrown `PendingThrow`, or an Effect. */
5
+ export type Impl = (thisValue: unknown, args: Array<unknown>) => unknown;
6
+ export declare const native: <R>(builtins: Builtins, options: NativeOptions<R>) => Native<R>;
7
+ export declare const fn: <R>(builtins: Builtins, name: string, length: number, impl: Impl) => Native<R>;
9
8
  export type Method = readonly [name: string, length: number, impl: Impl];
10
- export declare const methods: (protos: Prototypes, target: ProgramObject, table: ReadonlyArray<Method>) => void;
11
- export declare const constants: (target: ProgramObject, table: Record<string, unknown>) => void;
9
+ export declare const methods: (builtins: Builtins, target: Obj, table: ReadonlyArray<Method>) => void;
10
+ export declare const constants: (target: Obj, table: Record<string, unknown>) => void;
12
11
  /** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
13
- export declare const constructor: <R>(protos: Prototypes, proto: ProgramObject, options: NativeOptions<R>) => NativeFunction<R>;
12
+ export declare const constructor: <R>(builtins: Builtins, proto: Obj, options: NativeOptions<R>) => Native<R>;
14
13
  /** The `call` of a constructor that JS requires to be invoked with `new`. */
15
- export declare const requiresNew: (name: string) => (_: unknown, __: Array<unknown>, node: AstNode) => Effect.Effect<never, unknown, never>;
14
+ export declare const requiresNew: (name: string) => () => Effect.Effect<never, unknown, never>;
16
15
  /** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
17
- export declare const prototypeFrom: (newTarget: Callable, fallback: ProgramObject) => ProgramObject;
16
+ export declare const prototypeFrom: (newTarget: Callable, fallback: Obj) => Obj;
18
17
  /** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
19
- export declare const receiver: <T extends ProgramObject>(cls: abstract new (...args: never) => T, thisValue: unknown, method: string, node?: AstNode) => T;
18
+ export declare const receiver: <T extends Obj>(cls: abstract new (...args: never) => T, thisValue: unknown, method: string) => T;
@@ -1,40 +1,41 @@
1
1
  import { Effect } from "effect";
2
- import { InterpreterRuntimeError } from "./model.js";
3
- import { define, frozen, hidden, NativeFunction, ProgramObject } from "./objects.js";
2
+ import { typeError } from "./model.js";
3
+ import { define, frozen, hidden, Native, Obj } from "./objects.js";
4
4
  import { describeValue } from "./references.js";
5
- const lift = (impl) => (thisValue, args, node) => Effect.suspend(() => {
6
- const result = impl(thisValue, args, node);
5
+ // The dispatch in `Frame.call` suspends every native call, so a synchronous throw here is a defect.
6
+ const lift = (impl) => (thisValue, args) => {
7
+ const result = impl(thisValue, args);
7
8
  return Effect.isEffect(result) ? result : Effect.succeed(result);
8
- });
9
- export const native = (protos, options) => new NativeFunction(protos.Function, options);
10
- export const fn = (protos, name, length, impl) => native(protos, { name, length, call: lift(impl) });
11
- export const methods = (protos, target, table) => {
9
+ };
10
+ export const native = (builtins, options) => new Native(builtins.Function, options);
11
+ export const fn = (builtins, name, length, impl) => native(builtins, { name, length, call: lift(impl) });
12
+ export const methods = (builtins, target, table) => {
12
13
  for (const [name, length, impl] of table)
13
- define(target, name, fn(protos, name, length, impl), hidden);
14
+ define(target, name, fn(builtins, name, length, impl), hidden);
14
15
  };
15
16
  export const constants = (target, table) => {
16
17
  for (const [name, value] of Object.entries(table))
17
18
  define(target, name, value, frozen);
18
19
  };
19
20
  /** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
20
- export const constructor = (protos, proto, options) => {
21
- const ctor = native(protos, options);
21
+ export const constructor = (builtins, proto, options) => {
22
+ const ctor = native(builtins, options);
22
23
  define(ctor, "prototype", proto, frozen);
23
24
  define(proto, "constructor", ctor, hidden);
24
25
  return ctor;
25
26
  };
26
27
  /** The `call` of a constructor that JS requires to be invoked with `new`. */
27
- export const requiresNew = (name) => (_, __, node) => Effect.sync(() => {
28
- throw new InterpreterRuntimeError(`Constructor ${name} requires 'new'.`, node);
28
+ export const requiresNew = (name) => () => Effect.sync(() => {
29
+ throw typeError(`Constructor ${name} requires 'new'.`);
29
30
  });
30
31
  /** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
31
32
  export const prototypeFrom = (newTarget, fallback) => {
32
33
  const proto = newTarget.props.get("prototype");
33
- return proto !== undefined && "value" in proto && proto.value instanceof ProgramObject ? proto.value : fallback;
34
+ return proto !== undefined && "value" in proto && proto.value instanceof Obj ? proto.value : fallback;
34
35
  };
35
36
  /** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
36
- export const receiver = (cls, thisValue, method, node) => {
37
+ export const receiver = (cls, thisValue, method) => {
37
38
  if (thisValue instanceof cls)
38
39
  return thisValue;
39
- throw new InterpreterRuntimeError(`${method} called on incompatible receiver ${describeValue(thisValue)}.`, node);
40
+ throw typeError(`${method} called on incompatible receiver ${describeValue(thisValue)}.`);
40
41
  };
@@ -1,6 +1,6 @@
1
1
  import type { BlockStatement, Expression, Pattern } from "acorn";
2
2
  import type { Effect, Fiber } from "effect";
3
- import { type AstNode, type Binding, type GeneratorRequestKind } from "./model.js";
3
+ import { type Binding, type GeneratorRequestKind, type PendingThrow } from "./model.js";
4
4
  /** Property attributes, as in a JS property descriptor. */
5
5
  export type Attributes = {
6
6
  readonly writable: boolean;
@@ -30,31 +30,33 @@ export declare const readonly: Attributes;
30
30
  /** Constants such as `Math.PI` and a constructor's `prototype`. */
31
31
  export declare const frozen: Attributes;
32
32
  /** An object owned by the program: own properties plus a prototype link. */
33
- export declare class ProgramObject {
34
- proto: ProgramObject | null;
33
+ export declare class Obj {
34
+ proto: Obj | null;
35
35
  readonly props: Map<string | symbol, Slot>;
36
- constructor(proto: ProgramObject | null);
36
+ constructor(proto: Obj | null);
37
37
  }
38
- export declare class ProgramArray extends ProgramObject {
38
+ export declare class Arr extends Obj {
39
39
  readonly items: Array<unknown>;
40
- constructor(proto: ProgramObject, items?: Array<unknown>);
40
+ constructor(proto: Obj, items?: Array<unknown>);
41
41
  }
42
42
  /** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */
43
- export declare class ProgramError extends ProgramObject {
43
+ export declare class ErrorObj extends Obj {
44
+ /** The interpreter failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. */
45
+ host?: PendingThrow;
44
46
  }
45
- export declare abstract class Callable extends ProgramObject {
46
- constructor(proto: ProgramObject, name: string, length: number);
47
+ export declare abstract class Callable extends Obj {
48
+ constructor(proto: Obj, name: string, length: number);
47
49
  }
48
- export declare class ProgramFunction extends Callable {
50
+ export declare class Fn extends Callable {
49
51
  readonly parameters: ReadonlyArray<Pattern>;
50
52
  readonly body: BlockStatement | Expression;
51
53
  readonly capturedScopes: ReadonlyArray<Map<string, Binding>>;
52
54
  readonly async: boolean;
53
55
  readonly generator: boolean;
54
- constructor(proto: ProgramObject, name: string, parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: ReadonlyArray<Map<string, Binding>>, async: boolean, generator: boolean);
56
+ constructor(proto: Obj, name: string, parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: ReadonlyArray<Map<string, Binding>>, async: boolean, generator: boolean);
55
57
  }
56
- export type NativeCall<R> = (thisValue: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
57
- export type NativeConstruct<R> = (args: Array<unknown>, newTarget: Callable, node: AstNode) => Effect.Effect<unknown, unknown, R>;
58
+ export type NativeCall<R> = (thisValue: unknown, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
59
+ export type NativeConstruct<R> = (args: Array<unknown>, newTarget: Callable) => Effect.Effect<unknown, unknown, R>;
58
60
  export type NativeOptions<R> = {
59
61
  readonly name: string;
60
62
  readonly length?: number;
@@ -64,67 +66,72 @@ export type NativeOptions<R> = {
64
66
  /** Whether callback sites (array methods, replacers, promise reactions) admit this function. Defaults to true. */
65
67
  readonly callback?: boolean;
66
68
  };
67
- export declare class NativeFunction<R = never> extends Callable {
69
+ export declare class Native<R = never> extends Callable {
68
70
  readonly call: NativeCall<R>;
69
71
  readonly construct: NativeConstruct<R> | undefined;
70
72
  readonly callback: boolean;
71
- constructor(proto: ProgramObject, options: NativeOptions<R>);
73
+ constructor(proto: Obj, options: NativeOptions<R>);
72
74
  }
73
- export declare class ProgramPromise extends ProgramObject {
75
+ export declare class PromiseObj extends Obj {
74
76
  readonly fiber: Fiber.Fiber<unknown, unknown>;
75
- constructor(proto: ProgramObject, fiber: Fiber.Fiber<unknown, unknown>);
77
+ constructor(proto: Obj, fiber: Fiber.Fiber<unknown, unknown>);
76
78
  }
77
- export declare class ProgramGenerator extends ProgramObject {
79
+ export declare class GeneratorObj extends Obj {
78
80
  readonly asynchronous: boolean;
79
- readonly request: (kind: GeneratorRequestKind, value: unknown, node: AstNode) => Effect.Effect<unknown, unknown, unknown>;
80
- constructor(proto: ProgramObject, asynchronous: boolean, request: (kind: GeneratorRequestKind, value: unknown, node: AstNode) => Effect.Effect<unknown, unknown, unknown>);
81
+ readonly request: (kind: GeneratorRequestKind, value: unknown) => Effect.Effect<unknown, unknown, unknown>;
82
+ constructor(proto: Obj, asynchronous: boolean, request: (kind: GeneratorRequestKind, value: unknown) => Effect.Effect<unknown, unknown, unknown>);
81
83
  }
82
- export declare class ProgramDate extends ProgramObject {
84
+ export declare class DateObj extends Obj {
83
85
  time: number;
84
- constructor(proto: ProgramObject, time: number);
86
+ constructor(proto: Obj, time: number);
85
87
  }
86
- export declare class ProgramRegExp extends ProgramObject {
88
+ export declare class RegExpObj extends Obj {
87
89
  readonly regex: RegExp;
88
- constructor(proto: ProgramObject, pattern: string, flags: string);
90
+ constructor(proto: Obj, pattern: string, flags: string);
89
91
  }
90
- export declare class ProgramMap extends ProgramObject {
92
+ export declare class MapObj extends Obj {
91
93
  readonly map: Map<unknown, unknown>;
92
94
  }
93
- export declare class ProgramSet extends ProgramObject {
95
+ export declare class SetObj extends Obj {
94
96
  readonly set: Set<unknown>;
95
97
  }
96
- export declare class ProgramURLSearchParams extends ProgramObject {
98
+ export declare class URLSearchParamsObj extends Obj {
97
99
  readonly params: URLSearchParams;
98
- constructor(proto: ProgramObject, params: URLSearchParams);
100
+ constructor(proto: Obj, params: URLSearchParams);
99
101
  }
100
- export declare class ProgramURL extends ProgramObject {
102
+ export declare class URLObj extends Obj {
101
103
  readonly url: URL;
102
- readonly searchParams: ProgramURLSearchParams;
103
- constructor(proto: ProgramObject, searchParamsProto: ProgramObject, url: URL);
104
+ readonly searchParams: URLSearchParamsObj;
105
+ constructor(proto: Obj, searchParamsProto: Obj, url: URL);
106
+ }
107
+ /** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */
108
+ export declare class Bytes extends Obj {
109
+ readonly bytes: Uint8Array;
110
+ constructor(proto: Obj, bytes: Uint8Array);
104
111
  }
105
112
  /** Built-in objects that wrap a host value; data-like, but never plain data. */
106
- export declare const isWrapper: (value: unknown) => value is ProgramDate | ProgramRegExp | ProgramMap | ProgramSet | ProgramURL | ProgramURLSearchParams;
113
+ export declare const isWrapper: (value: unknown) => value is DateObj | RegExpObj | MapObj | SetObj | URLObj | URLSearchParamsObj | Bytes;
107
114
  export declare const parseArrayIndex: (key: string | number) => number | undefined;
108
115
  /** The own property under `key`, including an array's live indexes and `length`. */
109
- export declare const own: (target: ProgramObject, key: PropertyKey) => Slot | undefined;
110
- export declare const hasOwn: (target: ProgramObject, key: PropertyKey) => boolean;
111
- export declare const getOwn: (target: ProgramObject, key: PropertyKey) => unknown;
116
+ export declare const own: (target: Obj, key: PropertyKey) => Slot | undefined;
117
+ export declare const hasOwn: (target: Obj, key: PropertyKey) => boolean;
118
+ export declare const getOwn: (target: Obj, key: PropertyKey) => unknown;
112
119
  /** [[Get]]: walks the prototype chain; accessors see `receiver`, which is the primitive for wrapper prototypes. */
113
- export declare const get: (target: ProgramObject, key: PropertyKey, receiver?: unknown) => unknown;
114
- export declare const has: (target: ProgramObject, key: PropertyKey) => boolean;
115
- export declare const hasPrototype: (value: unknown, proto: ProgramObject) => boolean;
120
+ export declare const get: (target: Obj, key: PropertyKey, receiver?: unknown) => unknown;
121
+ export declare const has: (target: Obj, key: PropertyKey) => boolean;
122
+ export declare const hasPrototype: (value: unknown, proto: Obj) => boolean;
116
123
  /** [[Set]]: an inherited setter or read-only property decides before an own data property is created. */
117
- export declare const set: (target: ProgramObject, key: PropertyKey, value: unknown) => boolean;
124
+ export declare const set: (target: Obj, key: PropertyKey, value: unknown) => boolean;
118
125
  /** [[DefineOwnProperty]] for a data property, ignoring the chain. */
119
- export declare const define: (target: ProgramObject, key: PropertyKey, value: unknown, attrs?: Attributes) => void;
120
- export declare const defineAccessor: (target: ProgramObject, key: PropertyKey, get: Getter, set?: Setter) => void;
121
- export declare const remove: (target: ProgramObject, key: PropertyKey) => boolean;
122
- export declare const ownKeys: (target: ProgramObject) => Array<string | symbol>;
126
+ export declare const define: (target: Obj, key: PropertyKey, value: unknown, attrs?: Attributes) => void;
127
+ export declare const defineAccessor: (target: Obj, key: PropertyKey, get: Getter | undefined, set?: Setter) => void;
128
+ export declare const remove: (target: Obj, key: PropertyKey) => boolean;
129
+ export declare const ownKeys: (target: Obj) => Array<string | symbol>;
123
130
  /** Own enumerable keys, including the iterator symbols; what spread and `Object.assign` copy. */
124
- export declare const enumerableKeys: (target: ProgramObject) => Array<string | symbol>;
131
+ export declare const enumerableKeys: (target: Obj) => Array<string | symbol>;
125
132
  /** Own enumerable string keys: `Object.keys`. */
126
- export declare const keys: (target: ProgramObject) => Array<string>;
133
+ export declare const keys: (target: Obj) => Array<string>;
127
134
  /** Own enumerable string entries: `Object.entries` and serialization. */
128
- export declare const entries: (target: ProgramObject) => Array<[string, unknown]>;
129
- export declare const record: (proto: ProgramObject, fields: Record<string, unknown>) => ProgramObject;
130
- export declare const assign: (target: ProgramObject, source: ProgramObject, skip?: ReadonlySet<PropertyKey>) => void;
135
+ export declare const entries: (target: Obj) => Array<[string, unknown]>;
136
+ export declare const record: (proto: Obj, fields: Record<string, unknown>) => Obj;
137
+ export declare const assign: (target: Obj, source: Obj, skip?: ReadonlySet<PropertyKey>) => void;