poe-code 14.0.81 → 14.0.83

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.
Files changed (44) hide show
  1. package/dist/index.js +933 -353
  2. package/dist/index.js.map +4 -4
  3. package/dist/metafile.json +1 -1
  4. package/package.json +1 -1
  5. package/packages/safe-js/dist/chunks/{chunk-2LNBLFJ3.js → chunk-37V6Q4K3.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-ZSYBRZU6.js → chunk-PT7PBLVT.js} +764 -224
  7. package/packages/safe-js/dist/chunks/chunk-PT7PBLVT.js.map +7 -0
  8. package/packages/safe-js/dist/cli.js +2 -2
  9. package/packages/safe-js/dist/core.js +1 -1
  10. package/packages/safe-js/dist/index.js +2 -2
  11. package/packages/safe-js/dist/interp/error-prototypes.d.ts +3 -0
  12. package/packages/safe-js/dist/interp/error-prototypes.js +1 -0
  13. package/packages/safe-js/dist/interp/exceptions.d.ts +1 -0
  14. package/packages/safe-js/dist/interp/exceptions.js +39 -15
  15. package/packages/safe-js/dist/interp/float32-prototypes.d.ts +3 -0
  16. package/packages/safe-js/dist/interp/float32-prototypes.js +1 -0
  17. package/packages/safe-js/dist/interp/float32.d.ts +1 -0
  18. package/packages/safe-js/dist/interp/float32.js +12 -4
  19. package/packages/safe-js/dist/interp/globals/error.d.ts +2 -0
  20. package/packages/safe-js/dist/interp/globals/error.js +104 -4
  21. package/packages/safe-js/dist/interp/globals/float32array.d.ts +3 -2
  22. package/packages/safe-js/dist/interp/globals/float32array.js +371 -65
  23. package/packages/safe-js/dist/interp/globals/object-array.js +3 -3
  24. package/packages/safe-js/dist/interp/globals.d.ts +2 -0
  25. package/packages/safe-js/dist/interp/globals.js +8 -4
  26. package/packages/safe-js/dist/interp/host-bridge.js +4 -2
  27. package/packages/safe-js/dist/interp/instanceof.js +4 -3
  28. package/packages/safe-js/dist/interp/interpreter.js +2 -4
  29. package/packages/safe-js/dist/interp/iteration.js +15 -6
  30. package/packages/safe-js/dist/interp/object-model.js +27 -1
  31. package/packages/safe-js/dist/interp/values.js +59 -6
  32. package/packages/safe-js/dist/parse/parser.js +8 -3
  33. package/packages/safe-js/dist/parse/tokenizer.js +45 -3
  34. package/packages/safe-js/dist/snapshot/float32array.d.ts +3 -0
  35. package/packages/safe-js/dist/snapshot/float32array.js +20 -1
  36. package/packages/safe-js/dist/snapshot/guest-heap-validation.js +4 -1
  37. package/packages/safe-js/dist/snapshot/guest-heap.d.ts +2 -0
  38. package/packages/safe-js/dist/snapshot/guest-heap.js +6 -2
  39. package/packages/safe-js/dist/snapshot/restore.js +33 -3
  40. package/packages/safe-js/dist/snapshot/serialize.d.ts +2 -1
  41. package/packages/safe-js/dist/snapshot/serialize.js +10 -9
  42. package/packages/safe-js/dist/snapshot/validation.js +11 -1
  43. package/packages/safe-js/dist/chunks/chunk-ZSYBRZU6.js.map +0 -7
  44. /package/packages/safe-js/dist/chunks/{chunk-2LNBLFJ3.js.map → chunk-37V6Q4K3.js.map} +0 -0
@@ -22,7 +22,7 @@ import {
22
22
  resolveFsConfig,
23
23
  splitFrontmatter,
24
24
  withBrokenPipeGuard
25
- } from "./chunks/chunk-2LNBLFJ3.js";
25
+ } from "./chunks/chunk-37V6Q4K3.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-ZSYBRZU6.js";
37
+ } from "./chunks/chunk-PT7PBLVT.js";
38
38
  import "./chunks/chunk-M2DLCGEF.js";
39
39
  import "./chunks/chunk-Q7USUEL7.js";
40
40
  import "./chunks/chunk-4YIDX44G.js";
@@ -5,7 +5,7 @@ import {
5
5
  defineExtension,
6
6
  lint,
7
7
  run
8
- } from "./chunks/chunk-ZSYBRZU6.js";
8
+ } from "./chunks/chunk-PT7PBLVT.js";
9
9
  import "./chunks/chunk-4YIDX44G.js";
10
10
  export {
11
11
  Budget,
@@ -26,7 +26,7 @@ import {
26
26
  runWithSpawnUsageAccumulator,
27
27
  splitFrontmatter,
28
28
  supportsSpawnMode
29
- } from "./chunks/chunk-2LNBLFJ3.js";
29
+ } from "./chunks/chunk-37V6Q4K3.js";
30
30
  import {
31
31
  Budget,
32
32
  FileSnapshotBackend,
@@ -50,7 +50,7 @@ import {
50
50
  registerPendingHostCallPolicy,
51
51
  restore,
52
52
  run
53
- } from "./chunks/chunk-ZSYBRZU6.js";
53
+ } from "./chunks/chunk-PT7PBLVT.js";
54
54
  import "./chunks/chunk-M2DLCGEF.js";
55
55
  import "./chunks/chunk-Q7USUEL7.js";
56
56
  import "./chunks/chunk-4YIDX44G.js";
@@ -0,0 +1,3 @@
1
+ import type { Budget } from "./budget.js";
2
+ import type { SandboxObject } from "./values.js";
3
+ export declare const errorPrototypes: WeakMap<Budget, Map<"Error" | "TypeError" | "RangeError" | "ReferenceError" | "SyntaxError" | "URIError" | "EvalError" | "AbortError" | "AggregateError" | "HarnessFailure", SandboxObject>>;
@@ -0,0 +1 @@
1
+ export const errorPrototypes = new WeakMap();
@@ -55,5 +55,6 @@ export declare function createSubsetErrorValue(name: string, message: SandboxVal
55
55
  cause?: unknown;
56
56
  chargeBudget?: boolean;
57
57
  span?: ErrorSourceSpan;
58
+ transport?: boolean;
58
59
  }): SandboxObject;
59
60
  export declare function isSandboxErrorConstructorInstance(value: SandboxValue, name: SandboxErrorName): boolean;
@@ -5,7 +5,8 @@ import { withFatalPromiseCleanup } from "./promise-tracker.js";
5
5
  import { deepCopyToSandbox, ownEnumerableSandboxKeys } from "./values.js";
6
6
  import { hasOwnSandboxProperty } from "./globals/object.js";
7
7
  import { retainValues } from "./resources.js";
8
- import { getSandboxDataProperty } from "./object-model.js";
8
+ import { getSandboxDataProperty, setSandboxPrototype } from "./object-model.js";
9
+ import { errorPrototypes } from "./error-prototypes.js";
9
10
  import { toPropertyKey } from "./property-key.js";
10
11
  import { internalSymbols } from "./internal-symbols.js";
11
12
  import { containsResumeTarget } from "./resume-target.js";
@@ -162,8 +163,7 @@ export function surfaceThrownValue(reason, budget, stackFrames = [], span) {
162
163
  throw reason;
163
164
  }
164
165
  if (isSubsetErrorValue(reason)) {
165
- normalizeSurfacedSubsetError(reason, budget, stackFrames, span);
166
- return reason;
166
+ return normalizeSurfacedSubsetError(reason, budget, stackFrames, span);
167
167
  }
168
168
  if (reason instanceof Error) {
169
169
  const error = createSubsetErrorValue(reason.name || "Error", reason.message, stackFrames, budget, {
@@ -171,8 +171,7 @@ export function surfaceThrownValue(reason, budget, stackFrames = [], span) {
171
171
  chargeBudget: false,
172
172
  span
173
173
  });
174
- normalizeSurfacedSubsetError(error, budget, stackFrames, span);
175
- return error;
174
+ return normalizeSurfacedSubsetError(error, budget, stackFrames, span);
176
175
  }
177
176
  if (isErrorLikeValue(reason)) {
178
177
  const error = createSubsetErrorValue(reason.name || "Error", reason.message, stackFrames, budget, {
@@ -180,13 +179,13 @@ export function surfaceThrownValue(reason, budget, stackFrames = [], span) {
180
179
  chargeBudget: false,
181
180
  span
182
181
  });
183
- normalizeSurfacedSubsetError(error, budget, stackFrames, span);
184
- return error;
182
+ return normalizeSurfacedSubsetError(error, budget, stackFrames, span);
185
183
  }
186
- return createSubsetErrorValue("Error", describeThrownValue(reason), stackFrames, budget, {
184
+ const error = createSubsetErrorValue("Error", describeThrownValue(reason), stackFrames, budget, {
187
185
  chargeBudget: false,
188
186
  span
189
187
  });
188
+ return normalizeSurfacedSubsetError(error, budget, stackFrames, span);
190
189
  }
191
190
  export function createSubsetErrorValue(name, message, stackFrames, budget, options = {}) {
192
191
  const resumeChecks = options.chargeBudget === false ? budget.suspendChecks() : undefined;
@@ -195,11 +194,16 @@ export function createSubsetErrorValue(name, message, stackFrames, budget, optio
195
194
  const errorMessage = budget.allocateString(coerceErrorMessage(message));
196
195
  const header = errorMessage === "" ? errorName : `${errorName}: ${errorMessage}`;
197
196
  const stack = budget.allocateString([header, ...[...stackFrames].reverse()].join("\n"));
198
- const error = {
199
- name: errorName,
200
- message: errorMessage,
201
- stack
202
- };
197
+ const prototype = options.transport ? undefined : errorPrototypes.get(budget)?.get(toSandboxErrorName(errorName));
198
+ const error = prototype === undefined ? { name: errorName, message: errorMessage, stack } : {};
199
+ if (prototype !== undefined) {
200
+ if (message !== undefined)
201
+ Object.defineProperty(error, "message", { value: errorMessage, writable: true, configurable: true });
202
+ if (!errorPrototypes.get(budget).has(errorName))
203
+ Object.defineProperty(error, "name", { value: errorName, writable: true, configurable: true });
204
+ Object.defineProperty(error, "stack", { value: stack, writable: true, configurable: true });
205
+ setSandboxPrototype(error, prototype, budget);
206
+ }
203
207
  sandboxErrorTypes.set(error, toSandboxErrorName(errorName));
204
208
  attachErrorSpan(error, options.span);
205
209
  attachWrappedErrorCause(error, options.cause);
@@ -224,6 +228,8 @@ function isSubsetErrorValue(value) {
224
228
  if (typeof value !== "object" || value === null) {
225
229
  return false;
226
230
  }
231
+ if (sandboxErrorTypes.has(value))
232
+ return true;
227
233
  const prototype = Object.getPrototypeOf(value);
228
234
  return ((prototype === Object.prototype || prototype === null) &&
229
235
  typeof value.name === "string" &&
@@ -236,20 +242,38 @@ function normalizeSurfacedSubsetError(error, budget, stackFrames, span) {
236
242
  const name = budget.allocateString(toSandboxErrorName(readErrorName(error)));
237
243
  const message = budget.allocateString(readSurfacedErrorMessage(error, name));
238
244
  const frames = readSandboxStackFrames(error.stack);
245
+ if (!Object.isExtensible(error) || ["name", "message", "stack"].some(key => {
246
+ const descriptor = Object.getOwnPropertyDescriptor(error, key);
247
+ return descriptor !== undefined && (!("value" in descriptor) || !descriptor.writable);
248
+ })) {
249
+ // Public diagnostics must not mutate frozen guest state or invoke setters.
250
+ const original = error;
251
+ const descriptors = Object.getOwnPropertyDescriptors(original);
252
+ delete descriptors.name;
253
+ delete descriptors.message;
254
+ delete descriptors.stack;
255
+ error = Object.defineProperties({}, descriptors);
256
+ const errorType = sandboxErrorTypes.get(original);
257
+ if (errorType !== undefined)
258
+ sandboxErrorTypes.set(error, errorType);
259
+ }
239
260
  error.name = name;
240
261
  error.message = message;
241
262
  error.stack = budget.allocateString(formatErrorStack(name, message, frames.length > 0 ? frames : [...stackFrames].reverse()));
242
263
  attachErrorSpan(error, readErrorSpan(error) ?? span);
264
+ return error;
243
265
  }
244
266
  finally {
245
267
  resumeChecks();
246
268
  }
247
269
  }
248
270
  function readErrorName(error) {
249
- return typeof error.name === "string" && error.name.length > 0 ? error.name : "Error";
271
+ const name = getSandboxDataProperty(error, "name");
272
+ return typeof name === "string" && name.length > 0 ? name : sandboxErrorTypes.get(error) ?? "Error";
250
273
  }
251
274
  function readSurfacedErrorMessage(error, name) {
252
- const message = typeof error.message === "string" ? error.message : "";
275
+ const value = getSandboxDataProperty(error, "message");
276
+ const message = typeof value === "string" ? value : "";
253
277
  if (message === "") {
254
278
  return `${name} thrown`;
255
279
  }
@@ -0,0 +1,3 @@
1
+ import type { Budget } from "./budget.js";
2
+ import type { SandboxObject } from "./values.js";
3
+ export declare const float32Prototypes: WeakMap<Budget, SandboxObject>;
@@ -0,0 +1 @@
1
+ export const float32Prototypes = new WeakMap();
@@ -6,6 +6,7 @@ export declare function float32Storage(value: Float32Array): {
6
6
  length: number;
7
7
  byteLength: number;
8
8
  };
9
+ export declare function float32Properties(value: Float32Array): Array<[PropertyKey, PropertyDescriptor]>;
9
10
  export declare function float32DataProperties(value: Float32Array): Array<[string, PropertyDescriptor]>;
10
11
  export declare function isFloat32Index(key: string): boolean;
11
12
  export declare function float32Number(value: unknown): number;
@@ -23,14 +23,22 @@ export function float32Storage(value) {
23
23
  byteLength: Reflect.apply(bufferLength, buffer, [])
24
24
  };
25
25
  }
26
+ export function float32Properties(value) {
27
+ const properties = [];
28
+ for (const key of Reflect.ownKeys(value)) {
29
+ if (typeof key === "string" && isFloat32Index(key))
30
+ continue;
31
+ properties.push([key, Object.getOwnPropertyDescriptor(value, key)]);
32
+ }
33
+ return properties;
34
+ }
26
35
  export function float32DataProperties(value) {
27
36
  if (Object.getOwnPropertySymbols(value).length > 0)
28
37
  throw new TypeError("Float32Array symbol properties are not supported.");
29
38
  const properties = [];
30
- for (const key of Object.getOwnPropertyNames(value)) {
31
- if (isFloat32Index(key))
32
- continue;
33
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
39
+ for (const [key, descriptor] of float32Properties(value)) {
40
+ if (typeof key !== "string")
41
+ throw new TypeError("Float32Array symbol properties are not supported.");
34
42
  if (!("value" in descriptor))
35
43
  throw new TypeError(`Float32Array accessor property '${key}' is not supported.`);
36
44
  properties.push([key, descriptor]);
@@ -5,7 +5,9 @@ export type ErrorName = (typeof errorNames)[number];
5
5
  export type ErrorGlobals = Record<ErrorName, ReturnType<typeof createSandboxClosure>>;
6
6
  export declare function createErrorGlobals(options: {
7
7
  budget: Budget;
8
+ errorPrototypes?: boolean;
8
9
  }): ErrorGlobals;
9
10
  export declare function isSandboxErrorConstructorInstance(value: SandboxValue, constructor: SandboxValue): boolean;
10
11
  export declare function isSandboxErrorConstructor(value: SandboxValue): value is SandboxClosure;
12
+ export declare function createErrorPrototypes(budget: Budget, constructors: ErrorGlobals): void;
11
13
  export {};
@@ -1,3 +1,10 @@
1
+ import { errorPrototypes } from "../error-prototypes.js";
2
+ import { getSandboxPropertyDescriptor, getSandboxPrototype, materializeFunctionProperties, registerIntrinsicFunction, registerIntrinsicObject, setSandboxPrototype } from "../object-model.js";
3
+ import { registerBuiltinIdentities } from "../intrinsics.js";
4
+ import { sandboxString } from "../string-coercion.js";
5
+ import { retainValues } from "../resources.js";
6
+ import { readPropertyDescriptor } from "../accessors.js";
7
+ import { acquireSandboxIterator, getSandboxIterator, readIteratorResult } from "../iteration.js";
1
8
  import { createSubsetErrorValue, isSandboxErrorConstructorInstance as isNamedSandboxErrorConstructorInstance } from "../exceptions.js";
2
9
  import { createSandboxClosure, isSandboxClosure } from "../values.js";
3
10
  const errorConstructorNames = new WeakMap();
@@ -12,7 +19,7 @@ const errorNames = [
12
19
  "AggregateError"
13
20
  ];
14
21
  export function createErrorGlobals(options) {
15
- return Object.fromEntries(errorNames.map((name) => [name, createErrorConstructor(name, options.budget)]));
22
+ return Object.fromEntries(errorNames.map((name) => [name, createErrorConstructor(name, options.budget, options.errorPrototypes === true)]));
16
23
  }
17
24
  export function isSandboxErrorConstructorInstance(value, constructor) {
18
25
  if (!isSandboxClosure(constructor)) {
@@ -27,12 +34,105 @@ export function isSandboxErrorConstructorInstance(value, constructor) {
27
34
  export function isSandboxErrorConstructor(value) {
28
35
  return isSandboxClosure(value) && errorConstructorNames.has(value);
29
36
  }
30
- function createErrorConstructor(name, budget) {
31
- const call = (args, context) => createSubsetError(name, args, context?.stack ?? [], budget);
32
- const closure = createSandboxClosure({ sandbox: true, call, construct: call, name });
37
+ function createErrorConstructor(name, budget, guest) {
38
+ const call = (args, context) => errorPrototypes.has(budget) ? createNativeError(name, args, budget, context, closure)
39
+ : createSubsetError(name, args, context?.stack ?? [], budget);
40
+ const closure = createSandboxClosure({ guest, sandbox: true, call, construct: call, name, length: name === "AggregateError" ? 2 : 1 });
33
41
  errorConstructorNames.set(closure, name);
34
42
  return closure;
35
43
  }
44
+ export function createErrorPrototypes(budget, constructors) {
45
+ const prototypes = new Map();
46
+ for (const name of errorNames)
47
+ prototypes.set(name, Object.create(null));
48
+ errorPrototypes.set(budget, prototypes);
49
+ for (const name of errorNames) {
50
+ const constructor = constructors[name];
51
+ const prototype = prototypes.get(name);
52
+ Object.defineProperties(prototype, {
53
+ name: { value: name, writable: true, configurable: true },
54
+ message: { value: "", writable: true, configurable: true },
55
+ constructor: { value: constructor, writable: true, configurable: true }
56
+ });
57
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
58
+ setSandboxPrototype(prototype, name === "Error" ? getSandboxPrototype(Object.create(null), budget) : prototypes.get("Error"));
59
+ if (name !== "Error")
60
+ setSandboxPrototype(constructor, constructors.Error);
61
+ }
62
+ Object.defineProperty(prototypes.get("Error"), "toString", {
63
+ writable: true, configurable: true,
64
+ value: createSandboxClosure({ guest: true, sandbox: true, name: "toString", length: 0,
65
+ call: async (_args, context) => {
66
+ const receiver = context?.thisValue;
67
+ if (receiver === null || typeof receiver !== "object")
68
+ throw new TypeError("Error.prototype.toString requires an object.");
69
+ let name;
70
+ const release = retainValues(budget, () => [receiver, name]);
71
+ try {
72
+ const rawName = await readErrorProperty(receiver, "name", budget, context);
73
+ name = rawName === undefined ? "Error" : await sandboxString(rawName, budget, context);
74
+ const rawMessage = await readErrorProperty(receiver, "message", budget, context);
75
+ const message = rawMessage === undefined ? "" : await sandboxString(rawMessage, budget, context);
76
+ return budget.allocateString(name === "" ? message : message === "" ? name : `${name}: ${message}`);
77
+ }
78
+ finally {
79
+ release();
80
+ }
81
+ }
82
+ })
83
+ });
84
+ registerBuiltinIdentities(budget, Object.fromEntries(errorNames.map(name => [name, constructors[name]])));
85
+ for (const name of errorNames) {
86
+ registerIntrinsicFunction(budget, constructors[name]);
87
+ registerIntrinsicObject(budget, prototypes.get(name));
88
+ }
89
+ }
90
+ async function readErrorProperty(value, key, budget, context) {
91
+ if (context?.getProperty !== undefined)
92
+ return context.getProperty(value, key);
93
+ const descriptor = getSandboxPropertyDescriptor(value, key, budget);
94
+ return descriptor === undefined ? undefined : readPropertyDescriptor(descriptor, value, context);
95
+ }
96
+ async function createNativeError(name, args, budget, context, constructor) {
97
+ const newTarget = context?.newTarget ?? constructor;
98
+ const candidate = await readErrorProperty(newTarget, "prototype", budget, context);
99
+ const prototype = candidate !== null && typeof candidate === "object" ? candidate : errorPrototypes.get(budget).get(name);
100
+ let error;
101
+ let iterator;
102
+ const errors = [];
103
+ const release = retainValues(budget, () => [prototype, error, iterator?.retainedValue, ...errors, ...args]);
104
+ try {
105
+ const message = name === "AggregateError" ? args[1] : args[0];
106
+ const options = name === "AggregateError" ? args[2] : args[1];
107
+ const text = message === undefined ? undefined : await sandboxString(message, budget, context);
108
+ error = createSubsetErrorValue(name, text, context?.stack ?? [], budget);
109
+ setSandboxPrototype(error, prototype, budget);
110
+ if (options !== null && typeof options === "object" && getSandboxPropertyDescriptor(options, "cause", budget) !== undefined) {
111
+ const cause = await readErrorProperty(options, "cause", budget, context);
112
+ Object.defineProperty(error, "cause", { value: cause, writable: true, configurable: true });
113
+ }
114
+ if (name === "AggregateError") {
115
+ iterator = context === undefined ? getSandboxIterator(args[0], budget) : await acquireSandboxIterator(args[0], budget, context);
116
+ if (iterator === undefined)
117
+ throw new TypeError("AggregateError errors must be iterable.");
118
+ while (true) {
119
+ budget.visitNode();
120
+ const result = await iterator.next();
121
+ if (result === null || typeof result !== "object")
122
+ throw new TypeError("Iterator result must be an object.");
123
+ if ((await readIteratorResult(iterator, result, "done")).value)
124
+ break;
125
+ budget.allocateArrayLength(errors.length + 1);
126
+ errors.push((await readIteratorResult(iterator, result, "value")).value);
127
+ }
128
+ Object.defineProperty(error, "errors", { value: errors, writable: true, configurable: true });
129
+ }
130
+ return error;
131
+ }
132
+ finally {
133
+ release();
134
+ }
135
+ }
36
136
  function createSubsetError(name, args, stackFrames, budget) {
37
137
  const message = name === "AggregateError" ? args[1] : args[0];
38
138
  const options = name === "AggregateError" ? args[2] : args[1];
@@ -1,6 +1,7 @@
1
1
  import type { Budget } from "../budget.js";
2
2
  import { type SandboxClosure, type SandboxValue } from "../values.js";
3
- export declare function createFloat32ArrayGlobal(budget: Budget): SandboxClosure;
3
+ export declare function createFloat32ArrayGlobal(budget: Budget, nativePrototype?: boolean): SandboxClosure;
4
+ export declare function createFloat32ArrayPrototypes(budget: Budget, constructor: SandboxClosure): void;
4
5
  export declare function isFloat32ArrayConstructor(value: unknown): boolean;
5
6
  export declare function getFloat32Member(value: Float32Array, property: string | number, budget: Budget): SandboxValue;
6
- export declare function setFloat32Member(value: Float32Array, property: string | number, entry: SandboxValue): void;
7
+ export declare function setFloat32Member(value: Float32Array, property: PropertyKey, entry: SandboxValue, budget?: Budget): void;