@opencode/codemode 2.2.7 → 2.3.0

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 (197) hide show
  1. package/README.md +11 -13
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +8 -22
  4. package/dist/codemode.js +17 -38
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +8 -2
  8. package/dist/extension.js +9 -0
  9. package/dist/{vm/host.d.ts → host.d.ts} +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  12. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  13. package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.js +1 -1
  15. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  16. package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
  17. package/dist/{vm/runtime → runtime}/execute.js +11 -9
  18. package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
  19. package/dist/{vm/runtime → runtime}/extensions.js +39 -4
  20. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  21. package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
  22. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  23. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  24. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  25. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  27. package/dist/scan.js +2 -2
  28. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
  29. package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
  30. package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
  31. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
  32. package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
  33. package/dist/{vm/snapshot.js → snapshot.js} +1 -1
  34. package/dist/stdlib/array.d.ts +14 -9
  35. package/dist/stdlib/array.js +89 -276
  36. package/dist/stdlib/bytes.d.ts +11 -7
  37. package/dist/stdlib/bytes.js +112 -79
  38. package/dist/stdlib/collections.d.ts +7 -7
  39. package/dist/stdlib/collections.js +30 -339
  40. package/dist/stdlib/console.d.ts +2 -2
  41. package/dist/stdlib/console.js +3 -3
  42. package/dist/stdlib/date.d.ts +2 -2
  43. package/dist/stdlib/date.js +21 -30
  44. package/dist/stdlib/headers.d.ts +2 -2
  45. package/dist/stdlib/headers.js +15 -33
  46. package/dist/stdlib/iterator.d.ts +2 -2
  47. package/dist/stdlib/iterator.js +10 -302
  48. package/dist/stdlib/json.d.ts +2 -2
  49. package/dist/stdlib/json.js +15 -31
  50. package/dist/stdlib/math.d.ts +2 -7
  51. package/dist/stdlib/math.js +6 -30
  52. package/dist/stdlib/number.d.ts +3 -3
  53. package/dist/stdlib/number.js +8 -8
  54. package/dist/stdlib/object.d.ts +4 -4
  55. package/dist/stdlib/object.js +12 -30
  56. package/dist/stdlib/regexp.d.ts +6 -5
  57. package/dist/stdlib/regexp.js +18 -13
  58. package/dist/stdlib/string.d.ts +2 -2
  59. package/dist/stdlib/string.js +53 -53
  60. package/dist/stdlib/url.d.ts +21 -9
  61. package/dist/stdlib/url.js +55 -55
  62. package/dist/stdlib/value.d.ts +3 -4
  63. package/dist/stdlib/value.js +5 -6
  64. package/dist/stdlib/web.d.ts +4 -4
  65. package/dist/stdlib/web.js +7 -7
  66. package/dist/tool-runtime.d.ts +14 -13
  67. package/dist/tool-runtime.js +245 -53
  68. package/dist/tool-schema.d.ts +5 -0
  69. package/dist/tool-schema.js +36 -16
  70. package/dist/tool.d.ts +24 -9
  71. package/dist/tool.js +15 -22
  72. package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
  73. package/dist/{vm/vm.js → vm.js} +1 -1
  74. package/package.json +3 -5
  75. package/dist/interpreter/callback.d.ts +0 -35
  76. package/dist/interpreter/callback.js +0 -98
  77. package/dist/interpreter/errors.js +0 -173
  78. package/dist/interpreter/execute.d.ts +0 -14
  79. package/dist/interpreter/execute.js +0 -207
  80. package/dist/interpreter/extensions.d.ts +0 -9
  81. package/dist/interpreter/extensions.js +0 -248
  82. package/dist/interpreter/generators.d.ts +0 -3
  83. package/dist/interpreter/generators.js +0 -168
  84. package/dist/interpreter/globals.js +0 -144
  85. package/dist/interpreter/interpreter.d.ts +0 -48
  86. package/dist/interpreter/interpreter.js +0 -2253
  87. package/dist/interpreter/intrinsics.d.ts +0 -15
  88. package/dist/interpreter/intrinsics.js +0 -105
  89. package/dist/interpreter/model.d.ts +0 -68
  90. package/dist/interpreter/model.js +0 -54
  91. package/dist/interpreter/native.d.ts +0 -17
  92. package/dist/interpreter/native.js +0 -35
  93. package/dist/interpreter/objects.d.ts +0 -301
  94. package/dist/interpreter/objects.js +0 -592
  95. package/dist/interpreter/promises.d.ts +0 -18
  96. package/dist/interpreter/promises.js +0 -292
  97. package/dist/interpreter/runner.d.ts +0 -178
  98. package/dist/interpreter/runner.js +0 -480
  99. package/dist/interpreter/scope.d.ts +0 -16
  100. package/dist/interpreter/scope.js +0 -79
  101. package/dist/suspend-error.d.ts +0 -24
  102. package/dist/suspend-error.js +0 -25
  103. package/dist/vm/data.d.ts +0 -7
  104. package/dist/vm/data.js +0 -13
  105. package/dist/vm/index.d.ts +0 -6
  106. package/dist/vm/index.js +0 -6
  107. package/dist/vm/runtime/errors.d.ts +0 -23
  108. package/dist/vm/runtime/globals.d.ts +0 -6
  109. package/dist/vm/runtime/limits.d.ts +0 -10
  110. package/dist/vm/runtime/limits.js +0 -19
  111. package/dist/vm/runtime/modules.d.ts +0 -4
  112. package/dist/vm/runtime/modules.js +0 -33
  113. package/dist/vm/runtime/references.d.ts +0 -8
  114. package/dist/vm/runtime/references.js +0 -51
  115. package/dist/vm/stdlib/array.d.ts +0 -16
  116. package/dist/vm/stdlib/array.js +0 -311
  117. package/dist/vm/stdlib/bytes.d.ts +0 -11
  118. package/dist/vm/stdlib/bytes.js +0 -222
  119. package/dist/vm/stdlib/collections.d.ts +0 -7
  120. package/dist/vm/stdlib/collections.js +0 -188
  121. package/dist/vm/stdlib/console.d.ts +0 -6
  122. package/dist/vm/stdlib/console.js +0 -93
  123. package/dist/vm/stdlib/date.d.ts +0 -2
  124. package/dist/vm/stdlib/date.js +0 -123
  125. package/dist/vm/stdlib/headers.d.ts +0 -2
  126. package/dist/vm/stdlib/headers.js +0 -92
  127. package/dist/vm/stdlib/iterator.d.ts +0 -2
  128. package/dist/vm/stdlib/iterator.js +0 -30
  129. package/dist/vm/stdlib/json.d.ts +0 -3
  130. package/dist/vm/stdlib/json.js +0 -42
  131. package/dist/vm/stdlib/math.d.ts +0 -3
  132. package/dist/vm/stdlib/math.js +0 -73
  133. package/dist/vm/stdlib/number.d.ts +0 -3
  134. package/dist/vm/stdlib/number.js +0 -72
  135. package/dist/vm/stdlib/object.d.ts +0 -6
  136. package/dist/vm/stdlib/object.js +0 -201
  137. package/dist/vm/stdlib/regexp.d.ts +0 -8
  138. package/dist/vm/stdlib/regexp.js +0 -119
  139. package/dist/vm/stdlib/string.d.ts +0 -2
  140. package/dist/vm/stdlib/string.js +0 -272
  141. package/dist/vm/stdlib/url.d.ts +0 -25
  142. package/dist/vm/stdlib/url.js +0 -255
  143. package/dist/vm/stdlib/value.d.ts +0 -6
  144. package/dist/vm/stdlib/value.js +0 -32
  145. package/dist/vm/stdlib/web.d.ts +0 -5
  146. package/dist/vm/stdlib/web.js +0 -97
  147. /package/dist/{vm/async.js → async.js} +0 -0
  148. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  149. /package/dist/{vm/code.js → code.js} +0 -0
  150. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  151. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  152. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  153. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  154. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  155. /package/dist/{vm/generators.js → generators.js} +0 -0
  156. /package/dist/{vm/host.js → host.js} +0 -0
  157. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  158. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  159. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  160. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  161. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  163. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  164. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  165. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  166. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  168. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  169. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  170. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  172. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  173. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
  175. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  179. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  180. /package/dist/{interpreter → runtime}/limits.js +0 -0
  181. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  182. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  183. /package/dist/{interpreter → runtime}/modules.js +0 -0
  184. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  186. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  188. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  189. /package/dist/{interpreter → runtime}/references.js +0 -0
  190. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  192. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  194. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  196. /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -1,12 +1,12 @@
1
1
  import { Effect } from "effect";
2
- import { isAsync } from "../../extension.js";
3
- import { hooked } from "../../tool-runtime.js";
2
+ import { isAsync, StreamingResponse } from "../extension.js";
3
+ import { hooked } from "../tool-runtime.js";
4
4
  import { errorMessage, errorType } from "./errors.js";
5
5
  import { createErrorValue } from "./intrinsics.js";
6
6
  import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js";
7
- import { PendingThrow, rangeError, Throw, typeError } from "./model.js";
7
+ import { AsyncIteratorSymbol, PendingThrow, rangeError, Throw, typeError } from "./model.js";
8
8
  import { native } from "./native.js";
9
- import { define, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, PromiseObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
9
+ import { define, defineAccessor, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, PromiseObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
10
10
  import { describeValue, isOpaque } from "./references.js";
11
11
  import { Push } from "./signals.js";
12
12
  import { SnapshotError } from "../snapshot/codec.js";
@@ -74,6 +74,41 @@ export const extensionGlobals = (ctx, extensions) => {
74
74
  // method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read.
75
75
  if (value instanceof Date)
76
76
  return new DateObj(builtins.Date, Date.prototype.getTime.call(value));
77
+ if (value instanceof StreamingResponse) {
78
+ const response = value.response;
79
+ const body = response.body;
80
+ const stream = new Obj(builtins.Object);
81
+ if (body) {
82
+ defineAccessor(stream, "locked", () => body.locked);
83
+ define(stream, "cancel", next(async (reason) => body.cancel(reason), `${label}.body.cancel`));
84
+ define(stream, "getReader", next((options) => {
85
+ if (options?.mode !== undefined)
86
+ throw new TypeError("Only the default stream reader is supported.");
87
+ const reader = body.getReader();
88
+ return {
89
+ read: async () => reader.read(),
90
+ cancel: async (reason) => reader.cancel(reason),
91
+ releaseLock: () => reader.releaseLock(),
92
+ };
93
+ }, `${label}.body.getReader`));
94
+ define(stream, AsyncIteratorSymbol, next(() => {
95
+ const iterator = body.values();
96
+ return {
97
+ next: async () => iterator.next(),
98
+ return: async () => iterator.return(),
99
+ };
100
+ }, `${label}.body[Symbol.asyncIterator]`));
101
+ }
102
+ const result = new Obj(builtins.Object);
103
+ for (const key of ["url", "status", "statusText", "ok", "headers"])
104
+ define(result, key, next(response[key], `${label}.${key}`));
105
+ define(result, "redirected", value.redirected);
106
+ define(result, "body", body ? stream : null);
107
+ defineAccessor(result, "bodyUsed", () => response.bodyUsed);
108
+ for (const key of ["text", "json", "bytes", "arrayBuffer"])
109
+ define(result, key, next(async () => response[key](), `${label}.${key}`));
110
+ return result;
111
+ }
77
112
  if (value instanceof RegExp)
78
113
  return new RegExpObj(builtins.RegExp, value.source, value.flags);
79
114
  if (value instanceof Uint8Array)
@@ -15,7 +15,7 @@ import { headersGlobal } from "../stdlib/headers.js";
15
15
  import { iteratorGlobals } from "../stdlib/iterator.js";
16
16
  import { coercion } from "../stdlib/value.js";
17
17
  import { base64Global, cryptoGlobal, structuredCloneGlobal } from "../stdlib/web.js";
18
- import { ToolReference } from "../../tool-runtime.js";
18
+ import { ToolReference } from "../tool-runtime.js";
19
19
  import { errorGlobal } from "./errors.js";
20
20
  import { errorTypes } from "./intrinsics.js";
21
21
  import { constants, constructor, methods, native, receiver } from "./native.js";
@@ -1,4 +1,4 @@
1
- import type { ToolRuntime } from "../../tool-runtime.js";
1
+ import type { ToolRuntime } from "../tool-runtime.js";
2
2
  import type { ExtensionRequest } from "../scheduler.js";
3
3
  import type { HostBridge } from "../snapshot/codec.js";
4
4
  import { Vm } from "../vm.js";
@@ -1,6 +1,6 @@
1
1
  import type { Node } from "acorn";
2
2
  import type { ErrorType } from "./intrinsics.js";
3
- import type { DiagnosticKind } from "../../codemode.js";
3
+ import type { DiagnosticKind } from "../codemode.js";
4
4
  import type { ErrorObj, Value } from "./objects.js";
5
5
  /** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */
6
6
  export type AstNode = Node;
@@ -1,4 +1,4 @@
1
- import { ToolReference } from "../../tool-runtime.js";
1
+ import { ToolReference } from "../tool-runtime.js";
2
2
  import type { Builtins } from "./intrinsics.js";
3
3
  import { type AstNode, type PendingThrow } from "./model.js";
4
4
  import { type Convert, type GeneratorRequest, Invoke, type Push, type Resume } from "./signals.js";
@@ -1,4 +1,4 @@
1
- import { ToolReference } from "../../tool-runtime.js";
1
+ import { ToolReference } from "../tool-runtime.js";
2
2
  import { checkArrayLength } from "./limits.js";
3
3
  import { AsyncIteratorSymbol, IteratorSymbol, rangeError, Throw, typeError, } from "./model.js";
4
4
  import { Invoke } from "./signals.js";
@@ -1,5 +1,5 @@
1
1
  import type { Expression, MemberExpression, NewExpression, Super } from "acorn";
2
- import { ToolReference } from "../../tool-runtime.js";
2
+ import { ToolReference } from "../tool-runtime.js";
3
3
  import { type AstNode } from "./model.js";
4
4
  import { type Builtins } from "./intrinsics.js";
5
5
  import { type Cursor, Obj, type Value } from "./objects.js";
@@ -1,4 +1,4 @@
1
- import { ToolReference } from "../../tool-runtime.js";
1
+ import { ToolReference } from "../tool-runtime.js";
2
2
  import { invalidData, typeError } from "./model.js";
3
3
  import { checkStringLength } from "./limits.js";
4
4
  import { locate } from "./errors.js";
package/dist/scan.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Result } from "effect";
2
- import { parseProgram } from "./interpreter/execute.js";
3
- import { formatLocation } from "./interpreter/model.js";
2
+ import { parseProgram } from "./runtime/execute.js";
3
+ import { formatLocation } from "./runtime/model.js";
4
4
  /**
5
5
  * Extracts every `tools.<path>` referenced by a Code Mode program, in first-seen order. Fails on syntax errors,
6
6
  * dynamic bracket access (`tools[name]`), or aliasing/passing `tools` rather than calling `tools.<path>(...)` directly.
@@ -1,5 +1,5 @@
1
1
  import type { Json } from "./data.js";
2
- import type { ExtensionInvocation } from "../tool-runtime.js";
2
+ import type { ExtensionInvocation } from "./tool-runtime.js";
3
3
  import type { AstNode } from "./runtime/model.js";
4
4
  import type { Callable, Cursor, GeneratorObj, Obj, PromiseObj, Value } from "./runtime/objects.js";
5
5
  import type { Completion } from "./runtime/signals.js";
@@ -12,7 +12,7 @@ export type Json = null | boolean | number | string | ReadonlyArray<Json> | {
12
12
  * - `["s", name]` an interpreter symbol;
13
13
  * - `["i", name]` a built-in or host global, unchanged since the runtime was made;
14
14
  * - `["a", source, index]` an AST node (a call site, an error's location);
15
- * - `["x", data]` something the engine names itself (the VM's compiled code, by source and function index);
15
+ * - `["x", data]` something the VM names itself (compiled code, by source and function index);
16
16
  * - `["r", id]` another record.
17
17
  */
18
18
  export type Encoded = string | number | boolean | null | ReadonlyArray<Json>;
@@ -22,8 +22,9 @@ export type Record = {
22
22
  readonly [field: string]: Json;
23
23
  };
24
24
  /** A saved state can't be read, or doesn't fit this runtime. */
25
- import { SnapshotError } from "../../suspend-error.js";
26
- export { SnapshotError };
25
+ export declare class SnapshotError extends Error {
26
+ readonly name = "SnapshotError";
27
+ }
27
28
  /**
28
29
  * Where a function inside a value an extension returned came from, so a saved state can name it instead of losing it:
29
30
  * the extension, the value its function returned (`root`), which the extension's `snapshot` hooks save, and the path
@@ -65,9 +66,9 @@ export type EncodeEnv = {
65
66
  readonly lost: Array<string>;
66
67
  readonly strict: boolean;
67
68
  readonly hosts?: HostBridge | undefined;
68
- /** More classes to save, an engine's own (the VM's frames and closures), after the built-in ones. */
69
+ /** More classes to save (the VM's frames and closures), after the built-in ones. */
69
70
  readonly specs?: ReadonlyArray<ClassSpec> | undefined;
70
- /** The data an engine names an object or symbol by, saved as `["x", data]`, instead of copying it. */
71
+ /** The data the VM names an object or symbol by, saved as `["x", data]`, instead of copying it. */
71
72
  readonly external?: ((value: object | symbol) => Json | undefined) | undefined;
72
73
  };
73
74
  /** What the decoder asks of the fresh runtime it restores into. */
@@ -6,11 +6,12 @@ import { deleted, OrderedTable } from "../runtime/table.js";
6
6
  import { AsyncFromSyncIterator } from "../generators.js";
7
7
  import { HostCall, Resolving } from "../scheduler.js";
8
8
  import { TextDecoderObj } from "../stdlib/bytes.js";
9
- import { ToolError, toolError } from "../../tool-error.js";
10
- import { ToolReference, ToolRuntimeError } from "../../tool-runtime.js";
9
+ import { ToolError, toolError } from "../tool-error.js";
10
+ import { ToolReference, ToolRuntimeError } from "../tool-runtime.js";
11
11
  /** A saved state can't be read, or doesn't fit this runtime. */
12
- import { SnapshotError } from "../../suspend-error.js";
13
- export { SnapshotError };
12
+ export class SnapshotError extends Error {
13
+ name = "SnapshotError";
14
+ }
14
15
  // --- Registries ---
15
16
  const symbols = new Map([
16
17
  ["iterator", IteratorSymbol],
@@ -84,7 +85,13 @@ const builtinSpecs = () => [
84
85
  { name: "HeadersObj", ctor: HeadersObj, fields: ["headers"], obj: true, constants: { tag: "Headers" } },
85
86
  { name: "URLObj", ctor: URLObj, fields: ["searchParams", "url"], obj: true, constants: { tag: "URL" } },
86
87
  { name: "Bytes", ctor: Bytes, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } },
87
- { name: "TextDecoderObj", ctor: TextDecoderObj, fields: ["decoder"], obj: true, constants: { tag: "Object" } },
88
+ {
89
+ name: "TextDecoderObj",
90
+ ctor: TextDecoderObj,
91
+ fields: ["decoder", "ignoreBOM", "queued", "seen"],
92
+ obj: true,
93
+ constants: { tag: "Object" },
94
+ },
88
95
  {
89
96
  name: "AsyncFromSyncIterator",
90
97
  ctor: AsyncFromSyncIterator,
@@ -1,9 +1,12 @@
1
1
  import type { Interpreter } from "../runtime/interpreter.js";
2
- import type { ToolCall } from "../../tool-runtime.js";
3
- import { fromBytes, SnapshotError, toBytes } from "../../suspend-error.js";
4
- import type { Encoded, Record } from "./codec.js";
5
- export { fromBytes, SnapshotError, toBytes };
6
- /** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */
2
+ import type { ToolCall } from "../tool-runtime.js";
3
+ import { type Encoded, type Record, SnapshotError } from "./codec.js";
4
+ export { SnapshotError };
5
+ /** A snapshot as bytes: UTF-8 JSON. */
6
+ export declare const toBytes: (snapshot: Snapshot) => Uint8Array;
7
+ /** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
8
+ export declare const fromBytes: (bytes: Uint8Array) => unknown;
9
+ /** The snapshot format this build writes and reads (see `src/snapshot.ts`). */
7
10
  export declare const FORMAT = 2;
8
11
  /**
9
12
  * A suspended run's whole state as JSON: a header that says which build and sources it belongs to and what budget it
@@ -1,6 +1,17 @@
1
- import { fromBytes, SnapshotError, toBytes } from "../../suspend-error.js";
2
- export { fromBytes, SnapshotError, toBytes };
3
- /** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */
1
+ import { SnapshotError } from "./codec.js";
2
+ export { SnapshotError };
3
+ /** A snapshot as bytes: UTF-8 JSON. */
4
+ export const toBytes = (snapshot) => new TextEncoder().encode(JSON.stringify(snapshot));
5
+ /** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
6
+ export const fromBytes = (bytes) => {
7
+ try {
8
+ return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
9
+ }
10
+ catch (error) {
11
+ throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`);
12
+ }
13
+ };
14
+ /** The snapshot format this build writes and reads (see `src/snapshot.ts`). */
4
15
  export const FORMAT = 2;
5
16
  export const hostsId = (ctx) => hash([
6
17
  ...ctx.intrinsics.hosts(),
@@ -1,4 +1,4 @@
1
- import packageJson from "../../package.json" with { type: "json" };
1
+ import packageJson from "../package.json" with { type: "json" };
2
2
  import { staticNames, unchanged } from "./runtime/intrinsic-names.js";
3
3
  import { Fn, Obj } from "./runtime/objects.js";
4
4
  import { nodeId } from "./runtime/source.js";
@@ -1,11 +1,16 @@
1
- import type { Eval } from "../interpreter/runner.js";
2
- import { type Method } from "../interpreter/native.js";
3
- import { Obj, type Value } from "../interpreter/objects.js";
4
- import type { Interpreter } from "../interpreter/interpreter.js";
5
- export declare function sortArray<R>(ctx: Interpreter<R>, target: Array<Value>, comparator: Value, name: string): Eval<Array<Value>>;
6
- export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import { Invoke } from "../runtime/signals.js";
2
+ import type { Builtins } from "../runtime/intrinsics.js";
3
+ import { Arr, Bytes, type Value } from "../runtime/objects.js";
4
+ import type { Interpreter } from "../runtime/interpreter.js";
5
+ /** Where a sort puts its result: back into an array or bytes, or into a new array. */
6
+ export type SortInto = "Array.sort" | "Array.toSorted" | "Uint8Array.sort";
7
+ /** The most values the default sort sorts in one native call; more sort in bounded steps (see `sortDefault`). */
8
+ export declare const SORT_RUN = 4096;
7
9
  /**
8
- * The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
9
- * live; `wrap` builds the collection `map` and `filter` return.
10
+ * Sorts `target`'s values: at once without a comparator, unless there are many, otherwise in the prelude
11
+ * (`sortWith`), which calls it. The merges of a top-down merge sort are planned here, in post-order, so the prelude
12
+ * compares in the same order as the recursive sort. Holes and undefined values sort last without being compared.
10
13
  */
11
- export declare const callbackMethods: <R, T extends Obj>(ctx: Interpreter<R>, label: string, self: (thisValue: Value, name: string) => T, elements: (target: T) => ArrayLike<Value>, wrap: (values: Array<Value>) => Value) => Array<Method>;
14
+ export declare const sortArray: <R>(ctx: Interpreter<R>, target: Arr | Bytes, comparator: Value, into: SortInto) => Value | Invoke;
15
+ export declare const sorted: (builtins: Builtins, target: Arr | Bytes, into: SortInto, holes: number, result: Array<Value>) => Arr | Bytes;
16
+ export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;