poe-code 14.0.65 → 14.0.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/index.js +1723 -797
  2. package/dist/index.js.map +4 -4
  3. package/dist/metafile.json +1 -1
  4. package/package.json +1 -1
  5. package/packages/safe-js/dist/chunks/{chunk-4VT2M7WL.js → chunk-MKTUVVEB.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-WBWD7ZCM.js → chunk-YKISHTWT.js} +1675 -772
  7. package/packages/safe-js/dist/chunks/chunk-YKISHTWT.js.map +7 -0
  8. package/packages/safe-js/dist/cli.js +2 -2
  9. package/packages/safe-js/dist/core.js +1 -1
  10. package/packages/safe-js/dist/index.js +2 -2
  11. package/packages/safe-js/dist/interp/accessors.d.ts +9 -0
  12. package/packages/safe-js/dist/interp/accessors.js +68 -0
  13. package/packages/safe-js/dist/interp/async.js +3 -3
  14. package/packages/safe-js/dist/interp/classes.js +10 -4
  15. package/packages/safe-js/dist/interp/exceptions.d.ts +1 -1
  16. package/packages/safe-js/dist/interp/exceptions.js +47 -8
  17. package/packages/safe-js/dist/interp/globals/collections.js +53 -16
  18. package/packages/safe-js/dist/interp/globals/console-json.js +41 -26
  19. package/packages/safe-js/dist/interp/globals/object-array.js +229 -68
  20. package/packages/safe-js/dist/interp/globals/object.js +6 -3
  21. package/packages/safe-js/dist/interp/globals/primitives.js +11 -5
  22. package/packages/safe-js/dist/interp/interpreter.d.ts +2 -2
  23. package/packages/safe-js/dist/interp/interpreter.js +83 -44
  24. package/packages/safe-js/dist/interp/iteration.js +13 -0
  25. package/packages/safe-js/dist/interp/methods/array.d.ts +1 -1
  26. package/packages/safe-js/dist/interp/methods/array.js +368 -109
  27. package/packages/safe-js/dist/interp/methods/function.js +56 -20
  28. package/packages/safe-js/dist/interp/methods/regex.js +1 -1
  29. package/packages/safe-js/dist/interp/object-model.d.ts +1 -0
  30. package/packages/safe-js/dist/interp/object-model.js +48 -10
  31. package/packages/safe-js/dist/interp/patterns.d.ts +2 -2
  32. package/packages/safe-js/dist/interp/patterns.js +35 -16
  33. package/packages/safe-js/dist/interp/promise.d.ts +1 -1
  34. package/packages/safe-js/dist/interp/promise.js +103 -63
  35. package/packages/safe-js/dist/interp/string-coercion.d.ts +1 -0
  36. package/packages/safe-js/dist/interp/string-coercion.js +47 -37
  37. package/packages/safe-js/dist/interp/values.d.ts +2 -1
  38. package/packages/safe-js/dist/interp/values.js +26 -1
  39. package/packages/safe-js/dist/parse/parser.d.ts +1 -1
  40. package/packages/safe-js/dist/parse/parser.js +37 -8
  41. package/packages/safe-js/dist/chunks/chunk-WBWD7ZCM.js.map +0 -7
  42. /package/packages/safe-js/dist/chunks/{chunk-4VT2M7WL.js.map → chunk-MKTUVVEB.js.map} +0 -0
package/dist/index.js CHANGED
@@ -123913,7 +123913,7 @@ var init_parser3 = __esm({
123913
123913
  });
123914
123914
  });
123915
123915
  }
123916
- parseFunctionParts(generator, lexicalContext) {
123916
+ parseFunctionParts(generator, lexicalContext, accessor) {
123917
123917
  return this.withLexicalContext(lexicalContext, () => {
123918
123918
  const params = this.withScope(() => {
123919
123919
  const parsed = this.parseArrowParameters();
@@ -123921,7 +123921,24 @@ var init_parser3 = __esm({
123921
123921
  for (const identifier of boundIdentifiers(param)) this.declareBinding(identifier);
123922
123922
  return parsed;
123923
123923
  });
123924
+ if (accessor === "get" && params.length !== 0)
123925
+ throw new Error("A getter cannot have parameters.");
123926
+ if (accessor === "set" && (params.length !== 1 || params[0]?.type === "RestElement"))
123927
+ throw new Error("A setter must have exactly one non-rest parameter.");
123928
+ const bodyTokenIndex = this.index;
123924
123929
  const body = this.withFunctionContext(generator ? "generator" : "normal", () => this.parseBlockStatement(params));
123930
+ if (accessor === "set" && params[0]?.type !== "Identifier") {
123931
+ let directiveTokenIndex = bodyTokenIndex + 1;
123932
+ for (const statement of body.body) {
123933
+ const token = this.tokens[directiveTokenIndex];
123934
+ if (statement.type !== "ExpressionStatement" || statement.expression.type !== "StringLiteral" || token?.type !== "string" || statement.span.start.offset !== token.start.offset || statement.span.end.offset !== token.end.offset) break;
123935
+ if (statement.expression.raw === '"use strict"' || statement.expression.raw === "'use strict'")
123936
+ throw new Error(
123937
+ "A setter with a non-simple parameter cannot contain a use strict directive."
123938
+ );
123939
+ directiveTokenIndex += this.tokens[directiveTokenIndex + 1]?.value === ";" ? 2 : 1;
123940
+ }
123941
+ }
123925
123942
  return { params, body };
123926
123943
  });
123927
123944
  }
@@ -123952,8 +123969,14 @@ var init_parser3 = __esm({
123952
123969
  }
123953
123970
  const generator = this.consumePunctuator("*") !== void 0;
123954
123971
  if (async && generator) throw new DisallowedSyntaxError("async generator", methodStart.start);
123955
- if ((this.currentToken().value === "get" || this.currentToken().value === "set") && this.isObjectMethodStart())
123956
- throw new DisallowedSyntaxError("class accessor", this.currentToken().start);
123972
+ let accessor;
123973
+ const modifier = this.currentToken();
123974
+ if ((modifier.value === "get" || modifier.value === "set") && this.isObjectMethodStart()) {
123975
+ if (async || generator || modifier.end.offset - modifier.start.offset !== modifier.value.length)
123976
+ throw unexpectedTokenError(modifier);
123977
+ accessor = modifier.value;
123978
+ this.index++;
123979
+ }
123957
123980
  const computed = this.consumePunctuator("[") !== void 0;
123958
123981
  const key3 = computed ? this.parseExpression({ allowSequence: true }).node : this.currentToken().type === "string" ? createStringLiteral(this.tokens[this.index++]) : this.currentToken().type === "numeric" ? createNumericLiteral(this.tokens[this.index++]) : this.parseIdentifierName();
123959
123982
  if (computed) this.expectPunctuator("]");
@@ -123961,7 +123984,7 @@ var init_parser3 = __esm({
123961
123984
  if (isStatic && name === "prototype") throw new Error("A static class element cannot be named prototype.");
123962
123985
  if (this.currentToken().value === "(") {
123963
123986
  const constructor = !isStatic && name === "constructor";
123964
- if (constructor && (async || generator)) throw new Error("A class constructor cannot be async or a generator.");
123987
+ if (constructor && (async || generator || accessor !== void 0)) throw new Error("A class constructor must be an ordinary method.");
123965
123988
  const { params, body } = this.parseFunctionParts(generator, {
123966
123989
  newTarget: true,
123967
123990
  superProperty: true,
@@ -123970,7 +123993,7 @@ var init_parser3 = __esm({
123970
123993
  return: true,
123971
123994
  await: async,
123972
123995
  strictAwait: true
123973
- });
123996
+ }, accessor);
123974
123997
  const value2 = this.withFunctionSource({
123975
123998
  type: "FunctionExpression",
123976
123999
  async,
@@ -123985,7 +124008,7 @@ var init_parser3 = __esm({
123985
124008
  key: key3,
123986
124009
  computed,
123987
124010
  static: isStatic,
123988
- kind: constructor ? "constructor" : "method",
124011
+ kind: accessor ?? (constructor ? "constructor" : "method"),
123989
124012
  value: value2,
123990
124013
  span: createSpan2(start.start, value2.span.end)
123991
124014
  };
@@ -125233,7 +125256,10 @@ var init_parser3 = __esm({
125233
125256
  return (propertyToken.type === "identifier" || propertyToken.type === "keyword" || propertyToken.type === "numeric" || propertyToken.type === "string") && this.peekToken(2).type === "punctuator" && this.peekToken(2).value === "(";
125234
125257
  }
125235
125258
  parseObjectMethod(asyncToken, methodStart) {
125236
- const { params, body } = this.parseFunctionParts(false, ordinaryFunctionContext);
125259
+ const { params, body } = this.parseFunctionParts(false, {
125260
+ ...ordinaryFunctionContext,
125261
+ superProperty: true
125262
+ });
125237
125263
  return this.withFunctionSource({
125238
125264
  type: "FunctionExpression",
125239
125265
  async: asyncToken !== void 0,
@@ -126189,6 +126215,70 @@ var init_graph_depth = __esm({
126189
126215
  }
126190
126216
  });
126191
126217
 
126218
+ // packages/safe-js/src/interp/accessors.ts
126219
+ function accessorAdapter(closure, kind) {
126220
+ if (kind === "get") {
126221
+ let adapter2 = getterAdapters.get(closure);
126222
+ if (adapter2 === void 0) {
126223
+ adapter2 = () => void 0;
126224
+ getterAdapters.set(closure, adapter2);
126225
+ accessorClosures.set(adapter2, closure);
126226
+ }
126227
+ return adapter2;
126228
+ }
126229
+ let adapter = setterAdapters.get(closure);
126230
+ if (adapter === void 0) {
126231
+ adapter = () => {
126232
+ throw new TypeError("Accessor writes require sandbox execution.");
126233
+ };
126234
+ setterAdapters.set(closure, adapter);
126235
+ accessorClosures.set(adapter, closure);
126236
+ }
126237
+ return adapter;
126238
+ }
126239
+ function accessorClosure(adapter) {
126240
+ if (adapter === void 0) return void 0;
126241
+ const closure = accessorClosures.get(adapter);
126242
+ if (closure === void 0) throw new TypeError("Native accessors cannot execute in the sandbox.");
126243
+ return closure;
126244
+ }
126245
+ function retainedAccessorClosures(descriptor) {
126246
+ const closures = [];
126247
+ for (const adapter of [descriptor.get, descriptor.set]) {
126248
+ const closure = adapter === void 0 ? void 0 : accessorClosures.get(adapter);
126249
+ if (closure !== void 0) closures.push(closure);
126250
+ }
126251
+ return closures;
126252
+ }
126253
+ function readPropertyDescriptor(descriptor, receiver, context, allowNativeGetter = false) {
126254
+ if ("value" in descriptor) return descriptor.value;
126255
+ if (descriptor.get === void 0) return void 0;
126256
+ const getter = accessorClosures.get(descriptor.get);
126257
+ if (getter === void 0) {
126258
+ if (!allowNativeGetter) throw new TypeError("Native accessors cannot execute in the sandbox.");
126259
+ return Reflect.apply(descriptor.get, receiver, []);
126260
+ }
126261
+ if (context?.invokeClosure === void 0)
126262
+ throw new TypeError("Accessor reads require sandbox execution.");
126263
+ return context.invokeClosure(getter, [], receiver);
126264
+ }
126265
+ function writePropertyDescriptor(descriptor, receiver, value, context) {
126266
+ const setter = accessorClosure(descriptor.set);
126267
+ if (setter === void 0) throw new TypeError("Cannot assign to a getter-only property.");
126268
+ if (context?.invokeClosure === void 0)
126269
+ throw new TypeError("Accessor writes require sandbox execution.");
126270
+ return context.invokeClosure(setter, [value], receiver).then(() => void 0);
126271
+ }
126272
+ var accessorClosures, getterAdapters, setterAdapters;
126273
+ var init_accessors = __esm({
126274
+ "packages/safe-js/src/interp/accessors.ts"() {
126275
+ "use strict";
126276
+ accessorClosures = /* @__PURE__ */ new WeakMap();
126277
+ getterAdapters = /* @__PURE__ */ new WeakMap();
126278
+ setterAdapters = /* @__PURE__ */ new WeakMap();
126279
+ }
126280
+ });
126281
+
126192
126282
  // packages/safe-js/src/extensions.ts
126193
126283
  import { types as types2 } from "node:util";
126194
126284
  function readDataRecord(value, label) {
@@ -128858,7 +128948,7 @@ async function callRegexMethod(target, methodName, args, budget, context) {
128858
128948
  try {
128859
128949
  const input = await sandboxString(args[0], budget, context);
128860
128950
  if (methodName === "test") {
128861
- const exec2 = context?.getProperty === void 0 ? getSandboxDataProperty(target, "exec", budget) : context.getProperty(target, "exec");
128951
+ const exec2 = context?.getProperty === void 0 ? getSandboxDataProperty(target, "exec", budget) : await context.getProperty(target, "exec");
128862
128952
  if (isSandboxClosure(exec2)) {
128863
128953
  const result = await invokeBuiltinClosure(exec2, [input], budget, context, target);
128864
128954
  if (result !== null && typeof result !== "object") {
@@ -129008,12 +129098,30 @@ function sandboxString(value, budget, context, joining = /* @__PURE__ */ new Set
129008
129098
  }
129009
129099
  return objectToPrimitive(value, budget, context, joining, "string").then((primitive) => budget.allocateString(String(primitive)));
129010
129100
  }
129101
+ async function joinSandboxArray(value, length, separator, budget, context, joining = /* @__PURE__ */ new Set()) {
129102
+ if (joiningArrays.has(value)) return "";
129103
+ joiningArrays.add(value);
129104
+ let text5 = "";
129105
+ const release2 = retainValues(budget, () => [value, text5, separator]);
129106
+ try {
129107
+ for (let index = 0; index < length; index++) {
129108
+ budget.visitNode();
129109
+ const element = await readCoercionProperty(value, String(index), context);
129110
+ const part = element === null || element === void 0 ? "" : await sandboxString(element, budget, context, joining);
129111
+ text5 = budget.allocateString(text5 + (index === 0 ? "" : separator) + part);
129112
+ }
129113
+ return text5;
129114
+ } finally {
129115
+ release2();
129116
+ joiningArrays.delete(value);
129117
+ }
129118
+ }
129011
129119
  async function objectToPrimitive(value, budget, context, joining, hint) {
129012
129120
  const leaveCall = budget.enterCall();
129013
129121
  try {
129014
129122
  budget.visitNode();
129015
129123
  for (const name of hint === "string" ? ["toString", "valueOf"] : ["valueOf", "toString"]) {
129016
- const hook = conversionHook(value, name, budget);
129124
+ const hook = await conversionHook(value, name, budget, context);
129017
129125
  let result;
129018
129126
  if (hook === defaultStringHook) {
129019
129127
  result = await defaultToString(value, budget, context, joining);
@@ -129033,7 +129141,7 @@ async function objectToPrimitive(value, budget, context, joining, hint) {
129033
129141
  leaveCall();
129034
129142
  }
129035
129143
  }
129036
- function conversionHook(value, name, budget) {
129144
+ function conversionHook(value, name, budget, context) {
129037
129145
  const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) || isSandboxDate(value) || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) || isSandboxGenerator(value) || isGuestHostObject(value));
129038
129146
  let current = value;
129039
129147
  let depth = 0;
@@ -129041,9 +129149,7 @@ function conversionHook(value, name, budget) {
129041
129149
  const properties = isGuestClosure(current) ? getGuestFunctionProperties(current) : current;
129042
129150
  const descriptor = properties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(properties, name);
129043
129151
  if (descriptor !== void 0) {
129044
- if (!Object.hasOwn(descriptor, "value"))
129045
- throw new TypeError("String conversion requires sandbox data properties.");
129046
- return descriptor.value;
129152
+ return readPropertyDescriptor(descriptor, value, context);
129047
129153
  }
129048
129154
  const parent = getSandboxPrototype(current, budget);
129049
129155
  if (current === value && (implicitBuiltin || parent === null && !hasExplicitSandboxPrototype(value))) {
@@ -129062,7 +129168,8 @@ async function defaultToString(value, budget, context, joining) {
129062
129168
  if (isSandboxClosure(value)) return budget.allocateString(functionString(value));
129063
129169
  if (isSandboxMap(value)) return "[object Map]";
129064
129170
  if (isSandboxSet(value)) return "[object Set]";
129065
- if (isSandboxCollectionIterator(value)) return collectionIteratorState(value).collectionKind === "map" ? "[object Map Iterator]" : "[object Set Iterator]";
129171
+ if (isSandboxCollectionIterator(value))
129172
+ return collectionIteratorState(value).collectionKind === "map" ? "[object Map Iterator]" : "[object Set Iterator]";
129066
129173
  if (isSandboxGenerator(value)) return "[object Generator]";
129067
129174
  if (isSandboxRegex(value)) {
129068
129175
  return budget.allocateString(
@@ -129072,35 +129179,20 @@ async function defaultToString(value, budget, context, joining) {
129072
129179
  if (isSandboxDate(value)) return budget.allocateString(dateString(value));
129073
129180
  if (Array.isArray(value) || isFloat32Array(value)) {
129074
129181
  if (Object.hasOwn(value, "join")) {
129075
- const join21 = ownDataValue(value, "join");
129182
+ const join21 = await readCoercionProperty(value, "join", context);
129076
129183
  if (!isSandboxClosure(join21))
129077
129184
  return isFloat32Array(value) ? "[object Float32Array]" : "[object Array]";
129078
129185
  return invokeBuiltinClosure(join21, [], budget, context, value);
129079
129186
  }
129080
- if (joining.has(value)) return "";
129081
- joining.add(value);
129082
- let text5 = "";
129083
- const release2 = retainValues(budget, () => [text5]);
129084
- try {
129085
- const length = isFloat32Array(value) ? float32Storage(value).length : value.length;
129086
- for (let index = 0; index < length; index++) {
129087
- budget.visitNode();
129088
- const element = ownDataValue(value, String(index));
129089
- const part = element === null || element === void 0 ? "" : await sandboxString(element, budget, context, joining);
129090
- text5 = budget.allocateString(text5 + (index === 0 ? "" : ",") + part);
129091
- }
129092
- return text5;
129093
- } finally {
129094
- release2();
129095
- joining.delete(value);
129096
- }
129187
+ const length = isFloat32Array(value) ? float32Storage(value).length : value.length;
129188
+ return joinSandboxArray(value, length, ",", budget, context, joining);
129097
129189
  }
129098
129190
  if (sandboxErrorTypes.has(value)) {
129099
- const nameValue = ownDataValue(value, "name");
129191
+ const nameValue = await readCoercionProperty(value, "name", context);
129100
129192
  const name = nameValue === void 0 ? "Error" : await sandboxString(nameValue, budget, context, joining);
129101
129193
  const release2 = retainValues(budget, () => [name]);
129102
129194
  try {
129103
- const messageValue2 = ownDataValue(value, "message");
129195
+ const messageValue2 = await readCoercionProperty(value, "message", context);
129104
129196
  const message2 = messageValue2 === void 0 ? "" : await sandboxString(messageValue2, budget, context, joining);
129105
129197
  return name === "" ? message2 : message2 === "" ? name : `${name}: ${message2}`;
129106
129198
  } finally {
@@ -129109,18 +129201,17 @@ async function defaultToString(value, budget, context, joining) {
129109
129201
  }
129110
129202
  return isSandboxPromise(value) ? "[object Promise]" : "[object Object]";
129111
129203
  }
129112
- function ownDataValue(value, name) {
129204
+ function readCoercionProperty(value, name, context) {
129205
+ if (context?.getProperty !== void 0) return context.getProperty(value, name);
129113
129206
  const descriptor = Object.getOwnPropertyDescriptor(value, name);
129114
- if (descriptor !== void 0 && !Object.hasOwn(descriptor, "value")) {
129115
- throw new TypeError("String conversion requires sandbox data properties.");
129116
- }
129117
- return descriptor?.value;
129207
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, value, context);
129118
129208
  }
129119
- var defaultStringHook, defaultValueHook;
129209
+ var defaultStringHook, defaultValueHook, joiningArrays;
129120
129210
  var init_string_coercion = __esm({
129121
129211
  "packages/safe-js/src/interp/string-coercion.ts"() {
129122
129212
  "use strict";
129123
129213
  init_shape();
129214
+ init_accessors();
129124
129215
  init_date();
129125
129216
  init_builtin_call();
129126
129217
  init_float32();
@@ -129135,6 +129226,166 @@ var init_string_coercion = __esm({
129135
129226
  init_values();
129136
129227
  defaultStringHook = /* @__PURE__ */ Symbol("defaultStringHook");
129137
129228
  defaultValueHook = /* @__PURE__ */ Symbol("defaultValueHook");
129229
+ joiningArrays = /* @__PURE__ */ new WeakSet();
129230
+ }
129231
+ });
129232
+
129233
+ // packages/safe-js/src/interp/globals/object.ts
129234
+ function createObjectGlobal(methods, budget) {
129235
+ const construct = ([value]) => {
129236
+ if (value === null || value === void 0) {
129237
+ budget.chargeDataUsage(1);
129238
+ return /* @__PURE__ */ Object.create(null);
129239
+ }
129240
+ if (typeof value !== "object") {
129241
+ const box = createSandboxBox(value);
129242
+ budget.chargeDataUsage(measureSandboxData([box]));
129243
+ return box;
129244
+ }
129245
+ return value;
129246
+ };
129247
+ const constructor = createSandboxClosure({
129248
+ guest: true,
129249
+ sandbox: true,
129250
+ name: "Object",
129251
+ length: 1,
129252
+ call: construct,
129253
+ construct: (args, context) => {
129254
+ if (context?.newTarget === void 0 || context.newTarget === constructor)
129255
+ return construct(args);
129256
+ const value = construct([]);
129257
+ const prototype2 = context.getProperty(context.newTarget, "prototype");
129258
+ const finish = (prototype3) => {
129259
+ if (typeof prototype3 === "object" && prototype3 !== null)
129260
+ setSandboxPrototype(value, prototype3, budget);
129261
+ return value;
129262
+ };
129263
+ return prototype2 instanceof Promise ? prototype2.then(finish) : finish(prototype2);
129264
+ }
129265
+ });
129266
+ const properties = materializeFunctionProperties(constructor);
129267
+ const prototype = properties.prototype;
129268
+ Object.defineProperty(properties, "prototype", { writable: false });
129269
+ for (const [name, method] of Object.entries(methods)) {
129270
+ Object.defineProperty(properties, name, { value: method, writable: true, configurable: true });
129271
+ }
129272
+ const prototypeMethods = {
129273
+ toString: createSandboxClosure({
129274
+ sandbox: true,
129275
+ name: "toString",
129276
+ length: 0,
129277
+ call: (_args, context) => budget.allocateString(`[object ${typeTag(context?.thisValue)}]`)
129278
+ }),
129279
+ valueOf: createSandboxClosure({
129280
+ sandbox: true,
129281
+ name: "valueOf",
129282
+ length: 0,
129283
+ call: (_args, context) => {
129284
+ const value = requireReceiver(context?.thisValue);
129285
+ return construct([value]);
129286
+ }
129287
+ }),
129288
+ hasOwnProperty: createSandboxClosure({
129289
+ sandbox: true,
129290
+ name: "hasOwnProperty",
129291
+ length: 1,
129292
+ call: async ([key3], context) => hasOwnSandboxProperty(
129293
+ requireReceiver(context?.thisValue),
129294
+ await sandboxString(key3, budget, context),
129295
+ false
129296
+ )
129297
+ }),
129298
+ propertyIsEnumerable: createSandboxClosure({
129299
+ sandbox: true,
129300
+ name: "propertyIsEnumerable",
129301
+ length: 1,
129302
+ call: async ([key3], context) => hasOwnSandboxProperty(
129303
+ requireReceiver(context?.thisValue),
129304
+ await sandboxString(key3, budget, context),
129305
+ true
129306
+ )
129307
+ }),
129308
+ isPrototypeOf: createSandboxClosure({
129309
+ sandbox: true,
129310
+ name: "isPrototypeOf",
129311
+ length: 1,
129312
+ call: ([value], context) => {
129313
+ if (typeof value !== "object" || value === null) return false;
129314
+ const receiver = requireReceiver(context?.thisValue);
129315
+ let depth = 0;
129316
+ for (let current = getSandboxPrototype(value, budget); current !== null; current = getSandboxPrototype(current, budget)) {
129317
+ budget.visitNode();
129318
+ assertSandboxDataDepth(depth++);
129319
+ if (current === receiver) return true;
129320
+ }
129321
+ return false;
129322
+ }
129323
+ })
129324
+ };
129325
+ for (const [name, method] of Object.entries(prototypeMethods)) {
129326
+ Object.defineProperty(prototype, name, { value: method, writable: true, configurable: true });
129327
+ }
129328
+ markDescriptorObject(prototype);
129329
+ installObjectPrototype(budget, prototype, constructor);
129330
+ return constructor;
129331
+ }
129332
+ function requireReceiver(value) {
129333
+ if (value === null || value === void 0)
129334
+ throw new TypeError("Object method requires a non-null receiver.");
129335
+ return value;
129336
+ }
129337
+ function hasOwnSandboxProperty(value, key3, enumerable) {
129338
+ requireReceiver(value);
129339
+ if (isGuestHostObject(value)) return hasHostObjectMember(value, key3, enumerable);
129340
+ let properties;
129341
+ if (isGuestClosure(value)) properties = materializeFunctionProperties(value);
129342
+ else if (isSandboxClosure(value)) {
129343
+ if (key3 === "length" || key3 === "name") return !enumerable;
129344
+ properties = value.properties ?? /* @__PURE__ */ Object.create(null);
129345
+ } else if (isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value) || isSandboxGenerator(value))
129346
+ return false;
129347
+ else if (isSandboxRegex(value)) return key3 === "lastIndex" && !enumerable;
129348
+ else properties = Object(value);
129349
+ const descriptor = Object.getOwnPropertyDescriptor(properties, key3);
129350
+ return descriptor !== void 0 && (!enumerable || descriptor.enumerable === true);
129351
+ }
129352
+ function typeTag(value) {
129353
+ if (isSandboxBox(value)) value = boxedValue(value);
129354
+ if (value === void 0) return "Undefined";
129355
+ if (value === null) return "Null";
129356
+ if (typeof value === "string") return "String";
129357
+ if (typeof value === "number") return "Number";
129358
+ if (typeof value === "boolean") return "Boolean";
129359
+ if (isSandboxClosure(value)) {
129360
+ while (value.boundTarget !== void 0) value = value.boundTarget;
129361
+ return value.generator ? "GeneratorFunction" : value.async ? "AsyncFunction" : "Function";
129362
+ }
129363
+ if (Array.isArray(value)) return "Array";
129364
+ if (isSandboxDate(value)) return "Date";
129365
+ if (isSandboxErrorConstructorInstance(value, "Error")) return "Error";
129366
+ if (isSandboxRegex(value)) return "RegExp";
129367
+ if (isSandboxMap(value)) return "Map";
129368
+ if (isSandboxSet(value)) return "Set";
129369
+ if (isSandboxCollectionIterator(value)) return collectionIteratorState(value).collectionKind === "map" ? "Map Iterator" : "Set Iterator";
129370
+ if (isSandboxPromise(value)) return "Promise";
129371
+ if (isSandboxGenerator(value)) return "Generator";
129372
+ if (isFloat32Array(value)) return "Float32Array";
129373
+ return "Object";
129374
+ }
129375
+ var init_object2 = __esm({
129376
+ "packages/safe-js/src/interp/globals/object.ts"() {
129377
+ "use strict";
129378
+ init_graph_depth();
129379
+ init_date();
129380
+ init_boxed();
129381
+ init_exceptions();
129382
+ init_float32();
129383
+ init_host_capabilities();
129384
+ init_collection_iterator();
129385
+ init_object_model();
129386
+ init_string_coercion();
129387
+ init_values();
129388
+ init_values();
129138
129389
  }
129139
129390
  });
129140
129391
 
@@ -129523,12 +129774,34 @@ async function bindArrayPattern(pattern, value, context, evaluateNode3) {
129523
129774
  if (!Array.isArray(value)) {
129524
129775
  throw new TypeError("Array catch bindings require an array value.");
129525
129776
  }
129777
+ let cursor2 = 0;
129778
+ let done = false;
129779
+ const next = async () => {
129780
+ if (done || cursor2 >= value.length) {
129781
+ done = true;
129782
+ return { done: true, value: void 0 };
129783
+ }
129784
+ const key3 = cursor2++;
129785
+ return {
129786
+ done: false,
129787
+ value: context.getProperty === void 0 ? getSandboxDataProperty(value, key3, context.budget) : await context.getProperty(value, key3)
129788
+ };
129789
+ };
129526
129790
  for (let index = 0; index < pattern.elements.length; index += 1) {
129527
129791
  const element = pattern.elements[index];
129528
129792
  if (element === null) {
129793
+ await next();
129529
129794
  continue;
129530
129795
  }
129531
- const elementValue = element.type === "RestElement" ? value.slice(index) : value[index];
129796
+ let elementValue;
129797
+ if (element.type === "RestElement") {
129798
+ const rest = [];
129799
+ for (let entry = await next(); !entry.done; entry = await next()) {
129800
+ context.budget.allocateArrayLength(rest.length + 1);
129801
+ rest.push(entry.value);
129802
+ }
129803
+ elementValue = rest;
129804
+ } else elementValue = (await next()).value;
129532
129805
  const binding = await bindPattern(element, elementValue, context, evaluateNode3);
129533
129806
  if (!binding.ok) {
129534
129807
  return binding;
@@ -129543,7 +129816,7 @@ async function bindObjectPattern(pattern, value, context, evaluateNode3) {
129543
129816
  const excludedKeys = /* @__PURE__ */ new Set();
129544
129817
  for (const property of pattern.properties) {
129545
129818
  if (property.type === "RestElement") {
129546
- const restValue = copyObjectRest(value, excludedKeys);
129819
+ const restValue = await copyObjectRest(value, excludedKeys, context);
129547
129820
  const binding2 = await bindPattern(property, restValue, context, evaluateNode3);
129548
129821
  if (!binding2.ok) {
129549
129822
  return binding2;
@@ -129557,7 +129830,7 @@ async function bindObjectPattern(pattern, value, context, evaluateNode3) {
129557
129830
  excludedKeys.add(String(key3.value));
129558
129831
  const binding = await bindPattern(
129559
129832
  property.value,
129560
- context.getProperty === void 0 ? getSandboxDataProperty(value, key3.value, context.budget) : context.getProperty(value, key3.value),
129833
+ context.getProperty === void 0 ? getSandboxDataProperty(value, key3.value, context.budget) : await context.getProperty(value, key3.value),
129561
129834
  context,
129562
129835
  evaluateNode3
129563
129836
  );
@@ -129601,12 +129874,18 @@ function getStaticPropertyKey(property) {
129601
129874
  throw new TypeError(`Unsupported catch binding property key '${property.type}'.`);
129602
129875
  }
129603
129876
  }
129604
- function copyObjectRest(value, excludedKeys) {
129877
+ async function copyObjectRest(value, excludedKeys, context) {
129605
129878
  const rest = /* @__PURE__ */ Object.create(null);
129606
- for (const [key3, entryValue] of ownEnumerableSandboxEntries(value, excludedKeys)) {
129607
- rest[key3] = entryValue;
129879
+ const release2 = retainValues(context.budget, () => [value, rest]);
129880
+ try {
129881
+ for (const key3 of ownEnumerableSandboxKeys(value)) {
129882
+ if (excludedKeys.has(key3) || !hasOwnSandboxProperty(value, key3, true)) continue;
129883
+ rest[key3] = context.getProperty === void 0 ? getSandboxDataProperty(value, key3, context.budget) : await context.getProperty(value, key3);
129884
+ }
129885
+ return rest;
129886
+ } finally {
129887
+ release2();
129608
129888
  }
129609
- return rest;
129610
129889
  }
129611
129890
  var capturedExceptionBrand;
129612
129891
  var init_exceptions = __esm({
@@ -129617,6 +129896,8 @@ var init_exceptions = __esm({
129617
129896
  init_host_call();
129618
129897
  init_promise_tracker();
129619
129898
  init_values();
129899
+ init_object2();
129900
+ init_resources();
129620
129901
  init_object_model();
129621
129902
  init_property_key();
129622
129903
  capturedExceptionBrand = /* @__PURE__ */ Symbol("CapturedException");
@@ -129703,6 +129984,18 @@ function getSandboxIterator(value, budget, context) {
129703
129984
  if (isSandboxSet(value)) {
129704
129985
  return collectionIterator(value.values);
129705
129986
  }
129987
+ if (Array.isArray(value) && context?.getProperty !== void 0) {
129988
+ let index = 0;
129989
+ return {
129990
+ asynchronous: true,
129991
+ snapshotIndex: () => index,
129992
+ next: async () => {
129993
+ if (index >= value.length) return { done: true, value: void 0 };
129994
+ budget?.visitNode();
129995
+ return { done: false, value: await context.getProperty(value, index++) };
129996
+ }
129997
+ };
129998
+ }
129706
129999
  if (typeof value !== "object" && typeof value !== "function" || value === null) {
129707
130000
  return void 0;
129708
130001
  }
@@ -130113,7 +130406,7 @@ async function callPromiseClosure(callback, args, thisValue, budget, context) {
130113
130406
  const values = args.map(
130114
130407
  (value) => value instanceof Error && !(value instanceof SandboxError) ? coerceThrownValue(value, budget, stack) : value
130115
130408
  );
130116
- let result = callback.call(values, { stack, thisValue });
130409
+ let result = callback.call(values, { ...context, stack, thisValue, newTarget: void 0 });
130117
130410
  if (callback.async !== true) result = await result;
130118
130411
  else if (isPromiseLike3(result)) result = createSandboxPromise(Promise.resolve(result));
130119
130412
  if (isSandboxPromise(result) && result.synchronousPrefix !== void 0) {
@@ -130140,11 +130433,11 @@ function getPromisePrototype(budget) {
130140
130433
  target.promise.then(
130141
130434
  (value) => {
130142
130435
  consumeSettledHostCall(target);
130143
- return runPromiseReaction(onFulfilled, value, "fulfilled", budget, chained);
130436
+ return runPromiseReaction(onFulfilled, value, "fulfilled", budget, chained, context);
130144
130437
  },
130145
130438
  (reason) => {
130146
130439
  consumeSettledHostCall(target);
130147
- return runPromiseReaction(onRejected, reason, "rejected", budget, chained);
130440
+ return runPromiseReaction(onRejected, reason, "rejected", budget, chained, context);
130148
130441
  }
130149
130442
  )
130150
130443
  );
@@ -130156,12 +130449,18 @@ function getPromisePrototype(budget) {
130156
130449
  sandbox: true,
130157
130450
  call: ([onRejected], context) => {
130158
130451
  const target = context?.thisValue;
130159
- const then = readPromiseReceiverProperty(target, "then", prototype);
130160
- if (!isSandboxClosure(then)) throw new TypeError("Promise.catch requires a callable then.");
130161
- return then.call([void 0, onRejected], {
130162
- stack: context?.stack ?? [],
130163
- thisValue: target
130164
- });
130452
+ const invoke2 = (then2) => {
130453
+ if (!isSandboxClosure(then2))
130454
+ throw new TypeError("Promise.catch requires a callable then.");
130455
+ return then2.call([void 0, onRejected], {
130456
+ ...context,
130457
+ stack: context?.stack ?? [],
130458
+ thisValue: target,
130459
+ newTarget: void 0
130460
+ });
130461
+ };
130462
+ const then = readPromiseReceiverProperty(target, "then", prototype, context);
130463
+ return then instanceof Promise ? then.then(invoke2) : invoke2(then);
130165
130464
  },
130166
130465
  name: "catch"
130167
130466
  }),
@@ -130172,46 +130471,57 @@ function getPromisePrototype(budget) {
130172
130471
  if (typeof target !== "object" || target === null) {
130173
130472
  throw new TypeError("Promise.finally requires an object receiver.");
130174
130473
  }
130175
- validatePromiseConstructorProperty(target, prototype);
130176
- const then = readPromiseReceiverProperty(target, "then", prototype);
130177
- if (!isSandboxClosure(then))
130178
- throw new TypeError("Promise.finally requires a callable then.");
130179
- const handlers = isSandboxClosure(onFinally) ? ["fulfilled", "rejected"].map(
130180
- (state) => createSandboxClosure({
130181
- sandbox: true,
130182
- retainedValues: () => [onFinally],
130183
- call: async ([value]) => {
130184
- const result = await callPromiseClosure(
130185
- onFinally,
130186
- [],
130187
- void 0,
130188
- budget,
130189
- context
130190
- );
130191
- const pending = isSandboxPromise(result) && getPromiseMember("constructor", budget) === intrinsicPromiseConstructors.get(budget) ? result : createSandboxPromise(resolveSandboxValue(result, { budget }));
130192
- const cleanupThen = getPromiseMember("then", budget);
130193
- if (!isSandboxClosure(cleanupThen))
130194
- throw new TypeError("Promise cleanup requires a callable then.");
130195
- return callPromiseClosure(
130196
- cleanupThen,
130197
- [
130198
- createSandboxClosure({
130199
- sandbox: true,
130200
- retainedValues: () => [value],
130201
- call: () => {
130202
- if (state === "rejected") throw value;
130203
- return value;
130204
- }
130205
- })
130206
- ],
130207
- pending,
130208
- budget,
130209
- context
130210
- );
130211
- }
130212
- })
130213
- ) : [onFinally, onFinally];
130214
- return then.call(handlers, { stack: context?.stack ?? [], thisValue: target });
130474
+ const invoke2 = (then) => {
130475
+ if (!isSandboxClosure(then))
130476
+ throw new TypeError("Promise.finally requires a callable then.");
130477
+ const handlers = isSandboxClosure(onFinally) ? ["fulfilled", "rejected"].map(
130478
+ (state) => createSandboxClosure({
130479
+ sandbox: true,
130480
+ retainedValues: () => [onFinally],
130481
+ call: async ([value]) => {
130482
+ const result = await callPromiseClosure(
130483
+ onFinally,
130484
+ [],
130485
+ void 0,
130486
+ budget,
130487
+ context
130488
+ );
130489
+ const pending = isSandboxPromise(result) && getPromiseMember("constructor", budget) === intrinsicPromiseConstructors.get(budget) ? result : createSandboxPromise(resolveSandboxValue(result, { budget }));
130490
+ const cleanupThen = getPromiseMember("then", budget);
130491
+ if (!isSandboxClosure(cleanupThen))
130492
+ throw new TypeError("Promise cleanup requires a callable then.");
130493
+ return callPromiseClosure(
130494
+ cleanupThen,
130495
+ [
130496
+ createSandboxClosure({
130497
+ sandbox: true,
130498
+ retainedValues: () => [value],
130499
+ call: () => {
130500
+ if (state === "rejected") throw value;
130501
+ return value;
130502
+ }
130503
+ })
130504
+ ],
130505
+ pending,
130506
+ budget,
130507
+ context
130508
+ );
130509
+ }
130510
+ })
130511
+ ) : [onFinally, onFinally];
130512
+ return then.call(handlers, {
130513
+ ...context,
130514
+ stack: context?.stack ?? [],
130515
+ thisValue: target,
130516
+ newTarget: void 0
130517
+ });
130518
+ };
130519
+ const finish = () => {
130520
+ const then = readPromiseReceiverProperty(target, "then", prototype, context);
130521
+ return then instanceof Promise ? then.then(invoke2) : invoke2(then);
130522
+ };
130523
+ const validation = validatePromiseConstructorProperty(target, prototype, context);
130524
+ return validation instanceof Promise ? validation.then(finish) : finish();
130215
130525
  },
130216
130526
  name: "finally"
130217
130527
  })
@@ -130222,15 +130532,19 @@ function getPromisePrototype(budget) {
130222
130532
  promisePrototypes.set(budget, prototype);
130223
130533
  return prototype;
130224
130534
  }
130225
- function readPromiseReceiverProperty(receiver, property, prototype) {
130535
+ function readPromiseReceiverProperty(receiver, property, prototype, context) {
130536
+ if (!isSandboxPromise(receiver) && context?.getProperty !== void 0)
130537
+ return context.getProperty(receiver, property);
130226
130538
  const properties = isSandboxPromise(receiver) ? prototype : isSandboxClosure(receiver) ? receiver.properties : receiver;
130227
130539
  return typeof properties === "object" && properties !== null && Object.hasOwn(properties, property) ? properties[property] : void 0;
130228
130540
  }
130229
- function validatePromiseConstructorProperty(receiver, prototype) {
130230
- const constructor = readPromiseReceiverProperty(receiver, "constructor", prototype);
130231
- if (constructor !== void 0 && (typeof constructor !== "object" || constructor === null)) {
130232
- throw new TypeError("Promise constructor property must be an object.");
130233
- }
130541
+ function validatePromiseConstructorProperty(receiver, prototype, context) {
130542
+ const validate2 = (constructor2) => {
130543
+ if (constructor2 !== void 0 && (typeof constructor2 !== "object" || constructor2 === null))
130544
+ throw new TypeError("Promise constructor property must be an object.");
130545
+ };
130546
+ const constructor = readPromiseReceiverProperty(receiver, "constructor", prototype, context);
130547
+ return constructor instanceof Promise ? constructor.then(validate2) : validate2(constructor);
130234
130548
  }
130235
130549
  async function settleIterable(iterable, method, budget, constructor, context) {
130236
130550
  const capability = await createPromiseCapability(constructor, budget, context);
@@ -130265,7 +130579,7 @@ async function settleIterable(iterable, method, budget, constructor, context) {
130265
130579
  }
130266
130580
  };
130267
130581
  try {
130268
- const promiseResolve = readPromiseReceiverProperty(constructor, "resolve", prototype);
130582
+ const promiseResolve = await readPromiseReceiverProperty(constructor, "resolve", prototype, context);
130269
130583
  if (!isSandboxClosure(promiseResolve))
130270
130584
  throw new TypeError("Promise constructor requires a callable resolve.");
130271
130585
  const iterator = getSandboxIterator(iterable, budget, context);
@@ -130315,7 +130629,7 @@ async function settleIterable(iterable, method, budget, constructor, context) {
130315
130629
  });
130316
130630
  });
130317
130631
  remaining++;
130318
- const then = readPromiseReceiverProperty(entry, "then", prototype);
130632
+ const then = await readPromiseReceiverProperty(entry, "then", prototype, context);
130319
130633
  if (!isSandboxClosure(then))
130320
130634
  throw new TypeError("Promise resolver result requires a callable then.");
130321
130635
  await callPromiseClosure(then, handlers, entry, budget, context);
@@ -130402,7 +130716,11 @@ function resolveSandboxValueNow(value, options) {
130402
130716
  }
130403
130717
  );
130404
130718
  }
130405
- const then = getThenable(value);
130719
+ const then = getThenable(value, options.budget);
130720
+ if (then instanceof Promise)
130721
+ return then.then(
130722
+ (method) => method === void 0 ? budgetIfNeeded(value, options.budget) : resolveThenable(value, method, options)
130723
+ );
130406
130724
  if (then !== void 0) {
130407
130725
  return resolveThenable(value, then, options);
130408
130726
  }
@@ -130422,7 +130740,7 @@ function resolveThenable(value, then, options) {
130422
130740
  try {
130423
130741
  if (settlement.state === "fulfilled") {
130424
130742
  resolve16(
130425
- isSandboxPromise(settlement.value) || getThenable(settlement.value) !== void 0 ? resolveSandboxValueNow(settlement.value, options) : budgetIfNeeded(settlement.value, options.budget)
130743
+ requiresPromiseResolution(settlement.value, options.budget) ? resolveSandboxValueNow(settlement.value, options) : budgetIfNeeded(settlement.value, options.budget)
130426
130744
  );
130427
130745
  } else {
130428
130746
  reject(budgetIfNeeded(settlement.value, options.budget));
@@ -130478,7 +130796,7 @@ function resolveThenable(value, then, options) {
130478
130796
  ).catch(reject);
130479
130797
  });
130480
130798
  }
130481
- function runPromiseReaction(handler, value, state, budget, self) {
130799
+ function runPromiseReaction(handler, value, state, budget, self, context) {
130482
130800
  return new Promise((resolve16, reject) => {
130483
130801
  if (state === "rejected" && value instanceof SandboxError && (value.code === "budgetExceeded" || value.code === "reentry")) {
130484
130802
  reject(value);
@@ -130492,14 +130810,14 @@ function runPromiseReaction(handler, value, state, budget, self) {
130492
130810
  reject(
130493
130811
  createSubsetErrorValue("TypeError", "Promise cannot resolve to itself.", [], budget)
130494
130812
  );
130495
- } else if (isSandboxPromise(result) || getThenable(result) !== void 0) {
130813
+ } else if (requiresPromiseResolution(result, budget)) {
130496
130814
  resolve16(resolvePromiseResult(result, budget, self));
130497
130815
  } else {
130498
130816
  resolve16(budgetSandboxValue(result, budget));
130499
130817
  }
130500
130818
  };
130501
130819
  if (isSandboxClosure(handler)) {
130502
- callInPromiseJob(handler, [argument], void 0, { fulfilled, rejected: reject }).catch(
130820
+ callInPromiseJob(handler, [argument], void 0, { fulfilled, rejected: reject }, context).catch(
130503
130821
  reject
130504
130822
  );
130505
130823
  } else {
@@ -130510,10 +130828,10 @@ function runPromiseReaction(handler, value, state, budget, self) {
130510
130828
  }
130511
130829
  });
130512
130830
  }
130513
- function callInPromiseJob(handler, args, thisValue = void 0, completion) {
130831
+ function callInPromiseJob(handler, args, thisValue = void 0, completion, context) {
130514
130832
  return runPromiseJob(async () => {
130515
130833
  try {
130516
- let result = handler.call(args, { stack: [], thisValue });
130834
+ let result = handler.call(args, { ...context, stack: [], thisValue, newTarget: void 0 });
130517
130835
  if (handler.async !== true) result = await result;
130518
130836
  if (isSandboxPromise(result) && result.synchronousPrefix !== void 0) {
130519
130837
  await result.synchronousPrefix;
@@ -130546,7 +130864,7 @@ function resolvePromiseResult(result, budget, self) {
130546
130864
  settled = true;
130547
130865
  try {
130548
130866
  if (state === "rejected") reject(budgetSandboxValue(value, budget));
130549
- else if (isSandboxPromise(value) || getThenable(value) !== void 0) {
130867
+ else if (requiresPromiseResolution(value, budget)) {
130550
130868
  resolve16(resolvePromiseResult(value, budget, self));
130551
130869
  } else {
130552
130870
  resolve16(budgetSandboxValue(value, budget));
@@ -130594,11 +130912,23 @@ function resolvePromiseResult(result, budget, self) {
130594
130912
  function isSelfResolution(result, self) {
130595
130913
  return self !== void 0 && (result === self || isSandboxPromise(result) && result.promise === self.promise);
130596
130914
  }
130597
- function getThenable(value) {
130915
+ function requiresPromiseResolution(value, budget) {
130916
+ if (isSandboxPromise(value)) return true;
130917
+ const descriptor = getSandboxPropertyDescriptor(value, "then", budget);
130918
+ return descriptor !== void 0 && (!("value" in descriptor) || isSandboxClosure(descriptor.value));
130919
+ }
130920
+ function getThenable(value, budget) {
130598
130921
  if (typeof value !== "object" || value === null || isSandboxPromise(value)) {
130599
130922
  return void 0;
130600
130923
  }
130601
- const then = isSandboxClosure(value) ? value.properties?.then : value.then;
130924
+ const descriptor = getSandboxPropertyDescriptor(value, "then", budget);
130925
+ if (descriptor !== void 0 && !("value" in descriptor)) {
130926
+ const getter = accessorClosure(descriptor.get);
130927
+ if (getter === void 0) return void 0;
130928
+ const result = budget === void 0 ? getter.call([], { stack: [], thisValue: value }) : callPromiseClosure(getter, [], value, budget);
130929
+ return Promise.resolve(result).then((then2) => isSandboxClosure(then2) ? then2 : void 0);
130930
+ }
130931
+ const then = descriptor?.value;
130602
130932
  return isSandboxClosure(then) ? then : void 0;
130603
130933
  }
130604
130934
  function budgetIfNeeded(value, budget) {
@@ -130639,6 +130969,8 @@ var init_promise = __esm({
130639
130969
  "packages/safe-js/src/interp/promise.ts"() {
130640
130970
  "use strict";
130641
130971
  init_budget2();
130972
+ init_accessors();
130973
+ init_object_model();
130642
130974
  init_exceptions();
130643
130975
  init_iteration();
130644
130976
  init_resources();
@@ -130913,6 +131245,13 @@ function ownEnumerableSandboxEntries(value, excludedKeys) {
130913
131245
  else entries = Object.entries(Object(value));
130914
131246
  return excludedKeys === void 0 ? entries : entries.filter(([key3]) => !excludedKeys.has(key3));
130915
131247
  }
131248
+ function ownEnumerableSandboxKeys(value) {
131249
+ if (isGuestHostObject(value)) return getHostObjectKeys(value);
131250
+ if (value === null || value === void 0) throw new TypeError("Cannot convert undefined or null to object.");
131251
+ if (isGuestClosure(value)) return Object.keys(value.properties ?? {});
131252
+ if (isSandboxClosure(value) || isSandboxGenerator(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value) || isSandboxRegex(value)) return [];
131253
+ return Object.keys(Object(value));
131254
+ }
130916
131255
  function createSandboxPromise(promise, metadata = {}) {
130917
131256
  const original = metadata.trackReplay === false ? promise : promiseReplayContext.getStore()?.track(promise) ?? promise;
130918
131257
  const sandboxPromise = {
@@ -131067,6 +131406,7 @@ function measureSandboxData(values, options = {}) {
131067
131406
  for (const [key3, descriptor] of boxedDataProperties(value)) {
131068
131407
  usage += key3.length + 1;
131069
131408
  if ("value" in descriptor) visit(descriptor.value, depth + 1);
131409
+ else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
131070
131410
  }
131071
131411
  return;
131072
131412
  }
@@ -131099,6 +131439,7 @@ function measureSandboxData(values, options = {}) {
131099
131439
  if (key3 === "length") continue;
131100
131440
  usage += key3.length + 1;
131101
131441
  if ("value" in descriptor) visit(descriptor.value, depth + 1);
131442
+ else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
131102
131443
  }
131103
131444
  return;
131104
131445
  }
@@ -131137,6 +131478,7 @@ function measureSandboxData(values, options = {}) {
131137
131478
  if (key3 === "prototype" || key3 === "name" || key3 === "length") continue;
131138
131479
  usage += key3.length + 1;
131139
131480
  if ("value" in descriptor) visit(descriptor.value, depth + 1);
131481
+ else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
131140
131482
  }
131141
131483
  } else visit(value.properties, depth + 1);
131142
131484
  }
@@ -131180,7 +131522,8 @@ function measureSandboxData(values, options = {}) {
131180
131522
  const descriptor = descriptors[key3];
131181
131523
  if (!descriptor.enumerable && !hasManagedDescriptors(value)) continue;
131182
131524
  usage += key3.length;
131183
- visit("value" in descriptor ? descriptor.value : void 0, depth + 1);
131525
+ if ("value" in descriptor) visit(descriptor.value, depth + 1);
131526
+ else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
131184
131527
  }
131185
131528
  };
131186
131529
  for (const value of values) visit(value);
@@ -131799,6 +132142,7 @@ var init_values = __esm({
131799
132142
  "packages/safe-js/src/interp/values.ts"() {
131800
132143
  "use strict";
131801
132144
  init_otel2();
132145
+ init_accessors();
131802
132146
  init_collection_brands();
131803
132147
  init_collection_iterator();
131804
132148
  init_date();
@@ -131905,15 +132249,23 @@ function registerIntrinsicPrototype(budget, prototype, constructor) {
131905
132249
  ...record2,
131906
132250
  descriptors: new Map(Object.entries(Object.getOwnPropertyDescriptors(record2.value)))
131907
132251
  }));
131908
- const unchanged = (before, after) => before !== void 0 && after !== void 0 && Object.is(before.value, after.value) && before.writable === after.writable && before.configurable === after.configurable && before.enumerable === after.enumerable;
131909
- intrinsicConstructors.set(constructor, () => records.every(({ target, value, descriptors, prototype: parent, explicit }) => {
131910
- const current = Object.getOwnPropertyDescriptors(value);
131911
- return getSandboxPrototype(target) === parent && hasExplicitSandboxPrototype(target) === explicit && Object.keys(current).length === descriptors.size && Object.keys(current).every((key3) => unchanged(descriptors.get(key3), current[key3]));
131912
- }));
131913
- budget.setRetainedValues(prototype, () => records.flatMap(({ target, value, descriptors, prototype: parent }) => [
131914
- ...getSandboxPrototype(target) === parent ? [] : [getSandboxPrototype(target)],
131915
- ...Object.entries(Object.getOwnPropertyDescriptors(value)).flatMap(([key3, descriptor]) => unchanged(descriptors.get(key3), descriptor) ? [] : [key3, descriptor.value])
131916
- ]));
132252
+ const unchanged = (before, after) => before !== void 0 && after !== void 0 && Object.is(before.value, after.value) && before.get === after.get && before.set === after.set && before.writable === after.writable && before.configurable === after.configurable && before.enumerable === after.enumerable;
132253
+ intrinsicConstructors.set(
132254
+ constructor,
132255
+ () => records.every(({ target, value, descriptors, prototype: parent, explicit }) => {
132256
+ const current = Object.getOwnPropertyDescriptors(value);
132257
+ return getSandboxPrototype(target) === parent && hasExplicitSandboxPrototype(target) === explicit && Object.keys(current).length === descriptors.size && Object.keys(current).every((key3) => unchanged(descriptors.get(key3), current[key3]));
132258
+ })
132259
+ );
132260
+ budget.setRetainedValues(
132261
+ prototype,
132262
+ () => records.flatMap(({ target, value, descriptors, prototype: parent }) => [
132263
+ ...getSandboxPrototype(target) === parent ? [] : [getSandboxPrototype(target)],
132264
+ ...Object.entries(Object.getOwnPropertyDescriptors(value)).flatMap(
132265
+ ([key3, descriptor]) => unchanged(descriptors.get(key3), descriptor) ? [] : [key3, descriptor.value, ...retainedAccessorClosures(descriptor)]
132266
+ )
132267
+ ])
132268
+ );
131917
132269
  }
131918
132270
  function releaseObjectPrototype(budget) {
131919
132271
  for (const prototype of intrinsicPrototypeRoots.get(budget) ?? []) budget.setRetainedValues(prototype, void 0);
@@ -131932,6 +132284,24 @@ function getSandboxPrototype(value, budget) {
131932
132284
  function hasExplicitSandboxPrototype(value) {
131933
132285
  return prototypes.has(value);
131934
132286
  }
132287
+ function getSandboxPropertyDescriptor(value, key3, budget) {
132288
+ const hostProperties = isSandboxClosure(value) ? value.properties : void 0;
132289
+ if (isSandboxClosure(value) && !isGuestClosure(value))
132290
+ return hostProperties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(hostProperties, key3);
132291
+ let current = value;
132292
+ let depth = 0;
132293
+ while (typeof current === "object" && current !== null && (Array.isArray(current) || isPrototypeRecord(current))) {
132294
+ const properties = isGuestClosure(current) ? getGuestFunctionProperties(current) : current;
132295
+ const descriptor = properties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(properties, key3);
132296
+ if (descriptor !== void 0) return descriptor;
132297
+ current = getSandboxPrototype(current, budget);
132298
+ if (current !== null) {
132299
+ budget?.visitNode();
132300
+ assertSandboxDataDepth(++depth);
132301
+ }
132302
+ }
132303
+ return void 0;
132304
+ }
131935
132305
  function getSandboxDataProperty(value, key3, budget) {
131936
132306
  let current = value;
131937
132307
  let depth = 0;
@@ -132005,6 +132375,7 @@ var init_object_model = __esm({
132005
132375
  "packages/safe-js/src/interp/object-model.ts"() {
132006
132376
  "use strict";
132007
132377
  init_graph_depth();
132378
+ init_accessors();
132008
132379
  init_host_capabilities();
132009
132380
  init_boxed();
132010
132381
  init_values();
@@ -144729,24 +145100,32 @@ async function bindAssignmentPattern2(pattern, value, target, scope, context) {
144729
145100
  async function bindArrayPattern2(pattern, value, target, scope, context) {
144730
145101
  const iterator = isSandboxCollectionIterator(value) ? value : void 0;
144731
145102
  const values = iterator === void 0 ? getArrayPatternValues(value) : void 0;
145103
+ let cursor2 = 0;
145104
+ let done = false;
145105
+ const next = async () => {
145106
+ if (iterator !== void 0) return nextCollectionIterator(iterator, context.budget);
145107
+ if (done || cursor2 >= values.length) {
145108
+ done = true;
145109
+ return { done: true, value: void 0 };
145110
+ }
145111
+ return { done: false, value: await context.getProperty(values, cursor2++) };
145112
+ };
144732
145113
  for (let index = 0; index < pattern.elements.length; index += 1) {
144733
145114
  const element = pattern.elements[index];
144734
145115
  if (element === null) {
144735
- if (iterator !== void 0) nextCollectionIterator(iterator, context.budget);
145116
+ await next();
144736
145117
  continue;
144737
145118
  }
144738
145119
  let elementValue;
144739
- if (iterator === void 0) {
144740
- elementValue = element.type === "RestElement" ? values.slice(index) : values[index];
144741
- } else if (element.type === "RestElement") {
145120
+ if (element.type === "RestElement") {
144742
145121
  const rest = [];
144743
- for (let entry = nextCollectionIterator(iterator, context.budget); !entry.done; entry = nextCollectionIterator(iterator, context.budget)) {
145122
+ for (let entry = await next(); !entry.done; entry = await next()) {
144744
145123
  context.budget?.allocateArrayLength(rest.length + 1);
144745
145124
  rest.push(entry.value);
144746
145125
  }
144747
145126
  elementValue = rest;
144748
145127
  } else {
144749
- elementValue = nextCollectionIterator(iterator, context.budget).value;
145128
+ elementValue = (await next()).value;
144750
145129
  }
144751
145130
  const binding = await bindPattern2(element, elementValue, target, scope, context);
144752
145131
  if (!binding.ok) {
@@ -144764,7 +145143,7 @@ async function bindObjectPattern2(pattern, value, target, scope, context) {
144764
145143
  if (property.type === "RestElement") {
144765
145144
  const binding2 = await bindPattern2(
144766
145145
  property,
144767
- copyObjectRestValue(value, excludedKeys),
145146
+ await copyObjectRestValue(value, excludedKeys, context),
144768
145147
  target,
144769
145148
  scope,
144770
145149
  context
@@ -144781,7 +145160,7 @@ async function bindObjectPattern2(pattern, value, target, scope, context) {
144781
145160
  excludedKeys.add(String(key3.value));
144782
145161
  const binding = await bindPattern2(
144783
145162
  property.value,
144784
- context.getProperty(value, key3.value),
145163
+ await context.getProperty(value, key3.value),
144785
145164
  target,
144786
145165
  scope,
144787
145166
  context
@@ -144807,7 +145186,7 @@ async function bindMemberExpression(pattern, value, scope, context) {
144807
145186
  if (!isIndexableValue(object.value)) {
144808
145187
  throw new TypeError("Assignment expressions require a sandbox object property.");
144809
145188
  }
144810
- context.setProperty(object.value, await context.toPropertyKey(property.value), value);
145189
+ await context.setProperty(object.value, await context.toPropertyKey(property.value), value);
144811
145190
  return { ok: true };
144812
145191
  }
144813
145192
  async function evaluatePatternKey(property, context) {
@@ -144858,12 +145237,18 @@ function describeRuntimeValue(value) {
144858
145237
  if (typeof value === "object") return value.constructor?.name ?? "Object";
144859
145238
  return typeof value;
144860
145239
  }
144861
- function copyObjectRestValue(value, excludedKeys) {
145240
+ async function copyObjectRestValue(value, excludedKeys, context) {
144862
145241
  const rest = /* @__PURE__ */ Object.create(null);
144863
- for (const [key3, entryValue] of ownEnumerableSandboxEntries(value, excludedKeys)) {
144864
- defineProperty(rest, key3, entryValue);
145242
+ const release2 = context.budget === void 0 ? () => void 0 : retainValues(context.budget, () => [value, rest]);
145243
+ try {
145244
+ for (const key3 of ownEnumerableSandboxKeys(value)) {
145245
+ if (excludedKeys.has(key3) || !hasOwnSandboxProperty(value, key3, true)) continue;
145246
+ defineProperty(rest, key3, await context.getProperty(value, key3));
145247
+ }
145248
+ return rest;
145249
+ } finally {
145250
+ release2();
144865
145251
  }
144866
- return rest;
144867
145252
  }
144868
145253
  function isIndexableValue(value) {
144869
145254
  return typeof value === "object" && value !== null;
@@ -144879,6 +145264,8 @@ function defineProperty(target, key3, value) {
144879
145264
  var init_patterns = __esm({
144880
145265
  "packages/safe-js/src/interp/patterns.ts"() {
144881
145266
  "use strict";
145267
+ init_resources();
145268
+ init_object2();
144882
145269
  init_collection_iterator();
144883
145270
  init_values();
144884
145271
  }
@@ -145025,160 +145412,6 @@ var init_date2 = __esm({
145025
145412
  }
145026
145413
  });
145027
145414
 
145028
- // packages/safe-js/src/interp/globals/object.ts
145029
- function createObjectGlobal(methods, budget) {
145030
- const construct = ([value]) => {
145031
- if (value === null || value === void 0) {
145032
- budget.chargeDataUsage(1);
145033
- return /* @__PURE__ */ Object.create(null);
145034
- }
145035
- if (typeof value !== "object") {
145036
- const box = createSandboxBox(value);
145037
- budget.chargeDataUsage(measureSandboxData([box]));
145038
- return box;
145039
- }
145040
- return value;
145041
- };
145042
- const constructor = createSandboxClosure({
145043
- guest: true,
145044
- sandbox: true,
145045
- name: "Object",
145046
- length: 1,
145047
- call: construct,
145048
- construct: (args, context) => {
145049
- if (context?.newTarget === void 0 || context.newTarget === constructor) return construct(args);
145050
- const value = construct([]);
145051
- const prototype2 = context.getProperty(context.newTarget, "prototype");
145052
- if (typeof prototype2 === "object" && prototype2 !== null) setSandboxPrototype(value, prototype2, budget);
145053
- return value;
145054
- }
145055
- });
145056
- const properties = materializeFunctionProperties(constructor);
145057
- const prototype = properties.prototype;
145058
- Object.defineProperty(properties, "prototype", { writable: false });
145059
- for (const [name, method] of Object.entries(methods)) {
145060
- Object.defineProperty(properties, name, { value: method, writable: true, configurable: true });
145061
- }
145062
- const prototypeMethods = {
145063
- toString: createSandboxClosure({
145064
- sandbox: true,
145065
- name: "toString",
145066
- length: 0,
145067
- call: (_args, context) => budget.allocateString(`[object ${typeTag(context?.thisValue)}]`)
145068
- }),
145069
- valueOf: createSandboxClosure({
145070
- sandbox: true,
145071
- name: "valueOf",
145072
- length: 0,
145073
- call: (_args, context) => {
145074
- const value = requireReceiver(context?.thisValue);
145075
- return construct([value]);
145076
- }
145077
- }),
145078
- hasOwnProperty: createSandboxClosure({
145079
- sandbox: true,
145080
- name: "hasOwnProperty",
145081
- length: 1,
145082
- call: async ([key3], context) => hasOwnSandboxProperty(
145083
- requireReceiver(context?.thisValue),
145084
- await sandboxString(key3, budget, context),
145085
- false
145086
- )
145087
- }),
145088
- propertyIsEnumerable: createSandboxClosure({
145089
- sandbox: true,
145090
- name: "propertyIsEnumerable",
145091
- length: 1,
145092
- call: async ([key3], context) => hasOwnSandboxProperty(
145093
- requireReceiver(context?.thisValue),
145094
- await sandboxString(key3, budget, context),
145095
- true
145096
- )
145097
- }),
145098
- isPrototypeOf: createSandboxClosure({
145099
- sandbox: true,
145100
- name: "isPrototypeOf",
145101
- length: 1,
145102
- call: ([value], context) => {
145103
- if (typeof value !== "object" || value === null) return false;
145104
- const receiver = requireReceiver(context?.thisValue);
145105
- let depth = 0;
145106
- for (let current = getSandboxPrototype(value, budget); current !== null; current = getSandboxPrototype(current, budget)) {
145107
- budget.visitNode();
145108
- assertSandboxDataDepth(depth++);
145109
- if (current === receiver) return true;
145110
- }
145111
- return false;
145112
- }
145113
- })
145114
- };
145115
- for (const [name, method] of Object.entries(prototypeMethods)) {
145116
- Object.defineProperty(prototype, name, { value: method, writable: true, configurable: true });
145117
- }
145118
- markDescriptorObject(prototype);
145119
- installObjectPrototype(budget, prototype, constructor);
145120
- return constructor;
145121
- }
145122
- function requireReceiver(value) {
145123
- if (value === null || value === void 0)
145124
- throw new TypeError("Object method requires a non-null receiver.");
145125
- return value;
145126
- }
145127
- function hasOwnSandboxProperty(value, key3, enumerable) {
145128
- requireReceiver(value);
145129
- if (isGuestHostObject(value)) return hasHostObjectMember(value, key3, enumerable);
145130
- let properties;
145131
- if (isGuestClosure(value)) properties = materializeFunctionProperties(value);
145132
- else if (isSandboxClosure(value)) {
145133
- if (key3 === "length" || key3 === "name") return !enumerable;
145134
- properties = value.properties ?? /* @__PURE__ */ Object.create(null);
145135
- } else if (isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value) || isSandboxGenerator(value))
145136
- return false;
145137
- else if (isSandboxRegex(value)) return key3 === "lastIndex" && !enumerable;
145138
- else properties = Object(value);
145139
- const descriptor = Object.getOwnPropertyDescriptor(properties, key3);
145140
- return descriptor !== void 0 && (!enumerable || descriptor.enumerable === true);
145141
- }
145142
- function typeTag(value) {
145143
- if (isSandboxBox(value)) value = boxedValue(value);
145144
- if (value === void 0) return "Undefined";
145145
- if (value === null) return "Null";
145146
- if (typeof value === "string") return "String";
145147
- if (typeof value === "number") return "Number";
145148
- if (typeof value === "boolean") return "Boolean";
145149
- if (isSandboxClosure(value)) {
145150
- while (value.boundTarget !== void 0) value = value.boundTarget;
145151
- return value.generator ? "GeneratorFunction" : value.async ? "AsyncFunction" : "Function";
145152
- }
145153
- if (Array.isArray(value)) return "Array";
145154
- if (isSandboxDate(value)) return "Date";
145155
- if (isSandboxErrorConstructorInstance(value, "Error")) return "Error";
145156
- if (isSandboxRegex(value)) return "RegExp";
145157
- if (isSandboxMap(value)) return "Map";
145158
- if (isSandboxSet(value)) return "Set";
145159
- if (isSandboxCollectionIterator(value)) return collectionIteratorState(value).collectionKind === "map" ? "Map Iterator" : "Set Iterator";
145160
- if (isSandboxPromise(value)) return "Promise";
145161
- if (isSandboxGenerator(value)) return "Generator";
145162
- if (isFloat32Array(value)) return "Float32Array";
145163
- return "Object";
145164
- }
145165
- var init_object2 = __esm({
145166
- "packages/safe-js/src/interp/globals/object.ts"() {
145167
- "use strict";
145168
- init_graph_depth();
145169
- init_date();
145170
- init_boxed();
145171
- init_exceptions();
145172
- init_float32();
145173
- init_host_capabilities();
145174
- init_collection_iterator();
145175
- init_object_model();
145176
- init_string_coercion();
145177
- init_values();
145178
- init_values();
145179
- }
145180
- });
145181
-
145182
145415
  // packages/safe-js/src/interp/globals/numeric-parsers.ts
145183
145416
  function createNumericParsers(budget) {
145184
145417
  return {
@@ -145814,12 +146047,17 @@ function createPrimitiveConstructor(options, budget) {
145814
146047
  const prototype = createSandboxBox(initial);
145815
146048
  const allocate = (value, context) => {
145816
146049
  const box = createSandboxBox(value);
146050
+ const finish = (prototype2) => {
146051
+ if (typeof prototype2 === "object" && prototype2 !== null)
146052
+ setSandboxPrototype(box, prototype2, budget);
146053
+ budget.chargeDataUsage(measureSandboxData([box]));
146054
+ return box;
146055
+ };
145817
146056
  if (context?.newTarget !== void 0 && context.newTarget !== constructor) {
145818
146057
  const prototype2 = context.getProperty(context.newTarget, "prototype");
145819
- if (typeof prototype2 === "object" && prototype2 !== null) setSandboxPrototype(box, prototype2, budget);
146058
+ return prototype2 instanceof Promise ? prototype2.then(finish) : finish(prototype2);
145820
146059
  }
145821
- budget.chargeDataUsage(measureSandboxData([box]));
145822
- return box;
146060
+ return finish(void 0);
145823
146061
  };
145824
146062
  const constructor = createSandboxClosure({
145825
146063
  guest: true,
@@ -145890,230 +146128,277 @@ var init_primitives = __esm({
145890
146128
  // packages/safe-js/src/interp/globals/object-array.ts
145891
146129
  function createObjectArrayGlobals(options) {
145892
146130
  return {
145893
- Object: createObjectGlobal({
145894
- keys: createSandboxClosure({
145895
- sandbox: true,
145896
- call: ([value]) => budgetSandboxValue2(getOwnEnumerableKeys(value), options.budget),
145897
- name: "keys"
145898
- }),
145899
- values: createSandboxClosure({
145900
- sandbox: true,
145901
- call: ([value]) => allocateProducedSandboxValue(getOwnEnumerableValues(value), options.budget),
145902
- name: "values"
145903
- }),
145904
- entries: createSandboxClosure({
145905
- sandbox: true,
145906
- call: ([value]) => allocateProducedSandboxValue(ownEnumerableSandboxEntries(value), options.budget),
145907
- name: "entries"
145908
- }),
145909
- hasOwn: createSandboxClosure({
145910
- sandbox: true,
145911
- call: ([value, key3], context) => {
145912
- if (value === null || value === void 0) throw new TypeError("Cannot convert undefined or null to object.");
145913
- const name = sandboxString(key3, options.budget, context);
145914
- return typeof name === "string" ? hasOwnSandboxProperty(value, name, false) : name.then((property) => hasOwnSandboxProperty(value, property, false));
145915
- },
145916
- name: "hasOwn"
145917
- }),
145918
- getOwnPropertyDescriptor: createSandboxClosure({
145919
- sandbox: true,
145920
- call: async ([value, key3], context) => {
145921
- const descriptor = Object.getOwnPropertyDescriptor(objectProperties(value), await sandboxString(key3, options.budget, context));
145922
- if (descriptor !== void 0 && !("value" in descriptor)) throw new TypeError("Only data property descriptors are supported.");
145923
- return descriptor === void 0 ? void 0 : allocateProducedSandboxValue(descriptor, options.budget);
145924
- },
145925
- name: "getOwnPropertyDescriptor"
145926
- }),
145927
- getOwnPropertyNames: createSandboxClosure({
145928
- sandbox: true,
145929
- call: ([value]) => budgetSandboxValue2(Object.getOwnPropertyNames(objectProperties(value)), options.budget),
145930
- name: "getOwnPropertyNames"
145931
- }),
145932
- defineProperty: createSandboxClosure({
145933
- sandbox: true,
145934
- call: async ([value, key3, descriptor], context) => {
145935
- defineDataProperty8(value, await sandboxString(key3, options.budget, context), dataDescriptor(descriptor), options.budget);
145936
- return value;
145937
- },
145938
- name: "defineProperty"
145939
- }),
145940
- defineProperties: createSandboxClosure({
145941
- sandbox: true,
145942
- call: ([value, descriptors]) => {
145943
- const properties = ownEnumerableSandboxEntries(descriptors).map(([key3, descriptor]) => [key3, dataDescriptor(descriptor)]);
145944
- for (const [key3, descriptor] of properties) {
145945
- defineDataProperty8(value, key3, descriptor, options.budget);
145946
- }
145947
- return value;
145948
- },
145949
- name: "defineProperties"
145950
- }),
145951
- getPrototypeOf: createSandboxClosure({
145952
- sandbox: true,
145953
- call: ([value]) => {
145954
- if (isSandboxDate(value)) return getDatePrototype(value, options.budget, options.compileOwner);
145955
- if (value !== null && value !== void 0 && typeof value !== "object") value = createSandboxBox(value);
145956
- objectProperties(value);
145957
- return getSandboxPrototype(value, options.budget);
145958
- },
145959
- name: "getPrototypeOf"
145960
- }),
145961
- setPrototypeOf: createSandboxClosure({
145962
- sandbox: true,
145963
- call: ([value, prototype]) => {
145964
- objectProperties(value, true);
145965
- if (prototype !== null) objectProperties(prototype);
145966
- setSandboxPrototype(value, prototype, options.budget);
145967
- return value;
145968
- },
145969
- name: "setPrototypeOf"
145970
- }),
145971
- create: createSandboxClosure({
145972
- sandbox: true,
145973
- call: ([prototype, descriptors]) => {
145974
- if (prototype !== null) objectProperties(prototype);
145975
- const value = /* @__PURE__ */ Object.create(null);
145976
- setSandboxPrototype(value, prototype, options.budget);
145977
- if (descriptors !== void 0) {
145978
- const properties = ownEnumerableSandboxEntries(descriptors).map(([key3, descriptor]) => [key3, dataDescriptor(descriptor)]);
145979
- for (const [key3, descriptor] of properties) {
145980
- defineDataProperty8(value, key3, descriptor, options.budget);
145981
- }
145982
- }
145983
- return allocateProducedSandboxValue(value, options.budget);
145984
- },
145985
- name: "create"
145986
- }),
145987
- is: createSandboxClosure({
145988
- sandbox: true,
145989
- call: ([left, right]) => Reflect.apply(Object.is, Object, [left, right]),
145990
- name: "is"
145991
- }),
145992
- fromEntries: createSandboxClosure({
145993
- sandbox: true,
145994
- call: ([value], context) => {
145995
- const iterator = getSandboxIterator(value, options.budget, context);
145996
- if (iterator === void 0) {
145997
- throw new TypeError("Object.fromEntries requires an iterable.");
145998
- }
145999
- if (context === void 0 && !iterator.generator && !iterator.asynchronous) {
146000
- return allocateProducedSandboxValue(
146001
- Object.setPrototypeOf(
146002
- Reflect.apply(Object.fromEntries, Object, [{ [Symbol.iterator]: () => iterator }]),
146003
- null
146004
- ),
146005
- options.budget
146006
- );
146007
- }
146008
- return objectFromSandboxEntries(value, iterator, options.budget, context);
146009
- },
146010
- name: "fromEntries"
146011
- }),
146012
- freeze: createSandboxClosure({
146013
- sandbox: true,
146014
- call: ([value]) => {
146015
- if (isGuestHostObject(value)) throw new TypeError("Live host objects cannot be frozen.");
146016
- if (typeof value === "object" && value !== null) {
146017
- Object.freeze(isGuestClosure(value) ? materializeFunctionProperties(value) : value);
146018
- }
146019
- return value;
146020
- },
146021
- name: "freeze"
146022
- }),
146023
- isFrozen: createSandboxClosure({
146024
- sandbox: true,
146025
- call: ([value]) => Object.isFrozen(isGuestClosure(value) ? materializeFunctionProperties(value) : value),
146026
- name: "isFrozen"
146027
- }),
146028
- assign: createSandboxClosure({
146029
- sandbox: true,
146030
- call: ([target, ...sources]) => assignSandboxValues(target, sources, options.budget),
146031
- name: "assign"
146032
- })
146033
- }, options.budget),
146034
- Array: createSandboxClosure({
146035
- sandbox: true,
146036
- call: (args) => createArrayFromConstructorArgs(args, options.budget),
146037
- construct: (args) => createArrayFromConstructorArgs(args, options.budget),
146038
- name: "Array",
146039
- properties: {
146040
- isArray: createSandboxClosure({
146131
+ Object: createObjectGlobal(
146132
+ {
146133
+ keys: createSandboxClosure({
146041
146134
  sandbox: true,
146042
- call: ([value]) => Array.isArray(value),
146043
- name: "isArray"
146135
+ call: ([value]) => budgetSandboxValue2(ownEnumerableSandboxKeys(value), options.budget),
146136
+ name: "keys"
146044
146137
  }),
146045
- from: createSandboxClosure({
146138
+ values: createSandboxClosure({
146046
146139
  sandbox: true,
146047
- call: (args, context) => arrayFromSandboxValues(args, options.budget, context),
146048
- name: "from"
146140
+ call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(
146141
+ ownEnumerableSandboxEntries(value).map(([, entry]) => entry),
146142
+ options.budget
146143
+ ) : getOwnEnumerableEntries(value, options.budget, context).then(
146144
+ (entries) => allocateProducedSandboxValue(
146145
+ entries.map(([, entry]) => entry),
146146
+ options.budget
146147
+ )
146148
+ ),
146149
+ name: "values"
146049
146150
  }),
146050
- of: createSandboxClosure({
146151
+ entries: createSandboxClosure({
146051
146152
  sandbox: true,
146052
- call: (args) => budgetSandboxValue2(Reflect.apply(Array.of, Array, [...args]), options.budget),
146053
- name: "of"
146054
- })
146055
- }
146056
- }),
146057
- String: createPrimitiveConstructor({
146058
- call: (args, context) => sandboxString(args.length === 0 ? "" : args[0], options.budget, context),
146059
- name: "String",
146060
- properties: {
146061
- raw: createSandboxClosure({
146153
+ call: ([value], context) => context === void 0 ? allocateProducedSandboxValue(ownEnumerableSandboxEntries(value), options.budget) : getOwnEnumerableEntries(value, options.budget, context).then(
146154
+ (entries) => allocateProducedSandboxValue(entries, options.budget)
146155
+ ),
146156
+ name: "entries"
146157
+ }),
146158
+ hasOwn: createSandboxClosure({
146159
+ sandbox: true,
146160
+ call: ([value, key3], context) => {
146161
+ if (value === null || value === void 0)
146162
+ throw new TypeError("Cannot convert undefined or null to object.");
146163
+ const name = sandboxString(key3, options.budget, context);
146164
+ return typeof name === "string" ? hasOwnSandboxProperty(value, name, false) : name.then((property) => hasOwnSandboxProperty(value, property, false));
146165
+ },
146166
+ name: "hasOwn"
146167
+ }),
146168
+ getOwnPropertyDescriptor: createSandboxClosure({
146062
146169
  sandbox: true,
146063
- call: (args) => stringRaw(args, options.budget),
146064
- name: "raw"
146170
+ call: async ([value, key3], context) => {
146171
+ const descriptor = Object.getOwnPropertyDescriptor(
146172
+ objectProperties(value),
146173
+ await sandboxString(key3, options.budget, context)
146174
+ );
146175
+ if (descriptor === void 0) return void 0;
146176
+ return allocateProducedSandboxValue(
146177
+ exposePropertyDescriptor(descriptor),
146178
+ options.budget
146179
+ );
146180
+ },
146181
+ name: "getOwnPropertyDescriptor"
146065
146182
  }),
146066
- fromCharCode: createSandboxClosure({
146183
+ getOwnPropertyDescriptors: createSandboxClosure({
146067
146184
  sandbox: true,
146068
- call: (args) => options.budget.allocateString(Reflect.apply(String.fromCharCode, String, [...args])),
146069
- name: "fromCharCode"
146185
+ call: ([value]) => {
146186
+ const descriptors = /* @__PURE__ */ Object.create(null);
146187
+ for (const [key3, descriptor] of Object.entries(
146188
+ Object.getOwnPropertyDescriptors(objectProperties(value))
146189
+ ))
146190
+ defineOwnDataProperty(descriptors, key3, exposePropertyDescriptor(descriptor));
146191
+ return allocateProducedSandboxValue(descriptors, options.budget);
146192
+ },
146193
+ name: "getOwnPropertyDescriptors"
146070
146194
  }),
146071
- fromCodePoint: createSandboxClosure({
146195
+ getOwnPropertyNames: createSandboxClosure({
146072
146196
  sandbox: true,
146073
- call: (args) => options.budget.allocateString(Reflect.apply(String.fromCodePoint, String, [...args])),
146074
- name: "fromCodePoint"
146075
- })
146076
- }
146077
- }, options.budget),
146078
- Number: createPrimitiveConstructor({
146079
- call: (args, context) => sandboxNumber(args.length === 0 ? 0 : args[0], options.budget, context),
146080
- name: "Number",
146081
- properties: {
146082
- isFinite: createSandboxClosure({
146197
+ call: ([value]) => budgetSandboxValue2(Object.getOwnPropertyNames(objectProperties(value)), options.budget),
146198
+ name: "getOwnPropertyNames"
146199
+ }),
146200
+ defineProperty: createSandboxClosure({
146201
+ sandbox: true,
146202
+ call: async ([value, key3, descriptor], context) => {
146203
+ objectProperties(value, true);
146204
+ const property = await sandboxString(key3, options.budget, context);
146205
+ defineDataProperty8(
146206
+ value,
146207
+ property,
146208
+ await propertyDescriptor(descriptor, options.budget, context),
146209
+ options.budget
146210
+ );
146211
+ return value;
146212
+ },
146213
+ name: "defineProperty"
146214
+ }),
146215
+ defineProperties: createSandboxClosure({
146216
+ sandbox: true,
146217
+ call: async ([value, descriptors], context) => {
146218
+ await definePropertiesFromObject(value, descriptors, options.budget, context);
146219
+ return value;
146220
+ },
146221
+ name: "defineProperties"
146222
+ }),
146223
+ getPrototypeOf: createSandboxClosure({
146224
+ sandbox: true,
146225
+ call: ([value]) => {
146226
+ if (isSandboxDate(value))
146227
+ return getDatePrototype(value, options.budget, options.compileOwner);
146228
+ if (value !== null && value !== void 0 && typeof value !== "object")
146229
+ value = createSandboxBox(value);
146230
+ objectProperties(value);
146231
+ return getSandboxPrototype(value, options.budget);
146232
+ },
146233
+ name: "getPrototypeOf"
146234
+ }),
146235
+ setPrototypeOf: createSandboxClosure({
146236
+ sandbox: true,
146237
+ call: ([value, prototype]) => {
146238
+ objectProperties(value, true);
146239
+ if (prototype !== null) objectProperties(prototype);
146240
+ setSandboxPrototype(value, prototype, options.budget);
146241
+ return value;
146242
+ },
146243
+ name: "setPrototypeOf"
146244
+ }),
146245
+ create: createSandboxClosure({
146246
+ sandbox: true,
146247
+ call: async ([prototype, descriptors], context) => {
146248
+ if (prototype !== null) objectProperties(prototype);
146249
+ const value = /* @__PURE__ */ Object.create(null);
146250
+ setSandboxPrototype(value, prototype, options.budget);
146251
+ if (descriptors !== void 0) {
146252
+ await definePropertiesFromObject(value, descriptors, options.budget, context);
146253
+ }
146254
+ return allocateProducedSandboxValue(value, options.budget);
146255
+ },
146256
+ name: "create"
146257
+ }),
146258
+ is: createSandboxClosure({
146083
146259
  sandbox: true,
146084
- call: ([value]) => typeof value === "number" && Number.isFinite(value),
146085
- name: "isFinite"
146260
+ call: ([left, right]) => Reflect.apply(Object.is, Object, [left, right]),
146261
+ name: "is"
146086
146262
  }),
146087
- isNaN: createSandboxClosure({
146263
+ fromEntries: createSandboxClosure({
146088
146264
  sandbox: true,
146089
- call: ([value]) => typeof value === "number" && Number.isNaN(value),
146090
- name: "isNaN"
146265
+ call: ([value], context) => {
146266
+ const iterator = getSandboxIterator(value, options.budget, context);
146267
+ if (iterator === void 0) {
146268
+ throw new TypeError("Object.fromEntries requires an iterable.");
146269
+ }
146270
+ if (context === void 0 && !iterator.generator && !iterator.asynchronous) {
146271
+ return allocateProducedSandboxValue(
146272
+ Object.setPrototypeOf(
146273
+ Reflect.apply(Object.fromEntries, Object, [
146274
+ { [Symbol.iterator]: () => iterator }
146275
+ ]),
146276
+ null
146277
+ ),
146278
+ options.budget
146279
+ );
146280
+ }
146281
+ return objectFromSandboxEntries(value, iterator, options.budget, context);
146282
+ },
146283
+ name: "fromEntries"
146091
146284
  }),
146092
- isInteger: createSandboxClosure({
146285
+ freeze: createSandboxClosure({
146093
146286
  sandbox: true,
146094
- call: ([value]) => typeof value === "number" && Number.isInteger(value),
146095
- name: "isInteger"
146287
+ call: ([value]) => {
146288
+ if (isGuestHostObject(value))
146289
+ throw new TypeError("Live host objects cannot be frozen.");
146290
+ if (typeof value === "object" && value !== null) {
146291
+ Object.freeze(isGuestClosure(value) ? materializeFunctionProperties(value) : value);
146292
+ }
146293
+ return value;
146294
+ },
146295
+ name: "freeze"
146096
146296
  }),
146097
- ...createNumericParsers(options.budget),
146098
- isSafeInteger: createSandboxClosure({
146297
+ isFrozen: createSandboxClosure({
146099
146298
  sandbox: true,
146100
- call: ([value]) => typeof value === "number" && Number.isSafeInteger(value),
146101
- name: "isSafeInteger"
146299
+ call: ([value]) => Object.isFrozen(isGuestClosure(value) ? materializeFunctionProperties(value) : value),
146300
+ name: "isFrozen"
146102
146301
  }),
146103
- MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
146104
- MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER,
146105
- EPSILON: Number.EPSILON,
146106
- MAX_VALUE: Number.MAX_VALUE,
146107
- MIN_VALUE: Number.MIN_VALUE,
146108
- NaN: Number.NaN,
146109
- NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
146110
- POSITIVE_INFINITY: Number.POSITIVE_INFINITY
146111
- }
146112
- }, options.budget),
146113
- Boolean: createPrimitiveConstructor({
146114
- call: ([value]) => Boolean(value),
146115
- name: "Boolean"
146116
- }, options.budget)
146302
+ assign: createSandboxClosure({
146303
+ sandbox: true,
146304
+ call: ([target, ...sources], context) => assignSandboxValues(target, sources, options.budget, context),
146305
+ name: "assign"
146306
+ })
146307
+ },
146308
+ options.budget
146309
+ ),
146310
+ Array: createSandboxClosure({
146311
+ sandbox: true,
146312
+ call: (args) => createArrayFromConstructorArgs(args, options.budget),
146313
+ construct: (args) => createArrayFromConstructorArgs(args, options.budget),
146314
+ name: "Array",
146315
+ properties: {
146316
+ isArray: createSandboxClosure({
146317
+ sandbox: true,
146318
+ call: ([value]) => Array.isArray(value),
146319
+ name: "isArray"
146320
+ }),
146321
+ from: createSandboxClosure({
146322
+ sandbox: true,
146323
+ call: (args, context) => arrayFromSandboxValues(args, options.budget, context),
146324
+ name: "from"
146325
+ }),
146326
+ of: createSandboxClosure({
146327
+ sandbox: true,
146328
+ call: (args) => budgetSandboxValue2(Reflect.apply(Array.of, Array, [...args]), options.budget),
146329
+ name: "of"
146330
+ })
146331
+ }
146332
+ }),
146333
+ String: createPrimitiveConstructor(
146334
+ {
146335
+ call: (args, context) => sandboxString(args.length === 0 ? "" : args[0], options.budget, context),
146336
+ name: "String",
146337
+ properties: {
146338
+ raw: createSandboxClosure({
146339
+ sandbox: true,
146340
+ call: (args, context) => stringRaw(args, options.budget, context),
146341
+ name: "raw"
146342
+ }),
146343
+ fromCharCode: createSandboxClosure({
146344
+ sandbox: true,
146345
+ call: (args) => options.budget.allocateString(Reflect.apply(String.fromCharCode, String, [...args])),
146346
+ name: "fromCharCode"
146347
+ }),
146348
+ fromCodePoint: createSandboxClosure({
146349
+ sandbox: true,
146350
+ call: (args) => options.budget.allocateString(Reflect.apply(String.fromCodePoint, String, [...args])),
146351
+ name: "fromCodePoint"
146352
+ })
146353
+ }
146354
+ },
146355
+ options.budget
146356
+ ),
146357
+ Number: createPrimitiveConstructor(
146358
+ {
146359
+ call: (args, context) => sandboxNumber(args.length === 0 ? 0 : args[0], options.budget, context),
146360
+ name: "Number",
146361
+ properties: {
146362
+ isFinite: createSandboxClosure({
146363
+ sandbox: true,
146364
+ call: ([value]) => typeof value === "number" && Number.isFinite(value),
146365
+ name: "isFinite"
146366
+ }),
146367
+ isNaN: createSandboxClosure({
146368
+ sandbox: true,
146369
+ call: ([value]) => typeof value === "number" && Number.isNaN(value),
146370
+ name: "isNaN"
146371
+ }),
146372
+ isInteger: createSandboxClosure({
146373
+ sandbox: true,
146374
+ call: ([value]) => typeof value === "number" && Number.isInteger(value),
146375
+ name: "isInteger"
146376
+ }),
146377
+ ...createNumericParsers(options.budget),
146378
+ isSafeInteger: createSandboxClosure({
146379
+ sandbox: true,
146380
+ call: ([value]) => typeof value === "number" && Number.isSafeInteger(value),
146381
+ name: "isSafeInteger"
146382
+ }),
146383
+ MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
146384
+ MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER,
146385
+ EPSILON: Number.EPSILON,
146386
+ MAX_VALUE: Number.MAX_VALUE,
146387
+ MIN_VALUE: Number.MIN_VALUE,
146388
+ NaN: Number.NaN,
146389
+ NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
146390
+ POSITIVE_INFINITY: Number.POSITIVE_INFINITY
146391
+ }
146392
+ },
146393
+ options.budget
146394
+ ),
146395
+ Boolean: createPrimitiveConstructor(
146396
+ {
146397
+ call: ([value]) => Boolean(value),
146398
+ name: "Boolean"
146399
+ },
146400
+ options.budget
146401
+ )
146117
146402
  };
146118
146403
  }
146119
146404
  async function objectFromSandboxEntries(items, iterator, budget, context) {
@@ -146123,7 +146408,15 @@ async function objectFromSandboxEntries(items, iterator, budget, context) {
146123
146408
  let value;
146124
146409
  let failure;
146125
146410
  const retained = {};
146126
- budget.setRetainedValues(retained, () => [items, iterator.retainedValue, object, entry, key3, value, failure]);
146411
+ budget.setRetainedValues(retained, () => [
146412
+ items,
146413
+ iterator.retainedValue,
146414
+ object,
146415
+ entry,
146416
+ key3,
146417
+ value,
146418
+ failure
146419
+ ]);
146127
146420
  const checkData = createDataCheckpoint(budget, context);
146128
146421
  const closeOnThrow = async (error3) => {
146129
146422
  failure = isCapturedException(error3) ? error3.reason : error3;
@@ -146152,8 +146445,8 @@ async function objectFromSandboxEntries(items, iterator, budget, context) {
146152
146445
  if (typeof entry !== "object" || entry === null) {
146153
146446
  throw new TypeError("Object.fromEntries requires entry objects.");
146154
146447
  }
146155
- key3 = context?.getProperty !== void 0 ? context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, budget);
146156
- value = context?.getProperty !== void 0 ? context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, budget);
146448
+ key3 = context?.getProperty !== void 0 ? await context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, budget);
146449
+ value = context?.getProperty !== void 0 ? await context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, budget);
146157
146450
  const property = await sandboxString(key3, budget, context);
146158
146451
  const growth = property.length + 1 + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([value]));
146159
146452
  budget.visitNode();
@@ -146170,7 +146463,7 @@ async function objectFromSandboxEntries(items, iterator, budget, context) {
146170
146463
  budget.setRetainedValues(retained, void 0);
146171
146464
  }
146172
146465
  }
146173
- function assignSandboxValues(target, sources, budget) {
146466
+ function assignSandboxValues(target, sources, budget, context) {
146174
146467
  if (target === null || target === void 0) {
146175
146468
  throw new TypeError("Object.assign(target, ...sources) requires a non-null target.");
146176
146469
  }
@@ -146181,50 +146474,120 @@ function assignSandboxValues(target, sources, budget) {
146181
146474
  if (!isGuestClosure(target) && !isAssignableSandboxTarget(target)) {
146182
146475
  throw new TypeError("Object.assign(target, ...sources) requires an object or array target.");
146183
146476
  }
146184
- for (const source of sources) {
146185
- if (source === null || source === void 0) {
146186
- continue;
146187
- }
146188
- for (const [key3, value] of ownEnumerableSandboxEntries(source)) {
146189
- setSandboxProperty(target, key3, value, budget);
146477
+ if (context === void 0) {
146478
+ for (const source of sources) {
146479
+ if (source === null || source === void 0) continue;
146480
+ for (const [key3, value] of ownEnumerableSandboxEntries(source))
146481
+ setSandboxProperty(target, key3, value, budget);
146190
146482
  }
146483
+ return target;
146191
146484
  }
146192
- return target;
146485
+ return (async () => {
146486
+ const release2 = retainValues(budget, () => [target, ...sources]);
146487
+ try {
146488
+ for (const source of sources) {
146489
+ if (source === null || source === void 0) continue;
146490
+ for (const key3 of ownEnumerableSandboxKeys(source)) {
146491
+ if (!hasOwnSandboxProperty(source, key3, true)) continue;
146492
+ const value = await (context.getProperty !== void 0 ? context.getProperty(source, key3) : getSandboxDataProperty(source, key3, budget));
146493
+ await setSandboxProperty(target, key3, value, budget, true, context);
146494
+ }
146495
+ }
146496
+ return target;
146497
+ } finally {
146498
+ release2();
146499
+ }
146500
+ })();
146193
146501
  }
146194
146502
  function objectProperties(value, mutable = false) {
146195
146503
  if (isSandboxDate(value)) {
146196
146504
  if (mutable) throw new TypeError("Date own properties and prototypes are not supported.");
146197
146505
  return value;
146198
146506
  }
146199
- if (isGuestHostObject(value)) throw new TypeError("Live host object descriptors are not supported.");
146507
+ if (isGuestHostObject(value))
146508
+ throw new TypeError("Live host object descriptors are not supported.");
146200
146509
  if (isGuestClosure(value)) return materializeFunctionProperties(value);
146201
146510
  if (isSandboxClosure(value)) {
146202
146511
  if (mutable) throw new TypeError("Host function properties are read only.");
146203
146512
  return value.properties ?? /* @__PURE__ */ Object.create(null);
146204
146513
  }
146205
- if (!isAssignableSandboxTarget(value)) throw new TypeError("Expected a sandbox object or function.");
146514
+ if (!isAssignableSandboxTarget(value))
146515
+ throw new TypeError("Expected a sandbox object or function.");
146206
146516
  return value;
146207
146517
  }
146208
- function dataDescriptor(input) {
146209
- const source = objectProperties(input);
146518
+ function exposePropertyDescriptor(descriptor) {
146519
+ return "value" in descriptor ? descriptor : {
146520
+ get: accessorClosure(descriptor.get),
146521
+ set: accessorClosure(descriptor.set),
146522
+ enumerable: descriptor.enumerable,
146523
+ configurable: descriptor.configurable
146524
+ };
146525
+ }
146526
+ async function propertyDescriptor(input, budget, context) {
146527
+ objectProperties(input);
146210
146528
  const descriptor = {};
146211
- for (const field of ["get", "set", "value", "writable", "enumerable", "configurable"]) {
146212
- const entry = Object.getOwnPropertyDescriptor(source, field);
146213
- if (entry === void 0) continue;
146214
- if (!("value" in entry) || field === "get" || field === "set") {
146215
- throw new TypeError("Only data property descriptors are supported.");
146529
+ const release2 = retainValues(budget, () => [
146530
+ input,
146531
+ descriptor.value,
146532
+ ...retainedAccessorClosures(descriptor)
146533
+ ]);
146534
+ try {
146535
+ for (const field of [
146536
+ "enumerable",
146537
+ "configurable",
146538
+ "value",
146539
+ "writable",
146540
+ "get",
146541
+ "set"
146542
+ ]) {
146543
+ if (getSandboxPropertyDescriptor(input, field, budget) === void 0 && !hasOwnSandboxProperty(input, field, false))
146544
+ continue;
146545
+ const value = await (context?.getProperty !== void 0 ? context.getProperty(input, field) : getSandboxDataProperty(input, field, budget));
146546
+ if (field === "get" || field === "set") {
146547
+ if (value !== void 0 && !isSandboxClosure(value))
146548
+ throw new TypeError("Accessor must be a function or undefined.");
146549
+ descriptor[field] = value === void 0 ? void 0 : accessorAdapter(value, field);
146550
+ } else if (field === "value") {
146551
+ descriptor.value = value;
146552
+ } else {
146553
+ descriptor[field] = Boolean(value);
146554
+ }
146555
+ }
146556
+ if (("get" in descriptor || "set" in descriptor) && ("value" in descriptor || "writable" in descriptor))
146557
+ throw new TypeError("A property cannot be both a data property and an accessor.");
146558
+ return descriptor;
146559
+ } finally {
146560
+ release2();
146561
+ }
146562
+ }
146563
+ async function definePropertiesFromObject(target, descriptors, budget, context) {
146564
+ objectProperties(target, true);
146565
+ const properties = [];
146566
+ const release2 = retainValues(budget, () => [
146567
+ target,
146568
+ descriptors,
146569
+ properties,
146570
+ ...properties.flatMap(([, descriptor]) => retainedAccessorClosures(descriptor))
146571
+ ]);
146572
+ try {
146573
+ for (const key3 of ownEnumerableSandboxKeys(descriptors)) {
146574
+ if (!hasOwnSandboxProperty(descriptors, key3, true)) continue;
146575
+ const descriptor = await (context?.getProperty !== void 0 ? context.getProperty(descriptors, key3) : getSandboxDataProperty(descriptors, key3, budget));
146576
+ properties.push([key3, await propertyDescriptor(descriptor, budget, context)]);
146216
146577
  }
146217
- if (field === "value") descriptor.value = entry.value;
146218
- else descriptor[field] = Boolean(entry.value);
146578
+ for (const [key3, descriptor] of properties) defineDataProperty8(target, key3, descriptor, budget);
146579
+ } finally {
146580
+ release2();
146219
146581
  }
146220
- return descriptor;
146221
146582
  }
146222
146583
  function defineDataProperty8(target, key3, descriptor, budget) {
146223
146584
  budget.visitNode();
146224
- if (isFloat32Array(target)) throw new TypeError("Typed array property descriptors are not supported.");
146585
+ if (isFloat32Array(target))
146586
+ throw new TypeError("Typed array property descriptors are not supported.");
146225
146587
  const properties = objectProperties(target, true);
146226
146588
  if (Array.isArray(properties)) {
146227
- if (key3 === "length" && "value" in descriptor) budget.allocateArrayLength(Number(descriptor.value));
146589
+ if (key3 === "length" && "value" in descriptor)
146590
+ budget.allocateArrayLength(Number(descriptor.value));
146228
146591
  else {
146229
146592
  const index = Number(key3);
146230
146593
  if (Number.isInteger(index) && index >= 0 && index < 4294967295 && String(index) === key3) {
@@ -146253,7 +146616,15 @@ async function arrayFromSandboxValues(args, budget, context) {
146253
146616
  let currentValue;
146254
146617
  let failure;
146255
146618
  const retained = {};
146256
- budget.setRetainedValues(retained, () => [items, iterator?.retainedValue, mapFn, constructor, result, currentValue, failure]);
146619
+ budget.setRetainedValues(retained, () => [
146620
+ items,
146621
+ iterator?.retainedValue,
146622
+ mapFn,
146623
+ constructor,
146624
+ result,
146625
+ currentValue,
146626
+ failure
146627
+ ]);
146257
146628
  const checkData = createDataCheckpoint(budget, context);
146258
146629
  const closeOnThrow = async (error3) => {
146259
146630
  failure = isCapturedException(error3) ? error3.reason : error3;
@@ -146267,30 +146638,46 @@ async function arrayFromSandboxValues(args, budget, context) {
146267
146638
  try {
146268
146639
  let length = 0;
146269
146640
  if (iterator === void 0) {
146270
- const number = await sandboxNumber(read("length"), budget, context);
146641
+ const number = await sandboxNumber(await read("length"), budget, context);
146271
146642
  length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
146272
146643
  }
146273
- result = isSandboxClosure(constructor) && constructor.construct !== void 0 ? await invokeBuiltinClosure(constructor, iterator === void 0 ? [length] : [], budget, context, void 0, true) : createArrayFromConstructorArgs([length], budget);
146644
+ result = isSandboxClosure(constructor) && constructor.construct !== void 0 ? await invokeBuiltinClosure(
146645
+ constructor,
146646
+ iterator === void 0 ? [length] : [],
146647
+ budget,
146648
+ context,
146649
+ void 0,
146650
+ true
146651
+ ) : createArrayFromConstructorArgs([length], budget);
146274
146652
  checkData(result, 0, true);
146275
146653
  let index = 0;
146276
146654
  while (iterator !== void 0 || index < length) {
146277
146655
  try {
146278
146656
  budget.visitNode();
146279
- if (iterator !== void 0 && index >= Number.MAX_SAFE_INTEGER) throw new TypeError("Array.from input is too long.");
146657
+ if (iterator !== void 0 && index >= Number.MAX_SAFE_INTEGER)
146658
+ throw new TypeError("Array.from input is too long.");
146280
146659
  } catch (error3) {
146281
146660
  await closeOnThrow(error3);
146282
146661
  }
146283
146662
  if (iterator !== void 0) {
146284
146663
  const next = await iterator.next();
146285
- if (typeof next !== "object" || next === null) throw new TypeError("Iterator result must be an object.");
146664
+ if (typeof next !== "object" || next === null)
146665
+ throw new TypeError("Iterator result must be an object.");
146286
146666
  if (next.done) break;
146287
146667
  currentValue = next.value;
146288
146668
  } else {
146289
- currentValue = read(index);
146669
+ currentValue = await read(index);
146290
146670
  }
146291
146671
  try {
146292
146672
  if (Array.isArray(result)) budget.allocateArrayLength(index + 1);
146293
- if (mapFn !== void 0) currentValue = await invokeBuiltinClosure(mapFn, [currentValue, index], budget, context, thisValue);
146673
+ if (mapFn !== void 0)
146674
+ currentValue = await invokeBuiltinClosure(
146675
+ mapFn,
146676
+ [currentValue, index],
146677
+ budget,
146678
+ context,
146679
+ thisValue
146680
+ );
146294
146681
  const key3 = String(index);
146295
146682
  const growth = key3.length + 1 + (Array.isArray(result) ? Math.max(0, index + 1 - result.length) : 0) + (budget.limits.dataSize === void 0 ? 0 : measureSandboxData([currentValue]));
146296
146683
  budget.visitNode();
@@ -146328,19 +146715,60 @@ function createArrayFromConstructorArgs(args, budget) {
146328
146715
  release2();
146329
146716
  }
146330
146717
  }
146331
- function getOwnEnumerableKeys(value) {
146332
- if (isGuestHostObject(value)) return getHostObjectKeys(value);
146333
- return ownEnumerableSandboxEntries(value).map(([key3]) => key3);
146334
- }
146335
- function getOwnEnumerableValues(value) {
146336
- return ownEnumerableSandboxEntries(value).map(([, entryValue]) => entryValue);
146718
+ async function getOwnEnumerableEntries(value, budget, context) {
146719
+ const entries = [];
146720
+ const release2 = retainValues(budget, () => [value, entries]);
146721
+ try {
146722
+ for (const key3 of ownEnumerableSandboxKeys(value)) {
146723
+ if (!hasOwnSandboxProperty(value, key3, true)) continue;
146724
+ entries.push([
146725
+ key3,
146726
+ await (context?.getProperty !== void 0 ? context.getProperty(value, key3) : getSandboxDataProperty(value, key3, budget))
146727
+ ]);
146728
+ }
146729
+ return entries;
146730
+ } finally {
146731
+ release2();
146732
+ }
146337
146733
  }
146338
146734
  function budgetSandboxValue2(value, budget) {
146339
146735
  const sandboxValue = deepCopyToSandbox(value);
146340
146736
  return allocateProducedSandboxValue(sandboxValue, budget);
146341
146737
  }
146342
- function stringRaw(args, budget) {
146738
+ function stringRaw(args, budget, context) {
146343
146739
  const [template2, ...substitutions] = args;
146740
+ if (context?.getProperty !== void 0)
146741
+ return (async () => {
146742
+ if (template2 === null || template2 === void 0)
146743
+ throw new TypeError("String.raw requires a template object.");
146744
+ const raw2 = await context.getProperty(template2, "raw");
146745
+ if (raw2 === null || raw2 === void 0)
146746
+ throw new TypeError("String.raw requires raw strings.");
146747
+ const number = await sandboxNumber(
146748
+ await context.getProperty(raw2, "length"),
146749
+ budget,
146750
+ context
146751
+ );
146752
+ const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
146753
+ let result2 = "";
146754
+ const retained = {};
146755
+ budget.setRetainedValues(retained, () => [raw2, result2]);
146756
+ try {
146757
+ for (let index = 0; index < length; index++) {
146758
+ budget.visitNode();
146759
+ result2 = budget.allocateString(
146760
+ result2 + await sandboxString(await context.getProperty(raw2, index), budget, context)
146761
+ );
146762
+ if (index + 1 < length && index < substitutions.length)
146763
+ result2 = budget.allocateString(
146764
+ result2 + await sandboxString(substitutions[index], budget, context)
146765
+ );
146766
+ }
146767
+ return result2;
146768
+ } finally {
146769
+ budget.setRetainedValues(retained, void 0);
146770
+ }
146771
+ })();
146344
146772
  const raw = getTemplateRawParts(template2);
146345
146773
  let result = "";
146346
146774
  for (let index = 0; index < raw.length; index += 1) {
@@ -146362,8 +146790,10 @@ var init_object_array = __esm({
146362
146790
  "packages/safe-js/src/interp/globals/object-array.ts"() {
146363
146791
  "use strict";
146364
146792
  init_budget2();
146793
+ init_accessors();
146365
146794
  init_builtin_call();
146366
146795
  init_data_checkpoint();
146796
+ init_resources();
146367
146797
  init_exceptions();
146368
146798
  init_date();
146369
146799
  init_boxed();
@@ -146399,7 +146829,7 @@ async function evaluateClass(node, context, evaluateNode3, callContext) {
146399
146829
  parent = result.value;
146400
146830
  if (parent !== null && (!isSandboxClosure(parent) || parent.construct === void 0))
146401
146831
  throw new TypeError("Class extends value is not a constructor or null.");
146402
- prototypeParent = parent === null ? null : callContext.getProperty(parent, "prototype");
146832
+ prototypeParent = parent === null ? null : await callContext.getProperty(parent, "prototype");
146403
146833
  if (prototypeParent !== null && typeof prototypeParent !== "object")
146404
146834
  throw new TypeError("Class extends value has an invalid prototype.");
146405
146835
  }
@@ -146439,7 +146869,7 @@ async function evaluateClass(node, context, evaluateNode3, callContext) {
146439
146869
  };
146440
146870
  if (!derived) {
146441
146871
  thisValue = {};
146442
- const targetPrototype = invocation.getProperty(newTarget, "prototype");
146872
+ const targetPrototype = await invocation.getProperty(newTarget, "prototype");
146443
146873
  if (typeof targetPrototype === "object" && targetPrototype !== null)
146444
146874
  setSandboxPrototype(thisValue, targetPrototype, context.budget);
146445
146875
  }
@@ -146498,8 +146928,15 @@ async function evaluateClass(node, context, evaluateNode3, callContext) {
146498
146928
  } else {
146499
146929
  const home = element.static ? constructor : prototype;
146500
146930
  const method = createInterpretedClosure(element.value, classContext, evaluateNode3, home);
146501
- Object.defineProperty(materializeFunctionProperties(method), "name", { value: key3 });
146502
- Object.defineProperty(element.static ? properties : prototype, key3, { value: method, configurable: true, writable: true, enumerable: false });
146931
+ const accessor = element.kind === "get" || element.kind === "set" ? element.kind : void 0;
146932
+ Object.defineProperty(materializeFunctionProperties(method), "name", {
146933
+ value: accessor === void 0 ? key3 : `${accessor} ${key3}`
146934
+ });
146935
+ Object.defineProperty(
146936
+ element.static ? properties : prototype,
146937
+ key3,
146938
+ accessor === void 0 ? { value: method, configurable: true, writable: true, enumerable: false } : { [accessor]: accessorAdapter(method, accessor), configurable: true, enumerable: false }
146939
+ );
146503
146940
  }
146504
146941
  }
146505
146942
  if (node.id !== void 0) scope.declare(node.id.name, "const", constructor);
@@ -146535,6 +146972,7 @@ var init_classes = __esm({
146535
146972
  "use strict";
146536
146973
  init_bindings();
146537
146974
  init_function_source();
146975
+ init_accessors();
146538
146976
  init_async();
146539
146977
  init_resources();
146540
146978
  init_object_model();
@@ -146561,7 +146999,13 @@ function getArrayMember(value, property, options) {
146561
146999
  return createSandboxClosure({
146562
147000
  sandbox: true,
146563
147001
  name: `Array#${property}`,
146564
- call: (args, context) => callArrayMethod(context?.thisValue, property, args, { ...options, context }, context?.stack ?? [])
147002
+ call: (args, context) => callArrayMethod(
147003
+ context?.thisValue,
147004
+ property,
147005
+ args,
147006
+ { ...options, context },
147007
+ context?.stack ?? []
147008
+ )
146565
147009
  });
146566
147010
  }
146567
147011
  return void 0;
@@ -146577,7 +147021,8 @@ function isArrayMethodName(property) {
146577
147021
  return typeof property === "string" && arrayMethodNames.has(property);
146578
147022
  }
146579
147023
  async function callArrayMethod(receiver, methodName, args, options, stack = []) {
146580
- if (receiver === null || receiver === void 0) throw new TypeError("Array method requires a receiver.");
147024
+ if (receiver === null || receiver === void 0)
147025
+ throw new TypeError("Array method requires a receiver.");
146581
147026
  if (typeof receiver !== "object") {
146582
147027
  receiver = createSandboxBox(receiver);
146583
147028
  options.budget.chargeDataUsage(measureSandboxData([receiver]));
@@ -146614,38 +147059,44 @@ async function callArrayMethod(receiver, methodName, args, options, stack = [])
146614
147059
  }
146615
147060
  }
146616
147061
  async function arrayLikeView(receiver, options) {
146617
- const rawLength = options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, "length") : getSandboxDataProperty(receiver, "length", options.budget);
147062
+ const rawLength = options.context?.getProperty !== void 0 ? await options.context.getProperty(receiver, "length") : getSandboxDataProperty(receiver, "length", options.budget);
146618
147063
  const number = await sandboxNumber(rawLength, options.budget, options.context);
146619
147064
  const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
146620
- const view = new Proxy(/* @__PURE__ */ Object.create(null), {
146621
- get: (_target, key3) => {
146622
- options.budget.visitNode();
146623
- if (key3 === "length") return length;
146624
- if (typeof key3 !== "string") return void 0;
146625
- return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, key3) : getSandboxDataProperty(receiver, key3, options.budget);
146626
- },
146627
- has: (_target, key3) => {
146628
- options.budget.visitNode();
146629
- if (typeof key3 === "string" && options.hasProperty !== void 0) return options.hasProperty(receiver, key3);
146630
- for (let current = receiver; current !== null; current = getSandboxPrototype(current, options.budget)) {
147065
+ const view = new Proxy(
147066
+ /* @__PURE__ */ Object.create(null),
147067
+ {
147068
+ get: (_target, key3) => {
147069
+ options.budget.visitNode();
147070
+ if (key3 === "length") return length;
147071
+ if (typeof key3 !== "string") return void 0;
147072
+ return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, key3) : getSandboxDataProperty(receiver, key3, options.budget);
147073
+ },
147074
+ has: (_target, key3) => {
147075
+ options.budget.visitNode();
147076
+ if (typeof key3 === "string" && options.hasProperty !== void 0)
147077
+ return options.hasProperty(receiver, key3);
147078
+ for (let current = receiver; current !== null; current = getSandboxPrototype(current, options.budget)) {
147079
+ options.budget.visitNode();
147080
+ if (Object.hasOwn(current, key3)) return true;
147081
+ }
147082
+ return false;
147083
+ },
147084
+ set: (_target, key3, entry) => {
146631
147085
  options.budget.visitNode();
146632
- if (Object.hasOwn(current, key3)) return true;
147086
+ if (typeof key3 !== "string") throw new TypeError("Array indices must be string keys.");
147087
+ if (options.setProperty !== void 0) options.setProperty(receiver, key3, entry);
147088
+ else if (!Reflect.set(receiver, key3, entry))
147089
+ throw new TypeError(`Cannot assign property '${key3}'.`);
147090
+ return true;
147091
+ },
147092
+ deleteProperty: (_target, key3) => {
147093
+ options.budget.visitNode();
147094
+ if (typeof key3 === "string" && options.deleteProperty !== void 0)
147095
+ return options.deleteProperty(receiver, key3);
147096
+ return Reflect.deleteProperty(receiver, key3);
146633
147097
  }
146634
- return false;
146635
- },
146636
- set: (_target, key3, entry) => {
146637
- options.budget.visitNode();
146638
- if (typeof key3 !== "string") throw new TypeError("Array indices must be string keys.");
146639
- if (options.setProperty !== void 0) options.setProperty(receiver, key3, entry);
146640
- else if (!Reflect.set(receiver, key3, entry)) throw new TypeError(`Cannot assign property '${key3}'.`);
146641
- return true;
146642
- },
146643
- deleteProperty: (_target, key3) => {
146644
- options.budget.visitNode();
146645
- if (typeof key3 === "string" && options.deleteProperty !== void 0) return options.deleteProperty(receiver, key3);
146646
- return Reflect.deleteProperty(receiver, key3);
146647
147098
  }
146648
- });
147099
+ );
146649
147100
  arrayLikeSources.set(view, receiver);
146650
147101
  return view;
146651
147102
  }
@@ -146749,166 +147200,371 @@ async function callArrayMethodUnlocked(value, methodName, args, options, stack)
146749
147200
  );
146750
147201
  case "flat":
146751
147202
  return budgetProducedValue(
146752
- flattenArray(value, toIntegerOrInfinity(args[0] ?? 1), options.budget),
147203
+ await flattenArray(
147204
+ value,
147205
+ args[0] === void 0 ? 1 : toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context)),
147206
+ options
147207
+ ),
146753
147208
  options.budget
146754
147209
  );
146755
147210
  case "includes":
146756
- return Reflect.apply(Array.prototype.includes, value, [...args]);
146757
147211
  case "indexOf":
146758
- return Reflect.apply(Array.prototype.indexOf, value, [...args]);
146759
- case "lastIndexOf":
146760
- return Reflect.apply(Array.prototype.lastIndexOf, value, [...args]);
146761
- case "join":
146762
- return options.budget.allocateString(Reflect.apply(Array.prototype.join, value, [...args]));
147212
+ case "lastIndexOf": {
147213
+ const length = value.length;
147214
+ if (length === 0) return methodName === "includes" ? false : -1;
147215
+ const reverse = methodName === "lastIndexOf";
147216
+ const start = reverse && args.length < 2 ? length - 1 : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
147217
+ let index = reverse ? Math.min(start < 0 ? length + start : start, length - 1) : start < 0 ? Math.max(length + start, 0) : start;
147218
+ for (; index >= 0 && index < length; index += reverse ? -1 : 1) {
147219
+ options.budget.visitNode();
147220
+ if (methodName !== "includes" && !(index in value)) continue;
147221
+ const entry = await readArrayElement(value, index, options);
147222
+ if (entry === args[0] || methodName === "includes" && Number.isNaN(entry) && Number.isNaN(args[0]))
147223
+ return methodName === "includes" ? true : index;
147224
+ }
147225
+ return methodName === "includes" ? false : -1;
147226
+ }
147227
+ case "join": {
147228
+ const length = value.length;
147229
+ const separator = args[0] === void 0 ? "," : await sandboxString(args[0], options.budget, options.context);
147230
+ return joinSandboxArray(
147231
+ arrayLikeSources.get(value) ?? value,
147232
+ length,
147233
+ separator,
147234
+ options.budget,
147235
+ options.context
147236
+ );
147237
+ }
146763
147238
  case "slice": {
146764
147239
  const length = value.length;
146765
- const start = toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context));
147240
+ const start = toIntegerOrInfinity(
147241
+ await sandboxNumber(args[0], options.budget, options.context)
147242
+ );
146766
147243
  const end = args[1] === void 0 ? length : toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context));
146767
147244
  const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
146768
147245
  const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
146769
147246
  const count = Math.max(final - first, 0);
146770
147247
  options.budget.allocateArrayLength(count);
146771
147248
  const result = new Array(count);
146772
- for (let index = 0; index < count; index += 1) {
146773
- options.budget.visitNode();
146774
- if (first + index in value) result[index] = value[first + index];
147249
+ const release2 = retainValues(options.budget, () => [result]);
147250
+ try {
147251
+ for (let index = 0; index < count; index += 1) {
147252
+ options.budget.visitNode();
147253
+ if (first + index in value)
147254
+ result[index] = await readArrayElement(value, first + index, options);
147255
+ }
147256
+ return budgetProducedValue(result, options.budget);
147257
+ } finally {
147258
+ release2();
147259
+ }
147260
+ }
147261
+ case "concat": {
147262
+ const result = [];
147263
+ const retained = {};
147264
+ options.budget.setRetainedValues(retained, () => [result]);
147265
+ try {
147266
+ for (const entry of [value, ...args]) {
147267
+ if (!Array.isArray(entry)) {
147268
+ options.budget.allocateArrayLength(result.length + 1);
147269
+ result.push(entry);
147270
+ continue;
147271
+ }
147272
+ const start = result.length;
147273
+ const length = entry.length;
147274
+ options.budget.allocateArrayLength(start + length);
147275
+ result.length += length;
147276
+ for (let index = 0; index < length; index++) {
147277
+ options.budget.visitNode();
147278
+ if (index in entry)
147279
+ result[start + index] = await readArrayElement(entry, index, options);
147280
+ }
147281
+ }
147282
+ return budgetProducedValue(result, options.budget);
147283
+ } finally {
147284
+ options.budget.setRetainedValues(retained, void 0);
146775
147285
  }
146776
- return budgetProducedValue(result, options.budget);
146777
147286
  }
146778
- case "concat":
146779
- return budgetProducedValue(
146780
- Reflect.apply(Array.prototype.concat, value, [...args]),
146781
- options.budget
146782
- );
146783
147287
  case "splice": {
146784
- const removed = Reflect.apply(Array.prototype.splice, value, [...args]);
147288
+ const length = value.length;
147289
+ const start = toIntegerOrInfinity(
147290
+ await sandboxNumber(args[0], options.budget, options.context)
147291
+ );
147292
+ const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
147293
+ const inserted = Math.max(args.length - 2, 0);
147294
+ const deleted = args.length === 0 ? 0 : args.length === 1 ? length - first : Math.min(
147295
+ Math.max(
147296
+ toIntegerOrInfinity(
147297
+ await sandboxNumber(args[1], options.budget, options.context)
147298
+ ),
147299
+ 0
147300
+ ),
147301
+ length - first
147302
+ );
147303
+ const nextLength = length + inserted - deleted;
147304
+ if (nextLength > Number.MAX_SAFE_INTEGER)
147305
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
147306
+ options.budget.allocateArrayLength(deleted);
147307
+ const removed = new Array(deleted);
147308
+ const retained = {};
147309
+ options.budget.setRetainedValues(retained, () => [removed]);
147310
+ try {
147311
+ for (let index = 0; index < deleted; index++) {
147312
+ options.budget.visitNode();
147313
+ if (first + index in value)
147314
+ removed[index] = await readArrayElement(value, first + index, options);
147315
+ }
147316
+ if (inserted < deleted) {
147317
+ for (let index = first; index < length - deleted; index++)
147318
+ await moveArrayElement(value, index + deleted, index + inserted, options);
147319
+ for (let index = length; index > nextLength; index--)
147320
+ deleteArrayElement(value, index - 1, options);
147321
+ } else if (inserted > deleted) {
147322
+ for (let index = length - deleted; index > first; index--)
147323
+ await moveArrayElement(value, index + deleted - 1, index + inserted - 1, options);
147324
+ }
147325
+ for (let index = 0; index < inserted; index++)
147326
+ await writeArrayProperty(value, first + index, args[index + 2], options);
147327
+ await writeArrayProperty(value, "length", nextLength, options);
147328
+ } finally {
147329
+ options.budget.setRetainedValues(retained, void 0);
147330
+ }
146785
147331
  budgetProducedValue(removed, options.budget);
146786
147332
  budgetProducedValue(value, options.budget);
146787
147333
  return removed;
146788
147334
  }
146789
- case "fill":
146790
- Reflect.apply(Array.prototype.fill, value, [...args]);
147335
+ case "fill": {
147336
+ const length = value.length;
147337
+ const start = toIntegerOrInfinity(
147338
+ await sandboxNumber(args[1], options.budget, options.context)
147339
+ );
147340
+ const end = args[2] === void 0 ? length : toIntegerOrInfinity(await sandboxNumber(args[2], options.budget, options.context));
147341
+ const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
147342
+ const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
147343
+ for (let index = first; index < final; index++)
147344
+ await writeArrayProperty(value, index, args[0], options);
146791
147345
  budgetProducedValue(value, options.budget);
146792
147346
  return value;
146793
- case "copyWithin":
146794
- Reflect.apply(Array.prototype.copyWithin, value, [...args]);
147347
+ }
147348
+ case "copyWithin": {
147349
+ const length = value.length;
147350
+ const target = toIntegerOrInfinity(
147351
+ await sandboxNumber(args[0], options.budget, options.context)
147352
+ );
147353
+ const start = toIntegerOrInfinity(
147354
+ await sandboxNumber(args[1], options.budget, options.context)
147355
+ );
147356
+ const end = args[2] === void 0 ? length : toIntegerOrInfinity(await sandboxNumber(args[2], options.budget, options.context));
147357
+ let to = target < 0 ? Math.max(length + target, 0) : Math.min(target, length);
147358
+ let from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
147359
+ const final = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
147360
+ let count = Math.min(final - from, length - to);
147361
+ const direction = from < to && to < from + count ? -1 : 1;
147362
+ if (direction < 0) {
147363
+ from += count - 1;
147364
+ to += count - 1;
147365
+ }
147366
+ while (count-- > 0) {
147367
+ await moveArrayElement(value, from, to, options);
147368
+ from += direction;
147369
+ to += direction;
147370
+ }
146795
147371
  budgetProducedValue(value, options.budget);
146796
147372
  return value;
146797
- case "at":
146798
- return budgetProducedValue(
146799
- Reflect.apply(Array.prototype.at, value, [...args]),
146800
- options.budget
147373
+ }
147374
+ case "at": {
147375
+ const length = value.length;
147376
+ const index = toIntegerOrInfinity(
147377
+ await sandboxNumber(args[0], options.budget, options.context)
146801
147378
  );
147379
+ const actual = index < 0 ? length + index : index;
147380
+ return actual < 0 || actual >= length ? void 0 : budgetProducedValue(await readArrayElement(value, actual, options), options.budget);
147381
+ }
146802
147382
  case "sort":
146803
- if (args[0] === void 0) {
146804
- Reflect.apply(Array.prototype.sort, value, []);
147383
+ await sortArray(
147384
+ value,
147385
+ args[0] === void 0 ? void 0 : getRequiredCallback(methodName, args[0]),
147386
+ options,
147387
+ stack
147388
+ );
147389
+ budgetProducedValue(value, options.budget);
147390
+ return value;
147391
+ case "reverse": {
147392
+ const length = value.length;
147393
+ let lowerValue;
147394
+ let upperValue;
147395
+ const release2 = retainValues(options.budget, () => [lowerValue, upperValue]);
147396
+ try {
147397
+ for (let lower = 0; lower < Math.floor(length / 2); lower++) {
147398
+ options.budget.visitNode();
147399
+ lowerValue = upperValue = void 0;
147400
+ const upper = length - lower - 1;
147401
+ const lowerExists = lower in value;
147402
+ lowerValue = lowerExists ? await readArrayElement(value, lower, options) : void 0;
147403
+ const upperExists = upper in value;
147404
+ upperValue = upperExists ? await readArrayElement(value, upper, options) : void 0;
147405
+ if (lowerExists && upperExists) {
147406
+ await writeArrayProperty(value, lower, upperValue, options);
147407
+ await writeArrayProperty(value, upper, lowerValue, options);
147408
+ } else if (!lowerExists && upperExists) {
147409
+ await writeArrayProperty(value, lower, upperValue, options);
147410
+ deleteArrayElement(value, upper, options);
147411
+ } else if (lowerExists) {
147412
+ deleteArrayElement(value, lower, options);
147413
+ await writeArrayProperty(value, upper, lowerValue, options);
147414
+ }
147415
+ }
146805
147416
  budgetProducedValue(value, options.budget);
146806
147417
  return value;
147418
+ } finally {
147419
+ release2();
146807
147420
  }
146808
- await sortArray(value, getRequiredCallback(methodName, args[0]), options, stack);
146809
- budgetProducedValue(value, options.budget);
146810
- return value;
146811
- case "reverse":
146812
- Reflect.apply(Array.prototype.reverse, value, []);
146813
- budgetProducedValue(value, options.budget);
146814
- return value;
147421
+ }
146815
147422
  case "toSorted": {
146816
147423
  const length = value.length;
146817
147424
  options.budget.allocateArrayLength(length);
146818
147425
  const result = new Array(length);
146819
- for (let index = 0; index < length; index += 1) {
146820
- options.budget.visitNode();
146821
- result[index] = value[index];
146822
- }
146823
- if (args[0] === void 0) {
146824
- result.sort();
146825
- } else {
146826
- await sortArray(result, getRequiredCallback(methodName, args[0]), options, stack);
147426
+ const release2 = retainValues(options.budget, () => [result]);
147427
+ try {
147428
+ for (let index = 0; index < length; index += 1) {
147429
+ options.budget.visitNode();
147430
+ result[index] = await readArrayElement(value, index, options);
147431
+ }
147432
+ await sortArray(
147433
+ result,
147434
+ args[0] === void 0 ? void 0 : getRequiredCallback(methodName, args[0]),
147435
+ options,
147436
+ stack
147437
+ );
147438
+ return budgetProducedValue(result, options.budget);
147439
+ } finally {
147440
+ release2();
146827
147441
  }
146828
- return budgetProducedValue(result, options.budget);
146829
147442
  }
146830
147443
  case "toReversed": {
146831
147444
  const length = value.length;
146832
147445
  options.budget.allocateArrayLength(length);
146833
147446
  const result = new Array(length);
146834
- for (let index = 0; index < length; index += 1) {
146835
- options.budget.visitNode();
146836
- result[index] = value[length - index - 1];
147447
+ const release2 = retainValues(options.budget, () => [result]);
147448
+ try {
147449
+ for (let index = 0; index < length; index += 1) {
147450
+ options.budget.visitNode();
147451
+ result[index] = await readArrayElement(value, length - index - 1, options);
147452
+ }
147453
+ return budgetProducedValue(result, options.budget);
147454
+ } finally {
147455
+ release2();
146837
147456
  }
146838
- return budgetProducedValue(result, options.budget);
146839
147457
  }
146840
147458
  case "toSpliced": {
146841
147459
  const length = value.length;
146842
- const start = toIntegerOrInfinity(await sandboxNumber(args[0], options.budget, options.context));
147460
+ const start = toIntegerOrInfinity(
147461
+ await sandboxNumber(args[0], options.budget, options.context)
147462
+ );
146843
147463
  const first = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
146844
147464
  const inserted = Math.max(args.length - 2, 0);
146845
- const deleted = args.length === 0 ? 0 : args.length === 1 ? length - first : Math.min(Math.max(toIntegerOrInfinity(await sandboxNumber(args[1], options.budget, options.context)), 0), length - first);
147465
+ const deleted = args.length === 0 ? 0 : args.length === 1 ? length - first : Math.min(
147466
+ Math.max(
147467
+ toIntegerOrInfinity(
147468
+ await sandboxNumber(args[1], options.budget, options.context)
147469
+ ),
147470
+ 0
147471
+ ),
147472
+ length - first
147473
+ );
146846
147474
  const resultLength = length + inserted - deleted;
146847
- if (resultLength > Number.MAX_SAFE_INTEGER) throw new TypeError("Array-like length exceeds the safe integer limit.");
147475
+ if (resultLength > Number.MAX_SAFE_INTEGER)
147476
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
146848
147477
  options.budget.allocateArrayLength(resultLength);
146849
147478
  const result = new Array(resultLength);
146850
- for (let index = 0; index < resultLength; index += 1) {
146851
- options.budget.visitNode();
146852
- result[index] = index < first ? value[index] : index < first + inserted ? args[index - first + 2] : value[index - inserted + deleted];
147479
+ const release2 = retainValues(options.budget, () => [result]);
147480
+ try {
147481
+ for (let index = 0; index < resultLength; index += 1) {
147482
+ options.budget.visitNode();
147483
+ result[index] = index < first ? await readArrayElement(value, index, options) : index < first + inserted ? args[index - first + 2] : await readArrayElement(value, index - inserted + deleted, options);
147484
+ }
147485
+ return budgetProducedValue(result, options.budget);
147486
+ } finally {
147487
+ release2();
146853
147488
  }
146854
- return budgetProducedValue(result, options.budget);
146855
147489
  }
146856
147490
  case "with": {
146857
147491
  const length = value.length;
146858
- const index = toIntegerOrInfinity(args[0]);
147492
+ const index = toIntegerOrInfinity(
147493
+ options.context === void 0 ? args[0] : await sandboxNumber(args[0], options.budget, options.context)
147494
+ );
146859
147495
  const actualIndex = index < 0 ? length + index : index;
146860
147496
  if (actualIndex < 0 || actualIndex >= length) {
146861
147497
  throw new RangeError("Invalid index");
146862
147498
  }
146863
147499
  options.budget.allocateArrayLength(length);
146864
147500
  const result = [];
146865
- for (let position = 0; position < length; position += 1) {
146866
- options.budget.visitNode();
146867
- result[position] = position === actualIndex ? args[1] : Array.isArray(value) && !Object.hasOwn(value, position) ? void 0 : value[position];
147501
+ const release2 = retainValues(options.budget, () => [result]);
147502
+ try {
147503
+ for (let position = 0; position < length; position += 1) {
147504
+ options.budget.visitNode();
147505
+ result[position] = position === actualIndex ? args[1] : await readArrayElement(value, position, options);
147506
+ }
147507
+ return budgetProducedValue(result, options.budget);
147508
+ } finally {
147509
+ release2();
146868
147510
  }
146869
- return budgetProducedValue(result, options.budget);
146870
147511
  }
146871
147512
  case "push": {
146872
- const nextLength = appendArrayValues(value, args);
147513
+ const nextLength = await appendArrayValues(value, args, options);
146873
147514
  budgetProducedValue(value, options.budget);
146874
147515
  return nextLength;
146875
147516
  }
146876
147517
  case "pop":
146877
- return budgetProducedValue(Reflect.apply(Array.prototype.pop, value, []), options.budget);
146878
- case "shift":
146879
- return budgetProducedValue(Reflect.apply(Array.prototype.shift, value, []), options.budget);
147518
+ case "shift": {
147519
+ const length = value.length;
147520
+ if (length === 0) {
147521
+ await writeArrayProperty(value, "length", 0, options);
147522
+ return void 0;
147523
+ }
147524
+ const result = await readArrayElement(value, methodName === "pop" ? length - 1 : 0, options);
147525
+ const retained = {};
147526
+ options.budget.setRetainedValues(retained, () => [result]);
147527
+ try {
147528
+ if (methodName === "shift")
147529
+ for (let index = 1; index < length; index++)
147530
+ await moveArrayElement(value, index, index - 1, options);
147531
+ deleteArrayElement(value, length - 1, options);
147532
+ await writeArrayProperty(value, "length", length - 1, options);
147533
+ return budgetProducedValue(result, options.budget);
147534
+ } finally {
147535
+ options.budget.setRetainedValues(retained, void 0);
147536
+ }
147537
+ }
146880
147538
  case "unshift": {
146881
- const nextLength = prependArrayValues(value, args);
147539
+ const nextLength = await prependArrayValues(value, args, options);
146882
147540
  budgetProducedValue(value, options.budget);
146883
147541
  return nextLength;
146884
147542
  }
146885
147543
  }
146886
147544
  }
146887
- function appendArrayValues(target, values) {
147545
+ async function appendArrayValues(target, values, options) {
146888
147546
  let length = target.length;
146889
- if (length + values.length > Number.MAX_SAFE_INTEGER) throw new TypeError("Array-like length exceeds the safe integer limit.");
147547
+ if (length + values.length > Number.MAX_SAFE_INTEGER)
147548
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
146890
147549
  for (const value of values) {
146891
- target[length++] = value;
147550
+ await writeArrayProperty(target, length++, value, options);
146892
147551
  }
146893
- target.length = length;
147552
+ await writeArrayProperty(target, "length", length, options);
146894
147553
  return length;
146895
147554
  }
146896
- function prependArrayValues(target, values) {
147555
+ async function prependArrayValues(target, values, options) {
146897
147556
  const originalLength = target.length;
146898
147557
  const length = originalLength + values.length;
146899
- if (length > Number.MAX_SAFE_INTEGER) throw new TypeError("Array-like length exceeds the safe integer limit.");
147558
+ if (length > Number.MAX_SAFE_INTEGER)
147559
+ throw new TypeError("Array-like length exceeds the safe integer limit.");
146900
147560
  for (let index = values.length === 0 ? -1 : originalLength - 1; index >= 0; index -= 1) {
146901
147561
  const targetIndex = index + values.length;
146902
- if (index in target) {
146903
- target[targetIndex] = target[index];
146904
- } else {
146905
- delete target[targetIndex];
146906
- }
147562
+ await moveArrayElement(target, index, targetIndex, options);
146907
147563
  }
146908
147564
  for (let index = 0; index < values.length; index += 1) {
146909
- target[index] = values[index];
147565
+ await writeArrayProperty(target, index, values[index], options);
146910
147566
  }
146911
- target.length = length;
147567
+ await writeArrayProperty(target, "length", length, options);
146912
147568
  return length;
146913
147569
  }
146914
147570
  function isCallbackArrayMethod(methodName) {
@@ -146923,6 +147579,33 @@ function getRequiredCallback(methodName, value) {
146923
147579
  }
146924
147580
  return value;
146925
147581
  }
147582
+ function readArrayElement(value, index, options) {
147583
+ const receiver = arrayLikeSources.get(value) ?? value;
147584
+ return options.context?.getProperty !== void 0 ? options.context.getProperty(receiver, index) : getSandboxDataProperty(receiver, index, options.budget);
147585
+ }
147586
+ function writeArrayProperty(value, key3, entry, options) {
147587
+ options.budget.visitNode();
147588
+ const receiver = arrayLikeSources.get(value) ?? value;
147589
+ if (Array.isArray(receiver)) {
147590
+ if (key3 === "length") options.budget.allocateArrayLength(Number(entry));
147591
+ else if (typeof key3 === "number") options.budget.allocateArrayLength(key3 + 1);
147592
+ }
147593
+ if (options.setProperty !== void 0) return options.setProperty(receiver, String(key3), entry);
147594
+ if (!Reflect.set(receiver, key3, entry))
147595
+ throw new TypeError(`Cannot assign to read only property '${key3}'.`);
147596
+ }
147597
+ function deleteArrayElement(value, index, options) {
147598
+ options.budget.visitNode();
147599
+ const receiver = arrayLikeSources.get(value) ?? value;
147600
+ const deleted = options.deleteProperty === void 0 ? Reflect.deleteProperty(receiver, String(index)) : options.deleteProperty(receiver, String(index));
147601
+ if (!deleted) throw new TypeError(`Cannot delete property '${index}'.`);
147602
+ }
147603
+ async function moveArrayElement(value, from, to, options) {
147604
+ options.budget.visitNode();
147605
+ if (from in value)
147606
+ await writeArrayProperty(value, to, await readArrayElement(value, from, options), options);
147607
+ else deleteArrayElement(value, to, options);
147608
+ }
146926
147609
  async function mapArray(value, callback, options, stack, thisValue) {
146927
147610
  const length = value.length;
146928
147611
  options.budget.allocateArrayLength(length);
@@ -146936,7 +147619,7 @@ async function mapArray(value, callback, options, stack, thisValue) {
146936
147619
  }
146937
147620
  result[index] = await callArrayCallback(
146938
147621
  callback,
146939
- value[index],
147622
+ await readArrayElement(value, index, options),
146940
147623
  index,
146941
147624
  value,
146942
147625
  options,
@@ -146959,7 +147642,7 @@ async function filterArray(value, callback, options, stack, thisValue) {
146959
147642
  if (!(index in value)) {
146960
147643
  continue;
146961
147644
  }
146962
- const entry = value[index];
147645
+ const entry = await readArrayElement(value, index, options);
146963
147646
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
146964
147647
  result.push(entry);
146965
147648
  }
@@ -146973,7 +147656,7 @@ async function findInArray(value, callback, options, stack, thisValue) {
146973
147656
  const length = value.length;
146974
147657
  for (let index = 0; index < length; index += 1) {
146975
147658
  options.budget.visitNode();
146976
- const entry = index in value ? value[index] : void 0;
147659
+ const entry = await readArrayElement(value, index, options);
146977
147660
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
146978
147661
  return entry;
146979
147662
  }
@@ -146984,7 +147667,7 @@ async function findIndexInArray(value, callback, options, stack, thisValue) {
146984
147667
  const length = value.length;
146985
147668
  for (let index = 0; index < length; index += 1) {
146986
147669
  options.budget.visitNode();
146987
- const entry = index in value ? value[index] : void 0;
147670
+ const entry = await readArrayElement(value, index, options);
146988
147671
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
146989
147672
  return index;
146990
147673
  }
@@ -146995,7 +147678,7 @@ async function findLastInArray(value, callback, options, stack, thisValue) {
146995
147678
  const length = value.length;
146996
147679
  for (let index = length - 1; index >= 0; index -= 1) {
146997
147680
  options.budget.visitNode();
146998
- const entry = index in value ? value[index] : void 0;
147681
+ const entry = await readArrayElement(value, index, options);
146999
147682
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
147000
147683
  return entry;
147001
147684
  }
@@ -147006,7 +147689,7 @@ async function findLastIndexInArray(value, callback, options, stack, thisValue)
147006
147689
  const length = value.length;
147007
147690
  for (let index = length - 1; index >= 0; index -= 1) {
147008
147691
  options.budget.visitNode();
147009
- const entry = index in value ? value[index] : void 0;
147692
+ const entry = await readArrayElement(value, index, options);
147010
147693
  if (await callArrayCallback(callback, entry, index, value, options, stack, thisValue)) {
147011
147694
  return index;
147012
147695
  }
@@ -147020,7 +147703,15 @@ async function someInArray(value, callback, options, stack, thisValue) {
147020
147703
  if (!(index in value)) {
147021
147704
  continue;
147022
147705
  }
147023
- if (await callArrayCallback(callback, value[index], index, value, options, stack, thisValue)) {
147706
+ if (await callArrayCallback(
147707
+ callback,
147708
+ await readArrayElement(value, index, options),
147709
+ index,
147710
+ value,
147711
+ options,
147712
+ stack,
147713
+ thisValue
147714
+ )) {
147024
147715
  return true;
147025
147716
  }
147026
147717
  }
@@ -147033,7 +147724,15 @@ async function everyInArray(value, callback, options, stack, thisValue) {
147033
147724
  if (!(index in value)) {
147034
147725
  continue;
147035
147726
  }
147036
- if (!await callArrayCallback(callback, value[index], index, value, options, stack, thisValue)) {
147727
+ if (!await callArrayCallback(
147728
+ callback,
147729
+ await readArrayElement(value, index, options),
147730
+ index,
147731
+ value,
147732
+ options,
147733
+ stack,
147734
+ thisValue
147735
+ )) {
147037
147736
  return false;
147038
147737
  }
147039
147738
  }
@@ -147048,7 +147747,15 @@ async function reduceArray(value, callback, hasInitialValue, initialValue, optio
147048
147747
  if (start < 0) {
147049
147748
  throw new TypeError("Reduce of empty array with no initial value.");
147050
147749
  }
147051
- return reduceFromLeft(value, callback, value[start], start + 1, length, options, stack);
147750
+ return reduceFromLeft(
147751
+ value,
147752
+ callback,
147753
+ await readArrayElement(value, start, options),
147754
+ start + 1,
147755
+ length,
147756
+ options,
147757
+ stack
147758
+ );
147052
147759
  }
147053
147760
  async function reduceRightArray(value, callback, hasInitialValue, initialValue, options, stack) {
147054
147761
  const length = value.length;
@@ -147059,7 +147766,15 @@ async function reduceRightArray(value, callback, hasInitialValue, initialValue,
147059
147766
  if (start < 0) {
147060
147767
  throw new TypeError("Reduce of empty array with no initial value.");
147061
147768
  }
147062
- return reduceFromRight(value, callback, value[start], start - 1, length, options, stack);
147769
+ return reduceFromRight(
147770
+ value,
147771
+ callback,
147772
+ await readArrayElement(value, start, options),
147773
+ start - 1,
147774
+ length,
147775
+ options,
147776
+ stack
147777
+ );
147063
147778
  }
147064
147779
  async function reduceFromLeft(value, callback, accumulator, startIndex, length, options, stack) {
147065
147780
  let current = accumulator;
@@ -147071,7 +147786,16 @@ async function reduceFromLeft(value, callback, accumulator, startIndex, length,
147071
147786
  if (!(index in value)) {
147072
147787
  continue;
147073
147788
  }
147074
- current = await options.callClosure(callback, [current, value[index], index, arrayLikeSources.get(value) ?? value], stack);
147789
+ current = await options.callClosure(
147790
+ callback,
147791
+ [
147792
+ current,
147793
+ await readArrayElement(value, index, options),
147794
+ index,
147795
+ arrayLikeSources.get(value) ?? value
147796
+ ],
147797
+ stack
147798
+ );
147075
147799
  }
147076
147800
  return current;
147077
147801
  } finally {
@@ -147088,7 +147812,16 @@ async function reduceFromRight(value, callback, accumulator, startIndex, length,
147088
147812
  if (!(index in value)) {
147089
147813
  continue;
147090
147814
  }
147091
- current = await options.callClosure(callback, [current, value[index], index, arrayLikeSources.get(value) ?? value], stack);
147815
+ current = await options.callClosure(
147816
+ callback,
147817
+ [
147818
+ current,
147819
+ await readArrayElement(value, index, options),
147820
+ index,
147821
+ arrayLikeSources.get(value) ?? value
147822
+ ],
147823
+ stack
147824
+ );
147092
147825
  }
147093
147826
  return current;
147094
147827
  } finally {
@@ -147102,7 +147835,15 @@ async function forEachArray(value, callback, options, stack, thisValue) {
147102
147835
  if (!(index in value)) {
147103
147836
  continue;
147104
147837
  }
147105
- await callArrayCallback(callback, value[index], index, value, options, stack, thisValue);
147838
+ await callArrayCallback(
147839
+ callback,
147840
+ await readArrayElement(value, index, options),
147841
+ index,
147842
+ value,
147843
+ options,
147844
+ stack,
147845
+ thisValue
147846
+ );
147106
147847
  }
147107
147848
  }
147108
147849
  async function flatMapArray(value, callback, options, stack, thisValue) {
@@ -147117,7 +147858,7 @@ async function flatMapArray(value, callback, options, stack, thisValue) {
147117
147858
  }
147118
147859
  const mapped = await callArrayCallback(
147119
147860
  callback,
147120
- value[index],
147861
+ await readArrayElement(value, index, options),
147121
147862
  index,
147122
147863
  value,
147123
147864
  options,
@@ -147130,7 +147871,7 @@ async function flatMapArray(value, callback, options, stack, thisValue) {
147130
147871
  if (!(mappedIndex in mapped)) {
147131
147872
  continue;
147132
147873
  }
147133
- result.push(mapped[mappedIndex]);
147874
+ result.push(await readArrayElement(mapped, mappedIndex, options));
147134
147875
  options.budget.allocateArrayLength(result.length);
147135
147876
  }
147136
147877
  continue;
@@ -147143,23 +147884,31 @@ async function flatMapArray(value, callback, options, stack, thisValue) {
147143
147884
  options.budget.setRetainedValues(result, void 0);
147144
147885
  }
147145
147886
  }
147146
- function flattenArray(value, depth, budget) {
147887
+ async function flattenArray(value, depth, options) {
147147
147888
  const result = [];
147148
- appendFlattenedEntries(value, depth, result, budget);
147149
- return result;
147889
+ const retained = {};
147890
+ options.budget.setRetainedValues(retained, () => [result]);
147891
+ try {
147892
+ await appendFlattenedEntries(value, depth, result, options);
147893
+ return result;
147894
+ } finally {
147895
+ options.budget.setRetainedValues(retained, void 0);
147896
+ }
147150
147897
  }
147151
- function appendFlattenedEntries(value, depth, result, budget) {
147152
- for (let index = 0; index < value.length; index += 1) {
147898
+ async function appendFlattenedEntries(value, depth, result, options) {
147899
+ const length = value.length;
147900
+ for (let index = 0; index < length; index += 1) {
147901
+ options.budget.visitNode();
147153
147902
  if (!(index in value)) {
147154
147903
  continue;
147155
147904
  }
147156
- const entry = value[index];
147905
+ const entry = await readArrayElement(value, index, options);
147157
147906
  if (depth > 0 && Array.isArray(entry)) {
147158
- appendFlattenedEntries(entry, depth - 1, result, budget);
147907
+ await appendFlattenedEntries(entry, depth - 1, result, options);
147159
147908
  continue;
147160
147909
  }
147161
147910
  result.push(entry);
147162
- budget.allocateArrayLength(result.length);
147911
+ options.budget.allocateArrayLength(result.length);
147163
147912
  }
147164
147913
  }
147165
147914
  async function sortArray(value, comparator, options, stack) {
@@ -147174,7 +147923,7 @@ async function sortArray(value, comparator, options, stack) {
147174
147923
  if (!(index in value)) {
147175
147924
  continue;
147176
147925
  }
147177
- const entry = value[index];
147926
+ const entry = await readArrayElement(value, index, options);
147178
147927
  if (entry === void 0) {
147179
147928
  undefinedCount += 1;
147180
147929
  continue;
@@ -147192,14 +147941,14 @@ async function sortArray(value, comparator, options, stack) {
147192
147941
  }
147193
147942
  currentEntry = void 0;
147194
147943
  for (let index = 0; index < definedValues.length; index += 1) {
147195
- value[index] = definedValues[index];
147944
+ await writeArrayProperty(value, index, definedValues[index], options);
147196
147945
  }
147197
147946
  for (let index = 0; index < undefinedCount; index += 1) {
147198
- value[definedValues.length + index] = void 0;
147947
+ await writeArrayProperty(value, definedValues.length + index, void 0, options);
147199
147948
  }
147200
147949
  for (let index = definedValues.length + undefinedCount; index < length; index += 1) {
147201
147950
  options.budget.visitNode();
147202
- delete value[index];
147951
+ deleteArrayElement(value, index, options);
147203
147952
  }
147204
147953
  } finally {
147205
147954
  options.budget.setRetainedValues(definedValues, void 0);
@@ -147207,11 +147956,30 @@ async function sortArray(value, comparator, options, stack) {
147207
147956
  }
147208
147957
  async function compareEntries(left, right, comparator, options, stack) {
147209
147958
  options.budget.visitNode();
147210
- const result = Number(await options.callClosure(comparator, [left, right], stack));
147959
+ if (comparator === void 0) {
147960
+ const leftString = await sandboxString(left, options.budget, options.context);
147961
+ const release2 = retainValues(options.budget, () => [leftString]);
147962
+ try {
147963
+ const rightString = await sandboxString(right, options.budget, options.context);
147964
+ return leftString < rightString ? -1 : leftString > rightString ? 1 : 0;
147965
+ } finally {
147966
+ release2();
147967
+ }
147968
+ }
147969
+ const result = await sandboxNumber(
147970
+ await options.callClosure(comparator, [left, right], stack),
147971
+ options.budget,
147972
+ options.context
147973
+ );
147211
147974
  return Number.isNaN(result) ? 0 : result;
147212
147975
  }
147213
147976
  async function callArrayCallback(callback, value, index, array, options, stack, thisValue) {
147214
- return options.callClosure(callback, [value, index, arrayLikeSources.get(array) ?? array], stack, thisValue);
147977
+ return options.callClosure(
147978
+ callback,
147979
+ [value, index, arrayLikeSources.get(array) ?? array],
147980
+ stack,
147981
+ thisValue
147982
+ );
147215
147983
  }
147216
147984
  function findNextDefinedIndex(value, startIndex, direction, length, budget) {
147217
147985
  for (let index = startIndex; direction > 0 ? index < length : index >= 0; index += direction) {
@@ -147294,6 +148062,7 @@ var init_array = __esm({
147294
148062
  init_boxed();
147295
148063
  init_values();
147296
148064
  init_running_state();
148065
+ init_resources();
147297
148066
  init_object_model();
147298
148067
  init_string_coercion();
147299
148068
  arrayLikeSources = /* @__PURE__ */ new WeakMap();
@@ -147343,7 +148112,8 @@ function getFunctionMember(target, property, options) {
147343
148112
  while (current !== null) {
147344
148113
  if (isGuestClosure(current)) {
147345
148114
  const value = getGuestFunctionProperty(current, String(property));
147346
- if (value !== void 0 || Object.hasOwn(current.properties ?? {}, String(property))) return value;
148115
+ if (value !== void 0 || Object.hasOwn(current.properties ?? {}, String(property)))
148116
+ return value;
147347
148117
  } else if (isSandboxClosure(current)) {
147348
148118
  if (current.properties !== void 0 && Object.hasOwn(current.properties, String(property)))
147349
148119
  return current.properties[String(property)];
@@ -147359,7 +148129,8 @@ function getFunctionMember(target, property, options) {
147359
148129
  }
147360
148130
  }
147361
148131
  if (current === null) return void 0;
147362
- if (property === "toString" && runResources.getStore()?.functionSourceText === false) return void 0;
148132
+ if (property === "toString" && runResources.getStore()?.functionSourceText === false)
148133
+ return void 0;
147363
148134
  if (!isFunctionMethodName(property)) {
147364
148135
  return void 0;
147365
148136
  }
@@ -147367,13 +148138,13 @@ function getFunctionMember(target, property, options) {
147367
148138
  sandbox: true,
147368
148139
  name: `Function#${property}`,
147369
148140
  ...property === "toString" ? { length: 0 } : {},
147370
- call: (args, context) => callFunctionMethod(context?.thisValue, property, args, options, context?.stack ?? [])
148141
+ call: (args, context) => callFunctionMethod(context?.thisValue, property, args, options, context?.stack ?? [], context)
147371
148142
  });
147372
148143
  }
147373
148144
  function isFunctionMethodName(property) {
147374
148145
  return typeof property === "string" && functionMethodNames.has(property);
147375
148146
  }
147376
- function callFunctionMethod(target, methodName, args, options, stack) {
148147
+ function callFunctionMethod(target, methodName, args, options, stack, context) {
147377
148148
  if (!isSandboxClosure(target)) {
147378
148149
  throw new TypeError(`Function#${methodName} requires a callable receiver.`);
147379
148150
  }
@@ -147384,28 +148155,46 @@ function callFunctionMethod(target, methodName, args, options, stack) {
147384
148155
  const thisValue = args[0];
147385
148156
  if (methodName === "bind") {
147386
148157
  const boundArgs = args.slice(1);
147387
- const bound = createSandboxClosure({
147388
- guest: true,
147389
- sandbox: true,
147390
- name: `bound ${target.name ?? ""}`,
147391
- length: target.length === void 0 ? void 0 : Math.max(0, target.length - boundArgs.length),
147392
- boundTarget: target,
147393
- retainedValues: () => [target, thisValue, ...boundArgs],
147394
- call: (callArgs, context) => options.callClosure(target, [...boundArgs, ...callArgs], context?.stack ?? [], thisValue),
147395
- ...target.construct === void 0 ? {} : {
147396
- construct: (callArgs, context) => options.callClosure(
147397
- target,
147398
- [...boundArgs, ...callArgs],
147399
- context?.stack ?? [],
147400
- void 0,
147401
- true,
147402
- context?.newTarget === bound ? target : context?.newTarget
147403
- )
147404
- }
147405
- });
147406
- if (hasExplicitSandboxPrototype(target))
147407
- setSandboxPrototype(bound, getSandboxPrototype(target, options.budget), options.budget);
147408
- return bound;
148158
+ const bind2 = (length, name) => {
148159
+ const bound = createSandboxClosure({
148160
+ guest: true,
148161
+ sandbox: true,
148162
+ name: `bound ${name}`,
148163
+ length,
148164
+ boundTarget: target,
148165
+ retainedValues: () => [target, thisValue, ...boundArgs, bound.name],
148166
+ call: (callArgs, context2) => options.callClosure(target, [...boundArgs, ...callArgs], context2?.stack ?? [], thisValue),
148167
+ ...target.construct === void 0 ? {} : {
148168
+ construct: (callArgs, context2) => options.callClosure(
148169
+ target,
148170
+ [...boundArgs, ...callArgs],
148171
+ context2?.stack ?? [],
148172
+ void 0,
148173
+ true,
148174
+ context2?.newTarget === bound ? target : context2?.newTarget
148175
+ )
148176
+ }
148177
+ });
148178
+ if (hasExplicitSandboxPrototype(target))
148179
+ setSandboxPrototype(bound, getSandboxPrototype(target, options.budget), options.budget);
148180
+ return bound;
148181
+ };
148182
+ if (context?.getProperty === void 0)
148183
+ return bind2(
148184
+ target.length === void 0 ? void 0 : Math.max(0, target.length - boundArgs.length),
148185
+ target.name ?? ""
148186
+ );
148187
+ return (async () => {
148188
+ const properties = target.properties;
148189
+ const defaultName = target.name;
148190
+ const hasLength = !isGuestClosure(target) || target.properties === void 0 || Object.hasOwn(target.properties, "length");
148191
+ const length = hasLength ? await context.getProperty(target, "length") : void 0;
148192
+ const name = !isGuestClosure(target) && !Object.hasOwn(properties ?? {}, "name") ? defaultName : await context.getProperty(target, "name");
148193
+ return bind2(
148194
+ typeof length === "number" && !Number.isNaN(length) ? Math.max(0, Math.trunc(length) - boundArgs.length) : 0,
148195
+ typeof name === "string" ? name : ""
148196
+ );
148197
+ })();
147409
148198
  }
147410
148199
  if (methodName === "call") {
147411
148200
  return options.callClosure(target, args.slice(1), stack, thisValue);
@@ -147417,7 +148206,24 @@ function callFunctionMethod(target, methodName, args, options, stack) {
147417
148206
  if (!Array.isArray(applyArgs)) {
147418
148207
  throw new TypeError("Function#apply requires an array or nullish arguments value.");
147419
148208
  }
147420
- return options.callClosure(target, applyArgs, stack, thisValue);
148209
+ if (context?.getProperty === void 0)
148210
+ return options.callClosure(target, applyArgs, stack, thisValue);
148211
+ return (async () => {
148212
+ const values = [];
148213
+ const length = applyArgs.length;
148214
+ options.budget?.allocateArrayLength(length);
148215
+ const retained = {};
148216
+ options.budget?.setRetainedValues(retained, () => values);
148217
+ try {
148218
+ for (let index = 0; index < length; index++) {
148219
+ options.budget?.visitNode();
148220
+ values.push(await context.getProperty(applyArgs, index));
148221
+ }
148222
+ return await options.callClosure(target, values, stack, thisValue);
148223
+ } finally {
148224
+ options.budget?.setRetainedValues(retained, void 0);
148225
+ }
148226
+ })();
147421
148227
  }
147422
148228
  var functionMethodNames;
147423
148229
  var init_function = __esm({
@@ -147782,15 +148588,24 @@ function createCollectionGlobals(options) {
147782
148588
  context,
147783
148589
  retainedValues: () => [key3, value],
147784
148590
  append: (entry) => {
147785
- if (typeof entry !== "object" || entry === null) throw new TypeError("Map constructor requires entry objects.");
147786
- key3 = context?.getProperty !== void 0 ? context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, options.budget);
147787
- value = context?.getProperty !== void 0 ? context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, options.budget);
147788
- const added = map.entries.has(key3) ? 0 : 1;
147789
- const growth = added + (options.budget.limits.dataSize === void 0 ? 0 : measureSandboxData([key3, value]));
147790
- options.budget.allocateCollectionEntries(map.entries.size + added);
147791
- map.entries.set(key3, value);
147792
- key3 = value = void 0;
147793
- return growth;
148591
+ if (typeof entry !== "object" || entry === null)
148592
+ throw new TypeError("Map constructor requires entry objects.");
148593
+ const store = (entryValue) => {
148594
+ value = entryValue;
148595
+ const added = map.entries.has(key3) ? 0 : 1;
148596
+ const growth = added + (options.budget.limits.dataSize === void 0 ? 0 : measureSandboxData([key3, value]));
148597
+ options.budget.allocateCollectionEntries(map.entries.size + added);
148598
+ map.entries.set(key3, value);
148599
+ key3 = value = void 0;
148600
+ return growth;
148601
+ };
148602
+ const readValue = (entryKey) => {
148603
+ key3 = entryKey;
148604
+ const result2 = context?.getProperty !== void 0 ? context.getProperty(entry, 1) : getSandboxDataProperty(entry, 1, options.budget);
148605
+ return result2 instanceof Promise ? result2.then(store) : store(result2);
148606
+ };
148607
+ const result = context?.getProperty !== void 0 ? context.getProperty(entry, 0) : getSandboxDataProperty(entry, 0, options.budget);
148608
+ return result instanceof Promise ? result.then(readValue) : readValue(result);
147794
148609
  }
147795
148610
  });
147796
148611
  },
@@ -147828,7 +148643,13 @@ function isSandboxMapConstructor(value) {
147828
148643
  function isSandboxSetConstructor(value) {
147829
148644
  return typeof value === "object" && value !== null && setConstructors.has(value);
147830
148645
  }
147831
- function populateCollection(source, collection, { name, budget, context, append, retainedValues }) {
148646
+ function populateCollection(source, collection, {
148647
+ name,
148648
+ budget,
148649
+ context,
148650
+ append,
148651
+ retainedValues
148652
+ }) {
147832
148653
  budget.allocateCollectionEntries(0);
147833
148654
  if (source === void 0 || source === null) return collection;
147834
148655
  const iterator = getSandboxIterator(source, budget, context);
@@ -147836,7 +148657,14 @@ function populateCollection(source, collection, { name, budget, context, append,
147836
148657
  let entry;
147837
148658
  let failure;
147838
148659
  const retained = {};
147839
- budget.setRetainedValues(retained, () => [source, iterator.retainedValue, collection, entry, failure, ...retainedValues?.() ?? []]);
148660
+ budget.setRetainedValues(retained, () => [
148661
+ source,
148662
+ iterator.retainedValue,
148663
+ collection,
148664
+ entry,
148665
+ failure,
148666
+ ...retainedValues?.() ?? []
148667
+ ]);
147840
148668
  const checkData = createDataCheckpoint(budget, context);
147841
148669
  const closeOnThrow = (error3) => {
147842
148670
  failure = isCapturedException(error3) ? error3.reason : error3;
@@ -147846,21 +148674,29 @@ function populateCollection(source, collection, { name, budget, context, append,
147846
148674
  };
147847
148675
  try {
147848
148676
  const closing = iterator.return?.();
147849
- if (iterator.generator || iterator.asynchronous) return Promise.resolve(closing).then(() => {
147850
- throw error3;
147851
- }, rethrow);
148677
+ if (iterator.generator || iterator.asynchronous)
148678
+ return Promise.resolve(closing).then(() => {
148679
+ throw error3;
148680
+ }, rethrow);
147852
148681
  } catch (closeError) {
147853
148682
  return rethrow(closeError);
147854
148683
  }
147855
148684
  throw error3;
147856
148685
  };
147857
148686
  const consume = (result) => {
147858
- if (typeof result !== "object" || result === null) throw new TypeError("Iterator result must be an object.");
148687
+ if (typeof result !== "object" || result === null)
148688
+ throw new TypeError("Iterator result must be an object.");
147859
148689
  if (result.done) return true;
147860
148690
  entry = result.value;
147861
148691
  try {
147862
148692
  budget.visitNode();
147863
148693
  const growth = append(entry);
148694
+ if (growth instanceof Promise)
148695
+ return growth.then((size) => {
148696
+ entry = void 0;
148697
+ checkData(collection, size);
148698
+ return false;
148699
+ }).catch(closeOnThrow);
147864
148700
  entry = void 0;
147865
148701
  checkData(collection, growth);
147866
148702
  } catch (error3) {
@@ -147868,7 +148704,7 @@ function populateCollection(source, collection, { name, budget, context, append,
147868
148704
  }
147869
148705
  return false;
147870
148706
  };
147871
- if (iterator.generator || iterator.asynchronous) {
148707
+ if (iterator.generator || iterator.asynchronous || context !== void 0) {
147872
148708
  return (async () => {
147873
148709
  try {
147874
148710
  checkData(collection, 0, true);
@@ -148570,7 +149406,11 @@ async function evaluateObjectExpression(node, context) {
148570
149406
  const releaseKey = retainValues(context.budget, () => [key3.value]);
148571
149407
  let value;
148572
149408
  try {
148573
- value = await evaluateNode2(property.value, { ...context, inferredName: String(key3.value) });
149409
+ value = await evaluateNode2(property.value, {
149410
+ ...context,
149411
+ inferredName: String(key3.value),
149412
+ ...property.value.type === "FunctionExpression" && property.value.method === true ? { functionEnvironment: { homeObject: object } } : {}
149413
+ });
148574
149414
  } finally {
148575
149415
  releaseKey();
148576
149416
  }
@@ -148595,7 +149435,7 @@ async function evaluateObjectExpression(node, context) {
148595
149435
  }
148596
149436
  }
148597
149437
  function isObjectPrototypeSetterProperty(property, key3) {
148598
- return !property.computed && !property.shorthand && key3 === "__proto__";
149438
+ return !property.computed && !property.shorthand && key3 === "__proto__" && !(property.value.type === "FunctionExpression" && property.value.method === true);
148599
149439
  }
148600
149440
  async function evaluateTemplateLiteral(node, context) {
148601
149441
  let value = context.budget.allocateString(node.quasis[0]?.value.cooked ?? "");
@@ -148650,7 +149490,8 @@ async function evaluateTaggedTemplateExpression(node, context) {
148650
149490
  if (node.tag.type === "MemberExpression") return evaluateMemberAccess(node.tag, context, async (member) => {
148651
149491
  if (member.kind === "nullish") throw new TypeError("Tagged template tag must be a function.");
148652
149492
  const key3 = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148653
- return invokeTag(getPropertyValue(member.object, key3, context), member.superReceiver === void 0 ? member.object : member.superReceiver.value);
149493
+ const receiver = member.superReceiver === void 0 ? member.object : member.superReceiver.value;
149494
+ return invokeTag(await getPropertyValue(member.object, key3, context, receiver), receiver);
148654
149495
  });
148655
149496
  const tag = await evaluateNode2(node.tag, context);
148656
149497
  return tag.kind === "normal" ? invokeTag(tag.value, void 0) : tag;
@@ -148745,7 +149586,8 @@ async function evaluateBinaryExpression(node, context) {
148745
149586
  if (isSandboxBox(rightValue) && (!equality || left.value !== null && left.value !== void 0 && typeof left.value !== "object"))
148746
149587
  rightValue = await toNumericPrimitive(rightValue, context);
148747
149588
  }
148748
- const value = applyBinaryOperator(node, leftValue, rightValue, context);
149589
+ const operation = applyBinaryOperator(node, leftValue, rightValue, context);
149590
+ const value = operation instanceof Promise ? await operation : operation;
148749
149591
  return {
148750
149592
  kind: "normal",
148751
149593
  hasValue: true,
@@ -148836,8 +149678,17 @@ async function evaluateMemberAssignmentExpression(node, context) {
148836
149678
  if (member.object === null || member.object === void 0) {
148837
149679
  throw new TypeError("Cannot assign properties of null or undefined.");
148838
149680
  }
148839
- property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148840
- current = getPropertyValue(member.object, property, context);
149681
+ property = await toPropertyKey(
149682
+ member.property,
149683
+ context.budget,
149684
+ createCoercionContext(context)
149685
+ );
149686
+ current = await getPropertyValue(
149687
+ member.object,
149688
+ property,
149689
+ context,
149690
+ member.superReceiver === void 0 ? member.object : member.superReceiver.value
149691
+ );
148841
149692
  }
148842
149693
  if (node.operator === "&&=" && !isTruthy(current)) {
148843
149694
  return {
@@ -148873,9 +149724,22 @@ async function evaluateMemberAssignmentExpression(node, context) {
148873
149724
  throw new TypeError("Cannot assign properties of null or undefined.");
148874
149725
  }
148875
149726
  operands = [value];
148876
- property ??= await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148877
- if (member.superReceiver === void 0) setSandboxProperty(member.object, property, value, context.budget);
148878
- else setSuperProperty(member.object, member.superReceiver.value, property, value, context.budget);
149727
+ property ??= await toPropertyKey(
149728
+ member.property,
149729
+ context.budget,
149730
+ createCoercionContext(context)
149731
+ );
149732
+ if (member.superReceiver === void 0)
149733
+ await setSandboxProperty(
149734
+ member.object,
149735
+ property,
149736
+ value,
149737
+ context.budget,
149738
+ true,
149739
+ createCoercionContext(context)
149740
+ );
149741
+ else
149742
+ await setSuperProperty(member.object, member.superReceiver.value, property, value, context);
148879
149743
  return {
148880
149744
  kind: "normal",
148881
149745
  hasValue: true,
@@ -149950,11 +150814,11 @@ async function evaluateMemberUpdateExpression(node, context) {
149950
150814
  }
149951
150815
  const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
149952
150816
  const current = toNumber(
149953
- await toNumericPrimitive(getPropertyValue(member.object, property, context), context)
150817
+ await toNumericPrimitive(await getPropertyValue(member.object, property, context, member.superReceiver === void 0 ? member.object : member.superReceiver.value), context)
149954
150818
  );
149955
150819
  const next = node.operator === "++" ? current + 1 : current - 1;
149956
- if (member.superReceiver === void 0) setSandboxProperty(member.object, property, next, context.budget);
149957
- else setSuperProperty(member.object, member.superReceiver.value, property, next, context.budget);
150820
+ if (member.superReceiver === void 0) await setSandboxProperty(member.object, property, next, context.budget, true, createCoercionContext(context));
150821
+ else await setSuperProperty(member.object, member.superReceiver.value, property, next, context);
149958
150822
  return {
149959
150823
  kind: "normal",
149960
150824
  hasValue: true,
@@ -149971,28 +150835,38 @@ async function evaluateMemberExpression(node, context) {
149971
150835
  return {
149972
150836
  kind: "normal",
149973
150837
  hasValue: true,
149974
- value: getPropertyValue(member.object, await toPropertyKey(member.property, context.budget, createCoercionContext(context)), context)
150838
+ value: await getPropertyValue(
150839
+ member.object,
150840
+ await toPropertyKey(member.property, context.budget, createCoercionContext(context)),
150841
+ context,
150842
+ member.superReceiver === void 0 ? member.object : member.superReceiver.value
150843
+ )
149975
150844
  };
149976
150845
  });
149977
150846
  }
149978
- function getPropertyValue(target, property, context) {
150847
+ function getPropertyValue(target, property, context, receiver = target) {
149979
150848
  if (isGuestHostObject(target)) return getHostObjectMember(target, String(property));
150849
+ const descriptor = getSandboxPropertyDescriptor(target, property, context.budget);
150850
+ if (descriptor !== void 0)
150851
+ return readPropertyDescriptor(descriptor, receiver, createCoercionContext(context), true);
149980
150852
  if (typeof target === "string" || typeof target === "number" || typeof target === "boolean") {
149981
150853
  const prototype = getBoxedPrototype(target, context.budget);
149982
150854
  if (prototype !== void 0) {
149983
150855
  if (typeof target === "string" && (property === "length" || getStringIndex(property) !== void 0))
149984
150856
  return getStringMember(target, property, context.budget);
149985
- return getMemberValue(prototype, property, context);
150857
+ return getPropertyValue(prototype, property, context, receiver);
149986
150858
  }
149987
150859
  }
149988
150860
  if (typeof target === "string") return getStringMember(target, property, context.budget);
149989
150861
  if (typeof target === "number") return getNumberMember(property, context.budget);
149990
150862
  if (typeof target === "boolean") return void 0;
149991
150863
  if (isFloat32Array(target)) return getFloat32Member(target, property, context.budget);
149992
- if (isSandboxDate(target)) return getDateMember(property, context.budget, context.compilation?.owner);
150864
+ if (isSandboxDate(target))
150865
+ return getDateMember(property, context.budget, context.compilation?.owner);
149993
150866
  if (isSandboxMap(target)) return getMapMember(target, property, createMapMethodOptions(context));
149994
150867
  if (isSandboxSet(target)) return getSetMember(target, property, createSetMethodOptions(context));
149995
- if (isSandboxCollectionIterator(target)) return getCollectionIteratorMember(target, property, context.budget);
150868
+ if (isSandboxCollectionIterator(target))
150869
+ return getCollectionIteratorMember(target, property, context.budget);
149996
150870
  if (isSandboxGenerator(target)) return getGeneratorMember(target, property, context.budget);
149997
150871
  if (isSandboxClosure(target)) return getClosureMemberValue(target, property, context);
149998
150872
  if (isSandboxPromise(target)) return getPromiseMember(property, context.budget);
@@ -150009,13 +150883,21 @@ function createPatternContext(context, scope = context.scope, evaluate = evaluat
150009
150883
  evaluate: (node, inferredName) => evaluate(node, { ...evaluationContext, inferredName }),
150010
150884
  toPropertyKey: (value) => toPropertyKey(value, context.budget, createCoercionContext(evaluationContext)),
150011
150885
  getProperty: (value, key3) => getPropertyValue(value, key3, evaluationContext),
150012
- setProperty: (target, key3, value) => setSandboxProperty(target, key3, value, context.budget)
150886
+ setProperty: (target, key3, value) => setSandboxProperty(
150887
+ target,
150888
+ key3,
150889
+ value,
150890
+ context.budget,
150891
+ true,
150892
+ createCoercionContext(evaluationContext)
150893
+ )
150013
150894
  };
150014
150895
  }
150015
150896
  async function evaluateCallExpression(node, context) {
150016
150897
  if (node.callee.type === "Super") {
150017
150898
  const construction = context.functionEnvironment?.construction;
150018
- if (construction === void 0) throw new ReferenceError("Super constructor binding is unavailable.");
150899
+ if (construction === void 0)
150900
+ throw new ReferenceError("Super constructor binding is unavailable.");
150019
150901
  const args = await evaluateCallArguments(node.arguments, context);
150020
150902
  if (!args.ok) return args.result;
150021
150903
  return { kind: "normal", hasValue: true, value: await construction.superCall(args.value) };
@@ -150170,7 +151052,7 @@ async function evaluateMemberCallExpression(node, context) {
150170
151052
  property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
150171
151053
  };
150172
151054
  if (member.superReceiver !== void 0)
150173
- return evaluateResolvedCallExpression(node, getPropertyValue(member.object, member.property, context), context, member.superReceiver.value);
151055
+ return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context, member.superReceiver.value), context, member.superReceiver.value);
150174
151056
  if ((typeof member.object === "string" || typeof member.object === "number" || typeof member.object === "boolean") && getBoxedPrototype(member.object, context.budget) !== void 0) {
150175
151057
  if (isDefaultBoxedMethod(member.object, member.property, context.budget)) {
150176
151058
  if (typeof member.object === "string" && isStringMethodName(member.property))
@@ -150178,7 +151060,7 @@ async function evaluateMemberCallExpression(node, context) {
150178
151060
  if (typeof member.object === "number" && isNumberMethodName(member.property))
150179
151061
  return evaluateNumberMethodCall(node, member.object, member.property, context);
150180
151062
  }
150181
- return evaluateResolvedCallExpression(node, getPropertyValue(member.object, member.property, context), context, member.object);
151063
+ return evaluateResolvedCallExpression(node, await getPropertyValue(member.object, member.property, context), context, member.object);
150182
151064
  }
150183
151065
  if (typeof member.object === "string" && isStringMethodName(member.property)) {
150184
151066
  return evaluateStringMethodCall(node, member.object, member.property, context);
@@ -150259,7 +151141,7 @@ async function evaluateMemberCallExpression(node, context) {
150259
151141
  return evaluateResolvedCallExpression(node, memberValue, context, member.object);
150260
151142
  }
150261
151143
  if (isSandboxClosure(member.object)) {
150262
- const memberValue = getClosureMemberValue(member.object, member.property, context);
151144
+ const memberValue = await getPropertyValue(member.object, member.property, context);
150263
151145
  if (memberValue === void 0) {
150264
151146
  throw new TypeError(`Function#${String(member.property)} is not a supported method.`);
150265
151147
  }
@@ -150286,7 +151168,7 @@ async function evaluateMemberCallExpression(node, context) {
150286
151168
  }
150287
151169
  return evaluateResolvedCallExpression(
150288
151170
  node,
150289
- getMemberValue(member.object, member.property, context),
151171
+ await getPropertyValue(member.object, member.property, context),
150290
151172
  context,
150291
151173
  member.object
150292
151174
  );
@@ -150488,7 +151370,8 @@ function applyBinaryOperator(node, left, right, context) {
150488
151370
  return true;
150489
151371
  }
150490
151372
  if (isFloat32ArrayConstructor(right)) return isFloat32Array(left);
150491
- if (isDateConstructor(right)) return isSandboxDate(left) && getDatePrototype(left, context.budget, context.compilation?.owner) !== null;
151373
+ if (isDateConstructor(right))
151374
+ return isSandboxDate(left) && getDatePrototype(left, context.budget, context.compilation?.owner) !== null;
150492
151375
  if (isSandboxSetConstructor(right) && isSandboxSet(left)) {
150493
151376
  return true;
150494
151377
  }
@@ -150497,16 +151380,20 @@ function applyBinaryOperator(node, left, right, context) {
150497
151380
  }
150498
151381
  if (isGuestClosure(right)) {
150499
151382
  if (typeof left !== "object" || left === null) return false;
150500
- const prototype = getGuestFunctionProperty(right, "prototype");
150501
- if (typeof prototype !== "object" || prototype === null) {
150502
- throw new TypeError("Function has a non-object prototype in instanceof check.");
150503
- }
150504
- let depth = 0;
150505
- for (let current = getSandboxPrototype(left, context.budget); current !== null; current = getSandboxPrototype(current, context.budget)) {
150506
- context.budget.visitNode();
150507
- assertSandboxDataDepth(depth++);
150508
- if (current === prototype) return true;
150509
- }
151383
+ const check = (prototype2) => {
151384
+ if (typeof prototype2 !== "object" || prototype2 === null) {
151385
+ throw new TypeError("Function has a non-object prototype in instanceof check.");
151386
+ }
151387
+ let depth = 0;
151388
+ for (let current = getSandboxPrototype(left, context.budget); current !== null; current = getSandboxPrototype(current, context.budget)) {
151389
+ context.budget.visitNode();
151390
+ assertSandboxDataDepth(depth++);
151391
+ if (current === prototype2) return true;
151392
+ }
151393
+ return false;
151394
+ };
151395
+ const prototype = getPropertyValue(right, "prototype", context);
151396
+ return prototype instanceof Promise ? prototype.then(check) : check(prototype);
150510
151397
  }
150511
151398
  return false;
150512
151399
  case "in":
@@ -150677,7 +151564,7 @@ async function toNumericPrimitive(value, context) {
150677
151564
  }
150678
151565
  if (isIndexableSandboxValue(value)) {
150679
151566
  for (const methodName of ["valueOf", "toString"]) {
150680
- const method = getMemberValue(value, methodName, context);
151567
+ const method = await getPropertyValue(value, methodName, context);
150681
151568
  if (methodName === "toString" && method === void 0 && !Object.hasOwn(value, methodName)) {
150682
151569
  return toString(value);
150683
151570
  }
@@ -150765,13 +151652,18 @@ function getArrayMemberValue(target, property, context) {
150765
151652
  }
150766
151653
  return getArrayMember(target, property, createArrayMethodOptions(context));
150767
151654
  }
150768
- function setSandboxProperty(target, property, value, budget, checkInherited = true) {
151655
+ function setSandboxProperty(target, property, value, budget, checkInherited = true, context) {
150769
151656
  if (isSandboxDate(target)) throw new TypeError("Date own properties are not supported.");
150770
151657
  if (isGuestHostObject(target)) {
150771
151658
  setHostObjectMember(target, String(property), value);
150772
151659
  return;
150773
151660
  }
150774
151661
  const prototypeOwner = target;
151662
+ if (checkInherited) {
151663
+ const descriptor2 = getSandboxPropertyDescriptor(target, property, budget);
151664
+ if (descriptor2 !== void 0 && !("value" in descriptor2))
151665
+ return writePropertyDescriptor(descriptor2, target, value, context);
151666
+ }
150775
151667
  if (isGuestClosure(target)) target = materializeFunctionProperties(target);
150776
151668
  if (isFloat32Array(target)) {
150777
151669
  setFloat32Member(target, property, value);
@@ -150807,15 +151699,18 @@ function setSandboxProperty(target, property, value, budget, checkInherited = tr
150807
151699
  const properties = isSandboxClosure(prototype) ? prototype.properties : prototype;
150808
151700
  const inherited = properties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(properties, key3);
150809
151701
  if (inherited === void 0) continue;
150810
- if (inherited.writable !== true) throw new TypeError(`Cannot assign to read only property '${key3}'.`);
151702
+ if (inherited.writable !== true)
151703
+ throw new TypeError(`Cannot assign to read only property '${key3}'.`);
150811
151704
  break;
150812
151705
  }
150813
151706
  }
150814
151707
  defineSandboxProperty(target, key3, value);
150815
151708
  }
150816
151709
  }
150817
- function setSuperProperty(base, receiver, key3, value, budget) {
150818
- if (typeof base !== "object" || base === null) throw new TypeError("Cannot assign a property of null.");
151710
+ function setSuperProperty(base, receiver, key3, value, context) {
151711
+ const budget = context.budget;
151712
+ if (typeof base !== "object" || base === null)
151713
+ throw new TypeError("Cannot assign a property of null.");
150819
151714
  let depth = 0;
150820
151715
  for (let current = base; current !== null; current = getSandboxPrototype(current, budget)) {
150821
151716
  budget.visitNode();
@@ -150823,11 +151718,15 @@ function setSuperProperty(base, receiver, key3, value, budget) {
150823
151718
  const properties = isSandboxClosure(current) ? current.properties : current;
150824
151719
  const descriptor = properties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(properties, key3);
150825
151720
  if (descriptor === void 0) continue;
150826
- if (descriptor.writable !== true) throw new TypeError(`Cannot assign to read only property '${key3}'.`);
151721
+ if (!("value" in descriptor))
151722
+ return writePropertyDescriptor(descriptor, receiver, value, createCoercionContext(context));
151723
+ if (descriptor.writable !== true)
151724
+ throw new TypeError(`Cannot assign to read only property '${key3}'.`);
150827
151725
  break;
150828
151726
  }
150829
- if (typeof receiver !== "object" || receiver === null) throw new TypeError("Super assignment requires an object receiver.");
150830
- setSandboxProperty(receiver, key3, value, budget, false);
151727
+ if (typeof receiver !== "object" || receiver === null)
151728
+ throw new TypeError("Super assignment requires an object receiver.");
151729
+ return setSandboxProperty(receiver, key3, value, budget, false);
150831
151730
  }
150832
151731
  function deleteSandboxProperty(target, property) {
150833
151732
  if (isGuestHostObject(target)) return deleteHostObjectMember(target, String(property));
@@ -150891,7 +151790,7 @@ function createArrayMethodOptions(context) {
150891
151790
  budget: context.budget,
150892
151791
  context: createCoercionContext(context),
150893
151792
  hasProperty: (value, property) => hasSandboxProperty(value, property, context),
150894
- setProperty: (value, property, entry) => setSandboxProperty(value, property, entry, context.budget),
151793
+ setProperty: (value, property, entry) => setSandboxProperty(value, property, entry, context.budget, true, createCoercionContext(context)),
150895
151794
  deleteProperty: deleteSandboxProperty,
150896
151795
  callClosure: (closure, args, stack, thisValue) => invokeSandboxClosure(closure, args, context, stack, void 0, thisValue)
150897
151796
  };
@@ -151029,11 +151928,12 @@ async function evaluateObjectSpread(node, context) {
151029
151928
  };
151030
151929
  }
151031
151930
  if (isGuestHostObject(value.value)) {
151032
- const entries = [];
151931
+ const entries2 = [];
151033
151932
  for (const key3 of getHostObjectKeys(value.value)) {
151034
- if (hasHostObjectMember(value.value, key3, true)) entries.push([key3, getHostObjectMember(value.value, key3)]);
151933
+ if (hasHostObjectMember(value.value, key3, true))
151934
+ entries2.push([key3, getHostObjectMember(value.value, key3)]);
151035
151935
  }
151036
- return { ok: true, value: entries };
151936
+ return { ok: true, value: entries2 };
151037
151937
  }
151038
151938
  if (isSandboxClosure(value.value) && !isGuestClosure(value.value) || isSandboxPromise(value.value)) {
151039
151939
  throw new TypeError(
@@ -151043,10 +151943,17 @@ async function evaluateObjectSpread(node, context) {
151043
151943
  const spreadValue = isGuestClosure(value.value) ? value.value.properties ?? {} : Object(value.value);
151044
151944
  const keys = Object.keys(spreadValue);
151045
151945
  context.budget.allocateArrayLength(keys.length);
151046
- return {
151047
- ok: true,
151048
- value: keys.map((key3) => [key3, spreadValue[key3]])
151049
- };
151946
+ const entries = [];
151947
+ const release2 = retainValues(context.budget, () => [value.value, entries]);
151948
+ try {
151949
+ for (const key3 of keys) {
151950
+ if (!hasOwnSandboxProperty(value.value, key3, true)) continue;
151951
+ entries.push([key3, await getPropertyValue(value.value, key3, context)]);
151952
+ }
151953
+ return { ok: true, value: entries };
151954
+ } finally {
151955
+ release2();
151956
+ }
151050
151957
  }
151051
151958
  function describeObjectSpreadValue(value) {
151052
151959
  if (value === null) {
@@ -151098,6 +152005,7 @@ var init_interpreter = __esm({
151098
152005
  "packages/safe-js/src/interp/interpreter.ts"() {
151099
152006
  "use strict";
151100
152007
  init_promise_replay();
152008
+ init_accessors();
151101
152009
  init_host_capabilities();
151102
152010
  init_property_key();
151103
152011
  init_promise_tracker();
@@ -151220,7 +152128,12 @@ async function evaluateFunctionExpression(node, context, evaluateNode3) {
151220
152128
  return {
151221
152129
  kind: "normal",
151222
152130
  hasValue: true,
151223
- value: createInterpretedClosure(node, context, evaluateNode3)
152131
+ value: createInterpretedClosure(
152132
+ node,
152133
+ context,
152134
+ evaluateNode3,
152135
+ node.method === true ? context.functionEnvironment?.homeObject : void 0
152136
+ )
151224
152137
  };
151225
152138
  }
151226
152139
  const wrapperScope = context.scope.child();
@@ -151302,7 +152215,7 @@ function executeAsyncFunction(execute2, budget, signal) {
151302
152215
  try {
151303
152216
  const value = await execute2(completePrefix);
151304
152217
  resolve16(
151305
- isSandboxPromise(value) || getThenable(value) !== void 0 ? awaitSandboxValue(
152218
+ requiresPromiseResolution(value, budget) ? awaitSandboxValue(
151306
152219
  createSandboxPromise(resolveSandboxValue(value, { budget }), {
151307
152220
  trackReplay: false
151308
152221
  }),
@@ -152643,19 +153556,29 @@ async function stringifyJson4(value, replacer, indent, budget, context) {
152643
153556
  return budget.allocateString(output);
152644
153557
  }
152645
153558
  async function stringifyProperty(key3, holder, state, indent = "") {
152646
- let value = getOwnDataValue(holder, key3);
152647
- if (isSandboxDate(value)) {
152648
- value = dateMethods.get("toJSON").invoke(value, []);
152649
- } else if (isStringifyContainer(value)) {
152650
- const toJSON = getOwnDataValue(value, "toJSON");
152651
- if (isSandboxClosure(toJSON)) {
152652
- value = await callStringifyClosure(toJSON, [key3], value, state);
153559
+ let value = await getStringifyProperty(holder, key3, state);
153560
+ const release2 = retainValues(state.budget, () => [holder, value]);
153561
+ try {
153562
+ if (isSandboxDate(value)) {
153563
+ value = dateMethods.get("toJSON").invoke(value, []);
153564
+ } else if (isStringifyContainer(value)) {
153565
+ const toJSON = await getStringifyProperty(value, "toJSON", state);
153566
+ if (isSandboxClosure(toJSON)) {
153567
+ value = await callStringifyClosure(toJSON, [key3], value, state);
153568
+ }
153569
+ }
153570
+ if (state.replacer !== void 0) {
153571
+ value = await callStringifyClosure(
153572
+ state.replacer,
153573
+ [key3, toSandboxValue(value)],
153574
+ holder,
153575
+ state
153576
+ );
152653
153577
  }
153578
+ return await stringifyValue3(value, state, indent);
153579
+ } finally {
153580
+ release2();
152654
153581
  }
152655
- if (state.replacer !== void 0) {
152656
- value = await callStringifyClosure(state.replacer, [key3, toSandboxValue(value)], holder, state);
152657
- }
152658
- return stringifyValue3(value, state, indent);
152659
153582
  }
152660
153583
  async function stringifyValue3(value, state, indent) {
152661
153584
  if (isSandboxBox(value)) {
@@ -152691,10 +153614,12 @@ async function stringifyValue3(value, state, indent) {
152691
153614
  }
152692
153615
  async function stringifyArray(value, state, indent) {
152693
153616
  enterStringifyObject(value, state);
153617
+ const entries = [];
153618
+ const release2 = retainValues(state.budget, () => entries);
152694
153619
  try {
152695
153620
  const nextIndent = indent + state.gap;
152696
- const entries = [];
152697
- for (let index = 0; index < value.length; index += 1) {
153621
+ const length = value.length;
153622
+ for (let index = 0; index < length; index += 1) {
152698
153623
  entries.push(await stringifyProperty(String(index), value, state, nextIndent) ?? "null");
152699
153624
  }
152700
153625
  if (entries.length === 0) {
@@ -152708,14 +153633,16 @@ ${nextIndent}${entries.join(`,
152708
153633
  ${nextIndent}`)}
152709
153634
  ${indent}]`;
152710
153635
  } finally {
153636
+ release2();
152711
153637
  leaveStringifyObject(value, state);
152712
153638
  }
152713
153639
  }
152714
153640
  async function stringifyObject(value, state, indent) {
152715
153641
  enterStringifyObject(value, state);
153642
+ const entries = [];
153643
+ const release2 = retainValues(state.budget, () => entries);
152716
153644
  try {
152717
153645
  const nextIndent = indent + state.gap;
152718
- const entries = [];
152719
153646
  for (const key3 of Object.keys(value)) {
152720
153647
  const serialized = await stringifyProperty(key3, value, state, nextIndent);
152721
153648
  if (serialized !== void 0) {
@@ -152733,6 +153660,7 @@ ${nextIndent}${entries.join(`,
152733
153660
  ${nextIndent}`)}
152734
153661
  ${indent}}`;
152735
153662
  } finally {
153663
+ release2();
152736
153664
  leaveStringifyObject(value, state);
152737
153665
  }
152738
153666
  }
@@ -152788,15 +153716,10 @@ function toSandboxValue(value) {
152788
153716
  `JSON.stringify(value) produced an unsupported value of type ${typeof value}.`
152789
153717
  );
152790
153718
  }
152791
- function getOwnDataValue(target, key3) {
152792
- const descriptor = Object.getOwnPropertyDescriptor(target, key3);
152793
- if (descriptor === void 0) {
152794
- return void 0;
152795
- }
152796
- if ("get" in descriptor || "set" in descriptor) {
152797
- throw new TypeError(`JSON.stringify(value) cannot serialize accessor property ${key3}.`);
152798
- }
152799
- return descriptor.value;
153719
+ function getStringifyProperty(target, key3, state) {
153720
+ if (state.context?.getProperty !== void 0) return state.context.getProperty(target, key3);
153721
+ const descriptor = getSandboxPropertyDescriptor(target, key3, state.budget);
153722
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, target, state.context);
152800
153723
  }
152801
153724
  function copyJsonToSandbox(value, budget) {
152802
153725
  if (value === null || value === void 0 || typeof value === "boolean" || typeof value === "number") {
@@ -152836,6 +153759,9 @@ function defineDataProperty9(target, key3, value) {
152836
153759
  var init_console_json = __esm({
152837
153760
  "packages/safe-js/src/interp/globals/console-json.ts"() {
152838
153761
  "use strict";
153762
+ init_resources();
153763
+ init_accessors();
153764
+ init_object_model();
152839
153765
  init_date();
152840
153766
  init_boxed();
152841
153767
  init_string_coercion();