@poe-platform/safe-js 0.1.301 → 0.1.302

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-M4L3EZHL.js";
30
+ } from "./chunk-T2TQBEDG.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-R2SP3JEZ.js.map
8252
+ //# sourceMappingURL=chunk-RPUBUYIO.js.map
@@ -7398,6 +7398,9 @@ function graphEntries(value) {
7398
7398
  return entries;
7399
7399
  }
7400
7400
 
7401
+ // packages/safe-js/src/interp/generator-prototypes.ts
7402
+ var generatorPrototypes = /* @__PURE__ */ new WeakMap();
7403
+
7401
7404
  // packages/safe-js/src/interp/resources.ts
7402
7405
  import { AsyncLocalStorage } from "node:async_hooks";
7403
7406
  var runResources = new AsyncLocalStorage();
@@ -8082,6 +8085,7 @@ function releaseObjectPrototype(budget) {
8082
8085
  datePrototypes.delete(budget);
8083
8086
  arrayPrototypes.delete(budget);
8084
8087
  functionPrototypes.delete(budget);
8088
+ generatorPrototypes.delete(budget);
8085
8089
  initialRegexDescriptors.delete(budget);
8086
8090
  intrinsicPrototypes.delete(budget);
8087
8091
  }
@@ -9455,13 +9459,12 @@ async function acquireSandboxIterator(value, budget, context, asyncProtocol = fa
9455
9459
  return asyncProtocol ? getSandboxAsyncIterator(value, budget, context, signal) : getSandboxIterator(value, budget, context);
9456
9460
  if (getSandboxPropertyDescriptor(value, key, budget) === void 0 && !(isSandboxRegExpIterator(value) && !asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)) {
9457
9461
  if (isSandboxGenerator(value)) {
9458
- if (hasExplicitSandboxPrototype(value)) return void 0;
9459
- if (value.async === true === asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)
9462
+ if (!hasExplicitSandboxPrototype(value) && value.async === true === asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)
9460
9463
  return guestIterator(value, await context.getProperty(value, "next"), asyncProtocol, budget, context, signal);
9461
9464
  }
9462
9465
  if (!asyncProtocol && Array.isArray(value) && getSandboxPrototype(value, budget) !== null) return void 0;
9463
9466
  if (!asyncProtocol) return getSandboxIterator(value, budget, context);
9464
- if (isSandboxGenerator(value) && value.async)
9467
+ if (isSandboxGenerator(value) && value.async && !hasExplicitSandboxPrototype(value))
9465
9468
  return getSandboxAsyncIterator(value, budget, context, signal);
9466
9469
  const iterator2 = await acquireSandboxIterator(value, budget, context);
9467
9470
  return iterator2 === void 0 ? void 0 : asyncFromSyncIterator(iterator2, budget, signal, context);
@@ -9523,7 +9526,7 @@ async function readIteratorResult(iterator, result, property) {
9523
9526
  }
9524
9527
  function getSandboxAsyncIterator(value, budget, context, signal) {
9525
9528
  if (isSandboxGenerator(value) && value.async) {
9526
- return hasExplicitSandboxPrototype(value) ? void 0 : { ...generatorObjectIterator(value, budget, context), asyncProtocol: true };
9529
+ return hasExplicitSandboxPrototype(value) ? context?.getProperty === void 0 ? generatorProtocolAdapter(value, budget, context, true, signal) : void 0 : { ...generatorObjectIterator(value, budget, context), asyncProtocol: true };
9527
9530
  }
9528
9531
  if (value !== null && (typeof value === "object" || typeof value === "function") && !isGuestHostObject(value)) {
9529
9532
  const method = value[Symbol.asyncIterator];
@@ -9673,7 +9676,9 @@ function getSandboxIterator(value, budget, context) {
9673
9676
  return syncIterator(Float32Array.prototype.values.call(value));
9674
9677
  }
9675
9678
  if (isSandboxGenerator(value)) {
9676
- return value.async || hasExplicitSandboxPrototype(value) ? void 0 : generatorObjectIterator(value, budget, context);
9679
+ if (value.async) return void 0;
9680
+ if (hasExplicitSandboxPrototype(value)) return context?.getProperty === void 0 ? generatorProtocolAdapter(value, budget ?? new Budget(), context, false) : void 0;
9681
+ return generatorObjectIterator(value, budget, context);
9677
9682
  }
9678
9683
  if (typeof value === "string") {
9679
9684
  const iterator2 = value[Symbol.iterator]();
@@ -9750,6 +9755,38 @@ function collectionIterator(collection, value) {
9750
9755
  return adapter;
9751
9756
  }
9752
9757
  var asyncGeneratorRequests = /* @__PURE__ */ new WeakMap();
9758
+ function generatorProtocolAdapter(value, budget, context, asyncProtocol, signal) {
9759
+ const bridge = {
9760
+ ...context,
9761
+ stack: context?.stack ?? [],
9762
+ thisValue: value,
9763
+ invokeClosure: (closure, args, receiver, construct) => invokeBuiltinClosure(closure, args, budget, context, receiver, construct),
9764
+ getProperty: (target, key) => {
9765
+ const descriptor = getSandboxPropertyDescriptor(target, key, budget);
9766
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, target, bridge, true);
9767
+ }
9768
+ };
9769
+ let resolved;
9770
+ let pending;
9771
+ const initialize = () => pending ??= acquireSandboxIterator(value, budget, bridge, asyncProtocol, signal).then((iterator) => {
9772
+ if (iterator === void 0) throw new TypeError("Generator does not provide an iterator protocol.");
9773
+ return resolved = iterator;
9774
+ });
9775
+ return {
9776
+ asynchronous: true,
9777
+ ...asyncProtocol ? { asyncProtocol: true } : {},
9778
+ get retainedValue() {
9779
+ return [value, resolved?.retainedValue];
9780
+ },
9781
+ snapshot: () => resolved?.snapshot?.() ?? { kind: "unsupported" },
9782
+ next: async (...args) => (await initialize()).next(...args),
9783
+ getOperation: async (method) => {
9784
+ const iterator = await initialize();
9785
+ return iterator.getOperation === void 0 ? iterator[method] : iterator.getOperation(method);
9786
+ },
9787
+ readResultProperty: async (result, property) => readIteratorResult(await initialize(), result, property)
9788
+ };
9789
+ }
9753
9790
  function generatorObjectIterator(generator, budget, context) {
9754
9791
  const iterator = generatorIterator(generator, budget, context);
9755
9792
  if (context?.getProperty === void 0) return iterator;
@@ -10225,12 +10262,12 @@ async function evaluateFunctionExpression(node, context, evaluateNode2) {
10225
10262
  value: closure
10226
10263
  };
10227
10264
  }
10228
- function createInterpretedClosure(node, context, evaluateNode2, homeObject) {
10265
+ function createInterpretedClosure(node, context, evaluateNode2, homeObject, initializeGeneratorPrototype = true) {
10229
10266
  if (node.type !== "ArrowFunctionExpression") {
10230
10267
  context = { ...context, functionEnvironment: { homeObject } };
10231
10268
  }
10232
10269
  if (node.type !== "ArrowFunctionExpression" && node.generator) {
10233
- return createGeneratorClosure(node, context, evaluateNode2);
10270
+ return createGeneratorClosure(node, context, evaluateNode2, initializeGeneratorPrototype);
10234
10271
  }
10235
10272
  const construct = node.type !== "ArrowFunctionExpression" && !(node.type === "FunctionExpression" && node.method === true) && !node.async ? async (args, callContext) => {
10236
10273
  const thisValue = {};
@@ -10317,7 +10354,8 @@ function executeAsyncFunction(execute, budget, signal, callContext) {
10317
10354
  });
10318
10355
  return createSandboxPromise(promise, { synchronousPrefix });
10319
10356
  }
10320
- function createGeneratorClosure(node, context, evaluateNode2) {
10357
+ function createGeneratorClosure(node, context, evaluateNode2, initializePrototype) {
10358
+ const prototypes2 = !initializePrototype || runResources.getStore()?.functionSourceText === false ? void 0 : generatorPrototypes.get(context.budget)?.get(node.async === true);
10321
10359
  const closure = createSandboxClosure({
10322
10360
  sourceRange: functionSources.get(node),
10323
10361
  guest: true,
@@ -10327,54 +10365,69 @@ function createGeneratorClosure(node, context, evaluateNode2) {
10327
10365
  ...node.id === void 0 ? { name: context.inferredName } : { name: node.id.name },
10328
10366
  retainedValues: () => [...context.scope.retainedValues(), context.functionEnvironment?.homeObject, context.functionEnvironment?.newTarget],
10329
10367
  call: async (args, callContext) => {
10330
- const closureContext = {
10331
- ...context,
10332
- compilation: callContext?.compilation ?? context.compilation,
10333
- callStack: [...callContext?.stack ?? context.callStack]
10334
- };
10335
- const scope = await createClosureScope(
10336
- node,
10337
- args,
10338
- callContext?.thisValue,
10339
- closureContext,
10340
- evaluateNode2
10341
- );
10342
- const channel = createGeneratorChannel((generatorYield) => {
10343
- const execute = async () => {
10344
- const result = await evaluateNode2(node.body, {
10345
- ...closureContext,
10346
- functionBody: node.body,
10347
- asyncGenerator: node.async,
10348
- generatorBlockScopes: /* @__PURE__ */ new Map(),
10349
- generatorExpressionStates: /* @__PURE__ */ new Map(),
10350
- captureGeneratorScope: (suspendedScope, blocks, completions, expressions) => {
10351
- origin.suspendedScope = suspendedScope;
10352
- origin.blockScopes = blocks;
10353
- origin.finallyCompletions = completions;
10354
- origin.expressionStates = expressions;
10355
- },
10356
- generatorYield: (value2, yieldNodeId) => {
10357
- generator.state = "suspended";
10358
- return generatorYield(value2, yieldNodeId);
10359
- },
10360
- scope
10361
- });
10362
- if (result.kind === "error") {
10363
- throw result.error;
10364
- }
10365
- if (result.kind === "throw") {
10366
- throw result.value;
10367
- }
10368
- const value = result.hasValue ? result.value : void 0;
10369
- return node.async ? awaitSandboxValue(value, context.signal, context.budget, createCoercionContext(context)) : value;
10368
+ const candidate = prototypes2 === void 0 ? void 0 : getGuestFunctionProperty(closure, "prototype");
10369
+ const prototype = typeof candidate === "object" && candidate !== null ? candidate : prototypes2?.instancePrototype;
10370
+ const releasePrototype = prototype === void 0 ? void 0 : retainValues(context.budget, () => [prototype]);
10371
+ try {
10372
+ const closureContext = {
10373
+ ...context,
10374
+ compilation: callContext?.compilation ?? context.compilation,
10375
+ callStack: [...callContext?.stack ?? context.callStack]
10370
10376
  };
10371
- return node.async ? runAsyncPrefix(execute) : execute();
10372
- });
10373
- const generator = createSandboxGenerator(channel, { async: node.async });
10374
- const origin = registerGeneratorOrigin(generator, node, scope, closureContext);
10375
- return generator;
10377
+ const scope = await createClosureScope(
10378
+ node,
10379
+ args,
10380
+ callContext?.thisValue,
10381
+ closureContext,
10382
+ evaluateNode2
10383
+ );
10384
+ const channel = createGeneratorChannel((generatorYield) => {
10385
+ const execute = async () => {
10386
+ const result = await evaluateNode2(node.body, {
10387
+ ...closureContext,
10388
+ functionBody: node.body,
10389
+ asyncGenerator: node.async,
10390
+ generatorBlockScopes: /* @__PURE__ */ new Map(),
10391
+ generatorExpressionStates: /* @__PURE__ */ new Map(),
10392
+ captureGeneratorScope: (suspendedScope, blocks, completions, expressions) => {
10393
+ origin.suspendedScope = suspendedScope;
10394
+ origin.blockScopes = blocks;
10395
+ origin.finallyCompletions = completions;
10396
+ origin.expressionStates = expressions;
10397
+ },
10398
+ generatorYield: (value2, yieldNodeId) => {
10399
+ generator.state = "suspended";
10400
+ return generatorYield(value2, yieldNodeId);
10401
+ },
10402
+ scope
10403
+ });
10404
+ if (result.kind === "error") {
10405
+ throw result.error;
10406
+ }
10407
+ if (result.kind === "throw") {
10408
+ throw result.value;
10409
+ }
10410
+ const value = result.hasValue ? result.value : void 0;
10411
+ return node.async ? awaitSandboxValue(value, context.signal, context.budget, createCoercionContext(context)) : value;
10412
+ };
10413
+ return node.async ? runAsyncPrefix(execute) : execute();
10414
+ });
10415
+ const generator = createSandboxGenerator(channel, { async: node.async });
10416
+ if (prototype !== void 0) setSandboxPrototype(generator, prototype, context.budget);
10417
+ const origin = registerGeneratorOrigin(generator, node, scope, closureContext);
10418
+ return generator;
10419
+ } finally {
10420
+ releasePrototype?.();
10421
+ }
10376
10422
  }
10377
10423
  });
10424
+ if (prototypes2 !== void 0) {
10425
+ const prototype = /* @__PURE__ */ Object.create(null);
10426
+ markDescriptorObject(prototype);
10427
+ setSandboxPrototype(prototype, prototypes2.instancePrototype);
10428
+ Object.defineProperty(materializeFunctionProperties(closure), "prototype", { value: prototype, writable: true });
10429
+ setSandboxPrototype(closure, prototypes2.functionPrototype);
10430
+ }
10378
10431
  registerClosureOrigin(closure, node, context);
10379
10432
  return closure;
10380
10433
  }
@@ -13261,6 +13314,32 @@ function createArrayGlobal(budget) {
13261
13314
  length: 0,
13262
13315
  call: (_args, context) => context?.thisValue
13263
13316
  }), writable: true, configurable: true });
13317
+ const iteratorTagGetter = createSandboxClosure({
13318
+ guest: true,
13319
+ sandbox: true,
13320
+ name: "get [Symbol.toStringTag]",
13321
+ length: 0,
13322
+ call: () => "Iterator"
13323
+ });
13324
+ const iteratorTagSetter = createSandboxClosure({
13325
+ guest: true,
13326
+ sandbox: true,
13327
+ name: "set [Symbol.toStringTag]",
13328
+ length: 1,
13329
+ call: ([value], context) => {
13330
+ const receiver = context?.thisValue;
13331
+ if (receiver === iterablePrototype || receiver === null || typeof receiver !== "object")
13332
+ throw new TypeError("Iterator tag setter requires a distinct object receiver.");
13333
+ defineOwnDataProperty(objectProperties(receiver, true), Symbol.toStringTag, value);
13334
+ createDataCheckpoint(budget, context)(receiver, 0, true);
13335
+ return void 0;
13336
+ }
13337
+ });
13338
+ Object.defineProperty(iterablePrototype, Symbol.toStringTag, {
13339
+ get: accessorAdapter(iteratorTagGetter, "get"),
13340
+ set: accessorAdapter(iteratorTagSetter, "set"),
13341
+ configurable: true
13342
+ });
13264
13343
  setSandboxPrototype(iterablePrototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
13265
13344
  setSandboxPrototype(iteratorPrototype, iterablePrototype);
13266
13345
  Object.defineProperties(iteratorPrototype, {
@@ -13274,6 +13353,9 @@ function createArrayGlobal(budget) {
13274
13353
  [Symbol.toStringTag]: { value: "Array Iterator", configurable: true }
13275
13354
  });
13276
13355
  registerBuiltinIdentities(budget, { "%ArrayIteratorPrototype%": iteratorPrototype, "%IteratorPrototype%": iterablePrototype });
13356
+ registerIntrinsicFunction(budget, iterablePrototype[Symbol.iterator]);
13357
+ registerIntrinsicFunction(budget, iteratorTagGetter);
13358
+ registerIntrinsicFunction(budget, iteratorTagSetter);
13277
13359
  registerIntrinsicObject(budget, iterablePrototype);
13278
13360
  registerIntrinsicObject(budget, iteratorPrototype);
13279
13361
  for (const method of ["keys", "values", "entries"]) {
@@ -14272,16 +14354,19 @@ function getGeneratorMember(target, property, budget) {
14272
14354
  return createSandboxClosure({
14273
14355
  sandbox: true,
14274
14356
  name: property,
14275
- call: ([value], context) => {
14276
- const iterator = generatorIterator(target, budget, context);
14277
- const result = Promise.resolve(iterator[property](value)).then((result2) => allocateProducedSandboxValue(
14278
- { value: result2.value, done: result2.done === true },
14279
- budget
14280
- ));
14281
- return target.async ? createSandboxPromise(result) : result;
14282
- }
14357
+ call: ([value], context) => callGeneratorMethod(target, property, value, budget, context, target.async === true)
14283
14358
  });
14284
14359
  }
14360
+ function callGeneratorMethod(target, method, value, budget, context, async) {
14361
+ const result = (async () => {
14362
+ if (!isSandboxGenerator(target) || target.async === true !== async)
14363
+ throw new TypeError(`${async ? "AsyncGenerator" : "Generator"}.${method} requires a matching generator receiver.`);
14364
+ const iterator = generatorIterator(target, budget, context);
14365
+ const result2 = await iterator[method](value);
14366
+ return allocateProducedSandboxValue({ value: result2.value, done: result2.done === true }, budget);
14367
+ })();
14368
+ return async ? createSandboxPromise(result) : result;
14369
+ }
14285
14370
 
14286
14371
  // packages/safe-js/src/interp/methods/set.ts
14287
14372
  var setMethodNames = /* @__PURE__ */ new Set([
@@ -23968,6 +24053,66 @@ function createFunctionPrototype(budget) {
23968
24053
  installFunctionPrototype(budget, prototype);
23969
24054
  }
23970
24055
 
24056
+ // packages/safe-js/src/interp/globals/generator.ts
24057
+ function createGeneratorPrototypes(budget) {
24058
+ const asyncIteratorPrototype = /* @__PURE__ */ Object.create(null);
24059
+ const asyncIterator = createSandboxClosure({
24060
+ guest: true,
24061
+ sandbox: true,
24062
+ name: "[Symbol.asyncIterator]",
24063
+ length: 0,
24064
+ call: (_args, context) => context?.thisValue
24065
+ });
24066
+ Object.defineProperty(asyncIteratorPrototype, Symbol.asyncIterator, {
24067
+ value: asyncIterator,
24068
+ writable: true,
24069
+ configurable: true
24070
+ });
24071
+ setSandboxPrototype(asyncIteratorPrototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
24072
+ registerBuiltinIdentities(budget, { "%AsyncIteratorPrototype%": asyncIteratorPrototype });
24073
+ registerIntrinsicObject(budget, asyncIteratorPrototype);
24074
+ registerIntrinsicFunction(budget, asyncIterator);
24075
+ const state = /* @__PURE__ */ new Map();
24076
+ for (const async of [false, true]) {
24077
+ const tag = async ? "AsyncGenerator" : "Generator";
24078
+ const functionPrototype = /* @__PURE__ */ Object.create(null);
24079
+ const instancePrototype = /* @__PURE__ */ Object.create(null);
24080
+ setSandboxPrototype(functionPrototype, resolveIntrinsicIdentity(budget, JSON.stringify(["%FunctionPrototype%"])));
24081
+ setSandboxPrototype(instancePrototype, async ? asyncIteratorPrototype : resolveIntrinsicIdentity(budget, JSON.stringify(["%IteratorPrototype%"])));
24082
+ Object.defineProperties(functionPrototype, {
24083
+ prototype: { value: instancePrototype, configurable: true },
24084
+ // Do not expose native dynamic-source constructors through this graph.
24085
+ constructor: { value: void 0, configurable: true },
24086
+ [Symbol.toStringTag]: { value: `${tag}Function`, configurable: true }
24087
+ });
24088
+ Object.defineProperties(instancePrototype, {
24089
+ constructor: { value: functionPrototype, configurable: true },
24090
+ [Symbol.toStringTag]: { value: tag, configurable: true }
24091
+ });
24092
+ for (const method of ["next", "return", "throw"]) {
24093
+ Object.defineProperty(instancePrototype, method, {
24094
+ writable: true,
24095
+ configurable: true,
24096
+ value: createSandboxClosure({
24097
+ guest: true,
24098
+ sandbox: true,
24099
+ name: method,
24100
+ length: 1,
24101
+ call: ([value], context) => callGeneratorMethod(context?.thisValue, method, value, budget, context, async)
24102
+ })
24103
+ });
24104
+ }
24105
+ registerBuiltinIdentities(budget, {
24106
+ [`%${tag}Prototype%`]: instancePrototype,
24107
+ [`%${tag}FunctionPrototype%`]: functionPrototype
24108
+ });
24109
+ registerIntrinsicObject(budget, functionPrototype);
24110
+ registerIntrinsicObject(budget, instancePrototype);
24111
+ state.set(async, { functionPrototype, instancePrototype });
24112
+ }
24113
+ generatorPrototypes.set(budget, state);
24114
+ }
24115
+
23971
24116
  // packages/safe-js/src/interp/globals/symbol.ts
23972
24117
  var registries = /* @__PURE__ */ new WeakMap();
23973
24118
  function createSymbolGlobal(budget) {
@@ -24199,6 +24344,7 @@ function createBuiltinBindings(options) {
24199
24344
  ...createRegexGlobals(options)
24200
24345
  };
24201
24346
  createFunctionPrototype(options.budget);
24347
+ createGeneratorPrototypes(options.budget);
24202
24348
  registerBuiltinIdentities(options.budget, bindings);
24203
24349
  return bindings;
24204
24350
  }
@@ -41049,4 +41195,4 @@ export {
41049
41195
  FileSnapshotBackend,
41050
41196
  run
41051
41197
  };
41052
- //# sourceMappingURL=chunk-M4L3EZHL.js.map
41198
+ //# sourceMappingURL=chunk-T2TQBEDG.js.map