poe-code 14.0.57 → 14.0.59

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.
package/dist/index.js CHANGED
@@ -144084,6 +144084,50 @@ var init_var_hoist = __esm({
144084
144084
  }
144085
144085
  });
144086
144086
 
144087
+ // packages/safe-js/src/interp/resources.ts
144088
+ import { AsyncLocalStorage as AsyncLocalStorage8 } from "node:async_hooks";
144089
+ async function withRunResources(signal, execute2) {
144090
+ const controller = new AbortController();
144091
+ const cancel2 = () => controller.abort(signal?.reason);
144092
+ const cleanups = /* @__PURE__ */ new Set();
144093
+ const resources = {
144094
+ signal: controller.signal,
144095
+ referenceReleases: /* @__PURE__ */ new Set(),
144096
+ add(close) {
144097
+ cleanups.add(close);
144098
+ }
144099
+ };
144100
+ signal?.addEventListener("abort", cancel2, { once: true });
144101
+ if (signal?.aborted) cancel2();
144102
+ let result;
144103
+ let failure;
144104
+ let errors2 = [];
144105
+ try {
144106
+ result = await runResources.run(resources, execute2);
144107
+ } catch (error3) {
144108
+ failure = { reason: error3 };
144109
+ } finally {
144110
+ signal?.removeEventListener("abort", cancel2);
144111
+ controller.abort(new Error("SafeJS run finished."));
144112
+ for (const release2 of resources.referenceReleases) release2();
144113
+ resources.referenceReleases.clear();
144114
+ const outcomes = await Promise.allSettled(
144115
+ [...cleanups].map((close) => Promise.resolve().then(close))
144116
+ );
144117
+ errors2 = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
144118
+ }
144119
+ if (failure !== void 0) throw failure.reason;
144120
+ if (errors2.length > 0) throw new AggregateError(errors2, "SafeJS resource cleanup failed.");
144121
+ return result;
144122
+ }
144123
+ var runResources;
144124
+ var init_resources = __esm({
144125
+ "packages/safe-js/src/interp/resources.ts"() {
144126
+ "use strict";
144127
+ runResources = new AsyncLocalStorage8();
144128
+ }
144129
+ });
144130
+
144087
144131
  // packages/safe-js/src/interp/methods/array.ts
144088
144132
  function getArrayMember(value, property, options) {
144089
144133
  const index = getArrayIndex(property);
@@ -146855,89 +146899,110 @@ async function evaluateEmptyStatement(_node, _context) {
146855
146899
  }
146856
146900
  async function evaluateArrayExpression(node, context) {
146857
146901
  const values = [];
146858
- for (const element of node.elements) {
146859
- if (element.type === "UndefinedLiteral" && element.elision === true) {
146860
- values.length += 1;
146861
- context.budget.allocateArrayLength(values.length);
146862
- continue;
146863
- }
146864
- if (element.type === "SpreadElement") {
146865
- const spreadValues = await evaluateSpreadElement(element, context);
146866
- if (!spreadValues.ok) {
146867
- return spreadValues.result;
146902
+ const release2 = retainValues(context.budget, () => [values]);
146903
+ try {
146904
+ for (const element of node.elements) {
146905
+ if (element.type === "UndefinedLiteral" && element.elision === true) {
146906
+ values.length += 1;
146907
+ context.budget.allocateArrayLength(values.length);
146908
+ continue;
146909
+ }
146910
+ if (element.type === "SpreadElement") {
146911
+ const spreadValues = await evaluateSpreadElement(element, context);
146912
+ if (!spreadValues.ok) {
146913
+ return spreadValues.result;
146914
+ }
146915
+ appendArrayValues2(values, spreadValues.value);
146916
+ context.budget.allocateArrayLength(values.length);
146917
+ continue;
146868
146918
  }
146869
- appendArrayValues2(values, spreadValues.value);
146919
+ const result = await evaluateNode2(element, context);
146920
+ if (result.kind !== "normal") {
146921
+ return result;
146922
+ }
146923
+ values.push(result.value);
146870
146924
  context.budget.allocateArrayLength(values.length);
146871
- continue;
146872
146925
  }
146873
- const result = await evaluateNode2(element, context);
146874
- if (result.kind !== "normal") {
146875
- return result;
146876
- }
146877
- values.push(result.value);
146878
- context.budget.allocateArrayLength(values.length);
146926
+ return {
146927
+ kind: "normal",
146928
+ hasValue: true,
146929
+ value: values
146930
+ };
146931
+ } finally {
146932
+ release2();
146879
146933
  }
146880
- return {
146881
- kind: "normal",
146882
- hasValue: true,
146883
- value: values
146884
- };
146885
146934
  }
146886
146935
  async function evaluateObjectExpression(node, context) {
146887
146936
  const object = /* @__PURE__ */ Object.create(null);
146888
- for (const property of node.properties) {
146889
- if (property.type === "SpreadElement") {
146890
- const spreadEntries = await evaluateObjectSpread(property, context);
146891
- if (!spreadEntries.ok) {
146892
- return spreadEntries.result;
146937
+ const release2 = retainValues(context.budget, () => [object]);
146938
+ try {
146939
+ for (const property of node.properties) {
146940
+ if (property.type === "SpreadElement") {
146941
+ const spreadEntries = await evaluateObjectSpread(property, context);
146942
+ if (!spreadEntries.ok) {
146943
+ return spreadEntries.result;
146944
+ }
146945
+ for (const [key4, value2] of spreadEntries.value) {
146946
+ defineSandboxProperty(object, key4, value2);
146947
+ }
146948
+ continue;
146893
146949
  }
146894
- for (const [key4, value2] of spreadEntries.value) {
146895
- defineSandboxProperty(object, key4, value2);
146950
+ const key3 = await evaluateObjectPropertyKey(property, context);
146951
+ if (!key3.ok) {
146952
+ return key3.result;
146896
146953
  }
146897
- continue;
146898
- }
146899
- const key3 = await evaluateObjectPropertyKey(property, context);
146900
- if (!key3.ok) {
146901
- return key3.result;
146902
- }
146903
- const value = await evaluateNode2(property.value, context);
146904
- if (value.kind !== "normal") {
146905
- return value;
146906
- }
146907
- if (isObjectPrototypeSetterProperty(property, key3.value)) {
146908
- if (value.value === null || typeof value.value === "object") {
146909
- setSandboxPrototype(object, value.value, context.budget);
146954
+ const releaseKey = retainValues(context.budget, () => [key3.value]);
146955
+ let value;
146956
+ try {
146957
+ value = await evaluateNode2(property.value, context);
146958
+ } finally {
146959
+ releaseKey();
146910
146960
  }
146911
- continue;
146961
+ if (value.kind !== "normal") {
146962
+ return value;
146963
+ }
146964
+ if (isObjectPrototypeSetterProperty(property, key3.value)) {
146965
+ if (value.value === null || typeof value.value === "object") {
146966
+ setSandboxPrototype(object, value.value, context.budget);
146967
+ }
146968
+ continue;
146969
+ }
146970
+ defineSandboxProperty(object, String(key3.value), value.value);
146912
146971
  }
146913
- defineSandboxProperty(object, String(key3.value), value.value);
146972
+ return {
146973
+ kind: "normal",
146974
+ hasValue: true,
146975
+ value: object
146976
+ };
146977
+ } finally {
146978
+ release2();
146914
146979
  }
146915
- return {
146916
- kind: "normal",
146917
- hasValue: true,
146918
- value: object
146919
- };
146920
146980
  }
146921
146981
  function isObjectPrototypeSetterProperty(property, key3) {
146922
146982
  return !property.computed && !property.shorthand && key3 === "__proto__";
146923
146983
  }
146924
146984
  async function evaluateTemplateLiteral(node, context) {
146925
146985
  let value = context.budget.allocateString(node.quasis[0]?.value.cooked ?? "");
146926
- for (let index = 0; index < node.expressions.length; index += 1) {
146927
- const expression = await evaluateNode2(node.expressions[index], context);
146928
- if (expression.kind !== "normal") {
146929
- return expression;
146986
+ const release2 = retainValues(context.budget, () => [value]);
146987
+ try {
146988
+ for (let index = 0; index < node.expressions.length; index += 1) {
146989
+ const expression = await evaluateNode2(node.expressions[index], context);
146990
+ if (expression.kind !== "normal") {
146991
+ return expression;
146992
+ }
146993
+ const expressionText = context.budget.allocateString(String(expression.value));
146994
+ value = context.budget.allocateString(value + expressionText);
146995
+ const quasiText = context.budget.allocateString(node.quasis[index + 1]?.value.cooked ?? "");
146996
+ value = context.budget.allocateString(value + quasiText);
146930
146997
  }
146931
- const expressionText = context.budget.allocateString(String(expression.value));
146932
- value = context.budget.allocateString(value + expressionText);
146933
- const quasiText = context.budget.allocateString(node.quasis[index + 1]?.value.cooked ?? "");
146934
- value = context.budget.allocateString(value + quasiText);
146998
+ return {
146999
+ kind: "normal",
147000
+ hasValue: true,
147001
+ value
147002
+ };
147003
+ } finally {
147004
+ release2();
146935
147005
  }
146936
- return {
146937
- kind: "normal",
146938
- hasValue: true,
146939
- value
146940
- };
146941
147006
  }
146942
147007
  async function evaluateTaggedTemplateExpression(node, context) {
146943
147008
  const tag = await evaluateNode2(node.tag, context);
@@ -146964,20 +147029,25 @@ async function evaluateTaggedTemplateExpression(node, context) {
146964
147029
  }
146965
147030
  async function evaluateTemplateExpressionValues(node, context) {
146966
147031
  const values = [];
146967
- for (const expressionNode of node.expressions) {
146968
- const expression = await evaluateNode2(expressionNode, context);
146969
- if (expression.kind !== "normal") {
146970
- return {
146971
- ok: false,
146972
- result: expression
146973
- };
147032
+ const release2 = retainValues(context.budget, () => values);
147033
+ try {
147034
+ for (const expressionNode of node.expressions) {
147035
+ const expression = await evaluateNode2(expressionNode, context);
147036
+ if (expression.kind !== "normal") {
147037
+ return {
147038
+ ok: false,
147039
+ result: expression
147040
+ };
147041
+ }
147042
+ values.push(expression.value);
146974
147043
  }
146975
- values.push(expression.value);
147044
+ return {
147045
+ ok: true,
147046
+ value: values
147047
+ };
147048
+ } finally {
147049
+ release2();
146976
147050
  }
146977
- return {
146978
- ok: true,
146979
- value: values
146980
- };
146981
147051
  }
146982
147052
  function createTaggedTemplateStrings(node, context) {
146983
147053
  context.budget.allocateArrayLength(node.quasis.length);
@@ -147025,23 +147095,30 @@ async function evaluateBinaryExpression(node, context) {
147025
147095
  if (left.kind !== "normal") {
147026
147096
  return left;
147027
147097
  }
147028
- const right = await evaluateNode2(node.right, context);
147029
- if (right.kind !== "normal") {
147030
- return right;
147031
- }
147032
- let leftValue = left.value;
147033
- if (node.operator === "in") {
147034
- if (typeof right.value !== "object" || right.value === null) {
147035
- throw new TypeError("Right-hand side of 'in' must be an object.");
147098
+ let rightValue;
147099
+ const release2 = retainValues(context.budget, () => [left.value, rightValue]);
147100
+ try {
147101
+ const right = await evaluateNode2(node.right, context);
147102
+ if (right.kind !== "normal") {
147103
+ return right;
147104
+ }
147105
+ rightValue = right.value;
147106
+ let leftValue = left.value;
147107
+ if (node.operator === "in") {
147108
+ if (typeof right.value !== "object" || right.value === null) {
147109
+ throw new TypeError("Right-hand side of 'in' must be an object.");
147110
+ }
147111
+ leftValue = await toPropertyKey(leftValue, context.budget, createCoercionContext(context));
147036
147112
  }
147037
- leftValue = await toPropertyKey(leftValue, context.budget, createCoercionContext(context));
147113
+ const value = applyBinaryOperator(node, leftValue, right.value, context);
147114
+ return {
147115
+ kind: "normal",
147116
+ hasValue: true,
147117
+ value
147118
+ };
147119
+ } finally {
147120
+ release2();
147038
147121
  }
147039
- const value = applyBinaryOperator(node, leftValue, right.value, context);
147040
- return {
147041
- kind: "normal",
147042
- hasValue: true,
147043
- value
147044
- };
147045
147122
  }
147046
147123
  async function evaluateAssignmentExpression(node, context) {
147047
147124
  if (node.left.type === "ArrayPattern" || node.left.type === "ObjectPattern") {
@@ -147114,61 +147191,64 @@ async function evaluateMemberAssignmentExpression(node, context) {
147114
147191
  if (node.left.type !== "MemberExpression") {
147115
147192
  throw new TypeError("Expected member assignment target.");
147116
147193
  }
147117
- const member = await evaluateMemberAccess(node.left, context);
147118
- if (member.kind === "error") {
147119
- return member;
147120
- }
147121
- if (member.kind === "completion") {
147122
- return member.result;
147123
- }
147124
- if (member.kind === "nullish") {
147125
- throw new TypeError("Cannot assign properties of null or undefined.");
147126
- }
147127
- let property;
147128
- let current = void 0;
147129
- if (node.operator !== "=") {
147130
- if (member.object === null || member.object === void 0) {
147194
+ return evaluateMemberAccess(node.left, context, async (member) => {
147195
+ if (member.kind === "nullish") {
147131
147196
  throw new TypeError("Cannot assign properties of null or undefined.");
147132
147197
  }
147133
- property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
147134
- current = getPropertyValue(member.object, property, context);
147135
- }
147136
- if (node.operator === "&&=" && !isTruthy(current)) {
147137
- return {
147138
- kind: "normal",
147139
- hasValue: true,
147140
- value: current
147141
- };
147142
- }
147143
- if (node.operator === "||=" && isTruthy(current)) {
147144
- return {
147145
- kind: "normal",
147146
- hasValue: true,
147147
- value: current
147148
- };
147149
- }
147150
- if (node.operator === "??=" && current !== null && current !== void 0) {
147151
- return {
147152
- kind: "normal",
147153
- hasValue: true,
147154
- value: current
147155
- };
147156
- }
147157
- const right = await evaluateNode2(node.right, context);
147158
- if (right.kind !== "normal") {
147159
- return right;
147160
- }
147161
- const value = node.operator === "=" || node.operator === "&&=" || node.operator === "||=" || node.operator === "??=" ? right.value : await applyCompoundAssignmentOperator(node.operator, current, right.value, context);
147162
- if (member.object === null || member.object === void 0) {
147163
- throw new TypeError("Cannot assign properties of null or undefined.");
147164
- }
147165
- property ??= await toPropertyKey(member.property, context.budget, createCoercionContext(context));
147166
- setSandboxProperty(member.object, property, value, context.budget);
147167
- return {
147168
- kind: "normal",
147169
- hasValue: true,
147170
- value
147171
- };
147198
+ let property;
147199
+ let current = void 0;
147200
+ if (node.operator !== "=") {
147201
+ if (member.object === null || member.object === void 0) {
147202
+ throw new TypeError("Cannot assign properties of null or undefined.");
147203
+ }
147204
+ property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
147205
+ current = getPropertyValue(member.object, property, context);
147206
+ }
147207
+ if (node.operator === "&&=" && !isTruthy(current)) {
147208
+ return {
147209
+ kind: "normal",
147210
+ hasValue: true,
147211
+ value: current
147212
+ };
147213
+ }
147214
+ if (node.operator === "||=" && isTruthy(current)) {
147215
+ return {
147216
+ kind: "normal",
147217
+ hasValue: true,
147218
+ value: current
147219
+ };
147220
+ }
147221
+ if (node.operator === "??=" && current !== null && current !== void 0) {
147222
+ return {
147223
+ kind: "normal",
147224
+ hasValue: true,
147225
+ value: current
147226
+ };
147227
+ }
147228
+ let operands = [current, property];
147229
+ const release2 = retainValues(context.budget, () => operands);
147230
+ try {
147231
+ const right = await evaluateNode2(node.right, context);
147232
+ if (right.kind !== "normal") {
147233
+ return right;
147234
+ }
147235
+ operands.push(right.value);
147236
+ const value = node.operator === "=" || node.operator === "&&=" || node.operator === "||=" || node.operator === "??=" ? right.value : await applyCompoundAssignmentOperator(node.operator, current, right.value, context);
147237
+ if (member.object === null || member.object === void 0) {
147238
+ throw new TypeError("Cannot assign properties of null or undefined.");
147239
+ }
147240
+ operands = [value];
147241
+ property ??= await toPropertyKey(member.property, context.budget, createCoercionContext(context));
147242
+ setSandboxProperty(member.object, property, value, context.budget);
147243
+ return {
147244
+ kind: "normal",
147245
+ hasValue: true,
147246
+ value
147247
+ };
147248
+ } finally {
147249
+ release2();
147250
+ }
147251
+ });
147172
147252
  }
147173
147253
  async function evaluateLogicalExpression(node, context) {
147174
147254
  const left = await evaluateNode2(node.left, context);
@@ -148147,33 +148227,28 @@ async function evaluateDeleteExpression(node, context) {
148147
148227
  "Unary operator 'delete' requires a member target."
148148
148228
  );
148149
148229
  }
148150
- const member = await evaluateMemberAccess(node.argument, context);
148151
- if (member.kind === "error") {
148152
- return member;
148153
- }
148154
- if (member.kind === "completion") {
148155
- return member.result;
148156
- }
148157
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148158
- if (member.kind === "nullish" && node.argument.optional) {
148159
- return {
148160
- kind: "normal",
148161
- hasValue: true,
148162
- value: true
148163
- };
148230
+ return evaluateMemberAccess(node.argument, context, async (member) => {
148231
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148232
+ if (member.kind === "nullish") {
148233
+ return {
148234
+ kind: "normal",
148235
+ hasValue: true,
148236
+ value: true
148237
+ };
148238
+ }
148239
+ throw new TypeError("Cannot delete properties of null or undefined.");
148164
148240
  }
148165
- throw new TypeError("Cannot delete properties of null or undefined.");
148166
- }
148167
- if (!isIndexableSandboxValue(member.object)) {
148168
- throw new TypeError("Unary operator 'delete' requires a sandbox object property.");
148169
- }
148170
- const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148171
- const deleted = deleteSandboxProperty(member.object, property);
148172
- return {
148173
- kind: "normal",
148174
- hasValue: true,
148175
- value: deleted
148176
- };
148241
+ if (!isIndexableSandboxValue(member.object)) {
148242
+ throw new TypeError("Unary operator 'delete' requires a sandbox object property.");
148243
+ }
148244
+ const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148245
+ const deleted = deleteSandboxProperty(member.object, property);
148246
+ return {
148247
+ kind: "normal",
148248
+ hasValue: true,
148249
+ value: deleted
148250
+ };
148251
+ });
148177
148252
  }
148178
148253
  async function evaluateUpdateExpression(node, context) {
148179
148254
  if (node.argument.type === "Identifier") {
@@ -148209,41 +148284,35 @@ async function evaluateMemberUpdateExpression(node, context) {
148209
148284
  if (node.argument.type !== "MemberExpression") {
148210
148285
  throw new TypeError("Expected member update target.");
148211
148286
  }
148212
- const member = await evaluateMemberAccess(node.argument, context);
148213
- if (member.kind === "error") {
148214
- return member;
148215
- }
148216
- if (member.kind === "completion") {
148217
- return member.result;
148218
- }
148219
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148220
- throw new TypeError("Cannot update properties of null or undefined.");
148221
- }
148222
- const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148223
- const current = toNumber(
148224
- await toNumericPrimitive(getPropertyValue(member.object, property, context), context)
148225
- );
148226
- const next = node.operator === "++" ? current + 1 : current - 1;
148227
- setSandboxProperty(member.object, property, next, context.budget);
148228
- return {
148229
- kind: "normal",
148230
- hasValue: true,
148231
- value: node.prefix ? next : current
148232
- };
148287
+ return evaluateMemberAccess(node.argument, context, async (member) => {
148288
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148289
+ throw new TypeError("Cannot update properties of null or undefined.");
148290
+ }
148291
+ const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
148292
+ const current = toNumber(
148293
+ await toNumericPrimitive(getPropertyValue(member.object, property, context), context)
148294
+ );
148295
+ const next = node.operator === "++" ? current + 1 : current - 1;
148296
+ setSandboxProperty(member.object, property, next, context.budget);
148297
+ return {
148298
+ kind: "normal",
148299
+ hasValue: true,
148300
+ value: node.prefix ? next : current
148301
+ };
148302
+ });
148233
148303
  }
148234
148304
  async function evaluateMemberExpression(node, context) {
148235
- const member = await evaluateMemberAccess(node, context);
148236
- if (member.kind === "error") return member;
148237
- if (member.kind === "completion") return member.result;
148238
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148239
- if (member.kind === "nullish" && node.optional) return { kind: "normal", hasValue: true, value: void 0 };
148240
- throw new TypeError("Cannot read properties of null or undefined.");
148241
- }
148242
- return {
148243
- kind: "normal",
148244
- hasValue: true,
148245
- value: getPropertyValue(member.object, await toPropertyKey(member.property, context.budget, createCoercionContext(context)), context)
148246
- };
148305
+ return evaluateMemberAccess(node, context, async (member) => {
148306
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
148307
+ if (member.kind === "nullish") return { kind: "normal", hasValue: true, value: void 0 };
148308
+ throw new TypeError("Cannot read properties of null or undefined.");
148309
+ }
148310
+ return {
148311
+ kind: "normal",
148312
+ hasValue: true,
148313
+ value: getPropertyValue(member.object, await toPropertyKey(member.property, context.budget, createCoercionContext(context)), context)
148314
+ };
148315
+ });
148247
148316
  }
148248
148317
  function getPropertyValue(target, property, context) {
148249
148318
  if (isGuestHostObject(target)) return getHostObjectMember(target, String(property));
@@ -148351,26 +148420,33 @@ function attachFatalSandboxErrorContext(error3, node, stackFrames) {
148351
148420
  attachErrorSpan(error3, node.span);
148352
148421
  replaceErrorStack(error3, stackFrames);
148353
148422
  }
148354
- async function evaluateMemberAccess(node, context) {
148423
+ async function evaluateMemberAccess(node, context, consume) {
148355
148424
  const object = await evaluateNode2(node.object, context);
148356
- if (object.kind !== "normal") {
148357
- return {
148358
- kind: "completion",
148359
- result: object
148360
- };
148361
- }
148425
+ if (object.kind !== "normal") return object;
148362
148426
  if ((object.value === null || object.value === void 0) && node.optional) {
148363
- return {
148364
- kind: "nullish"
148365
- };
148427
+ return consume({ kind: "nullish" });
148366
148428
  }
148367
- const property = node.computed ? await evaluateNode2(node.property, context) : { kind: "normal", value: getStaticPropertyName2(node.property) };
148368
- if (property.kind !== "normal") return { kind: "completion", result: property };
148369
- return {
148370
- kind: "resolved",
148371
- object: object.value,
148372
- property: property.value
148429
+ let property;
148430
+ const release2 = retainValues(context.budget, () => [object.value, property]);
148431
+ try {
148432
+ const result = node.computed ? await evaluateNode2(node.property, context) : { kind: "normal", value: getStaticPropertyName2(node.property) };
148433
+ if (result.kind !== "normal") return result;
148434
+ property = result.value;
148435
+ return await consume({ kind: "resolved", object: object.value, property });
148436
+ } finally {
148437
+ release2();
148438
+ }
148439
+ }
148440
+ function retainValues(budget, values) {
148441
+ const retained = {};
148442
+ budget.setRetainedValues(retained, values);
148443
+ const pending = runResources.getStore()?.referenceReleases;
148444
+ const release2 = () => {
148445
+ budget.setRetainedValues(retained, void 0);
148446
+ pending?.delete(release2);
148373
148447
  };
148448
+ pending?.add(release2);
148449
+ return release2;
148374
148450
  }
148375
148451
  async function evaluateMemberProperty(node, context) {
148376
148452
  const property = await evaluateNode2(node, context);
@@ -148407,137 +148483,132 @@ async function evaluateMemberCallExpression(node, context) {
148407
148483
  if (node.callee.type !== "MemberExpression") {
148408
148484
  throw new TypeError("Expected member call expression.");
148409
148485
  }
148410
- const reference = await evaluateMemberAccess(node.callee, context);
148411
- if (reference.kind === "error") {
148412
- return reference;
148413
- }
148414
- if (reference.kind === "completion") {
148415
- return reference.result;
148416
- }
148417
- if (reference.kind === "nullish" || reference.object === null || reference.object === void 0) {
148418
- if (reference.kind === "nullish") {
148419
- return {
148420
- kind: "normal",
148421
- hasValue: true,
148422
- value: void 0
148423
- };
148486
+ return evaluateMemberAccess(node.callee, context, async (reference) => {
148487
+ if (reference.kind === "nullish" || reference.object === null || reference.object === void 0) {
148488
+ if (reference.kind === "nullish") {
148489
+ return {
148490
+ kind: "normal",
148491
+ hasValue: true,
148492
+ value: void 0
148493
+ };
148494
+ }
148495
+ throw new TypeError("Cannot read properties of null or undefined.");
148424
148496
  }
148425
- throw new TypeError("Cannot read properties of null or undefined.");
148426
- }
148427
- const member = {
148428
- ...reference,
148429
- property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
148430
- };
148431
- if (typeof member.object === "string" && isStringMethodName(member.property)) {
148432
- return evaluateStringMethodCall(node, member.object, member.property, context);
148433
- }
148434
- if (typeof member.object === "number" && isNumberMethodName(member.property)) {
148435
- return evaluateNumberMethodCall(node, member.object, member.property, context);
148436
- }
148437
- if (Array.isArray(member.object) && isArrayMethodName(member.property) && !Object.hasOwn(member.object, member.property)) {
148438
- return evaluateArrayMethodCall(node, member.object, member.property, context);
148439
- }
148440
- if (isSandboxMap(member.object) && isMapMethodName(member.property)) {
148441
- return evaluateMapMethodCall(node, member.object, member.property, context);
148442
- }
148443
- if (isSandboxSet(member.object) && isSetMethodName(member.property)) {
148444
- return evaluateSetMethodCall(node, member.object, member.property, context);
148445
- }
148446
- if (typeof member.object === "string") {
148447
- return evaluatePrimitiveMemberCall(
148448
- node,
148449
- "String",
148450
- member.property,
148451
- getStringMember(member.object, member.property, context.budget),
148452
- context
148453
- );
148454
- }
148455
- if (typeof member.object === "number") {
148456
- return evaluatePrimitiveMemberCall(
148457
- node,
148458
- "Number",
148459
- member.property,
148460
- getNumberMember(member.property, context.budget),
148461
- context
148462
- );
148463
- }
148464
- if (Array.isArray(member.object) && !Object.hasOwn(member.object, member.property)) {
148465
- return evaluatePrimitiveMemberCall(
148466
- node,
148467
- "Array",
148468
- member.property,
148469
- getArrayMemberValue(member.object, member.property, context),
148470
- context
148471
- );
148472
- }
148473
- if (isSandboxMap(member.object)) {
148474
- return evaluatePrimitiveMemberCall(
148475
- node,
148476
- "Map",
148477
- member.property,
148478
- getMapMember(member.object, member.property, createMapMethodOptions(context)),
148479
- context
148480
- );
148481
- }
148482
- if (isSandboxDate(member.object)) {
148483
- return evaluateResolvedCallExpression(node, getDateMember(member.property, context.budget, context.compilation?.owner), context, member.object);
148484
- }
148485
- if (isFloat32Array(member.object)) {
148486
- return evaluateResolvedCallExpression(
148487
- node,
148488
- getFloat32Member(member.object, member.property, context.budget),
148489
- context,
148490
- member.object
148491
- );
148492
- }
148493
- if (isSandboxSet(member.object)) {
148494
- return evaluatePrimitiveMemberCall(
148495
- node,
148496
- "Set",
148497
- member.property,
148498
- getSetMember(member.object, member.property, createSetMethodOptions(context)),
148499
- context
148500
- );
148501
- }
148502
- if (isSandboxGenerator(member.object)) {
148503
- const memberValue = getGeneratorMember(member.object, member.property, context.budget);
148504
- if (memberValue === void 0) {
148505
- throw new TypeError(`Generator#${String(member.property)} is not a supported method.`);
148497
+ const member = {
148498
+ ...reference,
148499
+ property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
148500
+ };
148501
+ if (typeof member.object === "string" && isStringMethodName(member.property)) {
148502
+ return evaluateStringMethodCall(node, member.object, member.property, context);
148506
148503
  }
148507
- return evaluateResolvedCallExpression(node, memberValue, context, member.object);
148508
- }
148509
- if (isSandboxClosure(member.object)) {
148510
- const memberValue = getClosureMemberValue(member.object, member.property, context);
148511
- if (memberValue === void 0) {
148512
- throw new TypeError(`Function#${String(member.property)} is not a supported method.`);
148504
+ if (typeof member.object === "number" && isNumberMethodName(member.property)) {
148505
+ return evaluateNumberMethodCall(node, member.object, member.property, context);
148506
+ }
148507
+ if (Array.isArray(member.object) && isArrayMethodName(member.property) && !Object.hasOwn(member.object, member.property)) {
148508
+ return evaluateArrayMethodCall(node, member.object, member.property, context);
148509
+ }
148510
+ if (isSandboxMap(member.object) && isMapMethodName(member.property)) {
148511
+ return evaluateMapMethodCall(node, member.object, member.property, context);
148512
+ }
148513
+ if (isSandboxSet(member.object) && isSetMethodName(member.property)) {
148514
+ return evaluateSetMethodCall(node, member.object, member.property, context);
148515
+ }
148516
+ if (typeof member.object === "string") {
148517
+ return evaluatePrimitiveMemberCall(
148518
+ node,
148519
+ "String",
148520
+ member.property,
148521
+ getStringMember(member.object, member.property, context.budget),
148522
+ context
148523
+ );
148524
+ }
148525
+ if (typeof member.object === "number") {
148526
+ return evaluatePrimitiveMemberCall(
148527
+ node,
148528
+ "Number",
148529
+ member.property,
148530
+ getNumberMember(member.property, context.budget),
148531
+ context
148532
+ );
148533
+ }
148534
+ if (Array.isArray(member.object) && !Object.hasOwn(member.object, member.property)) {
148535
+ return evaluatePrimitiveMemberCall(
148536
+ node,
148537
+ "Array",
148538
+ member.property,
148539
+ getArrayMemberValue(member.object, member.property, context),
148540
+ context
148541
+ );
148542
+ }
148543
+ if (isSandboxMap(member.object)) {
148544
+ return evaluatePrimitiveMemberCall(
148545
+ node,
148546
+ "Map",
148547
+ member.property,
148548
+ getMapMember(member.object, member.property, createMapMethodOptions(context)),
148549
+ context
148550
+ );
148551
+ }
148552
+ if (isSandboxDate(member.object)) {
148553
+ return evaluateResolvedCallExpression(node, getDateMember(member.property, context.budget, context.compilation?.owner), context, member.object);
148554
+ }
148555
+ if (isFloat32Array(member.object)) {
148556
+ return evaluateResolvedCallExpression(
148557
+ node,
148558
+ getFloat32Member(member.object, member.property, context.budget),
148559
+ context,
148560
+ member.object
148561
+ );
148562
+ }
148563
+ if (isSandboxSet(member.object)) {
148564
+ return evaluatePrimitiveMemberCall(
148565
+ node,
148566
+ "Set",
148567
+ member.property,
148568
+ getSetMember(member.object, member.property, createSetMethodOptions(context)),
148569
+ context
148570
+ );
148571
+ }
148572
+ if (isSandboxGenerator(member.object)) {
148573
+ const memberValue = getGeneratorMember(member.object, member.property, context.budget);
148574
+ if (memberValue === void 0) {
148575
+ throw new TypeError(`Generator#${String(member.property)} is not a supported method.`);
148576
+ }
148577
+ return evaluateResolvedCallExpression(node, memberValue, context, member.object);
148578
+ }
148579
+ if (isSandboxClosure(member.object)) {
148580
+ const memberValue = getClosureMemberValue(member.object, member.property, context);
148581
+ if (memberValue === void 0) {
148582
+ throw new TypeError(`Function#${String(member.property)} is not a supported method.`);
148583
+ }
148584
+ return evaluateResolvedCallExpression(node, memberValue, context, member.object);
148585
+ }
148586
+ if (isSandboxPromise(member.object)) {
148587
+ return evaluateResolvedCallExpression(
148588
+ node,
148589
+ getPromiseMember(member.property, context.budget),
148590
+ context,
148591
+ member.object
148592
+ );
148593
+ }
148594
+ if (isSandboxRegex(member.object) && isRegexMethodName(member.property)) {
148595
+ return evaluateResolvedCallExpression(
148596
+ node,
148597
+ getRegexMember(member.object, member.property, context.budget),
148598
+ context,
148599
+ member.object
148600
+ );
148601
+ }
148602
+ if (!isIndexableSandboxValue(member.object)) {
148603
+ throw new TypeError("Attempted to read a property from a non-object value.");
148513
148604
  }
148514
- return evaluateResolvedCallExpression(node, memberValue, context, member.object);
148515
- }
148516
- if (isSandboxPromise(member.object)) {
148517
- return evaluateResolvedCallExpression(
148518
- node,
148519
- getPromiseMember(member.property, context.budget),
148520
- context,
148521
- member.object
148522
- );
148523
- }
148524
- if (isSandboxRegex(member.object) && isRegexMethodName(member.property)) {
148525
148605
  return evaluateResolvedCallExpression(
148526
148606
  node,
148527
- getRegexMember(member.object, member.property, context.budget),
148607
+ getMemberValue(member.object, member.property, context),
148528
148608
  context,
148529
148609
  member.object
148530
148610
  );
148531
- }
148532
- if (!isIndexableSandboxValue(member.object)) {
148533
- throw new TypeError("Attempted to read a property from a non-object value.");
148534
- }
148535
- return evaluateResolvedCallExpression(
148536
- node,
148537
- getMemberValue(member.object, member.property, context),
148538
- context,
148539
- member.object
148540
- );
148611
+ });
148541
148612
  }
148542
148613
  function evaluatePrimitiveMemberCall(node, receiverType, property, value, context) {
148543
148614
  if (value === void 0) {
@@ -149172,30 +149243,35 @@ async function invokeSandboxClosure(callee, args, context, stack, span, thisValu
149172
149243
  }
149173
149244
  async function evaluateCallArguments(args, context) {
149174
149245
  const values = [];
149175
- for (const arg of args) {
149176
- if (arg.type === "SpreadElement") {
149177
- const spreadValues = await evaluateSpreadElement(arg, context);
149178
- if (!spreadValues.ok) {
149179
- return spreadValues;
149246
+ const release2 = retainValues(context.budget, () => values);
149247
+ try {
149248
+ for (const arg of args) {
149249
+ if (arg.type === "SpreadElement") {
149250
+ const spreadValues = await evaluateSpreadElement(arg, context);
149251
+ if (!spreadValues.ok) {
149252
+ return spreadValues;
149253
+ }
149254
+ appendArrayValues2(values, spreadValues.value);
149255
+ context.budget.allocateArrayLength(values.length);
149256
+ continue;
149180
149257
  }
149181
- appendArrayValues2(values, spreadValues.value);
149258
+ const result = await evaluateNode2(arg, context);
149259
+ if (result.kind !== "normal") {
149260
+ return {
149261
+ ok: false,
149262
+ result
149263
+ };
149264
+ }
149265
+ values.push(result.value);
149182
149266
  context.budget.allocateArrayLength(values.length);
149183
- continue;
149184
149267
  }
149185
- const result = await evaluateNode2(arg, context);
149186
- if (result.kind !== "normal") {
149187
- return {
149188
- ok: false,
149189
- result
149190
- };
149191
- }
149192
- values.push(result.value);
149193
- context.budget.allocateArrayLength(values.length);
149268
+ return {
149269
+ ok: true,
149270
+ value: values
149271
+ };
149272
+ } finally {
149273
+ release2();
149194
149274
  }
149195
- return {
149196
- ok: true,
149197
- value: values
149198
- };
149199
149275
  }
149200
149276
  async function evaluateSpreadElement(node, context) {
149201
149277
  const value = await evaluateNode2(node.argument, context);
@@ -149210,21 +149286,26 @@ async function evaluateSpreadElement(node, context) {
149210
149286
  throw new TypeError("Spread arguments must evaluate to an iterable.");
149211
149287
  }
149212
149288
  const spreadValues = [];
149213
- while (true) {
149214
- const next = await iterator.next();
149215
- if (typeof next !== "object" || next === null) {
149216
- throw new TypeError("Iterator result must be an object.");
149217
- }
149218
- if (next.done === true) {
149219
- break;
149289
+ const release2 = retainValues(context.budget, () => [value.value, ...spreadValues]);
149290
+ try {
149291
+ while (true) {
149292
+ const next = await iterator.next();
149293
+ if (typeof next !== "object" || next === null) {
149294
+ throw new TypeError("Iterator result must be an object.");
149295
+ }
149296
+ if (next.done === true) {
149297
+ break;
149298
+ }
149299
+ spreadValues.push(next.value);
149300
+ context.budget.allocateArrayLength(spreadValues.length);
149220
149301
  }
149221
- spreadValues.push(next.value);
149222
- context.budget.allocateArrayLength(spreadValues.length);
149302
+ return {
149303
+ ok: true,
149304
+ value: spreadValues
149305
+ };
149306
+ } finally {
149307
+ release2();
149223
149308
  }
149224
- return {
149225
- ok: true,
149226
- value: spreadValues
149227
- };
149228
149309
  }
149229
149310
  async function evaluateObjectSpread(node, context) {
149230
149311
  const value = await evaluateNode2(node.argument, context);
@@ -149315,6 +149396,7 @@ var init_interpreter = __esm({
149315
149396
  init_promise_tracker();
149316
149397
  init_jobs4();
149317
149398
  init_cancel2();
149399
+ init_resources();
149318
149400
  init_shape();
149319
149401
  init_async();
149320
149402
  init_host_call();
@@ -151779,47 +151861,6 @@ var init_globals = __esm({
151779
151861
  }
151780
151862
  });
151781
151863
 
151782
- // packages/safe-js/src/interp/resources.ts
151783
- import { AsyncLocalStorage as AsyncLocalStorage8 } from "node:async_hooks";
151784
- async function withRunResources(signal, execute2) {
151785
- const controller = new AbortController();
151786
- const cancel2 = () => controller.abort(signal?.reason);
151787
- const cleanups = /* @__PURE__ */ new Set();
151788
- const resources = {
151789
- signal: controller.signal,
151790
- add(close) {
151791
- cleanups.add(close);
151792
- }
151793
- };
151794
- signal?.addEventListener("abort", cancel2, { once: true });
151795
- if (signal?.aborted) cancel2();
151796
- let result;
151797
- let failure;
151798
- let errors2 = [];
151799
- try {
151800
- result = await runResources.run(resources, execute2);
151801
- } catch (error3) {
151802
- failure = { reason: error3 };
151803
- } finally {
151804
- signal?.removeEventListener("abort", cancel2);
151805
- controller.abort(new Error("SafeJS run finished."));
151806
- const outcomes = await Promise.allSettled(
151807
- [...cleanups].map((close) => Promise.resolve().then(close))
151808
- );
151809
- errors2 = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
151810
- }
151811
- if (failure !== void 0) throw failure.reason;
151812
- if (errors2.length > 0) throw new AggregateError(errors2, "SafeJS resource cleanup failed.");
151813
- return result;
151814
- }
151815
- var runResources;
151816
- var init_resources = __esm({
151817
- "packages/safe-js/src/interp/resources.ts"() {
151818
- "use strict";
151819
- runResources = new AsyncLocalStorage8();
151820
- }
151821
- });
151822
-
151823
151864
  // packages/safe-js/src/modules/registry.ts
151824
151865
  function createUnknownModuleMessage2(moduleName, moduleNames) {
151825
151866
  if (moduleNames.length === 0) {
@@ -152192,6 +152233,7 @@ var init_realm = __esm({
152192
152233
  extensions;
152193
152234
  consoleExtension;
152194
152235
  cleanups = [];
152236
+ referenceReleases = /* @__PURE__ */ new Set();
152195
152237
  callbacks = /* @__PURE__ */ new Map();
152196
152238
  pendingCallbacks = /* @__PURE__ */ new Set();
152197
152239
  callbackCache = /* @__PURE__ */ new WeakMap();
@@ -152518,7 +152560,7 @@ var init_realm = __esm({
152518
152560
  const pending = withSandboxPromiseRejectionTracker(
152519
152561
  this.tracker,
152520
152562
  () => runResources.run(
152521
- { signal: this.controller.signal, add: this.onCleanup },
152563
+ { signal: this.controller.signal, referenceReleases: this.referenceReleases, add: this.onCleanup },
152522
152564
  () => withCancellationSignal(
152523
152565
  this.controller.signal,
152524
152566
  () => active && this.phase.getStore()?.active ? runAsyncPrefix(invoke2) : this.queue.run(invoke2)
@@ -152713,7 +152755,7 @@ var init_realm = __esm({
152713
152755
  () => withSandboxPromiseRejectionTracker(
152714
152756
  this.tracker,
152715
152757
  () => runResources.run(
152716
- { signal: this.controller.signal, add: this.onCleanup },
152758
+ { signal: this.controller.signal, referenceReleases: this.referenceReleases, add: this.onCleanup },
152717
152759
  () => withCancellationSignal(this.controller.signal, task)
152718
152760
  )
152719
152761
  )
@@ -152770,6 +152812,8 @@ var init_realm = __esm({
152770
152812
  this.hostObjects.clear();
152771
152813
  for (const reference of this.guestReferences.keys()) revokeGuestReference(reference, this);
152772
152814
  this.guestReferences.clear();
152815
+ for (const release2 of this.referenceReleases) release2();
152816
+ this.referenceReleases.clear();
152773
152817
  this.budget.setRetainedValues(this, void 0);
152774
152818
  releaseObjectPrototype(this.budget);
152775
152819
  this.disposal = (async () => {