@opencode/codemode 2.2.6 → 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 +115 -29
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +13 -22
  4. package/dist/codemode.js +18 -39
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +20 -1
  8. package/dist/extension.js +14 -1
  9. package/dist/{vm/host.d.ts → host.d.ts} +5 -2
  10. package/dist/{vm/host.js → host.js} +9 -4
  11. package/dist/index.d.ts +1 -1
  12. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  13. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
  15. package/dist/{vm/runtime → runtime}/driver.js +90 -20
  16. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  17. package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
  18. package/dist/{vm/runtime → runtime}/execute.js +45 -15
  19. package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
  20. package/dist/{vm/runtime → runtime}/extensions.js +121 -19
  21. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  22. package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
  23. package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
  24. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  25. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  27. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  28. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  29. package/dist/scan.js +2 -2
  30. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
  31. package/dist/{vm/scheduler.js → scheduler.js} +13 -2
  32. package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
  33. package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
  34. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
  35. package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
  36. package/dist/{vm/snapshot.js → snapshot.js} +5 -2
  37. package/dist/stdlib/array.d.ts +14 -9
  38. package/dist/stdlib/array.js +89 -276
  39. package/dist/stdlib/bytes.d.ts +11 -7
  40. package/dist/stdlib/bytes.js +112 -79
  41. package/dist/stdlib/collections.d.ts +7 -7
  42. package/dist/stdlib/collections.js +30 -339
  43. package/dist/stdlib/console.d.ts +2 -2
  44. package/dist/stdlib/console.js +3 -3
  45. package/dist/stdlib/date.d.ts +2 -2
  46. package/dist/stdlib/date.js +21 -30
  47. package/dist/stdlib/headers.d.ts +2 -2
  48. package/dist/stdlib/headers.js +15 -33
  49. package/dist/stdlib/iterator.d.ts +2 -2
  50. package/dist/stdlib/iterator.js +10 -303
  51. package/dist/stdlib/json.d.ts +2 -2
  52. package/dist/stdlib/json.js +15 -31
  53. package/dist/stdlib/math.d.ts +2 -7
  54. package/dist/stdlib/math.js +6 -30
  55. package/dist/stdlib/number.d.ts +3 -3
  56. package/dist/stdlib/number.js +8 -8
  57. package/dist/stdlib/object.d.ts +4 -4
  58. package/dist/stdlib/object.js +12 -30
  59. package/dist/stdlib/regexp.d.ts +6 -5
  60. package/dist/stdlib/regexp.js +18 -13
  61. package/dist/stdlib/string.d.ts +2 -2
  62. package/dist/stdlib/string.js +53 -53
  63. package/dist/stdlib/url.d.ts +21 -9
  64. package/dist/stdlib/url.js +55 -55
  65. package/dist/stdlib/value.d.ts +3 -4
  66. package/dist/stdlib/value.js +5 -6
  67. package/dist/stdlib/web.d.ts +4 -4
  68. package/dist/stdlib/web.js +7 -7
  69. package/dist/tool-runtime.d.ts +34 -12
  70. package/dist/tool-runtime.js +350 -68
  71. package/dist/tool-schema.d.ts +5 -0
  72. package/dist/tool-schema.js +36 -16
  73. package/dist/tool.d.ts +60 -2
  74. package/dist/tool.js +17 -1
  75. package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
  76. package/dist/{vm/vm.js → vm.js} +1 -1
  77. package/package.json +5 -7
  78. package/dist/interpreter/callback.d.ts +0 -24
  79. package/dist/interpreter/callback.js +0 -86
  80. package/dist/interpreter/errors.js +0 -173
  81. package/dist/interpreter/execute.d.ts +0 -14
  82. package/dist/interpreter/execute.js +0 -207
  83. package/dist/interpreter/extensions.d.ts +0 -9
  84. package/dist/interpreter/extensions.js +0 -248
  85. package/dist/interpreter/generators.d.ts +0 -3
  86. package/dist/interpreter/generators.js +0 -168
  87. package/dist/interpreter/globals.js +0 -144
  88. package/dist/interpreter/interpreter.d.ts +0 -48
  89. package/dist/interpreter/interpreter.js +0 -2253
  90. package/dist/interpreter/intrinsics.d.ts +0 -15
  91. package/dist/interpreter/intrinsics.js +0 -105
  92. package/dist/interpreter/model.d.ts +0 -68
  93. package/dist/interpreter/model.js +0 -54
  94. package/dist/interpreter/native.d.ts +0 -17
  95. package/dist/interpreter/native.js +0 -35
  96. package/dist/interpreter/objects.d.ts +0 -301
  97. package/dist/interpreter/objects.js +0 -592
  98. package/dist/interpreter/promises.d.ts +0 -18
  99. package/dist/interpreter/promises.js +0 -292
  100. package/dist/interpreter/runner.d.ts +0 -188
  101. package/dist/interpreter/runner.js +0 -492
  102. package/dist/interpreter/scope.d.ts +0 -16
  103. package/dist/interpreter/scope.js +0 -79
  104. package/dist/suspend-error.d.ts +0 -23
  105. package/dist/suspend-error.js +0 -25
  106. package/dist/vm/data.d.ts +0 -7
  107. package/dist/vm/data.js +0 -13
  108. package/dist/vm/index.d.ts +0 -6
  109. package/dist/vm/index.js +0 -6
  110. package/dist/vm/runtime/errors.d.ts +0 -23
  111. package/dist/vm/runtime/globals.d.ts +0 -6
  112. package/dist/vm/runtime/limits.d.ts +0 -10
  113. package/dist/vm/runtime/limits.js +0 -19
  114. package/dist/vm/runtime/modules.d.ts +0 -4
  115. package/dist/vm/runtime/modules.js +0 -33
  116. package/dist/vm/runtime/references.d.ts +0 -8
  117. package/dist/vm/runtime/references.js +0 -51
  118. package/dist/vm/stdlib/array.d.ts +0 -16
  119. package/dist/vm/stdlib/array.js +0 -311
  120. package/dist/vm/stdlib/bytes.d.ts +0 -11
  121. package/dist/vm/stdlib/bytes.js +0 -222
  122. package/dist/vm/stdlib/collections.d.ts +0 -7
  123. package/dist/vm/stdlib/collections.js +0 -188
  124. package/dist/vm/stdlib/console.d.ts +0 -6
  125. package/dist/vm/stdlib/console.js +0 -93
  126. package/dist/vm/stdlib/date.d.ts +0 -2
  127. package/dist/vm/stdlib/date.js +0 -123
  128. package/dist/vm/stdlib/headers.d.ts +0 -2
  129. package/dist/vm/stdlib/headers.js +0 -92
  130. package/dist/vm/stdlib/iterator.d.ts +0 -2
  131. package/dist/vm/stdlib/iterator.js +0 -30
  132. package/dist/vm/stdlib/json.d.ts +0 -3
  133. package/dist/vm/stdlib/json.js +0 -42
  134. package/dist/vm/stdlib/math.d.ts +0 -3
  135. package/dist/vm/stdlib/math.js +0 -73
  136. package/dist/vm/stdlib/number.d.ts +0 -3
  137. package/dist/vm/stdlib/number.js +0 -72
  138. package/dist/vm/stdlib/object.d.ts +0 -6
  139. package/dist/vm/stdlib/object.js +0 -201
  140. package/dist/vm/stdlib/regexp.d.ts +0 -8
  141. package/dist/vm/stdlib/regexp.js +0 -119
  142. package/dist/vm/stdlib/string.d.ts +0 -2
  143. package/dist/vm/stdlib/string.js +0 -272
  144. package/dist/vm/stdlib/url.d.ts +0 -25
  145. package/dist/vm/stdlib/url.js +0 -255
  146. package/dist/vm/stdlib/value.d.ts +0 -6
  147. package/dist/vm/stdlib/value.js +0 -32
  148. package/dist/vm/stdlib/web.d.ts +0 -5
  149. package/dist/vm/stdlib/web.js +0 -97
  150. /package/dist/{vm/async.js → async.js} +0 -0
  151. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  152. /package/dist/{vm/code.js → code.js} +0 -0
  153. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  154. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  155. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  156. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  157. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  158. /package/dist/{vm/generators.js → generators.js} +0 -0
  159. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  160. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  161. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  163. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  164. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  165. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  166. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  168. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  169. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  170. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  172. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  173. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  175. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  179. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  180. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  181. /package/dist/{interpreter → runtime}/limits.js +0 -0
  182. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  183. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  184. /package/dist/{interpreter → runtime}/modules.js +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  186. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  188. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  189. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  190. /package/dist/{interpreter → runtime}/references.js +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  192. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  194. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  196. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -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,
@@ -110,7 +117,7 @@ const builtinSpecs = () => [
110
117
  {
111
118
  name: "HostCall",
112
119
  ctor: HostCall,
113
- fields: ["status", "id", "request", "label", "blocking", "result", "resolving", "site"],
120
+ fields: ["status", "data", "admitted", "id", "key", "request", "label", "blocking", "result", "resolving", "site"],
114
121
  },
115
122
  { name: "Resolving", ctor: Resolving, fields: ["done", "scheduler", "promise", "site"] },
116
123
  { name: "Throw", ctor: Throw, fields: ["value"] },
@@ -124,7 +131,7 @@ const builtinSpecs = () => [
124
131
  ctor: Interpreter,
125
132
  anchor: "ctx",
126
133
  fields: ["logs", "globalScope", "modules", "namespaces"],
127
- skip: ["tools", "builtins", "vm", "intrinsics", "extensionWork", "sources", "prelude"],
134
+ skip: ["tools", "builtins", "vm", "intrinsics", "extensionWork", "hosts", "sources", "prelude"],
128
135
  },
129
136
  ];
130
137
  let builtins;
@@ -186,6 +193,8 @@ class Encoder {
186
193
  // A URL's `searchParams` is the same host object its URLSearchParams wraps; it is encoded once every URL is known.
187
194
  owners = new Map();
188
195
  params = [];
196
+ // One `HostValue` record per value an extension returned, however many of its functions the state holds.
197
+ roots = new Map();
189
198
  specs;
190
199
  constructor(env) {
191
200
  this.env = env;
@@ -296,7 +305,14 @@ class Encoder {
296
305
  return { t: spec.name, ...this.fields(object, spec) };
297
306
  }
298
307
  if (object instanceof Native) {
299
- // A host function made while the program ran, inside a value an extension returned: its name says what was lost.
308
+ // A host function made while the program ran: inside a value an extension returned. Its extension saves that
309
+ // value when it has `snapshot` hooks; otherwise its name says what was lost.
310
+ const origin = this.env.hosts?.origin(object);
311
+ if (origin !== undefined) {
312
+ const root = this.hostValue(origin.extension, origin.root);
313
+ const fields = this.fields(object, hostFunction, hostFunction.fields);
314
+ return { t: "HostFunction", e: origin.extension, h: root, k: [...origin.path], l: origin.label, ...fields };
315
+ }
300
316
  this.lose(`the function ${functionName(object)}`);
301
317
  return { t: "LostNative", ...this.fields(object, hostFunction, hostFunction.fields) };
302
318
  }
@@ -332,6 +348,19 @@ class Encoder {
332
348
  this.lose(what);
333
349
  return { t: "Lost", f: [what] };
334
350
  }
351
+ // A value an extension returned, as its `snapshot.encode` saves it: plain data, checked before it is copied.
352
+ hostValue(extension, root) {
353
+ const known = this.roots.get(root);
354
+ if (known !== undefined)
355
+ return known;
356
+ const data = plainData(this.env.hosts.encode(extension, root), extension, new Set());
357
+ const id = this.records.length;
358
+ const ref = ["r", id];
359
+ this.roots.set(root, ref);
360
+ this.records.push(undefined);
361
+ this.records[id] = { t: "HostValue", e: extension, d: this.value(data) };
362
+ return ref;
363
+ }
335
364
  lose(what) {
336
365
  if (this.env.strict)
337
366
  throw new SnapshotError(`Codec gap: ${what} has no data form.`);
@@ -421,6 +450,10 @@ class Decoder {
421
450
  filled;
422
451
  // Derived state is rebuilt once every record is filled in, since it reads other records.
423
452
  resets = [];
453
+ // Functions inside values extensions returned, wrapped again once every record is filled in.
454
+ hostFunctions = [];
455
+ // Those values, given back once each by their extension's `snapshot.decode`, by `HostValue` record.
456
+ hostValues = new Map();
424
457
  specs;
425
458
  constructor(records, env) {
426
459
  this.records = records;
@@ -464,7 +497,10 @@ class Decoder {
464
497
  return named;
465
498
  }
466
499
  case "LostNative":
500
+ case "HostFunction":
467
501
  return Object.create(Native.prototype);
502
+ case "HostValue":
503
+ return {};
468
504
  case "Interruption":
469
505
  return this.env.interruption();
470
506
  case "Error": {
@@ -607,6 +643,11 @@ class Decoder {
607
643
  native.callback = true;
608
644
  return;
609
645
  }
646
+ case "HostFunction":
647
+ this.fillClass(target, record, hostFunction, false);
648
+ this.hostFunctions.push([target, record]);
649
+ return;
650
+ case "HostValue":
610
651
  case "Interruption":
611
652
  case "URL":
612
653
  case "URLSearchParams":
@@ -643,6 +684,32 @@ class Decoder {
643
684
  finish() {
644
685
  for (const reset of this.resets)
645
686
  reset();
687
+ if (this.hostFunctions.length === 0)
688
+ return;
689
+ const hosts = this.env.hosts ?? invalid("a function from an extension is saved, and this runtime has no extensions.");
690
+ for (const [target, record] of this.hostFunctions) {
691
+ const extension = text(record.e, "an extension");
692
+ const path = list(record.k, "a path").map((key) => typeof key === "number" ? count(key, "a path index") : text(key, "a path key"));
693
+ hosts.rewrap(target, {
694
+ extension,
695
+ root: this.hostValue(hosts, record.h, extension),
696
+ path,
697
+ label: text(record.l, "a label"),
698
+ });
699
+ }
700
+ }
701
+ // A value an extension returned, given back once by its `snapshot.decode` from the data it saved.
702
+ hostValue(hosts, ref, extension) {
703
+ const id = Array.isArray(ref) && ref[0] === "r" ? count(ref[1], "a record id") : invalid("a host value is not a record.");
704
+ const known = this.hostValues.get(id);
705
+ if (known !== undefined)
706
+ return known;
707
+ const record = id < this.records.length ? this.at(id) : invalid(`record ${id} does not exist.`);
708
+ if (record.t !== "HostValue" || record.e !== extension)
709
+ invalid(`record ${id} is not a value of ${extension}.`);
710
+ const root = hosts.decode(extension, this.value(record.d));
711
+ this.hostValues.set(id, root);
712
+ return root;
646
713
  }
647
714
  // An object's prototype, extensibility, and properties. A named object's accessors stay the ones it was made with.
648
715
  fillObj(target, record) {
@@ -730,6 +797,43 @@ class Decoder {
730
797
  return invalid(`unknown tag ${String(value[0])}.`);
731
798
  }
732
799
  }
800
+ // What an extension's `snapshot.encode` gives must be plain data the codec copies and hands back to `decode`: JSON
801
+ // values and Uint8Arrays. Views (such as a `Buffer` slice on a shared slab) are copied into standalone `Uint8Array`s.
802
+ const plainData = (data, extension, seen) => {
803
+ if (data === null || typeof data === "string" || typeof data === "boolean")
804
+ return data;
805
+ if (typeof data === "number" && Number.isFinite(data))
806
+ return data;
807
+ if (data instanceof Uint8Array) {
808
+ return data.constructor === Uint8Array && data.byteOffset === 0 && data.byteLength === data.buffer.byteLength
809
+ ? data
810
+ : new Uint8Array(data);
811
+ }
812
+ const fail = (what) => {
813
+ throw new SnapshotError(`The extension "${extension}" saved a value it returned as data holding ${what}; snapshot.encode must give plain data (JSON values and Uint8Arrays).`);
814
+ };
815
+ if (typeof data !== "object") {
816
+ return fail(data === undefined ? "undefined" : typeof data === "number" ? String(data) : `a ${typeof data}`);
817
+ }
818
+ if (seen.has(data))
819
+ return fail("a circular reference");
820
+ const proto = Object.getPrototypeOf(data);
821
+ if (!Array.isArray(data) && proto !== Object.prototype && proto !== null) {
822
+ const name = data.constructor?.name ?? "object";
823
+ return fail(`${/^[AEIOU]/.test(name) ? "an" : "a"} ${name}`);
824
+ }
825
+ seen.add(data);
826
+ if (Array.isArray(data)) {
827
+ const out = data.map((item) => plainData(item, extension, seen));
828
+ seen.delete(data);
829
+ return out;
830
+ }
831
+ const out = proto === null ? Object.create(null) : {};
832
+ for (const [key, item] of Object.entries(data))
833
+ out[key] = plainData(item, extension, seen);
834
+ seen.delete(data);
835
+ return out;
836
+ };
733
837
  const functionName = (fn) => {
734
838
  const slot = fn.props.get("name");
735
839
  return slot !== undefined && "value" in slot && typeof slot.value === "string" && slot.value !== ""
@@ -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
@@ -19,8 +22,12 @@ export type Snapshot = {
19
22
  export type Header = {
20
23
  /** The build that wrote it: the package version, and a hash of the opcodes, the prelude's bytecode, codecs, and built-ins. */
21
24
  readonly build: string;
22
- /** A hash of the host's globals (extensions, a test harness), which the restoring runtime must have too. */
25
+ /**
26
+ * A hash of the host's globals (extensions, a test harness), and of each extension's name and whether it has
27
+ * `snapshot` hooks, which the restoring runtime must have too.
28
+ */
23
29
  readonly hosts: string;
30
+ readonly runId: string;
24
31
  /** The program texts, re-parsed and recompiled on restore, and checked against their hashes and bytecode hashes. */
25
32
  readonly sources: ReadonlyArray<{
26
33
  readonly id: string;
@@ -1,8 +1,22 @@
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
- export const hostsId = (ctx) => hash(ctx.intrinsics.hosts().join("\n"));
16
+ export const hostsId = (ctx) => hash([
17
+ ...ctx.intrinsics.hosts(),
18
+ ...(ctx.hosts?.extensions ?? []).map((extension) => `${extension.name} ${extension.hooked ? "hooks" : "no hooks"}`),
19
+ ].join("\n"));
6
20
  // --- Validation ---
7
21
  const fail = (message) => {
8
22
  throw new SnapshotError(`Invalid snapshot: ${message}`);
@@ -27,6 +41,7 @@ export const validate = (saved) => {
27
41
  const checked = {
28
42
  build: string(header.build, "the build"),
29
43
  hosts: string(header.hosts, "the host globals"),
44
+ runId: string(header.runId, "the run id"),
30
45
  sources: array(header.sources, "the sources").map((item) => {
31
46
  const source = object(item, "a source");
32
47
  return {
@@ -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";
@@ -34,7 +34,7 @@ const vmSpecs = () => (specs ??= [
34
34
  name: "HostCalls",
35
35
  ctor: HostCalls,
36
36
  anchor: "hosts",
37
- fields: ["queued", "running", "inbox", "nextID", "blocked", "input"],
37
+ fields: ["runId", "queued", "running", "inbox", "nextID", "blocked", "input"],
38
38
  skip: ["ctx", "promises", "extension", "extensionResume"],
39
39
  },
40
40
  {
@@ -111,6 +111,7 @@ export const captureVm = (vm, options) => {
111
111
  },
112
112
  lost,
113
113
  strict: options.strict,
114
+ hosts: ctx.hosts,
114
115
  specs: vmSpecs(),
115
116
  external: (value) => {
116
117
  if (value === HOLE)
@@ -128,6 +129,7 @@ export const captureVm = (vm, options) => {
128
129
  header: {
129
130
  build: buildId(),
130
131
  hosts: hostsId(ctx),
132
+ runId: vm.hosts.runId,
131
133
  sources: programs.map((source) => ({
132
134
  id: source.id,
133
135
  code: source.code,
@@ -179,6 +181,7 @@ export const restoreVm = (vm, saved, options) => {
179
181
  anchor: (name) => anchors.get(name),
180
182
  node: (source, index) => sources.get(source)?.nodes[index],
181
183
  interruption: options.interruption,
184
+ hosts: ctx.hosts,
182
185
  specs: vmSpecs(),
183
186
  external: (data) => {
184
187
  if (data === "hole")
@@ -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;