@poe-platform/safe-js 0.1.404 → 0.1.406

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.
@@ -27,7 +27,7 @@ import {
27
27
  validateMigrationSemantics,
28
28
  validateSnapshotData,
29
29
  validateSnapshotMigration
30
- } from "./chunk-LQCVRFUS.js";
30
+ } from "./chunk-XY5ZPK3G.js";
31
31
 
32
32
  // packages/safe-js/src/migrate.ts
33
33
  import { createHash } from "node:crypto";
@@ -8249,4 +8249,4 @@ export {
8249
8249
  parseMcpConfig,
8250
8250
  makeMcpModule
8251
8251
  };
8252
- //# sourceMappingURL=chunk-2RUHJIRX.js.map
8252
+ //# sourceMappingURL=chunk-WHJV4CR7.js.map
@@ -10766,6 +10766,9 @@ function getGeneratorOrigin(value) {
10766
10766
  return generatorOrigins.get(value);
10767
10767
  }
10768
10768
 
10769
+ // packages/safe-js/src/interp/construction-state.ts
10770
+ var constructionStates = /* @__PURE__ */ new WeakMap();
10771
+
10769
10772
  // packages/safe-js/src/interp/async.ts
10770
10773
  function emitResumeBreakpoint(context, breakpoint) {
10771
10774
  context.onYield?.({
@@ -10809,6 +10812,8 @@ function createInterpretedClosure(node, context, evaluateNode2, homeObject, init
10809
10812
  if (node.type !== "ArrowFunctionExpression" && node.generator) {
10810
10813
  return createGeneratorClosure(node, context, evaluateNode2, initializeGeneratorPrototype);
10811
10814
  }
10815
+ const construction = context.functionEnvironment?.construction;
10816
+ const constructionState = construction === void 0 ? void 0 : constructionStates.get(construction);
10812
10817
  const construct = node.type !== "ArrowFunctionExpression" && !(node.type === "FunctionExpression" && node.method === true) && !node.async ? async (args, callContext) => {
10813
10818
  const thisValue = {};
10814
10819
  const newTarget = callContext?.newTarget ?? closure;
@@ -10838,7 +10843,19 @@ function createInterpretedClosure(node, context, evaluateNode2, homeObject, init
10838
10843
  ...node.async ? { async: true } : {},
10839
10844
  ...node.type === "FunctionDeclaration" || node.type === "FunctionExpression" ? node.id === void 0 ? { name: context.inferredName } : { name: node.id.name } : { name: context.inferredName },
10840
10845
  ...construct === void 0 ? {} : { construct },
10841
- retainedValues: () => [...context.scope.retainedValues(), context.functionEnvironment?.homeObject, context.functionEnvironment?.newTarget],
10846
+ retainedValues: () => {
10847
+ const values = [...context.scope.retainedValues(), context.functionEnvironment?.homeObject, context.functionEnvironment?.newTarget];
10848
+ if (constructionState !== void 0) {
10849
+ values.push(
10850
+ constructionState.constructor,
10851
+ constructionState.newTarget,
10852
+ constructionState.prototype,
10853
+ constructionState.thisValue
10854
+ );
10855
+ if (constructionState.thisScope !== void 0) values.push(...constructionState.thisScope.retainedValues());
10856
+ }
10857
+ return values;
10858
+ },
10842
10859
  call: (args, callContext) => {
10843
10860
  const invocationContext = {
10844
10861
  ...context,
@@ -15903,64 +15920,81 @@ function createClassConstructor(node, context, evaluateNode2, fields2) {
15903
15920
  construct: async (args, invocation) => {
15904
15921
  const prototype = materializeFunctionProperties(constructor).prototype;
15905
15922
  const newTarget = invocation?.newTarget ?? constructor;
15906
- let thisValue;
15907
- let thisScope;
15908
- let initialized = !derived;
15909
- const initializeFields = async (receiver) => {
15910
- for (const [name, element] of classOrigins.get(constructor).privateMethods) addPrivateElement(receiver, name, element);
15911
- for (const field of fields2) {
15912
- await initializeElement(field, receiver, prototype, classContext, evaluateNode2);
15913
- }
15914
- };
15915
- const superCall = async (argumentsList) => {
15916
- const superConstructor = getSandboxPrototype(constructor, context.budget);
15917
- if (!isSandboxClosure(superConstructor) || superConstructor.construct === void 0)
15918
- throw new TypeError("Super constructor is not a constructor.");
15919
- const receiver = await invocation.invokeClosure(superConstructor, argumentsList, void 0, true, newTarget);
15920
- if (initialized) throw new ReferenceError("Super constructor may only initialize this once.");
15921
- initialized = true;
15922
- thisValue = receiver;
15923
- thisScope?.declare("this", "const", receiver);
15924
- await initializeFields(receiver);
15925
- return receiver;
15923
+ const state = {
15924
+ constructor,
15925
+ newTarget,
15926
+ prototype,
15927
+ thisValue: void 0,
15928
+ thisScope: void 0,
15929
+ initialized: !derived,
15930
+ activeCalls: 1
15926
15931
  };
15927
- if (!derived) {
15928
- thisValue = {};
15929
- const targetPrototype = await invocation.getProperty(newTarget, "prototype");
15930
- if (typeof targetPrototype === "object" && targetPrototype !== null)
15931
- setSandboxPrototype(thisValue, targetPrototype, context.budget);
15932
- }
15933
- if (constructorElement === void 0) {
15934
- if (derived) return superCall(args);
15935
- await initializeFields(thisValue);
15936
- return thisValue;
15937
- }
15938
- const result = await executeClosure(constructorElement.value, args, thisValue, {
15939
- ...classContext,
15940
- compilation: invocation?.compilation ?? context.compilation,
15941
- callStack: [...invocation?.stack ?? context.callStack],
15942
- functionEnvironment: {
15943
- newTarget,
15944
- homeObject: prototype,
15945
- construction: {
15946
- derived,
15947
- superCall,
15948
- initialize: async (functionScope) => {
15949
- thisScope = functionScope;
15950
- if (!derived) await initializeFields(thisValue);
15951
- }
15952
- }
15953
- }
15954
- }, evaluateNode2);
15955
- if (typeof result === "object" && result !== null) return result;
15956
- if (derived && result !== void 0) throw new TypeError("Derived constructors may only return an object or undefined.");
15957
- if (!initialized) throw new ReferenceError("Must call super constructor before returning from derived constructor.");
15958
- return thisValue;
15932
+ try {
15933
+ const construction = createConstructionEnvironment(state, classContext, evaluateNode2, invocation);
15934
+ if (!derived) {
15935
+ state.thisValue = {};
15936
+ const targetPrototype = await invocation.getProperty(newTarget, "prototype");
15937
+ if (typeof targetPrototype === "object" && targetPrototype !== null)
15938
+ setSandboxPrototype(state.thisValue, targetPrototype, context.budget);
15939
+ }
15940
+ if (constructorElement === void 0) {
15941
+ if (derived) return construction.superCall(args);
15942
+ await initializeConstructionFields(state, classContext, evaluateNode2);
15943
+ return state.thisValue;
15944
+ }
15945
+ const result = await executeClosure(constructorElement.value, args, state.thisValue, {
15946
+ ...classContext,
15947
+ compilation: invocation?.compilation ?? context.compilation,
15948
+ callStack: [...invocation?.stack ?? context.callStack],
15949
+ functionEnvironment: { newTarget, homeObject: prototype, construction }
15950
+ }, evaluateNode2);
15951
+ if (typeof result === "object" && result !== null) return result;
15952
+ if (derived && result !== void 0) throw new TypeError("Derived constructors may only return an object or undefined.");
15953
+ if (!state.initialized) throw new ReferenceError("Must call super constructor before returning from derived constructor.");
15954
+ return state.thisValue;
15955
+ } finally {
15956
+ state.activeCalls--;
15957
+ }
15959
15958
  }
15960
15959
  });
15961
15960
  classOrigins.set(constructor, { node, scope, fields: fields2, initialized: false, privateMethods: /* @__PURE__ */ new Map() });
15962
15961
  return constructor;
15963
15962
  }
15963
+ function createConstructionEnvironment(state, context, evaluateNode2, invocation = createCoercionContext(context)) {
15964
+ const derived = classOrigins.get(state.constructor).node.superClass !== void 0;
15965
+ const construction = {
15966
+ derived,
15967
+ initialize: async (scope) => {
15968
+ state.thisScope = scope;
15969
+ if (!derived) await initializeConstructionFields(state, context, evaluateNode2);
15970
+ },
15971
+ superCall: async (args) => {
15972
+ state.activeCalls++;
15973
+ try {
15974
+ const parent = getSandboxPrototype(state.constructor, context.budget);
15975
+ if (!isSandboxClosure(parent) || parent.construct === void 0)
15976
+ throw new TypeError("Super constructor is not a constructor.");
15977
+ const receiver = await invocation.invokeClosure(parent, args, void 0, true, state.newTarget);
15978
+ if (state.initialized) throw new ReferenceError("Super constructor may only initialize this once.");
15979
+ state.initialized = true;
15980
+ state.thisValue = receiver;
15981
+ state.thisScope?.declare("this", "const", receiver);
15982
+ await initializeConstructionFields(state, context, evaluateNode2);
15983
+ return receiver;
15984
+ } finally {
15985
+ state.activeCalls--;
15986
+ }
15987
+ }
15988
+ };
15989
+ constructionStates.set(construction, state);
15990
+ return construction;
15991
+ }
15992
+ async function initializeConstructionFields(state, context, evaluateNode2) {
15993
+ const origin = classOrigins.get(state.constructor);
15994
+ for (const [name, element] of origin.privateMethods) addPrivateElement(state.thisValue, name, element);
15995
+ for (const field of origin.fields)
15996
+ await initializeElement(field, state.thisValue, state.prototype, context, evaluateNode2);
15997
+ }
15964
15998
  async function initializeElement(definition, receiver, homeObject, context, evaluateNode2) {
15965
15999
  const scope = context.scope.child({}, { functionBoundary: true });
15966
16000
  scope.declare("this", "const", receiver);
@@ -22368,6 +22402,20 @@ function serializeCollectionProperties(value, encode, dataOnly = false) {
22368
22402
 
22369
22403
  // packages/safe-js/src/snapshot/guest-heap.ts
22370
22404
  function captureGuestHeapNode(value, encode) {
22405
+ const construction = constructionStates.get(value);
22406
+ if (construction !== void 0) {
22407
+ if (construction.activeCalls !== 0 || construction.thisScope === void 0)
22408
+ throw new TypeError("Active class construction environments cannot yet be serialized.");
22409
+ return {
22410
+ kind: "construction-environment",
22411
+ constructor: encode(construction.constructor),
22412
+ newTarget: encode(construction.newTarget),
22413
+ prototype: encode(construction.prototype),
22414
+ thisValue: encode(construction.thisValue),
22415
+ thisScope: encode(construction.thisScope),
22416
+ initialized: construction.initialized
22417
+ };
22418
+ }
22371
22419
  const executor = isSandboxClosure(value) ? promiseCapabilityExecutors.get(value) : void 0;
22372
22420
  if (executor !== void 0)
22373
22421
  return { kind: "capability-executor", resolve: encode(executor.resolve), reject: encode(executor.reject), state: captureObjectState(value, encode) };
@@ -22593,7 +22641,6 @@ function captureGuestHeapNode(value, encode) {
22593
22641
  if (isSandboxGenerator(value)) {
22594
22642
  const origin2 = getGeneratorOrigin(value);
22595
22643
  if (origin2?.node.nodeId === void 0) throw new TypeError("Generators require a captured origin for public dumps.");
22596
- if (origin2.environment?.construction !== void 0) throw new TypeError("Active class construction environments cannot yet be serialized.");
22597
22644
  const channel = value.channel.snapshot();
22598
22645
  return {
22599
22646
  kind: "guest-generator",
@@ -22674,7 +22721,8 @@ function captureGuestHeapNode(value, encode) {
22674
22721
  sent: channel.sent.map((completion) => ({ type: completion.type, value: encode(completion.value) })),
22675
22722
  ...origin2.environment === void 0 ? {} : { environment: {
22676
22723
  ...origin2.environment.homeObject === void 0 ? {} : { homeObject: encode(origin2.environment.homeObject) },
22677
- ...origin2.environment.newTarget === void 0 ? {} : { newTarget: encode(origin2.environment.newTarget) }
22724
+ ...origin2.environment.newTarget === void 0 ? {} : { newTarget: encode(origin2.environment.newTarget) },
22725
+ ...origin2.environment.construction === void 0 ? {} : { construction: encode(origin2.environment.construction) }
22678
22726
  } }
22679
22727
  };
22680
22728
  }
@@ -22707,8 +22755,6 @@ function captureGuestHeapNode(value, encode) {
22707
22755
  return void 0;
22708
22756
  }
22709
22757
  if (origin.node.nodeId === void 0) throw new TypeError("Guest closures require an AST node identity.");
22710
- if (origin.environment?.construction !== void 0)
22711
- throw new TypeError("Active class construction environments cannot yet be serialized.");
22712
22758
  const state = captureObjectState(value, encode);
22713
22759
  if (state === void 0) throw new TypeError("Guest closures require a property state.");
22714
22760
  return {
@@ -22719,7 +22765,8 @@ function captureGuestHeapNode(value, encode) {
22719
22765
  ...isSandboxClosure(value) && value.name !== void 0 ? { name: value.name } : {},
22720
22766
  ...origin.environment === void 0 ? {} : { environment: {
22721
22767
  ...origin.environment.homeObject === void 0 ? {} : { homeObject: encode(origin.environment.homeObject) },
22722
- ...origin.environment.newTarget === void 0 ? {} : { newTarget: encode(origin.environment.newTarget) }
22768
+ ...origin.environment.newTarget === void 0 ? {} : { newTarget: encode(origin.environment.newTarget) },
22769
+ ...origin.environment.construction === void 0 ? {} : { construction: encode(origin.environment.construction) }
22723
22770
  } }
22724
22771
  };
22725
22772
  }
@@ -27913,7 +27960,7 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
27913
27960
  createRawJson(node.text);
27914
27961
  return true;
27915
27962
  }
27916
- if (!["capability-executor", "promise-aggregate", "aggregate-entry", "aggregate-handler", "intrinsic", "bound-function", "promise-resolver", "pending-promise", "promise-reaction", "promise-adoption", "adoption-resolver", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "guest-boxed", "guest-date", "guest-regex", "guest-promise", "array-iterator", "string-iterator", "iterator-wrapper", "iterator-helper", "guest-collection-iterator", "guest-regexp-iterator", "map", "set"].includes(String(node.kind))) return false;
27963
+ if (!["construction-environment", "capability-executor", "promise-aggregate", "aggregate-entry", "aggregate-handler", "intrinsic", "bound-function", "promise-resolver", "pending-promise", "promise-reaction", "promise-adoption", "adoption-resolver", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "guest-boxed", "guest-date", "guest-regex", "guest-promise", "array-iterator", "string-iterator", "iterator-wrapper", "iterator-helper", "guest-collection-iterator", "guest-regexp-iterator", "map", "set"].includes(String(node.kind))) return false;
27917
27964
  const reference = (value, kinds) => {
27918
27965
  const ref = record3(value);
27919
27966
  fields(ref, ["kind", "id"]);
@@ -28005,7 +28052,25 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
28005
28052
  });
28006
28053
  return false;
28007
28054
  }
28008
- if (node.kind === "capability-executor") {
28055
+ if (node.kind === "construction-environment") {
28056
+ fields(node, ["kind", "constructor", "newTarget", "prototype", "thisValue", "thisScope", "initialized"]);
28057
+ const owner = reference(node.constructor, ["guest-class"]);
28058
+ callable(node.newTarget);
28059
+ const prototype = reference(node.prototype);
28060
+ const scope = reference(node.thisScope, ["scope-frame"]);
28061
+ if (typeof node.initialized !== "boolean") throw new TypeError("Invalid construction initialization flag.");
28062
+ if (!node.initialized && !absent(node.thisValue)) throw new TypeError("Uninitialized construction cannot have this value.");
28063
+ const property = array(record3(record3(owner.state).properties).properties).map(array).find((entry) => entry[0] === "prototype");
28064
+ if (property === void 0 || record3(property[1]).kind !== "data" || reference(record3(property[1]).value) !== prototype)
28065
+ throw new TypeError("Invalid construction prototype ownership.");
28066
+ if (absent(scope.parent) || reference(scope.parent, ["scope-frame"]) !== reference(owner.scope, ["scope-frame"]))
28067
+ throw new TypeError("Invalid construction scope ownership.");
28068
+ const binding = array(scope.bindings).map(array).find((entry) => entry[0] === "this");
28069
+ if (binding === void 0) throw new TypeError("Invalid construction this binding.");
28070
+ const cell = record3(array(scope.cells)[integer(binding[1])]);
28071
+ if (cell.kind !== "const" || cell.initialized !== node.initialized || node.initialized && reference(cell.value) !== reference(node.thisValue))
28072
+ throw new TypeError("Invalid construction this binding.");
28073
+ } else if (node.kind === "capability-executor") {
28009
28074
  fields(node, ["kind", "resolve", "reject", "state"]);
28010
28075
  state(node.state);
28011
28076
  } else if (node.kind === "promise-aggregate") {
@@ -28294,9 +28359,10 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
28294
28359
  if (Object.hasOwn(node, "name") && typeof node.name !== "string") throw new TypeError("Invalid guest function name.");
28295
28360
  if (Object.hasOwn(node, "environment")) {
28296
28361
  const environment = record3(node.environment);
28297
- fields(environment, [], ["homeObject", "newTarget"]);
28362
+ fields(environment, [], ["homeObject", "newTarget", "construction"]);
28298
28363
  if (Object.hasOwn(environment, "homeObject")) reference(environment.homeObject);
28299
28364
  if (Object.hasOwn(environment, "newTarget")) callable(environment.newTarget);
28365
+ if (Object.hasOwn(environment, "construction")) reference(environment.construction, ["construction-environment"]);
28300
28366
  }
28301
28367
  state(node.state);
28302
28368
  } else if (node.kind === "guest-generator") {
@@ -28463,9 +28529,10 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
28463
28529
  }
28464
28530
  if (Object.hasOwn(node, "environment")) {
28465
28531
  const environment = record3(node.environment);
28466
- fields(environment, [], ["homeObject", "newTarget"]);
28532
+ fields(environment, [], ["homeObject", "newTarget", "construction"]);
28467
28533
  if (Object.hasOwn(environment, "homeObject")) reference(environment.homeObject);
28468
28534
  if (Object.hasOwn(environment, "newTarget")) callable(environment.newTarget);
28535
+ if (Object.hasOwn(environment, "construction")) reference(environment.construction, ["construction-environment"]);
28469
28536
  }
28470
28537
  } else {
28471
28538
  fields(node, ["kind", "parent", "importMeta", "functionBoundary", "chargeData", "bindings", "cells"], ["restoredBindings", "privateNames"]);
@@ -28917,7 +28984,7 @@ function validateDumpHeap(root, state) {
28917
28984
  fail("invalidCycle", "$.heap", error instanceof Error ? error.message : "invalid guest scope parent graph");
28918
28985
  }
28919
28986
  }
28920
- function validateDumpReferences(value, path, depth, state, heapIds, heap, role = "data", allowScopeReference = false) {
28987
+ function validateDumpReferences(value, path, depth, state, heapIds, heap, role = "data", allowScopeReference = false, allowConstructionReference = false) {
28921
28988
  if (value === null || typeof value !== "object") return;
28922
28989
  if (depth > state.limits.maxDepth)
28923
28990
  fail("budgetExceeded", path, `exceeds nesting limit ${state.limits.maxDepth}`);
@@ -28933,11 +29000,23 @@ function validateDumpReferences(value, path, depth, state, heapIds, heap, role =
28933
29000
  if (!heapIds.has(id)) fail("danglingReference", `${path}.id`, `unknown heap value ${id}`);
28934
29001
  if (heap[String(id)].kind === "scope-frame" && !allowScopeReference)
28935
29002
  fail("invalidValue", path, "Internal scopes cannot be guest data");
29003
+ if (heap[String(id)].kind === "construction-environment" && !allowConstructionReference)
29004
+ fail("invalidValue", path, "Internal construction environments cannot be guest data");
28936
29005
  }
28937
29006
  for (const [key, entry] of Object.entries(record4)) {
28938
- const childRole = role === "root" && key === "heap" ? "heap" : role === "heap" ? "heap-node" : role === "heap-node" && record4.kind === "guest-generator" && key === "blockScopes" ? "scope-map" : role === "heap-node" && record4.kind === "guest-generator" && key === "expressionStates" ? "expressions" : role === "expressions" ? "expression" : "data";
28939
- const scopeField = role === "scope-map" || role === "expression" && (record4.kind === "for" && ["loopScope", "activeScope"].includes(key) || ["switch", "for-in", "for-of-array", "for-of-iterator"].includes(String(record4.kind)) && key === "scope") || role === "heap-node" && (record4.kind === "scope-frame" && key === "parent" || (record4.kind === "guest-function" || record4.kind === "guest-class") && key === "scope" || record4.kind === "guest-generator" && ["scope", "closureScope", "suspendedScope"].includes(key));
28940
- validateDumpReferences(entry, `${path}${formatKey(key)}`, depth + 1, state, heapIds, heap, childRole, scopeField);
29007
+ const childRole = role === "root" && key === "heap" ? "heap" : role === "heap" ? "heap-node" : role === "heap-node" && (record4.kind === "guest-function" || record4.kind === "guest-generator") && key === "environment" ? "function-environment" : role === "heap-node" && record4.kind === "guest-generator" && key === "blockScopes" ? "scope-map" : role === "heap-node" && record4.kind === "guest-generator" && key === "expressionStates" ? "expressions" : role === "expressions" ? "expression" : "data";
29008
+ const scopeField = role === "scope-map" || role === "expression" && (record4.kind === "for" && ["loopScope", "activeScope"].includes(key) || ["switch", "for-in", "for-of-array", "for-of-iterator"].includes(String(record4.kind)) && key === "scope") || role === "heap-node" && (record4.kind === "scope-frame" && key === "parent" || record4.kind === "construction-environment" && key === "thisScope" || (record4.kind === "guest-function" || record4.kind === "guest-class") && key === "scope" || record4.kind === "guest-generator" && ["scope", "closureScope", "suspendedScope"].includes(key));
29009
+ validateDumpReferences(
29010
+ entry,
29011
+ `${path}${formatKey(key)}`,
29012
+ depth + 1,
29013
+ state,
29014
+ heapIds,
29015
+ heap,
29016
+ childRole,
29017
+ scopeField,
29018
+ role === "function-environment" && key === "construction"
29019
+ );
28941
29020
  }
28942
29021
  }
28943
29022
  function validateRunSnapshotState(root, state) {
@@ -30236,9 +30315,8 @@ function measureSandboxData(values, options = {}) {
30236
30315
  const storage = typedArrayStorage(value);
30237
30316
  visit(storage.buffer, depth + 1);
30238
30317
  for (const [key, descriptor] of typedArrayProperties(value)) {
30239
- usage += 1;
30240
- if (typeof key === "string") usage += key.length;
30241
- else visit(key, depth + 1);
30318
+ if (typeof key !== "string") continue;
30319
+ usage += 1 + key.length;
30242
30320
  if ("value" in descriptor) visit(descriptor.value, depth + 1);
30243
30321
  else for (const closure of retainedAccessorClosures(descriptor)) visit(closure, depth + 1);
30244
30322
  }
@@ -45374,4 +45452,4 @@ export {
45374
45452
  FileSnapshotBackend,
45375
45453
  run
45376
45454
  };
45377
- //# sourceMappingURL=chunk-LQCVRFUS.js.map
45455
+ //# sourceMappingURL=chunk-XY5ZPK3G.js.map