poe-code 14.0.73 → 14.0.74

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 (40) hide show
  1. package/dist/index.js +1855 -1153
  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-2J6W3YGB.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-XKCTB5FR.js → chunk-YEZOXLY4.js} +3203 -2543
  7. package/packages/safe-js/dist/chunks/chunk-YEZOXLY4.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 +88 -7
  16. package/packages/safe-js/dist/interp/interpreter.js +14 -7
  17. package/packages/safe-js/dist/interp/iteration.js +6 -1
  18. package/packages/safe-js/dist/interp/methods/regex.d.ts +7 -3
  19. package/packages/safe-js/dist/interp/methods/regex.js +127 -21
  20. package/packages/safe-js/dist/interp/methods/regexp-iterator.d.ts +8 -0
  21. package/packages/safe-js/dist/interp/methods/regexp-iterator.js +49 -0
  22. package/packages/safe-js/dist/interp/methods/string.d.ts +2 -0
  23. package/packages/safe-js/dist/interp/methods/string.js +412 -118
  24. package/packages/safe-js/dist/interp/object-model.d.ts +2 -0
  25. package/packages/safe-js/dist/interp/object-model.js +39 -9
  26. package/packages/safe-js/dist/interp/regexp-iterator.d.ts +14 -0
  27. package/packages/safe-js/dist/interp/regexp-iterator.js +18 -0
  28. package/packages/safe-js/dist/interp/string-coercion.js +11 -7
  29. package/packages/safe-js/dist/interp/values.d.ts +2 -1
  30. package/packages/safe-js/dist/interp/values.js +35 -0
  31. package/packages/safe-js/dist/snapshot/dump-format.js +26 -2
  32. package/packages/safe-js/dist/snapshot/replay-data.d.ts +9 -1
  33. package/packages/safe-js/dist/snapshot/replay-data.js +31 -0
  34. package/packages/safe-js/dist/snapshot/replay-inputs.js +17 -0
  35. package/packages/safe-js/dist/snapshot/restore.js +17 -1
  36. package/packages/safe-js/dist/snapshot/serialize.d.ts +10 -2
  37. package/packages/safe-js/dist/snapshot/serialize.js +26 -10
  38. package/packages/safe-js/dist/snapshot/validation.js +14 -2
  39. package/packages/safe-js/dist/chunks/chunk-XKCTB5FR.js.map +0 -7
  40. /package/packages/safe-js/dist/chunks/{chunk-I2Z37MKZ.js.map → chunk-2J6W3YGB.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-2J6W3YGB.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-YEZOXLY4.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-YEZOXLY4.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-2J6W3YGB.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-YEZOXLY4.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))
@@ -2,6 +2,11 @@ import { createSandboxClosure, createSandboxRegex, isSandboxRegex, reconcileComp
2
2
  import { CompileScope } from "../regex/compile-guard.js";
3
3
  import { SandboxError } from "../budget.js";
4
4
  import { 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";
5
10
  export function createRegexGlobals(options) {
6
11
  const invoke = (construct) => async (args, context) => {
7
12
  const selected = options.compileOwner ?? context?.compilation?.owner;
@@ -12,18 +17,51 @@ export function createRegexGlobals(options) {
12
17
  const compilation = context?.compilation?.owner === operation.owner
13
18
  ? context.compilation : new CompileScope(operation.owner);
14
19
  let source = "";
20
+ let retainedSource;
21
+ let retainedFlags;
22
+ let retainedPrototype;
15
23
  const retained = {};
16
- options.budget.setRetainedValues(retained, () => [...args, source]);
24
+ options.budget.setRetainedValues(retained, () => [...args, source, retainedSource, retainedFlags, retainedPrototype]);
17
25
  try {
18
26
  const pattern = args[0];
19
27
  const flags = args[1];
20
- if (!construct && isSandboxRegex(pattern) && flags === undefined)
28
+ const read = (key) => {
29
+ const descriptor = getSandboxPropertyDescriptor(pattern, key, options.budget);
30
+ if (context?.getProperty !== undefined)
31
+ return context.getProperty(pattern, key);
32
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, pattern, context);
33
+ };
34
+ let regexLike = false;
35
+ if (pattern !== null && typeof pattern === "object") {
36
+ const match = await read(Symbol.match);
37
+ regexLike = match === undefined ? isSandboxRegex(pattern) : Boolean(match);
38
+ }
39
+ if (!construct && regexLike && flags === undefined && await read("constructor") === constructor)
21
40
  return pattern;
22
- const sourceValue = isSandboxRegex(pattern) ? pattern.source : pattern;
23
- const flagsValue = isSandboxRegex(pattern) && flags === undefined ? pattern.flags : flags;
41
+ let sourceValue = pattern;
42
+ let flagsValue = flags;
43
+ if (isSandboxRegex(pattern)) {
44
+ sourceValue = pattern.source;
45
+ if (flags === undefined)
46
+ flagsValue = pattern.flags;
47
+ }
48
+ else if (regexLike) {
49
+ sourceValue = retainedSource = await read("source");
50
+ if (flags === undefined)
51
+ flagsValue = retainedFlags = await read("flags");
52
+ }
53
+ if (construct && context?.newTarget !== undefined && context.newTarget !== constructor) {
54
+ retainedPrototype = context.getProperty !== undefined
55
+ ? await context.getProperty(context.newTarget, "prototype")
56
+ : await readPropertyDescriptor(getSandboxPropertyDescriptor(context.newTarget, "prototype", options.budget) ?? { value: undefined }, context.newTarget, context);
57
+ }
24
58
  source = sourceValue === undefined ? "" : await sandboxString(sourceValue, options.budget, context);
59
+ retainedSource = undefined;
25
60
  const flagText = flagsValue === undefined ? "" : await sandboxString(flagsValue, options.budget, context);
61
+ retainedFlags = undefined;
26
62
  const regex = createSandboxRegex(source, flagText, 0, compilation);
63
+ if (typeof retainedPrototype === "object" && retainedPrototype !== null)
64
+ setSandboxPrototype(regex, retainedPrototype, options.budget);
27
65
  if (compilation !== context?.compilation) {
28
66
  reconcileCompiledValues(options.budget, [regex], compilation);
29
67
  }
@@ -36,7 +74,50 @@ export function createRegexGlobals(options) {
36
74
  operation.release();
37
75
  }
38
76
  };
39
- return {
40
- RegExp: createSandboxClosure({ sandbox: true, name: "RegExp", call: invoke(false), construct: invoke(true) })
41
- };
77
+ const constructor = createSandboxClosure({ guest: true, sandbox: true, name: "RegExp", length: 2, call: invoke(false), construct: invoke(true) });
78
+ const prototype = Object.create(null);
79
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
80
+ Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
81
+ Object.defineProperty(prototype, Symbol.search, {
82
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.search]", length: 1,
83
+ call: (args, context) => regexSearch(context?.thisValue, args[0], options.budget, context) }),
84
+ writable: true, configurable: true
85
+ });
86
+ Object.defineProperty(prototype, Symbol.match, {
87
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.match]", length: 1,
88
+ call: (args, context) => regexMatch(args[0], context?.thisValue, options.budget, context) }),
89
+ writable: true, configurable: true
90
+ });
91
+ Object.defineProperty(prototype, Symbol.replace, {
92
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "[Symbol.replace]", length: 2,
93
+ call: (args, context) => regexReplace(args[0], context?.thisValue, args[1], options.budget, (closure, values) => invokeBuiltinClosure(closure, values, options.budget, context, undefined), context) }),
94
+ writable: true, configurable: true
95
+ });
96
+ for (const name of ["exec", "test", "toString"]) {
97
+ Object.defineProperty(prototype, name, {
98
+ value: createSandboxClosure({ sandbox: true, name, length: name === "toString" ? 0 : 1,
99
+ call: (args, context) => callRegexMethod(context?.thisValue, name, args, options.budget, context) }),
100
+ writable: true, configurable: true
101
+ });
102
+ }
103
+ for (const name of ["source", "flags", ...Object.keys(regexFlagProperties)]) {
104
+ const getter = createSandboxClosure({ sandbox: true, name: `get ${name}`, length: 0,
105
+ call: (_args, context) => {
106
+ const receiver = context?.thisValue;
107
+ if (name === "flags") {
108
+ if (receiver === null || typeof receiver !== "object")
109
+ throw new TypeError("RegExp flags requires an object receiver.");
110
+ return getRegexMember(receiver, name, options.budget, context);
111
+ }
112
+ if (receiver === prototype)
113
+ return name === "source" ? "(?:)" : undefined;
114
+ if (!isSandboxRegex(receiver))
115
+ throw new TypeError("RegExp accessor requires a regex receiver.");
116
+ return getRegexMember(receiver, name, options.budget, context);
117
+ }
118
+ });
119
+ Object.defineProperty(prototype, name, { get: accessorAdapter(getter, "get"), configurable: true });
120
+ }
121
+ installRegexPrototype(options.budget, prototype, constructor);
122
+ return { RegExp: constructor };
42
123
  }
@@ -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 { 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,8 @@ 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
+ return { next: () => nextRegExpIterator(value, budget), snapshotIndex: () => 0 };
225
230
  if (isGuestHostObject(value))
226
231
  return getHostObjectIterator(value);
227
232
  if (isFloat32Array(value)) {
@@ -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
14
  export declare function toMatchArray(match: RegexMatch | null, input: string): SandboxValue;
@@ -1,22 +1,84 @@
1
1
  import { createSandboxClosure, getSandboxRegexPattern, getRegexProperties, isSandboxClosure, isSandboxRegex } from "../values.js";
2
2
  import { matchRegex } from "../regex/engine.js";
3
3
  import { invokeBuiltinClosure } from "../builtin-call.js";
4
- import { getSandboxDataProperty } from "../object-model.js";
4
+ import { getSandboxPropertyDescriptor, getSandboxPrototype, hasExplicitSandboxPrototype } from "../object-model.js";
5
+ import { readPropertyDescriptor } from "../accessors.js";
6
+ import { retainValues } from "../resources.js";
5
7
  import { sandboxNumber, sandboxString } from "../string-coercion.js";
6
- const regexMethodNames = new Set(["exec", "test"]);
7
- const regexFlagProperties = {
8
+ import { setSandboxProperty } from "../interpreter.js";
9
+ export async function regexSearch(target, input, budget, context) {
10
+ if (target === null || typeof target !== "object")
11
+ throw new TypeError("RegExp search requires an object receiver.");
12
+ let string;
13
+ let previous;
14
+ let result;
15
+ const release = retainValues(budget, () => [target, input, string, previous, result]);
16
+ const read = (value, key) => {
17
+ if (context?.getProperty !== undefined)
18
+ return context.getProperty(value, key);
19
+ const descriptor = getSandboxPropertyDescriptor(value, key, budget);
20
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, value, context);
21
+ };
22
+ try {
23
+ string = await sandboxString(input, budget, context);
24
+ input = undefined;
25
+ previous = await read(target, "lastIndex");
26
+ if (!Object.is(previous, 0))
27
+ await setSandboxProperty(target, "lastIndex", 0, budget, true, context);
28
+ result = await regexExec(target, string, budget, context);
29
+ if (!Object.is(await read(target, "lastIndex"), previous))
30
+ await setSandboxProperty(target, "lastIndex", previous, budget, true, context);
31
+ return result === null ? -1 : await read(result, "index");
32
+ }
33
+ finally {
34
+ release();
35
+ }
36
+ }
37
+ const regexMethodNames = new Set(["exec", "test", "toString"]);
38
+ export const regexFlagProperties = {
8
39
  hasIndices: "d", global: "g", ignoreCase: "i", multiline: "m",
9
40
  dotAll: "s", unicode: "u", unicodeSets: "v", sticky: "y"
10
41
  };
11
42
  export function isRegexMethodName(property) {
12
43
  return typeof property === "string" && regexMethodNames.has(property);
13
44
  }
14
- export function getRegexMember(target, property, budget) {
45
+ export function getRegexMember(target, property, budget, context) {
15
46
  if (property === "source")
16
47
  return escapeRegexSource(target.source, budget);
17
48
  if (property === "flags") {
18
- const flags = [..."gims"].filter((flag) => target.flags.includes(flag)).join("");
19
- return budget === undefined ? flags : budget.allocateString(flags);
49
+ let flags = "";
50
+ const entries = Object.entries(regexFlagProperties);
51
+ const release = retainValues(budget, () => [target, flags]);
52
+ const read = (index) => {
53
+ if (index === entries.length)
54
+ return budget.allocateString(flags);
55
+ budget.visitNode();
56
+ const [key, flag] = entries[index];
57
+ const descriptor = context?.getProperty === undefined
58
+ ? getSandboxPropertyDescriptor(target, key, budget) : undefined;
59
+ const value = context?.getProperty !== undefined
60
+ ? context.getProperty(target, key)
61
+ : descriptor !== undefined
62
+ ? readPropertyDescriptor(descriptor, target, context)
63
+ : target.flags.includes(flag);
64
+ const append = (value) => {
65
+ if (value)
66
+ flags += flag;
67
+ return read(index + 1);
68
+ };
69
+ return value instanceof Promise ? value.then(append) : append(value);
70
+ };
71
+ try {
72
+ const result = read(0);
73
+ if (result instanceof Promise)
74
+ return result.finally(release);
75
+ release();
76
+ return result;
77
+ }
78
+ catch (error) {
79
+ release();
80
+ throw error;
81
+ }
20
82
  }
21
83
  if (property === "lastIndex")
22
84
  return target.lastIndex;
@@ -27,7 +89,7 @@ export function getRegexMember(target, property, budget) {
27
89
  }
28
90
  return createSandboxClosure({
29
91
  sandbox: true,
30
- name: `RegExp#${property}`,
92
+ name: property === "toString" ? "toString" : `RegExp#${property}`,
31
93
  call: (args, context) => callRegexMethod(context?.thisValue, property, args, budget, context)
32
94
  });
33
95
  }
@@ -66,9 +128,9 @@ function escapeRegexSource(source, budget) {
66
128
  text = text === "" ? "(?:)" : text;
67
129
  return budget === undefined ? text : budget.allocateString(text);
68
130
  }
69
- export function setRegexMember(target, property, value) {
131
+ export function setRegexMember(target, property, value, budget) {
70
132
  const properties = getRegexProperties(target);
71
- if (!Object.hasOwn(properties, property) &&
133
+ if (!hasExplicitSandboxPrototype(target) && getSandboxPrototype(target, budget) === null && !Object.hasOwn(properties, property) &&
72
134
  (property === "source" || property === "flags" || Object.hasOwn(regexFlagProperties, property))) {
73
135
  throw new TypeError(`RegExp#${String(property)} is not writable.`);
74
136
  }
@@ -76,6 +138,8 @@ export function setRegexMember(target, property, value) {
76
138
  throw new TypeError(`RegExp#${String(property)} is not writable.`);
77
139
  }
78
140
  export async function callRegexMethod(target, methodName, args, budget, context) {
141
+ if (methodName === "toString")
142
+ return regexToString(target, budget, context);
79
143
  if (target === null || typeof target !== "object" || (methodName === "exec" && !isSandboxRegex(target))) {
80
144
  throw new TypeError(`RegExp#${methodName} requires ${methodName === "exec" ? "a regex" : "an object"} receiver.`);
81
145
  }
@@ -86,29 +150,71 @@ export async function callRegexMethod(target, methodName, args, budget, context)
86
150
  try {
87
151
  const input = await sandboxString(args[0], budget, context);
88
152
  if (methodName === "test") {
89
- const exec = context?.getProperty === undefined
90
- ? getSandboxDataProperty(target, "exec", budget) : await context.getProperty(target, "exec");
91
- if (isSandboxClosure(exec)) {
92
- const result = await invokeBuiltinClosure(exec, [input], budget, context, target);
93
- if (result !== null && typeof result !== "object") {
94
- throw new TypeError("RegExp#test exec must return an object or null.");
95
- }
96
- return result !== null;
97
- }
153
+ budget.setRetainedValues(retained, undefined);
154
+ return await regexExec(target, input, budget, context) !== null;
98
155
  }
99
- if (!isSandboxRegex(target))
100
- throw new TypeError("RegExp execution requires a regex receiver.");
101
156
  if (typeof args[0] !== "string")
102
157
  convertedInput = input;
158
+ if (!isSandboxRegex(target))
159
+ throw new TypeError("RegExp execution requires a regex receiver.");
103
160
  cursor = target.lastIndex;
104
161
  const lastIndex = await sandboxNumber(cursor, budget, context);
105
162
  const match = executeRegex(target, input, lastIndex);
106
- return methodName === "test" ? match !== null : toMatchArray(match, input);
163
+ return toMatchArray(match, input);
107
164
  }
108
165
  finally {
109
166
  budget.setRetainedValues(retained, undefined);
110
167
  }
111
168
  }
169
+ export async function regexExec(target, input, budget, context) {
170
+ let exec;
171
+ const release = retainValues(budget, () => [target, input, exec]);
172
+ try {
173
+ const descriptor = context?.getProperty === undefined
174
+ ? getSandboxPropertyDescriptor(target, "exec", budget) : undefined;
175
+ exec = await (context?.getProperty !== undefined ? context.getProperty(target, "exec")
176
+ : descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, target, context));
177
+ if (isSandboxClosure(exec)) {
178
+ const result = await invokeBuiltinClosure(exec, [input], budget, context, target);
179
+ if (result !== null && typeof result !== "object")
180
+ throw new TypeError("RegExp exec must return an object or null.");
181
+ return result;
182
+ }
183
+ return await callRegexMethod(target, "exec", [input], budget, context);
184
+ }
185
+ finally {
186
+ release();
187
+ }
188
+ }
189
+ export async function regexToString(target, budget, context) {
190
+ if (target === null || typeof target !== "object")
191
+ throw new TypeError("RegExp#toString requires an object receiver.");
192
+ const read = (key) => {
193
+ if (context?.getProperty !== undefined)
194
+ return context.getProperty(target, key);
195
+ const descriptor = getSandboxPropertyDescriptor(target, key, budget);
196
+ if (descriptor !== undefined)
197
+ return readPropertyDescriptor(descriptor, target, context);
198
+ return isSandboxRegex(target) ? getRegexMember(target, key, budget, context) : undefined;
199
+ };
200
+ let sourceValue;
201
+ let flagsValue;
202
+ let source;
203
+ let flags;
204
+ const release = retainValues(budget, () => [target, sourceValue, flagsValue, source, flags]);
205
+ try {
206
+ sourceValue = await read("source");
207
+ source = await sandboxString(sourceValue, budget, context);
208
+ sourceValue = undefined;
209
+ flagsValue = await read("flags");
210
+ flags = await sandboxString(flagsValue, budget, context);
211
+ flagsValue = undefined;
212
+ return budget.allocateString(`/${source}/${flags}`);
213
+ }
214
+ finally {
215
+ release();
216
+ }
217
+ }
112
218
  export function executeRegex(target, input, lastIndex) {
113
219
  const pattern = getSandboxRegexPattern(target);
114
220
  const match = matchRegex(pattern, input, lastIndex);
@@ -0,0 +1,8 @@
1
+ import type { Budget } from "../budget.js";
2
+ import { type SandboxRegExpIterator } from "../regexp-iterator.js";
3
+ import { type SandboxValue } from "../values.js";
4
+ export declare function nextRegExpIterator(iterator: SandboxRegExpIterator, budget?: Budget): {
5
+ value: SandboxValue;
6
+ done: boolean;
7
+ };
8
+ export declare function getRegExpIteratorMember(property: PropertyKey, budget: Budget): SandboxValue;
@@ -0,0 +1,49 @@
1
+ import { isSandboxRegExpIterator, regexpIteratorState } from "../regexp-iterator.js";
2
+ import { createSandboxClosure } from "../values.js";
3
+ import { executeRegex, toMatchArray } from "./regex.js";
4
+ export function nextRegExpIterator(iterator, budget) {
5
+ const state = regexpIteratorState(iterator);
6
+ budget?.visitNode();
7
+ if (state.exhausted)
8
+ return { value: undefined, done: true };
9
+ const matcher = state.matcher;
10
+ const input = state.input;
11
+ const match = executeRegex(matcher, input, Number(matcher.lastIndex));
12
+ if (match === null || !matcher.flags.includes("g")) {
13
+ state.exhausted = true;
14
+ state.matcher = undefined;
15
+ state.input = undefined;
16
+ }
17
+ if (match === null)
18
+ return { value: undefined, done: true };
19
+ if (!state.exhausted && match.text.length === 0) {
20
+ const index = Number(matcher.lastIndex);
21
+ const unicode = matcher.flags.includes("u") || matcher.flags.includes("v");
22
+ const codePoint = input.codePointAt(index);
23
+ matcher.lastIndex = index + (unicode && codePoint !== undefined && codePoint > 0xffff ? 2 : 1);
24
+ }
25
+ budget?.allocateArrayLength(match.captures.length + 1);
26
+ return { value: toMatchArray(match, input), done: false };
27
+ }
28
+ export function getRegExpIteratorMember(property, budget) {
29
+ if (property === Symbol.toStringTag)
30
+ return "RegExp String Iterator";
31
+ if (property === Symbol.iterator)
32
+ return createSandboxClosure({
33
+ sandbox: true,
34
+ name: "[Symbol.iterator]",
35
+ call: (_args, context) => context?.thisValue
36
+ });
37
+ if (property !== "next")
38
+ return undefined;
39
+ return createSandboxClosure({
40
+ sandbox: true,
41
+ name: "next",
42
+ call: (_args, context) => {
43
+ const receiver = context?.thisValue;
44
+ if (!isSandboxRegExpIterator(receiver))
45
+ throw new TypeError("RegExp string iterator next requires a matching receiver.");
46
+ return nextRegExpIterator(receiver, budget);
47
+ }
48
+ });
49
+ }
@@ -9,4 +9,6 @@ export declare function getStringIndex(property: string | number): number | unde
9
9
  export declare function isStringMethodName(property: string | number): property is StringMethodName;
10
10
  export declare function validateStringMethodArguments(_methodName: StringMethodName, _args: readonly Expression[]): void;
11
11
  export declare function callStringMethod(value: SandboxValue, methodName: StringMethodName, args: readonly SandboxValue[], budget: Budget, callClosure?: (closure: SandboxClosure, args: readonly SandboxValue[]) => Promise<SandboxValue>, parent?: CompileScope, context?: SandboxCallContext): SandboxValue | Promise<SandboxValue>;
12
+ export declare function regexReplace(input: SandboxValue, regex: SandboxValue, replacementInput: SandboxValue, budget: Budget, callClosure: (closure: SandboxClosure, args: readonly SandboxValue[]) => Promise<SandboxValue>, context?: SandboxCallContext): Promise<string>;
13
+ export declare function regexMatch(input: SandboxValue, regex: SandboxValue, budget: Budget, context?: SandboxCallContext): Promise<SandboxValue>;
12
14
  export {};