@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
@@ -1,6 +1,6 @@
1
- import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
2
- import { syntaxError, typeError } from "../interpreter/model.js";
3
- import { define, defineAccessor, Arr, Obj, RegExpObj, record, coerceToNumber, coerceToString, } from "../interpreter/objects.js";
1
+ import { constructor, methods, prototypeFrom, receiver } from "../runtime/native.js";
2
+ import { syntaxError, typeError } from "../runtime/model.js";
3
+ import { define, defineAccessor, Arr, Obj, RegExpObj, record, coerceToNumber, coerceToString, } from "../runtime/objects.js";
4
4
  const flagProperties = [
5
5
  "hasIndices",
6
6
  "global",
@@ -13,11 +13,15 @@ const flagProperties = [
13
13
  ];
14
14
  const regexFailureReason = (error) => (error instanceof Error ? error.message : String(error)).replace(/^Invalid regular expression:\s*/i, "");
15
15
  const escapeRegexHint = 'To match special characters like ( ) [ ] { } + * ? . literally, escape them with a backslash (e.g. "\\\\(") or test for them with String.includes instead.';
16
- export const toHostRegex = (arg, method, extraFlags = "") => {
16
+ /** Runs host regex work on a RegExp argument, sharing its `lastIndex`, or on a pattern string compiled for the call. */
17
+ export const withHostRegex = (arg, method, extraFlags, work) => {
17
18
  if (arg instanceof RegExpObj)
18
- return arg.regex;
19
+ return arg.use(work);
19
20
  // Anything else is a pattern string, as `new RegExp(arg)` would read it: undefined is the empty pattern.
20
21
  const source = arg === undefined ? "" : coerceToString(arg);
22
+ return work(compilePattern(source, method, extraFlags));
23
+ };
24
+ const compilePattern = (source, method, extraFlags) => {
21
25
  try {
22
26
  return new RegExp(source, extraFlags);
23
27
  }
@@ -39,12 +43,12 @@ export const matchToValue = (builtins, match) => {
39
43
  };
40
44
  export const constructRegExp = (builtins, args, proto = builtins.RegExp) => {
41
45
  const first = args[0];
42
- const pattern = first instanceof RegExpObj ? first.regex.source : first === undefined ? "" : coerceToString(first);
46
+ const pattern = first instanceof RegExpObj ? first.source : first === undefined ? "" : coerceToString(first);
43
47
  const flagsArg = args[1];
44
48
  if (flagsArg !== undefined && typeof flagsArg !== "string") {
45
49
  throw syntaxError(`RegExp flags must be a string of flag characters (e.g. "g", "gi"), not ${flagsArg === null ? "null" : typeof flagsArg}.`);
46
50
  }
47
- const flags = flagsArg ?? (first instanceof RegExpObj ? first.regex.flags : "");
51
+ const flags = flagsArg ?? (first instanceof RegExpObj ? first.flags : "");
48
52
  try {
49
53
  return new RegExpObj(proto, pattern, flags);
50
54
  }
@@ -77,11 +81,11 @@ export const regexpGlobal = (ctx) => {
77
81
  ],
78
82
  ]);
79
83
  const self = (thisValue, name) => receiver(RegExpObj, thisValue, `RegExp.prototype.${name}`);
80
- defineAccessor(proto, "source", (thisValue) => self(thisValue, "source").regex.source);
81
- defineAccessor(proto, "flags", (thisValue) => self(thisValue, "flags").regex.flags);
82
- // The host regex holds the only lastIndex, so exec/test and the String methods share one counter.
83
- defineAccessor(proto, "lastIndex", (thisValue) => self(thisValue, "lastIndex").regex.lastIndex, (thisValue, value) => {
84
- self(thisValue, "lastIndex").regex.lastIndex = coerceToNumber(value);
84
+ defineAccessor(proto, "source", (thisValue) => self(thisValue, "source").source);
85
+ defineAccessor(proto, "flags", (thisValue) => self(thisValue, "flags").flags);
86
+ // One lastIndex per RegExp, so exec/test and the String methods share one counter.
87
+ defineAccessor(proto, "lastIndex", (thisValue) => self(thisValue, "lastIndex").lastIndex, (thisValue, value) => {
88
+ self(thisValue, "lastIndex").lastIndex = coerceToNumber(value);
85
89
  });
86
90
  for (const name of flagProperties)
87
91
  defineAccessor(proto, name, (thisValue) => self(thisValue, name).regex[name]);
@@ -90,7 +94,8 @@ export const regexpGlobal = (ctx) => {
90
94
  1,
91
95
  (thisValue, args) => {
92
96
  const value = self(thisValue, name);
93
- const matched = value.regex.exec(coerceToString(args[0]));
97
+ const input = coerceToString(args[0]);
98
+ const matched = value.use((regex) => regex.exec(input));
94
99
  if (name === "test")
95
100
  return matched !== null;
96
101
  return matched === null ? null : matchToValue(builtins, matched);
@@ -1,2 +1,2 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const stringGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ export declare const stringGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,11 +1,12 @@
1
- import { constructor, fn, methods } from "../interpreter/native.js";
2
- import { checkArrayLength, checkStringLength } from "../interpreter/limits.js";
3
- import { invalidData, IteratorSymbol, rangeError, typeError } from "../interpreter/model.js";
4
- import { define, get, hidden, Arr, hostIterator, Obj, RegExpObj, record, coerceToInteger, coerceToNumber, coerceToString, } from "../interpreter/objects.js";
5
- import { containsOpaqueReference, typeofValue } from "../interpreter/references.js";
6
- import { applyCollectionCallback, isSupportedCallback, toPrimitiveString, withPrimitives, } from "../interpreter/callback.js";
7
- import { matchToValue, toHostRegex } from "./regexp.js";
8
- import { coercion } from "./value.js";
1
+ import { Invoke } from "../runtime/signals.js";
2
+ import { constructor, fn, methods } from "../runtime/native.js";
3
+ import { checkArrayLength, checkStringLength } from "../runtime/limits.js";
4
+ import { invalidData, IteratorSymbol, rangeError, typeError } from "../runtime/model.js";
5
+ import { define, get, hidden, Arr, positionIterator, Obj, RegExpObj, record, coerceToInteger, coerceToNumber, coerceToString, } from "../runtime/objects.js";
6
+ import { containsOpaqueReference, typeofValue } from "../runtime/references.js";
7
+ import { isSupportedCallback, requireCallback, withPrimitives } from "../runtime/callback.js";
8
+ import { matchToValue, withHostRegex } from "./regexp.js";
9
+ import { coerce } from "./value.js";
9
10
  // console is intercepted by the interpreter before reaching here.
10
11
  const requireDataArgument = (name, index, arg) => {
11
12
  if (containsOpaqueReference(arg)) {
@@ -18,9 +19,10 @@ const replaceAllNeedsGlobal = (pattern) => {
18
19
  throw typeError(`String.replaceAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.replace to replace only the first match.`);
19
20
  }
20
21
  };
21
- function* replaceWithCallback(ctx, value, name, args) {
22
+ // The matches are found first, as the host replaces; then the callback runs for each in order.
23
+ const replaceWithCallback = (ctx, value, name, args) => {
22
24
  const builtins = ctx.builtins;
23
- const apply = applyCollectionCallback(ctx, args[1], `String.${name}`);
25
+ const callback = requireCallback(args[1], `String.${name}`);
24
26
  const matches = [];
25
27
  // The host calls back with (match, ...captures, offset, string, groups?); only groups is not already a Value.
26
28
  const collect = (...callbackArgs) => {
@@ -36,12 +38,14 @@ function* replaceWithCallback(ctx, value, name, args) {
36
38
  };
37
39
  const pattern = args[0];
38
40
  if (pattern instanceof RegExpObj) {
39
- if (name === "replaceAll")
40
- replaceAllNeedsGlobal(pattern.regex);
41
- if (name === "replace")
42
- value.replace(pattern.regex, collect);
43
- else
44
- value.replaceAll(pattern.regex, collect);
41
+ pattern.use((regex) => {
42
+ if (name === "replaceAll")
43
+ replaceAllNeedsGlobal(regex);
44
+ if (name === "replace")
45
+ value.replace(regex, collect);
46
+ else
47
+ value.replaceAll(regex, collect);
48
+ });
45
49
  }
46
50
  else {
47
51
  const search = coerceToString(requireDataArgument(name, 0, pattern));
@@ -50,22 +54,20 @@ function* replaceWithCallback(ctx, value, name, args) {
50
54
  else
51
55
  value.replaceAll(search, collect);
52
56
  }
53
- const output = [];
54
- let end = 0;
55
- for (const match of matches) {
56
- const replacement = yield* apply(match.args);
57
- output.push(value.slice(end, match.offset), coerceToString(replacement));
58
- end = match.offset + match.match.length;
59
- }
60
- output.push(value.slice(end));
61
- return output.join("");
62
- }
57
+ const pieces = matches.map((match, index) => new Arr(builtins.Array, [
58
+ value.slice(index === 0 ? 0 : matches[index - 1].offset + matches[index - 1].match.length, match.offset),
59
+ new Arr(builtins.Array, match.args),
60
+ ]));
61
+ const last = matches.at(-1);
62
+ const rest = value.slice(last === undefined ? 0 : last.offset + last.match.length);
63
+ return new Invoke(ctx.prelude.fn("replaceWith"), undefined, [new Arr(builtins.Array, pieces), rest, callback], undefined);
64
+ };
63
65
  export const stringGlobal = (ctx) => {
64
66
  const builtins = ctx.builtins;
65
67
  const string = constructor(builtins, builtins.String, {
66
68
  name: "String",
67
69
  length: 1,
68
- call: coercion(ctx, "String").call,
70
+ call: (_, args) => coerce(ctx, "String", args),
69
71
  });
70
72
  const codeUnits = (name, op) => [
71
73
  name,
@@ -78,7 +80,7 @@ export const stringGlobal = (ctx) => {
78
80
  [
79
81
  "raw",
80
82
  1,
81
- function* (_, args) {
83
+ (_, args) => {
82
84
  const template = args[0];
83
85
  const raw = template instanceof Obj ? get(template, "raw") : undefined;
84
86
  if (!(raw instanceof Obj))
@@ -87,12 +89,11 @@ export const stringGlobal = (ctx) => {
87
89
  checkArrayLength(count);
88
90
  // Each literal is followed by its substitution, except the last literal, or when substitutions run out.
89
91
  const parts = Array.from({ length: count }, (_, index) => index + 1 < count && index + 1 < args.length ? [get(raw, index), args[index + 1]] : [get(raw, index)]).flat();
90
- const strings = [];
91
- for (const part of parts)
92
- strings.push(yield* toPrimitiveString(ctx, part));
93
- const output = strings.join("");
94
- checkStringLength(output.length);
95
- return output;
92
+ return withPrimitives("string", parts, (strings) => {
93
+ const output = strings.map(coerceToString).join("");
94
+ checkStringLength(output.length);
95
+ return output;
96
+ }, true);
96
97
  },
97
98
  ],
98
99
  ]);
@@ -118,7 +119,7 @@ export const stringGlobal = (ctx) => {
118
119
  if (thisValue === null || thisValue === undefined) {
119
120
  throw typeError(`String.prototype.${name} called on null or undefined.`);
120
121
  }
121
- return withPrimitives(ctx, ["string", ...hints], [thisValue, ...args.slice(0, hints.length)], ([value, ...primitives]) => op(coerceToString(value), [...primitives, ...args.slice(hints.length)]));
122
+ return withPrimitives(["string", ...hints], [thisValue, ...args.slice(0, hints.length)], ([value, ...primitives]) => op(coerceToString(value), [...primitives, ...args.slice(hints.length)]));
122
123
  },
123
124
  ];
124
125
  // includes, startsWith, and endsWith reject a RegExp before converting their search string and position.
@@ -126,15 +127,15 @@ export const stringGlobal = (ctx) => {
126
127
  if (args[0] instanceof RegExpObj) {
127
128
  throw typeError(`String.${name} cannot take a regular expression; use regex.test(string) or String.search instead.`);
128
129
  }
129
- return withPrimitives(ctx, ["string", "number"], args.slice(0, 2), (primitives) => op(value, str(name, primitives, 0), optNum(name, primitives, 1)));
130
+ return withPrimitives(["string", "number"], args.slice(0, 2), (primitives) => op(value, str(name, primitives, 0), optNum(name, primitives, 1)));
130
131
  });
131
132
  // match, matchAll, and search read a RegExp as is and convert anything else to its pattern text.
132
- const withPattern = (args, op) => args[0] instanceof RegExpObj ? op(args[0]) : withPrimitives(ctx, "string", [args[0]], ([text]) => op(text));
133
+ const withPattern = (args, op) => args[0] instanceof RegExpObj ? op(args[0]) : withPrimitives("string", [args[0]], ([text]) => op(text));
133
134
  const replace = (name) => simple(name, 2, (value, args) => {
134
135
  const pattern = args[0];
135
136
  const replacer = args[1];
136
137
  // A RegExp pattern is used as is; a plain one converts to its search string, then a non-callable replacement.
137
- return withPrimitives(ctx, "string", [pattern instanceof RegExpObj ? undefined : pattern, isSupportedCallback(replacer) ? undefined : replacer], ([search, replacement]) => {
138
+ return withPrimitives("string", [pattern instanceof RegExpObj ? undefined : pattern, isSupportedCallback(replacer) ? undefined : replacer], ([search, replacement]) => {
138
139
  if (isSupportedCallback(replacer)) {
139
140
  return replaceWithCallback(ctx, value, name, [pattern instanceof RegExpObj ? pattern : search, replacer]);
140
141
  }
@@ -143,11 +144,12 @@ export const stringGlobal = (ctx) => {
143
144
  }
144
145
  const primitives = [search, replacement];
145
146
  if (pattern instanceof RegExpObj) {
146
- const regex = pattern.regex;
147
147
  const text = str(name, primitives, 1);
148
- if (name === "replaceAll")
149
- replaceAllNeedsGlobal(regex);
150
- return name === "replace" ? value.replace(regex, text) : value.replaceAll(regex, text);
148
+ return pattern.use((regex) => {
149
+ if (name === "replaceAll")
150
+ replaceAllNeedsGlobal(regex);
151
+ return name === "replace" ? value.replace(regex, text) : value.replaceAll(regex, text);
152
+ });
151
153
  }
152
154
  if (name === "replace")
153
155
  return value.replace(str(name, primitives, 0), str(name, primitives, 1));
@@ -180,7 +182,7 @@ export const stringGlobal = (ctx) => {
180
182
  simple("split", 2, (value, args) => {
181
183
  const separator = args[0];
182
184
  // A RegExp separator is used as is; the limit converts before a plain separator does, as in the spec.
183
- return withPrimitives(ctx, ["number", "string"], [args[1], separator instanceof RegExpObj ? undefined : separator], ([limit, pattern]) => {
185
+ return withPrimitives(["number", "string"], [args[1], separator instanceof RegExpObj ? undefined : separator], ([limit, pattern]) => {
184
186
  const wrap = (parts) => new Arr(builtins.Array, parts);
185
187
  // Native: an undefined separator returns the whole string, not a split on "undefined",
186
188
  // unless the limit truncates to zero.
@@ -189,7 +191,7 @@ export const stringGlobal = (ctx) => {
189
191
  return wrap(requestedLimit !== undefined && requestedLimit >>> 0 === 0 ? [] : [value]);
190
192
  }
191
193
  const parts = separator instanceof RegExpObj
192
- ? value.split(separator.regex, requestedLimit)
194
+ ? separator.use((regex) => value.split(regex, requestedLimit))
193
195
  : value.split(str("split", [pattern], 0), requestedLimit === undefined ? undefined : requestedLimit >>> 0);
194
196
  checkArrayLength(parts.length);
195
197
  return wrap(parts);
@@ -209,8 +211,7 @@ export const stringGlobal = (ctx) => {
209
211
  simple("lastIndexOf", 1, (value, args) => value.lastIndexOf(str("lastIndexOf", args, 0), optNum("lastIndexOf", args, 1)), ["string", "number"]),
210
212
  replace("replace"),
211
213
  replace("replaceAll"),
212
- simple("match", 1, (value, args) => withPattern(args, (arg) => {
213
- const regex = toHostRegex(arg, "match");
214
+ simple("match", 1, (value, args) => withPattern(args, (arg) => withHostRegex(arg, "match", "", (regex) => {
214
215
  const matched = value.match(regex);
215
216
  if (matched === null)
216
217
  return null;
@@ -218,9 +219,8 @@ export const stringGlobal = (ctx) => {
218
219
  if (regex.global)
219
220
  return new Arr(builtins.Array, [...matched]);
220
221
  return matchToValue(builtins, matched);
221
- })),
222
- simple("matchAll", 1, (value, args) => withPattern(args, (arg) => {
223
- const regex = toHostRegex(arg, "matchAll", "g");
222
+ }))),
223
+ simple("matchAll", 1, (value, args) => withPattern(args, (arg) => withHostRegex(arg, "matchAll", "g", (regex) => {
224
224
  if (!regex.global) {
225
225
  throw typeError(`String.matchAll requires a regular expression with the global (g) flag: write /${regex.source}/${regex.flags}g, or use String.match for a single match.`);
226
226
  }
@@ -230,8 +230,8 @@ export const stringGlobal = (ctx) => {
230
230
  matches.push(matchToValue(builtins, match));
231
231
  }
232
232
  return new Arr(builtins.Array, matches);
233
- })),
234
- simple("search", 1, (value, args) => withPattern(args, (arg) => value.search(toHostRegex(arg, "search")))),
233
+ }))),
234
+ simple("search", 1, (value, args) => withPattern(args, (arg) => withHostRegex(arg, "search", "", (regex) => value.search(regex)))),
235
235
  simple("repeat", 1, (value, args) => {
236
236
  const count = num("repeat", args, 0);
237
237
  if (!Number.isFinite(count) || count < 0) {
@@ -261,12 +261,12 @@ export const stringGlobal = (ctx) => {
261
261
  simple("toWellFormed", 0, (value) => value.toWellFormed()),
262
262
  simple("charCodeAt", 1, (value, args) => value.charCodeAt(optNum("charCodeAt", args, 0) ?? 0), ["number"]),
263
263
  simple("codePointAt", 1, (value, args) => value.codePointAt(optNum("codePointAt", args, 0) ?? 0), ["number"]),
264
- simple("concat", 1, (value, args) => withPrimitives(ctx, "string", args, (parts) => {
264
+ simple("concat", 1, (value, args) => withPrimitives("string", args, (parts) => {
265
265
  const joined = value.concat(...parts.map((_, index) => str("concat", parts, index)));
266
266
  checkStringLength(joined.length);
267
267
  return joined;
268
268
  })),
269
269
  ]);
270
- define(builtins.String, IteratorSymbol, fn(builtins, "[Symbol.iterator]", 0, (thisValue) => hostIterator(builtins, self(thisValue, "[Symbol.iterator]")[Symbol.iterator]())), hidden);
270
+ define(builtins.String, IteratorSymbol, fn(builtins, "[Symbol.iterator]", 0, (thisValue) => positionIterator(builtins, self(thisValue, "[Symbol.iterator]"), "values")), hidden);
271
271
  return string;
272
272
  };
@@ -1,13 +1,25 @@
1
- import type { Eval } from "../interpreter/runner.js";
2
- import { type Value } from "../interpreter/objects.js";
3
- import type { Interpreter } from "../interpreter/interpreter.js";
1
+ import { Invoke } from "../runtime/signals.js";
2
+ import { Obj, type Callable, type Value } from "../runtime/objects.js";
3
+ import type { Interpreter } from "../runtime/interpreter.js";
4
4
  type UriFunction = "encodeURI" | "encodeURIComponent" | "decodeURI" | "decodeURIComponent";
5
- export declare const uriGlobal: <R>(ctx: Interpreter<R>, name: UriFunction) => import("../interpreter/objects.js").Native;
6
- export declare const urlGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
5
+ export declare const uriGlobal: <R>(ctx: Interpreter<R>, name: UriFunction) => import("../runtime/objects.js").Native;
6
+ export declare const urlGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
7
+ /** What `readPairs` builds from `[name, value]` pairs, or from a record of names to values when `init` is no iterable. */
8
+ export type PairsInto = {
9
+ readonly kind: "Headers" | "URLSearchParams";
10
+ readonly proto: Obj;
11
+ };
7
12
  /**
8
- * Reads a synchronous iterable of `[name, value]` pairs as strings; `undefined` when `init` is not iterable. As in
9
- * WebIDL, the whole sequence is converted before any pair's length is checked.
13
+ * Reads a synchronous iterable of `[name, value]` pairs as strings (in the prelude), then builds `into` from them; a
14
+ * record of names to values when `init` is not iterable. As in WebIDL, the whole sequence is converted before any
15
+ * pair's length is checked.
10
16
  */
11
- export declare function readPairs<R>(ctx: Interpreter<R>, init: Value, label: string): Eval<Array<[string, string]> | undefined>;
12
- export declare const urlSearchParamsGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
17
+ export declare const readPairs: <R>(ctx: Interpreter<R>, init: Value, label: string, into: PairsInto) => Invoke;
18
+ /** What `readPairs` makes of the pairs it read (an array of arrays of strings), or of a record. */
19
+ export declare const buildPairs: (into: PairsInto, value: Value, label: string) => Value;
20
+ /** The host validates header names and values and throws its own TypeError; the program gets one of its own. */
21
+ export declare const attempt: <T>(run: () => T) => T;
22
+ /** Headers and URLSearchParams `forEach`: the callback sees each `[name, value]` pair as listed when it started. */
23
+ export declare const pairsForEach: <R>(ctx: Interpreter<R>, target: Value, pairs: Array<[string, string]>, callback: Callable, thisArg: Value) => Invoke;
24
+ export declare const urlSearchParamsGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
13
25
  export {};
@@ -1,7 +1,8 @@
1
- import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
2
- import { IteratorSymbol, PendingThrow, typeError, uriError } from "../interpreter/model.js";
3
- import { define, defineAccessor, entries, get, hidden, Arr, hostIterator, Obj, URLObj, URLSearchParamsObj, coerceToString, isRuntimeReference, } from "../interpreter/objects.js";
4
- import { applyCollectionCallback, preserveConsumerError } from "../interpreter/callback.js";
1
+ import { Invoke } from "../runtime/signals.js";
2
+ import { constructor, fn, methods, prototypeFrom, receiver, requiresNew, iteratorMethods, } from "../runtime/native.js";
3
+ import { IteratorSymbol, PendingThrow, typeError, uriError } from "../runtime/model.js";
4
+ import { define, defineAccessor, entries, get, hidden, Arr, Obj, URLObj, URLSearchParamsObj, HeadersObj, coerceToString, isRuntimeReference, } from "../runtime/objects.js";
5
+ import { requireCallback } from "../runtime/callback.js";
5
6
  const urlProperties = [
6
7
  "href",
7
8
  "origin",
@@ -94,42 +95,54 @@ export const urlGlobal = (ctx) => {
94
95
  ]);
95
96
  return url;
96
97
  };
97
- function* readPair(ctx, value, label) {
98
- const cursor = yield* ctx.iterate(value);
99
- if (cursor === undefined)
100
- throw typeError(`${label} expects iterable [name, value] pairs.`);
101
- const items = [];
102
- while (true) {
103
- const step = yield* cursor.next();
104
- if (step.done)
105
- return items;
106
- items.push(yield* preserveConsumerError(cursor.close, pairString(step.value)));
107
- }
108
- }
109
- // A step, so a failing conversion closes the pair's iterator.
110
- function* pairString(value) {
111
- return coerceToString(value);
112
- }
113
98
  /**
114
- * Reads a synchronous iterable of `[name, value]` pairs as strings; `undefined` when `init` is not iterable. As in
115
- * WebIDL, the whole sequence is converted before any pair's length is checked.
99
+ * Reads a synchronous iterable of `[name, value]` pairs as strings (in the prelude), then builds `into` from them; a
100
+ * record of names to values when `init` is not iterable. As in WebIDL, the whole sequence is converted before any
101
+ * pair's length is checked.
116
102
  */
117
- export function* readPairs(ctx, init, label) {
118
- const cursor = yield* ctx.iterate(init);
119
- if (cursor === undefined)
120
- return undefined;
121
- const pairs = [];
122
- while (true) {
123
- const step = yield* cursor.next();
124
- if (step.done) {
125
- if (pairs.some((entry) => entry.length !== 2))
126
- throw typeError(`${label} expects iterable [name, value] pairs.`);
127
- return pairs;
103
+ export const readPairs = (ctx, init, label, into) => new Invoke(ctx.prelude.fn("readPairs"), undefined, [init, label, into.kind, into.proto], undefined);
104
+ /** What `readPairs` makes of the pairs it read (an array of arrays of strings), or of a record. */
105
+ export const buildPairs = (into, value, label) => {
106
+ if (!(value instanceof Arr))
107
+ return fromRecord(into, value);
108
+ const pairs = value.items.map((pair) => pair.items);
109
+ if (pairs.some((entry) => entry.length !== 2))
110
+ throw typeError(`${label} expects iterable [name, value] pairs.`);
111
+ if (into.kind === "URLSearchParams") {
112
+ return new URLSearchParamsObj(into.proto, new URLSearchParams(pairs));
113
+ }
114
+ return new HeadersObj(into.proto, attempt(() => new Headers(pairs)));
115
+ };
116
+ const fromRecord = (into, init) => {
117
+ if (into.kind === "Headers") {
118
+ if (!(init instanceof Obj) || isRuntimeReference(init)) {
119
+ throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.");
128
120
  }
129
- pairs.push(yield* preserveConsumerError(cursor.close, readPair(ctx, step.value, label)));
121
+ return new HeadersObj(into.proto, attempt(() => new Headers(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)])))));
122
+ }
123
+ if (isRuntimeReference(init)) {
124
+ throw typeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.");
125
+ }
126
+ if (!(init instanceof Obj)) {
127
+ throw typeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.");
128
+ }
129
+ return new URLSearchParamsObj(into.proto, new URLSearchParams(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)]))));
130
+ };
131
+ /** The host validates header names and values and throws its own TypeError; the program gets one of its own. */
132
+ export const attempt = (run) => {
133
+ try {
134
+ return run();
130
135
  }
131
- }
132
- function* constructURLSearchParams(ctx, init, proto) {
136
+ catch (error) {
137
+ throw typeError(error instanceof Error ? error.message : String(error));
138
+ }
139
+ };
140
+ /** Headers and URLSearchParams `forEach`: the callback sees each `[name, value]` pair as listed when it started. */
141
+ export const pairsForEach = (ctx, target, pairs, callback, thisArg) => {
142
+ const list = new Arr(ctx.builtins.Array, pairs.map((pair) => new Arr(ctx.builtins.Array, pair)));
143
+ return new Invoke(ctx.prelude.fn("pairsForEach"), undefined, [target, list, callback, thisArg], undefined);
144
+ };
145
+ const constructURLSearchParams = (ctx, init, proto) => {
133
146
  const wrap = (params) => new URLSearchParamsObj(proto, params);
134
147
  if (init === undefined)
135
148
  return wrap(new URLSearchParams());
@@ -140,17 +153,8 @@ function* constructURLSearchParams(ctx, init, proto) {
140
153
  if (init === null || typeof init === "number" || typeof init === "boolean") {
141
154
  return wrap(new URLSearchParams(coerceToString(init)));
142
155
  }
143
- const pairs = yield* readPairs(ctx, init, "new URLSearchParams(...)");
144
- if (pairs !== undefined)
145
- return wrap(new URLSearchParams(pairs));
146
- if (isRuntimeReference(init)) {
147
- throw typeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.");
148
- }
149
- if (!(init instanceof Obj)) {
150
- throw typeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.");
151
- }
152
- return wrap(new URLSearchParams(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)]))));
153
- }
156
+ return readPairs(ctx, init, "new URLSearchParams(...)", { kind: "URLSearchParams", proto });
157
+ };
154
158
  export const urlSearchParamsGlobal = (ctx) => {
155
159
  const builtins = ctx.builtins;
156
160
  const proto = builtins.URLSearchParams;
@@ -233,20 +237,16 @@ export const urlSearchParamsGlobal = (ctx) => {
233
237
  return undefined;
234
238
  },
235
239
  ],
236
- ["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").params.keys())],
237
- ["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").params.values())],
238
- ["entries", 0, (thisValue) => hostIterator(builtins, self(thisValue, "entries").iterator(builtins))],
240
+ ...iteratorMethods(builtins, self),
239
241
  ["toString", 0, (thisValue) => self(thisValue, "toString").params.toString()],
240
242
  [
241
243
  "forEach",
242
244
  1,
243
- function* (thisValue, args) {
245
+ (thisValue, args) => {
244
246
  requireArgs("forEach", args, 1);
245
247
  const target = self(thisValue, "forEach");
246
- const apply = applyCollectionCallback(ctx, args[0], "URLSearchParams.forEach");
247
- for (const [key, value] of Array.from(target.params.entries()))
248
- yield* apply([value, key, target], args[1]);
249
- return undefined;
248
+ const callback = requireCallback(args[0], "URLSearchParams.forEach");
249
+ return pairsForEach(ctx, target, Array.from(target.params.entries()), callback, args[1]);
250
250
  },
251
251
  ],
252
252
  ]);
@@ -1,7 +1,6 @@
1
- import { type Native, type Value } from "../interpreter/objects.js";
2
- import type { Interpreter } from "../interpreter/interpreter.js";
3
- export declare const compoundOperators: Set<string>;
1
+ import { type Native, type Value } from "../runtime/objects.js";
2
+ import type { Interpreter } from "../runtime/interpreter.js";
4
3
  export type Coercion = "Number" | "String" | "Boolean" | "parseInt" | "parseFloat" | "isFinite" | "isNaN";
5
- export declare const coerce: <R>(ctx: Interpreter<R>, name: Coercion, args: Array<Value>) => import("../interpreter/objects.js").NativeResult;
4
+ export declare const coerce: <R>(ctx: Interpreter<R>, name: Coercion, args: Array<Value>) => import("../runtime/objects.js").NativeResult;
6
5
  /** A global coercion function such as `Number` or `parseInt`. */
7
6
  export declare const coercion: <R>(ctx: Interpreter<R>, name: Coercion, length?: number) => Native;
@@ -1,7 +1,6 @@
1
- import { fn } from "../interpreter/native.js";
2
- import { coerceToNumber, coerceToString } from "../interpreter/objects.js";
3
- import { withPrimitives } from "../interpreter/callback.js";
4
- export const compoundOperators = new Set(["+=", "-=", "*=", "/=", "%=", "**=", "&=", "|=", "^=", "<<=", ">>=", ">>>="]);
1
+ import { fn } from "../runtime/native.js";
2
+ import { coerceToNumber, coerceToString } from "../runtime/objects.js";
3
+ import { withPrimitives } from "../runtime/callback.js";
5
4
  export const coerce = (ctx, name, args) => {
6
5
  // Native: Number() is 0 and String() is "", unlike their undefined-argument forms; the
7
6
  // other coercers match native through the undefined-argument path below.
@@ -15,9 +14,9 @@ export const coerce = (ctx, name, args) => {
15
14
  if (name === "Boolean")
16
15
  return Boolean(raw);
17
16
  if (name === "parseInt") {
18
- return withPrimitives(ctx, ["string", "number"], [raw, args[1]], ([text, radix]) => parseInt(coerceToString(text), coerceToNumber(radix)));
17
+ return withPrimitives(["string", "number"], [raw, args[1]], ([text, radix]) => parseInt(coerceToString(text), coerceToNumber(radix)));
19
18
  }
20
- return withPrimitives(ctx, name === "String" || name === "parseFloat" ? "string" : "number", [raw], ([value]) => {
19
+ return withPrimitives(name === "String" || name === "parseFloat" ? "string" : "number", [raw], ([value]) => {
21
20
  if (name === "Number")
22
21
  return coerceToNumber(value);
23
22
  if (name === "isFinite")
@@ -1,5 +1,5 @@
1
- import { Obj } from "../interpreter/objects.js";
2
- import type { Interpreter } from "../interpreter/interpreter.js";
3
- export declare const base64Global: <R>(ctx: Interpreter<R>, name: "atob" | "btoa") => import("../interpreter/objects.js").Native;
4
- export declare const structuredCloneGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import { Obj } from "../runtime/objects.js";
2
+ import type { Interpreter } from "../runtime/interpreter.js";
3
+ export declare const base64Global: <R>(ctx: Interpreter<R>, name: "atob" | "btoa") => import("../runtime/objects.js").Native;
4
+ export declare const structuredCloneGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
5
5
  export declare const cryptoGlobal: <R>(ctx: Interpreter<R>) => Obj;
@@ -1,8 +1,8 @@
1
- import { fn, methods } from "../interpreter/native.js";
2
- import { typeError } from "../interpreter/model.js";
3
- import { define, entries, get, getOwn, hasOwn, hidden, isRuntimeReference, Arr, Bytes, DateObj, ErrorObj, MapObj, Obj, RegExpObj, SetObj, WeakMapObj, WeakSetObj, coerceToString, } from "../interpreter/objects.js";
4
- import { createErrorValue, isErrorType } from "../interpreter/intrinsics.js";
5
- import { describeValue } from "../interpreter/references.js";
1
+ import { fn, methods } from "../runtime/native.js";
2
+ import { typeError } from "../runtime/model.js";
3
+ import { define, entries, get, getOwn, hasOwn, hidden, isRuntimeReference, Arr, Bytes, DateObj, ErrorObj, MapObj, Obj, RegExpObj, SetObj, WeakMapObj, WeakSetObj, coerceToString, } from "../runtime/objects.js";
4
+ import { createErrorValue, isErrorType } from "../runtime/intrinsics.js";
5
+ import { describeValue } from "../runtime/references.js";
6
6
  import { ToolReference } from "../tool-runtime.js";
7
7
  // WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies. Invalid input is a
8
8
  // TypeError as well; browsers throw a DOMException named InvalidCharacterError, which CodeMode does not have.
@@ -45,14 +45,14 @@ export const structuredCloneGlobal = (ctx) => fn(ctx.builtins, "structuredClone"
45
45
  }
46
46
  if (value instanceof SetObj) {
47
47
  const copy = remember(new SetObj(builtins.Set));
48
- for (const item of value.set)
48
+ for (const item of value.set.liveKeys())
49
49
  copy.set.add(clone(item));
50
50
  return copy;
51
51
  }
52
52
  if (value instanceof DateObj)
53
53
  return remember(new DateObj(builtins.Date, value.time));
54
54
  if (value instanceof RegExpObj) {
55
- return remember(new RegExpObj(builtins.RegExp, value.regex.source, value.regex.flags));
55
+ return remember(new RegExpObj(builtins.RegExp, value.source, value.flags));
56
56
  }
57
57
  if (value instanceof Bytes)
58
58
  return remember(new Bytes(builtins.Uint8Array, new Uint8Array(value.bytes)));