poe-code 12.0.5 → 12.0.6

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.
@@ -22,7 +22,7 @@ import {
22
22
  resolveFsConfig,
23
23
  splitFrontmatter,
24
24
  withBrokenPipeGuard
25
- } from "./chunks/chunk-RRSINVWT.js";
25
+ } from "./chunks/chunk-KJSS53IH.js";
26
26
  import {
27
27
  Budget,
28
28
  SandboxError,
@@ -34,7 +34,7 @@ import {
34
34
  replaceErrorStack,
35
35
  restore,
36
36
  run
37
- } from "./chunks/chunk-XBRQ72W6.js";
37
+ } from "./chunks/chunk-NDHGDM46.js";
38
38
  import "./chunks/chunk-B3G3JVCW.js";
39
39
  import "./chunks/chunk-4YIDX44G.js";
40
40
 
@@ -3,7 +3,7 @@ import {
3
3
  createReplayableRandom,
4
4
  lint,
5
5
  run
6
- } from "./chunks/chunk-XBRQ72W6.js";
6
+ } from "./chunks/chunk-NDHGDM46.js";
7
7
  import "./chunks/chunk-4YIDX44G.js";
8
8
  export {
9
9
  Budget,
@@ -29,7 +29,7 @@ import {
29
29
  runWithSpawnUsageAccumulator,
30
30
  splitFrontmatter,
31
31
  supportsSpawnMode
32
- } from "./chunks/chunk-RRSINVWT.js";
32
+ } from "./chunks/chunk-KJSS53IH.js";
33
33
  import {
34
34
  Budget,
35
35
  FileSnapshotBackend,
@@ -51,7 +51,7 @@ import {
51
51
  registerPendingHostCallPolicy,
52
52
  restore,
53
53
  run
54
- } from "./chunks/chunk-XBRQ72W6.js";
54
+ } from "./chunks/chunk-NDHGDM46.js";
55
55
  import "./chunks/chunk-B3G3JVCW.js";
56
56
  import "./chunks/chunk-4YIDX44G.js";
57
57
 
@@ -0,0 +1,7 @@
1
+ import type { Budget } from "../budget.js";
2
+ import type { SandboxValue } from "../values.js";
3
+ export declare function enterKeyedCollectionCallback(target: object, keys: Iterable<SandboxValue>, budget: Budget): {
4
+ next(): IteratorResult<SandboxValue>;
5
+ leave(): void;
6
+ };
7
+ export declare function updateKeyedCollectionCallbacks(target: object, mutation: "add" | "delete" | "clear", key?: SandboxValue): void;
@@ -0,0 +1,68 @@
1
+ import { enterRunningState } from "../running-state.js";
2
+ const activeCallbacks = new WeakMap();
3
+ export function enterKeyedCollectionCallback(target, keys, budget) {
4
+ let state = activeCallbacks.get(target);
5
+ if (state === undefined) {
6
+ state = { cursors: new Set(), leaveRunning: enterRunningState(target) };
7
+ activeCallbacks.set(target, state);
8
+ }
9
+ const cursor = { budget, pending: new Set() };
10
+ state.cursors.add(cursor);
11
+ const leave = () => {
12
+ if (!state.cursors.delete(cursor))
13
+ return;
14
+ cursor.pending.clear();
15
+ budget.setRetainedDataUsage(cursor, 0);
16
+ budget.setRetainedValues(cursor, undefined);
17
+ if (state.cursors.size === 0) {
18
+ activeCallbacks.delete(target);
19
+ state.leaveRunning();
20
+ }
21
+ };
22
+ try {
23
+ budget.setRetainedDataUsage(cursor, 1);
24
+ budget.setRetainedValues(cursor, () => cursor.pending);
25
+ for (const key of keys)
26
+ updatePendingKeys(cursor, "add", key);
27
+ }
28
+ catch (error) {
29
+ leave();
30
+ throw error;
31
+ }
32
+ return {
33
+ next: () => {
34
+ budget.visitNode();
35
+ for (const key of cursor.pending) {
36
+ cursor.pending.delete(key);
37
+ budget.setRetainedDataUsage(cursor, 1 + cursor.pending.size);
38
+ return { done: false, value: key };
39
+ }
40
+ return { done: true, value: undefined };
41
+ },
42
+ leave
43
+ };
44
+ }
45
+ export function updateKeyedCollectionCallbacks(target, mutation, key) {
46
+ const state = activeCallbacks.get(target);
47
+ if (state === undefined)
48
+ return;
49
+ for (const cursor of state.cursors)
50
+ updatePendingKeys(cursor, mutation, key);
51
+ }
52
+ function updatePendingKeys(cursor, mutation, key) {
53
+ const { budget, pending } = cursor;
54
+ budget.visitNode();
55
+ if (mutation === "add") {
56
+ const nextSize = pending.has(key) ? pending.size : pending.size + 1;
57
+ budget.allocateCollectionEntries(nextSize);
58
+ budget.setRetainedDataUsage(cursor, 1 + nextSize);
59
+ pending.add(key);
60
+ }
61
+ else {
62
+ if (mutation === "delete")
63
+ pending.delete(key);
64
+ else
65
+ pending.clear();
66
+ budget.setRetainedDataUsage(cursor, 1 + pending.size);
67
+ }
68
+ }
@@ -1,5 +1,6 @@
1
1
  import { allocateProducedSandboxValue, createSandboxClosure, isSandboxClosure } from "../values.js";
2
- import { assertCollectionMutable, enterCollectionCallback } from "../running-state.js";
2
+ import { assertCollectionMutable } from "../running-state.js";
3
+ import { enterKeyedCollectionCallback, updateKeyedCollectionCallbacks } from "./collection-callback.js";
3
4
  const mapMethodNames = new Set([
4
5
  "get",
5
6
  "set",
@@ -33,35 +34,43 @@ export async function callMapMethod(target, methodName, args, options, stack = [
33
34
  return target.entries.get(args[0]);
34
35
  case "set": {
35
36
  assertCollectionMutable(target);
36
- const nextSize = target.entries.has(args[0]) ? target.entries.size : target.entries.size + 1;
37
+ const exists = target.entries.has(args[0]);
38
+ const nextSize = exists ? target.entries.size : target.entries.size + 1;
37
39
  options.budget.allocateCollectionEntries(nextSize);
40
+ if (!exists)
41
+ updateKeyedCollectionCallbacks(target, "add", args[0]);
38
42
  target.entries.set(args[0], args[1]);
39
43
  return target;
40
44
  }
41
45
  case "has":
42
46
  return target.entries.has(args[0]);
43
- case "delete":
47
+ case "delete": {
44
48
  assertCollectionMutable(target);
45
- return target.entries.delete(args[0]);
49
+ const deleted = target.entries.delete(args[0]);
50
+ if (deleted)
51
+ updateKeyedCollectionCallbacks(target, "delete", args[0]);
52
+ return deleted;
53
+ }
46
54
  case "clear":
47
55
  assertCollectionMutable(target);
48
56
  target.entries.clear();
57
+ updateKeyedCollectionCallbacks(target, "clear");
49
58
  return undefined;
50
59
  case "forEach": {
51
60
  const callback = args[0];
52
61
  if (!isSandboxClosure(callback)) {
53
62
  throw new TypeError("Map.prototype.forEach requires a callback function.");
54
63
  }
55
- const leaveCallback = enterCollectionCallback(target);
64
+ const cursor = enterKeyedCollectionCallback(target, target.entries.keys(), options.budget);
56
65
  try {
57
- const entries = [...target.entries];
58
- for (let index = 0; index < entries.length; index += 1) {
59
- const [key, value] = entries[index];
66
+ for (let entry = cursor.next(); !entry.done; entry = cursor.next()) {
67
+ const key = entry.value;
68
+ const value = target.entries.get(key);
60
69
  await options.callClosure(callback, [value, key, target], stack, args[1]);
61
70
  }
62
71
  }
63
72
  finally {
64
- leaveCallback();
73
+ cursor.leave();
65
74
  }
66
75
  return undefined;
67
76
  }
@@ -1,5 +1,6 @@
1
1
  import { allocateProducedSandboxValue, createSandboxClosure, isSandboxClosure } from "../values.js";
2
- import { assertCollectionMutable, enterCollectionCallback } from "../running-state.js";
2
+ import { assertCollectionMutable } from "../running-state.js";
3
+ import { enterKeyedCollectionCallback, updateKeyedCollectionCallbacks } from "./collection-callback.js";
3
4
  const setMethodNames = new Set([
4
5
  "add",
5
6
  "has",
@@ -30,35 +31,42 @@ export async function callSetMethod(target, methodName, args, options, stack = [
30
31
  switch (methodName) {
31
32
  case "add": {
32
33
  assertCollectionMutable(target);
33
- const nextSize = target.values.has(args[0]) ? target.values.size : target.values.size + 1;
34
+ const exists = target.values.has(args[0]);
35
+ const nextSize = exists ? target.values.size : target.values.size + 1;
34
36
  options.budget.allocateCollectionEntries(nextSize);
37
+ if (!exists)
38
+ updateKeyedCollectionCallbacks(target, "add", args[0]);
35
39
  target.values.add(args[0]);
36
40
  return target;
37
41
  }
38
42
  case "has":
39
43
  return target.values.has(args[0]);
40
- case "delete":
44
+ case "delete": {
41
45
  assertCollectionMutable(target);
42
- return target.values.delete(args[0]);
46
+ const deleted = target.values.delete(args[0]);
47
+ if (deleted)
48
+ updateKeyedCollectionCallbacks(target, "delete", args[0]);
49
+ return deleted;
50
+ }
43
51
  case "clear":
44
52
  assertCollectionMutable(target);
45
53
  target.values.clear();
54
+ updateKeyedCollectionCallbacks(target, "clear");
46
55
  return undefined;
47
56
  case "forEach": {
48
57
  const callback = args[0];
49
58
  if (!isSandboxClosure(callback)) {
50
59
  throw new TypeError("Set.prototype.forEach requires a callback function.");
51
60
  }
52
- const leaveCallback = enterCollectionCallback(target);
61
+ const cursor = enterKeyedCollectionCallback(target, target.values, options.budget);
53
62
  try {
54
- const values = [...target.values];
55
- for (let index = 0; index < values.length; index += 1) {
56
- const value = values[index];
63
+ for (let entry = cursor.next(); !entry.done; entry = cursor.next()) {
64
+ const value = entry.value;
57
65
  await options.callClosure(callback, [value, value, target], stack, args[1]);
58
66
  }
59
67
  }
60
68
  finally {
61
- leaveCallback();
69
+ cursor.leave();
62
70
  }
63
71
  return undefined;
64
72
  }