@poe-platform/safe-js 0.1.123 → 0.1.124

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.
@@ -23880,6 +23880,44 @@ function hoistVarDeclarations(node, scope) {
23880
23880
  }
23881
23881
  }
23882
23882
 
23883
+ // packages/safe-js/src/interp/resources.ts
23884
+ import { AsyncLocalStorage as AsyncLocalStorage5 } from "node:async_hooks";
23885
+ var runResources = new AsyncLocalStorage5();
23886
+ async function withRunResources(signal, execute) {
23887
+ const controller = new AbortController();
23888
+ const cancel = () => controller.abort(signal?.reason);
23889
+ const cleanups = /* @__PURE__ */ new Set();
23890
+ const resources = {
23891
+ signal: controller.signal,
23892
+ referenceReleases: /* @__PURE__ */ new Set(),
23893
+ add(close) {
23894
+ cleanups.add(close);
23895
+ }
23896
+ };
23897
+ signal?.addEventListener("abort", cancel, { once: true });
23898
+ if (signal?.aborted) cancel();
23899
+ let result;
23900
+ let failure;
23901
+ let errors = [];
23902
+ try {
23903
+ result = await runResources.run(resources, execute);
23904
+ } catch (error) {
23905
+ failure = { reason: error };
23906
+ } finally {
23907
+ signal?.removeEventListener("abort", cancel);
23908
+ controller.abort(new Error("SafeJS run finished."));
23909
+ for (const release of resources.referenceReleases) release();
23910
+ resources.referenceReleases.clear();
23911
+ const outcomes = await Promise.allSettled(
23912
+ [...cleanups].map((close) => Promise.resolve().then(close))
23913
+ );
23914
+ errors = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
23915
+ }
23916
+ if (failure !== void 0) throw failure.reason;
23917
+ if (errors.length > 0) throw new AggregateError(errors, "SafeJS resource cleanup failed.");
23918
+ return result;
23919
+ }
23920
+
23883
23921
  // packages/safe-js/src/interp/methods/array.ts
23884
23922
  var arrayLikeSources = /* @__PURE__ */ new WeakMap();
23885
23923
  var activeArrayCallbacks = /* @__PURE__ */ new WeakMap();
@@ -26816,61 +26854,56 @@ async function evaluateMemberAssignmentExpression(node, context) {
26816
26854
  if (node.left.type !== "MemberExpression") {
26817
26855
  throw new TypeError("Expected member assignment target.");
26818
26856
  }
26819
- const member = await evaluateMemberAccess(node.left, context);
26820
- if (member.kind === "error") {
26821
- return member;
26822
- }
26823
- if (member.kind === "completion") {
26824
- return member.result;
26825
- }
26826
- if (member.kind === "nullish") {
26827
- throw new TypeError("Cannot assign properties of null or undefined.");
26828
- }
26829
- let property;
26830
- let current = void 0;
26831
- if (node.operator !== "=") {
26857
+ return evaluateMemberAccess(node.left, context, async (member) => {
26858
+ if (member.kind === "nullish") {
26859
+ throw new TypeError("Cannot assign properties of null or undefined.");
26860
+ }
26861
+ let property;
26862
+ let current = void 0;
26863
+ if (node.operator !== "=") {
26864
+ if (member.object === null || member.object === void 0) {
26865
+ throw new TypeError("Cannot assign properties of null or undefined.");
26866
+ }
26867
+ property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
26868
+ current = getPropertyValue(member.object, property, context);
26869
+ }
26870
+ if (node.operator === "&&=" && !isTruthy(current)) {
26871
+ return {
26872
+ kind: "normal",
26873
+ hasValue: true,
26874
+ value: current
26875
+ };
26876
+ }
26877
+ if (node.operator === "||=" && isTruthy(current)) {
26878
+ return {
26879
+ kind: "normal",
26880
+ hasValue: true,
26881
+ value: current
26882
+ };
26883
+ }
26884
+ if (node.operator === "??=" && current !== null && current !== void 0) {
26885
+ return {
26886
+ kind: "normal",
26887
+ hasValue: true,
26888
+ value: current
26889
+ };
26890
+ }
26891
+ const right = await evaluateNode(node.right, context);
26892
+ if (right.kind !== "normal") {
26893
+ return right;
26894
+ }
26895
+ const value = node.operator === "=" || node.operator === "&&=" || node.operator === "||=" || node.operator === "??=" ? right.value : await applyCompoundAssignmentOperator(node.operator, current, right.value, context);
26832
26896
  if (member.object === null || member.object === void 0) {
26833
26897
  throw new TypeError("Cannot assign properties of null or undefined.");
26834
26898
  }
26835
- property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
26836
- current = getPropertyValue(member.object, property, context);
26837
- }
26838
- if (node.operator === "&&=" && !isTruthy(current)) {
26839
- return {
26840
- kind: "normal",
26841
- hasValue: true,
26842
- value: current
26843
- };
26844
- }
26845
- if (node.operator === "||=" && isTruthy(current)) {
26899
+ property ??= await toPropertyKey(member.property, context.budget, createCoercionContext(context));
26900
+ setSandboxProperty(member.object, property, value, context.budget);
26846
26901
  return {
26847
26902
  kind: "normal",
26848
26903
  hasValue: true,
26849
- value: current
26850
- };
26851
- }
26852
- if (node.operator === "??=" && current !== null && current !== void 0) {
26853
- return {
26854
- kind: "normal",
26855
- hasValue: true,
26856
- value: current
26904
+ value
26857
26905
  };
26858
- }
26859
- const right = await evaluateNode(node.right, context);
26860
- if (right.kind !== "normal") {
26861
- return right;
26862
- }
26863
- const value = node.operator === "=" || node.operator === "&&=" || node.operator === "||=" || node.operator === "??=" ? right.value : await applyCompoundAssignmentOperator(node.operator, current, right.value, context);
26864
- if (member.object === null || member.object === void 0) {
26865
- throw new TypeError("Cannot assign properties of null or undefined.");
26866
- }
26867
- property ??= await toPropertyKey(member.property, context.budget, createCoercionContext(context));
26868
- setSandboxProperty(member.object, property, value, context.budget);
26869
- return {
26870
- kind: "normal",
26871
- hasValue: true,
26872
- value
26873
- };
26906
+ });
26874
26907
  }
26875
26908
  async function evaluateLogicalExpression(node, context) {
26876
26909
  const left = await evaluateNode(node.left, context);
@@ -27849,33 +27882,28 @@ async function evaluateDeleteExpression(node, context) {
27849
27882
  "Unary operator 'delete' requires a member target."
27850
27883
  );
27851
27884
  }
27852
- const member = await evaluateMemberAccess(node.argument, context);
27853
- if (member.kind === "error") {
27854
- return member;
27855
- }
27856
- if (member.kind === "completion") {
27857
- return member.result;
27858
- }
27859
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27860
- if (member.kind === "nullish" && node.argument.optional) {
27861
- return {
27862
- kind: "normal",
27863
- hasValue: true,
27864
- value: true
27865
- };
27885
+ return evaluateMemberAccess(node.argument, context, async (member) => {
27886
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27887
+ if (member.kind === "nullish") {
27888
+ return {
27889
+ kind: "normal",
27890
+ hasValue: true,
27891
+ value: true
27892
+ };
27893
+ }
27894
+ throw new TypeError("Cannot delete properties of null or undefined.");
27866
27895
  }
27867
- throw new TypeError("Cannot delete properties of null or undefined.");
27868
- }
27869
- if (!isIndexableSandboxValue(member.object)) {
27870
- throw new TypeError("Unary operator 'delete' requires a sandbox object property.");
27871
- }
27872
- const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
27873
- const deleted = deleteSandboxProperty(member.object, property);
27874
- return {
27875
- kind: "normal",
27876
- hasValue: true,
27877
- value: deleted
27878
- };
27896
+ if (!isIndexableSandboxValue(member.object)) {
27897
+ throw new TypeError("Unary operator 'delete' requires a sandbox object property.");
27898
+ }
27899
+ const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
27900
+ const deleted = deleteSandboxProperty(member.object, property);
27901
+ return {
27902
+ kind: "normal",
27903
+ hasValue: true,
27904
+ value: deleted
27905
+ };
27906
+ });
27879
27907
  }
27880
27908
  async function evaluateUpdateExpression(node, context) {
27881
27909
  if (node.argument.type === "Identifier") {
@@ -27911,41 +27939,35 @@ async function evaluateMemberUpdateExpression(node, context) {
27911
27939
  if (node.argument.type !== "MemberExpression") {
27912
27940
  throw new TypeError("Expected member update target.");
27913
27941
  }
27914
- const member = await evaluateMemberAccess(node.argument, context);
27915
- if (member.kind === "error") {
27916
- return member;
27917
- }
27918
- if (member.kind === "completion") {
27919
- return member.result;
27920
- }
27921
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27922
- throw new TypeError("Cannot update properties of null or undefined.");
27923
- }
27924
- const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
27925
- const current = toNumber(
27926
- await toNumericPrimitive(getPropertyValue(member.object, property, context), context)
27927
- );
27928
- const next = node.operator === "++" ? current + 1 : current - 1;
27929
- setSandboxProperty(member.object, property, next, context.budget);
27930
- return {
27931
- kind: "normal",
27932
- hasValue: true,
27933
- value: node.prefix ? next : current
27934
- };
27942
+ return evaluateMemberAccess(node.argument, context, async (member) => {
27943
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27944
+ throw new TypeError("Cannot update properties of null or undefined.");
27945
+ }
27946
+ const property = await toPropertyKey(member.property, context.budget, createCoercionContext(context));
27947
+ const current = toNumber(
27948
+ await toNumericPrimitive(getPropertyValue(member.object, property, context), context)
27949
+ );
27950
+ const next = node.operator === "++" ? current + 1 : current - 1;
27951
+ setSandboxProperty(member.object, property, next, context.budget);
27952
+ return {
27953
+ kind: "normal",
27954
+ hasValue: true,
27955
+ value: node.prefix ? next : current
27956
+ };
27957
+ });
27935
27958
  }
27936
27959
  async function evaluateMemberExpression(node, context) {
27937
- const member = await evaluateMemberAccess(node, context);
27938
- if (member.kind === "error") return member;
27939
- if (member.kind === "completion") return member.result;
27940
- if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27941
- if (member.kind === "nullish" && node.optional) return { kind: "normal", hasValue: true, value: void 0 };
27942
- throw new TypeError("Cannot read properties of null or undefined.");
27943
- }
27944
- return {
27945
- kind: "normal",
27946
- hasValue: true,
27947
- value: getPropertyValue(member.object, await toPropertyKey(member.property, context.budget, createCoercionContext(context)), context)
27948
- };
27960
+ return evaluateMemberAccess(node, context, async (member) => {
27961
+ if (member.kind === "nullish" || member.object === null || member.object === void 0) {
27962
+ if (member.kind === "nullish") return { kind: "normal", hasValue: true, value: void 0 };
27963
+ throw new TypeError("Cannot read properties of null or undefined.");
27964
+ }
27965
+ return {
27966
+ kind: "normal",
27967
+ hasValue: true,
27968
+ value: getPropertyValue(member.object, await toPropertyKey(member.property, context.budget, createCoercionContext(context)), context)
27969
+ };
27970
+ });
27949
27971
  }
27950
27972
  function getPropertyValue(target, property, context) {
27951
27973
  if (isGuestHostObject(target)) return getHostObjectMember(target, String(property));
@@ -28053,26 +28075,27 @@ function attachFatalSandboxErrorContext(error, node, stackFrames) {
28053
28075
  attachErrorSpan(error, node.span);
28054
28076
  replaceErrorStack(error, stackFrames);
28055
28077
  }
28056
- async function evaluateMemberAccess(node, context) {
28078
+ async function evaluateMemberAccess(node, context, consume) {
28057
28079
  const object = await evaluateNode(node.object, context);
28058
- if (object.kind !== "normal") {
28059
- return {
28060
- kind: "completion",
28061
- result: object
28062
- };
28063
- }
28080
+ if (object.kind !== "normal") return object;
28064
28081
  if ((object.value === null || object.value === void 0) && node.optional) {
28065
- return {
28066
- kind: "nullish"
28067
- };
28082
+ return consume({ kind: "nullish" });
28083
+ }
28084
+ const retained = {};
28085
+ let property;
28086
+ context.budget.setRetainedValues(retained, () => [object.value, property]);
28087
+ const release = () => context.budget.setRetainedValues(retained, void 0);
28088
+ const pending = runResources.getStore()?.referenceReleases;
28089
+ pending?.add(release);
28090
+ try {
28091
+ const result = node.computed ? await evaluateNode(node.property, context) : { kind: "normal", value: getStaticPropertyName2(node.property) };
28092
+ if (result.kind !== "normal") return result;
28093
+ property = result.value;
28094
+ return await consume({ kind: "resolved", object: object.value, property });
28095
+ } finally {
28096
+ release();
28097
+ pending?.delete(release);
28068
28098
  }
28069
- const property = node.computed ? await evaluateNode(node.property, context) : { kind: "normal", value: getStaticPropertyName2(node.property) };
28070
- if (property.kind !== "normal") return { kind: "completion", result: property };
28071
- return {
28072
- kind: "resolved",
28073
- object: object.value,
28074
- property: property.value
28075
- };
28076
28099
  }
28077
28100
  async function evaluateMemberProperty(node, context) {
28078
28101
  const property = await evaluateNode(node, context);
@@ -28109,137 +28132,132 @@ async function evaluateMemberCallExpression(node, context) {
28109
28132
  if (node.callee.type !== "MemberExpression") {
28110
28133
  throw new TypeError("Expected member call expression.");
28111
28134
  }
28112
- const reference = await evaluateMemberAccess(node.callee, context);
28113
- if (reference.kind === "error") {
28114
- return reference;
28115
- }
28116
- if (reference.kind === "completion") {
28117
- return reference.result;
28118
- }
28119
- if (reference.kind === "nullish" || reference.object === null || reference.object === void 0) {
28120
- if (reference.kind === "nullish") {
28121
- return {
28122
- kind: "normal",
28123
- hasValue: true,
28124
- value: void 0
28125
- };
28135
+ return evaluateMemberAccess(node.callee, context, async (reference) => {
28136
+ if (reference.kind === "nullish" || reference.object === null || reference.object === void 0) {
28137
+ if (reference.kind === "nullish") {
28138
+ return {
28139
+ kind: "normal",
28140
+ hasValue: true,
28141
+ value: void 0
28142
+ };
28143
+ }
28144
+ throw new TypeError("Cannot read properties of null or undefined.");
28126
28145
  }
28127
- throw new TypeError("Cannot read properties of null or undefined.");
28128
- }
28129
- const member = {
28130
- ...reference,
28131
- property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
28132
- };
28133
- if (typeof member.object === "string" && isStringMethodName(member.property)) {
28134
- return evaluateStringMethodCall(node, member.object, member.property, context);
28135
- }
28136
- if (typeof member.object === "number" && isNumberMethodName(member.property)) {
28137
- return evaluateNumberMethodCall(node, member.object, member.property, context);
28138
- }
28139
- if (Array.isArray(member.object) && isArrayMethodName(member.property) && !Object.hasOwn(member.object, member.property)) {
28140
- return evaluateArrayMethodCall(node, member.object, member.property, context);
28141
- }
28142
- if (isSandboxMap(member.object) && isMapMethodName(member.property)) {
28143
- return evaluateMapMethodCall(node, member.object, member.property, context);
28144
- }
28145
- if (isSandboxSet(member.object) && isSetMethodName(member.property)) {
28146
- return evaluateSetMethodCall(node, member.object, member.property, context);
28147
- }
28148
- if (typeof member.object === "string") {
28149
- return evaluatePrimitiveMemberCall(
28150
- node,
28151
- "String",
28152
- member.property,
28153
- getStringMember(member.object, member.property, context.budget),
28154
- context
28155
- );
28156
- }
28157
- if (typeof member.object === "number") {
28158
- return evaluatePrimitiveMemberCall(
28159
- node,
28160
- "Number",
28161
- member.property,
28162
- getNumberMember(member.property, context.budget),
28163
- context
28164
- );
28165
- }
28166
- if (Array.isArray(member.object) && !Object.hasOwn(member.object, member.property)) {
28167
- return evaluatePrimitiveMemberCall(
28168
- node,
28169
- "Array",
28170
- member.property,
28171
- getArrayMemberValue(member.object, member.property, context),
28172
- context
28173
- );
28174
- }
28175
- if (isSandboxMap(member.object)) {
28176
- return evaluatePrimitiveMemberCall(
28177
- node,
28178
- "Map",
28179
- member.property,
28180
- getMapMember(member.object, member.property, createMapMethodOptions(context)),
28181
- context
28182
- );
28183
- }
28184
- if (isSandboxDate(member.object)) {
28185
- return evaluateResolvedCallExpression(node, getDateMember(member.property, context.budget, context.compilation?.owner), context, member.object);
28186
- }
28187
- if (isFloat32Array(member.object)) {
28188
- return evaluateResolvedCallExpression(
28189
- node,
28190
- getFloat32Member(member.object, member.property, context.budget),
28191
- context,
28192
- member.object
28193
- );
28194
- }
28195
- if (isSandboxSet(member.object)) {
28196
- return evaluatePrimitiveMemberCall(
28197
- node,
28198
- "Set",
28199
- member.property,
28200
- getSetMember(member.object, member.property, createSetMethodOptions(context)),
28201
- context
28202
- );
28203
- }
28204
- if (isSandboxGenerator(member.object)) {
28205
- const memberValue = getGeneratorMember(member.object, member.property, context.budget);
28206
- if (memberValue === void 0) {
28207
- throw new TypeError(`Generator#${String(member.property)} is not a supported method.`);
28146
+ const member = {
28147
+ ...reference,
28148
+ property: await toPropertyKey(reference.property, context.budget, createCoercionContext(context))
28149
+ };
28150
+ if (typeof member.object === "string" && isStringMethodName(member.property)) {
28151
+ return evaluateStringMethodCall(node, member.object, member.property, context);
28208
28152
  }
28209
- return evaluateResolvedCallExpression(node, memberValue, context, member.object);
28210
- }
28211
- if (isSandboxClosure(member.object)) {
28212
- const memberValue = getClosureMemberValue(member.object, member.property, context);
28213
- if (memberValue === void 0) {
28214
- throw new TypeError(`Function#${String(member.property)} is not a supported method.`);
28153
+ if (typeof member.object === "number" && isNumberMethodName(member.property)) {
28154
+ return evaluateNumberMethodCall(node, member.object, member.property, context);
28155
+ }
28156
+ if (Array.isArray(member.object) && isArrayMethodName(member.property) && !Object.hasOwn(member.object, member.property)) {
28157
+ return evaluateArrayMethodCall(node, member.object, member.property, context);
28158
+ }
28159
+ if (isSandboxMap(member.object) && isMapMethodName(member.property)) {
28160
+ return evaluateMapMethodCall(node, member.object, member.property, context);
28161
+ }
28162
+ if (isSandboxSet(member.object) && isSetMethodName(member.property)) {
28163
+ return evaluateSetMethodCall(node, member.object, member.property, context);
28164
+ }
28165
+ if (typeof member.object === "string") {
28166
+ return evaluatePrimitiveMemberCall(
28167
+ node,
28168
+ "String",
28169
+ member.property,
28170
+ getStringMember(member.object, member.property, context.budget),
28171
+ context
28172
+ );
28173
+ }
28174
+ if (typeof member.object === "number") {
28175
+ return evaluatePrimitiveMemberCall(
28176
+ node,
28177
+ "Number",
28178
+ member.property,
28179
+ getNumberMember(member.property, context.budget),
28180
+ context
28181
+ );
28182
+ }
28183
+ if (Array.isArray(member.object) && !Object.hasOwn(member.object, member.property)) {
28184
+ return evaluatePrimitiveMemberCall(
28185
+ node,
28186
+ "Array",
28187
+ member.property,
28188
+ getArrayMemberValue(member.object, member.property, context),
28189
+ context
28190
+ );
28191
+ }
28192
+ if (isSandboxMap(member.object)) {
28193
+ return evaluatePrimitiveMemberCall(
28194
+ node,
28195
+ "Map",
28196
+ member.property,
28197
+ getMapMember(member.object, member.property, createMapMethodOptions(context)),
28198
+ context
28199
+ );
28200
+ }
28201
+ if (isSandboxDate(member.object)) {
28202
+ return evaluateResolvedCallExpression(node, getDateMember(member.property, context.budget, context.compilation?.owner), context, member.object);
28203
+ }
28204
+ if (isFloat32Array(member.object)) {
28205
+ return evaluateResolvedCallExpression(
28206
+ node,
28207
+ getFloat32Member(member.object, member.property, context.budget),
28208
+ context,
28209
+ member.object
28210
+ );
28211
+ }
28212
+ if (isSandboxSet(member.object)) {
28213
+ return evaluatePrimitiveMemberCall(
28214
+ node,
28215
+ "Set",
28216
+ member.property,
28217
+ getSetMember(member.object, member.property, createSetMethodOptions(context)),
28218
+ context
28219
+ );
28220
+ }
28221
+ if (isSandboxGenerator(member.object)) {
28222
+ const memberValue = getGeneratorMember(member.object, member.property, context.budget);
28223
+ if (memberValue === void 0) {
28224
+ throw new TypeError(`Generator#${String(member.property)} is not a supported method.`);
28225
+ }
28226
+ return evaluateResolvedCallExpression(node, memberValue, context, member.object);
28227
+ }
28228
+ if (isSandboxClosure(member.object)) {
28229
+ const memberValue = getClosureMemberValue(member.object, member.property, context);
28230
+ if (memberValue === void 0) {
28231
+ throw new TypeError(`Function#${String(member.property)} is not a supported method.`);
28232
+ }
28233
+ return evaluateResolvedCallExpression(node, memberValue, context, member.object);
28234
+ }
28235
+ if (isSandboxPromise(member.object)) {
28236
+ return evaluateResolvedCallExpression(
28237
+ node,
28238
+ getPromiseMember(member.property, context.budget),
28239
+ context,
28240
+ member.object
28241
+ );
28242
+ }
28243
+ if (isSandboxRegex(member.object) && isRegexMethodName(member.property)) {
28244
+ return evaluateResolvedCallExpression(
28245
+ node,
28246
+ getRegexMember(member.object, member.property, context.budget),
28247
+ context,
28248
+ member.object
28249
+ );
28250
+ }
28251
+ if (!isIndexableSandboxValue(member.object)) {
28252
+ throw new TypeError("Attempted to read a property from a non-object value.");
28215
28253
  }
28216
- return evaluateResolvedCallExpression(node, memberValue, context, member.object);
28217
- }
28218
- if (isSandboxPromise(member.object)) {
28219
- return evaluateResolvedCallExpression(
28220
- node,
28221
- getPromiseMember(member.property, context.budget),
28222
- context,
28223
- member.object
28224
- );
28225
- }
28226
- if (isSandboxRegex(member.object) && isRegexMethodName(member.property)) {
28227
28254
  return evaluateResolvedCallExpression(
28228
28255
  node,
28229
- getRegexMember(member.object, member.property, context.budget),
28256
+ getMemberValue(member.object, member.property, context),
28230
28257
  context,
28231
28258
  member.object
28232
28259
  );
28233
- }
28234
- if (!isIndexableSandboxValue(member.object)) {
28235
- throw new TypeError("Attempted to read a property from a non-object value.");
28236
- }
28237
- return evaluateResolvedCallExpression(
28238
- node,
28239
- getMemberValue(member.object, member.property, context),
28240
- context,
28241
- member.object
28242
- );
28260
+ });
28243
28261
  }
28244
28262
  function evaluatePrimitiveMemberCall(node, receiverType, property, value, context) {
28245
28263
  if (value === void 0) {
@@ -31301,41 +31319,6 @@ function createBuiltinBindings(options) {
31301
31319
  };
31302
31320
  }
31303
31321
 
31304
- // packages/safe-js/src/interp/resources.ts
31305
- import { AsyncLocalStorage as AsyncLocalStorage5 } from "node:async_hooks";
31306
- var runResources = new AsyncLocalStorage5();
31307
- async function withRunResources(signal, execute) {
31308
- const controller = new AbortController();
31309
- const cancel = () => controller.abort(signal?.reason);
31310
- const cleanups = /* @__PURE__ */ new Set();
31311
- const resources = {
31312
- signal: controller.signal,
31313
- add(close) {
31314
- cleanups.add(close);
31315
- }
31316
- };
31317
- signal?.addEventListener("abort", cancel, { once: true });
31318
- if (signal?.aborted) cancel();
31319
- let result;
31320
- let failure;
31321
- let errors = [];
31322
- try {
31323
- result = await runResources.run(resources, execute);
31324
- } catch (error) {
31325
- failure = { reason: error };
31326
- } finally {
31327
- signal?.removeEventListener("abort", cancel);
31328
- controller.abort(new Error("SafeJS run finished."));
31329
- const outcomes = await Promise.allSettled(
31330
- [...cleanups].map((close) => Promise.resolve().then(close))
31331
- );
31332
- errors = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
31333
- }
31334
- if (failure !== void 0) throw failure.reason;
31335
- if (errors.length > 0) throw new AggregateError(errors, "SafeJS resource cleanup failed.");
31336
- return result;
31337
- }
31338
-
31339
31322
  // packages/safe-js/src/modules/registry.ts
31340
31323
  function createUnknownModuleMessage2(moduleName, moduleNames) {
31341
31324
  if (moduleNames.length === 0) {
@@ -31577,6 +31560,7 @@ var RealmState = class {
31577
31560
  extensions;
31578
31561
  consoleExtension;
31579
31562
  cleanups = [];
31563
+ referenceReleases = /* @__PURE__ */ new Set();
31580
31564
  callbacks = /* @__PURE__ */ new Map();
31581
31565
  pendingCallbacks = /* @__PURE__ */ new Set();
31582
31566
  callbackCache = /* @__PURE__ */ new WeakMap();
@@ -31903,7 +31887,7 @@ var RealmState = class {
31903
31887
  const pending = withSandboxPromiseRejectionTracker(
31904
31888
  this.tracker,
31905
31889
  () => runResources.run(
31906
- { signal: this.controller.signal, add: this.onCleanup },
31890
+ { signal: this.controller.signal, referenceReleases: this.referenceReleases, add: this.onCleanup },
31907
31891
  () => withCancellationSignal(
31908
31892
  this.controller.signal,
31909
31893
  () => active && this.phase.getStore()?.active ? runAsyncPrefix(invoke) : this.queue.run(invoke)
@@ -32098,7 +32082,7 @@ var RealmState = class {
32098
32082
  () => withSandboxPromiseRejectionTracker(
32099
32083
  this.tracker,
32100
32084
  () => runResources.run(
32101
- { signal: this.controller.signal, add: this.onCleanup },
32085
+ { signal: this.controller.signal, referenceReleases: this.referenceReleases, add: this.onCleanup },
32102
32086
  () => withCancellationSignal(this.controller.signal, task)
32103
32087
  )
32104
32088
  )
@@ -32155,6 +32139,8 @@ var RealmState = class {
32155
32139
  this.hostObjects.clear();
32156
32140
  for (const reference of this.guestReferences.keys()) revokeGuestReference(reference, this);
32157
32141
  this.guestReferences.clear();
32142
+ for (const release of this.referenceReleases) release();
32143
+ this.referenceReleases.clear();
32158
32144
  this.budget.setRetainedValues(this, void 0);
32159
32145
  releaseObjectPrototype(this.budget);
32160
32146
  this.disposal = (async () => {
@@ -33408,9 +33394,9 @@ export {
33408
33394
  validateSnapshotMigration,
33409
33395
  restore,
33410
33396
  lint,
33397
+ runResources,
33411
33398
  declareHostOperation,
33412
33399
  createSeededRandom,
33413
- runResources,
33414
33400
  createReplayableRandom,
33415
33401
  createRealm,
33416
33402
  dump,
@@ -33420,4 +33406,4 @@ export {
33420
33406
  FileSnapshotBackend,
33421
33407
  run
33422
33408
  };
33423
- //# sourceMappingURL=chunk-N3CN2ACZ.js.map
33409
+ //# sourceMappingURL=chunk-DHD453E5.js.map