@poe-platform/safe-js 0.1.292 → 0.1.294

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.
@@ -7790,6 +7790,8 @@ var regexPrototypes = /* @__PURE__ */ new WeakMap();
7790
7790
  var collectionPrototypes = /* @__PURE__ */ new WeakMap();
7791
7791
  var promisePrototypes = /* @__PURE__ */ new WeakMap();
7792
7792
  var datePrototypes = /* @__PURE__ */ new WeakMap();
7793
+ var arrayPrototypes = /* @__PURE__ */ new WeakMap();
7794
+ var initialArrayMethods = /* @__PURE__ */ new WeakMap();
7793
7795
  var initialRegexDescriptors = /* @__PURE__ */ new WeakMap();
7794
7796
  var intrinsicPrototypeRoots = /* @__PURE__ */ new WeakMap();
7795
7797
  var intrinsicConstructors = /* @__PURE__ */ new WeakMap();
@@ -7873,6 +7875,16 @@ function installDatePrototype(budget, prototype, constructor) {
7873
7875
  datePrototypes.set(budget, prototype);
7874
7876
  registerIntrinsicPrototype(budget, prototype, constructor);
7875
7877
  }
7878
+ function installArrayPrototype(budget, prototype, constructor) {
7879
+ arrayPrototypes.set(budget, prototype);
7880
+ prototypes.set(prototype, intrinsicPrototypes.get(budget) ?? null);
7881
+ initialArrayMethods.set(prototype, new Map(Object.entries(Object.getOwnPropertyDescriptors(prototype)).map(([key, descriptor]) => [key, descriptor.value])));
7882
+ registerIntrinsicPrototype(budget, prototype, constructor);
7883
+ }
7884
+ function isDefaultArrayMethod(value, key, budget) {
7885
+ const prototype = arrayPrototypes.get(budget);
7886
+ return prototype !== void 0 && getSandboxPrototype(value, budget) === prototype && !Object.hasOwn(value, key) && initialArrayMethods.get(prototype)?.has(key) === true && Object.getOwnPropertyDescriptor(prototype, key)?.value === initialArrayMethods.get(prototype)?.get(key);
7887
+ }
7876
7888
  function hasRegexPropertyOverride(value, keys, budget) {
7877
7889
  const initial = initialRegexDescriptors.get(budget);
7878
7890
  const prototype = regexPrototypes.get(budget);
@@ -7933,16 +7945,19 @@ function trackIntrinsicState(budget, root, owner, targets) {
7933
7945
  return getSandboxPrototype(target) === parent && Object.isExtensible(value) === extensible && hasExplicitSandboxPrototype(target) === explicit && Reflect.ownKeys(current).length === descriptors2.size && Reflect.ownKeys(value).every((key) => unchanged(descriptors2.get(key), Object.getOwnPropertyDescriptor(value, key)));
7934
7946
  })
7935
7947
  );
7936
- budget.setRetainedValues(
7937
- root,
7938
- () => records.flatMap(({ target, value, descriptors: descriptors2, prototype: parent }) => [
7939
- ...getSandboxPrototype(target) === parent ? [] : [getSandboxPrototype(target)],
7940
- ...Reflect.ownKeys(value).flatMap((key) => {
7948
+ budget.setRetainedValues(root, () => {
7949
+ const retained = [];
7950
+ for (const { target, value, descriptors: descriptors2, prototype: parent } of records) {
7951
+ const currentPrototype = getSandboxPrototype(target);
7952
+ if (currentPrototype !== parent) retained.push(currentPrototype);
7953
+ for (const key of Reflect.ownKeys(value)) {
7941
7954
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
7942
- return unchanged(descriptors2.get(key), descriptor) ? [] : [key, descriptor.value, ...retainedAccessorClosures(descriptor)];
7943
- })
7944
- ])
7945
- );
7955
+ if (unchanged(descriptors2.get(key), descriptor)) continue;
7956
+ retained.push(key, descriptor.value, ...retainedAccessorClosures(descriptor));
7957
+ }
7958
+ }
7959
+ return retained;
7960
+ });
7946
7961
  }
7947
7962
  function releaseObjectPrototype(budget) {
7948
7963
  releaseTemplateObjects(budget);
@@ -7954,11 +7969,13 @@ function releaseObjectPrototype(budget) {
7954
7969
  collectionPrototypes.delete(budget);
7955
7970
  promisePrototypes.delete(budget);
7956
7971
  datePrototypes.delete(budget);
7972
+ arrayPrototypes.delete(budget);
7957
7973
  initialRegexDescriptors.delete(budget);
7958
7974
  intrinsicPrototypes.delete(budget);
7959
7975
  }
7960
7976
  function getSandboxPrototype(value, budget) {
7961
7977
  if (prototypes.has(value)) return prototypes.get(value) ?? null;
7978
+ if (budget !== void 0 && Array.isArray(value)) return arrayPrototypes.get(budget) ?? null;
7962
7979
  if (budget !== void 0 && isSandboxDate(value)) return datePrototypes.get(budget) ?? null;
7963
7980
  if (budget !== void 0 && isSandboxPromise(value)) return promisePrototypes.get(budget) ?? null;
7964
7981
  if (budget !== void 0 && (isSandboxMap(value) || isSandboxSet(value)))
@@ -11590,1256 +11607,344 @@ function createPrimitiveConstructor(options, budget) {
11590
11607
  return constructor;
11591
11608
  }
11592
11609
 
11593
- // packages/safe-js/src/interp/globals/object-array.ts
11594
- function createObjectArrayGlobals(options) {
11595
- return {
11596
- Object: createObjectGlobal(
11597
- {
11598
- keys: createSandboxClosure({
11599
- sandbox: true,
11600
- call: ([value]) => budgetSandboxValue(ownEnumerableSandboxKeys(value), options.budget),
11601
- name: "keys"
11602
- }),
11603
- values: createSandboxClosure({
11604
- sandbox: true,
11605
- call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(
11606
- ownEnumerableSandboxEntries(value).map(([, entry]) => entry),
11607
- options.budget
11608
- ) : getOwnEnumerableEntries(value, options.budget, context).then(
11609
- (entries) => allocateProducedSandboxValue(
11610
- entries.map(([, entry]) => entry),
11611
- options.budget
11612
- )
11613
- ),
11614
- name: "values"
11615
- }),
11616
- entries: createSandboxClosure({
11617
- sandbox: true,
11618
- call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(ownEnumerableSandboxEntries(value), options.budget) : getOwnEnumerableEntries(value, options.budget, context).then(
11619
- (entries) => allocateProducedSandboxValue(entries, options.budget)
11620
- ),
11621
- name: "entries"
11622
- }),
11623
- hasOwn: createSandboxClosure({
11624
- sandbox: true,
11625
- call: ([value, key], context) => {
11626
- if (value === null || value === void 0)
11627
- throw new TypeError("Cannot convert undefined or null to object.");
11628
- const name = toPropertyKey(key, options.budget, context);
11629
- return typeof name === "string" || typeof name === "symbol" ? hasOwnSandboxProperty(value, name, false) : name.then((property) => hasOwnSandboxProperty(value, property, false));
11630
- },
11631
- name: "hasOwn"
11632
- }),
11633
- getOwnPropertyDescriptor: createSandboxClosure({
11634
- sandbox: true,
11635
- call: async ([value, key], context) => {
11636
- const descriptor = Object.getOwnPropertyDescriptor(
11637
- reflectionProperties(value),
11638
- await toPropertyKey(key, options.budget, context)
11639
- );
11640
- if (descriptor === void 0) return void 0;
11641
- return allocateProducedSandboxValue(
11642
- exposePropertyDescriptor(descriptor),
11643
- options.budget
11644
- );
11645
- },
11646
- name: "getOwnPropertyDescriptor"
11647
- }),
11648
- getOwnPropertyDescriptors: createSandboxClosure({
11649
- sandbox: true,
11650
- call: ([value]) => {
11651
- const descriptors2 = /* @__PURE__ */ Object.create(null);
11652
- const properties = reflectionProperties(value);
11653
- for (const key of [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(value)])
11654
- defineOwnDataProperty(descriptors2, key, exposePropertyDescriptor(Object.getOwnPropertyDescriptor(properties, key)));
11655
- return allocateProducedSandboxValue(descriptors2, options.budget);
11656
- },
11657
- name: "getOwnPropertyDescriptors"
11658
- }),
11659
- getOwnPropertyNames: createSandboxClosure({
11660
- sandbox: true,
11661
- call: ([value]) => budgetSandboxValue(Object.getOwnPropertyNames(reflectionProperties(value)), options.budget),
11662
- name: "getOwnPropertyNames"
11663
- }),
11664
- getOwnPropertySymbols: createSandboxClosure({
11665
- sandbox: true,
11666
- call: ([value]) => allocateProducedSandboxValue(ownSandboxSymbolKeys(value), options.budget),
11667
- name: "getOwnPropertySymbols"
11668
- }),
11669
- defineProperty: createSandboxClosure({
11670
- sandbox: true,
11671
- call: async ([value, key, descriptor], context) => {
11672
- objectProperties(value, true);
11673
- const property = await toPropertyKey(key, options.budget, context);
11674
- defineDataProperty(
11675
- value,
11676
- property,
11677
- await propertyDescriptor(descriptor, options.budget, context),
11678
- options.budget
11679
- );
11680
- return value;
11681
- },
11682
- name: "defineProperty"
11683
- }),
11684
- defineProperties: createSandboxClosure({
11685
- sandbox: true,
11686
- call: async ([value, descriptors2], context) => {
11687
- await definePropertiesFromObject(value, descriptors2, options.budget, context);
11688
- return value;
11689
- },
11690
- name: "defineProperties"
11691
- }),
11692
- getPrototypeOf: createSandboxClosure({
11693
- sandbox: true,
11694
- call: ([value]) => {
11695
- if (isSandboxMap(value) || isSandboxSet(value))
11696
- return getSandboxPrototype(value, options.budget);
11697
- if (value !== null && value !== void 0 && typeof value !== "object")
11698
- value = createSandboxBox(value);
11699
- objectProperties(value);
11700
- return getSandboxPrototype(value, options.budget);
11701
- },
11702
- name: "getPrototypeOf"
11703
- }),
11704
- setPrototypeOf: createSandboxClosure({
11705
- sandbox: true,
11706
- call: ([value, prototype]) => {
11707
- if (value === null || value === void 0)
11708
- throw new TypeError("Cannot set the prototype of null or undefined.");
11709
- if (prototype !== null && typeof prototype !== "object")
11710
- throw new TypeError("Prototype must be an object or null.");
11711
- if (typeof value !== "object") return value;
11712
- objectProperties(value, true);
11713
- if (prototype !== null) objectProperties(prototype);
11714
- setSandboxPrototype(value, prototype, options.budget);
11715
- return value;
11716
- },
11717
- name: "setPrototypeOf"
11718
- }),
11719
- create: createSandboxClosure({
11720
- sandbox: true,
11721
- call: async ([prototype, descriptors2], context) => {
11722
- if (prototype !== null) objectProperties(prototype);
11723
- const value = /* @__PURE__ */ Object.create(null);
11724
- setSandboxPrototype(value, prototype, options.budget);
11725
- if (descriptors2 !== void 0) {
11726
- await definePropertiesFromObject(value, descriptors2, options.budget, context);
11727
- }
11728
- return allocateProducedSandboxValue(value, options.budget);
11729
- },
11730
- name: "create"
11731
- }),
11732
- is: createSandboxClosure({
11733
- sandbox: true,
11734
- call: ([left, right]) => Reflect.apply(Object.is, Object, [left, right]),
11735
- name: "is"
11736
- }),
11737
- fromEntries: createSandboxClosure({
11738
- sandbox: true,
11739
- call: ([value], context) => {
11740
- if (context !== void 0) return acquireSandboxIterator(value, options.budget, context).then((iterator2) => {
11741
- if (iterator2 === void 0) throw new TypeError("Object.fromEntries requires an iterable.");
11742
- return objectFromSandboxEntries(value, iterator2, options.budget, context);
11743
- });
11744
- const iterator = getSandboxIterator(value, options.budget, context);
11745
- if (iterator === void 0) {
11746
- throw new TypeError("Object.fromEntries requires an iterable.");
11747
- }
11748
- if (context === void 0 && !iterator.generator && !iterator.asynchronous) {
11749
- return allocateProducedSandboxValue(
11750
- Object.setPrototypeOf(
11751
- Reflect.apply(Object.fromEntries, Object, [
11752
- { [Symbol.iterator]: () => iterator }
11753
- ]),
11754
- null
11755
- ),
11756
- options.budget
11757
- );
11758
- }
11759
- return objectFromSandboxEntries(value, iterator, options.budget, context);
11760
- },
11761
- name: "fromEntries"
11762
- }),
11763
- preventExtensions: createSandboxClosure({
11764
- sandbox: true,
11765
- call: ([value]) => {
11766
- if (isGuestHostObject(value))
11767
- throw new TypeError("Live host objects cannot be made non-extensible.");
11768
- Object.preventExtensions(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
11769
- return value;
11770
- },
11771
- name: "preventExtensions"
11772
- }),
11773
- isExtensible: createSandboxClosure({
11774
- sandbox: true,
11775
- call: ([value]) => Object.isExtensible(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
11776
- name: "isExtensible"
11777
- }),
11778
- seal: createSandboxClosure({
11779
- sandbox: true,
11780
- call: ([value]) => {
11781
- if (isGuestHostObject(value))
11782
- throw new TypeError("Live host objects cannot be sealed.");
11783
- Object.seal(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
11784
- return value;
11785
- },
11786
- name: "seal"
11787
- }),
11788
- isSealed: createSandboxClosure({
11789
- sandbox: true,
11790
- call: ([value]) => Object.isSealed(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
11791
- name: "isSealed"
11792
- }),
11793
- freeze: createSandboxClosure({
11794
- sandbox: true,
11795
- call: ([value]) => {
11796
- if (isGuestHostObject(value))
11797
- throw new TypeError("Live host objects cannot be frozen.");
11798
- if (typeof value === "object" && value !== null) {
11799
- Object.freeze(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
11800
- }
11801
- return value;
11802
- },
11803
- name: "freeze"
11804
- }),
11805
- isFrozen: createSandboxClosure({
11806
- sandbox: true,
11807
- call: ([value]) => Object.isFrozen(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
11808
- name: "isFrozen"
11809
- }),
11810
- assign: createSandboxClosure({
11811
- sandbox: true,
11812
- call: ([target, ...sources], context) => assignSandboxValues(target, sources, options.budget, context),
11813
- name: "assign"
11814
- })
11815
- },
11816
- options.budget
11817
- ),
11818
- Array: createSandboxClosure({
11610
+ // packages/safe-js/src/interp/methods/array.ts
11611
+ var arrayLikeSources = /* @__PURE__ */ new WeakMap();
11612
+ var activeArrayCallbacks = /* @__PURE__ */ new WeakMap();
11613
+ var localeArrays = /* @__PURE__ */ new WeakSet();
11614
+ var arrayMethodNames = /* @__PURE__ */ new Set([
11615
+ "map",
11616
+ "filter",
11617
+ "find",
11618
+ "findIndex",
11619
+ "findLast",
11620
+ "findLastIndex",
11621
+ "some",
11622
+ "every",
11623
+ "reduce",
11624
+ "reduceRight",
11625
+ "forEach",
11626
+ "flatMap",
11627
+ "flat",
11628
+ "includes",
11629
+ "indexOf",
11630
+ "lastIndexOf",
11631
+ "join",
11632
+ "toLocaleString",
11633
+ "slice",
11634
+ "concat",
11635
+ "splice",
11636
+ "fill",
11637
+ "copyWithin",
11638
+ "at",
11639
+ "sort",
11640
+ "reverse",
11641
+ "toSorted",
11642
+ "toReversed",
11643
+ "toSpliced",
11644
+ "with",
11645
+ "push",
11646
+ "pop",
11647
+ "shift",
11648
+ "unshift"
11649
+ ]);
11650
+ function getArrayMember(value, property, options) {
11651
+ const index = getArrayIndex(property);
11652
+ if (index !== void 0) {
11653
+ return Object.hasOwn(value, index) ? value[index] : void 0;
11654
+ }
11655
+ if (property === "length") {
11656
+ return value.length;
11657
+ }
11658
+ if (Object.hasOwn(value, property)) {
11659
+ return value[String(property)];
11660
+ }
11661
+ if (isArrayMethodName(property)) {
11662
+ return createSandboxClosure({
11819
11663
  sandbox: true,
11820
- call: (args) => createArrayFromConstructorArgs(args, options.budget),
11821
- construct: (args) => createArrayFromConstructorArgs(args, options.budget),
11822
- name: "Array",
11823
- properties: {
11824
- isArray: createSandboxClosure({
11825
- sandbox: true,
11826
- call: ([value]) => Array.isArray(value),
11827
- name: "isArray"
11828
- }),
11829
- from: createSandboxClosure({
11830
- sandbox: true,
11831
- call: (args, context) => arrayFromSandboxValues(args, options.budget, context),
11832
- name: "from"
11833
- }),
11834
- of: createSandboxClosure({
11835
- sandbox: true,
11836
- call: (args) => budgetSandboxValue(Reflect.apply(Array.of, Array, [...args]), options.budget),
11837
- name: "of"
11838
- })
11839
- }
11840
- }),
11841
- String: createPrimitiveConstructor(
11842
- {
11843
- call: (args, context) => typeof args[0] === "symbol" ? options.budget.allocateString(String(args[0])) : sandboxString(args.length === 0 ? "" : args[0], options.budget, context),
11844
- name: "String",
11845
- properties: {
11846
- raw: createSandboxClosure({
11847
- sandbox: true,
11848
- call: (args, context) => stringRaw(args, options.budget, context),
11849
- name: "raw"
11850
- }),
11851
- fromCharCode: createSandboxClosure({
11852
- sandbox: true,
11853
- call: (args, context) => stringFromCodes(args, String.fromCharCode, options.budget, context),
11854
- name: "fromCharCode"
11855
- }),
11856
- fromCodePoint: createSandboxClosure({
11857
- sandbox: true,
11858
- call: (args, context) => stringFromCodes(args, String.fromCodePoint, options.budget, context),
11859
- name: "fromCodePoint"
11860
- })
11861
- }
11862
- },
11863
- options.budget
11864
- ),
11865
- Number: createPrimitiveConstructor(
11866
- {
11867
- call: (args, context) => sandboxNumber(args.length === 0 ? 0 : args[0], options.budget, context, true),
11868
- name: "Number",
11869
- properties: {
11870
- isFinite: createSandboxClosure({
11871
- guest: true,
11872
- sandbox: true,
11873
- call: ([value]) => typeof value === "number" && Number.isFinite(value),
11874
- name: "isFinite"
11875
- }),
11876
- isNaN: createSandboxClosure({
11877
- guest: true,
11878
- sandbox: true,
11879
- call: ([value]) => typeof value === "number" && Number.isNaN(value),
11880
- name: "isNaN"
11881
- }),
11882
- isInteger: createSandboxClosure({
11883
- guest: true,
11884
- sandbox: true,
11885
- call: ([value]) => typeof value === "number" && Number.isInteger(value),
11886
- name: "isInteger"
11887
- }),
11888
- ...createNumericParsers(options.budget),
11889
- isSafeInteger: createSandboxClosure({
11890
- guest: true,
11891
- sandbox: true,
11892
- call: ([value]) => typeof value === "number" && Number.isSafeInteger(value),
11893
- name: "isSafeInteger"
11894
- }),
11895
- MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
11896
- MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER,
11897
- EPSILON: Number.EPSILON,
11898
- MAX_VALUE: Number.MAX_VALUE,
11899
- MIN_VALUE: Number.MIN_VALUE,
11900
- NaN: Number.NaN,
11901
- NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
11902
- POSITIVE_INFINITY: Number.POSITIVE_INFINITY
11903
- }
11904
- },
11905
- options.budget
11906
- ),
11907
- Boolean: createPrimitiveConstructor(
11908
- {
11909
- call: ([value]) => Boolean(value),
11910
- name: "Boolean"
11911
- },
11912
- options.budget
11913
- )
11914
- };
11664
+ name: `Array#${property}`,
11665
+ call: (args, context) => callArrayMethod(
11666
+ context?.thisValue,
11667
+ property,
11668
+ args,
11669
+ { ...options, context },
11670
+ context?.stack ?? []
11671
+ )
11672
+ });
11673
+ }
11674
+ return void 0;
11915
11675
  }
11916
- async function objectFromSandboxEntries(items, iterator, budget, context) {
11917
- const object = /* @__PURE__ */ Object.create(null);
11918
- let entry;
11919
- let key;
11920
- let value;
11921
- let failure;
11922
- const retained = {};
11923
- budget.setRetainedValues(retained, () => [
11924
- items,
11925
- iterator.retainedValue,
11926
- object,
11927
- entry,
11928
- key,
11929
- value,
11930
- failure
11931
- ]);
11932
- const checkData = createDataCheckpoint(budget, context);
11933
- const closeOnThrow = async (error) => {
11934
- failure = isCapturedException(error) ? error.reason : error;
11935
- try {
11936
- await closeIterator(iterator, true);
11937
- } catch (closeError) {
11938
- if (!isFatalSandboxError(error) && isFatalSandboxError(closeError)) throw closeError;
11939
- }
11940
- throw error;
11941
- };
11676
+ function getArrayIndex(property) {
11677
+ const index = typeof property === "number" ? property : Number(property);
11678
+ if (!Number.isSafeInteger(index) || index < 0 || index >= 2 ** 32 - 1 || String(index) !== String(property)) {
11679
+ return void 0;
11680
+ }
11681
+ return index;
11682
+ }
11683
+ function isArrayMethodName(property) {
11684
+ return typeof property === "string" && arrayMethodNames.has(property);
11685
+ }
11686
+ async function callArrayMethod(receiver, methodName, args, options, stack = []) {
11687
+ if (receiver === null || receiver === void 0)
11688
+ throw new TypeError("Array method requires a receiver.");
11689
+ if (typeof receiver !== "object") {
11690
+ receiver = createSandboxBox(receiver);
11691
+ options.budget.chargeDataUsage(measureSandboxData([receiver]));
11692
+ }
11693
+ const retainedReceiver = {};
11694
+ options.budget.setRetainedValues(retainedReceiver, () => [receiver]);
11942
11695
  try {
11943
- checkData(object, 0, true);
11944
- while (true) {
11945
- try {
11946
- budget.visitNode();
11947
- } catch (error) {
11948
- await closeOnThrow(error);
11949
- }
11950
- const result = await iterator.next();
11951
- if (typeof result !== "object" || result === null) {
11952
- throw new TypeError("Iterator result must be an object.");
11696
+ const value = Array.isArray(receiver) || methodName === "concat" ? receiver : await arrayLikeView(receiver, options);
11697
+ if (isMutatingArrayMethod(methodName)) {
11698
+ assertCollectionMutable(receiver);
11699
+ }
11700
+ if (isCallbackArrayMethod(methodName)) {
11701
+ let callbackState = activeArrayCallbacks.get(receiver);
11702
+ if (callbackState === void 0) {
11703
+ callbackState = { depth: 0, leave: enterRunningState(receiver) };
11704
+ activeArrayCallbacks.set(receiver, callbackState);
11953
11705
  }
11954
- if ((await readIteratorResult(iterator, result, "done")).value) break;
11955
- entry = (await readIteratorResult(iterator, result, "value")).value;
11706
+ callbackState.depth += 1;
11956
11707
  try {
11957
- if (typeof entry !== "object" || entry === null) {
11958
- throw new TypeError("Object.fromEntries requires entry objects.");
11708
+ const result2 = await callArrayMethodUnlocked(value, methodName, args, options, stack);
11709
+ return result2 === value ? receiver : result2;
11710
+ } finally {
11711
+ callbackState.depth -= 1;
11712
+ if (callbackState.depth === 0) {
11713
+ activeArrayCallbacks.delete(receiver);
11714
+ callbackState.leave();
11959
11715
  }
11960
- key = context?.getProperty !== void 0 ? await context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, budget);
11961
- value = context?.getProperty !== void 0 ? await context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, budget);
11962
- const property = await toPropertyKey(key, budget, context);
11963
- const growth = (typeof property === "symbol" ? property.description?.length ?? 0 : property.length) + 1 + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([value]));
11964
- budget.visitNode();
11965
- defineOwnDataProperty(object, property, value);
11966
- entry = key = value = void 0;
11967
- checkData(object, growth);
11968
- } catch (error) {
11969
- await closeOnThrow(error);
11970
11716
  }
11971
11717
  }
11972
- checkData(object, 0, true);
11973
- return allocateProducedSandboxValue(object, budget);
11718
+ const result = await callArrayMethodUnlocked(value, methodName, args, options, stack);
11719
+ return result === value ? receiver : result;
11974
11720
  } finally {
11975
- budget.setRetainedValues(retained, void 0);
11721
+ options.budget.setRetainedValues(retainedReceiver, void 0);
11976
11722
  }
11977
11723
  }
11978
- function assignSandboxValues(target, sources, budget, context) {
11979
- if (target === null || target === void 0) {
11980
- throw new TypeError("Object.assign(target, ...sources) requires a non-null target.");
11981
- }
11982
- if (typeof target !== "object") {
11983
- target = createSandboxBox(target);
11984
- budget.chargeDataUsage(measureSandboxData([target]));
11985
- }
11986
- if (!isGuestHostObject(target)) objectProperties(target, true);
11987
- if (context === void 0) {
11988
- for (const source of sources) {
11989
- if (source === null || source === void 0) continue;
11990
- const properties = isGuestHostObject(source) ? void 0 : reflectionProperties(source);
11991
- const keys = properties === void 0 ? ownEnumerableSandboxKeys(source, true) : [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(source)];
11992
- for (const key of keys) {
11993
- if (!hasOwnSandboxProperty(source, key, true)) continue;
11994
- const value = properties === void 0 ? getSandboxDataProperty(source, key, budget) : Reflect.get(properties, key, source);
11995
- setSandboxProperty(target, key, value, budget);
11996
- }
11997
- }
11998
- return target;
11999
- }
12000
- return (async () => {
12001
- const release = retainValues(budget, () => [target, ...sources]);
12002
- try {
12003
- for (const source of sources) {
12004
- if (source === null || source === void 0) continue;
12005
- const properties = isGuestHostObject(source) ? void 0 : reflectionProperties(source);
12006
- const keys = properties === void 0 ? ownEnumerableSandboxKeys(source, true) : [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(source)];
12007
- for (const key of keys) {
12008
- if (!hasOwnSandboxProperty(source, key, true)) continue;
12009
- const value = await (context?.getProperty !== void 0 ? context.getProperty(source, key) : isGuestHostObject(source) ? getSandboxDataProperty(source, key, budget) : readPropertyDescriptor(Object.getOwnPropertyDescriptor(properties, key), source, context, true));
12010
- await setSandboxProperty(target, key, value, budget, true, context);
12011
- }
12012
- }
12013
- return target;
12014
- } finally {
12015
- release();
12016
- }
12017
- })();
12018
- }
12019
- function reflectionProperties(value) {
12020
- if (value !== null && value !== void 0 && typeof value !== "object")
12021
- value = createSandboxBox(value);
12022
- return objectProperties(value);
12023
- }
12024
- function objectProperties(value, mutable = false) {
12025
- if (isSandboxPromise(value)) return getPromiseProperties(value);
12026
- if (isSandboxMap(value) || isSandboxSet(value)) return getCollectionProperties(value);
12027
- if (isSandboxRegex(value)) return getRegexProperties(value);
12028
- if (isSandboxDate(value)) {
12029
- return value;
12030
- }
12031
- if (isGuestHostObject(value))
12032
- throw new TypeError("Live host object descriptors are not supported.");
12033
- if (isGuestClosure(value)) return materializeFunctionProperties(value);
12034
- if (isSandboxClosure(value)) {
12035
- if (mutable) throw new TypeError("Host function properties are read only.");
12036
- return value.properties ?? /* @__PURE__ */ Object.create(null);
12037
- }
12038
- if (!isAssignableSandboxTarget(value))
12039
- throw new TypeError("Expected a sandbox object or function.");
12040
- return value;
12041
- }
12042
- function exposePropertyDescriptor(descriptor) {
12043
- return "value" in descriptor ? descriptor : {
12044
- get: accessorClosure(descriptor.get),
12045
- set: accessorClosure(descriptor.set),
12046
- enumerable: descriptor.enumerable,
12047
- configurable: descriptor.configurable
12048
- };
12049
- }
12050
- async function propertyDescriptor(input, budget, context) {
12051
- objectProperties(input);
12052
- const descriptor = {};
12053
- const release = retainValues(budget, () => [
12054
- input,
12055
- descriptor.value,
12056
- ...retainedAccessorClosures(descriptor)
12057
- ]);
12058
- try {
12059
- for (const field of [
12060
- "enumerable",
12061
- "configurable",
12062
- "value",
12063
- "writable",
12064
- "get",
12065
- "set"
12066
- ]) {
12067
- if (getSandboxPropertyDescriptor(input, field, budget) === void 0 && !hasOwnSandboxProperty(input, field, false))
12068
- continue;
12069
- const value = await (context?.getProperty !== void 0 ? context.getProperty(input, field) : getSandboxDataProperty(input, field, budget));
12070
- if (field === "get" || field === "set") {
12071
- if (value !== void 0 && !isSandboxClosure(value))
12072
- throw new TypeError("Accessor must be a function or undefined.");
12073
- descriptor[field] = value === void 0 ? void 0 : accessorAdapter(value, field);
12074
- } else if (field === "value") {
12075
- descriptor.value = value;
12076
- } else {
12077
- descriptor[field] = Boolean(value);
12078
- }
12079
- }
12080
- if (("get" in descriptor || "set" in descriptor) && ("value" in descriptor || "writable" in descriptor))
12081
- throw new TypeError("A property cannot be both a data property and an accessor.");
12082
- return descriptor;
12083
- } finally {
12084
- release();
12085
- }
12086
- }
12087
- async function definePropertiesFromObject(target, descriptors2, budget, context) {
12088
- objectProperties(target, true);
12089
- const properties = [];
12090
- const release = retainValues(budget, () => [
12091
- target,
12092
- descriptors2,
12093
- properties,
12094
- ...properties.flatMap(([, descriptor]) => retainedAccessorClosures(descriptor))
12095
- ]);
12096
- try {
12097
- for (const key of ownEnumerableSandboxKeys(descriptors2, true)) {
12098
- if (!hasOwnSandboxProperty(descriptors2, key, true)) continue;
12099
- const descriptor = await (context?.getProperty !== void 0 ? context.getProperty(descriptors2, key) : getSandboxDataProperty(descriptors2, key, budget));
12100
- properties.push([key, await propertyDescriptor(descriptor, budget, context)]);
12101
- }
12102
- for (const [key, descriptor] of properties) defineDataProperty(target, key, descriptor, budget);
12103
- } finally {
12104
- release();
12105
- }
12106
- }
12107
- function defineDataProperty(target, key, descriptor, budget) {
12108
- budget.visitNode();
12109
- if (isFloat32Array(target))
12110
- throw new TypeError("Typed array property descriptors are not supported.");
12111
- const properties = objectProperties(target, true);
12112
- if (Array.isArray(properties)) {
12113
- if (key === "length" && "value" in descriptor)
12114
- budget.allocateArrayLength(Number(descriptor.value));
12115
- else if (typeof key !== "symbol") {
12116
- const index = Number(key);
12117
- if (Number.isInteger(index) && index >= 0 && index < 4294967295 && String(index) === key) {
12118
- budget.allocateArrayLength(index + 1);
11724
+ async function arrayLikeView(receiver, options) {
11725
+ const rawLength = options.context?.getProperty !== void 0 ? await options.context.getProperty(receiver, "length") : getSandboxDataProperty(receiver, "length", options.budget);
11726
+ const number = await sandboxNumber(rawLength, options.budget, options.context);
11727
+ const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
11728
+ const view = new Proxy(
11729
+ /* @__PURE__ */ Object.create(null),
11730
+ {
11731
+ get: (_target, key) => {
11732
+ options.budget.visitNode();
11733
+ if (key === "length") return length;
11734
+ if (typeof key !== "string") return void 0;
11735
+ return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, key) : getSandboxDataProperty(receiver, key, options.budget);
11736
+ },
11737
+ has: (_target, key) => {
11738
+ options.budget.visitNode();
11739
+ if (typeof key === "string" && options.hasProperty !== void 0)
11740
+ return options.hasProperty(receiver, key);
11741
+ for (let current = receiver; current !== null; current = getSandboxPrototype(current, options.budget)) {
11742
+ options.budget.visitNode();
11743
+ if (Object.hasOwn(current, key)) return true;
11744
+ }
11745
+ return false;
11746
+ },
11747
+ set: (_target, key, entry) => {
11748
+ options.budget.visitNode();
11749
+ if (typeof key !== "string") throw new TypeError("Array indices must be string keys.");
11750
+ if (options.setProperty !== void 0) options.setProperty(receiver, key, entry);
11751
+ else if (!Reflect.set(receiver, key, entry))
11752
+ throw new TypeError(`Cannot assign property '${key}'.`);
11753
+ return true;
11754
+ },
11755
+ deleteProperty: (_target, key) => {
11756
+ options.budget.visitNode();
11757
+ if (typeof key === "string" && options.deleteProperty !== void 0)
11758
+ return options.deleteProperty(receiver, key);
11759
+ return Reflect.deleteProperty(receiver, key);
12119
11760
  }
12120
11761
  }
12121
- }
12122
- Object.defineProperty(properties, key, descriptor);
12123
- markDescriptorObject(properties);
12124
- }
12125
- function isAssignableSandboxTarget(value) {
12126
- return typeof value === "object" && value !== null && !isSandboxClosure(value) && !isSandboxGenerator(value) && !isSandboxMap(value) && !isSandboxSet(value) && !isSandboxPromise(value) && !isSandboxRegex(value);
11762
+ );
11763
+ arrayLikeSources.set(view, receiver);
11764
+ return view;
12127
11765
  }
12128
- async function arrayFromSandboxValues(args, budget, context) {
12129
- const [items, mapFn, thisValue] = args;
12130
- if (mapFn !== void 0 && !isSandboxClosure(mapFn)) {
12131
- throw new TypeError("Array.from mapping callback must be a function.");
12132
- }
12133
- if (items === null || items === void 0) {
12134
- throw new TypeError("Array.from requires a non-null input.");
12135
- }
12136
- const read = (property) => context?.getProperty !== void 0 ? context.getProperty(items, property) : getSandboxDataProperty(items, property, budget);
12137
- const iterator = context === void 0 ? getSandboxIterator(items, budget) : await acquireSandboxIterator(items, budget, context);
12138
- const constructor = context?.thisValue;
12139
- let result;
12140
- let currentValue;
12141
- let failure;
12142
- const retained = {};
12143
- budget.setRetainedValues(retained, () => [
12144
- items,
12145
- iterator?.retainedValue,
12146
- mapFn,
12147
- constructor,
12148
- result,
12149
- currentValue,
12150
- failure
12151
- ]);
12152
- const checkData = createDataCheckpoint(budget, context);
12153
- const closeOnThrow = async (error) => {
12154
- failure = isCapturedException(error) ? error.reason : error;
12155
- try {
12156
- if (iterator !== void 0) await closeIterator(iterator, true);
12157
- } catch (closeError) {
12158
- if (!isFatalSandboxError(error) && isFatalSandboxError(closeError)) throw closeError;
12159
- }
12160
- throw error;
12161
- };
12162
- try {
12163
- let length = 0;
12164
- if (iterator === void 0) {
12165
- const number = await sandboxNumber(await read("length"), budget, context);
12166
- length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
12167
- }
12168
- result = isSandboxClosure(constructor) && constructor.construct !== void 0 ? await invokeBuiltinClosure(
12169
- constructor,
12170
- iterator === void 0 ? [length] : [],
12171
- budget,
12172
- context,
12173
- void 0,
12174
- true
12175
- ) : createArrayFromConstructorArgs([length], budget);
12176
- checkData(result, 0, true);
12177
- let index = 0;
12178
- while (iterator !== void 0 || index < length) {
12179
- try {
12180
- budget.visitNode();
12181
- if (iterator !== void 0 && index >= Number.MAX_SAFE_INTEGER)
12182
- throw new TypeError("Array.from input is too long.");
12183
- } catch (error) {
12184
- await closeOnThrow(error);
12185
- }
12186
- if (iterator !== void 0) {
12187
- const next = await iterator.next();
12188
- if (typeof next !== "object" || next === null)
12189
- throw new TypeError("Iterator result must be an object.");
12190
- if ((await readIteratorResult(iterator, next, "done")).value) break;
12191
- currentValue = (await readIteratorResult(iterator, next, "value")).value;
12192
- } else {
12193
- currentValue = await read(index);
12194
- }
11766
+ async function callArrayMethodUnlocked(value, methodName, args, options, stack) {
11767
+ switch (methodName) {
11768
+ case "toLocaleString": {
11769
+ const receiver = arrayLikeSources.get(value) ?? value;
11770
+ const length = value.length;
11771
+ if (localeArrays.has(receiver)) return "";
11772
+ localeArrays.add(receiver);
11773
+ let text = "";
11774
+ const release = retainValues(options.budget, () => [receiver, text, ...args]);
12195
11775
  try {
12196
- if (Array.isArray(result)) budget.allocateArrayLength(index + 1);
12197
- if (mapFn !== void 0)
12198
- currentValue = await invokeBuiltinClosure(
12199
- mapFn,
12200
- [currentValue, index],
12201
- budget,
12202
- context,
12203
- thisValue
12204
- );
12205
- const key = String(index);
12206
- const growth = key.length + 1 + (Array.isArray(result) ? Math.max(0, index + 1 - result.length) : 0) + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([currentValue]));
12207
- budget.visitNode();
12208
- defineOwnDataProperty(objectProperties(result, true), key, currentValue);
12209
- currentValue = void 0;
12210
- checkData(result, growth, isSandboxClosure(result));
12211
- } catch (error) {
12212
- await closeOnThrow(error);
11776
+ for (let index = 0; index < length; index++) {
11777
+ options.budget.visitNode();
11778
+ const element = options.context?.getProperty === void 0 ? getSandboxDataProperty(receiver, String(index), options.budget) : await options.context.getProperty(receiver, String(index));
11779
+ let part = "";
11780
+ if (element !== null && element !== void 0) {
11781
+ const method = options.context?.getProperty === void 0 ? getSandboxDataProperty(element, "toLocaleString", options.budget) : await options.context.getProperty(element, "toLocaleString");
11782
+ if (!isSandboxClosure(method)) throw new TypeError("Element toLocaleString must be callable.");
11783
+ const converted = await invokeBuiltinClosure(method, [args[0], args[1]], options.budget, options.context, element);
11784
+ part = await sandboxString(converted, options.budget, options.context);
11785
+ }
11786
+ text = options.budget.allocateString(text + (index === 0 ? "" : ",") + part);
11787
+ }
11788
+ return text;
11789
+ } finally {
11790
+ release();
11791
+ localeArrays.delete(receiver);
12213
11792
  }
12214
- index += 1;
12215
11793
  }
12216
- setSandboxProperty(result, "length", index, budget);
12217
- checkData(result, 0, true);
12218
- return allocateProducedSandboxValue(result, budget);
12219
- } finally {
12220
- budget.setRetainedValues(retained, void 0);
12221
- }
12222
- }
12223
- function createArrayFromConstructorArgs(args, budget) {
12224
- if (args.length !== 1) {
12225
- return budgetSandboxValue(Reflect.apply(Array, Array, [...args]), budget);
12226
- }
12227
- const [lengthOrValue] = args;
12228
- if (typeof lengthOrValue !== "number") {
12229
- return budgetSandboxValue([lengthOrValue], budget);
12230
- }
12231
- if (!Number.isInteger(lengthOrValue) || lengthOrValue < 0 || lengthOrValue > 4294967295) {
12232
- throw new RangeError("Invalid array length.");
12233
- }
12234
- budget.allocateArrayLength(lengthOrValue);
12235
- const release = budget.provisionDataUsage(lengthOrValue + 1);
12236
- try {
12237
- return new Array(lengthOrValue);
12238
- } finally {
12239
- release();
12240
- }
12241
- }
12242
- async function getOwnEnumerableEntries(value, budget, context) {
12243
- const entries = [];
12244
- const release = retainValues(budget, () => [value, entries]);
12245
- try {
12246
- for (const key of ownEnumerableSandboxKeys(value)) {
12247
- if (!hasOwnSandboxProperty(value, key, true)) continue;
12248
- entries.push([
12249
- key,
12250
- await (context?.getProperty !== void 0 ? context.getProperty(value, key) : getSandboxDataProperty(value, key, budget))
12251
- ]);
11794
+ case "map":
11795
+ return budgetProducedValue(
11796
+ await mapArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
11797
+ options.budget
11798
+ );
11799
+ case "filter":
11800
+ return budgetProducedValue(
11801
+ await filterArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
11802
+ options.budget
11803
+ );
11804
+ case "find":
11805
+ return budgetProducedValue(
11806
+ await findInArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
11807
+ options.budget
11808
+ );
11809
+ case "findIndex":
11810
+ return await findIndexInArray(
11811
+ value,
11812
+ getRequiredCallback(methodName, args[0]),
11813
+ options,
11814
+ stack,
11815
+ args[1]
11816
+ );
11817
+ case "findLast":
11818
+ return budgetProducedValue(
11819
+ await findLastInArray(
11820
+ value,
11821
+ getRequiredCallback(methodName, args[0]),
11822
+ options,
11823
+ stack,
11824
+ args[1]
11825
+ ),
11826
+ options.budget
11827
+ );
11828
+ case "findLastIndex":
11829
+ return await findLastIndexInArray(
11830
+ value,
11831
+ getRequiredCallback(methodName, args[0]),
11832
+ options,
11833
+ stack,
11834
+ args[1]
11835
+ );
11836
+ case "some":
11837
+ return await someInArray(
11838
+ value,
11839
+ getRequiredCallback(methodName, args[0]),
11840
+ options,
11841
+ stack,
11842
+ args[1]
11843
+ );
11844
+ case "every":
11845
+ return await everyInArray(
11846
+ value,
11847
+ getRequiredCallback(methodName, args[0]),
11848
+ options,
11849
+ stack,
11850
+ args[1]
11851
+ );
11852
+ case "reduce":
11853
+ return budgetProducedValue(
11854
+ await reduceArray(
11855
+ value,
11856
+ getRequiredCallback(methodName, args[0]),
11857
+ args.length > 1,
11858
+ args[1],
11859
+ options,
11860
+ stack
11861
+ ),
11862
+ options.budget
11863
+ );
11864
+ case "reduceRight":
11865
+ return budgetProducedValue(
11866
+ await reduceRightArray(
11867
+ value,
11868
+ getRequiredCallback(methodName, args[0]),
11869
+ args.length > 1,
11870
+ args[1],
11871
+ options,
11872
+ stack
11873
+ ),
11874
+ options.budget
11875
+ );
11876
+ case "forEach":
11877
+ await forEachArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]);
11878
+ return void 0;
11879
+ case "flatMap":
11880
+ return budgetProducedValue(
11881
+ await flatMapArray(
11882
+ value,
11883
+ getRequiredCallback(methodName, args[0]),
11884
+ options,
11885
+ stack,
11886
+ args[1]
11887
+ ),
11888
+ options.budget
11889
+ );
11890
+ case "flat":
11891
+ return budgetProducedValue(
11892
+ await flattenArray(
11893
+ value,
11894
+ args[0] === void 0 ? 1 : toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context)),
11895
+ options
11896
+ ),
11897
+ options.budget
11898
+ );
11899
+ case "includes":
11900
+ case "indexOf":
11901
+ case "lastIndexOf": {
11902
+ const length = value.length;
11903
+ if (length === 0) return methodName === "includes" ? false : -1;
11904
+ const reverse = methodName === "lastIndexOf";
11905
+ const start = reverse && args.length < 2 ? length - 1 : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
11906
+ let index = reverse ? Math.min(start < 0 ? length + start : start, length - 1) : start < 0 ? Math.max(length + start, 0) : start;
11907
+ for (; index >= 0 && index < length; index += reverse ? -1 : 1) {
11908
+ options.budget.visitNode();
11909
+ if (methodName !== "includes" && !(index in value)) continue;
11910
+ const entry = await readArrayElement(value, index, options);
11911
+ if (entry === args[0] || methodName === "includes" && Number.isNaN(entry) && Number.isNaN(args[0]))
11912
+ return methodName === "includes" ? true : index;
11913
+ }
11914
+ return methodName === "includes" ? false : -1;
12252
11915
  }
12253
- return entries;
12254
- } finally {
12255
- release();
12256
- }
12257
- }
12258
- function budgetSandboxValue(value, budget) {
12259
- const sandboxValue = deepCopyToSandbox(value);
12260
- return allocateProducedSandboxValue(sandboxValue, budget);
12261
- }
12262
- function stringFromCodes(args, convert, budget, context) {
12263
- let result = "";
12264
- let index = 0;
12265
- const release = retainValues(budget, () => [...args, result]);
12266
- try {
12267
- const output = advance();
12268
- if (typeof output !== "string") return output.finally(release);
12269
- release();
12270
- return output;
12271
- } catch (error) {
12272
- release();
12273
- throw error;
12274
- }
12275
- function advance() {
12276
- while (index < args.length) {
12277
- budget.visitNode();
12278
- const number = sandboxNumber(args[index++], budget, context);
12279
- if (number instanceof Promise) return number.then((value) => {
12280
- result = budget.allocateString(result + convert(value));
12281
- return advance();
12282
- });
12283
- result = budget.allocateString(result + convert(number));
11916
+ case "join": {
11917
+ const length = value.length;
11918
+ const separator = args[0] === void 0 ? "," : await sandboxString(args[0], options.budget, options.context);
11919
+ return joinSandboxArray(
11920
+ arrayLikeSources.get(value) ?? value,
11921
+ length,
11922
+ separator,
11923
+ options.budget,
11924
+ options.context
11925
+ );
12284
11926
  }
12285
- return result;
12286
- }
12287
- }
12288
- function stringRaw(args, budget, context) {
12289
- const [template, ...substitutions] = args;
12290
- if (context?.getProperty !== void 0)
12291
- return (async () => {
12292
- if (template === null || template === void 0)
12293
- throw new TypeError("String.raw requires a template object.");
12294
- const raw2 = await context.getProperty(template, "raw");
12295
- if (raw2 === null || raw2 === void 0)
12296
- throw new TypeError("String.raw requires raw strings.");
12297
- const number = await sandboxNumber(
12298
- await context.getProperty(raw2, "length"),
12299
- budget,
12300
- context
11927
+ case "slice": {
11928
+ const length = value.length;
11929
+ const start = toIntegerOrInfinity(
11930
+ await sandboxNumber(args[0], options.budget, options.context)
12301
11931
  );
12302
- const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
12303
- let result2 = "";
12304
- const retained = {};
12305
- budget.setRetainedValues(retained, () => [raw2, result2]);
11932
+ const end = args[1] === void 0 ? length : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
11933
+ const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
11934
+ const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
11935
+ const count = Math.max(final - first, 0);
11936
+ options.budget.allocateArrayLength(count);
11937
+ const result = new Array(count);
11938
+ const release = retainValues(options.budget, () => [result]);
12306
11939
  try {
12307
- for (let index = 0; index < length; index++) {
12308
- budget.visitNode();
12309
- result2 = budget.allocateString(
12310
- result2 + await sandboxString(await context.getProperty(raw2, index), budget, context)
12311
- );
12312
- if (index + 1 < length && index < substitutions.length)
12313
- result2 = budget.allocateString(
12314
- result2 + await sandboxString(substitutions[index], budget, context)
12315
- );
11940
+ for (let index = 0; index < count; index += 1) {
11941
+ options.budget.visitNode();
11942
+ if (first + index in value)
11943
+ result[index] = await readArrayElement(value, first + index, options);
12316
11944
  }
12317
- return result2;
11945
+ return budgetProducedValue(result, options.budget);
12318
11946
  } finally {
12319
- budget.setRetainedValues(retained, void 0);
12320
- }
12321
- })();
12322
- const raw = getTemplateRawParts(template);
12323
- let result = "";
12324
- for (let index = 0; index < raw.length; index += 1) {
12325
- result += String(raw[index]);
12326
- if (index < raw.length - 1 && index < substitutions.length) {
12327
- result += String(substitutions[index]);
12328
- }
12329
- }
12330
- return budget.allocateString(result);
12331
- }
12332
- function getTemplateRawParts(template) {
12333
- const raw = typeof template === "object" && template !== null ? template.raw : void 0;
12334
- if (typeof template !== "object" || template === null || isSandboxClosure(template) || isSandboxPromise(template) || !Array.isArray(raw)) {
12335
- throw new TypeError("String.raw requires a raw strings array.");
12336
- }
12337
- return raw;
12338
- }
12339
-
12340
- // packages/safe-js/src/interp/classes.ts
12341
- async function evaluateClass(node, context, evaluateNode2, callContext) {
12342
- const scope = context.scope.child();
12343
- if (node.id !== void 0) scope.predeclare(node.id.name, "const");
12344
- const classContext = { ...context, scope, inferredName: void 0 };
12345
- let parent;
12346
- let prototypeParent;
12347
- const fields2 = [];
12348
- const statics = [];
12349
- let constructor;
12350
- const release = retainValues(context.budget, () => [parent, prototypeParent, constructor, ...fields2.map((field) => field.key), ...statics.flatMap((field) => "key" in field ? [field.key] : [])]);
12351
- try {
12352
- if (node.superClass !== void 0) {
12353
- const result = await evaluateNode2(node.superClass, classContext);
12354
- if (result.kind !== "normal") return result;
12355
- parent = result.value;
12356
- if (parent !== null && (!isSandboxClosure(parent) || parent.construct === void 0))
12357
- throw new TypeError("Class extends value is not a constructor or null.");
12358
- prototypeParent = parent === null ? null : await callContext.getProperty(parent, "prototype");
12359
- if (prototypeParent !== null && typeof prototypeParent !== "object")
12360
- throw new TypeError("Class extends value has an invalid prototype.");
12361
- }
12362
- const derived = node.superClass !== void 0;
12363
- constructor = createClassConstructor(node, { ...classContext, inferredName: context.inferredName }, evaluateNode2, fields2);
12364
- const properties = materializeFunctionProperties(constructor);
12365
- const prototype = properties.prototype;
12366
- Object.defineProperty(properties, "prototype", { writable: false });
12367
- if (derived) setSandboxPrototype(prototype, prototypeParent, context.budget);
12368
- if (isSandboxClosure(parent)) setSandboxPrototype(constructor, parent, context.budget);
12369
- const pattern = createPatternContext(classContext, scope, evaluateNode2);
12370
- for (const element of node.body.body) {
12371
- context.budget.visitNode();
12372
- if (element.type === "StaticBlock") {
12373
- statics.push({ element });
12374
- continue;
12375
- }
12376
- if (element.type === "MethodDefinition" && element.kind === "constructor") continue;
12377
- let key;
12378
- if (element.computed) {
12379
- const result = await evaluateNode2(element.key, classContext);
12380
- if (result.kind !== "normal") return result;
12381
- key = await pattern.toPropertyKey(result.value);
12382
- } else {
12383
- key = element.key.type === "Identifier" ? element.key.name : String(element.key.value);
12384
- }
12385
- if (typeof key === "string") context.budget.allocateString(key);
12386
- if (element.type === "PropertyDefinition") {
12387
- (element.static ? statics : fields2).push({ element, key });
12388
- } else {
12389
- const home = element.static ? constructor : prototype;
12390
- const method = createInterpretedClosure(element.value, classContext, evaluateNode2, home);
12391
- const accessor = element.kind === "get" || element.kind === "set" ? element.kind : void 0;
12392
- Object.defineProperty(materializeFunctionProperties(method), "name", {
12393
- value: accessor === void 0 ? propertyFunctionName(key) : `${accessor} ${propertyFunctionName(key)}`
12394
- });
12395
- Object.defineProperty(
12396
- element.static ? properties : prototype,
12397
- key,
12398
- accessor === void 0 ? { value: method, configurable: true, writable: true, enumerable: false } : { [accessor]: accessorAdapter(method, accessor), configurable: true, enumerable: false }
12399
- );
12400
- }
12401
- }
12402
- if (node.id !== void 0) scope.declare(node.id.name, "const", constructor);
12403
- for (const element of statics) await initializeElement(element, constructor, constructor, classContext, evaluateNode2);
12404
- classOrigins.get(constructor).initialized = true;
12405
- if (node.type === "ClassDeclaration") {
12406
- context.scope.declare(node.id.name, "let", constructor);
12407
- return { kind: "normal", hasValue: false, value: void 0 };
12408
- }
12409
- return { kind: "normal", hasValue: true, value: constructor };
12410
- } finally {
12411
- release();
12412
- }
12413
- }
12414
- var classOrigins = /* @__PURE__ */ new WeakMap();
12415
- function createClassConstructor(node, context, evaluateNode2, fields2) {
12416
- const scope = context.scope;
12417
- const classContext = { ...context, inferredName: void 0 };
12418
- const constructorElement = node.body.body.find((element) => element.type === "MethodDefinition" && element.kind === "constructor");
12419
- const derived = node.superClass !== void 0;
12420
- const constructor = createSandboxClosure({
12421
- guest: true,
12422
- sandbox: true,
12423
- name: node.id?.name ?? context.inferredName ?? "",
12424
- length: constructorElement === void 0 ? 0 : getFunctionLength(constructorElement.value.params),
12425
- sourceRange: functionSources.get(node),
12426
- retainedValues: () => [...scope.retainedValues(), ...fields2.map((field) => field.key)],
12427
- call: () => {
12428
- throw new TypeError("Class constructor cannot be invoked without 'new'.");
12429
- },
12430
- construct: async (args, invocation) => {
12431
- const prototype = materializeFunctionProperties(constructor).prototype;
12432
- const newTarget = invocation?.newTarget ?? constructor;
12433
- let thisValue;
12434
- let thisScope;
12435
- let initialized = !derived;
12436
- const initializeFields = async (receiver) => {
12437
- for (const field of fields2) {
12438
- await initializeElement(field, receiver, prototype, classContext, evaluateNode2);
12439
- }
12440
- };
12441
- const superCall = async (argumentsList) => {
12442
- const superConstructor = getSandboxPrototype(constructor, context.budget);
12443
- if (!isSandboxClosure(superConstructor) || superConstructor.construct === void 0)
12444
- throw new TypeError("Super constructor is not a constructor.");
12445
- const receiver = await invocation.invokeClosure(superConstructor, argumentsList, void 0, true, newTarget);
12446
- if (initialized) throw new ReferenceError("Super constructor may only initialize this once.");
12447
- initialized = true;
12448
- thisValue = receiver;
12449
- thisScope?.declare("this", "const", receiver);
12450
- await initializeFields(receiver);
12451
- return receiver;
12452
- };
12453
- if (!derived) {
12454
- thisValue = {};
12455
- const targetPrototype = await invocation.getProperty(newTarget, "prototype");
12456
- if (typeof targetPrototype === "object" && targetPrototype !== null)
12457
- setSandboxPrototype(thisValue, targetPrototype, context.budget);
12458
- }
12459
- if (constructorElement === void 0) {
12460
- if (derived) return superCall(args);
12461
- await initializeFields(thisValue);
12462
- return thisValue;
12463
- }
12464
- const result = await executeClosure(constructorElement.value, args, thisValue, {
12465
- ...classContext,
12466
- compilation: invocation?.compilation ?? context.compilation,
12467
- callStack: [...invocation?.stack ?? context.callStack],
12468
- functionEnvironment: {
12469
- newTarget,
12470
- homeObject: prototype,
12471
- construction: {
12472
- derived,
12473
- superCall,
12474
- initialize: async (functionScope) => {
12475
- thisScope = functionScope;
12476
- if (!derived) await initializeFields(thisValue);
12477
- }
12478
- }
12479
- }
12480
- }, evaluateNode2);
12481
- if (typeof result === "object" && result !== null) return result;
12482
- if (derived && result !== void 0) throw new TypeError("Derived constructors may only return an object or undefined.");
12483
- if (!initialized) throw new ReferenceError("Must call super constructor before returning from derived constructor.");
12484
- return thisValue;
12485
- }
12486
- });
12487
- classOrigins.set(constructor, { node, scope, fields: fields2, initialized: false });
12488
- return constructor;
12489
- }
12490
- async function initializeElement(definition, receiver, homeObject, context, evaluateNode2) {
12491
- const scope = context.scope.child({}, { functionBoundary: true });
12492
- scope.declare("this", "const", receiver);
12493
- const element = definition.element;
12494
- const initializer = element.type === "StaticBlock" ? element.body : element.value;
12495
- let value;
12496
- if (initializer !== void 0) {
12497
- if (element.type === "StaticBlock") hoistVarDeclarations(element.body, scope);
12498
- const result = await evaluateNode2(initializer, { ...context, scope, functionEnvironment: { homeObject }, inferredName: "key" in definition ? propertyFunctionName(definition.key) : void 0 });
12499
- if (result.kind === "error") throw result.error;
12500
- if (result.kind === "throw") throw result.value;
12501
- value = result.hasValue ? result.value : void 0;
12502
- }
12503
- if ("key" in definition) {
12504
- defineDataProperty(receiver, definition.key, { value, configurable: true, writable: true, enumerable: true }, context.budget);
12505
- }
12506
- }
12507
-
12508
- // packages/safe-js/src/interp/resume-target.ts
12509
- function containsResumeTarget(node, targetNodeIds) {
12510
- if (targetNodeIds === void 0 ? node.type === "YieldExpression" : node.nodeId !== void 0 && targetNodeIds.has(node.nodeId)) {
12511
- return true;
12512
- }
12513
- if (node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression") {
12514
- return false;
12515
- }
12516
- for (const value of Object.values(node)) {
12517
- if (Array.isArray(value)) {
12518
- if (value.some((entry) => isParseResult(entry) && containsResumeTarget(entry, targetNodeIds))) {
12519
- return true;
12520
- }
12521
- continue;
12522
- }
12523
- if (isParseResult(value) && containsResumeTarget(value, targetNodeIds)) {
12524
- return true;
12525
- }
12526
- }
12527
- return false;
12528
- }
12529
- function isParseResult(value) {
12530
- return typeof value === "object" && value !== null && typeof value.type === "string" && Object.hasOwn(value, "span");
12531
- }
12532
-
12533
- // packages/safe-js/src/interp/methods/array.ts
12534
- var arrayLikeSources = /* @__PURE__ */ new WeakMap();
12535
- var activeArrayCallbacks = /* @__PURE__ */ new WeakMap();
12536
- var arrayMethodNames = /* @__PURE__ */ new Set([
12537
- "map",
12538
- "filter",
12539
- "find",
12540
- "findIndex",
12541
- "findLast",
12542
- "findLastIndex",
12543
- "some",
12544
- "every",
12545
- "reduce",
12546
- "reduceRight",
12547
- "forEach",
12548
- "flatMap",
12549
- "flat",
12550
- "includes",
12551
- "indexOf",
12552
- "lastIndexOf",
12553
- "join",
12554
- "slice",
12555
- "concat",
12556
- "splice",
12557
- "fill",
12558
- "copyWithin",
12559
- "at",
12560
- "sort",
12561
- "reverse",
12562
- "toSorted",
12563
- "toReversed",
12564
- "toSpliced",
12565
- "with",
12566
- "push",
12567
- "pop",
12568
- "shift",
12569
- "unshift"
12570
- ]);
12571
- function getArrayMember(value, property, options) {
12572
- const index = getArrayIndex(property);
12573
- if (index !== void 0) {
12574
- return Object.hasOwn(value, index) ? value[index] : void 0;
12575
- }
12576
- if (property === "length") {
12577
- return value.length;
12578
- }
12579
- if (Object.hasOwn(value, property)) {
12580
- return value[String(property)];
12581
- }
12582
- if (isArrayMethodName(property)) {
12583
- return createSandboxClosure({
12584
- sandbox: true,
12585
- name: `Array#${property}`,
12586
- call: (args, context) => callArrayMethod(
12587
- context?.thisValue,
12588
- property,
12589
- args,
12590
- { ...options, context },
12591
- context?.stack ?? []
12592
- )
12593
- });
12594
- }
12595
- return void 0;
12596
- }
12597
- function getArrayIndex(property) {
12598
- const index = typeof property === "number" ? property : Number(property);
12599
- if (!Number.isSafeInteger(index) || index < 0 || index >= 2 ** 32 - 1 || String(index) !== String(property)) {
12600
- return void 0;
12601
- }
12602
- return index;
12603
- }
12604
- function isArrayMethodName(property) {
12605
- return typeof property === "string" && arrayMethodNames.has(property);
12606
- }
12607
- async function callArrayMethod(receiver, methodName, args, options, stack = []) {
12608
- if (receiver === null || receiver === void 0)
12609
- throw new TypeError("Array method requires a receiver.");
12610
- if (typeof receiver !== "object") {
12611
- receiver = createSandboxBox(receiver);
12612
- options.budget.chargeDataUsage(measureSandboxData([receiver]));
12613
- }
12614
- const retainedReceiver = {};
12615
- options.budget.setRetainedValues(retainedReceiver, () => [receiver]);
12616
- try {
12617
- const value = Array.isArray(receiver) || methodName === "concat" ? receiver : await arrayLikeView(receiver, options);
12618
- if (isMutatingArrayMethod(methodName)) {
12619
- assertCollectionMutable(receiver);
12620
- }
12621
- if (isCallbackArrayMethod(methodName)) {
12622
- let callbackState = activeArrayCallbacks.get(receiver);
12623
- if (callbackState === void 0) {
12624
- callbackState = { depth: 0, leave: enterRunningState(receiver) };
12625
- activeArrayCallbacks.set(receiver, callbackState);
12626
- }
12627
- callbackState.depth += 1;
12628
- try {
12629
- const result2 = await callArrayMethodUnlocked(value, methodName, args, options, stack);
12630
- return result2 === value ? receiver : result2;
12631
- } finally {
12632
- callbackState.depth -= 1;
12633
- if (callbackState.depth === 0) {
12634
- activeArrayCallbacks.delete(receiver);
12635
- callbackState.leave();
12636
- }
12637
- }
12638
- }
12639
- const result = await callArrayMethodUnlocked(value, methodName, args, options, stack);
12640
- return result === value ? receiver : result;
12641
- } finally {
12642
- options.budget.setRetainedValues(retainedReceiver, void 0);
12643
- }
12644
- }
12645
- async function arrayLikeView(receiver, options) {
12646
- const rawLength = options.context?.getProperty !== void 0 ? await options.context.getProperty(receiver, "length") : getSandboxDataProperty(receiver, "length", options.budget);
12647
- const number = await sandboxNumber(rawLength, options.budget, options.context);
12648
- const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
12649
- const view = new Proxy(
12650
- /* @__PURE__ */ Object.create(null),
12651
- {
12652
- get: (_target, key) => {
12653
- options.budget.visitNode();
12654
- if (key === "length") return length;
12655
- if (typeof key !== "string") return void 0;
12656
- return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, key) : getSandboxDataProperty(receiver, key, options.budget);
12657
- },
12658
- has: (_target, key) => {
12659
- options.budget.visitNode();
12660
- if (typeof key === "string" && options.hasProperty !== void 0)
12661
- return options.hasProperty(receiver, key);
12662
- for (let current = receiver; current !== null; current = getSandboxPrototype(current, options.budget)) {
12663
- options.budget.visitNode();
12664
- if (Object.hasOwn(current, key)) return true;
12665
- }
12666
- return false;
12667
- },
12668
- set: (_target, key, entry) => {
12669
- options.budget.visitNode();
12670
- if (typeof key !== "string") throw new TypeError("Array indices must be string keys.");
12671
- if (options.setProperty !== void 0) options.setProperty(receiver, key, entry);
12672
- else if (!Reflect.set(receiver, key, entry))
12673
- throw new TypeError(`Cannot assign property '${key}'.`);
12674
- return true;
12675
- },
12676
- deleteProperty: (_target, key) => {
12677
- options.budget.visitNode();
12678
- if (typeof key === "string" && options.deleteProperty !== void 0)
12679
- return options.deleteProperty(receiver, key);
12680
- return Reflect.deleteProperty(receiver, key);
12681
- }
12682
- }
12683
- );
12684
- arrayLikeSources.set(view, receiver);
12685
- return view;
12686
- }
12687
- async function callArrayMethodUnlocked(value, methodName, args, options, stack) {
12688
- switch (methodName) {
12689
- case "map":
12690
- return budgetProducedValue(
12691
- await mapArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
12692
- options.budget
12693
- );
12694
- case "filter":
12695
- return budgetProducedValue(
12696
- await filterArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
12697
- options.budget
12698
- );
12699
- case "find":
12700
- return budgetProducedValue(
12701
- await findInArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]),
12702
- options.budget
12703
- );
12704
- case "findIndex":
12705
- return await findIndexInArray(
12706
- value,
12707
- getRequiredCallback(methodName, args[0]),
12708
- options,
12709
- stack,
12710
- args[1]
12711
- );
12712
- case "findLast":
12713
- return budgetProducedValue(
12714
- await findLastInArray(
12715
- value,
12716
- getRequiredCallback(methodName, args[0]),
12717
- options,
12718
- stack,
12719
- args[1]
12720
- ),
12721
- options.budget
12722
- );
12723
- case "findLastIndex":
12724
- return await findLastIndexInArray(
12725
- value,
12726
- getRequiredCallback(methodName, args[0]),
12727
- options,
12728
- stack,
12729
- args[1]
12730
- );
12731
- case "some":
12732
- return await someInArray(
12733
- value,
12734
- getRequiredCallback(methodName, args[0]),
12735
- options,
12736
- stack,
12737
- args[1]
12738
- );
12739
- case "every":
12740
- return await everyInArray(
12741
- value,
12742
- getRequiredCallback(methodName, args[0]),
12743
- options,
12744
- stack,
12745
- args[1]
12746
- );
12747
- case "reduce":
12748
- return budgetProducedValue(
12749
- await reduceArray(
12750
- value,
12751
- getRequiredCallback(methodName, args[0]),
12752
- args.length > 1,
12753
- args[1],
12754
- options,
12755
- stack
12756
- ),
12757
- options.budget
12758
- );
12759
- case "reduceRight":
12760
- return budgetProducedValue(
12761
- await reduceRightArray(
12762
- value,
12763
- getRequiredCallback(methodName, args[0]),
12764
- args.length > 1,
12765
- args[1],
12766
- options,
12767
- stack
12768
- ),
12769
- options.budget
12770
- );
12771
- case "forEach":
12772
- await forEachArray(value, getRequiredCallback(methodName, args[0]), options, stack, args[1]);
12773
- return void 0;
12774
- case "flatMap":
12775
- return budgetProducedValue(
12776
- await flatMapArray(
12777
- value,
12778
- getRequiredCallback(methodName, args[0]),
12779
- options,
12780
- stack,
12781
- args[1]
12782
- ),
12783
- options.budget
12784
- );
12785
- case "flat":
12786
- return budgetProducedValue(
12787
- await flattenArray(
12788
- value,
12789
- args[0] === void 0 ? 1 : toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context)),
12790
- options
12791
- ),
12792
- options.budget
12793
- );
12794
- case "includes":
12795
- case "indexOf":
12796
- case "lastIndexOf": {
12797
- const length = value.length;
12798
- if (length === 0) return methodName === "includes" ? false : -1;
12799
- const reverse = methodName === "lastIndexOf";
12800
- const start = reverse && args.length < 2 ? length - 1 : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
12801
- let index = reverse ? Math.min(start < 0 ? length + start : start, length - 1) : start < 0 ? Math.max(length + start, 0) : start;
12802
- for (; index >= 0 && index < length; index += reverse ? -1 : 1) {
12803
- options.budget.visitNode();
12804
- if (methodName !== "includes" && !(index in value)) continue;
12805
- const entry = await readArrayElement(value, index, options);
12806
- if (entry === args[0] || methodName === "includes" && Number.isNaN(entry) && Number.isNaN(args[0]))
12807
- return methodName === "includes" ? true : index;
12808
- }
12809
- return methodName === "includes" ? false : -1;
12810
- }
12811
- case "join": {
12812
- const length = value.length;
12813
- const separator = args[0] === void 0 ? "," : await sandboxString(args[0], options.budget, options.context);
12814
- return joinSandboxArray(
12815
- arrayLikeSources.get(value) ?? value,
12816
- length,
12817
- separator,
12818
- options.budget,
12819
- options.context
12820
- );
12821
- }
12822
- case "slice": {
12823
- const length = value.length;
12824
- const start = toIntegerOrInfinity(
12825
- await sandboxNumber(args[0], options.budget, options.context)
12826
- );
12827
- const end = args[1] === void 0 ? length : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
12828
- const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
12829
- const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
12830
- const count = Math.max(final - first, 0);
12831
- options.budget.allocateArrayLength(count);
12832
- const result = new Array(count);
12833
- const release = retainValues(options.budget, () => [result]);
12834
- try {
12835
- for (let index = 0; index < count; index += 1) {
12836
- options.budget.visitNode();
12837
- if (first + index in value)
12838
- result[index] = await readArrayElement(value, first + index, options);
12839
- }
12840
- return budgetProducedValue(result, options.budget);
12841
- } finally {
12842
- release();
11947
+ release();
12843
11948
  }
12844
11949
  }
12845
11950
  case "concat": {
@@ -13077,573 +12182,1549 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
13077
12182
  release();
13078
12183
  }
13079
12184
  }
13080
- case "with": {
13081
- const length = value.length;
13082
- const index = toIntegerOrInfinity(
13083
- options.context === void 0 ? args[0] : await sandboxNumber(args[0], options.budget, options.context)
13084
- );
13085
- const actualIndex = index < 0 ? length + index : index;
13086
- if (actualIndex < 0 || actualIndex >= length) {
13087
- throw new RangeError("Invalid index");
13088
- }
13089
- options.budget.allocateArrayLength(length);
13090
- const result = [];
13091
- const release = retainValues(options.budget, () => [result]);
12185
+ case "with": {
12186
+ const length = value.length;
12187
+ const index = toIntegerOrInfinity(
12188
+ options.context === void 0 ? args[0] : await sandboxNumber(args[0], options.budget, options.context)
12189
+ );
12190
+ const actualIndex = index < 0 ? length + index : index;
12191
+ if (actualIndex < 0 || actualIndex >= length) {
12192
+ throw new RangeError("Invalid index");
12193
+ }
12194
+ options.budget.allocateArrayLength(length);
12195
+ const result = [];
12196
+ const release = retainValues(options.budget, () => [result]);
12197
+ try {
12198
+ for (let position = 0; position < length; position += 1) {
12199
+ options.budget.visitNode();
12200
+ result[position] = position === actualIndex ? args[1] : await readArrayElement(value, position, options);
12201
+ }
12202
+ return budgetProducedValue(result, options.budget);
12203
+ } finally {
12204
+ release();
12205
+ }
12206
+ }
12207
+ case "push": {
12208
+ const nextLength = await appendArrayValues(value, args, options);
12209
+ budgetProducedValue(value, options.budget);
12210
+ return nextLength;
12211
+ }
12212
+ case "pop":
12213
+ case "shift": {
12214
+ const length = value.length;
12215
+ if (length === 0) {
12216
+ await writeArrayProperty(value, "length", 0, options);
12217
+ return void 0;
12218
+ }
12219
+ const result = await readArrayElement(value, methodName === "pop" ? length - 1 : 0, options);
12220
+ const retained = {};
12221
+ options.budget.setRetainedValues(retained, () => [result]);
12222
+ try {
12223
+ if (methodName === "shift")
12224
+ for (let index = 1; index < length; index++)
12225
+ await moveArrayElement(value, index, index - 1, options);
12226
+ deleteArrayElement(value, length - 1, options);
12227
+ await writeArrayProperty(value, "length", length - 1, options);
12228
+ return budgetProducedValue(result, options.budget);
12229
+ } finally {
12230
+ options.budget.setRetainedValues(retained, void 0);
12231
+ }
12232
+ }
12233
+ case "unshift": {
12234
+ const nextLength = await prependArrayValues(value, args, options);
12235
+ budgetProducedValue(value, options.budget);
12236
+ return nextLength;
12237
+ }
12238
+ }
12239
+ }
12240
+ async function appendArrayValues(target, values, options) {
12241
+ let length = target.length;
12242
+ if (length + values.length > Number.MAX_SAFE_INTEGER)
12243
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
12244
+ for (const value of values) {
12245
+ await writeArrayProperty(target, length++, value, options);
12246
+ }
12247
+ await writeArrayProperty(target, "length", length, options);
12248
+ return length;
12249
+ }
12250
+ async function prependArrayValues(target, values, options) {
12251
+ const originalLength = target.length;
12252
+ const length = originalLength + values.length;
12253
+ if (length > Number.MAX_SAFE_INTEGER)
12254
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
12255
+ for (let index = values.length === 0 ? -1 : originalLength - 1; index >= 0; index -= 1) {
12256
+ const targetIndex = index + values.length;
12257
+ await moveArrayElement(target, index, targetIndex, options);
12258
+ }
12259
+ for (let index = 0; index < values.length; index += 1) {
12260
+ await writeArrayProperty(target, index, values[index], options);
12261
+ }
12262
+ await writeArrayProperty(target, "length", length, options);
12263
+ return length;
12264
+ }
12265
+ function isCallbackArrayMethod(methodName) {
12266
+ return methodName === "map" || methodName === "filter" || methodName === "find" || methodName === "findIndex" || methodName === "findLast" || methodName === "findLastIndex" || methodName === "some" || methodName === "every" || methodName === "reduce" || methodName === "reduceRight" || methodName === "forEach" || methodName === "flatMap" || methodName === "sort";
12267
+ }
12268
+ function isMutatingArrayMethod(methodName) {
12269
+ return methodName === "splice" || methodName === "fill" || methodName === "copyWithin" || methodName === "sort" || methodName === "reverse" || methodName === "push" || methodName === "pop" || methodName === "shift" || methodName === "unshift";
12270
+ }
12271
+ function getRequiredCallback(methodName, value) {
12272
+ if (!isSandboxClosure(value)) {
12273
+ throw new TypeError(`Array#${methodName} requires a sandbox closure callback.`);
12274
+ }
12275
+ return value;
12276
+ }
12277
+ function readArrayElement(value, index, options) {
12278
+ const receiver = arrayLikeSources.get(value) ?? value;
12279
+ return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, index) : getSandboxDataProperty(receiver, index, options.budget);
12280
+ }
12281
+ function writeArrayProperty(value, key, entry, options) {
12282
+ options.budget.visitNode();
12283
+ const receiver = arrayLikeSources.get(value) ?? value;
12284
+ if (Array.isArray(receiver)) {
12285
+ if (key === "length") options.budget.allocateArrayLength(Number(entry));
12286
+ else if (typeof key === "number") options.budget.allocateArrayLength(key + 1);
12287
+ }
12288
+ if (options.setProperty !== void 0) return options.setProperty(receiver, String(key), entry);
12289
+ if (!Reflect.set(receiver, key, entry))
12290
+ throw new TypeError(`Cannot assign to read only property '${key}'.`);
12291
+ }
12292
+ function deleteArrayElement(value, index, options) {
12293
+ options.budget.visitNode();
12294
+ const receiver = arrayLikeSources.get(value) ?? value;
12295
+ const deleted = options.deleteProperty === void 0 ? Reflect.deleteProperty(receiver, String(index)) : options.deleteProperty(receiver, String(index));
12296
+ if (!deleted) throw new TypeError(`Cannot delete property '${index}'.`);
12297
+ }
12298
+ async function moveArrayElement(value, from, to, options) {
12299
+ options.budget.visitNode();
12300
+ if (from in value)
12301
+ await writeArrayProperty(value, to, await readArrayElement(value, from, options), options);
12302
+ else deleteArrayElement(value, to, options);
12303
+ }
12304
+ async function mapArray(value, callback, options, stack, thisValue) {
12305
+ const length = value.length;
12306
+ options.budget.allocateArrayLength(length);
12307
+ const result = new Array(length);
12308
+ options.budget.setRetainedValues(result, () => [result]);
12309
+ try {
12310
+ for (let index = 0; index < length; index += 1) {
12311
+ options.budget.visitNode();
12312
+ if (!(index in value)) {
12313
+ continue;
12314
+ }
12315
+ result[index] = await callArrayCallback(
12316
+ callback,
12317
+ await readArrayElement(value, index, options),
12318
+ index,
12319
+ value,
12320
+ options,
12321
+ stack,
12322
+ thisValue
12323
+ );
12324
+ }
12325
+ return result;
12326
+ } finally {
12327
+ options.budget.setRetainedValues(result, void 0);
12328
+ }
12329
+ }
12330
+ async function filterArray(value, callback, options, stack, thisValue) {
12331
+ const length = value.length;
12332
+ const result = [];
12333
+ options.budget.setRetainedValues(result, () => [result]);
12334
+ try {
12335
+ for (let index = 0; index < length; index += 1) {
12336
+ options.budget.visitNode();
12337
+ if (!(index in value)) {
12338
+ continue;
12339
+ }
12340
+ const entry = await readArrayElement(value, index, options);
12341
+ if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12342
+ result.push(entry);
12343
+ }
12344
+ }
12345
+ return result;
12346
+ } finally {
12347
+ options.budget.setRetainedValues(result, void 0);
12348
+ }
12349
+ }
12350
+ async function findInArray(value, callback, options, stack, thisValue) {
12351
+ const length = value.length;
12352
+ for (let index = 0; index < length; index += 1) {
12353
+ options.budget.visitNode();
12354
+ const entry = await readArrayElement(value, index, options);
12355
+ if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12356
+ return entry;
12357
+ }
12358
+ }
12359
+ return void 0;
12360
+ }
12361
+ async function findIndexInArray(value, callback, options, stack, thisValue) {
12362
+ const length = value.length;
12363
+ for (let index = 0; index < length; index += 1) {
12364
+ options.budget.visitNode();
12365
+ const entry = await readArrayElement(value, index, options);
12366
+ if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12367
+ return index;
12368
+ }
12369
+ }
12370
+ return -1;
12371
+ }
12372
+ async function findLastInArray(value, callback, options, stack, thisValue) {
12373
+ const length = value.length;
12374
+ for (let index = length - 1; index >= 0; index -= 1) {
12375
+ options.budget.visitNode();
12376
+ const entry = await readArrayElement(value, index, options);
12377
+ if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12378
+ return entry;
12379
+ }
12380
+ }
12381
+ return void 0;
12382
+ }
12383
+ async function findLastIndexInArray(value, callback, options, stack, thisValue) {
12384
+ const length = value.length;
12385
+ for (let index = length - 1; index >= 0; index -= 1) {
12386
+ options.budget.visitNode();
12387
+ const entry = await readArrayElement(value, index, options);
12388
+ if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12389
+ return index;
12390
+ }
12391
+ }
12392
+ return -1;
12393
+ }
12394
+ async function someInArray(value, callback, options, stack, thisValue) {
12395
+ const length = value.length;
12396
+ for (let index = 0; index < length; index += 1) {
12397
+ options.budget.visitNode();
12398
+ if (!(index in value)) {
12399
+ continue;
12400
+ }
12401
+ if (await callArrayCallback(
12402
+ callback,
12403
+ await readArrayElement(value, index, options),
12404
+ index,
12405
+ value,
12406
+ options,
12407
+ stack,
12408
+ thisValue
12409
+ )) {
12410
+ return true;
12411
+ }
12412
+ }
12413
+ return false;
12414
+ }
12415
+ async function everyInArray(value, callback, options, stack, thisValue) {
12416
+ const length = value.length;
12417
+ for (let index = 0; index < length; index += 1) {
12418
+ options.budget.visitNode();
12419
+ if (!(index in value)) {
12420
+ continue;
12421
+ }
12422
+ if (!await callArrayCallback(
12423
+ callback,
12424
+ await readArrayElement(value, index, options),
12425
+ index,
12426
+ value,
12427
+ options,
12428
+ stack,
12429
+ thisValue
12430
+ )) {
12431
+ return false;
12432
+ }
12433
+ }
12434
+ return true;
12435
+ }
12436
+ async function reduceArray(value, callback, hasInitialValue, initialValue, options, stack) {
12437
+ const length = value.length;
12438
+ if (hasInitialValue) {
12439
+ return reduceFromLeft(value, callback, initialValue, 0, length, options, stack);
12440
+ }
12441
+ const start = findNextDefinedIndex(value, 0, 1, length, options.budget);
12442
+ if (start < 0) {
12443
+ throw new TypeError("Reduce of empty array with no initial value.");
12444
+ }
12445
+ return reduceFromLeft(
12446
+ value,
12447
+ callback,
12448
+ await readArrayElement(value, start, options),
12449
+ start + 1,
12450
+ length,
12451
+ options,
12452
+ stack
12453
+ );
12454
+ }
12455
+ async function reduceRightArray(value, callback, hasInitialValue, initialValue, options, stack) {
12456
+ const length = value.length;
12457
+ if (hasInitialValue) {
12458
+ return reduceFromRight(value, callback, initialValue, length - 1, length, options, stack);
12459
+ }
12460
+ const start = findNextDefinedIndex(value, length - 1, -1, length, options.budget);
12461
+ if (start < 0) {
12462
+ throw new TypeError("Reduce of empty array with no initial value.");
12463
+ }
12464
+ return reduceFromRight(
12465
+ value,
12466
+ callback,
12467
+ await readArrayElement(value, start, options),
12468
+ start - 1,
12469
+ length,
12470
+ options,
12471
+ stack
12472
+ );
12473
+ }
12474
+ async function reduceFromLeft(value, callback, accumulator, startIndex, length, options, stack) {
12475
+ let current = accumulator;
12476
+ const retainedAccumulator = {};
12477
+ options.budget.setRetainedValues(retainedAccumulator, () => [current]);
12478
+ try {
12479
+ for (let index = startIndex; index < length; index += 1) {
12480
+ options.budget.visitNode();
12481
+ if (!(index in value)) {
12482
+ continue;
12483
+ }
12484
+ current = await options.callClosure(
12485
+ callback,
12486
+ [
12487
+ current,
12488
+ await readArrayElement(value, index, options),
12489
+ index,
12490
+ arrayLikeSources.get(value) ?? value
12491
+ ],
12492
+ stack
12493
+ );
12494
+ }
12495
+ return current;
12496
+ } finally {
12497
+ options.budget.setRetainedValues(retainedAccumulator, void 0);
12498
+ }
12499
+ }
12500
+ async function reduceFromRight(value, callback, accumulator, startIndex, length, options, stack) {
12501
+ let current = accumulator;
12502
+ const retainedAccumulator = {};
12503
+ options.budget.setRetainedValues(retainedAccumulator, () => [current]);
12504
+ try {
12505
+ for (let index = Math.min(startIndex, length - 1); index >= 0; index -= 1) {
12506
+ options.budget.visitNode();
12507
+ if (!(index in value)) {
12508
+ continue;
12509
+ }
12510
+ current = await options.callClosure(
12511
+ callback,
12512
+ [
12513
+ current,
12514
+ await readArrayElement(value, index, options),
12515
+ index,
12516
+ arrayLikeSources.get(value) ?? value
12517
+ ],
12518
+ stack
12519
+ );
12520
+ }
12521
+ return current;
12522
+ } finally {
12523
+ options.budget.setRetainedValues(retainedAccumulator, void 0);
12524
+ }
12525
+ }
12526
+ async function forEachArray(value, callback, options, stack, thisValue) {
12527
+ const length = value.length;
12528
+ for (let index = 0; index < length; index += 1) {
12529
+ options.budget.visitNode();
12530
+ if (!(index in value)) {
12531
+ continue;
12532
+ }
12533
+ await callArrayCallback(
12534
+ callback,
12535
+ await readArrayElement(value, index, options),
12536
+ index,
12537
+ value,
12538
+ options,
12539
+ stack,
12540
+ thisValue
12541
+ );
12542
+ }
12543
+ }
12544
+ async function flatMapArray(value, callback, options, stack, thisValue) {
12545
+ const length = value.length;
12546
+ const result = [];
12547
+ options.budget.setRetainedValues(result, () => [result]);
12548
+ try {
12549
+ for (let index = 0; index < length; index += 1) {
12550
+ options.budget.visitNode();
12551
+ if (!(index in value)) {
12552
+ continue;
12553
+ }
12554
+ const mapped = await callArrayCallback(
12555
+ callback,
12556
+ await readArrayElement(value, index, options),
12557
+ index,
12558
+ value,
12559
+ options,
12560
+ stack,
12561
+ thisValue
12562
+ );
12563
+ if (Array.isArray(mapped)) {
12564
+ for (let mappedIndex = 0; mappedIndex < mapped.length; mappedIndex += 1) {
12565
+ options.budget.visitNode();
12566
+ if (!(mappedIndex in mapped)) {
12567
+ continue;
12568
+ }
12569
+ result.push(await readArrayElement(mapped, mappedIndex, options));
12570
+ options.budget.allocateArrayLength(result.length);
12571
+ }
12572
+ continue;
12573
+ }
12574
+ result.push(mapped);
12575
+ options.budget.allocateArrayLength(result.length);
12576
+ }
12577
+ return result;
12578
+ } finally {
12579
+ options.budget.setRetainedValues(result, void 0);
12580
+ }
12581
+ }
12582
+ async function flattenArray(value, depth, options) {
12583
+ const result = [];
12584
+ const retained = {};
12585
+ options.budget.setRetainedValues(retained, () => [result]);
12586
+ try {
12587
+ await appendFlattenedEntries(value, depth, result, options);
12588
+ return result;
12589
+ } finally {
12590
+ options.budget.setRetainedValues(retained, void 0);
12591
+ }
12592
+ }
12593
+ async function appendFlattenedEntries(value, depth, result, options) {
12594
+ const length = value.length;
12595
+ for (let index = 0; index < length; index += 1) {
12596
+ options.budget.visitNode();
12597
+ if (!(index in value)) {
12598
+ continue;
12599
+ }
12600
+ const entry = await readArrayElement(value, index, options);
12601
+ if (depth > 0 && Array.isArray(entry)) {
12602
+ await appendFlattenedEntries(entry, depth - 1, result, options);
12603
+ continue;
12604
+ }
12605
+ result.push(entry);
12606
+ options.budget.allocateArrayLength(result.length);
12607
+ }
12608
+ }
12609
+ async function sortArray(value, comparator, options, stack) {
12610
+ const length = value.length;
12611
+ const definedValues = [];
12612
+ let undefinedCount = 0;
12613
+ let currentEntry;
12614
+ options.budget.setRetainedValues(definedValues, () => [definedValues, currentEntry]);
12615
+ try {
12616
+ for (let index = 0; index < length; index += 1) {
12617
+ options.budget.visitNode();
12618
+ if (!(index in value)) {
12619
+ continue;
12620
+ }
12621
+ const entry = await readArrayElement(value, index, options);
12622
+ if (entry === void 0) {
12623
+ undefinedCount += 1;
12624
+ continue;
12625
+ }
12626
+ definedValues.push(entry);
12627
+ }
12628
+ for (let index = 1; index < definedValues.length; index += 1) {
12629
+ currentEntry = definedValues[index];
12630
+ let cursor = index - 1;
12631
+ while (cursor >= 0 && await compareEntries(definedValues[cursor], currentEntry, comparator, options, stack) > 0) {
12632
+ definedValues[cursor + 1] = definedValues[cursor];
12633
+ cursor -= 1;
12634
+ }
12635
+ definedValues[cursor + 1] = currentEntry;
12636
+ }
12637
+ currentEntry = void 0;
12638
+ for (let index = 0; index < definedValues.length; index += 1) {
12639
+ await writeArrayProperty(value, index, definedValues[index], options);
12640
+ }
12641
+ for (let index = 0; index < undefinedCount; index += 1) {
12642
+ await writeArrayProperty(value, definedValues.length + index, void 0, options);
12643
+ }
12644
+ for (let index = definedValues.length + undefinedCount; index < length; index += 1) {
12645
+ options.budget.visitNode();
12646
+ deleteArrayElement(value, index, options);
12647
+ }
12648
+ } finally {
12649
+ options.budget.setRetainedValues(definedValues, void 0);
12650
+ }
12651
+ }
12652
+ async function compareEntries(left, right, comparator, options, stack) {
12653
+ options.budget.visitNode();
12654
+ if (comparator === void 0) {
12655
+ const leftString = await sandboxString(left, options.budget, options.context);
12656
+ const release = retainValues(options.budget, () => [leftString]);
12657
+ try {
12658
+ const rightString = await sandboxString(right, options.budget, options.context);
12659
+ return leftString < rightString ? -1 : leftString > rightString ? 1 : 0;
12660
+ } finally {
12661
+ release();
12662
+ }
12663
+ }
12664
+ const result = await sandboxNumber(
12665
+ await options.callClosure(comparator, [left, right], stack),
12666
+ options.budget,
12667
+ options.context
12668
+ );
12669
+ return Number.isNaN(result) ? 0 : result;
12670
+ }
12671
+ async function callArrayCallback(callback, value, index, array2, options, stack, thisValue) {
12672
+ return options.callClosure(
12673
+ callback,
12674
+ [value, index, arrayLikeSources.get(array2) ?? array2],
12675
+ stack,
12676
+ thisValue
12677
+ );
12678
+ }
12679
+ function findNextDefinedIndex(value, startIndex, direction, length, budget) {
12680
+ for (let index = startIndex; direction > 0 ? index < length : index >= 0; index += direction) {
12681
+ budget.visitNode();
12682
+ if (index in value) {
12683
+ return index;
12684
+ }
12685
+ }
12686
+ return -1;
12687
+ }
12688
+ function budgetProducedValue(value, budget) {
12689
+ if (typeof value === "object" && value !== null) value = arrayLikeSources.get(value) ?? value;
12690
+ allocateProducedValue(value, budget, /* @__PURE__ */ new WeakSet());
12691
+ return value;
12692
+ }
12693
+ function allocateProducedValue(value, budget, seen) {
12694
+ if (typeof value === "string") {
12695
+ budget.allocateString(value);
12696
+ return;
12697
+ }
12698
+ if (Array.isArray(value)) {
12699
+ budget.allocateArrayLength(value.length);
12700
+ if (seen.has(value)) {
12701
+ return;
12702
+ }
12703
+ seen.add(value);
12704
+ for (const entry of value) {
12705
+ allocateProducedValue(entry, budget, seen);
12706
+ }
12707
+ return;
12708
+ }
12709
+ if (isSandboxMap(value)) {
12710
+ budget.allocateCollectionEntries(value.entries.size);
12711
+ if (seen.has(value)) {
12712
+ return;
12713
+ }
12714
+ seen.add(value);
12715
+ for (const [key, entry] of value.entries) {
12716
+ allocateProducedValue(key, budget, seen);
12717
+ allocateProducedValue(entry, budget, seen);
12718
+ }
12719
+ return;
12720
+ }
12721
+ if (isSandboxSet(value)) {
12722
+ budget.allocateCollectionEntries(value.values.size);
12723
+ if (seen.has(value)) {
12724
+ return;
12725
+ }
12726
+ seen.add(value);
12727
+ for (const entry of value.values) {
12728
+ allocateProducedValue(entry, budget, seen);
12729
+ }
12730
+ return;
12731
+ }
12732
+ if (typeof value !== "object" || value === null || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value)) {
12733
+ return;
12734
+ }
12735
+ if (seen.has(value)) {
12736
+ return;
12737
+ }
12738
+ seen.add(value);
12739
+ for (const entry of Object.values(value)) {
12740
+ allocateProducedValue(entry, budget, seen);
12741
+ }
12742
+ }
12743
+ function toIntegerOrInfinity(value) {
12744
+ const number = Number(value);
12745
+ if (Number.isNaN(number) || Object.is(number, 0) || Object.is(number, -0)) {
12746
+ return 0;
12747
+ }
12748
+ if (!Number.isFinite(number)) {
12749
+ return number;
12750
+ }
12751
+ return Math.trunc(number);
12752
+ }
12753
+
12754
+ // packages/safe-js/src/interp/globals/object-array.ts
12755
+ function createObjectArrayGlobals(options) {
12756
+ return {
12757
+ Object: createObjectGlobal(
12758
+ {
12759
+ keys: createSandboxClosure({
12760
+ sandbox: true,
12761
+ call: ([value]) => budgetSandboxValue(ownEnumerableSandboxKeys(value), options.budget),
12762
+ name: "keys"
12763
+ }),
12764
+ values: createSandboxClosure({
12765
+ sandbox: true,
12766
+ call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(
12767
+ ownEnumerableSandboxEntries(value).map(([, entry]) => entry),
12768
+ options.budget
12769
+ ) : getOwnEnumerableEntries(value, options.budget, context).then(
12770
+ (entries) => allocateProducedSandboxValue(
12771
+ entries.map(([, entry]) => entry),
12772
+ options.budget
12773
+ )
12774
+ ),
12775
+ name: "values"
12776
+ }),
12777
+ entries: createSandboxClosure({
12778
+ sandbox: true,
12779
+ call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(ownEnumerableSandboxEntries(value), options.budget) : getOwnEnumerableEntries(value, options.budget, context).then(
12780
+ (entries) => allocateProducedSandboxValue(entries, options.budget)
12781
+ ),
12782
+ name: "entries"
12783
+ }),
12784
+ hasOwn: createSandboxClosure({
12785
+ sandbox: true,
12786
+ call: ([value, key], context) => {
12787
+ if (value === null || value === void 0)
12788
+ throw new TypeError("Cannot convert undefined or null to object.");
12789
+ const name = toPropertyKey(key, options.budget, context);
12790
+ return typeof name === "string" || typeof name === "symbol" ? hasOwnSandboxProperty(value, name, false) : name.then((property) => hasOwnSandboxProperty(value, property, false));
12791
+ },
12792
+ name: "hasOwn"
12793
+ }),
12794
+ getOwnPropertyDescriptor: createSandboxClosure({
12795
+ sandbox: true,
12796
+ call: async ([value, key], context) => {
12797
+ const descriptor = Object.getOwnPropertyDescriptor(
12798
+ reflectionProperties(value),
12799
+ await toPropertyKey(key, options.budget, context)
12800
+ );
12801
+ if (descriptor === void 0) return void 0;
12802
+ return allocateProducedSandboxValue(
12803
+ exposePropertyDescriptor(descriptor),
12804
+ options.budget
12805
+ );
12806
+ },
12807
+ name: "getOwnPropertyDescriptor"
12808
+ }),
12809
+ getOwnPropertyDescriptors: createSandboxClosure({
12810
+ sandbox: true,
12811
+ call: ([value]) => {
12812
+ const descriptors2 = /* @__PURE__ */ Object.create(null);
12813
+ const properties = reflectionProperties(value);
12814
+ for (const key of [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(value)])
12815
+ defineOwnDataProperty(descriptors2, key, exposePropertyDescriptor(Object.getOwnPropertyDescriptor(properties, key)));
12816
+ return allocateProducedSandboxValue(descriptors2, options.budget);
12817
+ },
12818
+ name: "getOwnPropertyDescriptors"
12819
+ }),
12820
+ getOwnPropertyNames: createSandboxClosure({
12821
+ sandbox: true,
12822
+ call: ([value]) => budgetSandboxValue(Object.getOwnPropertyNames(reflectionProperties(value)), options.budget),
12823
+ name: "getOwnPropertyNames"
12824
+ }),
12825
+ getOwnPropertySymbols: createSandboxClosure({
12826
+ sandbox: true,
12827
+ call: ([value]) => allocateProducedSandboxValue(ownSandboxSymbolKeys(value), options.budget),
12828
+ name: "getOwnPropertySymbols"
12829
+ }),
12830
+ defineProperty: createSandboxClosure({
12831
+ sandbox: true,
12832
+ call: async ([value, key, descriptor], context) => {
12833
+ objectProperties(value, true);
12834
+ const property = await toPropertyKey(key, options.budget, context);
12835
+ defineDataProperty(
12836
+ value,
12837
+ property,
12838
+ await propertyDescriptor(descriptor, options.budget, context),
12839
+ options.budget
12840
+ );
12841
+ return value;
12842
+ },
12843
+ name: "defineProperty"
12844
+ }),
12845
+ defineProperties: createSandboxClosure({
12846
+ sandbox: true,
12847
+ call: async ([value, descriptors2], context) => {
12848
+ await definePropertiesFromObject(value, descriptors2, options.budget, context);
12849
+ return value;
12850
+ },
12851
+ name: "defineProperties"
12852
+ }),
12853
+ getPrototypeOf: createSandboxClosure({
12854
+ sandbox: true,
12855
+ call: ([value]) => {
12856
+ if (isSandboxMap(value) || isSandboxSet(value))
12857
+ return getSandboxPrototype(value, options.budget);
12858
+ if (value !== null && value !== void 0 && typeof value !== "object")
12859
+ value = createSandboxBox(value);
12860
+ objectProperties(value);
12861
+ return getSandboxPrototype(value, options.budget);
12862
+ },
12863
+ name: "getPrototypeOf"
12864
+ }),
12865
+ setPrototypeOf: createSandboxClosure({
12866
+ sandbox: true,
12867
+ call: ([value, prototype]) => {
12868
+ if (value === null || value === void 0)
12869
+ throw new TypeError("Cannot set the prototype of null or undefined.");
12870
+ if (prototype !== null && typeof prototype !== "object")
12871
+ throw new TypeError("Prototype must be an object or null.");
12872
+ if (typeof value !== "object") return value;
12873
+ objectProperties(value, true);
12874
+ if (prototype !== null) objectProperties(prototype);
12875
+ setSandboxPrototype(value, prototype, options.budget);
12876
+ return value;
12877
+ },
12878
+ name: "setPrototypeOf"
12879
+ }),
12880
+ create: createSandboxClosure({
12881
+ sandbox: true,
12882
+ call: async ([prototype, descriptors2], context) => {
12883
+ if (prototype !== null) objectProperties(prototype);
12884
+ const value = /* @__PURE__ */ Object.create(null);
12885
+ setSandboxPrototype(value, prototype, options.budget);
12886
+ if (descriptors2 !== void 0) {
12887
+ await definePropertiesFromObject(value, descriptors2, options.budget, context);
12888
+ }
12889
+ return allocateProducedSandboxValue(value, options.budget);
12890
+ },
12891
+ name: "create"
12892
+ }),
12893
+ is: createSandboxClosure({
12894
+ sandbox: true,
12895
+ call: ([left, right]) => Reflect.apply(Object.is, Object, [left, right]),
12896
+ name: "is"
12897
+ }),
12898
+ fromEntries: createSandboxClosure({
12899
+ sandbox: true,
12900
+ call: ([value], context) => {
12901
+ if (context !== void 0) return acquireSandboxIterator(value, options.budget, context).then((iterator2) => {
12902
+ if (iterator2 === void 0) throw new TypeError("Object.fromEntries requires an iterable.");
12903
+ return objectFromSandboxEntries(value, iterator2, options.budget, context);
12904
+ });
12905
+ const iterator = getSandboxIterator(value, options.budget, context);
12906
+ if (iterator === void 0) {
12907
+ throw new TypeError("Object.fromEntries requires an iterable.");
12908
+ }
12909
+ if (context === void 0 && !iterator.generator && !iterator.asynchronous) {
12910
+ return allocateProducedSandboxValue(
12911
+ Object.setPrototypeOf(
12912
+ Reflect.apply(Object.fromEntries, Object, [
12913
+ { [Symbol.iterator]: () => iterator }
12914
+ ]),
12915
+ null
12916
+ ),
12917
+ options.budget
12918
+ );
12919
+ }
12920
+ return objectFromSandboxEntries(value, iterator, options.budget, context);
12921
+ },
12922
+ name: "fromEntries"
12923
+ }),
12924
+ preventExtensions: createSandboxClosure({
12925
+ sandbox: true,
12926
+ call: ([value]) => {
12927
+ if (isGuestHostObject(value))
12928
+ throw new TypeError("Live host objects cannot be made non-extensible.");
12929
+ Object.preventExtensions(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
12930
+ return value;
12931
+ },
12932
+ name: "preventExtensions"
12933
+ }),
12934
+ isExtensible: createSandboxClosure({
12935
+ sandbox: true,
12936
+ call: ([value]) => Object.isExtensible(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
12937
+ name: "isExtensible"
12938
+ }),
12939
+ seal: createSandboxClosure({
12940
+ sandbox: true,
12941
+ call: ([value]) => {
12942
+ if (isGuestHostObject(value))
12943
+ throw new TypeError("Live host objects cannot be sealed.");
12944
+ Object.seal(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
12945
+ return value;
12946
+ },
12947
+ name: "seal"
12948
+ }),
12949
+ isSealed: createSandboxClosure({
12950
+ sandbox: true,
12951
+ call: ([value]) => Object.isSealed(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
12952
+ name: "isSealed"
12953
+ }),
12954
+ freeze: createSandboxClosure({
12955
+ sandbox: true,
12956
+ call: ([value]) => {
12957
+ if (isGuestHostObject(value))
12958
+ throw new TypeError("Live host objects cannot be frozen.");
12959
+ if (typeof value === "object" && value !== null) {
12960
+ Object.freeze(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value);
12961
+ }
12962
+ return value;
12963
+ },
12964
+ name: "freeze"
12965
+ }),
12966
+ isFrozen: createSandboxClosure({
12967
+ sandbox: true,
12968
+ call: ([value]) => Object.isFrozen(isSandboxPromise(value) ? getPromiseProperties(value) : isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : isSandboxMap(value) || isSandboxSet(value) ? getCollectionProperties(value) : value),
12969
+ name: "isFrozen"
12970
+ }),
12971
+ assign: createSandboxClosure({
12972
+ sandbox: true,
12973
+ call: ([target, ...sources], context) => assignSandboxValues(target, sources, options.budget, context),
12974
+ name: "assign"
12975
+ })
12976
+ },
12977
+ options.budget
12978
+ ),
12979
+ Array: createArrayGlobal(options.budget),
12980
+ String: createPrimitiveConstructor(
12981
+ {
12982
+ call: (args, context) => typeof args[0] === "symbol" ? options.budget.allocateString(String(args[0])) : sandboxString(args.length === 0 ? "" : args[0], options.budget, context),
12983
+ name: "String",
12984
+ properties: {
12985
+ raw: createSandboxClosure({
12986
+ sandbox: true,
12987
+ call: (args, context) => stringRaw(args, options.budget, context),
12988
+ name: "raw"
12989
+ }),
12990
+ fromCharCode: createSandboxClosure({
12991
+ sandbox: true,
12992
+ call: (args, context) => stringFromCodes(args, String.fromCharCode, options.budget, context),
12993
+ name: "fromCharCode"
12994
+ }),
12995
+ fromCodePoint: createSandboxClosure({
12996
+ sandbox: true,
12997
+ call: (args, context) => stringFromCodes(args, String.fromCodePoint, options.budget, context),
12998
+ name: "fromCodePoint"
12999
+ })
13000
+ }
13001
+ },
13002
+ options.budget
13003
+ ),
13004
+ Number: createPrimitiveConstructor(
13005
+ {
13006
+ call: (args, context) => sandboxNumber(args.length === 0 ? 0 : args[0], options.budget, context, true),
13007
+ name: "Number",
13008
+ properties: {
13009
+ isFinite: createSandboxClosure({
13010
+ guest: true,
13011
+ sandbox: true,
13012
+ call: ([value]) => typeof value === "number" && Number.isFinite(value),
13013
+ name: "isFinite"
13014
+ }),
13015
+ isNaN: createSandboxClosure({
13016
+ guest: true,
13017
+ sandbox: true,
13018
+ call: ([value]) => typeof value === "number" && Number.isNaN(value),
13019
+ name: "isNaN"
13020
+ }),
13021
+ isInteger: createSandboxClosure({
13022
+ guest: true,
13023
+ sandbox: true,
13024
+ call: ([value]) => typeof value === "number" && Number.isInteger(value),
13025
+ name: "isInteger"
13026
+ }),
13027
+ ...createNumericParsers(options.budget),
13028
+ isSafeInteger: createSandboxClosure({
13029
+ guest: true,
13030
+ sandbox: true,
13031
+ call: ([value]) => typeof value === "number" && Number.isSafeInteger(value),
13032
+ name: "isSafeInteger"
13033
+ }),
13034
+ MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
13035
+ MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER,
13036
+ EPSILON: Number.EPSILON,
13037
+ MAX_VALUE: Number.MAX_VALUE,
13038
+ MIN_VALUE: Number.MIN_VALUE,
13039
+ NaN: Number.NaN,
13040
+ NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
13041
+ POSITIVE_INFINITY: Number.POSITIVE_INFINITY
13042
+ }
13043
+ },
13044
+ options.budget
13045
+ ),
13046
+ Boolean: createPrimitiveConstructor(
13047
+ {
13048
+ call: ([value]) => Boolean(value),
13049
+ name: "Boolean"
13050
+ },
13051
+ options.budget
13052
+ )
13053
+ };
13054
+ }
13055
+ function createArrayGlobal(budget) {
13056
+ const prototype = [];
13057
+ const objectToString = Object.getOwnPropertyDescriptor(getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget), "toString").value;
13058
+ const constructor = createSandboxClosure({
13059
+ guest: true,
13060
+ sandbox: true,
13061
+ call: (args) => createArrayFromConstructorArgs(args, budget),
13062
+ construct: (args) => createArrayFromConstructorArgs(args, budget),
13063
+ name: "Array",
13064
+ length: 1
13065
+ });
13066
+ const properties = materializeFunctionProperties(constructor);
13067
+ Object.defineProperty(properties, "prototype", { value: prototype, writable: false });
13068
+ Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
13069
+ for (const name of arrayMethodNames) {
13070
+ const method = createSandboxClosure({
13071
+ guest: true,
13072
+ sandbox: true,
13073
+ name,
13074
+ length: Object.getOwnPropertyDescriptor(Array.prototype, name).value.length,
13075
+ call: (args, context) => callArrayMethod(context?.thisValue, name, args, {
13076
+ budget,
13077
+ context,
13078
+ hasProperty: (value, key) => hasOwnSandboxProperty(value, key, false) || getSandboxPropertyDescriptor(value, key, budget) !== void 0,
13079
+ deleteProperty: deleteSandboxProperty,
13080
+ setProperty: (value, key, entry) => setSandboxProperty(value, key, entry, budget, true, context),
13081
+ callClosure: (closure, values, _stack, receiver) => invokeBuiltinClosure(closure, values, budget, context, receiver)
13082
+ }, context?.stack ?? [])
13083
+ });
13084
+ Object.defineProperty(prototype, name, { value: method, writable: true, configurable: true });
13085
+ }
13086
+ Object.defineProperty(prototype, "toString", {
13087
+ value: createSandboxClosure({
13088
+ guest: true,
13089
+ sandbox: true,
13090
+ name: "toString",
13091
+ length: 0,
13092
+ call: async (_args, context) => {
13093
+ const receiver = context?.thisValue;
13094
+ if (receiver === null || receiver === void 0) throw new TypeError("Array toString requires a receiver.");
13095
+ const object = typeof receiver === "object" ? receiver : createSandboxBox(receiver);
13096
+ const method = context?.getProperty === void 0 ? getSandboxDataProperty(object, "join", budget) : await context.getProperty(object, "join");
13097
+ return invokeBuiltinClosure(isSandboxClosure(method) ? method : objectToString, [], budget, context, object);
13098
+ }
13099
+ }),
13100
+ writable: true,
13101
+ configurable: true
13102
+ });
13103
+ const statics = {
13104
+ isArray: createSandboxClosure({ sandbox: true, name: "isArray", call: ([value]) => Array.isArray(value) }),
13105
+ from: createSandboxClosure({ sandbox: true, name: "from", call: (args, context) => arrayFromSandboxValues(args, budget, context) }),
13106
+ of: createSandboxClosure({ sandbox: true, name: "of", call: (args) => budgetSandboxValue(Reflect.apply(Array.of, Array, [...args]), budget) })
13107
+ };
13108
+ for (const [name, value] of Object.entries(statics))
13109
+ Object.defineProperty(properties, name, { value, writable: true, configurable: true });
13110
+ installArrayPrototype(budget, prototype, constructor);
13111
+ return constructor;
13112
+ }
13113
+ async function objectFromSandboxEntries(items, iterator, budget, context) {
13114
+ const object = /* @__PURE__ */ Object.create(null);
13115
+ let entry;
13116
+ let key;
13117
+ let value;
13118
+ let failure;
13119
+ const retained = {};
13120
+ budget.setRetainedValues(retained, () => [
13121
+ items,
13122
+ iterator.retainedValue,
13123
+ object,
13124
+ entry,
13125
+ key,
13126
+ value,
13127
+ failure
13128
+ ]);
13129
+ const checkData = createDataCheckpoint(budget, context);
13130
+ const closeOnThrow = async (error) => {
13131
+ failure = isCapturedException(error) ? error.reason : error;
13132
+ try {
13133
+ await closeIterator(iterator, true);
13134
+ } catch (closeError) {
13135
+ if (!isFatalSandboxError(error) && isFatalSandboxError(closeError)) throw closeError;
13136
+ }
13137
+ throw error;
13138
+ };
13139
+ try {
13140
+ checkData(object, 0, true);
13141
+ while (true) {
13092
13142
  try {
13093
- for (let position = 0; position < length; position += 1) {
13094
- options.budget.visitNode();
13095
- result[position] = position === actualIndex ? args[1] : await readArrayElement(value, position, options);
13096
- }
13097
- return budgetProducedValue(result, options.budget);
13098
- } finally {
13099
- release();
13143
+ budget.visitNode();
13144
+ } catch (error) {
13145
+ await closeOnThrow(error);
13100
13146
  }
13101
- }
13102
- case "push": {
13103
- const nextLength = await appendArrayValues(value, args, options);
13104
- budgetProducedValue(value, options.budget);
13105
- return nextLength;
13106
- }
13107
- case "pop":
13108
- case "shift": {
13109
- const length = value.length;
13110
- if (length === 0) {
13111
- await writeArrayProperty(value, "length", 0, options);
13112
- return void 0;
13147
+ const result = await iterator.next();
13148
+ if (typeof result !== "object" || result === null) {
13149
+ throw new TypeError("Iterator result must be an object.");
13113
13150
  }
13114
- const result = await readArrayElement(value, methodName === "pop" ? length - 1 : 0, options);
13115
- const retained = {};
13116
- options.budget.setRetainedValues(retained, () => [result]);
13151
+ if ((await readIteratorResult(iterator, result, "done")).value) break;
13152
+ entry = (await readIteratorResult(iterator, result, "value")).value;
13117
13153
  try {
13118
- if (methodName === "shift")
13119
- for (let index = 1; index < length; index++)
13120
- await moveArrayElement(value, index, index - 1, options);
13121
- deleteArrayElement(value, length - 1, options);
13122
- await writeArrayProperty(value, "length", length - 1, options);
13123
- return budgetProducedValue(result, options.budget);
13124
- } finally {
13125
- options.budget.setRetainedValues(retained, void 0);
13154
+ if (typeof entry !== "object" || entry === null) {
13155
+ throw new TypeError("Object.fromEntries requires entry objects.");
13156
+ }
13157
+ key = context?.getProperty !== void 0 ? await context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, budget);
13158
+ value = context?.getProperty !== void 0 ? await context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, budget);
13159
+ const property = await toPropertyKey(key, budget, context);
13160
+ const growth = (typeof property === "symbol" ? property.description?.length ?? 0 : property.length) + 1 + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([value]));
13161
+ budget.visitNode();
13162
+ defineOwnDataProperty(object, property, value);
13163
+ entry = key = value = void 0;
13164
+ checkData(object, growth);
13165
+ } catch (error) {
13166
+ await closeOnThrow(error);
13126
13167
  }
13127
13168
  }
13128
- case "unshift": {
13129
- const nextLength = await prependArrayValues(value, args, options);
13130
- budgetProducedValue(value, options.budget);
13131
- return nextLength;
13132
- }
13169
+ checkData(object, 0, true);
13170
+ return allocateProducedSandboxValue(object, budget);
13171
+ } finally {
13172
+ budget.setRetainedValues(retained, void 0);
13133
13173
  }
13134
13174
  }
13135
- async function appendArrayValues(target, values, options) {
13136
- let length = target.length;
13137
- if (length + values.length > Number.MAX_SAFE_INTEGER)
13138
- throw new TypeError("Array-like length exceeds the safe integer limit.");
13139
- for (const value of values) {
13140
- await writeArrayProperty(target, length++, value, options);
13175
+ function assignSandboxValues(target, sources, budget, context) {
13176
+ if (target === null || target === void 0) {
13177
+ throw new TypeError("Object.assign(target, ...sources) requires a non-null target.");
13141
13178
  }
13142
- await writeArrayProperty(target, "length", length, options);
13143
- return length;
13144
- }
13145
- async function prependArrayValues(target, values, options) {
13146
- const originalLength = target.length;
13147
- const length = originalLength + values.length;
13148
- if (length > Number.MAX_SAFE_INTEGER)
13149
- throw new TypeError("Array-like length exceeds the safe integer limit.");
13150
- for (let index = values.length === 0 ? -1 : originalLength - 1; index >= 0; index -= 1) {
13151
- const targetIndex = index + values.length;
13152
- await moveArrayElement(target, index, targetIndex, options);
13179
+ if (typeof target !== "object") {
13180
+ target = createSandboxBox(target);
13181
+ budget.chargeDataUsage(measureSandboxData([target]));
13153
13182
  }
13154
- for (let index = 0; index < values.length; index += 1) {
13155
- await writeArrayProperty(target, index, values[index], options);
13183
+ if (!isGuestHostObject(target)) objectProperties(target, true);
13184
+ if (context === void 0) {
13185
+ for (const source of sources) {
13186
+ if (source === null || source === void 0) continue;
13187
+ const properties = isGuestHostObject(source) ? void 0 : reflectionProperties(source);
13188
+ const keys = properties === void 0 ? ownEnumerableSandboxKeys(source, true) : [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(source)];
13189
+ for (const key of keys) {
13190
+ if (!hasOwnSandboxProperty(source, key, true)) continue;
13191
+ const value = properties === void 0 ? getSandboxDataProperty(source, key, budget) : Reflect.get(properties, key, source);
13192
+ setSandboxProperty(target, key, value, budget);
13193
+ }
13194
+ }
13195
+ return target;
13156
13196
  }
13157
- await writeArrayProperty(target, "length", length, options);
13158
- return length;
13159
- }
13160
- function isCallbackArrayMethod(methodName) {
13161
- return methodName === "map" || methodName === "filter" || methodName === "find" || methodName === "findIndex" || methodName === "findLast" || methodName === "findLastIndex" || methodName === "some" || methodName === "every" || methodName === "reduce" || methodName === "reduceRight" || methodName === "forEach" || methodName === "flatMap" || methodName === "sort";
13197
+ return (async () => {
13198
+ const release = retainValues(budget, () => [target, ...sources]);
13199
+ try {
13200
+ for (const source of sources) {
13201
+ if (source === null || source === void 0) continue;
13202
+ const properties = isGuestHostObject(source) ? void 0 : reflectionProperties(source);
13203
+ const keys = properties === void 0 ? ownEnumerableSandboxKeys(source, true) : [...Object.getOwnPropertyNames(properties), ...ownSandboxSymbolKeys(source)];
13204
+ for (const key of keys) {
13205
+ if (!hasOwnSandboxProperty(source, key, true)) continue;
13206
+ const value = await (context?.getProperty !== void 0 ? context.getProperty(source, key) : isGuestHostObject(source) ? getSandboxDataProperty(source, key, budget) : readPropertyDescriptor(Object.getOwnPropertyDescriptor(properties, key), source, context, true));
13207
+ await setSandboxProperty(target, key, value, budget, true, context);
13208
+ }
13209
+ }
13210
+ return target;
13211
+ } finally {
13212
+ release();
13213
+ }
13214
+ })();
13162
13215
  }
13163
- function isMutatingArrayMethod(methodName) {
13164
- return methodName === "splice" || methodName === "fill" || methodName === "copyWithin" || methodName === "sort" || methodName === "reverse" || methodName === "push" || methodName === "pop" || methodName === "shift" || methodName === "unshift";
13216
+ function reflectionProperties(value) {
13217
+ if (value !== null && value !== void 0 && typeof value !== "object")
13218
+ value = createSandboxBox(value);
13219
+ return objectProperties(value);
13165
13220
  }
13166
- function getRequiredCallback(methodName, value) {
13167
- if (!isSandboxClosure(value)) {
13168
- throw new TypeError(`Array#${methodName} requires a sandbox closure callback.`);
13221
+ function objectProperties(value, mutable = false) {
13222
+ if (isSandboxPromise(value)) return getPromiseProperties(value);
13223
+ if (isSandboxMap(value) || isSandboxSet(value)) return getCollectionProperties(value);
13224
+ if (isSandboxRegex(value)) return getRegexProperties(value);
13225
+ if (isSandboxDate(value)) {
13226
+ return value;
13169
13227
  }
13170
- return value;
13171
- }
13172
- function readArrayElement(value, index, options) {
13173
- const receiver = arrayLikeSources.get(value) ?? value;
13174
- return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, index) : getSandboxDataProperty(receiver, index, options.budget);
13175
- }
13176
- function writeArrayProperty(value, key, entry, options) {
13177
- options.budget.visitNode();
13178
- const receiver = arrayLikeSources.get(value) ?? value;
13179
- if (Array.isArray(receiver)) {
13180
- if (key === "length") options.budget.allocateArrayLength(Number(entry));
13181
- else if (typeof key === "number") options.budget.allocateArrayLength(key + 1);
13228
+ if (isGuestHostObject(value))
13229
+ throw new TypeError("Live host object descriptors are not supported.");
13230
+ if (isGuestClosure(value)) return materializeFunctionProperties(value);
13231
+ if (isSandboxClosure(value)) {
13232
+ if (mutable) throw new TypeError("Host function properties are read only.");
13233
+ return value.properties ?? /* @__PURE__ */ Object.create(null);
13182
13234
  }
13183
- if (options.setProperty !== void 0) return options.setProperty(receiver, String(key), entry);
13184
- if (!Reflect.set(receiver, key, entry))
13185
- throw new TypeError(`Cannot assign to read only property '${key}'.`);
13186
- }
13187
- function deleteArrayElement(value, index, options) {
13188
- options.budget.visitNode();
13189
- const receiver = arrayLikeSources.get(value) ?? value;
13190
- const deleted = options.deleteProperty === void 0 ? Reflect.deleteProperty(receiver, String(index)) : options.deleteProperty(receiver, String(index));
13191
- if (!deleted) throw new TypeError(`Cannot delete property '${index}'.`);
13235
+ if (!isAssignableSandboxTarget(value))
13236
+ throw new TypeError("Expected a sandbox object or function.");
13237
+ return value;
13192
13238
  }
13193
- async function moveArrayElement(value, from, to, options) {
13194
- options.budget.visitNode();
13195
- if (from in value)
13196
- await writeArrayProperty(value, to, await readArrayElement(value, from, options), options);
13197
- else deleteArrayElement(value, to, options);
13239
+ function exposePropertyDescriptor(descriptor) {
13240
+ return "value" in descriptor ? descriptor : {
13241
+ get: accessorClosure(descriptor.get),
13242
+ set: accessorClosure(descriptor.set),
13243
+ enumerable: descriptor.enumerable,
13244
+ configurable: descriptor.configurable
13245
+ };
13198
13246
  }
13199
- async function mapArray(value, callback, options, stack, thisValue) {
13200
- const length = value.length;
13201
- options.budget.allocateArrayLength(length);
13202
- const result = new Array(length);
13203
- options.budget.setRetainedValues(result, () => [result]);
13247
+ async function propertyDescriptor(input, budget, context) {
13248
+ objectProperties(input);
13249
+ const descriptor = {};
13250
+ const release = retainValues(budget, () => [
13251
+ input,
13252
+ descriptor.value,
13253
+ ...retainedAccessorClosures(descriptor)
13254
+ ]);
13204
13255
  try {
13205
- for (let index = 0; index < length; index += 1) {
13206
- options.budget.visitNode();
13207
- if (!(index in value)) {
13256
+ for (const field of [
13257
+ "enumerable",
13258
+ "configurable",
13259
+ "value",
13260
+ "writable",
13261
+ "get",
13262
+ "set"
13263
+ ]) {
13264
+ if (getSandboxPropertyDescriptor(input, field, budget) === void 0 && !hasOwnSandboxProperty(input, field, false))
13208
13265
  continue;
13266
+ const value = await (context?.getProperty !== void 0 ? context.getProperty(input, field) : getSandboxDataProperty(input, field, budget));
13267
+ if (field === "get" || field === "set") {
13268
+ if (value !== void 0 && !isSandboxClosure(value))
13269
+ throw new TypeError("Accessor must be a function or undefined.");
13270
+ descriptor[field] = value === void 0 ? void 0 : accessorAdapter(value, field);
13271
+ } else if (field === "value") {
13272
+ descriptor.value = value;
13273
+ } else {
13274
+ descriptor[field] = Boolean(value);
13209
13275
  }
13210
- result[index] = await callArrayCallback(
13211
- callback,
13212
- await readArrayElement(value, index, options),
13213
- index,
13214
- value,
13215
- options,
13216
- stack,
13217
- thisValue
13218
- );
13219
13276
  }
13220
- return result;
13277
+ if (("get" in descriptor || "set" in descriptor) && ("value" in descriptor || "writable" in descriptor))
13278
+ throw new TypeError("A property cannot be both a data property and an accessor.");
13279
+ return descriptor;
13221
13280
  } finally {
13222
- options.budget.setRetainedValues(result, void 0);
13281
+ release();
13223
13282
  }
13224
13283
  }
13225
- async function filterArray(value, callback, options, stack, thisValue) {
13226
- const length = value.length;
13227
- const result = [];
13228
- options.budget.setRetainedValues(result, () => [result]);
13284
+ async function definePropertiesFromObject(target, descriptors2, budget, context) {
13285
+ objectProperties(target, true);
13286
+ const properties = [];
13287
+ const release = retainValues(budget, () => [
13288
+ target,
13289
+ descriptors2,
13290
+ properties,
13291
+ ...properties.flatMap(([, descriptor]) => retainedAccessorClosures(descriptor))
13292
+ ]);
13229
13293
  try {
13230
- for (let index = 0; index < length; index += 1) {
13231
- options.budget.visitNode();
13232
- if (!(index in value)) {
13233
- continue;
13234
- }
13235
- const entry = await readArrayElement(value, index, options);
13236
- if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
13237
- result.push(entry);
13238
- }
13294
+ for (const key of ownEnumerableSandboxKeys(descriptors2, true)) {
13295
+ if (!hasOwnSandboxProperty(descriptors2, key, true)) continue;
13296
+ const descriptor = await (context?.getProperty !== void 0 ? context.getProperty(descriptors2, key) : getSandboxDataProperty(descriptors2, key, budget));
13297
+ properties.push([key, await propertyDescriptor(descriptor, budget, context)]);
13239
13298
  }
13240
- return result;
13299
+ for (const [key, descriptor] of properties) defineDataProperty(target, key, descriptor, budget);
13241
13300
  } finally {
13242
- options.budget.setRetainedValues(result, void 0);
13243
- }
13244
- }
13245
- async function findInArray(value, callback, options, stack, thisValue) {
13246
- const length = value.length;
13247
- for (let index = 0; index < length; index += 1) {
13248
- options.budget.visitNode();
13249
- const entry = await readArrayElement(value, index, options);
13250
- if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
13251
- return entry;
13252
- }
13301
+ release();
13253
13302
  }
13254
- return void 0;
13255
13303
  }
13256
- async function findIndexInArray(value, callback, options, stack, thisValue) {
13257
- const length = value.length;
13258
- for (let index = 0; index < length; index += 1) {
13259
- options.budget.visitNode();
13260
- const entry = await readArrayElement(value, index, options);
13261
- if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
13262
- return index;
13304
+ function defineDataProperty(target, key, descriptor, budget) {
13305
+ budget.visitNode();
13306
+ if (isFloat32Array(target))
13307
+ throw new TypeError("Typed array property descriptors are not supported.");
13308
+ const properties = objectProperties(target, true);
13309
+ if (Array.isArray(properties)) {
13310
+ if (key === "length" && "value" in descriptor)
13311
+ budget.allocateArrayLength(Number(descriptor.value));
13312
+ else if (typeof key !== "symbol") {
13313
+ const index = Number(key);
13314
+ if (Number.isInteger(index) && index >= 0 && index < 4294967295 && String(index) === key) {
13315
+ budget.allocateArrayLength(index + 1);
13316
+ }
13263
13317
  }
13264
13318
  }
13265
- return -1;
13319
+ Object.defineProperty(properties, key, descriptor);
13320
+ markDescriptorObject(properties);
13266
13321
  }
13267
- async function findLastInArray(value, callback, options, stack, thisValue) {
13268
- const length = value.length;
13269
- for (let index = length - 1; index >= 0; index -= 1) {
13270
- options.budget.visitNode();
13271
- const entry = await readArrayElement(value, index, options);
13272
- if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
13273
- return entry;
13274
- }
13275
- }
13276
- return void 0;
13322
+ function isAssignableSandboxTarget(value) {
13323
+ return typeof value === "object" && value !== null && !isSandboxClosure(value) && !isSandboxGenerator(value) && !isSandboxMap(value) && !isSandboxSet(value) && !isSandboxPromise(value) && !isSandboxRegex(value);
13277
13324
  }
13278
- async function findLastIndexInArray(value, callback, options, stack, thisValue) {
13279
- const length = value.length;
13280
- for (let index = length - 1; index >= 0; index -= 1) {
13281
- options.budget.visitNode();
13282
- const entry = await readArrayElement(value, index, options);
13283
- if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
13284
- return index;
13285
- }
13325
+ async function arrayFromSandboxValues(args, budget, context) {
13326
+ const [items, mapFn, thisValue] = args;
13327
+ if (mapFn !== void 0 && !isSandboxClosure(mapFn)) {
13328
+ throw new TypeError("Array.from mapping callback must be a function.");
13286
13329
  }
13287
- return -1;
13288
- }
13289
- async function someInArray(value, callback, options, stack, thisValue) {
13290
- const length = value.length;
13291
- for (let index = 0; index < length; index += 1) {
13292
- options.budget.visitNode();
13293
- if (!(index in value)) {
13294
- continue;
13295
- }
13296
- if (await callArrayCallback(
13297
- callback,
13298
- await readArrayElement(value, index, options),
13299
- index,
13300
- value,
13301
- options,
13302
- stack,
13303
- thisValue
13304
- )) {
13305
- return true;
13306
- }
13330
+ if (items === null || items === void 0) {
13331
+ throw new TypeError("Array.from requires a non-null input.");
13307
13332
  }
13308
- return false;
13309
- }
13310
- async function everyInArray(value, callback, options, stack, thisValue) {
13311
- const length = value.length;
13312
- for (let index = 0; index < length; index += 1) {
13313
- options.budget.visitNode();
13314
- if (!(index in value)) {
13315
- continue;
13316
- }
13317
- if (!await callArrayCallback(
13318
- callback,
13319
- await readArrayElement(value, index, options),
13320
- index,
13321
- value,
13322
- options,
13323
- stack,
13324
- thisValue
13325
- )) {
13326
- return false;
13333
+ const read = (property) => context?.getProperty !== void 0 ? context.getProperty(items, property) : getSandboxDataProperty(items, property, budget);
13334
+ const iterator = context === void 0 ? getSandboxIterator(items, budget) : await acquireSandboxIterator(items, budget, context);
13335
+ const constructor = context?.thisValue;
13336
+ let result;
13337
+ let currentValue;
13338
+ let failure;
13339
+ const retained = {};
13340
+ budget.setRetainedValues(retained, () => [
13341
+ items,
13342
+ iterator?.retainedValue,
13343
+ mapFn,
13344
+ constructor,
13345
+ result,
13346
+ currentValue,
13347
+ failure
13348
+ ]);
13349
+ const checkData = createDataCheckpoint(budget, context);
13350
+ const closeOnThrow = async (error) => {
13351
+ failure = isCapturedException(error) ? error.reason : error;
13352
+ try {
13353
+ if (iterator !== void 0) await closeIterator(iterator, true);
13354
+ } catch (closeError) {
13355
+ if (!isFatalSandboxError(error) && isFatalSandboxError(closeError)) throw closeError;
13327
13356
  }
13328
- }
13329
- return true;
13330
- }
13331
- async function reduceArray(value, callback, hasInitialValue, initialValue, options, stack) {
13332
- const length = value.length;
13333
- if (hasInitialValue) {
13334
- return reduceFromLeft(value, callback, initialValue, 0, length, options, stack);
13335
- }
13336
- const start = findNextDefinedIndex(value, 0, 1, length, options.budget);
13337
- if (start < 0) {
13338
- throw new TypeError("Reduce of empty array with no initial value.");
13339
- }
13340
- return reduceFromLeft(
13341
- value,
13342
- callback,
13343
- await readArrayElement(value, start, options),
13344
- start + 1,
13345
- length,
13346
- options,
13347
- stack
13348
- );
13349
- }
13350
- async function reduceRightArray(value, callback, hasInitialValue, initialValue, options, stack) {
13351
- const length = value.length;
13352
- if (hasInitialValue) {
13353
- return reduceFromRight(value, callback, initialValue, length - 1, length, options, stack);
13354
- }
13355
- const start = findNextDefinedIndex(value, length - 1, -1, length, options.budget);
13356
- if (start < 0) {
13357
- throw new TypeError("Reduce of empty array with no initial value.");
13358
- }
13359
- return reduceFromRight(
13360
- value,
13361
- callback,
13362
- await readArrayElement(value, start, options),
13363
- start - 1,
13364
- length,
13365
- options,
13366
- stack
13367
- );
13368
- }
13369
- async function reduceFromLeft(value, callback, accumulator, startIndex, length, options, stack) {
13370
- let current = accumulator;
13371
- const retainedAccumulator = {};
13372
- options.budget.setRetainedValues(retainedAccumulator, () => [current]);
13357
+ throw error;
13358
+ };
13373
13359
  try {
13374
- for (let index = startIndex; index < length; index += 1) {
13375
- options.budget.visitNode();
13376
- if (!(index in value)) {
13377
- continue;
13360
+ let length = 0;
13361
+ if (iterator === void 0) {
13362
+ const number = await sandboxNumber(await read("length"), budget, context);
13363
+ length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
13364
+ }
13365
+ result = isSandboxClosure(constructor) && constructor.construct !== void 0 ? await invokeBuiltinClosure(
13366
+ constructor,
13367
+ iterator === void 0 ? [length] : [],
13368
+ budget,
13369
+ context,
13370
+ void 0,
13371
+ true
13372
+ ) : createArrayFromConstructorArgs([length], budget);
13373
+ checkData(result, 0, true);
13374
+ let index = 0;
13375
+ while (iterator !== void 0 || index < length) {
13376
+ try {
13377
+ budget.visitNode();
13378
+ if (iterator !== void 0 && index >= Number.MAX_SAFE_INTEGER)
13379
+ throw new TypeError("Array.from input is too long.");
13380
+ } catch (error) {
13381
+ await closeOnThrow(error);
13378
13382
  }
13379
- current = await options.callClosure(
13380
- callback,
13381
- [
13382
- current,
13383
- await readArrayElement(value, index, options),
13384
- index,
13385
- arrayLikeSources.get(value) ?? value
13386
- ],
13387
- stack
13388
- );
13383
+ if (iterator !== void 0) {
13384
+ const next = await iterator.next();
13385
+ if (typeof next !== "object" || next === null)
13386
+ throw new TypeError("Iterator result must be an object.");
13387
+ if ((await readIteratorResult(iterator, next, "done")).value) break;
13388
+ currentValue = (await readIteratorResult(iterator, next, "value")).value;
13389
+ } else {
13390
+ currentValue = await read(index);
13391
+ }
13392
+ try {
13393
+ if (Array.isArray(result)) budget.allocateArrayLength(index + 1);
13394
+ if (mapFn !== void 0)
13395
+ currentValue = await invokeBuiltinClosure(
13396
+ mapFn,
13397
+ [currentValue, index],
13398
+ budget,
13399
+ context,
13400
+ thisValue
13401
+ );
13402
+ const key = String(index);
13403
+ const growth = key.length + 1 + (Array.isArray(result) ? Math.max(0, index + 1 - result.length) : 0) + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([currentValue]));
13404
+ budget.visitNode();
13405
+ defineOwnDataProperty(objectProperties(result, true), key, currentValue);
13406
+ currentValue = void 0;
13407
+ checkData(result, growth, isSandboxClosure(result));
13408
+ } catch (error) {
13409
+ await closeOnThrow(error);
13410
+ }
13411
+ index += 1;
13389
13412
  }
13390
- return current;
13413
+ setSandboxProperty(result, "length", index, budget);
13414
+ checkData(result, 0, true);
13415
+ return allocateProducedSandboxValue(result, budget);
13391
13416
  } finally {
13392
- options.budget.setRetainedValues(retainedAccumulator, void 0);
13417
+ budget.setRetainedValues(retained, void 0);
13393
13418
  }
13394
13419
  }
13395
- async function reduceFromRight(value, callback, accumulator, startIndex, length, options, stack) {
13396
- let current = accumulator;
13397
- const retainedAccumulator = {};
13398
- options.budget.setRetainedValues(retainedAccumulator, () => [current]);
13420
+ function createArrayFromConstructorArgs(args, budget) {
13421
+ if (args.length !== 1) {
13422
+ return budgetSandboxValue(Reflect.apply(Array, Array, [...args]), budget);
13423
+ }
13424
+ const [lengthOrValue] = args;
13425
+ if (typeof lengthOrValue !== "number") {
13426
+ return budgetSandboxValue([lengthOrValue], budget);
13427
+ }
13428
+ if (!Number.isInteger(lengthOrValue) || lengthOrValue < 0 || lengthOrValue > 4294967295) {
13429
+ throw new RangeError("Invalid array length.");
13430
+ }
13431
+ budget.allocateArrayLength(lengthOrValue);
13432
+ const release = budget.provisionDataUsage(lengthOrValue + 1);
13399
13433
  try {
13400
- for (let index = Math.min(startIndex, length - 1); index >= 0; index -= 1) {
13401
- options.budget.visitNode();
13402
- if (!(index in value)) {
13403
- continue;
13404
- }
13405
- current = await options.callClosure(
13406
- callback,
13407
- [
13408
- current,
13409
- await readArrayElement(value, index, options),
13410
- index,
13411
- arrayLikeSources.get(value) ?? value
13412
- ],
13413
- stack
13414
- );
13415
- }
13416
- return current;
13434
+ return new Array(lengthOrValue);
13417
13435
  } finally {
13418
- options.budget.setRetainedValues(retainedAccumulator, void 0);
13419
- }
13420
- }
13421
- async function forEachArray(value, callback, options, stack, thisValue) {
13422
- const length = value.length;
13423
- for (let index = 0; index < length; index += 1) {
13424
- options.budget.visitNode();
13425
- if (!(index in value)) {
13426
- continue;
13427
- }
13428
- await callArrayCallback(
13429
- callback,
13430
- await readArrayElement(value, index, options),
13431
- index,
13432
- value,
13433
- options,
13434
- stack,
13435
- thisValue
13436
- );
13436
+ release();
13437
13437
  }
13438
13438
  }
13439
- async function flatMapArray(value, callback, options, stack, thisValue) {
13440
- const length = value.length;
13441
- const result = [];
13442
- options.budget.setRetainedValues(result, () => [result]);
13439
+ async function getOwnEnumerableEntries(value, budget, context) {
13440
+ const entries = [];
13441
+ const release = retainValues(budget, () => [value, entries]);
13443
13442
  try {
13444
- for (let index = 0; index < length; index += 1) {
13445
- options.budget.visitNode();
13446
- if (!(index in value)) {
13447
- continue;
13448
- }
13449
- const mapped = await callArrayCallback(
13450
- callback,
13451
- await readArrayElement(value, index, options),
13452
- index,
13453
- value,
13454
- options,
13455
- stack,
13456
- thisValue
13457
- );
13458
- if (Array.isArray(mapped)) {
13459
- for (let mappedIndex = 0; mappedIndex < mapped.length; mappedIndex += 1) {
13460
- options.budget.visitNode();
13461
- if (!(mappedIndex in mapped)) {
13462
- continue;
13463
- }
13464
- result.push(await readArrayElement(mapped, mappedIndex, options));
13465
- options.budget.allocateArrayLength(result.length);
13466
- }
13467
- continue;
13468
- }
13469
- result.push(mapped);
13470
- options.budget.allocateArrayLength(result.length);
13443
+ for (const key of ownEnumerableSandboxKeys(value)) {
13444
+ if (!hasOwnSandboxProperty(value, key, true)) continue;
13445
+ entries.push([
13446
+ key,
13447
+ await (context?.getProperty !== void 0 ? context.getProperty(value, key) : getSandboxDataProperty(value, key, budget))
13448
+ ]);
13471
13449
  }
13472
- return result;
13450
+ return entries;
13473
13451
  } finally {
13474
- options.budget.setRetainedValues(result, void 0);
13452
+ release();
13475
13453
  }
13476
13454
  }
13477
- async function flattenArray(value, depth, options) {
13478
- const result = [];
13479
- const retained = {};
13480
- options.budget.setRetainedValues(retained, () => [result]);
13455
+ function budgetSandboxValue(value, budget) {
13456
+ const sandboxValue = deepCopyToSandbox(value);
13457
+ return allocateProducedSandboxValue(sandboxValue, budget);
13458
+ }
13459
+ function stringFromCodes(args, convert, budget, context) {
13460
+ let result = "";
13461
+ let index = 0;
13462
+ const release = retainValues(budget, () => [...args, result]);
13481
13463
  try {
13482
- await appendFlattenedEntries(value, depth, result, options);
13464
+ const output = advance();
13465
+ if (typeof output !== "string") return output.finally(release);
13466
+ release();
13467
+ return output;
13468
+ } catch (error) {
13469
+ release();
13470
+ throw error;
13471
+ }
13472
+ function advance() {
13473
+ while (index < args.length) {
13474
+ budget.visitNode();
13475
+ const number = sandboxNumber(args[index++], budget, context);
13476
+ if (number instanceof Promise) return number.then((value) => {
13477
+ result = budget.allocateString(result + convert(value));
13478
+ return advance();
13479
+ });
13480
+ result = budget.allocateString(result + convert(number));
13481
+ }
13483
13482
  return result;
13484
- } finally {
13485
- options.budget.setRetainedValues(retained, void 0);
13486
13483
  }
13487
13484
  }
13488
- async function appendFlattenedEntries(value, depth, result, options) {
13489
- const length = value.length;
13490
- for (let index = 0; index < length; index += 1) {
13491
- options.budget.visitNode();
13492
- if (!(index in value)) {
13493
- continue;
13494
- }
13495
- const entry = await readArrayElement(value, index, options);
13496
- if (depth > 0 && Array.isArray(entry)) {
13497
- await appendFlattenedEntries(entry, depth - 1, result, options);
13498
- continue;
13485
+ function stringRaw(args, budget, context) {
13486
+ const [template, ...substitutions] = args;
13487
+ if (context?.getProperty !== void 0)
13488
+ return (async () => {
13489
+ if (template === null || template === void 0)
13490
+ throw new TypeError("String.raw requires a template object.");
13491
+ const raw2 = await context.getProperty(template, "raw");
13492
+ if (raw2 === null || raw2 === void 0)
13493
+ throw new TypeError("String.raw requires raw strings.");
13494
+ const number = await sandboxNumber(
13495
+ await context.getProperty(raw2, "length"),
13496
+ budget,
13497
+ context
13498
+ );
13499
+ const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
13500
+ let result2 = "";
13501
+ const retained = {};
13502
+ budget.setRetainedValues(retained, () => [raw2, result2]);
13503
+ try {
13504
+ for (let index = 0; index < length; index++) {
13505
+ budget.visitNode();
13506
+ result2 = budget.allocateString(
13507
+ result2 + await sandboxString(await context.getProperty(raw2, index), budget, context)
13508
+ );
13509
+ if (index + 1 < length && index < substitutions.length)
13510
+ result2 = budget.allocateString(
13511
+ result2 + await sandboxString(substitutions[index], budget, context)
13512
+ );
13513
+ }
13514
+ return result2;
13515
+ } finally {
13516
+ budget.setRetainedValues(retained, void 0);
13517
+ }
13518
+ })();
13519
+ const raw = getTemplateRawParts(template);
13520
+ let result = "";
13521
+ for (let index = 0; index < raw.length; index += 1) {
13522
+ result += String(raw[index]);
13523
+ if (index < raw.length - 1 && index < substitutions.length) {
13524
+ result += String(substitutions[index]);
13499
13525
  }
13500
- result.push(entry);
13501
- options.budget.allocateArrayLength(result.length);
13502
13526
  }
13527
+ return budget.allocateString(result);
13503
13528
  }
13504
- async function sortArray(value, comparator, options, stack) {
13505
- const length = value.length;
13506
- const definedValues = [];
13507
- let undefinedCount = 0;
13508
- let currentEntry;
13509
- options.budget.setRetainedValues(definedValues, () => [definedValues, currentEntry]);
13529
+ function getTemplateRawParts(template) {
13530
+ const raw = typeof template === "object" && template !== null ? template.raw : void 0;
13531
+ if (typeof template !== "object" || template === null || isSandboxClosure(template) || isSandboxPromise(template) || !Array.isArray(raw)) {
13532
+ throw new TypeError("String.raw requires a raw strings array.");
13533
+ }
13534
+ return raw;
13535
+ }
13536
+
13537
+ // packages/safe-js/src/interp/classes.ts
13538
+ async function evaluateClass(node, context, evaluateNode2, callContext) {
13539
+ const scope = context.scope.child();
13540
+ if (node.id !== void 0) scope.predeclare(node.id.name, "const");
13541
+ const classContext = { ...context, scope, inferredName: void 0 };
13542
+ let parent;
13543
+ let prototypeParent;
13544
+ const fields2 = [];
13545
+ const statics = [];
13546
+ let constructor;
13547
+ const release = retainValues(context.budget, () => [parent, prototypeParent, constructor, ...fields2.map((field) => field.key), ...statics.flatMap((field) => "key" in field ? [field.key] : [])]);
13510
13548
  try {
13511
- for (let index = 0; index < length; index += 1) {
13512
- options.budget.visitNode();
13513
- if (!(index in value)) {
13549
+ if (node.superClass !== void 0) {
13550
+ const result = await evaluateNode2(node.superClass, classContext);
13551
+ if (result.kind !== "normal") return result;
13552
+ parent = result.value;
13553
+ if (parent !== null && (!isSandboxClosure(parent) || parent.construct === void 0))
13554
+ throw new TypeError("Class extends value is not a constructor or null.");
13555
+ prototypeParent = parent === null ? null : await callContext.getProperty(parent, "prototype");
13556
+ if (prototypeParent !== null && typeof prototypeParent !== "object")
13557
+ throw new TypeError("Class extends value has an invalid prototype.");
13558
+ }
13559
+ const derived = node.superClass !== void 0;
13560
+ constructor = createClassConstructor(node, { ...classContext, inferredName: context.inferredName }, evaluateNode2, fields2);
13561
+ const properties = materializeFunctionProperties(constructor);
13562
+ const prototype = properties.prototype;
13563
+ Object.defineProperty(properties, "prototype", { writable: false });
13564
+ if (derived) setSandboxPrototype(prototype, prototypeParent, context.budget);
13565
+ if (isSandboxClosure(parent)) setSandboxPrototype(constructor, parent, context.budget);
13566
+ const pattern = createPatternContext(classContext, scope, evaluateNode2);
13567
+ for (const element of node.body.body) {
13568
+ context.budget.visitNode();
13569
+ if (element.type === "StaticBlock") {
13570
+ statics.push({ element });
13514
13571
  continue;
13515
13572
  }
13516
- const entry = await readArrayElement(value, index, options);
13517
- if (entry === void 0) {
13518
- undefinedCount += 1;
13519
- continue;
13573
+ if (element.type === "MethodDefinition" && element.kind === "constructor") continue;
13574
+ let key;
13575
+ if (element.computed) {
13576
+ const result = await evaluateNode2(element.key, classContext);
13577
+ if (result.kind !== "normal") return result;
13578
+ key = await pattern.toPropertyKey(result.value);
13579
+ } else {
13580
+ key = element.key.type === "Identifier" ? element.key.name : String(element.key.value);
13520
13581
  }
13521
- definedValues.push(entry);
13522
- }
13523
- for (let index = 1; index < definedValues.length; index += 1) {
13524
- currentEntry = definedValues[index];
13525
- let cursor = index - 1;
13526
- while (cursor >= 0 && await compareEntries(definedValues[cursor], currentEntry, comparator, options, stack) > 0) {
13527
- definedValues[cursor + 1] = definedValues[cursor];
13528
- cursor -= 1;
13582
+ if (typeof key === "string") context.budget.allocateString(key);
13583
+ if (element.type === "PropertyDefinition") {
13584
+ (element.static ? statics : fields2).push({ element, key });
13585
+ } else {
13586
+ const home = element.static ? constructor : prototype;
13587
+ const method = createInterpretedClosure(element.value, classContext, evaluateNode2, home);
13588
+ const accessor = element.kind === "get" || element.kind === "set" ? element.kind : void 0;
13589
+ Object.defineProperty(materializeFunctionProperties(method), "name", {
13590
+ value: accessor === void 0 ? propertyFunctionName(key) : `${accessor} ${propertyFunctionName(key)}`
13591
+ });
13592
+ Object.defineProperty(
13593
+ element.static ? properties : prototype,
13594
+ key,
13595
+ accessor === void 0 ? { value: method, configurable: true, writable: true, enumerable: false } : { [accessor]: accessorAdapter(method, accessor), configurable: true, enumerable: false }
13596
+ );
13529
13597
  }
13530
- definedValues[cursor + 1] = currentEntry;
13531
- }
13532
- currentEntry = void 0;
13533
- for (let index = 0; index < definedValues.length; index += 1) {
13534
- await writeArrayProperty(value, index, definedValues[index], options);
13535
- }
13536
- for (let index = 0; index < undefinedCount; index += 1) {
13537
- await writeArrayProperty(value, definedValues.length + index, void 0, options);
13538
13598
  }
13539
- for (let index = definedValues.length + undefinedCount; index < length; index += 1) {
13540
- options.budget.visitNode();
13541
- deleteArrayElement(value, index, options);
13599
+ if (node.id !== void 0) scope.declare(node.id.name, "const", constructor);
13600
+ for (const element of statics) await initializeElement(element, constructor, constructor, classContext, evaluateNode2);
13601
+ classOrigins.get(constructor).initialized = true;
13602
+ if (node.type === "ClassDeclaration") {
13603
+ context.scope.declare(node.id.name, "let", constructor);
13604
+ return { kind: "normal", hasValue: false, value: void 0 };
13542
13605
  }
13606
+ return { kind: "normal", hasValue: true, value: constructor };
13543
13607
  } finally {
13544
- options.budget.setRetainedValues(definedValues, void 0);
13608
+ release();
13545
13609
  }
13546
13610
  }
13547
- async function compareEntries(left, right, comparator, options, stack) {
13548
- options.budget.visitNode();
13549
- if (comparator === void 0) {
13550
- const leftString = await sandboxString(left, options.budget, options.context);
13551
- const release = retainValues(options.budget, () => [leftString]);
13552
- try {
13553
- const rightString = await sandboxString(right, options.budget, options.context);
13554
- return leftString < rightString ? -1 : leftString > rightString ? 1 : 0;
13555
- } finally {
13556
- release();
13611
+ var classOrigins = /* @__PURE__ */ new WeakMap();
13612
+ function createClassConstructor(node, context, evaluateNode2, fields2) {
13613
+ const scope = context.scope;
13614
+ const classContext = { ...context, inferredName: void 0 };
13615
+ const constructorElement = node.body.body.find((element) => element.type === "MethodDefinition" && element.kind === "constructor");
13616
+ const derived = node.superClass !== void 0;
13617
+ const constructor = createSandboxClosure({
13618
+ guest: true,
13619
+ sandbox: true,
13620
+ name: node.id?.name ?? context.inferredName ?? "",
13621
+ length: constructorElement === void 0 ? 0 : getFunctionLength(constructorElement.value.params),
13622
+ sourceRange: functionSources.get(node),
13623
+ retainedValues: () => [...scope.retainedValues(), ...fields2.map((field) => field.key)],
13624
+ call: () => {
13625
+ throw new TypeError("Class constructor cannot be invoked without 'new'.");
13626
+ },
13627
+ construct: async (args, invocation) => {
13628
+ const prototype = materializeFunctionProperties(constructor).prototype;
13629
+ const newTarget = invocation?.newTarget ?? constructor;
13630
+ let thisValue;
13631
+ let thisScope;
13632
+ let initialized = !derived;
13633
+ const initializeFields = async (receiver) => {
13634
+ for (const field of fields2) {
13635
+ await initializeElement(field, receiver, prototype, classContext, evaluateNode2);
13636
+ }
13637
+ };
13638
+ const superCall = async (argumentsList) => {
13639
+ const superConstructor = getSandboxPrototype(constructor, context.budget);
13640
+ if (!isSandboxClosure(superConstructor) || superConstructor.construct === void 0)
13641
+ throw new TypeError("Super constructor is not a constructor.");
13642
+ const receiver = await invocation.invokeClosure(superConstructor, argumentsList, void 0, true, newTarget);
13643
+ if (initialized) throw new ReferenceError("Super constructor may only initialize this once.");
13644
+ initialized = true;
13645
+ thisValue = receiver;
13646
+ thisScope?.declare("this", "const", receiver);
13647
+ await initializeFields(receiver);
13648
+ return receiver;
13649
+ };
13650
+ if (!derived) {
13651
+ thisValue = {};
13652
+ const targetPrototype = await invocation.getProperty(newTarget, "prototype");
13653
+ if (typeof targetPrototype === "object" && targetPrototype !== null)
13654
+ setSandboxPrototype(thisValue, targetPrototype, context.budget);
13655
+ }
13656
+ if (constructorElement === void 0) {
13657
+ if (derived) return superCall(args);
13658
+ await initializeFields(thisValue);
13659
+ return thisValue;
13660
+ }
13661
+ const result = await executeClosure(constructorElement.value, args, thisValue, {
13662
+ ...classContext,
13663
+ compilation: invocation?.compilation ?? context.compilation,
13664
+ callStack: [...invocation?.stack ?? context.callStack],
13665
+ functionEnvironment: {
13666
+ newTarget,
13667
+ homeObject: prototype,
13668
+ construction: {
13669
+ derived,
13670
+ superCall,
13671
+ initialize: async (functionScope) => {
13672
+ thisScope = functionScope;
13673
+ if (!derived) await initializeFields(thisValue);
13674
+ }
13675
+ }
13676
+ }
13677
+ }, evaluateNode2);
13678
+ if (typeof result === "object" && result !== null) return result;
13679
+ if (derived && result !== void 0) throw new TypeError("Derived constructors may only return an object or undefined.");
13680
+ if (!initialized) throw new ReferenceError("Must call super constructor before returning from derived constructor.");
13681
+ return thisValue;
13557
13682
  }
13558
- }
13559
- const result = await sandboxNumber(
13560
- await options.callClosure(comparator, [left, right], stack),
13561
- options.budget,
13562
- options.context
13563
- );
13564
- return Number.isNaN(result) ? 0 : result;
13565
- }
13566
- async function callArrayCallback(callback, value, index, array2, options, stack, thisValue) {
13567
- return options.callClosure(
13568
- callback,
13569
- [value, index, arrayLikeSources.get(array2) ?? array2],
13570
- stack,
13571
- thisValue
13572
- );
13683
+ });
13684
+ classOrigins.set(constructor, { node, scope, fields: fields2, initialized: false });
13685
+ return constructor;
13573
13686
  }
13574
- function findNextDefinedIndex(value, startIndex, direction, length, budget) {
13575
- for (let index = startIndex; direction > 0 ? index < length : index >= 0; index += direction) {
13576
- budget.visitNode();
13577
- if (index in value) {
13578
- return index;
13579
- }
13687
+ async function initializeElement(definition, receiver, homeObject, context, evaluateNode2) {
13688
+ const scope = context.scope.child({}, { functionBoundary: true });
13689
+ scope.declare("this", "const", receiver);
13690
+ const element = definition.element;
13691
+ const initializer = element.type === "StaticBlock" ? element.body : element.value;
13692
+ let value;
13693
+ if (initializer !== void 0) {
13694
+ if (element.type === "StaticBlock") hoistVarDeclarations(element.body, scope);
13695
+ const result = await evaluateNode2(initializer, { ...context, scope, functionEnvironment: { homeObject }, inferredName: "key" in definition ? propertyFunctionName(definition.key) : void 0 });
13696
+ if (result.kind === "error") throw result.error;
13697
+ if (result.kind === "throw") throw result.value;
13698
+ value = result.hasValue ? result.value : void 0;
13580
13699
  }
13581
- return -1;
13582
- }
13583
- function budgetProducedValue(value, budget) {
13584
- if (typeof value === "object" && value !== null) value = arrayLikeSources.get(value) ?? value;
13585
- allocateProducedValue(value, budget, /* @__PURE__ */ new WeakSet());
13586
- return value;
13587
- }
13588
- function allocateProducedValue(value, budget, seen) {
13589
- if (typeof value === "string") {
13590
- budget.allocateString(value);
13591
- return;
13700
+ if ("key" in definition) {
13701
+ defineDataProperty(receiver, definition.key, { value, configurable: true, writable: true, enumerable: true }, context.budget);
13592
13702
  }
13593
- if (Array.isArray(value)) {
13594
- budget.allocateArrayLength(value.length);
13595
- if (seen.has(value)) {
13596
- return;
13597
- }
13598
- seen.add(value);
13599
- for (const entry of value) {
13600
- allocateProducedValue(entry, budget, seen);
13601
- }
13602
- return;
13703
+ }
13704
+
13705
+ // packages/safe-js/src/interp/resume-target.ts
13706
+ function containsResumeTarget(node, targetNodeIds) {
13707
+ if (targetNodeIds === void 0 ? node.type === "YieldExpression" : node.nodeId !== void 0 && targetNodeIds.has(node.nodeId)) {
13708
+ return true;
13603
13709
  }
13604
- if (isSandboxMap(value)) {
13605
- budget.allocateCollectionEntries(value.entries.size);
13606
- if (seen.has(value)) {
13607
- return;
13608
- }
13609
- seen.add(value);
13610
- for (const [key, entry] of value.entries) {
13611
- allocateProducedValue(key, budget, seen);
13612
- allocateProducedValue(entry, budget, seen);
13613
- }
13614
- return;
13710
+ if (node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression") {
13711
+ return false;
13615
13712
  }
13616
- if (isSandboxSet(value)) {
13617
- budget.allocateCollectionEntries(value.values.size);
13618
- if (seen.has(value)) {
13619
- return;
13713
+ for (const value of Object.values(node)) {
13714
+ if (Array.isArray(value)) {
13715
+ if (value.some((entry) => isParseResult(entry) && containsResumeTarget(entry, targetNodeIds))) {
13716
+ return true;
13717
+ }
13718
+ continue;
13620
13719
  }
13621
- seen.add(value);
13622
- for (const entry of value.values) {
13623
- allocateProducedValue(entry, budget, seen);
13720
+ if (isParseResult(value) && containsResumeTarget(value, targetNodeIds)) {
13721
+ return true;
13624
13722
  }
13625
- return;
13626
- }
13627
- if (typeof value !== "object" || value === null || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value)) {
13628
- return;
13629
- }
13630
- if (seen.has(value)) {
13631
- return;
13632
- }
13633
- seen.add(value);
13634
- for (const entry of Object.values(value)) {
13635
- allocateProducedValue(entry, budget, seen);
13636
13723
  }
13724
+ return false;
13637
13725
  }
13638
- function toIntegerOrInfinity(value) {
13639
- const number = Number(value);
13640
- if (Number.isNaN(number) || Object.is(number, 0) || Object.is(number, -0)) {
13641
- return 0;
13642
- }
13643
- if (!Number.isFinite(number)) {
13644
- return number;
13645
- }
13646
- return Math.trunc(number);
13726
+ function isParseResult(value) {
13727
+ return typeof value === "object" && value !== null && typeof value.type === "string" && Object.hasOwn(value, "span");
13647
13728
  }
13648
13729
 
13649
13730
  // packages/safe-js/src/interp/function-string.ts
@@ -16732,7 +16813,7 @@ async function evaluateMemberCallExpression(node, context) {
16732
16813
  property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
16733
16814
  };
16734
16815
  if (context.generatorYield !== void 0 && node.arguments.some((argument) => containsResumeTarget(argument.type === "SpreadElement" ? argument.argument : argument))) {
16735
- if (Array.isArray(member.object) && !hasExplicitSandboxPrototype(member.object) && typeof member.property !== "symbol" && isArrayMethodName(member.property) && !Object.hasOwn(member.object, member.property))
16816
+ if (Array.isArray(member.object) && typeof member.property !== "symbol" && isArrayMethodName(member.property) && isDefaultArrayMethod(member.object, member.property, context.budget))
16736
16817
  return evaluateArrayMethodCall(node, member.object, member.property, context);
16737
16818
  const receiver = member.superReceiver?.value ?? member.object;
16738
16819
  return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context, receiver), context, receiver);
@@ -16745,6 +16826,8 @@ async function evaluateMemberCallExpression(node, context) {
16745
16826
  return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context), context, member.object);
16746
16827
  if (Array.isArray(member.object) && hasExplicitSandboxPrototype(member.object))
16747
16828
  return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context), context, member.object);
16829
+ if (Array.isArray(member.object) && isArrayMethodName(member.property) && !isDefaultArrayMethod(member.object, member.property, context.budget))
16830
+ return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context), context, member.object);
16748
16831
  if ((typeof member.object === "string" || typeof member.object === "number" || typeof member.object === "bigint" || typeof member.object === "boolean" || typeof member.object === "symbol") && getBoxedPrototype(member.object, context.budget) !== void 0) {
16749
16832
  if (isDefaultBoxedMethod(member.object, member.property, context.budget)) {
16750
16833
  if (typeof member.object === "string" && isStringMethodName(member.property))
@@ -16788,6 +16871,8 @@ async function evaluateMemberCallExpression(node, context) {
16788
16871
  );
16789
16872
  }
16790
16873
  if (Array.isArray(member.object) && !Object.hasOwn(member.object, member.property)) {
16874
+ if (getSandboxPropertyDescriptor(member.object, member.property, context.budget) !== void 0)
16875
+ return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context), context, member.object);
16791
16876
  return evaluatePrimitiveMemberCall(
16792
16877
  node,
16793
16878
  "Array",
@@ -17359,6 +17444,7 @@ function getArrayMemberValue(target, property, context) {
17359
17444
  if (property === "raw" && templateRawArrays.has(target)) {
17360
17445
  return templateRawArrays.get(target);
17361
17446
  }
17447
+ if (getSandboxPrototype(target, context.budget) !== null) return void 0;
17362
17448
  return getArrayMember(target, property, createArrayMethodOptions(context));
17363
17449
  }
17364
17450
  function setSandboxProperty(target, property, value, budget, checkInherited = true, context) {
@@ -18011,7 +18097,7 @@ async function objectToPrimitive(value, budget, context, joining, hint, ordinary
18011
18097
  }
18012
18098
  }
18013
18099
  function conversionHook(value, name, budget, context) {
18014
- const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) && getSandboxPrototype(value, budget) === null || isSandboxGenerator(value) || isGuestHostObject(value));
18100
+ const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) && getSandboxPrototype(value, budget) === null || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) && getSandboxPrototype(value, budget) === null || isSandboxGenerator(value) || isGuestHostObject(value));
18015
18101
  let current = value;
18016
18102
  let depth = 0;
18017
18103
  while (current !== null) {
@@ -18129,6 +18215,20 @@ function createObjectGlobal(methods, budget) {
18129
18215
  Object.defineProperty(properties, name, { value: method, writable: true, configurable: true });
18130
18216
  }
18131
18217
  const prototypeMethods = {
18218
+ toLocaleString: createSandboxClosure({
18219
+ guest: true,
18220
+ sandbox: true,
18221
+ name: "toLocaleString",
18222
+ length: 0,
18223
+ call: async (_args, context) => {
18224
+ const receiver = requireReceiver(context?.thisValue);
18225
+ const object = construct([receiver]);
18226
+ const descriptor = getSandboxPropertyDescriptor(object, "toString", budget);
18227
+ const method = context?.getProperty === void 0 ? descriptor === void 0 ? void 0 : await readPropertyDescriptor(descriptor, receiver, context) : await context.getProperty(receiver, "toString");
18228
+ if (!isSandboxClosure(method)) throw new TypeError("toString must be callable.");
18229
+ return invokeBuiltinClosure(method, [], budget, context, receiver);
18230
+ }
18231
+ }),
18132
18232
  toString: createSandboxClosure({
18133
18233
  sandbox: true,
18134
18234
  name: "toString",
@@ -25691,7 +25791,8 @@ function measureSandboxData(values, options = {}) {
25691
25791
  seen.add(value);
25692
25792
  usage += 1;
25693
25793
  if (!isGuestHostObject(value)) {
25694
- const descriptors3 = Object.getOwnPropertySymbols(value).filter((key) => !internalSymbols.has(key)).map((key) => [key, Object.getOwnPropertyDescriptor(value, key)]);
25794
+ const symbols = Object.getOwnPropertySymbols(value);
25795
+ const descriptors3 = symbols.length === 0 ? [] : symbols.filter((key) => !internalSymbols.has(key)).map((key) => [key, Object.getOwnPropertyDescriptor(value, key)]);
25695
25796
  for (const [key, descriptor] of descriptors3) {
25696
25797
  usage += 1;
25697
25798
  visit(key, depth + 1);
@@ -25846,18 +25947,15 @@ function measureSandboxData(values, options = {}) {
25846
25947
  }
25847
25948
  return;
25848
25949
  }
25849
- const descriptors2 = Object.getOwnPropertyDescriptors(value);
25850
- const keys = Object.keys(descriptors2);
25851
- const includeNonEnumerable = isSandboxDate(value) || hasManagedDescriptors(value);
25852
- let entryCount = 0;
25853
- for (const key of keys) {
25854
- if (descriptors2[key].enumerable || includeNonEnumerable) entryCount += 1;
25950
+ const descriptors2 = [];
25951
+ for (const key of Object.getOwnPropertyNames(value)) {
25952
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
25953
+ if (descriptor !== void 0) descriptors2.push([key, descriptor]);
25855
25954
  }
25856
- usage += entryCount;
25857
- for (const key of keys) {
25858
- const descriptor = descriptors2[key];
25955
+ const includeNonEnumerable = isSandboxDate(value) || hasManagedDescriptors(value);
25956
+ for (const [key, descriptor] of descriptors2) {
25859
25957
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
25860
- usage += key.length;
25958
+ usage += 1 + key.length;
25861
25959
  if ("value" in descriptor) visit(descriptor.value, depth + 1);
25862
25960
  else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
25863
25961
  }
@@ -40630,4 +40728,4 @@ export {
40630
40728
  FileSnapshotBackend,
40631
40729
  run
40632
40730
  };
40633
- //# sourceMappingURL=chunk-4MMZLA7N.js.map
40731
+ //# sourceMappingURL=chunk-DB2DTDG3.js.map