@poe-platform/safe-js 0.1.152 → 0.1.154

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.
@@ -3355,15 +3355,10 @@ var Parser = class {
3355
3355
  const asyncToken = this.consumeKeyword("async");
3356
3356
  const functionToken = this.expectKeyword("function");
3357
3357
  const generatorToken = this.consumePunctuator("*");
3358
- if (asyncToken !== void 0 && generatorToken !== void 0) {
3359
- throw new Error(
3360
- `async function* is not supported at line ${asyncToken.start.line}, column ${asyncToken.start.column}.`
3361
- );
3362
- }
3363
3358
  const id = defaultExport && this.currentToken().value === "(" ? void 0 : this.parseBindingIdentifier();
3364
3359
  if (id !== void 0) this.declareBinding(id, defaultExport ? "lexical" : "function");
3365
3360
  const generator = generatorToken !== void 0;
3366
- const { params, body } = this.parseFunctionParts(generator, ordinaryFunctionContext);
3361
+ const { params, body } = this.parseFunctionParts(generator, ordinaryFunctionContext, void 0, asyncToken !== void 0);
3367
3362
  return this.withFunctionSource({
3368
3363
  type: "FunctionDeclaration",
3369
3364
  async: asyncToken !== void 0,
@@ -3409,7 +3404,7 @@ var Parser = class {
3409
3404
  });
3410
3405
  });
3411
3406
  }
3412
- parseFunctionParts(generator, lexicalContext, accessor) {
3407
+ parseFunctionParts(generator, lexicalContext, accessor, async = false) {
3413
3408
  return this.withLexicalContext(lexicalContext, () => {
3414
3409
  const params = this.withScope(() => {
3415
3410
  const parsed = this.parseArrowParameters();
@@ -3422,7 +3417,7 @@ var Parser = class {
3422
3417
  if (accessor === "set" && (params.length !== 1 || params[0]?.type === "RestElement"))
3423
3418
  throw new Error("A setter must have exactly one non-rest parameter.");
3424
3419
  const bodyTokenIndex = this.index;
3425
- const body = this.withFunctionContext(generator ? "generator" : "normal", () => this.parseBlockStatement(params));
3420
+ const body = this.withFunctionContext(generator ? async ? "async-generator" : "generator" : "normal", () => this.parseBlockStatement(params));
3426
3421
  if (accessor === "set" && params[0]?.type !== "Identifier") {
3427
3422
  let directiveTokenIndex = bodyTokenIndex + 1;
3428
3423
  for (const statement of body.body) {
@@ -3464,7 +3459,6 @@ var Parser = class {
3464
3459
  async = true;
3465
3460
  }
3466
3461
  const generator = this.consumePunctuator("*") !== void 0;
3467
- if (async && generator) throw new DisallowedSyntaxError("async generator", methodStart.start);
3468
3462
  let accessor;
3469
3463
  const modifier = this.currentToken();
3470
3464
  if ((modifier.value === "get" || modifier.value === "set") && this.isObjectMethodStart()) {
@@ -3489,7 +3483,7 @@ var Parser = class {
3489
3483
  return: true,
3490
3484
  await: async,
3491
3485
  strictAwait: true
3492
- }, accessor);
3486
+ }, accessor, async);
3493
3487
  const value2 = this.withFunctionSource({
3494
3488
  type: "FunctionExpression",
3495
3489
  async,
@@ -4130,7 +4124,7 @@ var Parser = class {
4130
4124
  };
4131
4125
  }
4132
4126
  if (token.type === "keyword" && token.value === "yield") {
4133
- if (this.functionContext !== "generator") {
4127
+ if (this.functionContext !== "generator" && this.functionContext !== "async-generator") {
4134
4128
  throw new Error(
4135
4129
  `yield is only valid inside a generator body at line ${token.start.line}, column ${token.start.column}.`
4136
4130
  );
@@ -4510,14 +4504,9 @@ var Parser = class {
4510
4504
  const asyncToken = this.consumeKeyword("async");
4511
4505
  const functionToken = this.expectKeyword("function");
4512
4506
  const generatorToken = this.consumePunctuator("*");
4513
- if (asyncToken !== void 0 && generatorToken !== void 0) {
4514
- throw new Error(
4515
- `async function* is not supported at line ${asyncToken.start.line}, column ${asyncToken.start.column}.`
4516
- );
4517
- }
4518
4507
  const id = isIdentifierLikeToken(this.currentToken()) ? this.parseBindingIdentifier() : void 0;
4519
4508
  const generator = generatorToken !== void 0;
4520
- const { params, body } = this.parseFunctionParts(generator, ordinaryFunctionContext);
4509
+ const { params, body } = this.parseFunctionParts(generator, ordinaryFunctionContext, void 0, asyncToken !== void 0);
4521
4510
  return this.withFunctionSource({
4522
4511
  type: "FunctionExpression",
4523
4512
  async: asyncToken !== void 0,
@@ -4620,10 +4609,10 @@ var Parser = class {
4620
4609
  }
4621
4610
  parseObjectProperty() {
4622
4611
  const generatorToken = this.currentToken().type === "punctuator" && this.currentToken().value === "*" ? this.currentToken() : this.currentToken().type === "keyword" && this.currentToken().value === "async" && this.peekToken(1).type === "punctuator" && this.peekToken(1).value === "*" ? this.peekToken(1) : void 0;
4623
- if (generatorToken !== void 0 && this.currentToken().value !== "*") {
4624
- throw new Error(
4625
- `Generator shorthand methods are not supported at line ${generatorToken.start.line}, column ${generatorToken.start.column}.`
4626
- );
4612
+ const asyncGeneratorToken = generatorToken !== void 0 && this.currentToken().value === "async" ? this.currentToken() : void 0;
4613
+ if (asyncGeneratorToken !== void 0) {
4614
+ if (hasLineBreakBetween(asyncGeneratorToken, generatorToken)) throw unexpectedTokenError(generatorToken);
4615
+ this.index++;
4627
4616
  }
4628
4617
  if (generatorToken !== void 0) this.index++;
4629
4618
  let accessor;
@@ -4637,8 +4626,8 @@ var Parser = class {
4637
4626
  this.index++;
4638
4627
  }
4639
4628
  const modifierToken = this.currentToken();
4640
- const asyncToken = generatorToken === void 0 && modifierToken.type === "keyword" && modifierToken.value === "async" && modifierToken.end.offset - modifierToken.start.offset === modifierToken.value.length && this.isObjectMethodStart() && !hasLineBreakBetween(modifierToken, this.peekToken(1)) ? modifierToken : void 0;
4641
- if (asyncToken !== void 0) {
4629
+ const asyncToken = asyncGeneratorToken ?? (generatorToken === void 0 && modifierToken.type === "keyword" && modifierToken.value === "async" && modifierToken.end.offset - modifierToken.start.offset === modifierToken.value.length && this.isObjectMethodStart() && !hasLineBreakBetween(modifierToken, this.peekToken(1)) ? modifierToken : void 0);
4630
+ if (asyncToken !== void 0 && asyncGeneratorToken === void 0) {
4642
4631
  this.index += 1;
4643
4632
  }
4644
4633
  if (this.consumePunctuator("[") !== void 0) {
@@ -4766,7 +4755,7 @@ var Parser = class {
4766
4755
  ...ordinaryFunctionContext,
4767
4756
  superProperty: true,
4768
4757
  ...accessor === void 0 ? {} : { await: false, strictAwait: true }
4769
- }, accessor);
4758
+ }, accessor, asyncToken !== void 0);
4770
4759
  return this.withFunctionSource({
4771
4760
  type: "FunctionExpression",
4772
4761
  async: asyncToken !== void 0,
@@ -8096,6 +8085,9 @@ function validateTaggedValue(record2, path, state) {
8096
8085
  }
8097
8086
  }
8098
8087
  function validateGeneratorShape(record2, path, state) {
8088
+ if (record2.async !== void 0 && typeof record2.async !== "boolean") {
8089
+ fail("invalidType", `${path}.async`, "generator async flag must be a boolean");
8090
+ }
8099
8091
  if (!["start", "suspended", "done"].includes(String(record2.state))) {
8100
8092
  fail("invalidState", `${path}.state`, "unknown generator state");
8101
8093
  }
@@ -9956,7 +9948,7 @@ function getSandboxIterator(value, budget, context) {
9956
9948
  return syncIterator(Float32Array.prototype.values.call(value));
9957
9949
  }
9958
9950
  if (isSandboxGenerator(value)) {
9959
- return generatorIterator(value);
9951
+ return value.async ? void 0 : generatorIterator(value);
9960
9952
  }
9961
9953
  if (typeof value === "string") {
9962
9954
  return syncIterator(value[Symbol.iterator]());
@@ -10011,11 +10003,17 @@ function collectionIterator(collection) {
10011
10003
  }
10012
10004
  };
10013
10005
  }
10014
- function generatorIterator(generator) {
10006
+ var asyncGeneratorRequests = /* @__PURE__ */ new WeakMap();
10007
+ function generatorIterator(generator, budget) {
10015
10008
  const invoke = async (method, value) => {
10016
10009
  const leaveRunning = enterRunningState(generator);
10010
+ const initialState = generator.state;
10017
10011
  generator.state = "running";
10018
10012
  try {
10013
+ if (generator.async && method === "return" && (initialState === "start" || initialState === "done")) {
10014
+ if (initialState === "start") await generator.channel.return();
10015
+ value = await awaitSandboxValue(value, void 0, budget);
10016
+ }
10019
10017
  const result = await generator.channel[method](value);
10020
10018
  generator.state = result.done ? "done" : "suspended";
10021
10019
  return result;
@@ -10026,11 +10024,18 @@ function generatorIterator(generator) {
10026
10024
  leaveRunning();
10027
10025
  }
10028
10026
  };
10027
+ const request = (method, value) => {
10028
+ if (!generator.async) return invoke(method, value);
10029
+ const previous = asyncGeneratorRequests.get(generator) ?? Promise.resolve();
10030
+ const result = previous.then(() => invoke(method, value));
10031
+ asyncGeneratorRequests.set(generator, result.catch(() => void 0));
10032
+ return result;
10033
+ };
10029
10034
  return {
10030
10035
  generator: true,
10031
- next: (value) => invoke("next", value),
10032
- return: (value) => invoke("return", value),
10033
- throw: (error) => invoke("throw", error)
10036
+ next: (value) => request("next", value),
10037
+ return: (value) => request("return", value),
10038
+ throw: (error) => request("throw", error)
10034
10039
  };
10035
10040
  }
10036
10041
  function syncIterator(iterator) {
@@ -13496,9 +13501,6 @@ var AS001Scanner = class {
13496
13501
  previousToken = token;
13497
13502
  continue;
13498
13503
  }
13499
- if (token.type === "punctuator" && token.value === "*" && previousToken?.value === "async" && isGeneratorMemberToken(previousToken, previousPreviousToken, braceContextStack)) {
13500
- this.report("generator", token.start, token.end);
13501
- }
13502
13504
  lastClosedControlParenthesis = token.type === "punctuator" ? updateGroupingState2(groupingStack, previousToken, token.value) : false;
13503
13505
  canStartStatement = updateStatementStart(
13504
13506
  token,
@@ -13842,19 +13844,6 @@ function isMemberNameToken(token, previousToken, previousPreviousToken, braceCon
13842
13844
  }
13843
13845
  return previousToken?.type === "punctuator" && previousToken.value === "*";
13844
13846
  }
13845
- function isGeneratorMemberToken(previousToken, previousPreviousToken, braceContextStack) {
13846
- const memberContext = braceContextStack.at(-1);
13847
- if (memberContext === void 0) {
13848
- return false;
13849
- }
13850
- if (memberContext.kind !== "class" && memberContext.kind !== "object") {
13851
- return false;
13852
- }
13853
- if (isMemberEntryStart(previousToken, memberContext.kind)) {
13854
- return true;
13855
- }
13856
- return isMemberModifierToken(previousToken) && isMemberEntryStart(previousPreviousToken, memberContext.kind);
13857
- }
13858
13847
  function isMemberEntryStart(token, kind) {
13859
13848
  if (token?.type !== "punctuator") {
13860
13849
  return false;
@@ -17645,7 +17634,7 @@ var ASAsyncNotNeededScanner = class {
17645
17634
  this.visitExpression(node.body);
17646
17635
  }
17647
17636
  visitFunctionExpression(node, isDefaultExport = false) {
17648
- if (node.async && !isDefaultExport && !bodyContainsAwait(node.body)) {
17637
+ if (node.async && !node.generator && !isDefaultExport && !bodyContainsAwait(node.body)) {
17649
17638
  this.report(createAsyncKeywordSpan(node.span));
17650
17639
  }
17651
17640
  for (const param of node.params) {
@@ -28011,13 +28000,13 @@ function getGeneratorMember(target, property, budget) {
28011
28000
  return createSandboxClosure({
28012
28001
  sandbox: true,
28013
28002
  name: property,
28014
- call: async ([value]) => {
28015
- const iterator = getSandboxIterator(target);
28016
- const result = await iterator[property](value);
28017
- return allocateProducedSandboxValue(
28018
- { value: result.value, done: result.done === true },
28003
+ call: ([value]) => {
28004
+ const iterator = generatorIterator(target, budget);
28005
+ const result = Promise.resolve(iterator[property](value)).then((result2) => allocateProducedSandboxValue(
28006
+ { value: result2.value, done: result2.done === true },
28019
28007
  budget
28020
- );
28008
+ ));
28009
+ return target.async ? createSandboxPromise(result) : result;
28021
28010
  }
28022
28011
  });
28023
28012
  }
@@ -28775,6 +28764,7 @@ async function interpret(node, options = {}) {
28775
28764
  ),
28776
28765
  generatorResume: options.generatorResume,
28777
28766
  generatorYield: options.generatorYield,
28767
+ asyncGenerator: options.asyncGenerator,
28778
28768
  resumeTarget: { nodeId: options.snapshot?.resumeNodeId }
28779
28769
  };
28780
28770
  const evaluation = await withCancellationSignal(
@@ -30187,11 +30177,8 @@ function hasLoopLabel(labels, target) {
30187
30177
  return Array.isArray(labels) ? labels.includes(target) : labels === target;
30188
30178
  }
30189
30179
  async function bindForOfLoopVariable(left, value, scope, context) {
30190
- if (left.type === "Identifier") {
30191
- return bindPattern2(left, value, { assign: true }, scope, createPatternContext(context, scope));
30192
- }
30193
30180
  if (left.type !== "VariableDeclaration") {
30194
- throw new TypeError(`Unsupported for...of left-hand side '${left.type}'.`);
30181
+ return bindPattern2(left, value, { assign: true }, scope, createPatternContext(context, scope));
30195
30182
  }
30196
30183
  const [declarator] = left.declarations;
30197
30184
  if (left.declarations.length !== 1 || declarator === void 0) {
@@ -30235,7 +30222,7 @@ async function evaluateReturnStatement(node, context) {
30235
30222
  return {
30236
30223
  kind: "return",
30237
30224
  hasValue: argument.hasValue,
30238
- value: argument.value
30225
+ value: context.asyncGenerator ? await suspendJob(awaitSandboxValue(argument.value, context.signal, context.budget)) : argument.value
30239
30226
  };
30240
30227
  }
30241
30228
  async function evaluateYieldExpression(node, context) {
@@ -30252,25 +30239,35 @@ async function evaluateYieldExpression(node, context) {
30252
30239
  if (argument.kind !== "normal") {
30253
30240
  return argument;
30254
30241
  }
30255
- const completionPromise = context.generatorYield(
30256
- allocateProducedSandboxValue(argument.value, context.budget),
30257
- node.nodeId
30258
- );
30242
+ const completion = await yieldGeneratorValue(argument.value, node, context);
30243
+ context.generatorResume = void 0;
30244
+ return generatorCompletionResult(completion);
30245
+ }
30246
+ async function yieldGeneratorValue(value, node, context) {
30247
+ if (context.asyncGenerator) value = await suspendJob(awaitSandboxValue(value, context.signal, context.budget));
30248
+ const completionPromise = context.generatorYield(allocateProducedSandboxValue(value, context.budget), node.nodeId);
30259
30249
  emitResumeBreakpoint(context, {
30260
30250
  kind: "generator-yield",
30261
30251
  nodeId: node.nodeId,
30262
30252
  span: node.span
30263
30253
  });
30264
- const completion = await completionPromise;
30265
- context.generatorResume = void 0;
30266
- return generatorCompletionResult(completion);
30254
+ const completion = await (context.asyncGenerator ? suspendJob(completionPromise) : completionPromise);
30255
+ if (context.asyncGenerator && completion.type === "return") {
30256
+ try {
30257
+ return { type: "return", value: await suspendJob(awaitSandboxValue(completion.value, context.signal, context.budget)) };
30258
+ } catch (error) {
30259
+ return { type: "throw", value: error };
30260
+ }
30261
+ }
30262
+ return completion;
30267
30263
  }
30268
30264
  async function evaluateYieldDelegate(node, context) {
30269
30265
  const argument = await evaluateNode(node.argument, context);
30270
30266
  if (argument.kind !== "normal") {
30271
30267
  return argument;
30272
30268
  }
30273
- const iterator = getSandboxIterator(
30269
+ const asyncDelegate = context.asyncGenerator && isSandboxGenerator(argument.value) && argument.value.async ? argument.value : void 0;
30270
+ const iterator = asyncDelegate ? generatorIterator(asyncDelegate, context.budget) : getSandboxIterator(
30274
30271
  argument.value,
30275
30272
  context.budget,
30276
30273
  createCoercionContext(context)
@@ -30294,19 +30291,35 @@ async function evaluateYieldDelegate(node, context) {
30294
30291
  const iteratorMethod = iterator[method];
30295
30292
  if (iteratorMethod === void 0) {
30296
30293
  if (completion.type === "throw") {
30297
- throw completion.value;
30294
+ await closeIterator(iterator);
30295
+ throw new TypeError("Delegated iterator does not provide a throw method.");
30298
30296
  }
30299
30297
  return generatorCompletionResult(completion);
30300
30298
  }
30301
- const result = await iteratorMethod(completion.value);
30302
- if (result.done) {
30299
+ const pendingResult = Promise.resolve(iteratorMethod(completion.value));
30300
+ const result = await (context.asyncGenerator ? suspendJob(pendingResult) : pendingResult);
30301
+ if (typeof result !== "object" && typeof result !== "function" || result === null) {
30302
+ throw new TypeError("Iterator result must be an object.");
30303
+ }
30304
+ const done = result.done;
30305
+ let value = result.value;
30306
+ if (context.asyncGenerator) {
30307
+ try {
30308
+ value = await suspendJob(awaitSandboxValue(value, context.signal, context.budget));
30309
+ } catch (error) {
30310
+ if (isFatalSandboxError(error) || error instanceof HostCallResumabilityError) throw error;
30311
+ if (!asyncDelegate && !done && method !== "return") await closeIterator(iterator, true);
30312
+ throw error;
30313
+ }
30314
+ }
30315
+ if (done) {
30303
30316
  if (completion.type === "return") {
30304
- return generatorCompletionResult({ type: "return", value: result.value });
30317
+ return generatorCompletionResult({ type: "return", value });
30305
30318
  }
30306
30319
  return {
30307
30320
  kind: "normal",
30308
30321
  hasValue: true,
30309
- value: result.value
30322
+ value
30310
30323
  };
30311
30324
  }
30312
30325
  if (replayIndex < replay.length - 1) {
@@ -30314,16 +30327,7 @@ async function evaluateYieldDelegate(node, context) {
30314
30327
  replayIndex += 1;
30315
30328
  continue;
30316
30329
  }
30317
- const completionPromise = context.generatorYield(
30318
- allocateProducedSandboxValue(result.value, context.budget),
30319
- node.nodeId
30320
- );
30321
- emitResumeBreakpoint(context, {
30322
- kind: "generator-yield",
30323
- nodeId: node.nodeId,
30324
- span: node.span
30325
- });
30326
- completion = await completionPromise;
30330
+ completion = await yieldGeneratorValue(value, node, context);
30327
30331
  context.generatorResume = void 0;
30328
30332
  }
30329
30333
  } finally {
@@ -31795,25 +31799,30 @@ function createGeneratorClosure(node, context, evaluateNode2) {
31795
31799
  closureContext,
31796
31800
  evaluateNode2
31797
31801
  );
31798
- const channel = createGeneratorChannel(async (generatorYield) => {
31799
- const result = await evaluateNode2(node.body, {
31800
- ...closureContext,
31801
- functionBody: node.body,
31802
- generatorYield: (value, yieldNodeId) => {
31803
- generator.state = "suspended";
31804
- return generatorYield(value, yieldNodeId);
31805
- },
31806
- scope
31807
- });
31808
- if (result.kind === "error") {
31809
- throw result.error;
31810
- }
31811
- if (result.kind === "throw") {
31812
- throw result.value;
31813
- }
31814
- return result.hasValue ? result.value : void 0;
31802
+ const channel = createGeneratorChannel((generatorYield) => {
31803
+ const execute = async () => {
31804
+ const result = await evaluateNode2(node.body, {
31805
+ ...closureContext,
31806
+ functionBody: node.body,
31807
+ asyncGenerator: node.async,
31808
+ generatorYield: (value2, yieldNodeId) => {
31809
+ generator.state = "suspended";
31810
+ return generatorYield(value2, yieldNodeId);
31811
+ },
31812
+ scope
31813
+ });
31814
+ if (result.kind === "error") {
31815
+ throw result.error;
31816
+ }
31817
+ if (result.kind === "throw") {
31818
+ throw result.value;
31819
+ }
31820
+ const value = result.hasValue ? result.value : void 0;
31821
+ return node.async ? awaitSandboxValue(value, context.signal, context.budget) : value;
31822
+ };
31823
+ return node.async ? runAsyncPrefix(execute) : execute();
31815
31824
  });
31816
- const generator = createSandboxGenerator(channel);
31825
+ const generator = createSandboxGenerator(channel, { async: node.async });
31817
31826
  return generator;
31818
31827
  }
31819
31828
  });
@@ -31855,6 +31864,7 @@ async function executeClosure(node, args, thisValue, context, evaluateNode2) {
31855
31864
  const scope = await createClosureScope(node, args, thisValue, context, evaluateNode2);
31856
31865
  const result = await evaluateNode2(node.body, {
31857
31866
  ...context,
31867
+ asyncGenerator: false,
31858
31868
  functionBody: node.body.type === "BlockStatement" ? node.body : void 0,
31859
31869
  scope
31860
31870
  });
@@ -35600,4 +35610,4 @@ export {
35600
35610
  FileSnapshotBackend,
35601
35611
  run
35602
35612
  };
35603
- //# sourceMappingURL=chunk-6NQIMBX3.js.map
35613
+ //# sourceMappingURL=chunk-KHKBLB42.js.map