@poe-platform/safe-js 0.1.154 → 0.1.156

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.
@@ -2547,7 +2547,7 @@ var ordinaryFunctionContext = {
2547
2547
  await: true
2548
2548
  };
2549
2549
  var Parser = class {
2550
- constructor(tokens, source, compilation, functionContext = "normal", lexicalContext = { ...ordinaryFunctionContext, newTarget: false }) {
2550
+ constructor(tokens, source, compilation, functionContext = "top-level", lexicalContext = { ...ordinaryFunctionContext, newTarget: false }) {
2551
2551
  this.tokens = tokens;
2552
2552
  this.source = source;
2553
2553
  this.compilation = compilation;
@@ -2720,7 +2720,7 @@ var Parser = class {
2720
2720
  ...this.lexicalContext,
2721
2721
  return: true,
2722
2722
  await: isAsync || this.lexicalContext.strictAwait !== true
2723
- }, () => this.parseArrowFunctionBody(params));
2723
+ }, () => this.parseArrowFunctionBody(params, isAsync));
2724
2724
  return this.withFunctionSource({
2725
2725
  type: "ArrowFunctionExpression",
2726
2726
  async: isAsync,
@@ -2760,11 +2760,11 @@ var Parser = class {
2760
2760
  this.conditionalExpressionDepth -= 1;
2761
2761
  }
2762
2762
  }
2763
- parseArrowFunctionBody(params) {
2763
+ parseArrowFunctionBody(params, async) {
2764
2764
  if (this.currentToken().type === "punctuator" && this.currentToken().value === "{") {
2765
- return this.withFunctionContext("normal", () => this.parseBlockStatement(params));
2765
+ return this.withFunctionContext(async ? "async" : "normal", () => this.parseBlockStatement(params));
2766
2766
  }
2767
- return this.withFunctionContext("normal", () => this.parseExpression().node);
2767
+ return this.withFunctionContext(async ? "async" : "normal", () => this.parseExpression().node);
2768
2768
  }
2769
2769
  parseBlockStatement(params, catchParam) {
2770
2770
  const start = this.expectPunctuator("{");
@@ -3039,9 +3039,16 @@ var Parser = class {
3039
3039
  }
3040
3040
  parseForStatement(labels) {
3041
3041
  const forToken = this.expectKeyword("for");
3042
+ const awaitToken = this.consumeKeyword("await");
3043
+ if (awaitToken !== void 0 && this.functionContext !== "top-level" && this.functionContext !== "async" && this.functionContext !== "async-generator") {
3044
+ throw new Error("for await is only valid at top level or inside an async function.");
3045
+ }
3042
3046
  return this.withScope(() => {
3043
3047
  this.expectPunctuator("(");
3044
3048
  const iterationOperator = this.findTopLevelForIterationOperator(this.index);
3049
+ if (awaitToken !== void 0 && iterationOperator?.value !== "of") {
3050
+ throw new Error("for await requires an of loop.");
3051
+ }
3045
3052
  if (iterationOperator?.value === "in") {
3046
3053
  const left = this.parseForInLeft();
3047
3054
  this.expectKeyword("in");
@@ -3065,6 +3072,7 @@ var Parser = class {
3065
3072
  const body2 = this.withLoopContext(() => this.parseStatement());
3066
3073
  return {
3067
3074
  type: "ForOfStatement",
3075
+ ...awaitToken === void 0 ? {} : { await: true },
3068
3076
  left,
3069
3077
  right,
3070
3078
  body: body2,
@@ -3417,7 +3425,7 @@ var Parser = class {
3417
3425
  if (accessor === "set" && (params.length !== 1 || params[0]?.type === "RestElement"))
3418
3426
  throw new Error("A setter must have exactly one non-rest parameter.");
3419
3427
  const bodyTokenIndex = this.index;
3420
- const body = this.withFunctionContext(generator ? async ? "async-generator" : "generator" : "normal", () => this.parseBlockStatement(params));
3428
+ const body = this.withFunctionContext(generator ? async ? "async-generator" : "generator" : async ? "async" : "normal", () => this.parseBlockStatement(params));
3421
3429
  if (accessor === "set" && params[0]?.type !== "Identifier") {
3422
3430
  let directiveTokenIndex = bodyTokenIndex + 1;
3423
3431
  for (const statement of body.body) {
@@ -9920,7 +9928,193 @@ function assertSnapshotInactive(snapshot) {
9920
9928
  }
9921
9929
  }
9922
9930
 
9931
+ // packages/safe-js/src/interp/jobs.ts
9932
+ import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
9933
+ var activeJob = new AsyncLocalStorage4();
9934
+ var SandboxJobQueue = class {
9935
+ running = false;
9936
+ pending = [];
9937
+ ready = [];
9938
+ idle = [];
9939
+ generation = 0;
9940
+ acquire(job) {
9941
+ return new Promise((resolve) => {
9942
+ this.pending.push(() => {
9943
+ this.running = true;
9944
+ this.generation += 1;
9945
+ job.ownsExecution = true;
9946
+ resolve();
9947
+ });
9948
+ this.advance();
9949
+ });
9950
+ }
9951
+ release(job) {
9952
+ job.prefixParent = void 0;
9953
+ if (!job.ownsExecution) return;
9954
+ job.ownsExecution = false;
9955
+ this.running = false;
9956
+ this.advance();
9957
+ }
9958
+ async run(task) {
9959
+ const job = { queue: this, ownsExecution: false };
9960
+ await this.acquire(job);
9961
+ return activeJob.run(job, async () => {
9962
+ try {
9963
+ return await task();
9964
+ } finally {
9965
+ this.release(job);
9966
+ }
9967
+ });
9968
+ }
9969
+ async drain() {
9970
+ let idleTurns = 0;
9971
+ while (idleTurns < 20) {
9972
+ const generation = this.generation;
9973
+ if (this.running) await new Promise((resolve) => this.idle.push(resolve));
9974
+ await Promise.resolve();
9975
+ idleTurns = generation === this.generation ? idleTurns + 1 : 0;
9976
+ }
9977
+ }
9978
+ advance() {
9979
+ if (this.running) return;
9980
+ if (this.ready.length === 0 && this.pending.length > 0) {
9981
+ const empty = this.ready;
9982
+ this.ready = this.pending.reverse();
9983
+ this.pending = empty;
9984
+ }
9985
+ const next = this.ready.pop();
9986
+ if (next !== void 0) {
9987
+ next();
9988
+ } else {
9989
+ for (const resolve of this.idle.splice(0)) resolve();
9990
+ }
9991
+ }
9992
+ };
9993
+ function runPromiseJob(task) {
9994
+ const job = activeJob.getStore();
9995
+ return job === void 0 ? Promise.resolve().then(task) : job.queue.run(task);
9996
+ }
9997
+ function runAsyncPrefix(task) {
9998
+ const parent = activeJob.getStore();
9999
+ if (parent === void 0) return task();
10000
+ let owner = parent;
10001
+ while (owner !== void 0 && !owner.ownsExecution) owner = owner.prefixParent;
10002
+ if (owner === void 0) return parent.queue.run(task);
10003
+ const job = { queue: parent.queue, ownsExecution: false, prefixParent: parent };
10004
+ return activeJob.run(job, async () => {
10005
+ try {
10006
+ return await task();
10007
+ } finally {
10008
+ job.queue.release(job);
10009
+ }
10010
+ });
10011
+ }
10012
+ async function suspendJob(pending) {
10013
+ const job = activeJob.getStore();
10014
+ if (job === void 0) return pending;
10015
+ job.queue.release(job);
10016
+ try {
10017
+ return await pending;
10018
+ } finally {
10019
+ await job.queue.acquire(job);
10020
+ }
10021
+ }
10022
+
9923
10023
  // packages/safe-js/src/interp/iteration.ts
10024
+ function getSandboxAsyncIterator(value, budget, context, signal) {
10025
+ if (isSandboxGenerator(value) && value.async) {
10026
+ return { ...generatorIterator(value, budget), asyncProtocol: true };
10027
+ }
10028
+ if (value !== null && (typeof value === "object" || typeof value === "function") && !isGuestHostObject(value)) {
10029
+ const method = value[Symbol.asyncIterator];
10030
+ if (method !== void 0 && method !== null) {
10031
+ if (typeof method !== "function") throw new TypeError("Async iterator method must be callable.");
10032
+ const iterator2 = Reflect.apply(method, value, []);
10033
+ if (typeof iterator2 !== "object" && typeof iterator2 !== "function" || iterator2 === null) {
10034
+ throw new TypeError("Async iterator must be an object.");
10035
+ }
10036
+ const next = iterator2.next;
10037
+ const invoke2 = async (operation, args) => {
10038
+ if (typeof operation !== "function") throw new TypeError("Async iterator operation must be callable.");
10039
+ const pending = Promise.resolve(Reflect.apply(operation, iterator2, args)).then((result2) => ({ result: result2 }));
10040
+ const { result } = await awaitWithSignal(pending, signal);
10041
+ if (typeof result !== "object" && typeof result !== "function" || result === null) {
10042
+ throw new TypeError("Iterator result must be an object.");
10043
+ }
10044
+ return {
10045
+ get done() {
10046
+ return result.done;
10047
+ },
10048
+ get value() {
10049
+ return result.value;
10050
+ }
10051
+ };
10052
+ };
10053
+ return {
10054
+ asyncProtocol: true,
10055
+ retainedValue: value,
10056
+ next: (...args) => invoke2(next, args),
10057
+ get return() {
10058
+ const operation = iterator2.return;
10059
+ return operation === void 0 || operation === null ? void 0 : (...args) => invoke2(operation, args);
10060
+ },
10061
+ get throw() {
10062
+ const operation = iterator2.throw;
10063
+ return operation === void 0 || operation === null ? void 0 : (...args) => invoke2(operation, args);
10064
+ }
10065
+ };
10066
+ }
10067
+ }
10068
+ const iterator = getSandboxIterator(value, budget, context);
10069
+ if (iterator === void 0) return void 0;
10070
+ const invoke = async (method, args) => {
10071
+ const operation = iterator[method];
10072
+ if (operation === void 0) {
10073
+ if (method === "throw") {
10074
+ await closeIterator(iterator);
10075
+ throw new TypeError("Delegated iterator does not provide a throw method.");
10076
+ }
10077
+ return { done: true, value: args[0] };
10078
+ }
10079
+ const returned = operation(...args);
10080
+ const result = iterator.generator || iterator.asynchronous ? await returned : returned;
10081
+ if (typeof result !== "object" && typeof result !== "function" || result === null) {
10082
+ throw new TypeError("Iterator result must be an object.");
10083
+ }
10084
+ const done = Boolean(result.done);
10085
+ const resultValue = result.value;
10086
+ try {
10087
+ return { done, value: await awaitSandboxValue(resultValue, signal, budget) };
10088
+ } catch (error) {
10089
+ if (isFatalSandboxError(error) || error instanceof HostCallResumabilityError) throw error;
10090
+ if (!done && method !== "return") await closeIterator(iterator, true);
10091
+ throw error;
10092
+ }
10093
+ };
10094
+ return {
10095
+ asyncProtocol: true,
10096
+ snapshotIndex: iterator.snapshotIndex,
10097
+ get retainedValue() {
10098
+ return iterator.retainedValue;
10099
+ },
10100
+ next: (...args) => invoke("next", args),
10101
+ return: (...args) => invoke("return", args),
10102
+ throw: (...args) => invoke("throw", args)
10103
+ };
10104
+ }
10105
+ async function closeIterator(iterator, preserveThrow = false) {
10106
+ try {
10107
+ const close = iterator.return;
10108
+ if (close === void 0) return;
10109
+ const returned = close();
10110
+ const result = iterator.asyncProtocol ? await suspendJob(Promise.resolve(returned)) : iterator.generator || iterator.asynchronous ? await returned : returned;
10111
+ if (typeof result !== "object" && typeof result !== "function" || result === null) {
10112
+ throw new TypeError("Iterator return result must be an object.");
10113
+ }
10114
+ } catch (error) {
10115
+ if (!preserveThrow || isFatalSandboxError(error) || error instanceof HostCallResumabilityError) throw error;
10116
+ }
10117
+ }
9924
10118
  function getSandboxIterator(value, budget, context) {
9925
10119
  if (isSandboxBox(value) && typeof boxedValue(value) === "string") {
9926
10120
  const primitive = boxedValue(value);
@@ -10065,98 +10259,6 @@ function syncIterator(iterator) {
10065
10259
  };
10066
10260
  }
10067
10261
 
10068
- // packages/safe-js/src/interp/jobs.ts
10069
- import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
10070
- var activeJob = new AsyncLocalStorage4();
10071
- var SandboxJobQueue = class {
10072
- running = false;
10073
- pending = [];
10074
- ready = [];
10075
- idle = [];
10076
- generation = 0;
10077
- acquire(job) {
10078
- return new Promise((resolve) => {
10079
- this.pending.push(() => {
10080
- this.running = true;
10081
- this.generation += 1;
10082
- job.ownsExecution = true;
10083
- resolve();
10084
- });
10085
- this.advance();
10086
- });
10087
- }
10088
- release(job) {
10089
- job.prefixParent = void 0;
10090
- if (!job.ownsExecution) return;
10091
- job.ownsExecution = false;
10092
- this.running = false;
10093
- this.advance();
10094
- }
10095
- async run(task) {
10096
- const job = { queue: this, ownsExecution: false };
10097
- await this.acquire(job);
10098
- return activeJob.run(job, async () => {
10099
- try {
10100
- return await task();
10101
- } finally {
10102
- this.release(job);
10103
- }
10104
- });
10105
- }
10106
- async drain() {
10107
- let idleTurns = 0;
10108
- while (idleTurns < 20) {
10109
- const generation = this.generation;
10110
- if (this.running) await new Promise((resolve) => this.idle.push(resolve));
10111
- await Promise.resolve();
10112
- idleTurns = generation === this.generation ? idleTurns + 1 : 0;
10113
- }
10114
- }
10115
- advance() {
10116
- if (this.running) return;
10117
- if (this.ready.length === 0 && this.pending.length > 0) {
10118
- const empty = this.ready;
10119
- this.ready = this.pending.reverse();
10120
- this.pending = empty;
10121
- }
10122
- const next = this.ready.pop();
10123
- if (next !== void 0) {
10124
- next();
10125
- } else {
10126
- for (const resolve of this.idle.splice(0)) resolve();
10127
- }
10128
- }
10129
- };
10130
- function runPromiseJob(task) {
10131
- const job = activeJob.getStore();
10132
- return job === void 0 ? Promise.resolve().then(task) : job.queue.run(task);
10133
- }
10134
- function runAsyncPrefix(task) {
10135
- const parent = activeJob.getStore();
10136
- if (parent === void 0) return task();
10137
- let owner = parent;
10138
- while (owner !== void 0 && !owner.ownsExecution) owner = owner.prefixParent;
10139
- if (owner === void 0) return parent.queue.run(task);
10140
- const job = { queue: parent.queue, ownsExecution: false, prefixParent: parent };
10141
- return activeJob.run(job, async () => {
10142
- try {
10143
- return await task();
10144
- } finally {
10145
- job.queue.release(job);
10146
- }
10147
- });
10148
- }
10149
- async function suspendJob(pending) {
10150
- const job = activeJob.getStore();
10151
- if (job === void 0) return pending;
10152
- job.queue.release(job);
10153
- try {
10154
- return await pending;
10155
- } finally {
10156
- await job.queue.acquire(job);
10157
- }
10158
- }
10159
-
10160
10262
  // packages/safe-js/src/interp/promise.ts
10161
10263
  var promiseConstructors = /* @__PURE__ */ new WeakSet();
10162
10264
  var intrinsicPromiseConstructors = /* @__PURE__ */ new WeakMap();
@@ -17811,7 +17913,7 @@ function statementContainsAwait(node) {
17811
17913
  return (node.init?.type === "VariableDeclaration" ? variableDeclarationContainsAwait(node.init) : node.init !== void 0 && expressionContainsAwait(node.init)) || node.test !== void 0 && expressionContainsAwait(node.test) || node.update !== void 0 && expressionContainsAwait(node.update) || statementContainsAwait(node.body);
17812
17914
  case "ForInStatement":
17813
17915
  case "ForOfStatement":
17814
- return node.left.type === "VariableDeclaration" && variableDeclarationContainsAwait(node.left) || node.left.type !== "VariableDeclaration" && assignmentTargetContainsAwait(node.left) || expressionContainsAwait(node.right) || statementContainsAwait(node.body);
17916
+ return node.type === "ForOfStatement" && node.await === true || node.left.type === "VariableDeclaration" && variableDeclarationContainsAwait(node.left) || node.left.type !== "VariableDeclaration" && assignmentTargetContainsAwait(node.left) || expressionContainsAwait(node.right) || statementContainsAwait(node.body);
17815
17917
  case "WhileStatement":
17816
17918
  case "DoWhileStatement":
17817
17919
  return expressionContainsAwait(node.test) || statementContainsAwait(node.body);
@@ -29772,7 +29874,7 @@ async function evaluateForOfStatement(node, context) {
29772
29874
  const restored = context.scope.consumeRestoredBinding(restoredIteration.values[0]);
29773
29875
  if (restored.found && Array.isArray(restored.value)) {
29774
29876
  restoredEntry = { done: false, value: restored.value[1] };
29775
- if (isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0]) || isSandboxCollectionIterator(restored.value[0])) {
29877
+ if (Array.isArray(restored.value[0]) || isSandboxMap(restored.value[0]) || isSandboxSet(restored.value[0]) || isSandboxCollectionIterator(restored.value[0])) {
29776
29878
  return evaluateForOfIterator(node, restored.value[0], context, restoredEntry);
29777
29879
  }
29778
29880
  }
@@ -29781,7 +29883,7 @@ async function evaluateForOfStatement(node, context) {
29781
29883
  if (iterable.kind !== "normal") {
29782
29884
  return iterable;
29783
29885
  }
29784
- const values = snapshotableIterationValues(iterable.value);
29886
+ const values = node.await ? void 0 : snapshotableIterationValues(iterable.value);
29785
29887
  if (values === void 0) {
29786
29888
  return evaluateForOfIterator(node, iterable.value, context, restoredEntry);
29787
29889
  }
@@ -29820,16 +29922,27 @@ async function evaluateForOfStatement(node, context) {
29820
29922
  };
29821
29923
  }
29822
29924
  async function evaluateForOfIterator(node, value, context, restoredEntry) {
29823
- const iterator = getSandboxIterator(value, context.budget, createCoercionContext(context));
29925
+ const iterator = node.await ? getSandboxAsyncIterator(value, context.budget, createCoercionContext(context), context.signal) : getSandboxIterator(value, context.budget, createCoercionContext(context));
29824
29926
  if (iterator === void 0) {
29825
29927
  throw new TypeError(`${String(value)} is not a supported iterable`);
29826
29928
  }
29929
+ const nextIteration = async () => {
29930
+ const pending = Promise.resolve(iterator.next());
29931
+ if (!node.await) return pending;
29932
+ context.onSuspend?.();
29933
+ const leaveAwait = context.budget.enterAwait();
29934
+ try {
29935
+ return await suspendJob(pending);
29936
+ } finally {
29937
+ leaveAwait();
29938
+ }
29939
+ };
29827
29940
  const releaseIterator = retainValues(context.budget, () => [value, iterator.retainedValue]);
29828
29941
  try {
29829
29942
  const nodeId = node.nodeId ?? -1;
29830
29943
  let index = consumeRestoredLoopIterationIndex(node, context);
29831
29944
  for (let skipped = 0; skipped < index; skipped += 1) {
29832
- const skippedIteration = await iterator.next();
29945
+ const skippedIteration = await nextIteration();
29833
29946
  if (typeof skippedIteration !== "object" || skippedIteration === null) {
29834
29947
  throw new TypeError("Iterator result must be an object.");
29835
29948
  }
@@ -29838,7 +29951,7 @@ async function evaluateForOfIterator(node, value, context, restoredEntry) {
29838
29951
  }
29839
29952
  }
29840
29953
  while (true) {
29841
- const iteration = restoredEntry ?? await iterator.next();
29954
+ const iteration = restoredEntry ?? await nextIteration();
29842
29955
  restoredEntry = void 0;
29843
29956
  if (typeof iteration !== "object" || iteration === null) {
29844
29957
  throw new TypeError("Iterator result must be an object.");
@@ -30133,7 +30246,7 @@ function createLoopIterationContext(context, scope) {
30133
30246
  if (context.activeLoopIterations.size === 0) return snapshot;
30134
30247
  snapshot.loopIterations = {};
30135
30248
  for (const [nodeId, iteration] of context.activeLoopIterations) {
30136
- if (typeof iteration !== "number" && (isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]) || isSandboxCollectionIterator(iteration.values[0]))) {
30249
+ if (typeof iteration !== "number" && (Array.isArray(iteration.values[0]) || isSandboxMap(iteration.values[0]) || isSandboxSet(iteration.values[0]) || isSandboxCollectionIterator(iteration.values[0]))) {
30137
30250
  const bindingName = `#for-of:${nodeId}`;
30138
30251
  snapshot.bindings[bindingName] = iteration.values;
30139
30252
  snapshot.loopIterations[nodeId] = { index: iteration.index, values: [bindingName] };
@@ -31610,21 +31723,6 @@ function describeObjectSpreadValue(value) {
31610
31723
  }
31611
31724
  return typeof value;
31612
31725
  }
31613
- async function closeIterator(iterator, preserveThrow = false) {
31614
- try {
31615
- const close = iterator.return;
31616
- if (close === void 0) return;
31617
- const returned = close();
31618
- const result = iterator.generator || iterator.asynchronous ? await returned : returned;
31619
- if (typeof result !== "object" && typeof result !== "function" || result === null) {
31620
- throw new TypeError("Iterator return result must be an object.");
31621
- }
31622
- } catch (error) {
31623
- if (!preserveThrow || isFatalSandboxError(error) || error instanceof HostCallResumabilityError) {
31624
- throw error;
31625
- }
31626
- }
31627
- }
31628
31726
  function defineSandboxProperty(target, key, value) {
31629
31727
  Object.defineProperty(target, key, {
31630
31728
  configurable: true,
@@ -35610,4 +35708,4 @@ export {
35610
35708
  FileSnapshotBackend,
35611
35709
  run
35612
35710
  };
35613
- //# sourceMappingURL=chunk-KHKBLB42.js.map
35711
+ //# sourceMappingURL=chunk-4DTGT4CA.js.map