poe-code 14.0.73 → 14.0.75

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 (47) hide show
  1. package/dist/index.js +2732 -1188
  2. package/dist/index.js.map +4 -4
  3. package/dist/metafile.json +1 -1
  4. package/package.json +1 -1
  5. package/packages/safe-js/dist/chunks/{chunk-I2Z37MKZ.js → chunk-BAJI3K4B.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-XKCTB5FR.js → chunk-EOTY4PCT.js} +2902 -1425
  7. package/packages/safe-js/dist/chunks/chunk-EOTY4PCT.js.map +7 -0
  8. package/packages/safe-js/dist/cli.js +2 -2
  9. package/packages/safe-js/dist/core.js +1 -1
  10. package/packages/safe-js/dist/graph-depth.js +3 -0
  11. package/packages/safe-js/dist/index.js +2 -2
  12. package/packages/safe-js/dist/interp/cancel.js +3 -0
  13. package/packages/safe-js/dist/interp/globals/misc.js +2 -1
  14. package/packages/safe-js/dist/interp/globals/object.js +3 -0
  15. package/packages/safe-js/dist/interp/globals/regex.js +196 -9
  16. package/packages/safe-js/dist/interp/interpreter.js +14 -7
  17. package/packages/safe-js/dist/interp/iteration.js +9 -1
  18. package/packages/safe-js/dist/interp/methods/regex-split.d.ts +3 -0
  19. package/packages/safe-js/dist/interp/methods/regex-split.js +102 -0
  20. package/packages/safe-js/dist/interp/methods/regex.d.ts +8 -4
  21. package/packages/safe-js/dist/interp/methods/regex.js +141 -24
  22. package/packages/safe-js/dist/interp/methods/regexp-iterator.d.ts +12 -0
  23. package/packages/safe-js/dist/interp/methods/regexp-iterator.js +101 -0
  24. package/packages/safe-js/dist/interp/methods/string.d.ts +2 -0
  25. package/packages/safe-js/dist/interp/methods/string.js +415 -121
  26. package/packages/safe-js/dist/interp/object-model.d.ts +3 -0
  27. package/packages/safe-js/dist/interp/object-model.js +43 -10
  28. package/packages/safe-js/dist/interp/regex/compile-guard.js +26 -4
  29. package/packages/safe-js/dist/interp/regex/engine.d.ts +4 -0
  30. package/packages/safe-js/dist/interp/regex/engine.js +230 -30
  31. package/packages/safe-js/dist/interp/regex/parse.d.ts +42 -4
  32. package/packages/safe-js/dist/interp/regex/parse.js +429 -25
  33. package/packages/safe-js/dist/interp/regexp-iterator.d.ts +16 -0
  34. package/packages/safe-js/dist/interp/regexp-iterator.js +23 -0
  35. package/packages/safe-js/dist/interp/string-coercion.js +11 -7
  36. package/packages/safe-js/dist/interp/values.d.ts +2 -1
  37. package/packages/safe-js/dist/interp/values.js +39 -2
  38. package/packages/safe-js/dist/snapshot/dump-format.js +35 -3
  39. package/packages/safe-js/dist/snapshot/replay-data.d.ts +12 -1
  40. package/packages/safe-js/dist/snapshot/replay-data.js +42 -1
  41. package/packages/safe-js/dist/snapshot/replay-inputs.js +17 -0
  42. package/packages/safe-js/dist/snapshot/restore.js +23 -1
  43. package/packages/safe-js/dist/snapshot/serialize.d.ts +13 -2
  44. package/packages/safe-js/dist/snapshot/serialize.js +30 -11
  45. package/packages/safe-js/dist/snapshot/validation.js +22 -2
  46. package/packages/safe-js/dist/chunks/chunk-XKCTB5FR.js.map +0 -7
  47. /package/packages/safe-js/dist/chunks/{chunk-I2Z37MKZ.js.map → chunk-BAJI3K4B.js.map} +0 -0
@@ -22,7 +22,7 @@ import {
22
22
  resolveFsConfig,
23
23
  splitFrontmatter,
24
24
  withBrokenPipeGuard
25
- } from "./chunks/chunk-I2Z37MKZ.js";
25
+ } from "./chunks/chunk-BAJI3K4B.js";
26
26
  import {
27
27
  Budget,
28
28
  SandboxError,
@@ -34,7 +34,7 @@ import {
34
34
  replaceErrorStack,
35
35
  restore,
36
36
  run
37
- } from "./chunks/chunk-XKCTB5FR.js";
37
+ } from "./chunks/chunk-EOTY4PCT.js";
38
38
  import "./chunks/chunk-ZWJ5BJUI.js";
39
39
  import "./chunks/chunk-3YTQEPZI.js";
40
40
  import "./chunks/chunk-4YIDX44G.js";
@@ -5,7 +5,7 @@ import {
5
5
  defineExtension,
6
6
  lint,
7
7
  run
8
- } from "./chunks/chunk-XKCTB5FR.js";
8
+ } from "./chunks/chunk-EOTY4PCT.js";
9
9
  import "./chunks/chunk-4YIDX44G.js";
10
10
  export {
11
11
  Budget,
@@ -2,6 +2,7 @@ import { replaceErrorStack } from "./error/shape.js";
2
2
  import { SandboxError } from "./interp/budget.js";
3
3
  import { getSandboxArgumentEntries, isSandboxArguments } from "./interp/arguments.js";
4
4
  import { collectionIteratorState, isSandboxCollectionIterator } from "./interp/collection-iterator.js";
5
+ import { regexpIteratorState, isSandboxRegExpIterator } from "./interp/regexp-iterator.js";
5
6
  import { isSandboxMap, isSandboxSet } from "./interp/collection-brands.js";
6
7
  import { boxedDataProperties, isSandboxBox } from "./interp/boxed.js";
7
8
  import { regexGuestProperties } from "./interp/regexp-properties.js";
@@ -90,6 +91,8 @@ function graphEntries(value) {
90
91
  const entries = [];
91
92
  if (isSandboxCollectionIterator(value))
92
93
  entries.push([".<collection>", collectionIteratorState(value).collection]);
94
+ if (isSandboxRegExpIterator(value))
95
+ entries.push([".<matcher>", regexpIteratorState(value).matcher]);
93
96
  for (const key of Object.keys(value)) {
94
97
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
95
98
  if (descriptor !== undefined && "value" in descriptor)
@@ -26,7 +26,7 @@ import {
26
26
  runWithSpawnUsageAccumulator,
27
27
  splitFrontmatter,
28
28
  supportsSpawnMode
29
- } from "./chunks/chunk-I2Z37MKZ.js";
29
+ } from "./chunks/chunk-BAJI3K4B.js";
30
30
  import {
31
31
  Budget,
32
32
  FileSnapshotBackend,
@@ -50,7 +50,7 @@ import {
50
50
  registerPendingHostCallPolicy,
51
51
  restore,
52
52
  run
53
- } from "./chunks/chunk-XKCTB5FR.js";
53
+ } from "./chunks/chunk-EOTY4PCT.js";
54
54
  import "./chunks/chunk-ZWJ5BJUI.js";
55
55
  import "./chunks/chunk-3YTQEPZI.js";
56
56
  import "./chunks/chunk-4YIDX44G.js";
@@ -1,5 +1,6 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { collectionIteratorState, isSandboxCollectionIterator } from "./collection-iterator.js";
3
+ import { regexpIteratorState, isSandboxRegExpIterator } from "./regexp-iterator.js";
3
4
  import { getSandboxPrototype } from "./object-model.js";
4
5
  import { createSandboxPromise, isSandboxClosure, isSandboxMap, isSandboxPromise, isSandboxSet } from "./values.js";
5
6
  import { assertPromiseExecutionAllowed, interruptOnFatalPromiseRejection, onFatalPromiseRejection, observeSandboxPromise, trackSandboxPromise } from "./promise-tracker.js";
@@ -100,6 +101,8 @@ function registerCancelablePromises(value, signal) {
100
101
  else {
101
102
  if (isSandboxCollectionIterator(current))
102
103
  pending.push(collectionIteratorState(current).collection);
104
+ if (isSandboxRegExpIterator(current))
105
+ pending.push(regexpIteratorState(current).matcher);
103
106
  for (const descriptor of Object.values(Object.getOwnPropertyDescriptors(current))) {
104
107
  if ("value" in descriptor)
105
108
  pending.push(descriptor.value);
@@ -1,4 +1,5 @@
1
1
  import { isSandboxCollectionIterator } from "../collection-iterator.js";
2
+ import { isSandboxRegExpIterator } from "../regexp-iterator.js";
2
3
  import { assertSandboxGraphDepth } from "../../graph-depth.js";
3
4
  import { CompileScope } from "../regex/compile-guard.js";
4
5
  import { createNumericParsers } from "./numeric-parsers.js";
@@ -49,7 +50,7 @@ function structuredCloneSandboxValue(value, budget, parent) {
49
50
  }
50
51
  }
51
52
  function assertStructuredCloneable(value, seen) {
52
- if (isSandboxClosure(value) || isSandboxPromise(value) || isSandboxCollectionIterator(value)) {
53
+ if (isSandboxClosure(value) || isSandboxPromise(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value)) {
53
54
  throw new TypeError("structuredClone() cannot clone closures, promises, or collection iterators.");
54
55
  }
55
56
  if (typeof value !== "object" || value === null || seen.has(value)) {
@@ -7,6 +7,7 @@ import { isSandboxErrorConstructorInstance } from "../exceptions.js";
7
7
  import { isFloat32Array } from "../float32.js";
8
8
  import { hasHostObjectMember, isGuestHostObject } from "../host-capabilities.js";
9
9
  import { collectionIteratorState, isSandboxCollectionIterator } from "../collection-iterator.js";
10
+ import { isSandboxRegExpIterator } from "../regexp-iterator.js";
10
11
  import { getSandboxPrototype, getSandboxPropertyDescriptor, hasExplicitSandboxPrototype, installObjectPrototype, isGuestClosure, markDescriptorObject, materializeFunctionProperties, setSandboxPrototype } from "../object-model.js";
11
12
  import { toPropertyKey } from "../property-key.js";
12
13
  import { createSandboxClosure, isSandboxClosure, isSandboxGenerator, isSandboxMap, isSandboxPromise, isSandboxRegex, getRegexProperties, isSandboxSet } from "../values.js";
@@ -180,6 +181,8 @@ function typeTag(value, builtinOnly = false) {
180
181
  return "Map";
181
182
  if (isSandboxSet(value))
182
183
  return "Set";
184
+ if (isSandboxRegExpIterator(value))
185
+ return "RegExp String Iterator";
183
186
  if (isSandboxCollectionIterator(value))
184
187
  return collectionIteratorState(value).collectionKind === "map" ? "Map Iterator" : "Set Iterator";
185
188
  if (isSandboxPromise(value))
@@ -1,7 +1,17 @@
1
- import { createSandboxClosure, createSandboxRegex, isSandboxRegex, reconcileCompiledValues } from "../values.js";
1
+ import { createSandboxClosure, createSandboxRegex, isSandboxClosure, isSandboxRegex, reconcileCompiledValues } from "../values.js";
2
2
  import { CompileScope } from "../regex/compile-guard.js";
3
3
  import { SandboxError } from "../budget.js";
4
- import { sandboxString } from "../string-coercion.js";
4
+ import { sandboxNumber, sandboxString } from "../string-coercion.js";
5
+ import { getSandboxPropertyDescriptor, installRegexPrototype, materializeFunctionProperties, setSandboxPrototype } from "../object-model.js";
6
+ import { accessorAdapter, readPropertyDescriptor } from "../accessors.js";
7
+ import { callRegexMethod, getRegexMember, regexFlagProperties, regexSearch } from "../methods/regex.js";
8
+ import { regexMatch, regexReplace } from "../methods/string.js";
9
+ import { invokeBuiltinClosure } from "../builtin-call.js";
10
+ import { restoreSandboxRegExpIterator } from "../regexp-iterator.js";
11
+ import { normalizeLastIndex } from "../regex/engine.js";
12
+ import { retainValues } from "../resources.js";
13
+ import { setSandboxProperty } from "../interpreter.js";
14
+ import { regexSplit } from "../methods/regex-split.js";
5
15
  export function createRegexGlobals(options) {
6
16
  const invoke = (construct) => async (args, context) => {
7
17
  const selected = options.compileOwner ?? context?.compilation?.owner;
@@ -12,18 +22,51 @@ export function createRegexGlobals(options) {
12
22
  const compilation = context?.compilation?.owner === operation.owner
13
23
  ? context.compilation : new CompileScope(operation.owner);
14
24
  let source = "";
25
+ let retainedSource;
26
+ let retainedFlags;
27
+ let retainedPrototype;
15
28
  const retained = {};
16
- options.budget.setRetainedValues(retained, () => [...args, source]);
29
+ options.budget.setRetainedValues(retained, () => [...args, source, retainedSource, retainedFlags, retainedPrototype]);
17
30
  try {
18
31
  const pattern = args[0];
19
32
  const flags = args[1];
20
- if (!construct && isSandboxRegex(pattern) && flags === undefined)
33
+ const read = (key) => {
34
+ const descriptor = getSandboxPropertyDescriptor(pattern, key, options.budget);
35
+ if (context?.getProperty !== undefined)
36
+ return context.getProperty(pattern, key);
37
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, pattern, context);
38
+ };
39
+ let regexLike = false;
40
+ if (pattern !== null && typeof pattern === "object") {
41
+ const match = await read(Symbol.match);
42
+ regexLike = match === undefined ? isSandboxRegex(pattern) : Boolean(match);
43
+ }
44
+ if (!construct && regexLike && flags === undefined && await read("constructor") === constructor)
21
45
  return pattern;
22
- const sourceValue = isSandboxRegex(pattern) ? pattern.source : pattern;
23
- const flagsValue = isSandboxRegex(pattern) && flags === undefined ? pattern.flags : flags;
46
+ let sourceValue = pattern;
47
+ let flagsValue = flags;
48
+ if (isSandboxRegex(pattern)) {
49
+ sourceValue = pattern.source;
50
+ if (flags === undefined)
51
+ flagsValue = pattern.flags;
52
+ }
53
+ else if (regexLike) {
54
+ sourceValue = retainedSource = await read("source");
55
+ if (flags === undefined)
56
+ flagsValue = retainedFlags = await read("flags");
57
+ }
58
+ if (construct && context?.newTarget !== undefined && context.newTarget !== constructor) {
59
+ retainedPrototype = context.getProperty !== undefined
60
+ ? await context.getProperty(context.newTarget, "prototype")
61
+ : await readPropertyDescriptor(getSandboxPropertyDescriptor(context.newTarget, "prototype", options.budget) ?? { value: undefined }, context.newTarget, context);
62
+ }
24
63
  source = sourceValue === undefined ? "" : await sandboxString(sourceValue, options.budget, context);
64
+ retainedSource = undefined;
25
65
  const flagText = flagsValue === undefined ? "" : await sandboxString(flagsValue, options.budget, context);
66
+ retainedFlags = undefined;
26
67
  const regex = createSandboxRegex(source, flagText, 0, compilation);
68
+ if (typeof retainedPrototype === "object" && retainedPrototype !== null)
69
+ setSandboxPrototype(regex, retainedPrototype, options.budget);
27
70
  if (compilation !== context?.compilation) {
28
71
  reconcileCompiledValues(options.budget, [regex], compilation);
29
72
  }
@@ -36,7 +79,151 @@ export function createRegexGlobals(options) {
36
79
  operation.release();
37
80
  }
38
81
  };
39
- return {
40
- RegExp: createSandboxClosure({ sandbox: true, name: "RegExp", call: invoke(false), construct: invoke(true) })
41
- };
82
+ const constructor = createSandboxClosure({ guest: true, sandbox: true, name: "RegExp", length: 2, call: invoke(false), construct: invoke(true) });
83
+ Object.defineProperty(materializeFunctionProperties(constructor), "escape", {
84
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "escape", length: 1,
85
+ call: (args) => {
86
+ const input = args[0];
87
+ if (typeof input !== "string")
88
+ throw new TypeError("RegExp.escape requires a string.");
89
+ const allocation = {};
90
+ let escaped = "";
91
+ try {
92
+ for (const character of input) {
93
+ options.budget.visitNode();
94
+ const point = character.codePointAt(0);
95
+ let part = character;
96
+ if (escaped.length === 0 && ((point >= 48 && point <= 57) ||
97
+ (point >= 65 && point <= 90) || (point >= 97 && point <= 122))) {
98
+ part = `\\x${point.toString(16)}`;
99
+ }
100
+ else if ("^$\\.*+?()[]{}|/".includes(character)) {
101
+ part = "\\" + character;
102
+ }
103
+ else {
104
+ const control = "\f\n\r\t\v".indexOf(character);
105
+ if (control !== -1)
106
+ part = "\\" + "fnrtv"[control];
107
+ else if (",-=<>#&!%:;@~'`\"".includes(character) || character.trim() === "" ||
108
+ (point >= 0xd800 && point <= 0xdfff)) {
109
+ part = point <= 0xff ? `\\x${point.toString(16).padStart(2, "0")}`
110
+ : `\\u${point.toString(16).padStart(4, "0")}`;
111
+ }
112
+ }
113
+ options.budget.setRetainedDataUsage(allocation, escaped.length + part.length);
114
+ escaped = options.budget.allocateString(escaped + part);
115
+ }
116
+ return escaped;
117
+ }
118
+ finally {
119
+ options.budget.setRetainedDataUsage(allocation, 0);
120
+ }
121
+ }
122
+ }), writable: true, configurable: true
123
+ });
124
+ const prototype = Object.create(null);
125
+ Object.defineProperty(materializeFunctionProperties(constructor), Symbol.species, {
126
+ get: accessorAdapter(createSandboxClosure({ sandbox: true, name: "get [Symbol.species]", length: 0,
127
+ call: (_args, context) => context?.thisValue }), "get"), configurable: true
128
+ });
129
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
130
+ Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
131
+ Object.defineProperty(prototype, Symbol.search, {
132
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.search]", length: 1,
133
+ call: (args, context) => regexSearch(context?.thisValue, args[0], options.budget, context) }),
134
+ writable: true, configurable: true
135
+ });
136
+ Object.defineProperty(prototype, Symbol.match, {
137
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.match]", length: 1,
138
+ call: (args, context) => regexMatch(args[0], context?.thisValue, options.budget, context) }),
139
+ writable: true, configurable: true
140
+ });
141
+ Object.defineProperty(prototype, Symbol.replace, {
142
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.replace]", length: 2,
143
+ call: (args, context) => regexReplace(args[0], context?.thisValue, args[1], options.budget, (closure, values) => invokeBuiltinClosure(closure, values, options.budget, context, undefined), context) }),
144
+ writable: true, configurable: true
145
+ });
146
+ Object.defineProperty(prototype, Symbol.matchAll, {
147
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.matchAll]", length: 1,
148
+ call: async (args, context) => {
149
+ const target = context?.thisValue;
150
+ if (target === null || typeof target !== "object")
151
+ throw new TypeError("RegExp matchAll requires an object receiver.");
152
+ let input = args[0];
153
+ let string;
154
+ let species;
155
+ let field;
156
+ let flags;
157
+ let matcher;
158
+ const release = retainValues(options.budget, () => [target, input, string, species, field, flags, matcher]);
159
+ const read = (value, key) => {
160
+ if (context?.getProperty !== undefined)
161
+ return context.getProperty(value, key);
162
+ const descriptor = getSandboxPropertyDescriptor(value, key, options.budget);
163
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, value, context);
164
+ };
165
+ try {
166
+ string = await sandboxString(input, options.budget, context);
167
+ input = undefined;
168
+ field = await read(target, "constructor");
169
+ species = constructor;
170
+ if (field !== undefined) {
171
+ if (field === null || typeof field !== "object")
172
+ throw new TypeError("Invalid RegExp species constructor.");
173
+ species = await read(field, Symbol.species);
174
+ if (species === null || species === undefined)
175
+ species = constructor;
176
+ }
177
+ field = undefined;
178
+ if (!isSandboxClosure(species) || species.construct === undefined)
179
+ throw new TypeError("RegExp species must be a constructor.");
180
+ field = await read(target, "flags");
181
+ flags = await sandboxString(field, options.budget, context);
182
+ field = undefined;
183
+ matcher = await invokeBuiltinClosure(species, [target, flags], options.budget, context, undefined, true);
184
+ field = await read(target, "lastIndex");
185
+ const cursor = normalizeLastIndex(await sandboxNumber(field, options.budget, context));
186
+ field = undefined;
187
+ await setSandboxProperty(matcher, "lastIndex", cursor, options.budget, true, context);
188
+ return restoreSandboxRegExpIterator({ matcher, input: string, exhausted: false,
189
+ global: flags.includes("g"), unicode: flags.includes("u") || flags.includes("v") });
190
+ }
191
+ finally {
192
+ release();
193
+ }
194
+ } }),
195
+ writable: true, configurable: true
196
+ });
197
+ Object.defineProperty(prototype, Symbol.split, {
198
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.split]", length: 2,
199
+ call: (args, context) => regexSplit(context?.thisValue, args[0], args[1], constructor, options.budget, context) }),
200
+ writable: true, configurable: true
201
+ });
202
+ for (const name of ["exec", "test", "toString"]) {
203
+ Object.defineProperty(prototype, name, {
204
+ value: createSandboxClosure({ sandbox: true, name, length: name === "toString" ? 0 : 1,
205
+ call: (args, context) => callRegexMethod(context?.thisValue, name, args, options.budget, context) }),
206
+ writable: true, configurable: true
207
+ });
208
+ }
209
+ for (const name of ["source", "flags", ...Object.keys(regexFlagProperties)]) {
210
+ const getter = createSandboxClosure({ sandbox: true, name: `get ${name}`, length: 0,
211
+ call: (_args, context) => {
212
+ const receiver = context?.thisValue;
213
+ if (name === "flags") {
214
+ if (receiver === null || typeof receiver !== "object")
215
+ throw new TypeError("RegExp flags requires an object receiver.");
216
+ return getRegexMember(receiver, name, options.budget, context);
217
+ }
218
+ if (receiver === prototype)
219
+ return name === "source" ? "(?:)" : undefined;
220
+ if (!isSandboxRegex(receiver))
221
+ throw new TypeError("RegExp accessor requires a regex receiver.");
222
+ return getRegexMember(receiver, name, options.budget, context);
223
+ }
224
+ });
225
+ Object.defineProperty(prototype, name, { get: accessorAdapter(getter, "get"), configurable: true });
226
+ }
227
+ installRegexPrototype(options.budget, prototype, constructor);
228
+ return { RegExp: constructor };
42
229
  }
@@ -35,6 +35,8 @@ import { hasOwnSandboxProperty } from "./globals/object.js";
35
35
  import { isSandboxMapConstructor, isSandboxSetConstructor } from "./globals/collections.js";
36
36
  import { collectionIteratorState, isSandboxCollectionIterator } from "./collection-iterator.js";
37
37
  import { getCollectionIteratorMember } from "./methods/collection-iterator.js";
38
+ import { isSandboxRegExpIterator, regexpIteratorState } from "./regexp-iterator.js";
39
+ import { getRegExpIteratorMember } from "./methods/regexp-iterator.js";
38
40
  import { getFloat32Member, isFloat32ArrayConstructor, setFloat32Member } from "./globals/float32array.js";
39
41
  import { isFloat32Array } from "./float32.js";
40
42
  import { dateString, dateTime, isSandboxDate } from "./date.js";
@@ -857,6 +859,8 @@ function isRestorableBindingValue(value, seen = new WeakSet()) {
857
859
  if (seen.has(value))
858
860
  return true;
859
861
  seen.add(value);
862
+ if (isSandboxRegExpIterator(value))
863
+ return isRestorableBindingValue(regexpIteratorState(value).matcher, seen);
860
864
  if (isSandboxCollectionIterator(value)) {
861
865
  return isRestorableBindingValue(collectionIteratorState(value).collection, seen);
862
866
  }
@@ -1116,7 +1120,7 @@ async function evaluateForOfStatement(node, context) {
1116
1120
  const restored = context.scope.consumeRestoredBinding(restoredIteration.values[0]);
1117
1121
  if (restored.found && Array.isArray(restored.value)) {
1118
1122
  restoredEntry = { done: false, value: restored.value[1] };
1119
- if (Array.isArray(restored.value[0]) || isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0]) || isSandboxCollectionIterator(restored.value[0])) {
1123
+ if (Array.isArray(restored.value[0]) || isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0]) || isSandboxCollectionIterator(restored.value[0]) || isSandboxRegExpIterator(restored.value[0])) {
1120
1124
  return evaluateForOfIterator(node, restored.value[0], context, restoredEntry);
1121
1125
  }
1122
1126
  }
@@ -1510,7 +1514,7 @@ function createLoopIterationContext(context, scope) {
1510
1514
  snapshot.loopIterations = {};
1511
1515
  for (const [nodeId, iteration] of context.activeLoopIterations) {
1512
1516
  if (typeof iteration !== "number" &&
1513
- (Array.isArray(iteration.values[0]) || isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]) || isSandboxCollectionIterator(iteration.values[0]))) {
1517
+ (Array.isArray(iteration.values[0]) || isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]) || isSandboxCollectionIterator(iteration.values[0]) || isSandboxRegExpIterator(iteration.values[0]))) {
1514
1518
  const bindingName = `#for-of:${nodeId}`;
1515
1519
  snapshot.bindings[bindingName] = iteration.values;
1516
1520
  snapshot.loopIterations[nodeId] = { index: iteration.index, values: [bindingName] };
@@ -1852,6 +1856,8 @@ function getPropertyValue(target, property, context, receiver = target) {
1852
1856
  const descriptor = getSandboxPropertyDescriptor(target, property, context.budget);
1853
1857
  if (descriptor !== undefined)
1854
1858
  return readPropertyDescriptor(descriptor, receiver, createCoercionContext(context), true);
1859
+ if (isSandboxRegExpIterator(target))
1860
+ return getRegExpIteratorMember(property, context.budget);
1855
1861
  if (typeof target === "symbol") {
1856
1862
  const prototype = getBoxedPrototype(target, context.budget);
1857
1863
  return prototype === undefined ? undefined : getPropertyValue(prototype, property, context, receiver);
@@ -1892,7 +1898,8 @@ function getPropertyValue(target, property, context, receiver = target) {
1892
1898
  if (isSandboxPromise(target))
1893
1899
  return getPromiseMember(property, context.budget);
1894
1900
  if (isSandboxRegex(target))
1895
- return getRegexMember(target, property, context.budget);
1901
+ return hasExplicitSandboxPrototype(target) || getSandboxPrototype(target, context.budget) !== null
1902
+ ? undefined : getRegexMember(target, property, context.budget, createCoercionContext(context));
1896
1903
  if (!isIndexableSandboxValue(target)) {
1897
1904
  throw new TypeError("Attempted to read a property from a non-object value.");
1898
1905
  }
@@ -2381,9 +2388,9 @@ function hasSandboxProperty(value, key, context) {
2381
2388
  return typeof key === "symbol" ? false : hasHostObjectMember(current, String(key));
2382
2389
  if (hasOwnSandboxProperty(current, key, false))
2383
2390
  return true;
2384
- if (!((isGuestClosure(current) || Array.isArray(current)) && hasExplicitSandboxPrototype(current)) &&
2391
+ if (!isSandboxRegex(current) && !((isGuestClosure(current) || Array.isArray(current)) && hasExplicitSandboxPrototype(current)) &&
2385
2392
  (Array.isArray(current) || !isPlainSandboxObject(current) ||
2386
- isSandboxDate(current) || isFloat32Array(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current))) {
2393
+ isSandboxDate(current) || isFloat32Array(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current) || isSandboxRegExpIterator(current))) {
2387
2394
  return getPropertyValue(current, key, context) !== undefined;
2388
2395
  }
2389
2396
  current = getSandboxPrototype(current, context.budget);
@@ -2603,7 +2610,7 @@ function getMemberValue(target, property, context) {
2603
2610
  return getClosureMemberValue(current, property, context);
2604
2611
  if (Array.isArray(current))
2605
2612
  return getArrayMemberValue(current, property, context);
2606
- if (!isPlainSandboxObject(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current) || isFloat32Array(current)) {
2613
+ if (!isPlainSandboxObject(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current) || isSandboxRegExpIterator(current) || isFloat32Array(current)) {
2607
2614
  return getPropertyValue(current, property, context);
2608
2615
  }
2609
2616
  if (Object.hasOwn(current, String(property)))
@@ -2646,7 +2653,7 @@ export function setSandboxProperty(target, property, value, budget, checkInherit
2646
2653
  return;
2647
2654
  }
2648
2655
  if (isSandboxRegex(target)) {
2649
- setRegexMember(target, property, value);
2656
+ setRegexMember(target, property, value, budget);
2650
2657
  return;
2651
2658
  }
2652
2659
  if (!isIndexableSandboxValue(target)) {
@@ -4,6 +4,8 @@ import { isFloat32Array } from "./float32.js";
4
4
  import { boxedValue, isSandboxBox } from "./boxed.js";
5
5
  import { getHostObjectIterator, isGuestHostObject } from "./host-capabilities.js";
6
6
  import { isSandboxCollectionIterator, nextCollectionIterator } from "./collection-iterator.js";
7
+ import { isSandboxRegExpIterator } from "./regexp-iterator.js";
8
+ import { nextObservableRegExpIterator, nextRegExpIterator } from "./methods/regexp-iterator.js";
7
9
  import { Budget, isFatalSandboxError } from "./budget.js";
8
10
  import { sandboxString } from "./string-coercion.js";
9
11
  import { awaitSandboxValue, awaitWithSignal } from "./cancel.js";
@@ -17,7 +19,8 @@ export async function acquireSandboxIterator(value, budget, context, asyncProtoc
17
19
  return asyncProtocol
18
20
  ? getSandboxAsyncIterator(value, budget, context, signal)
19
21
  : getSandboxIterator(value, budget, context);
20
- if (getSandboxPropertyDescriptor(value, key, budget) === undefined) {
22
+ if (getSandboxPropertyDescriptor(value, key, budget) === undefined &&
23
+ !(isSandboxRegExpIterator(value) && !asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== undefined)) {
21
24
  if (!asyncProtocol)
22
25
  return getSandboxIterator(value, budget, context);
23
26
  if (isSandboxGenerator(value) && value.async)
@@ -222,6 +225,11 @@ export function getSandboxIterator(value, budget, context) {
222
225
  }
223
226
  if (isSandboxCollectionIterator(value))
224
227
  return { next: () => nextCollectionIterator(value, budget), snapshotIndex: () => 0 };
228
+ if (isSandboxRegExpIterator(value)) {
229
+ if (context !== undefined && budget !== undefined)
230
+ return { asynchronous: true, next: () => nextObservableRegExpIterator(value, budget, context), snapshotIndex: () => 0 };
231
+ return { next: () => nextRegExpIterator(value, budget), snapshotIndex: () => 0 };
232
+ }
225
233
  if (isGuestHostObject(value))
226
234
  return getHostObjectIterator(value);
227
235
  if (isFloat32Array(value)) {
@@ -0,0 +1,3 @@
1
+ import { type Budget } from "../budget.js";
2
+ import { type SandboxCallContext, type SandboxClosure, type SandboxValue } from "../values.js";
3
+ export declare function regexSplit(target: SandboxValue, input: SandboxValue, limit: SandboxValue, constructor: SandboxClosure, budget: Budget, context?: SandboxCallContext): Promise<SandboxValue[]>;
@@ -0,0 +1,102 @@
1
+ import { isSandboxClosure } from "../values.js";
2
+ import { getSandboxPropertyDescriptor } from "../object-model.js";
3
+ import { readPropertyDescriptor } from "../accessors.js";
4
+ import { invokeBuiltinClosure } from "../builtin-call.js";
5
+ import { setSandboxProperty } from "../interpreter.js";
6
+ import { sandboxNumber, sandboxString } from "../string-coercion.js";
7
+ import { normalizeLastIndex } from "../regex/engine.js";
8
+ import { retainValues } from "../resources.js";
9
+ import { regexExec } from "./regex.js";
10
+ export async function regexSplit(target, input, limit, constructor, budget, context) {
11
+ if (target === null || typeof target !== "object")
12
+ throw new TypeError("RegExp split requires an object receiver.");
13
+ let string;
14
+ let species;
15
+ let field;
16
+ let flags;
17
+ let matcher;
18
+ let match;
19
+ const result = [];
20
+ const release = retainValues(budget, () => [target, input, limit, string, species, field, flags, matcher, match, result]);
21
+ const read = (value, key) => {
22
+ if (context?.getProperty !== undefined)
23
+ return context.getProperty(value, key);
24
+ const descriptor = getSandboxPropertyDescriptor(value, key, budget);
25
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, value, context);
26
+ };
27
+ const append = (value) => {
28
+ budget.allocateArrayLength(result.length + 1);
29
+ result.push(value);
30
+ };
31
+ try {
32
+ string = await sandboxString(input, budget, context);
33
+ input = undefined;
34
+ field = await read(target, "constructor");
35
+ species = constructor;
36
+ if (field !== undefined) {
37
+ if (field === null || typeof field !== "object")
38
+ throw new TypeError("Invalid RegExp species constructor.");
39
+ species = await read(field, Symbol.species);
40
+ if (species === null || species === undefined)
41
+ species = constructor;
42
+ }
43
+ field = undefined;
44
+ if (!isSandboxClosure(species) || species.construct === undefined)
45
+ throw new TypeError("RegExp species must be a constructor.");
46
+ field = await read(target, "flags");
47
+ flags = await sandboxString(field, budget, context);
48
+ field = undefined;
49
+ const unicode = flags.includes("u") || flags.includes("v");
50
+ if (!flags.includes("y"))
51
+ flags = budget.allocateString(flags + "y");
52
+ matcher = await invokeBuiltinClosure(species, [target, flags], budget, context, undefined, true);
53
+ const maximum = limit === undefined ? 2 ** 32 - 1 : (await sandboxNumber(limit, budget, context)) >>> 0;
54
+ limit = undefined;
55
+ if (maximum === 0)
56
+ return result;
57
+ if (string.length === 0) {
58
+ match = await regexExec(matcher, string, budget, context);
59
+ if (match === null)
60
+ append(string);
61
+ return result;
62
+ }
63
+ let end = 0;
64
+ let index = 0;
65
+ while (index < string.length) {
66
+ budget.visitNode();
67
+ match = undefined;
68
+ await setSandboxProperty(matcher, "lastIndex", index, budget, true, context);
69
+ match = await regexExec(matcher, string, budget, context);
70
+ if (match !== null) {
71
+ field = await read(matcher, "lastIndex");
72
+ const next = Math.min(normalizeLastIndex(await sandboxNumber(field, budget, context)), string.length);
73
+ field = undefined;
74
+ if (next !== end) {
75
+ append(budget.allocateString(string.slice(end, index)));
76
+ if (result.length === maximum)
77
+ return result;
78
+ end = next;
79
+ field = await read(match, "length");
80
+ const length = normalizeLastIndex(await sandboxNumber(field, budget, context));
81
+ field = undefined;
82
+ for (let capture = 1; capture < length; capture++) {
83
+ budget.visitNode();
84
+ field = await read(match, String(capture));
85
+ append(field);
86
+ field = undefined;
87
+ if (result.length === maximum)
88
+ return result;
89
+ }
90
+ index = end;
91
+ continue;
92
+ }
93
+ }
94
+ index += unicode && (string.codePointAt(index) ?? 0) > 0xffff ? 2 : 1;
95
+ }
96
+ append(budget.allocateString(string.slice(end)));
97
+ return result;
98
+ }
99
+ finally {
100
+ release();
101
+ }
102
+ }
@@ -1,10 +1,14 @@
1
1
  import { type SandboxCallContext, type SandboxRegex, type SandboxValue } from "../values.js";
2
2
  import { type RegexMatch } from "../regex/engine.js";
3
3
  import type { Budget } from "../budget.js";
4
- export type RegexMethodName = "exec" | "test";
4
+ export declare function regexSearch(target: SandboxValue, input: SandboxValue, budget: Budget, context?: SandboxCallContext): Promise<SandboxValue>;
5
+ export type RegexMethodName = "exec" | "test" | "toString";
6
+ export declare const regexFlagProperties: Readonly<Record<string, string>>;
5
7
  export declare function isRegexMethodName(property: string | number): property is RegexMethodName;
6
- export declare function getRegexMember(target: SandboxRegex, property: string | number, budget: Budget): SandboxValue | undefined;
7
- export declare function setRegexMember(target: SandboxRegex, property: PropertyKey, value: SandboxValue): void;
8
+ export declare function getRegexMember(target: SandboxRegex, property: string | number, budget: Budget, context?: SandboxCallContext): SandboxValue | Promise<SandboxValue>;
9
+ export declare function setRegexMember(target: SandboxRegex, property: PropertyKey, value: SandboxValue, budget?: Budget): void;
8
10
  export declare function callRegexMethod(target: SandboxValue, methodName: RegexMethodName, args: readonly SandboxValue[], budget: Budget, context?: SandboxCallContext): Promise<SandboxValue>;
11
+ export declare function regexExec(target: SandboxValue, input: string, budget: Budget, context?: SandboxCallContext): Promise<SandboxValue>;
12
+ export declare function regexToString(target: SandboxValue, budget: Budget, context?: SandboxCallContext): Promise<string>;
9
13
  export declare function executeRegex(target: SandboxRegex, input: string, lastIndex: number): RegexMatch | null;
10
- export declare function toMatchArray(match: RegexMatch | null, input: string): SandboxValue;
14
+ export declare function toMatchArray(match: RegexMatch | null, input: string, budget?: Budget): SandboxValue;