@poe-platform/safe-js 0.1.294 → 0.1.296

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.
@@ -11608,6 +11608,30 @@ function createPrimitiveConstructor(options, budget) {
11608
11608
  }
11609
11609
 
11610
11610
  // packages/safe-js/src/interp/methods/array.ts
11611
+ async function arraySpeciesCreate(value, length, options) {
11612
+ const receiver = arrayLikeSources.get(value) ?? value;
11613
+ const read = (target, key) => options.context?.getProperty !== void 0 ? options.context.getProperty(target, key) : readPropertyDescriptor(getSandboxPropertyDescriptor(target, key, options.budget) ?? { value: void 0 }, target, options.context);
11614
+ let constructor;
11615
+ if (Array.isArray(receiver)) {
11616
+ constructor = await read(receiver, "constructor");
11617
+ if (typeof constructor === "object" && constructor !== null) {
11618
+ constructor = await read(constructor, Symbol.species);
11619
+ if (constructor === null) constructor = void 0;
11620
+ }
11621
+ if (constructor !== void 0) {
11622
+ if (!isSandboxClosure(constructor) || constructor.construct === void 0)
11623
+ throw new TypeError("Array species must be a constructor.");
11624
+ return await invokeBuiltinClosure(constructor, [length], options.budget, options.context, void 0, true);
11625
+ }
11626
+ }
11627
+ options.budget.allocateArrayLength(length);
11628
+ return new Array(length);
11629
+ }
11630
+ function defineArrayResult(result, index, value, options) {
11631
+ if (index >= Number.MAX_SAFE_INTEGER) throw new TypeError("Array result exceeds the safe integer limit.");
11632
+ if (Array.isArray(result)) options.budget.allocateArrayLength(index + 1);
11633
+ defineOwnDataProperty(objectProperties(result, true), String(index), value);
11634
+ }
11611
11635
  var arrayLikeSources = /* @__PURE__ */ new WeakMap();
11612
11636
  var activeArrayCallbacks = /* @__PURE__ */ new WeakMap();
11613
11637
  var localeArrays = /* @__PURE__ */ new WeakSet();
@@ -11887,15 +11911,18 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
11887
11911
  ),
11888
11912
  options.budget
11889
11913
  );
11890
- case "flat":
11914
+ case "flat": {
11915
+ const length = value.length;
11891
11916
  return budgetProducedValue(
11892
11917
  await flattenArray(
11893
11918
  value,
11894
11919
  args[0] === void 0 ? 1 : toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context)),
11895
- options
11920
+ options,
11921
+ length
11896
11922
  ),
11897
11923
  options.budget
11898
11924
  );
11925
+ }
11899
11926
  case "includes":
11900
11927
  case "indexOf":
11901
11928
  case "lastIndexOf": {
@@ -11933,22 +11960,23 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
11933
11960
  const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
11934
11961
  const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
11935
11962
  const count = Math.max(final - first, 0);
11936
- options.budget.allocateArrayLength(count);
11937
- const result = new Array(count);
11963
+ const result = await arraySpeciesCreate(value, count, options);
11938
11964
  const release = retainValues(options.budget, () => [result]);
11939
11965
  try {
11940
11966
  for (let index = 0; index < count; index += 1) {
11941
11967
  options.budget.visitNode();
11942
11968
  if (first + index in value)
11943
- result[index] = await readArrayElement(value, first + index, options);
11969
+ defineArrayResult(result, index, await readArrayElement(value, first + index, options), options);
11944
11970
  }
11971
+ await setSandboxProperty(result, "length", count, options.budget, true, options.context);
11945
11972
  return budgetProducedValue(result, options.budget);
11946
11973
  } finally {
11947
11974
  release();
11948
11975
  }
11949
11976
  }
11950
11977
  case "concat": {
11951
- const result = [];
11978
+ const result = await arraySpeciesCreate(value, 0, options);
11979
+ let targetIndex = 0;
11952
11980
  const retained = {};
11953
11981
  options.budget.setRetainedValues(retained, () => [result]);
11954
11982
  try {
@@ -11959,21 +11987,22 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
11959
11987
  spread = flag === void 0 ? Array.isArray(entry) : Boolean(flag);
11960
11988
  }
11961
11989
  if (!spread) {
11962
- options.budget.allocateArrayLength(result.length + 1);
11963
- result.push(entry);
11990
+ defineArrayResult(result, targetIndex++, entry, options);
11964
11991
  continue;
11965
11992
  }
11966
- const start = result.length;
11993
+ const start = targetIndex;
11967
11994
  const source = await arrayLikeView(entry, options);
11968
11995
  const length = source.length;
11969
- options.budget.allocateArrayLength(start + length);
11970
- result.length += length;
11996
+ if (start + length > Number.MAX_SAFE_INTEGER) throw new TypeError("Array result exceeds the safe integer limit.");
11997
+ if (Array.isArray(result)) options.budget.allocateArrayLength(start + length);
11998
+ targetIndex += length;
11971
11999
  for (let index = 0; index < length; index++) {
11972
12000
  options.budget.visitNode();
11973
12001
  if (index in source)
11974
- result[start + index] = await readArrayElement(source, index, options);
12002
+ defineArrayResult(result, start + index, await readArrayElement(source, index, options), options);
11975
12003
  }
11976
12004
  }
12005
+ await setSandboxProperty(result, "length", targetIndex, options.budget, true, options.context);
11977
12006
  return budgetProducedValue(result, options.budget);
11978
12007
  } finally {
11979
12008
  options.budget.setRetainedValues(retained, void 0);
@@ -11998,16 +12027,16 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
11998
12027
  const nextLength = length + inserted - deleted;
11999
12028
  if (nextLength > Number.MAX_SAFE_INTEGER)
12000
12029
  throw new TypeError("Array-like length exceeds the safe integer limit.");
12001
- options.budget.allocateArrayLength(deleted);
12002
- const removed = new Array(deleted);
12030
+ const removed = await arraySpeciesCreate(value, deleted, options);
12003
12031
  const retained = {};
12004
12032
  options.budget.setRetainedValues(retained, () => [removed]);
12005
12033
  try {
12006
12034
  for (let index = 0; index < deleted; index++) {
12007
12035
  options.budget.visitNode();
12008
12036
  if (first + index in value)
12009
- removed[index] = await readArrayElement(value, first + index, options);
12037
+ defineArrayResult(removed, index, await readArrayElement(value, first + index, options), options);
12010
12038
  }
12039
+ await setSandboxProperty(removed, "length", deleted, options.budget, true, options.context);
12011
12040
  if (inserted < deleted) {
12012
12041
  for (let index = first; index < length - deleted; index++)
12013
12042
  await moveArrayElement(value, index + deleted, index + inserted, options);
@@ -12303,8 +12332,7 @@ async function moveArrayElement(value, from, to, options) {
12303
12332
  }
12304
12333
  async function mapArray(value, callback, options, stack, thisValue) {
12305
12334
  const length = value.length;
12306
- options.budget.allocateArrayLength(length);
12307
- const result = new Array(length);
12335
+ const result = await arraySpeciesCreate(value, length, options);
12308
12336
  options.budget.setRetainedValues(result, () => [result]);
12309
12337
  try {
12310
12338
  for (let index = 0; index < length; index += 1) {
@@ -12312,7 +12340,7 @@ async function mapArray(value, callback, options, stack, thisValue) {
12312
12340
  if (!(index in value)) {
12313
12341
  continue;
12314
12342
  }
12315
- result[index] = await callArrayCallback(
12343
+ defineArrayResult(result, index, await callArrayCallback(
12316
12344
  callback,
12317
12345
  await readArrayElement(value, index, options),
12318
12346
  index,
@@ -12320,7 +12348,7 @@ async function mapArray(value, callback, options, stack, thisValue) {
12320
12348
  options,
12321
12349
  stack,
12322
12350
  thisValue
12323
- );
12351
+ ), options);
12324
12352
  }
12325
12353
  return result;
12326
12354
  } finally {
@@ -12329,7 +12357,8 @@ async function mapArray(value, callback, options, stack, thisValue) {
12329
12357
  }
12330
12358
  async function filterArray(value, callback, options, stack, thisValue) {
12331
12359
  const length = value.length;
12332
- const result = [];
12360
+ const result = await arraySpeciesCreate(value, 0, options);
12361
+ let targetIndex = 0;
12333
12362
  options.budget.setRetainedValues(result, () => [result]);
12334
12363
  try {
12335
12364
  for (let index = 0; index < length; index += 1) {
@@ -12339,7 +12368,7 @@ async function filterArray(value, callback, options, stack, thisValue) {
12339
12368
  }
12340
12369
  const entry = await readArrayElement(value, index, options);
12341
12370
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
12342
- result.push(entry);
12371
+ defineArrayResult(result, targetIndex++, entry, options);
12343
12372
  }
12344
12373
  }
12345
12374
  return result;
@@ -12543,7 +12572,8 @@ async function forEachArray(value, callback, options, stack, thisValue) {
12543
12572
  }
12544
12573
  async function flatMapArray(value, callback, options, stack, thisValue) {
12545
12574
  const length = value.length;
12546
- const result = [];
12575
+ const result = await arraySpeciesCreate(value, 0, options);
12576
+ let targetIndex = 0;
12547
12577
  options.budget.setRetainedValues(result, () => [result]);
12548
12578
  try {
12549
12579
  for (let index = 0; index < length; index += 1) {
@@ -12561,37 +12591,35 @@ async function flatMapArray(value, callback, options, stack, thisValue) {
12561
12591
  thisValue
12562
12592
  );
12563
12593
  if (Array.isArray(mapped)) {
12564
- for (let mappedIndex = 0; mappedIndex < mapped.length; mappedIndex += 1) {
12594
+ const mappedLength = mapped.length;
12595
+ for (let mappedIndex = 0; mappedIndex < mappedLength; mappedIndex += 1) {
12565
12596
  options.budget.visitNode();
12566
12597
  if (!(mappedIndex in mapped)) {
12567
12598
  continue;
12568
12599
  }
12569
- result.push(await readArrayElement(mapped, mappedIndex, options));
12570
- options.budget.allocateArrayLength(result.length);
12600
+ defineArrayResult(result, targetIndex++, await readArrayElement(mapped, mappedIndex, options), options);
12571
12601
  }
12572
12602
  continue;
12573
12603
  }
12574
- result.push(mapped);
12575
- options.budget.allocateArrayLength(result.length);
12604
+ defineArrayResult(result, targetIndex++, mapped, options);
12576
12605
  }
12577
12606
  return result;
12578
12607
  } finally {
12579
12608
  options.budget.setRetainedValues(result, void 0);
12580
12609
  }
12581
12610
  }
12582
- async function flattenArray(value, depth, options) {
12583
- const result = [];
12611
+ async function flattenArray(value, depth, options, length) {
12612
+ const result = await arraySpeciesCreate(value, 0, options);
12584
12613
  const retained = {};
12585
12614
  options.budget.setRetainedValues(retained, () => [result]);
12586
12615
  try {
12587
- await appendFlattenedEntries(value, depth, result, options);
12616
+ await appendFlattenedEntries(value, depth, result, options, 0, length);
12588
12617
  return result;
12589
12618
  } finally {
12590
12619
  options.budget.setRetainedValues(retained, void 0);
12591
12620
  }
12592
12621
  }
12593
- async function appendFlattenedEntries(value, depth, result, options) {
12594
- const length = value.length;
12622
+ async function appendFlattenedEntries(value, depth, result, options, targetIndex, length = value.length) {
12595
12623
  for (let index = 0; index < length; index += 1) {
12596
12624
  options.budget.visitNode();
12597
12625
  if (!(index in value)) {
@@ -12599,12 +12627,12 @@ async function appendFlattenedEntries(value, depth, result, options) {
12599
12627
  }
12600
12628
  const entry = await readArrayElement(value, index, options);
12601
12629
  if (depth > 0 && Array.isArray(entry)) {
12602
- await appendFlattenedEntries(entry, depth - 1, result, options);
12630
+ targetIndex = await appendFlattenedEntries(entry, depth - 1, result, options, targetIndex);
12603
12631
  continue;
12604
12632
  }
12605
- result.push(entry);
12606
- options.budget.allocateArrayLength(result.length);
12633
+ defineArrayResult(result, targetIndex++, entry, options);
12607
12634
  }
12635
+ return targetIndex;
12608
12636
  }
12609
12637
  async function sortArray(value, comparator, options, stack) {
12610
12638
  const length = value.length;
@@ -13059,12 +13087,33 @@ function createArrayGlobal(budget) {
13059
13087
  guest: true,
13060
13088
  sandbox: true,
13061
13089
  call: (args) => createArrayFromConstructorArgs(args, budget),
13062
- construct: (args) => createArrayFromConstructorArgs(args, budget),
13090
+ construct: async (args, context) => {
13091
+ const target = context?.newTarget;
13092
+ const parent = target === void 0 || target === constructor ? prototype : context?.getProperty === void 0 ? await readPropertyDescriptor(
13093
+ getSandboxPropertyDescriptor(target, "prototype", budget) ?? { value: void 0 },
13094
+ target,
13095
+ context
13096
+ ) : await context.getProperty(target, "prototype");
13097
+ const value = createArrayFromConstructorArgs(args, budget);
13098
+ if (parent !== prototype && typeof parent === "object" && parent !== null)
13099
+ setSandboxPrototype(value, parent, budget);
13100
+ return value;
13101
+ },
13063
13102
  name: "Array",
13064
13103
  length: 1
13065
13104
  });
13066
13105
  const properties = materializeFunctionProperties(constructor);
13067
13106
  Object.defineProperty(properties, "prototype", { value: prototype, writable: false });
13107
+ Object.defineProperty(properties, Symbol.species, {
13108
+ get: accessorAdapter(createSandboxClosure({
13109
+ guest: true,
13110
+ sandbox: true,
13111
+ name: "get [Symbol.species]",
13112
+ length: 0,
13113
+ call: (_args, context) => context?.thisValue
13114
+ }), "get"),
13115
+ configurable: true
13116
+ });
13068
13117
  Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
13069
13118
  for (const name of arrayMethodNames) {
13070
13119
  const method = createSandboxClosure({
@@ -13103,7 +13152,33 @@ function createArrayGlobal(budget) {
13103
13152
  const statics = {
13104
13153
  isArray: createSandboxClosure({ sandbox: true, name: "isArray", call: ([value]) => Array.isArray(value) }),
13105
13154
  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) })
13155
+ of: createSandboxClosure({
13156
+ guest: true,
13157
+ sandbox: true,
13158
+ name: "of",
13159
+ length: 0,
13160
+ call: async (args, context) => {
13161
+ const target = context?.thisValue;
13162
+ let result;
13163
+ const release = retainValues(budget, () => [...args, target, result]);
13164
+ try {
13165
+ result = isSandboxClosure(target) && target.construct !== void 0 ? await invokeBuiltinClosure(target, [args.length], budget, context, void 0, true) : createArrayFromConstructorArgs([args.length], budget);
13166
+ const checkData = createDataCheckpoint(budget, context);
13167
+ checkData(result, 0, true);
13168
+ for (let index = 0; index < args.length; index += 1) {
13169
+ budget.visitNode();
13170
+ if (Array.isArray(result)) budget.allocateArrayLength(index + 1);
13171
+ defineOwnDataProperty(objectProperties(result, true), String(index), args[index]);
13172
+ checkData(result, 0, true);
13173
+ }
13174
+ await setSandboxProperty(result, "length", args.length, budget, true, context);
13175
+ checkData(result, 0, true);
13176
+ return allocateProducedSandboxValue(result, budget);
13177
+ } finally {
13178
+ release();
13179
+ }
13180
+ }
13181
+ })
13107
13182
  };
13108
13183
  for (const [name, value] of Object.entries(statics))
13109
13184
  Object.defineProperty(properties, name, { value, writable: true, configurable: true });
@@ -13410,7 +13485,7 @@ async function arrayFromSandboxValues(args, budget, context) {
13410
13485
  }
13411
13486
  index += 1;
13412
13487
  }
13413
- setSandboxProperty(result, "length", index, budget);
13488
+ await setSandboxProperty(result, "length", index, budget, true, context);
13414
13489
  checkData(result, 0, true);
13415
13490
  return allocateProducedSandboxValue(result, budget);
13416
13491
  } finally {
@@ -13419,11 +13494,11 @@ async function arrayFromSandboxValues(args, budget, context) {
13419
13494
  }
13420
13495
  function createArrayFromConstructorArgs(args, budget) {
13421
13496
  if (args.length !== 1) {
13422
- return budgetSandboxValue(Reflect.apply(Array, Array, [...args]), budget);
13497
+ return allocateProducedSandboxValue([...args], budget);
13423
13498
  }
13424
13499
  const [lengthOrValue] = args;
13425
13500
  if (typeof lengthOrValue !== "number") {
13426
- return budgetSandboxValue([lengthOrValue], budget);
13501
+ return allocateProducedSandboxValue([lengthOrValue], budget);
13427
13502
  }
13428
13503
  if (!Number.isInteger(lengthOrValue) || lengthOrValue < 0 || lengthOrValue > 4294967295) {
13429
13504
  throw new RangeError("Invalid array length.");
@@ -40728,4 +40803,4 @@ export {
40728
40803
  FileSnapshotBackend,
40729
40804
  run
40730
40805
  };
40731
- //# sourceMappingURL=chunk-DB2DTDG3.js.map
40806
+ //# sourceMappingURL=chunk-2Y6SJCIT.js.map