@poe-platform/safe-js 0.1.111 → 0.1.113

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.
@@ -27,7 +27,7 @@ import {
27
27
  validateMigrationSemantics,
28
28
  validateSnapshotData,
29
29
  validateSnapshotMigration
30
- } from "./chunk-7AIY5C3F.js";
30
+ } from "./chunk-DVWTNFTV.js";
31
31
 
32
32
  // packages/safe-js/src/migrate.ts
33
33
  import { createHash } from "node:crypto";
@@ -8245,4 +8245,4 @@ export {
8245
8245
  parseMcpConfig,
8246
8246
  makeMcpModule
8247
8247
  };
8248
- //# sourceMappingURL=chunk-VNXVV3WI.js.map
8248
+ //# sourceMappingURL=chunk-7Z6HCXET.js.map
@@ -6337,6 +6337,72 @@ function serializedDateTime(value) {
6337
6337
  return Number.isNaN(time) ? null : time;
6338
6338
  }
6339
6339
 
6340
+ // packages/safe-js/src/interp/collection-brands.ts
6341
+ var sandboxMapBrand = /* @__PURE__ */ Symbol("SandboxMap");
6342
+ var sandboxSetBrand = /* @__PURE__ */ Symbol("SandboxSet");
6343
+ function isSandboxMap(value) {
6344
+ return typeof value === "object" && value !== null && sandboxMapBrand in value;
6345
+ }
6346
+ function isSandboxSet(value) {
6347
+ return typeof value === "object" && value !== null && sandboxSetBrand in value;
6348
+ }
6349
+
6350
+ // packages/safe-js/src/interp/collection-iterator.ts
6351
+ var states = /* @__PURE__ */ new WeakMap();
6352
+ function isSandboxCollectionIterator(value) {
6353
+ return typeof value === "object" && value !== null && states.has(value);
6354
+ }
6355
+ function createSandboxCollectionIterator(collection, method) {
6356
+ return restoreSandboxCollectionIterator({ collection, collectionKind: collection.kind, method, index: 0, exhausted: false });
6357
+ }
6358
+ function restoreSandboxCollectionIterator(snapshot, target = /* @__PURE__ */ Object.create(null)) {
6359
+ const { collection, collectionKind, method, index, exhausted } = snapshot;
6360
+ if (collection !== void 0 && collection.kind !== collectionKind)
6361
+ throw new TypeError("Collection iterator source has the wrong brand.");
6362
+ if (!exhausted && collection === void 0)
6363
+ throw new TypeError("Live collection iterator requires its source collection.");
6364
+ const iterator = exhausted ? void 0 : nativeIterator(collection, method);
6365
+ for (let skipped = 0; skipped < index; skipped += 1) {
6366
+ if (iterator === void 0 || iterator.next().done)
6367
+ throw new TypeError("Collection iterator cursor exceeds its source.");
6368
+ }
6369
+ states.set(target, { collection: exhausted ? void 0 : collection, collectionKind, method, exhausted, iterator });
6370
+ return target;
6371
+ }
6372
+ function collectionIteratorState(value) {
6373
+ const state = states.get(value);
6374
+ if (state === void 0) throw new TypeError("Expected a collection iterator.");
6375
+ return state;
6376
+ }
6377
+ function snapshotCollectionIterator(value) {
6378
+ const state = collectionIteratorState(value);
6379
+ if (state.exhausted) return { ...state, index: 0 };
6380
+ const collection = state.collection;
6381
+ const size = collection.kind === "map" ? collection.entries.size : collection.values.size;
6382
+ let remaining = 0;
6383
+ while (!state.iterator.next().done) remaining += 1;
6384
+ const snapshot = { ...state, index: size - remaining };
6385
+ restoreSandboxCollectionIterator(snapshot, value);
6386
+ return snapshot;
6387
+ }
6388
+ function nextCollectionIterator(value, budget) {
6389
+ const state = states.get(value);
6390
+ budget?.visitNode();
6391
+ if (state.exhausted) return { value: void 0, done: true };
6392
+ const result = state.iterator.next();
6393
+ if (result.done) {
6394
+ state.exhausted = true;
6395
+ state.collection = void 0;
6396
+ state.iterator = void 0;
6397
+ return { value: void 0, done: true };
6398
+ }
6399
+ if (state.method === "entries") budget?.allocateArrayLength(2);
6400
+ return { value: result.value, done: false };
6401
+ }
6402
+ function nativeIterator(collection, method) {
6403
+ return collection.kind === "map" ? collection.entries[method]() : collection.values[method]();
6404
+ }
6405
+
6340
6406
  // packages/safe-js/src/interp/host-capabilities.ts
6341
6407
  import { types as types3 } from "node:util";
6342
6408
  var MAX_INDEXED_LENGTH = 65536;
@@ -6909,16 +6975,19 @@ function graphEntries(value) {
6909
6975
  if (isSandboxArguments(value)) {
6910
6976
  return getSandboxArgumentEntries(value).map(([key, entry]) => [`.${key}`, entry]);
6911
6977
  }
6912
- if (value instanceof Map) {
6913
- return [...value.entries()].flatMap(([key, entry], index) => [
6978
+ const map = isSandboxMap(value) ? value.entries : value instanceof Map ? value : void 0;
6979
+ if (map !== void 0) {
6980
+ return [...map.entries()].flatMap(([key, entry], index) => [
6914
6981
  [`.<map>[${index}].key`, key],
6915
6982
  [`.<map>[${index}].value`, entry]
6916
6983
  ]);
6917
6984
  }
6918
- if (value instanceof Set) {
6919
- return [...value.values()].map((entry, index) => [`.<set>[${index}]`, entry]);
6985
+ const set = isSandboxSet(value) ? value.values : value instanceof Set ? value : void 0;
6986
+ if (set !== void 0) {
6987
+ return [...set.values()].map((entry, index) => [`.<set>[${index}]`, entry]);
6920
6988
  }
6921
6989
  const entries = [];
6990
+ if (isSandboxCollectionIterator(value)) entries.push([".<collection>", collectionIteratorState(value).collection]);
6922
6991
  for (const key of Object.keys(value)) {
6923
6992
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
6924
6993
  if (descriptor !== void 0 && "value" in descriptor)
@@ -7581,6 +7650,7 @@ function validateTaggedValue(record2, path, state) {
7581
7650
  case "object":
7582
7651
  case "map":
7583
7652
  case "set":
7653
+ case "collection-iterator":
7584
7654
  return;
7585
7655
  }
7586
7656
  }
@@ -8548,7 +8618,7 @@ async function objectToPrimitive(value, budget, context, joining, hint) {
8548
8618
  }
8549
8619
  }
8550
8620
  function conversionHook(value, name, budget) {
8551
- const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) || isSandboxDate(value) || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value) || isSandboxRegex(value) || isSandboxGenerator(value) || isGuestHostObject(value));
8621
+ const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) || isSandboxDate(value) || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) || isSandboxGenerator(value) || isGuestHostObject(value));
8552
8622
  let current = value;
8553
8623
  let depth = 0;
8554
8624
  while (current !== null) {
@@ -8574,6 +8644,7 @@ function conversionHook(value, name, budget) {
8574
8644
  async function defaultToString(value, budget, context, joining) {
8575
8645
  if (isSandboxMap(value)) return "[object Map]";
8576
8646
  if (isSandboxSet(value)) return "[object Set]";
8647
+ if (isSandboxCollectionIterator(value)) return collectionIteratorState(value).collectionKind === "map" ? "[object Map Iterator]" : "[object Set Iterator]";
8577
8648
  if (isSandboxGenerator(value)) return "[object Generator]";
8578
8649
  if (isSandboxRegex(value)) {
8579
8650
  return budget.allocateString(
@@ -9119,7 +9190,8 @@ function assertSnapshotInactive(snapshot) {
9119
9190
  }
9120
9191
 
9121
9192
  // packages/safe-js/src/interp/iteration.ts
9122
- function getSandboxIterator(value) {
9193
+ function getSandboxIterator(value, budget) {
9194
+ if (isSandboxCollectionIterator(value)) return { next: () => nextCollectionIterator(value, budget), snapshotIndex: () => 0 };
9123
9195
  if (isGuestHostObject(value)) return getHostObjectIterator(value);
9124
9196
  if (isFloat32Array(value)) {
9125
9197
  return syncIterator(Float32Array.prototype.values.call(value));
@@ -9685,7 +9757,7 @@ async function settleIterable(iterable, method, budget, constructor, context) {
9685
9757
  const promiseResolve = readPromiseReceiverProperty(constructor, "resolve", prototype);
9686
9758
  if (!isSandboxClosure(promiseResolve))
9687
9759
  throw new TypeError("Promise constructor requires a callable resolve.");
9688
- const iterator = getSandboxIterator(iterable);
9760
+ const iterator = getSandboxIterator(iterable, budget);
9689
9761
  if (iterator === void 0) throw new TypeError("Promise helpers require an iterable.");
9690
9762
  let index = 0;
9691
9763
  while (true) {
@@ -10127,6 +10199,7 @@ function registerCancelablePromises(value, signal) {
10127
10199
  } else if (isSandboxSet(current)) {
10128
10200
  for (const entry of current.values) pending.push(entry);
10129
10201
  } else {
10202
+ if (isSandboxCollectionIterator(current)) pending.push(collectionIteratorState(current).collection);
10130
10203
  for (const descriptor of Object.values(Object.getOwnPropertyDescriptors(current))) {
10131
10204
  if ("value" in descriptor) pending.push(descriptor.value);
10132
10205
  }
@@ -10228,11 +10301,9 @@ function isPromiseLike2(value) {
10228
10301
  // packages/safe-js/src/interp/values.ts
10229
10302
  var sandboxClosureBrand = /* @__PURE__ */ Symbol("SandboxClosure");
10230
10303
  var sandboxGeneratorBrand = /* @__PURE__ */ Symbol("SandboxGenerator");
10231
- var sandboxMapBrand = /* @__PURE__ */ Symbol("SandboxMap");
10232
10304
  var sandboxPromiseBrand = /* @__PURE__ */ Symbol("SandboxPromise");
10233
10305
  var sandboxRegexBrand = /* @__PURE__ */ Symbol("SandboxRegex");
10234
10306
  var sandboxRegexPattern = /* @__PURE__ */ Symbol("SandboxRegexPattern");
10235
- var sandboxSetBrand = /* @__PURE__ */ Symbol("SandboxSet");
10236
10307
  var sandboxRetainedValues = /* @__PURE__ */ Symbol("SandboxRetainedValues");
10237
10308
  function createSandboxClosure(input) {
10238
10309
  const closure = {
@@ -10399,15 +10470,9 @@ function getSandboxRegexPattern(regex) {
10399
10470
  function isSandboxClosure(value) {
10400
10471
  return typeof value === "object" && value !== null && sandboxClosureBrand in value;
10401
10472
  }
10402
- function isSandboxMap(value) {
10403
- return typeof value === "object" && value !== null && sandboxMapBrand in value;
10404
- }
10405
10473
  function isSandboxPromise(value) {
10406
10474
  return typeof value === "object" && value !== null && sandboxPromiseBrand in value;
10407
10475
  }
10408
- function isSandboxSet(value) {
10409
- return typeof value === "object" && value !== null && sandboxSetBrand in value;
10410
- }
10411
10476
  function isSandboxGenerator(value) {
10412
10477
  return typeof value === "object" && value !== null && sandboxGeneratorBrand in value;
10413
10478
  }
@@ -10420,14 +10485,18 @@ function deepCopyToSandbox(value) {
10420
10485
  });
10421
10486
  }
10422
10487
  function cloneSandboxValue(value) {
10423
- return copyToSandbox(
10488
+ const initializeIterators = [];
10489
+ const copy = copyToSandbox(
10424
10490
  value,
10425
10491
  {
10426
- seen: /* @__PURE__ */ new WeakMap()
10492
+ seen: /* @__PURE__ */ new WeakMap(),
10493
+ initializeIterators
10427
10494
  },
10428
10495
  "<root>",
10429
10496
  true
10430
10497
  );
10498
+ for (const initialize of initializeIterators) initialize();
10499
+ return copy;
10431
10500
  }
10432
10501
  function allocateProducedSandboxValue(value, budget) {
10433
10502
  allocateSandboxValue2(value, budget, /* @__PURE__ */ new WeakSet());
@@ -10492,6 +10561,14 @@ function measureSandboxData(values, options = {}) {
10492
10561
  }
10493
10562
  return;
10494
10563
  }
10564
+ if (isSandboxCollectionIterator(value)) {
10565
+ visit(collectionIteratorState(value).collection, depth + 1);
10566
+ for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) {
10567
+ usage += key.length + 1;
10568
+ if ("value" in descriptor) visit(descriptor.value, depth + 1);
10569
+ }
10570
+ return;
10571
+ }
10495
10572
  if (isSandboxSet(value)) {
10496
10573
  usage += value.values.size;
10497
10574
  for (const entry of value.values) visit(entry, depth + 1);
@@ -10600,6 +10677,24 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
10600
10677
  if (isSandboxClosure(value) || isSandboxGenerator(value) || isSandboxRegex(value) || isSandboxPromise(value)) {
10601
10678
  return value;
10602
10679
  }
10680
+ if (isSandboxCollectionIterator(value)) {
10681
+ if (hasGuestObjectState(value)) throw new TypeError("Guest prototype links and custom descriptors cannot be copied as data.");
10682
+ if (!cloneSandboxCollections) return value;
10683
+ const existing = state.seen.get(value);
10684
+ if (existing !== void 0) return existing;
10685
+ const snapshot = snapshotCollectionIterator(value);
10686
+ const copy = restoreSandboxCollectionIterator({ ...snapshot, collection: void 0, index: 0, exhausted: true });
10687
+ state.seen.set(value, copy);
10688
+ const collection = copyToSandbox(snapshot.collection, state, `${path}.<collection>`, true, depth + 1);
10689
+ if (collection !== void 0 && !isSandboxMap(collection) && !isSandboxSet(collection)) throw new TypeError("Invalid cloned collection iterator source.");
10690
+ state.initializeIterators.push(() => {
10691
+ restoreSandboxCollectionIterator({ ...snapshot, collection }, copy);
10692
+ });
10693
+ for (const entry of getEnumerableObjectEntries(value, path)) {
10694
+ defineOwnDataProperty(copy, entry.key, copyToSandbox(entry.value, state, joinPath(path, entry.key), true, depth + 1));
10695
+ }
10696
+ return copy;
10697
+ }
10603
10698
  if (typeof value === "object" && value !== null && hasGuestObjectState(value)) {
10604
10699
  throw new TypeError("Guest prototype links and custom descriptors cannot be copied as data.");
10605
10700
  }
@@ -10855,6 +10950,9 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
10855
10950
  if (isSandboxGenerator(value)) {
10856
10951
  throw new TypeError("Sandbox generators cannot cross into host values.");
10857
10952
  }
10953
+ if (isSandboxCollectionIterator(value)) {
10954
+ throw new TypeError("Sandbox collection iterators cannot cross into host values.");
10955
+ }
10858
10956
  if (isSandboxRegex(value)) {
10859
10957
  throw new TypeError("Invalid sandbox RegExp brand.");
10860
10958
  }
@@ -11148,6 +11246,14 @@ function encodeReplayData(value, options = {}) {
11148
11246
  };
11149
11247
  }
11150
11248
  nodes[id] = { ...storage, properties, extensible: Object.isExtensible(entry) };
11249
+ } else if (isSandboxCollectionIterator(entry)) {
11250
+ const snapshot = snapshotCollectionIterator(entry);
11251
+ const properties = /* @__PURE__ */ Object.create(null);
11252
+ for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(entry))) {
11253
+ if (!("value" in descriptor)) throw new TypeError(`Cannot record replay data accessor '${key}'.`);
11254
+ properties[key] = { value: child(descriptor.value, JSON.stringify(["property", key])), configurable: descriptor.configurable === true, enumerable: descriptor.enumerable === true, writable: descriptor.writable === true };
11255
+ }
11256
+ nodes[id] = { kind: "collection-iterator", collectionKind: snapshot.collectionKind, method: snapshot.method, collection: child(snapshot.collection, "<collection>"), index: snapshot.index, exhausted: snapshot.exhausted, properties, extensible: Object.isExtensible(entry) };
11151
11257
  } else if (isSandboxMap(entry)) {
11152
11258
  nodes[id] = {
11153
11259
  kind: "map",
@@ -11206,6 +11312,7 @@ function decodeReplayData(input, options = {}, parent) {
11206
11312
  const graph = record(input);
11207
11313
  const nodes = list(own(graph, "nodes"));
11208
11314
  const restored = /* @__PURE__ */ new Map();
11315
+ const initializeIterators = [];
11209
11316
  const decode = (entry, depth = 0) => {
11210
11317
  if (depth > MAX_DATA_DEPTH) throw new TypeError("Replay data exceeds the nesting limit.");
11211
11318
  if (entry === null || typeof entry === "boolean" || typeof entry === "string") return entry;
@@ -11298,6 +11405,23 @@ function decodeReplayData(input, options = {}, parent) {
11298
11405
  if (!node.extensible) Object.preventExtensions(result3);
11299
11406
  return result3;
11300
11407
  }
11408
+ if (kind === "collection-iterator") {
11409
+ const collectionKind = own(node, "collectionKind");
11410
+ const method = own(node, "method");
11411
+ const index = own(node, "index");
11412
+ const exhausted = own(node, "exhausted");
11413
+ if (collectionKind !== "map" && collectionKind !== "set" || method !== "keys" && method !== "values" && method !== "entries" || typeof index !== "number" || !Number.isSafeInteger(index) || index < 0 || typeof exhausted !== "boolean" || typeof node.extensible !== "boolean") throw new TypeError("Invalid replay collection iterator.");
11414
+ const result3 = restoreSandboxCollectionIterator({ collection: void 0, collectionKind, method, index: 0, exhausted: true });
11415
+ restored.set(id, result3);
11416
+ const collection = child(own(node, "collection"));
11417
+ if (collection !== void 0 && !isSandboxMap(collection) && !isSandboxSet(collection)) throw new TypeError("Invalid replay collection iterator source.");
11418
+ initializeIterators.push(() => {
11419
+ restoreSandboxCollectionIterator({ collection, collectionKind, method, index, exhausted }, result3);
11420
+ });
11421
+ defineProperties(result3, record(own(node, "properties")), child);
11422
+ if (!node.extensible) Object.preventExtensions(result3);
11423
+ return result3;
11424
+ }
11301
11425
  if (kind === "map") {
11302
11426
  const result3 = createSandboxMap();
11303
11427
  restored.set(id, result3);
@@ -11354,6 +11478,7 @@ function decodeReplayData(input, options = {}, parent) {
11354
11478
  return result2;
11355
11479
  };
11356
11480
  const result = decode(own(graph, "root"));
11481
+ for (const initialize of initializeIterators) initialize();
11357
11482
  if (parent !== void 0) compilation.forward(compilation.tickets, parent);
11358
11483
  return result;
11359
11484
  } finally {
@@ -23540,13 +23665,27 @@ async function bindAssignmentPattern2(pattern, value, target, scope, context) {
23540
23665
  return bindPattern2(pattern.left, defaultValue.value, target, scope, context);
23541
23666
  }
23542
23667
  async function bindArrayPattern2(pattern, value, target, scope, context) {
23543
- const values = getArrayPatternValues(value);
23668
+ const iterator = isSandboxCollectionIterator(value) ? value : void 0;
23669
+ const values = iterator === void 0 ? getArrayPatternValues(value) : void 0;
23544
23670
  for (let index = 0; index < pattern.elements.length; index += 1) {
23545
23671
  const element = pattern.elements[index];
23546
23672
  if (element === null) {
23673
+ if (iterator !== void 0) nextCollectionIterator(iterator, context.budget);
23547
23674
  continue;
23548
23675
  }
23549
- const elementValue = element.type === "RestElement" ? values.slice(index) : values[index];
23676
+ let elementValue;
23677
+ if (iterator === void 0) {
23678
+ elementValue = element.type === "RestElement" ? values.slice(index) : values[index];
23679
+ } else if (element.type === "RestElement") {
23680
+ const rest = [];
23681
+ for (let entry = nextCollectionIterator(iterator, context.budget); !entry.done; entry = nextCollectionIterator(iterator, context.budget)) {
23682
+ context.budget?.allocateArrayLength(rest.length + 1);
23683
+ rest.push(entry.value);
23684
+ }
23685
+ elementValue = rest;
23686
+ } else {
23687
+ elementValue = nextCollectionIterator(iterator, context.budget).value;
23688
+ }
23550
23689
  const binding = await bindPattern2(element, elementValue, target, scope, context);
23551
23690
  if (!binding.ok) {
23552
23691
  return binding;
@@ -24580,14 +24719,9 @@ async function callMapMethod(target, methodName, args, options, stack = []) {
24580
24719
  return void 0;
24581
24720
  }
24582
24721
  case "keys":
24583
- return allocateProducedSandboxValue([...target.entries.keys()], options.budget);
24584
24722
  case "values":
24585
- return allocateProducedSandboxValue([...target.entries.values()], options.budget);
24586
24723
  case "entries":
24587
- return allocateProducedSandboxValue(
24588
- [...target.entries].map(([key, value]) => [key, value]),
24589
- options.budget
24590
- );
24724
+ return createSandboxCollectionIterator(target, methodName);
24591
24725
  }
24592
24726
  }
24593
24727
 
@@ -25184,12 +25318,8 @@ async function callSetMethod(target, methodName, args, options, stack = []) {
25184
25318
  }
25185
25319
  case "keys":
25186
25320
  case "values":
25187
- return allocateProducedSandboxValue([...target.values], options.budget);
25188
25321
  case "entries":
25189
- return allocateProducedSandboxValue(
25190
- [...target.values].map((value) => [value, value]),
25191
- options.budget
25192
- );
25322
+ return createSandboxCollectionIterator(target, methodName);
25193
25323
  }
25194
25324
  }
25195
25325
 
@@ -25364,6 +25494,7 @@ function typeTag(value) {
25364
25494
  if (isSandboxRegex(value)) return "RegExp";
25365
25495
  if (isSandboxMap(value)) return "Map";
25366
25496
  if (isSandboxSet(value)) return "Set";
25497
+ if (isSandboxCollectionIterator(value)) return collectionIteratorState(value).collectionKind === "map" ? "Map Iterator" : "Set Iterator";
25367
25498
  if (isSandboxPromise(value)) return "Promise";
25368
25499
  if (isSandboxGenerator(value)) return "Generator";
25369
25500
  if (isFloat32Array(value)) return "Float32Array";
@@ -25452,7 +25583,7 @@ function isSandboxSetConstructor(value) {
25452
25583
  function populateCollection(source, collection, { name, budget, context, append, retainedValues }) {
25453
25584
  budget.allocateCollectionEntries(0);
25454
25585
  if (source === void 0 || source === null) return collection;
25455
- const iterator = getSandboxIterator(source);
25586
+ const iterator = getSandboxIterator(source, budget);
25456
25587
  if (iterator === void 0) throw new TypeError(`${name} constructor requires an iterable.`);
25457
25588
  let entry;
25458
25589
  let failure;
@@ -25513,6 +25644,23 @@ function populateCollection(source, collection, { name, budget, context, append,
25513
25644
  }
25514
25645
  }
25515
25646
 
25647
+ // packages/safe-js/src/interp/methods/collection-iterator.ts
25648
+ function getCollectionIteratorMember(target, property, budget) {
25649
+ if (Object.hasOwn(target, property)) return target[property];
25650
+ if (property !== "next") return void 0;
25651
+ const { collectionKind } = collectionIteratorState(target);
25652
+ return createSandboxClosure({
25653
+ sandbox: true,
25654
+ name: "next",
25655
+ call: (_args, context) => {
25656
+ const receiver = context?.thisValue;
25657
+ if (!isSandboxCollectionIterator(receiver) || collectionIteratorState(receiver).collectionKind !== collectionKind)
25658
+ throw new TypeError("Iterator next requires a matching collection iterator receiver.");
25659
+ return nextCollectionIterator(receiver, budget);
25660
+ }
25661
+ });
25662
+ }
25663
+
25516
25664
  // packages/safe-js/src/interp/globals/float32array.ts
25517
25665
  var constructors = /* @__PURE__ */ new WeakSet();
25518
25666
  function createFloat32ArrayGlobal(budget) {
@@ -26697,6 +26845,9 @@ function isRestorableBindingValue(value, seen = /* @__PURE__ */ new WeakSet()) {
26697
26845
  }
26698
26846
  if (seen.has(value)) return true;
26699
26847
  seen.add(value);
26848
+ if (isSandboxCollectionIterator(value)) {
26849
+ return isRestorableBindingValue(collectionIteratorState(value).collection, seen);
26850
+ }
26700
26851
  if (isSandboxMap(value)) {
26701
26852
  for (const [key, entry] of value.entries) {
26702
26853
  if (!isRestorableBindingValue(key, seen) || !isRestorableBindingValue(entry, seen)) {
@@ -26939,7 +27090,7 @@ async function evaluateForOfStatement(node, context) {
26939
27090
  const restored = context.scope.consumeRestoredBinding(restoredIteration.values[0]);
26940
27091
  if (restored.found && Array.isArray(restored.value)) {
26941
27092
  restoredEntry = { done: false, value: restored.value[1] };
26942
- if (isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0])) {
27093
+ if (isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0]) || isSandboxCollectionIterator(restored.value[0])) {
26943
27094
  return evaluateForOfIterator(node, restored.value[0], context, restoredEntry);
26944
27095
  }
26945
27096
  }
@@ -26987,7 +27138,7 @@ async function evaluateForOfStatement(node, context) {
26987
27138
  };
26988
27139
  }
26989
27140
  async function evaluateForOfIterator(node, value, context, restoredEntry) {
26990
- const iterator = getSandboxIterator(value);
27141
+ const iterator = getSandboxIterator(value, context.budget);
26991
27142
  if (iterator === void 0) {
26992
27143
  throw new TypeError(`${String(value)} is not a supported iterable`);
26993
27144
  }
@@ -27286,7 +27437,7 @@ function createLoopIterationContext(context, scope) {
27286
27437
  if (context.activeLoopIterations.size === 0) return snapshot;
27287
27438
  snapshot.loopIterations = {};
27288
27439
  for (const [nodeId, iteration] of context.activeLoopIterations) {
27289
- if (typeof iteration !== "number" && (isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]))) {
27440
+ if (typeof iteration !== "number" && (isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]) || isSandboxCollectionIterator(iteration.values[0]))) {
27290
27441
  const bindingName = `#for-of:${nodeId}`;
27291
27442
  snapshot.bindings[bindingName] = iteration.values;
27292
27443
  snapshot.loopIterations[nodeId] = { index: iteration.index, values: [bindingName] };
@@ -27413,7 +27564,7 @@ async function evaluateYieldDelegate(node, context) {
27413
27564
  if (argument.kind !== "normal") {
27414
27565
  return argument;
27415
27566
  }
27416
- const iterator = getSandboxIterator(argument.value);
27567
+ const iterator = getSandboxIterator(argument.value, context.budget);
27417
27568
  if (iterator === void 0) {
27418
27569
  throw new TypeError(`${String(argument.value)} is not a supported iterable`);
27419
27570
  }
@@ -27623,6 +27774,7 @@ function getPropertyValue(target, property, context) {
27623
27774
  if (isSandboxDate(target)) return getDateMember(property, context.budget, context.compilation?.owner);
27624
27775
  if (isSandboxMap(target)) return getMapMember(target, property, createMapMethodOptions(context));
27625
27776
  if (isSandboxSet(target)) return getSetMember(target, property, createSetMethodOptions(context));
27777
+ if (isSandboxCollectionIterator(target)) return getCollectionIteratorMember(target, property, context.budget);
27626
27778
  if (isSandboxGenerator(target)) return getGeneratorMember(target, property, context.budget);
27627
27779
  if (isSandboxClosure(target)) return getClosureMemberValue(target, property, context);
27628
27780
  if (isSandboxPromise(target)) return getPromiseMember(property, context.budget);
@@ -27635,6 +27787,7 @@ function getPropertyValue(target, property, context) {
27635
27787
  function createPatternContext(context, scope = context.scope, evaluate = evaluateNode) {
27636
27788
  const evaluationContext = { ...context, scope };
27637
27789
  return {
27790
+ budget: context.budget,
27638
27791
  evaluate: (node) => evaluate(node, evaluationContext),
27639
27792
  toPropertyKey: (value) => toPropertyKey(value, context.budget, createCoercionContext(evaluationContext)),
27640
27793
  getProperty: (value, key) => getPropertyValue(value, key, evaluationContext),
@@ -28142,7 +28295,7 @@ function hasSandboxProperty(value, key, context) {
28142
28295
  while (typeof current === "object" && current !== null) {
28143
28296
  if (isGuestHostObject(current)) return hasHostObjectMember(current, key);
28144
28297
  if (hasOwnSandboxProperty(current, key, false)) return true;
28145
- if (Array.isArray(current) || !isPlainSandboxObject(current) || isSandboxDate(current) || isFloat32Array(current) || isSandboxGenerator(current)) {
28298
+ if (Array.isArray(current) || !isPlainSandboxObject(current) || isSandboxDate(current) || isFloat32Array(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current)) {
28146
28299
  return getPropertyValue(current, key, context) !== void 0;
28147
28300
  }
28148
28301
  current = getSandboxPrototype(current, context.budget);
@@ -28352,7 +28505,7 @@ function getMemberValue(target, property, context) {
28352
28505
  while (typeof current === "object" && current !== null) {
28353
28506
  if (isSandboxClosure(current)) return getClosureMemberValue(current, property, context);
28354
28507
  if (Array.isArray(current)) return getArrayMemberValue(current, property, context);
28355
- if (!isPlainSandboxObject(current) || isSandboxGenerator(current) || isFloat32Array(current)) {
28508
+ if (!isPlainSandboxObject(current) || isSandboxGenerator(current) || isSandboxCollectionIterator(current) || isFloat32Array(current)) {
28356
28509
  return getPropertyValue(current, property, context);
28357
28510
  }
28358
28511
  if (Object.hasOwn(current, String(property))) return current[String(property)];
@@ -28567,7 +28720,7 @@ async function evaluateSpreadElement(node, context) {
28567
28720
  result: value
28568
28721
  };
28569
28722
  }
28570
- const iterator = getSpreadIterator(value.value);
28723
+ const iterator = getSandboxIterator(value.value, context.budget);
28571
28724
  if (iterator === void 0) {
28572
28725
  throw new TypeError("Spread arguments must evaluate to an iterable.");
28573
28726
  }
@@ -28637,9 +28790,6 @@ function describeObjectSpreadValue(value) {
28637
28790
  }
28638
28791
  return typeof value;
28639
28792
  }
28640
- function getSpreadIterator(value) {
28641
- return getSandboxIterator(value);
28642
- }
28643
28793
  async function closeIterator(iterator) {
28644
28794
  if (iterator.generator && iterator.return !== void 0) {
28645
28795
  await iterator.return();
@@ -30424,8 +30574,8 @@ function structuredCloneSandboxValue(value, budget) {
30424
30574
  return allocateProducedSandboxValue(clone, budget);
30425
30575
  }
30426
30576
  function assertStructuredCloneable(value, seen) {
30427
- if (isSandboxClosure(value) || isSandboxPromise(value)) {
30428
- throw new TypeError("structuredClone() cannot clone closures or promises.");
30577
+ if (isSandboxClosure(value) || isSandboxPromise(value) || isSandboxCollectionIterator(value)) {
30578
+ throw new TypeError("structuredClone() cannot clone closures, promises, or collection iterators.");
30429
30579
  }
30430
30580
  if (typeof value !== "object" || value === null || seen.has(value)) {
30431
30581
  return;
@@ -30553,7 +30703,7 @@ function createObjectArrayGlobals(options) {
30553
30703
  fromEntries: createSandboxClosure({
30554
30704
  sandbox: true,
30555
30705
  call: ([value], context) => {
30556
- const iterator = getSandboxIterator(value);
30706
+ const iterator = getSandboxIterator(value, options.budget);
30557
30707
  if (iterator === void 0) {
30558
30708
  throw new TypeError("Object.fromEntries requires an iterable.");
30559
30709
  }
@@ -30816,7 +30966,7 @@ async function arrayFromSandboxValues(args, budget, context) {
30816
30966
  throw new TypeError("Array.from requires a non-null input.");
30817
30967
  }
30818
30968
  const read = (property) => context?.getProperty !== void 0 ? context.getProperty(items, property) : getSandboxDataProperty(items, property, budget);
30819
- const iterator = getSandboxIterator(items);
30969
+ const iterator = getSandboxIterator(items, budget);
30820
30970
  const constructor = context?.thisValue;
30821
30971
  let result;
30822
30972
  let currentValue;
@@ -32396,6 +32546,16 @@ function prepareReplayInputs(current, saved, preparePromise, onCapabilityRestore
32396
32546
  throw new TypeError("Invalid replay input capability path.");
32397
32547
  let value = current;
32398
32548
  for (const key of path) {
32549
+ if (isSandboxCollectionIterator(value)) {
32550
+ if (key === "<collection>") value = collectionIteratorState(value).collection;
32551
+ else {
32552
+ const property = JSON.parse(key);
32553
+ if (!Array.isArray(property) || property.length !== 2 || property[0] !== "property" || typeof property[1] !== "string") throw new TypeError("Invalid replay input iterator capability path.");
32554
+ const descriptor2 = Object.getOwnPropertyDescriptor(value, property[1]);
32555
+ value = descriptor2 !== void 0 && "value" in descriptor2 ? descriptor2.value : void 0;
32556
+ }
32557
+ continue;
32558
+ }
32399
32559
  if (isSandboxMap(value)) {
32400
32560
  const [kind, ordinal] = key.split(":");
32401
32561
  const index = Number(ordinal);
@@ -32456,11 +32616,11 @@ function prepareReplayInputs(current, saved, preparePromise, onCapabilityRestore
32456
32616
  return { values: restored, snapshot: structuredClone(saved) };
32457
32617
  }
32458
32618
  function assertReplayInputShape(restored) {
32459
- if (restored === null || typeof restored !== "object" || Array.isArray(restored) || isSandboxClosure(restored) || isSandboxPromise(restored) || isSandboxMap(restored) || isSandboxSet(restored))
32619
+ if (restored === null || typeof restored !== "object" || Array.isArray(restored) || isSandboxClosure(restored) || isSandboxPromise(restored) || isSandboxCollectionIterator(restored) || isSandboxMap(restored) || isSandboxSet(restored))
32460
32620
  throw new TypeError("Invalid replay inputs.");
32461
32621
  const values = restored;
32462
32622
  for (const key of ["bindings", "imports"]) {
32463
- if (values[key] === null || typeof values[key] !== "object" || Array.isArray(values[key]) || isSandboxClosure(values[key]) || isSandboxPromise(values[key]) || isSandboxMap(values[key]) || isSandboxSet(values[key]))
32623
+ if (values[key] === null || typeof values[key] !== "object" || Array.isArray(values[key]) || isSandboxClosure(values[key]) || isSandboxPromise(values[key]) || isSandboxCollectionIterator(values[key]) || isSandboxMap(values[key]) || isSandboxSet(values[key]))
32464
32624
  throw new TypeError(`Invalid replay input ${key}.`);
32465
32625
  }
32466
32626
  if (values.entryPointArgs !== void 0 && !Array.isArray(values.entryPointArgs))
@@ -33053,4 +33213,4 @@ export {
33053
33213
  FileSnapshotBackend,
33054
33214
  run
33055
33215
  };
33056
- //# sourceMappingURL=chunk-7AIY5C3F.js.map
33216
+ //# sourceMappingURL=chunk-DVWTNFTV.js.map