@opencode/codemode 0.0.0-dev-19937 → 0.0.0-dev-19941

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 (42) hide show
  1. package/dist/data.d.ts +5 -4
  2. package/dist/interpreter/callback.d.ts +8 -8
  3. package/dist/interpreter/callback.js +1 -2
  4. package/dist/interpreter/errors.d.ts +3 -3
  5. package/dist/interpreter/errors.js +1 -2
  6. package/dist/interpreter/execute.d.ts +2 -1
  7. package/dist/interpreter/extensions.d.ts +2 -1
  8. package/dist/interpreter/extensions.js +7 -35
  9. package/dist/interpreter/globals.d.ts +2 -1
  10. package/dist/interpreter/interpreter.d.ts +8 -14
  11. package/dist/interpreter/interpreter.js +25 -52
  12. package/dist/interpreter/intrinsics.d.ts +2 -2
  13. package/dist/interpreter/model.d.ts +7 -8
  14. package/dist/interpreter/model.js +0 -1
  15. package/dist/interpreter/native.d.ts +5 -5
  16. package/dist/interpreter/objects.d.ts +127 -42
  17. package/dist/interpreter/objects.js +207 -25
  18. package/dist/interpreter/promises.d.ts +7 -7
  19. package/dist/interpreter/references.d.ts +8 -7
  20. package/dist/interpreter/references.js +10 -46
  21. package/dist/interpreter/scope.d.ts +5 -4
  22. package/dist/stdlib/array.d.ts +2 -1
  23. package/dist/stdlib/array.js +1 -2
  24. package/dist/stdlib/bytes.js +1 -2
  25. package/dist/stdlib/collections.js +6 -13
  26. package/dist/stdlib/console.d.ts +2 -2
  27. package/dist/stdlib/console.js +6 -50
  28. package/dist/stdlib/date.js +1 -2
  29. package/dist/stdlib/headers.js +8 -11
  30. package/dist/stdlib/iterator.js +1 -1
  31. package/dist/stdlib/number.js +2 -1
  32. package/dist/stdlib/object.d.ts +3 -4
  33. package/dist/stdlib/object.js +4 -15
  34. package/dist/stdlib/regexp.d.ts +3 -3
  35. package/dist/stdlib/regexp.js +1 -2
  36. package/dist/stdlib/string.js +8 -13
  37. package/dist/stdlib/url.d.ts +2 -1
  38. package/dist/stdlib/url.js +2 -12
  39. package/dist/stdlib/value.d.ts +0 -3
  40. package/dist/stdlib/value.js +1 -68
  41. package/dist/stdlib/web.js +1 -2
  42. package/package.json +1 -1
@@ -1,5 +1,7 @@
1
1
  import type { BlockStatement, Expression, Pattern } from "acorn";
2
2
  import type { Effect, Fiber } from "effect";
3
+ import { ToolReference } from "../tool-runtime.js";
4
+ import type { Builtins } from "./intrinsics.js";
3
5
  import { type Binding, type GeneratorRequestKind, type PendingThrow } from "./model.js";
4
6
  /** Property attributes, as in a JS property descriptor. */
5
7
  export type Attributes = {
@@ -7,11 +9,11 @@ export type Attributes = {
7
9
  readonly enumerable: boolean;
8
10
  readonly configurable: boolean;
9
11
  };
10
- export type Getter = (receiver: unknown) => unknown;
11
- export type Setter = (receiver: unknown, value: unknown) => void;
12
+ export type Getter = (receiver: Value) => Value;
13
+ export type Setter = (receiver: Value, value: Value) => void;
12
14
  /** One own property: a data slot or a native accessor pair. */
13
15
  export type Slot = {
14
- value: unknown;
16
+ value: Value;
15
17
  writable: boolean;
16
18
  enumerable: boolean;
17
19
  configurable: boolean;
@@ -29,23 +31,57 @@ export declare const hidden: Attributes;
29
31
  export declare const readonly: Attributes;
30
32
  /** Constants such as `Math.PI` and a constructor's `prototype`. */
31
33
  export declare const frozen: Attributes;
32
- /** An object owned by the program: own properties plus a prototype link. */
34
+ /**
35
+ * An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a
36
+ * built-in kind of object prints, coerces, iterates, and crosses to the host.
37
+ */
33
38
  export declare class Obj {
34
39
  proto: Obj | null;
35
40
  readonly props: Map<string | symbol, Slot>;
36
41
  constructor(proto: Obj | null);
42
+ /** The class name `Object.prototype.toString` reports: `[object Map]`. */
43
+ readonly tag: string;
44
+ /** How diagnostics refer to a value of this kind. */
45
+ get describe(): string;
46
+ /** ToString without consulting program-defined methods. */
47
+ toString(): string;
48
+ /** ToPrimitive without consulting program-defined methods: only a Date answers a number hint differently. */
49
+ toPrimitive(hint: "default" | "number" | "string"): string | number;
50
+ /** ToNumber without consulting program-defined methods. */
51
+ toNumber(): number;
52
+ /** How `console.log` shows the value; `item` formats a child with cycle and depth tracking. */
53
+ inspect(item: (value: Value) => string): string;
54
+ /** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */
55
+ toHost(item: (value: Value) => unknown): unknown;
56
+ /** The built-in iteration `for...of` and spread use, when this kind of object has one. */
57
+ iterator(builtins: Builtins): Iterator<Value, undefined> | undefined;
37
58
  }
38
59
  export declare class Arr extends Obj {
39
- readonly items: Array<unknown>;
40
- constructor(proto: Obj, items?: Array<unknown>);
60
+ readonly items: Array<Value>;
61
+ readonly tag = "Array";
62
+ constructor(proto: Obj, items?: Array<Value>);
63
+ get describe(): string;
64
+ toString(): string;
65
+ inspect(item: (value: Value) => string): string;
66
+ toHost(item: (value: Value) => unknown): unknown[];
67
+ iterator(): ArrayIterator<Value>;
41
68
  }
42
69
  /** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */
43
70
  export declare class ErrorObj extends Obj {
71
+ readonly tag = "Error";
44
72
  /** The interpreter failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. */
45
73
  host?: PendingThrow;
74
+ /** Error.prototype.toString: "name: message", or just one when the other is empty. */
75
+ toString(): string;
46
76
  }
47
- export declare abstract class Callable extends Obj {
77
+ /** Interpreter machinery a program can hold but never inspect, serialize, or hand to the host. */
78
+ export declare abstract class Opaque extends Obj {
79
+ inspect(): string;
80
+ }
81
+ export declare abstract class Callable extends Opaque {
82
+ readonly tag = "Function";
48
83
  constructor(proto: Obj, name: string, length: number);
84
+ get describe(): string;
49
85
  }
50
86
  export declare class Fn extends Callable {
51
87
  readonly parameters: ReadonlyArray<Pattern>;
@@ -55,8 +91,8 @@ export declare class Fn extends Callable {
55
91
  readonly generator: boolean;
56
92
  constructor(proto: Obj, name: string, parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: ReadonlyArray<Map<string, Binding>>, async: boolean, generator: boolean);
57
93
  }
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>;
94
+ export type NativeCall<R> = (thisValue: Value, args: Array<Value>) => Effect.Effect<Value, unknown, R>;
95
+ export type NativeConstruct<R> = (args: Array<Value>, newTarget: Callable) => Effect.Effect<Value, unknown, R>;
60
96
  export type NativeOptions<R> = {
61
97
  readonly name: string;
62
98
  readonly length?: number;
@@ -72,67 +108,116 @@ export declare class Native<R = never> extends Callable {
72
108
  readonly callback: boolean;
73
109
  constructor(proto: Obj, options: NativeOptions<R>);
74
110
  }
75
- export declare class PromiseObj extends Obj {
76
- readonly fiber: Fiber.Fiber<unknown, unknown>;
77
- constructor(proto: Obj, fiber: Fiber.Fiber<unknown, unknown>);
111
+ export declare class PromiseObj extends Opaque {
112
+ readonly fiber: Fiber.Fiber<Value, unknown>;
113
+ readonly tag = "Promise";
114
+ constructor(proto: Obj, fiber: Fiber.Fiber<Value, unknown>);
115
+ get describe(): string;
116
+ inspect(): string;
78
117
  }
79
- export declare class GeneratorObj extends Obj {
118
+ export declare class GeneratorObj extends Opaque {
80
119
  readonly asynchronous: boolean;
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>);
120
+ readonly request: (kind: GeneratorRequestKind, value: Value) => Effect.Effect<Value, unknown, unknown>;
121
+ readonly tag = "Generator";
122
+ constructor(proto: Obj, asynchronous: boolean, request: (kind: GeneratorRequestKind, value: Value) => Effect.Effect<Value, unknown, unknown>);
123
+ get describe(): string;
83
124
  }
84
125
  /** A built-in collection iterator: live over the host collection, yielding program values. */
85
- export declare class IteratorObj extends Obj {
86
- readonly iterator: IteratorObject<unknown>;
87
- constructor(proto: Obj, iterator: IteratorObject<unknown>);
88
- }
89
- export declare class DateObj extends Obj {
126
+ export declare class IteratorObj extends Opaque {
127
+ readonly source: IteratorObject<Value, undefined>;
128
+ readonly tag = "Iterator";
129
+ constructor(proto: Obj, source: IteratorObject<Value, undefined>);
130
+ get describe(): string;
131
+ iterator(): IteratorObject<Value, undefined, unknown>;
132
+ }
133
+ /** A built-in object around a host value: data-like, so it prints as itself and crosses to extensions as a copy. */
134
+ export declare abstract class Wrapper extends Obj {
135
+ inspect(item: (value: Value) => string): string;
136
+ }
137
+ export declare class DateObj extends Wrapper {
90
138
  time: number;
139
+ readonly tag = "Date";
91
140
  constructor(proto: Obj, time: number);
141
+ toString(): string;
142
+ toPrimitive(hint: "default" | "number" | "string"): string | number;
143
+ toHost(): Date;
92
144
  }
93
- export declare class RegExpObj extends Obj {
145
+ export declare class RegExpObj extends Wrapper {
146
+ readonly tag = "RegExp";
94
147
  readonly regex: RegExp;
95
148
  constructor(proto: Obj, pattern: string, flags: string);
96
- }
97
- export declare class MapObj extends Obj {
98
- readonly map: Map<unknown, unknown>;
99
- }
100
- export declare class SetObj extends Obj {
101
- readonly set: Set<unknown>;
102
- }
103
- export declare class URLSearchParamsObj extends Obj {
149
+ toString(): string;
150
+ toHost(): RegExp;
151
+ }
152
+ export declare class MapObj extends Wrapper {
153
+ readonly tag = "Map";
154
+ readonly map: Map<Value, Value>;
155
+ inspect(item: (value: Value) => string): string;
156
+ toHost(item: (value: Value) => unknown): Map<unknown, unknown>;
157
+ iterator(builtins: Builtins): IteratorObject<Arr, undefined, unknown>;
158
+ }
159
+ export declare class SetObj extends Wrapper {
160
+ readonly tag = "Set";
161
+ readonly set: Set<Value>;
162
+ inspect(item: (value: Value) => string): string;
163
+ toHost(item: (value: Value) => unknown): Set<unknown>;
164
+ iterator(): SetIterator<Value>;
165
+ }
166
+ export declare class URLSearchParamsObj extends Wrapper {
104
167
  readonly params: URLSearchParams;
168
+ readonly tag = "URLSearchParams";
105
169
  constructor(proto: Obj, params: URLSearchParams);
170
+ toString(): string;
171
+ toHost(): URLSearchParams;
172
+ iterator(builtins: Builtins): IteratorObject<Arr, undefined, unknown>;
106
173
  }
107
- export declare class HeadersObj extends Obj {
174
+ export declare class HeadersObj extends Wrapper {
108
175
  readonly headers: Headers;
176
+ readonly tag = "Headers";
109
177
  constructor(proto: Obj, headers: Headers);
178
+ inspect(): string;
179
+ toHost(): Headers;
180
+ iterator(builtins: Builtins): IteratorObject<Arr, undefined, unknown>;
110
181
  }
111
- export declare class URLObj extends Obj {
182
+ export declare class URLObj extends Wrapper {
112
183
  readonly url: URL;
184
+ readonly tag = "URL";
113
185
  readonly searchParams: URLSearchParamsObj;
114
186
  constructor(proto: Obj, searchParamsProto: Obj, url: URL);
187
+ toString(): string;
188
+ toHost(): URL;
115
189
  }
116
190
  /** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */
117
- export declare class Bytes extends Obj {
191
+ export declare class Bytes extends Wrapper {
118
192
  readonly bytes: Uint8Array;
193
+ readonly tag = "Uint8Array";
119
194
  constructor(proto: Obj, bytes: Uint8Array);
120
- }
121
- /** Built-in objects that wrap a host value; data-like, but never plain data. */
122
- export declare const isWrapper: (value: unknown) => value is DateObj | RegExpObj | MapObj | SetObj | URLObj | URLSearchParamsObj | HeadersObj | Bytes;
195
+ toString(): string;
196
+ inspect(): string;
197
+ toHost(): Uint8Array<ArrayBuffer>;
198
+ iterator(): ArrayIterator<number>;
199
+ }
200
+ /** Every value a program can hold. Host values never appear here; they are copied in at the boundaries. */
201
+ export type Value = string | number | boolean | null | undefined | symbol | Obj | ToolReference;
202
+ /** ToString without consulting program-defined methods. */
203
+ export declare const coerceToString: (value: Value) => string;
204
+ /** ToNumber without consulting program-defined methods; tool references are not numbers. */
205
+ export declare const coerceToNumber: (value: Value) => number;
206
+ /** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */
207
+ export declare const isRuntimeReference: (value: Value) => boolean;
123
208
  export declare const parseArrayIndex: (key: string | number) => number | undefined;
124
209
  /** The own property under `key`, including an array's live indexes and `length`. */
125
210
  export declare const own: (target: Obj, key: PropertyKey) => Slot | undefined;
126
211
  export declare const hasOwn: (target: Obj, key: PropertyKey) => boolean;
127
- export declare const getOwn: (target: Obj, key: PropertyKey) => unknown;
212
+ export declare const getOwn: (target: Obj, key: PropertyKey) => Value;
128
213
  /** [[Get]]: walks the prototype chain; accessors see `receiver`, which is the primitive for wrapper prototypes. */
129
- export declare const get: (target: Obj, key: PropertyKey, receiver?: unknown) => unknown;
214
+ export declare const get: (target: Obj, key: PropertyKey, receiver?: Value) => Value;
130
215
  export declare const has: (target: Obj, key: PropertyKey) => boolean;
131
- export declare const hasPrototype: (value: unknown, proto: Obj) => boolean;
216
+ export declare const hasPrototype: (value: Value, proto: Obj) => boolean;
132
217
  /** [[Set]]: an inherited setter or read-only property decides before an own data property is created. */
133
- export declare const set: (target: Obj, key: PropertyKey, value: unknown) => boolean;
218
+ export declare const set: (target: Obj, key: PropertyKey, value: Value) => boolean;
134
219
  /** [[DefineOwnProperty]] for a data property, ignoring the chain. */
135
- export declare const define: (target: Obj, key: PropertyKey, value: unknown, attrs?: Attributes) => void;
220
+ export declare const define: (target: Obj, key: PropertyKey, value: Value, attrs?: Attributes) => void;
136
221
  export declare const defineAccessor: (target: Obj, key: PropertyKey, get: Getter | undefined, set?: Setter) => void;
137
222
  export declare const remove: (target: Obj, key: PropertyKey) => boolean;
138
223
  export declare const ownKeys: (target: Obj) => Array<string | symbol>;
@@ -141,6 +226,6 @@ export declare const enumerableKeys: (target: Obj) => Array<string | symbol>;
141
226
  /** Own enumerable string keys: `Object.keys`. */
142
227
  export declare const keys: (target: Obj) => Array<string>;
143
228
  /** Own enumerable string entries: `Object.entries` and serialization. */
144
- export declare const entries: (target: Obj) => Array<[string, unknown]>;
145
- export declare const record: (proto: Obj, fields: Record<string, unknown>) => Obj;
229
+ export declare const entries: (target: Obj) => Array<[string, Value]>;
230
+ export declare const record: (proto: Obj, fields: Record<string, Value>) => Obj;
146
231
  export declare const assign: (target: Obj, source: Obj, skip?: ReadonlySet<PropertyKey>) => void;
@@ -1,3 +1,4 @@
1
+ import { ToolReference } from "../tool-runtime.js";
1
2
  import { checkArrayLength } from "./limits.js";
2
3
  import { AsyncIteratorSymbol, IteratorSymbol, } from "./model.js";
3
4
  /** Ordinary assignment: writable, enumerable, configurable. */
@@ -8,32 +9,110 @@ export const hidden = { writable: true, enumerable: false, configurable: true };
8
9
  export const readonly = { writable: false, enumerable: false, configurable: true };
9
10
  /** Constants such as `Math.PI` and a constructor's `prototype`. */
10
11
  export const frozen = { writable: false, enumerable: false, configurable: false };
11
- /** An object owned by the program: own properties plus a prototype link. */
12
+ /**
13
+ * An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a
14
+ * built-in kind of object prints, coerces, iterates, and crosses to the host.
15
+ */
12
16
  export class Obj {
13
17
  proto;
14
18
  props = new Map();
15
19
  constructor(proto) {
16
20
  this.proto = proto;
17
21
  }
22
+ /** The class name `Object.prototype.toString` reports: `[object Map]`. */
23
+ tag = "Object";
24
+ /** How diagnostics refer to a value of this kind. */
25
+ get describe() {
26
+ if (this.tag === "Object")
27
+ return "a data object";
28
+ return `${/^[AEIO]/.test(this.tag) ? "an" : "a"} ${this.tag}`;
29
+ }
30
+ /** ToString without consulting program-defined methods. */
31
+ toString() {
32
+ return `[object ${this.tag}]`;
33
+ }
34
+ /** ToPrimitive without consulting program-defined methods: only a Date answers a number hint differently. */
35
+ toPrimitive(hint) {
36
+ return this.toString();
37
+ }
38
+ /** ToNumber without consulting program-defined methods. */
39
+ toNumber() {
40
+ return Number(this.toPrimitive("number"));
41
+ }
42
+ /** How `console.log` shows the value; `item` formats a child with cycle and depth tracking. */
43
+ inspect(item) {
44
+ return `{${entries(this)
45
+ .map(([key, value]) => `${JSON.stringify(key)}:${item(value)}`)
46
+ .join(",")}}`;
47
+ }
48
+ /** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */
49
+ toHost(item) {
50
+ return Object.fromEntries(entries(this)
51
+ .filter(([key]) => key !== "__proto__")
52
+ .map(([key, value]) => [key, item(value)]));
53
+ }
54
+ /** The built-in iteration `for...of` and spread use, when this kind of object has one. */
55
+ iterator(builtins) {
56
+ return undefined;
57
+ }
18
58
  }
19
59
  export class Arr extends Obj {
20
60
  items;
61
+ tag = "Array";
21
62
  constructor(proto, items = []) {
22
63
  super(proto);
23
64
  this.items = items;
24
65
  }
66
+ get describe() {
67
+ return "an array";
68
+ }
69
+ toString() {
70
+ return this.items.map((item) => (item === null || item === undefined ? "" : coerceToString(item))).join(",");
71
+ }
72
+ inspect(item) {
73
+ return `[${this.items.map(item).join(",")}]`;
74
+ }
75
+ toHost(item) {
76
+ return this.items.map(item);
77
+ }
78
+ iterator() {
79
+ return this.items.values();
80
+ }
25
81
  }
26
82
  /** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */
27
83
  export class ErrorObj extends Obj {
84
+ tag = "Error";
28
85
  /** The interpreter failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. */
29
86
  host;
87
+ /** Error.prototype.toString: "name: message", or just one when the other is empty. */
88
+ toString() {
89
+ const name = get(this, "name");
90
+ const message = get(this, "message");
91
+ const shownName = typeof name === "string" ? name : "Error";
92
+ const shownMessage = typeof message === "string" ? message : "";
93
+ if (shownMessage === "")
94
+ return shownName;
95
+ if (shownName === "")
96
+ return shownMessage;
97
+ return `${shownName}: ${shownMessage}`;
98
+ }
99
+ }
100
+ /** Interpreter machinery a program can hold but never inspect, serialize, or hand to the host. */
101
+ export class Opaque extends Obj {
102
+ inspect() {
103
+ return "[opaque reference]";
104
+ }
30
105
  }
31
- export class Callable extends Obj {
106
+ export class Callable extends Opaque {
107
+ tag = "Function";
32
108
  constructor(proto, name, length) {
33
109
  super(proto);
34
110
  define(this, "length", length, readonly);
35
111
  define(this, "name", name, readonly);
36
112
  }
113
+ get describe() {
114
+ return "a function";
115
+ }
37
116
  }
38
117
  export class Fn extends Callable {
39
118
  parameters;
@@ -62,90 +141,193 @@ export class Native extends Callable {
62
141
  this.callback = options.callback ?? true;
63
142
  }
64
143
  }
65
- export class PromiseObj extends Obj {
144
+ export class PromiseObj extends Opaque {
66
145
  fiber;
146
+ tag = "Promise";
67
147
  constructor(proto, fiber) {
68
148
  super(proto);
69
149
  this.fiber = fiber;
70
150
  }
151
+ get describe() {
152
+ return "an un-awaited Promise";
153
+ }
154
+ inspect() {
155
+ return "[Promise (await it to get its value)]";
156
+ }
71
157
  }
72
- export class GeneratorObj extends Obj {
158
+ export class GeneratorObj extends Opaque {
73
159
  asynchronous;
74
160
  request;
161
+ tag = "Generator";
75
162
  constructor(proto, asynchronous, request) {
76
163
  super(proto);
77
164
  this.asynchronous = asynchronous;
78
165
  this.request = request;
79
166
  }
167
+ get describe() {
168
+ return "a generator";
169
+ }
80
170
  }
81
171
  /** A built-in collection iterator: live over the host collection, yielding program values. */
82
- export class IteratorObj extends Obj {
83
- iterator;
84
- constructor(proto, iterator) {
172
+ export class IteratorObj extends Opaque {
173
+ source;
174
+ tag = "Iterator";
175
+ constructor(proto, source) {
85
176
  super(proto);
86
- this.iterator = iterator;
177
+ this.source = source;
178
+ }
179
+ get describe() {
180
+ return "an iterator";
181
+ }
182
+ iterator() {
183
+ return this.source;
184
+ }
185
+ }
186
+ /** A built-in object around a host value: data-like, so it prints as itself and crosses to extensions as a copy. */
187
+ export class Wrapper extends Obj {
188
+ inspect(item) {
189
+ return this.toString();
87
190
  }
88
191
  }
89
- export class DateObj extends Obj {
192
+ export class DateObj extends Wrapper {
90
193
  time;
194
+ tag = "Date";
91
195
  constructor(proto, time) {
92
196
  super(proto);
93
197
  this.time = time;
94
198
  }
199
+ toString() {
200
+ return Number.isFinite(this.time) ? new Date(this.time).toISOString() : "Invalid Date";
201
+ }
202
+ toPrimitive(hint) {
203
+ return hint === "number" ? this.time : this.toString();
204
+ }
205
+ toHost() {
206
+ return new Date(this.time);
207
+ }
95
208
  }
96
- export class RegExpObj extends Obj {
209
+ export class RegExpObj extends Wrapper {
210
+ tag = "RegExp";
97
211
  regex;
98
212
  constructor(proto, pattern, flags) {
99
213
  super(proto);
100
214
  this.regex = new RegExp(pattern, flags);
101
215
  }
216
+ toString() {
217
+ return `/${this.regex.source}/${this.regex.flags}`;
218
+ }
219
+ toHost() {
220
+ return new RegExp(this.regex.source, this.regex.flags);
221
+ }
102
222
  }
103
- export class MapObj extends Obj {
223
+ export class MapObj extends Wrapper {
224
+ tag = "Map";
104
225
  map = new Map();
226
+ inspect(item) {
227
+ return `Map(${this.map.size}) [${[...this.map].map(([key, value]) => `[${item(key)},${item(value)}]`).join(",")}]`;
228
+ }
229
+ toHost(item) {
230
+ return new Map([...this.map].map(([key, value]) => [item(key), item(value)]));
231
+ }
232
+ iterator(builtins) {
233
+ return this.map.entries().map((entry) => new Arr(builtins.Array, entry));
234
+ }
105
235
  }
106
- export class SetObj extends Obj {
236
+ export class SetObj extends Wrapper {
237
+ tag = "Set";
107
238
  set = new Set();
239
+ inspect(item) {
240
+ return `Set(${this.set.size}) [${[...this.set].map(item).join(",")}]`;
241
+ }
242
+ toHost(item) {
243
+ return new Set([...this.set].map(item));
244
+ }
245
+ iterator() {
246
+ return this.set.values();
247
+ }
108
248
  }
109
- export class URLSearchParamsObj extends Obj {
249
+ export class URLSearchParamsObj extends Wrapper {
110
250
  params;
251
+ tag = "URLSearchParams";
111
252
  constructor(proto, params) {
112
253
  super(proto);
113
254
  this.params = params;
114
255
  }
256
+ toString() {
257
+ return this.params.toString();
258
+ }
259
+ toHost() {
260
+ return new URLSearchParams(this.params);
261
+ }
262
+ iterator(builtins) {
263
+ return this.params.entries().map((entry) => new Arr(builtins.Array, entry));
264
+ }
115
265
  }
116
- export class HeadersObj extends Obj {
266
+ export class HeadersObj extends Wrapper {
117
267
  headers;
268
+ tag = "Headers";
118
269
  constructor(proto, headers) {
119
270
  super(proto);
120
271
  this.headers = headers;
121
272
  }
273
+ inspect() {
274
+ return `Headers ${JSON.stringify(Object.fromEntries(this.headers))}`;
275
+ }
276
+ toHost() {
277
+ return new Headers(this.headers);
278
+ }
279
+ iterator(builtins) {
280
+ // Bun's Headers typings lack the iterator helpers, so the host iterator is lifted first.
281
+ return Iterator.from(this.headers.entries()).map((entry) => new Arr(builtins.Array, entry));
282
+ }
122
283
  }
123
- export class URLObj extends Obj {
284
+ export class URLObj extends Wrapper {
124
285
  url;
286
+ tag = "URL";
125
287
  searchParams;
126
288
  constructor(proto, searchParamsProto, url) {
127
289
  super(proto);
128
290
  this.url = url;
129
291
  this.searchParams = new URLSearchParamsObj(searchParamsProto, url.searchParams);
130
292
  }
293
+ toString() {
294
+ return this.url.href;
295
+ }
296
+ toHost() {
297
+ return new URL(this.url.href);
298
+ }
131
299
  }
132
300
  /** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */
133
- export class Bytes extends Obj {
301
+ export class Bytes extends Wrapper {
134
302
  bytes;
303
+ tag = "Uint8Array";
135
304
  constructor(proto, bytes) {
136
305
  super(proto);
137
306
  this.bytes = bytes;
138
307
  }
308
+ toString() {
309
+ return this.bytes.join(",");
310
+ }
311
+ inspect() {
312
+ return `Uint8Array(${this.bytes.length}) [${this.bytes.join(",")}]`;
313
+ }
314
+ toHost() {
315
+ return new Uint8Array(this.bytes);
316
+ }
317
+ iterator() {
318
+ return this.bytes.values();
319
+ }
139
320
  }
140
- /** Built-in objects that wrap a host value; data-like, but never plain data. */
141
- export const isWrapper = (value) => value instanceof DateObj ||
142
- value instanceof RegExpObj ||
143
- value instanceof MapObj ||
144
- value instanceof SetObj ||
145
- value instanceof URLObj ||
146
- value instanceof URLSearchParamsObj ||
147
- value instanceof HeadersObj ||
148
- value instanceof Bytes;
321
+ /** ToString without consulting program-defined methods. */
322
+ export const coerceToString = (value) => (value instanceof Obj ? value.toString() : String(value));
323
+ /** ToNumber without consulting program-defined methods; tool references are not numbers. */
324
+ export const coerceToNumber = (value) => {
325
+ if (value instanceof Obj)
326
+ return value.toNumber();
327
+ return value instanceof ToolReference ? Number.NaN : Number(value);
328
+ };
329
+ /** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */
330
+ export const isRuntimeReference = (value) => value instanceof Opaque || value instanceof Wrapper || value instanceof ToolReference;
149
331
  const MAX_ARRAY_INDEX = 4_294_967_295;
150
332
  export const parseArrayIndex = (key) => {
151
333
  const property = String(key);
@@ -1,6 +1,6 @@
1
1
  import { Effect, Exit, Scope } from "effect";
2
2
  import type { Diagnostic } from "../codemode.js";
3
- import { Obj, PromiseObj } from "./objects.js";
3
+ import { Obj, PromiseObj, type Value } from "./objects.js";
4
4
  import type { Interpreter } from "./interpreter.js";
5
5
  export declare class Pending<R> {
6
6
  private readonly scope;
@@ -13,16 +13,16 @@ export declare class Pending<R> {
13
13
  constructor(scope: Scope.Scope, proto: Obj);
14
14
  createWithSelf(body: (self: {
15
15
  promise?: PromiseObj;
16
- }) => Effect.Effect<unknown, unknown, R>): Effect.Effect<PromiseObj, never, R>;
17
- create(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<PromiseObj, never, R>;
16
+ }) => Effect.Effect<Value, unknown, R>): Effect.Effect<PromiseObj, never, R>;
17
+ create(effect: Effect.Effect<Value, unknown, R>): Effect.Effect<PromiseObj, never, R>;
18
18
  markObserved(promise: PromiseObj): void;
19
- await(promise: PromiseObj): Effect.Effect<Exit.Exit<unknown, unknown>>;
19
+ await(promise: PromiseObj): Effect.Effect<Exit.Exit<Value, unknown>>;
20
20
  fork(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<void, never, R>;
21
21
  diagnostics(): Array<Diagnostic>;
22
22
  interrupt(): Effect.Effect<Array<Diagnostic>>;
23
23
  }
24
- export declare const resolvePromiseValue: <R>(ctx: Interpreter<R>, value: unknown, own?: {
24
+ export declare const resolvePromiseValue: <R>(ctx: Interpreter<R>, value: Value, own?: {
25
25
  promise?: PromiseObj;
26
- }) => Effect.Effect<unknown, unknown, R>;
27
- export declare const resolvePromise: <R>(ctx: Interpreter<R>, value: unknown) => Effect.Effect<PromiseObj, never, R>;
26
+ }) => Effect.Effect<Value, unknown, R>;
27
+ export declare const resolvePromise: <R>(ctx: Interpreter<R>, value: Value) => Effect.Effect<PromiseObj, never, R>;
28
28
  export declare const promiseGlobal: <R>(ctx: Interpreter<R>) => import("./objects.js").Native<R>;
@@ -1,7 +1,8 @@
1
- /** Values that cannot cross the data boundary. */
2
- export declare const isRuntimeReference: (value: unknown) => boolean;
3
- export declare const containsRuntimeReference: (value: unknown) => boolean;
4
- export declare const containsOpaqueReference: (value: unknown) => boolean;
5
- export declare const rejectCircularInsertion: (container: object, value: unknown, label: string, seen?: Set<object>) => void;
6
- export declare const describeValue: (value: unknown) => string;
7
- export declare const typeofValue: (value: unknown) => string;
1
+ import { Obj, type Value } from "./objects.js";
2
+ /** Interpreter machinery that is never data, unlike a Date or Map, which cross some boundaries as copies. */
3
+ export declare const isOpaque: (value: Value) => boolean;
4
+ export declare const containsRuntimeReference: (value: Value) => boolean;
5
+ export declare const containsOpaqueReference: (value: Value) => boolean;
6
+ export declare const rejectCircularInsertion: (container: Obj, value: Value, label: string, seen?: Set<object>) => void;
7
+ export declare const describeValue: (value: Value) => string;
8
+ export declare const typeofValue: (value: Value) => string;