poe-code 14.0.81 → 14.0.82

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 (29) hide show
  1. package/dist/index.js +360 -167
  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-7LGBDF56.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-ZSYBRZU6.js → chunk-V6LQP64H.js} +308 -134
  7. package/packages/safe-js/dist/chunks/chunk-V6LQP64H.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/globals/error.d.ts +2 -0
  16. package/packages/safe-js/dist/interp/globals/error.js +104 -4
  17. package/packages/safe-js/dist/interp/globals.d.ts +1 -0
  18. package/packages/safe-js/dist/interp/globals.js +4 -2
  19. package/packages/safe-js/dist/interp/host-bridge.js +4 -2
  20. package/packages/safe-js/dist/interp/instanceof.js +3 -2
  21. package/packages/safe-js/dist/interp/object-model.js +11 -0
  22. package/packages/safe-js/dist/interp/values.js +47 -2
  23. package/packages/safe-js/dist/snapshot/guest-heap-validation.js +4 -1
  24. package/packages/safe-js/dist/snapshot/guest-heap.d.ts +2 -0
  25. package/packages/safe-js/dist/snapshot/guest-heap.js +6 -2
  26. package/packages/safe-js/dist/snapshot/restore.js +17 -2
  27. package/packages/safe-js/dist/snapshot/validation.js +1 -1
  28. package/packages/safe-js/dist/chunks/chunk-ZSYBRZU6.js.map +0 -7
  29. /package/packages/safe-js/dist/chunks/{chunk-2LNBLFJ3.js.map → chunk-7LGBDF56.js.map} +0 -0
package/dist/index.js CHANGED
@@ -126512,60 +126512,6 @@ var init_internal_symbols = __esm({
126512
126512
  }
126513
126513
  });
126514
126514
 
126515
- // packages/safe-js/src/interp/generator-properties.ts
126516
- function getGeneratorProperties(value) {
126517
- let properties = generatorGuestProperties.get(value);
126518
- if (properties === void 0) {
126519
- properties = /* @__PURE__ */ Object.create(null);
126520
- generatorGuestProperties.set(value, properties);
126521
- }
126522
- return properties;
126523
- }
126524
- var generatorGuestProperties;
126525
- var init_generator_properties = __esm({
126526
- "packages/safe-js/src/interp/generator-properties.ts"() {
126527
- "use strict";
126528
- generatorGuestProperties = /* @__PURE__ */ new WeakMap();
126529
- }
126530
- });
126531
-
126532
- // packages/safe-js/src/interp/regexp-properties.ts
126533
- function getRegexProperties(value) {
126534
- const properties = regexGuestProperties.get(value);
126535
- if (properties === void 0) throw new TypeError("Invalid sandbox RegExp storage.");
126536
- return properties;
126537
- }
126538
- var regexGuestProperties;
126539
- var init_regexp_properties = __esm({
126540
- "packages/safe-js/src/interp/regexp-properties.ts"() {
126541
- "use strict";
126542
- regexGuestProperties = /* @__PURE__ */ new WeakMap();
126543
- }
126544
- });
126545
-
126546
- // packages/safe-js/src/interp/collection-properties.ts
126547
- function getCollectionProperties(value) {
126548
- const properties = collectionGuestProperties.get(value);
126549
- if (properties === void 0) throw new TypeError("Invalid sandbox collection storage.");
126550
- return properties;
126551
- }
126552
- function copyCollectionProperties(source, target, encode) {
126553
- const properties = collectionGuestProperties.get(source) ?? source;
126554
- for (const key3 of Reflect.ownKeys(properties)) {
126555
- const descriptor = Object.getOwnPropertyDescriptor(properties, key3);
126556
- if (!("value" in descriptor)) throw new TypeError("Collection accessor properties cannot be copied as data.");
126557
- Object.defineProperty(target, key3, { ...descriptor, value: encode(descriptor.value) });
126558
- }
126559
- if (!Object.isExtensible(properties)) Object.preventExtensions(target);
126560
- }
126561
- var collectionGuestProperties;
126562
- var init_collection_properties = __esm({
126563
- "packages/safe-js/src/interp/collection-properties.ts"() {
126564
- "use strict";
126565
- collectionGuestProperties = /* @__PURE__ */ new WeakMap();
126566
- }
126567
- });
126568
-
126569
126515
  // packages/safe-js/src/interp/accessors.ts
126570
126516
  function accessorAdapter(closure, kind) {
126571
126517
  if (kind === "get") {
@@ -126630,6 +126576,144 @@ var init_accessors = __esm({
126630
126576
  }
126631
126577
  });
126632
126578
 
126579
+ // packages/safe-js/src/interp/symbols.ts
126580
+ var wellKnownSymbols;
126581
+ var init_symbols2 = __esm({
126582
+ "packages/safe-js/src/interp/symbols.ts"() {
126583
+ "use strict";
126584
+ wellKnownSymbols = Object.freeze({
126585
+ iterator: Symbol.iterator,
126586
+ asyncIterator: Symbol.asyncIterator,
126587
+ hasInstance: Symbol.hasInstance,
126588
+ isConcatSpreadable: Symbol.isConcatSpreadable,
126589
+ match: Symbol.match,
126590
+ matchAll: Symbol.matchAll,
126591
+ replace: Symbol.replace,
126592
+ search: Symbol.search,
126593
+ species: Symbol.species,
126594
+ split: Symbol.split,
126595
+ toPrimitive: Symbol.toPrimitive,
126596
+ toStringTag: Symbol.toStringTag,
126597
+ unscopables: Symbol.unscopables
126598
+ });
126599
+ }
126600
+ });
126601
+
126602
+ // packages/safe-js/src/interp/intrinsics.ts
126603
+ function registerBuiltinIdentities(budget, bindings) {
126604
+ let realm = realms2.get(budget);
126605
+ if (realm === void 0) realms2.set(budget, realm = /* @__PURE__ */ new Map());
126606
+ const pending = Object.entries(bindings).map(([name, value]) => [[name], value]);
126607
+ const visited = /* @__PURE__ */ new WeakSet();
126608
+ for (let index = 0; index < pending.length; index++) {
126609
+ const [path193, value] = pending[index];
126610
+ if (value === null || typeof value !== "object") continue;
126611
+ const id = JSON.stringify(path193);
126612
+ const previous = realm.get(id);
126613
+ if (previous !== void 0 && previous !== value)
126614
+ throw new TypeError(`Duplicate intrinsic identity: ${id}`);
126615
+ realm.set(id, value);
126616
+ if (!identities.has(value)) identities.set(value, id);
126617
+ if (visited.has(value)) continue;
126618
+ visited.add(value);
126619
+ const owner = isSandboxClosure(value) ? value.properties : value;
126620
+ if (owner === void 0) continue;
126621
+ for (const key3 of Reflect.ownKeys(owner)) {
126622
+ if (typeof key3 === "symbol" && internalSymbols.has(key3)) continue;
126623
+ const descriptor = Object.getOwnPropertyDescriptor(owner, key3);
126624
+ const symbolName = typeof key3 === "symbol" ? Object.entries(wellKnownSymbols).find(([, symbol2]) => symbol2 === key3)?.[0] : void 0;
126625
+ if (typeof key3 === "symbol" && symbolName === void 0)
126626
+ throw new TypeError("Intrinsic symbol keys must be well-known symbols.");
126627
+ const member = [...path193, typeof key3 === "string" ? key3 : { symbol: symbolName }];
126628
+ if ("value" in descriptor) pending.push([member, descriptor.value]);
126629
+ for (const kind of ["get", "set"]) {
126630
+ const closure = accessorClosure(descriptor[kind]);
126631
+ if (closure !== void 0) pending.push([[...member, kind], closure]);
126632
+ }
126633
+ }
126634
+ }
126635
+ }
126636
+ function getIntrinsicIdentity(value) {
126637
+ return identities.get(value);
126638
+ }
126639
+ function resolveIntrinsicIdentity(budget, id) {
126640
+ const value = realms2.get(budget)?.get(id);
126641
+ if (value === void 0) throw new TypeError(`Unknown intrinsic identity: ${id}`);
126642
+ return value;
126643
+ }
126644
+ function listIntrinsicIdentities(budget) {
126645
+ return [...realms2.get(budget)?.keys() ?? []];
126646
+ }
126647
+ function releaseIntrinsicIdentities(budget) {
126648
+ realms2.delete(budget);
126649
+ }
126650
+ var identities, realms2;
126651
+ var init_intrinsics = __esm({
126652
+ "packages/safe-js/src/interp/intrinsics.ts"() {
126653
+ "use strict";
126654
+ init_accessors();
126655
+ init_symbols2();
126656
+ init_internal_symbols();
126657
+ init_values();
126658
+ identities = /* @__PURE__ */ new WeakMap();
126659
+ realms2 = /* @__PURE__ */ new WeakMap();
126660
+ }
126661
+ });
126662
+
126663
+ // packages/safe-js/src/interp/generator-properties.ts
126664
+ function getGeneratorProperties(value) {
126665
+ let properties = generatorGuestProperties.get(value);
126666
+ if (properties === void 0) {
126667
+ properties = /* @__PURE__ */ Object.create(null);
126668
+ generatorGuestProperties.set(value, properties);
126669
+ }
126670
+ return properties;
126671
+ }
126672
+ var generatorGuestProperties;
126673
+ var init_generator_properties = __esm({
126674
+ "packages/safe-js/src/interp/generator-properties.ts"() {
126675
+ "use strict";
126676
+ generatorGuestProperties = /* @__PURE__ */ new WeakMap();
126677
+ }
126678
+ });
126679
+
126680
+ // packages/safe-js/src/interp/regexp-properties.ts
126681
+ function getRegexProperties(value) {
126682
+ const properties = regexGuestProperties.get(value);
126683
+ if (properties === void 0) throw new TypeError("Invalid sandbox RegExp storage.");
126684
+ return properties;
126685
+ }
126686
+ var regexGuestProperties;
126687
+ var init_regexp_properties = __esm({
126688
+ "packages/safe-js/src/interp/regexp-properties.ts"() {
126689
+ "use strict";
126690
+ regexGuestProperties = /* @__PURE__ */ new WeakMap();
126691
+ }
126692
+ });
126693
+
126694
+ // packages/safe-js/src/interp/collection-properties.ts
126695
+ function getCollectionProperties(value) {
126696
+ const properties = collectionGuestProperties.get(value);
126697
+ if (properties === void 0) throw new TypeError("Invalid sandbox collection storage.");
126698
+ return properties;
126699
+ }
126700
+ function copyCollectionProperties(source, target, encode) {
126701
+ const properties = collectionGuestProperties.get(source) ?? source;
126702
+ for (const key3 of Reflect.ownKeys(properties)) {
126703
+ const descriptor = Object.getOwnPropertyDescriptor(properties, key3);
126704
+ if (!("value" in descriptor)) throw new TypeError("Collection accessor properties cannot be copied as data.");
126705
+ Object.defineProperty(target, key3, { ...descriptor, value: encode(descriptor.value) });
126706
+ }
126707
+ if (!Object.isExtensible(properties)) Object.preventExtensions(target);
126708
+ }
126709
+ var collectionGuestProperties;
126710
+ var init_collection_properties = __esm({
126711
+ "packages/safe-js/src/interp/collection-properties.ts"() {
126712
+ "use strict";
126713
+ collectionGuestProperties = /* @__PURE__ */ new WeakMap();
126714
+ }
126715
+ });
126716
+
126633
126717
  // packages/safe-js/src/interp/collection-brands.ts
126634
126718
  function isSandboxMap(value) {
126635
126719
  return typeof value === "object" && value !== null && sandboxMapBrand in value;
@@ -127180,90 +127264,6 @@ var init_resources = __esm({
127180
127264
  }
127181
127265
  });
127182
127266
 
127183
- // packages/safe-js/src/interp/symbols.ts
127184
- var wellKnownSymbols;
127185
- var init_symbols2 = __esm({
127186
- "packages/safe-js/src/interp/symbols.ts"() {
127187
- "use strict";
127188
- wellKnownSymbols = Object.freeze({
127189
- iterator: Symbol.iterator,
127190
- asyncIterator: Symbol.asyncIterator,
127191
- hasInstance: Symbol.hasInstance,
127192
- isConcatSpreadable: Symbol.isConcatSpreadable,
127193
- match: Symbol.match,
127194
- matchAll: Symbol.matchAll,
127195
- replace: Symbol.replace,
127196
- search: Symbol.search,
127197
- species: Symbol.species,
127198
- split: Symbol.split,
127199
- toPrimitive: Symbol.toPrimitive,
127200
- toStringTag: Symbol.toStringTag,
127201
- unscopables: Symbol.unscopables
127202
- });
127203
- }
127204
- });
127205
-
127206
- // packages/safe-js/src/interp/intrinsics.ts
127207
- function registerBuiltinIdentities(budget, bindings) {
127208
- let realm = realms2.get(budget);
127209
- if (realm === void 0) realms2.set(budget, realm = /* @__PURE__ */ new Map());
127210
- const pending = Object.entries(bindings).map(([name, value]) => [[name], value]);
127211
- const visited = /* @__PURE__ */ new WeakSet();
127212
- for (let index = 0; index < pending.length; index++) {
127213
- const [path193, value] = pending[index];
127214
- if (value === null || typeof value !== "object") continue;
127215
- const id = JSON.stringify(path193);
127216
- const previous = realm.get(id);
127217
- if (previous !== void 0 && previous !== value)
127218
- throw new TypeError(`Duplicate intrinsic identity: ${id}`);
127219
- realm.set(id, value);
127220
- if (!identities.has(value)) identities.set(value, id);
127221
- if (visited.has(value)) continue;
127222
- visited.add(value);
127223
- const owner = isSandboxClosure(value) ? value.properties : value;
127224
- if (owner === void 0) continue;
127225
- for (const key3 of Reflect.ownKeys(owner)) {
127226
- if (typeof key3 === "symbol" && internalSymbols.has(key3)) continue;
127227
- const descriptor = Object.getOwnPropertyDescriptor(owner, key3);
127228
- const symbolName = typeof key3 === "symbol" ? Object.entries(wellKnownSymbols).find(([, symbol2]) => symbol2 === key3)?.[0] : void 0;
127229
- if (typeof key3 === "symbol" && symbolName === void 0)
127230
- throw new TypeError("Intrinsic symbol keys must be well-known symbols.");
127231
- const member = [...path193, typeof key3 === "string" ? key3 : { symbol: symbolName }];
127232
- if ("value" in descriptor) pending.push([member, descriptor.value]);
127233
- for (const kind of ["get", "set"]) {
127234
- const closure = accessorClosure(descriptor[kind]);
127235
- if (closure !== void 0) pending.push([[...member, kind], closure]);
127236
- }
127237
- }
127238
- }
127239
- }
127240
- function getIntrinsicIdentity(value) {
127241
- return identities.get(value);
127242
- }
127243
- function resolveIntrinsicIdentity(budget, id) {
127244
- const value = realms2.get(budget)?.get(id);
127245
- if (value === void 0) throw new TypeError(`Unknown intrinsic identity: ${id}`);
127246
- return value;
127247
- }
127248
- function listIntrinsicIdentities(budget) {
127249
- return [...realms2.get(budget)?.keys() ?? []];
127250
- }
127251
- function releaseIntrinsicIdentities(budget) {
127252
- realms2.delete(budget);
127253
- }
127254
- var identities, realms2;
127255
- var init_intrinsics = __esm({
127256
- "packages/safe-js/src/interp/intrinsics.ts"() {
127257
- "use strict";
127258
- init_accessors();
127259
- init_symbols2();
127260
- init_internal_symbols();
127261
- init_values();
127262
- identities = /* @__PURE__ */ new WeakMap();
127263
- realms2 = /* @__PURE__ */ new WeakMap();
127264
- }
127265
- });
127266
-
127267
127267
  // packages/safe-js/src/extensions.ts
127268
127268
  import { types as types3 } from "node:util";
127269
127269
  function readDataRecord(value, label) {
@@ -127668,6 +127668,15 @@ var init_host_capabilities = __esm({
127668
127668
  }
127669
127669
  });
127670
127670
 
127671
+ // packages/safe-js/src/interp/error-prototypes.ts
127672
+ var errorPrototypes;
127673
+ var init_error_prototypes = __esm({
127674
+ "packages/safe-js/src/interp/error-prototypes.ts"() {
127675
+ "use strict";
127676
+ errorPrototypes = /* @__PURE__ */ new WeakMap();
127677
+ }
127678
+ });
127679
+
127671
127680
  // packages/safe-js/src/interp/object-model.ts
127672
127681
  function registerGuestClosure(closure) {
127673
127682
  guestClosures.add(closure);
@@ -127873,11 +127882,17 @@ function releaseObjectPrototype(budget) {
127873
127882
  arrayPrototypes.delete(budget);
127874
127883
  functionPrototypes.delete(budget);
127875
127884
  generatorPrototypes.delete(budget);
127885
+ errorPrototypes.delete(budget);
127876
127886
  initialRegexDescriptors.delete(budget);
127877
127887
  intrinsicPrototypes.delete(budget);
127878
127888
  }
127879
127889
  function getSandboxPrototype(value, budget) {
127880
127890
  if (prototypes.has(value)) return prototypes.get(value) ?? null;
127891
+ const errorType = sandboxErrorTypes.get(value);
127892
+ if (budget !== void 0 && errorType !== void 0) {
127893
+ const prototype = errorPrototypes.get(budget)?.get(errorType);
127894
+ if (prototype !== void 0) return prototype;
127895
+ }
127881
127896
  if (budget !== void 0 && isGuestClosure(value) && runResources.getStore()?.functionSourceText !== false)
127882
127897
  return functionPrototypes.get(budget) ?? null;
127883
127898
  if (budget !== void 0 && Array.isArray(value)) return arrayPrototypes.get(budget) ?? null;
@@ -128014,6 +128029,8 @@ var init_object_model = __esm({
128014
128029
  init_date();
128015
128030
  init_accessors();
128016
128031
  init_host_capabilities();
128032
+ init_error_prototypes();
128033
+ init_shape();
128017
128034
  init_boxed();
128018
128035
  init_values();
128019
128036
  guestClosures = /* @__PURE__ */ new WeakSet();
@@ -136403,7 +136420,7 @@ var init_float32array2 = __esm({
136403
136420
  // packages/safe-js/src/interp/globals/error.ts
136404
136421
  function createErrorGlobals(options) {
136405
136422
  return Object.fromEntries(
136406
- errorNames.map((name) => [name, createErrorConstructor(name, options.budget)])
136423
+ errorNames.map((name) => [name, createErrorConstructor(name, options.budget, options.errorPrototypes === true)])
136407
136424
  );
136408
136425
  }
136409
136426
  function isSandboxErrorConstructorInstance2(value, constructor) {
@@ -136416,12 +136433,100 @@ function isSandboxErrorConstructorInstance2(value, constructor) {
136416
136433
  }
136417
136434
  return isSandboxErrorConstructorInstance(value, name);
136418
136435
  }
136419
- function createErrorConstructor(name, budget) {
136420
- const call = (args, context) => createSubsetError(name, args, context?.stack ?? [], budget);
136421
- const closure = createSandboxClosure({ sandbox: true, call, construct: call, name });
136436
+ function createErrorConstructor(name, budget, guest) {
136437
+ const call = (args, context) => errorPrototypes.has(budget) ? createNativeError(name, args, budget, context, closure) : createSubsetError(name, args, context?.stack ?? [], budget);
136438
+ const closure = createSandboxClosure({ guest, sandbox: true, call, construct: call, name, length: name === "AggregateError" ? 2 : 1 });
136422
136439
  errorConstructorNames.set(closure, name);
136423
136440
  return closure;
136424
136441
  }
136442
+ function createErrorPrototypes(budget, constructors2) {
136443
+ const prototypes2 = /* @__PURE__ */ new Map();
136444
+ for (const name of errorNames) prototypes2.set(name, /* @__PURE__ */ Object.create(null));
136445
+ errorPrototypes.set(budget, prototypes2);
136446
+ for (const name of errorNames) {
136447
+ const constructor = constructors2[name];
136448
+ const prototype = prototypes2.get(name);
136449
+ Object.defineProperties(prototype, {
136450
+ name: { value: name, writable: true, configurable: true },
136451
+ message: { value: "", writable: true, configurable: true },
136452
+ constructor: { value: constructor, writable: true, configurable: true }
136453
+ });
136454
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
136455
+ setSandboxPrototype(prototype, name === "Error" ? getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget) : prototypes2.get("Error"));
136456
+ if (name !== "Error") setSandboxPrototype(constructor, constructors2.Error);
136457
+ }
136458
+ Object.defineProperty(prototypes2.get("Error"), "toString", {
136459
+ writable: true,
136460
+ configurable: true,
136461
+ value: createSandboxClosure({
136462
+ guest: true,
136463
+ sandbox: true,
136464
+ name: "toString",
136465
+ length: 0,
136466
+ call: async (_args, context) => {
136467
+ const receiver = context?.thisValue;
136468
+ if (receiver === null || typeof receiver !== "object") throw new TypeError("Error.prototype.toString requires an object.");
136469
+ let name;
136470
+ const release2 = retainValues(budget, () => [receiver, name]);
136471
+ try {
136472
+ const rawName = await readErrorProperty(receiver, "name", budget, context);
136473
+ name = rawName === void 0 ? "Error" : await sandboxString(rawName, budget, context);
136474
+ const rawMessage = await readErrorProperty(receiver, "message", budget, context);
136475
+ const message2 = rawMessage === void 0 ? "" : await sandboxString(rawMessage, budget, context);
136476
+ return budget.allocateString(name === "" ? message2 : message2 === "" ? name : `${name}: ${message2}`);
136477
+ } finally {
136478
+ release2();
136479
+ }
136480
+ }
136481
+ })
136482
+ });
136483
+ registerBuiltinIdentities(budget, Object.fromEntries(errorNames.map((name) => [name, constructors2[name]])));
136484
+ for (const name of errorNames) {
136485
+ registerIntrinsicFunction(budget, constructors2[name]);
136486
+ registerIntrinsicObject(budget, prototypes2.get(name));
136487
+ }
136488
+ }
136489
+ async function readErrorProperty(value, key3, budget, context) {
136490
+ if (context?.getProperty !== void 0) return context.getProperty(value, key3);
136491
+ const descriptor = getSandboxPropertyDescriptor(value, key3, budget);
136492
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, value, context);
136493
+ }
136494
+ async function createNativeError(name, args, budget, context, constructor) {
136495
+ const newTarget = context?.newTarget ?? constructor;
136496
+ const candidate = await readErrorProperty(newTarget, "prototype", budget, context);
136497
+ const prototype = candidate !== null && typeof candidate === "object" ? candidate : errorPrototypes.get(budget).get(name);
136498
+ let error3;
136499
+ let iterator;
136500
+ const errors2 = [];
136501
+ const release2 = retainValues(budget, () => [prototype, error3, iterator?.retainedValue, ...errors2, ...args]);
136502
+ try {
136503
+ const message2 = name === "AggregateError" ? args[1] : args[0];
136504
+ const options = name === "AggregateError" ? args[2] : args[1];
136505
+ const text5 = message2 === void 0 ? void 0 : await sandboxString(message2, budget, context);
136506
+ error3 = createSubsetErrorValue(name, text5, context?.stack ?? [], budget);
136507
+ setSandboxPrototype(error3, prototype, budget);
136508
+ if (options !== null && typeof options === "object" && getSandboxPropertyDescriptor(options, "cause", budget) !== void 0) {
136509
+ const cause = await readErrorProperty(options, "cause", budget, context);
136510
+ Object.defineProperty(error3, "cause", { value: cause, writable: true, configurable: true });
136511
+ }
136512
+ if (name === "AggregateError") {
136513
+ iterator = context === void 0 ? getSandboxIterator(args[0], budget) : await acquireSandboxIterator(args[0], budget, context);
136514
+ if (iterator === void 0) throw new TypeError("AggregateError errors must be iterable.");
136515
+ while (true) {
136516
+ budget.visitNode();
136517
+ const result = await iterator.next();
136518
+ if (result === null || typeof result !== "object") throw new TypeError("Iterator result must be an object.");
136519
+ if ((await readIteratorResult(iterator, result, "done")).value) break;
136520
+ budget.allocateArrayLength(errors2.length + 1);
136521
+ errors2.push((await readIteratorResult(iterator, result, "value")).value);
136522
+ }
136523
+ Object.defineProperty(error3, "errors", { value: errors2, writable: true, configurable: true });
136524
+ }
136525
+ return error3;
136526
+ } finally {
136527
+ release2();
136528
+ }
136529
+ }
136425
136530
  function createSubsetError(name, args, stackFrames, budget) {
136426
136531
  const message2 = name === "AggregateError" ? args[1] : args[0];
136427
136532
  const options = name === "AggregateError" ? args[2] : args[1];
@@ -136443,6 +136548,13 @@ var errorConstructorNames, errorNames;
136443
136548
  var init_error = __esm({
136444
136549
  "packages/safe-js/src/interp/globals/error.ts"() {
136445
136550
  "use strict";
136551
+ init_error_prototypes();
136552
+ init_object_model();
136553
+ init_intrinsics();
136554
+ init_string_coercion();
136555
+ init_resources();
136556
+ init_accessors();
136557
+ init_iteration();
136446
136558
  init_exceptions();
136447
136559
  init_values();
136448
136560
  errorConstructorNames = /* @__PURE__ */ new WeakMap();
@@ -136479,7 +136591,7 @@ async function ordinaryHasInstance(value, constructor, budget, context) {
136479
136591
  return evaluateInstanceof(value, constructor.boundTarget, budget, context);
136480
136592
  }
136481
136593
  if (isFloat32ArrayConstructor(constructor)) return isFloat32Array(value);
136482
- if (isSandboxErrorConstructorInstance2(value, constructor)) return true;
136594
+ if ((!isGuestClosure(constructor) || value !== null && typeof value === "object" && !hasExplicitSandboxPrototype(value)) && isSandboxErrorConstructorInstance2(value, constructor)) return true;
136483
136595
  if (!isGuestClosure(constructor) || typeof value !== "object" || value === null) return false;
136484
136596
  const prototype = await readInstanceProperty(constructor, "prototype", budget, context);
136485
136597
  if (typeof prototype !== "object" || prototype === null)
@@ -140820,11 +140932,10 @@ function surfaceThrownValue(reason, budget, stackFrames = [], span) {
140820
140932
  throw reason;
140821
140933
  }
140822
140934
  if (isSubsetErrorValue(reason)) {
140823
- normalizeSurfacedSubsetError(reason, budget, stackFrames, span);
140824
- return reason;
140935
+ return normalizeSurfacedSubsetError(reason, budget, stackFrames, span);
140825
140936
  }
140826
140937
  if (reason instanceof Error) {
140827
- const error3 = createSubsetErrorValue(
140938
+ const error4 = createSubsetErrorValue(
140828
140939
  reason.name || "Error",
140829
140940
  reason.message,
140830
140941
  stackFrames,
@@ -140835,11 +140946,10 @@ function surfaceThrownValue(reason, budget, stackFrames = [], span) {
140835
140946
  span
140836
140947
  }
140837
140948
  );
140838
- normalizeSurfacedSubsetError(error3, budget, stackFrames, span);
140839
- return error3;
140949
+ return normalizeSurfacedSubsetError(error4, budget, stackFrames, span);
140840
140950
  }
140841
140951
  if (isErrorLikeValue(reason)) {
140842
- const error3 = createSubsetErrorValue(
140952
+ const error4 = createSubsetErrorValue(
140843
140953
  reason.name || "Error",
140844
140954
  reason.message,
140845
140955
  stackFrames,
@@ -140850,13 +140960,13 @@ function surfaceThrownValue(reason, budget, stackFrames = [], span) {
140850
140960
  span
140851
140961
  }
140852
140962
  );
140853
- normalizeSurfacedSubsetError(error3, budget, stackFrames, span);
140854
- return error3;
140963
+ return normalizeSurfacedSubsetError(error4, budget, stackFrames, span);
140855
140964
  }
140856
- return createSubsetErrorValue("Error", describeThrownValue(reason), stackFrames, budget, {
140965
+ const error3 = createSubsetErrorValue("Error", describeThrownValue(reason), stackFrames, budget, {
140857
140966
  chargeBudget: false,
140858
140967
  span
140859
140968
  });
140969
+ return normalizeSurfacedSubsetError(error3, budget, stackFrames, span);
140860
140970
  }
140861
140971
  function createSubsetErrorValue(name, message2, stackFrames, budget, options = {}) {
140862
140972
  const resumeChecks = options.chargeBudget === false ? budget.suspendChecks() : void 0;
@@ -140865,11 +140975,15 @@ function createSubsetErrorValue(name, message2, stackFrames, budget, options = {
140865
140975
  const errorMessage9 = budget.allocateString(coerceErrorMessage(message2));
140866
140976
  const header = errorMessage9 === "" ? errorName : `${errorName}: ${errorMessage9}`;
140867
140977
  const stack = budget.allocateString([header, ...[...stackFrames].reverse()].join("\n"));
140868
- const error3 = {
140869
- name: errorName,
140870
- message: errorMessage9,
140871
- stack
140872
- };
140978
+ const prototype = options.transport ? void 0 : errorPrototypes.get(budget)?.get(toSandboxErrorName(errorName));
140979
+ const error3 = prototype === void 0 ? { name: errorName, message: errorMessage9, stack } : {};
140980
+ if (prototype !== void 0) {
140981
+ if (message2 !== void 0) Object.defineProperty(error3, "message", { value: errorMessage9, writable: true, configurable: true });
140982
+ if (!errorPrototypes.get(budget).has(errorName))
140983
+ Object.defineProperty(error3, "name", { value: errorName, writable: true, configurable: true });
140984
+ Object.defineProperty(error3, "stack", { value: stack, writable: true, configurable: true });
140985
+ setSandboxPrototype(error3, prototype, budget);
140986
+ }
140873
140987
  sandboxErrorTypes.set(error3, toSandboxErrorName(errorName));
140874
140988
  attachErrorSpan(error3, options.span);
140875
140989
  attachWrappedErrorCause(error3, options.cause);
@@ -140890,6 +141004,7 @@ function isSubsetErrorValue(value) {
140890
141004
  if (typeof value !== "object" || value === null) {
140891
141005
  return false;
140892
141006
  }
141007
+ if (sandboxErrorTypes.has(value)) return true;
140893
141008
  const prototype = Object.getPrototypeOf(value);
140894
141009
  return (prototype === Object.prototype || prototype === null) && typeof value.name === "string" && typeof value.message === "string" && typeof value.stack === "string";
140895
141010
  }
@@ -140899,21 +141014,37 @@ function normalizeSurfacedSubsetError(error3, budget, stackFrames, span) {
140899
141014
  const name = budget.allocateString(toSandboxErrorName(readErrorName(error3)));
140900
141015
  const message2 = budget.allocateString(readSurfacedErrorMessage(error3, name));
140901
141016
  const frames = readSandboxStackFrames(error3.stack);
141017
+ if (!Object.isExtensible(error3) || ["name", "message", "stack"].some((key3) => {
141018
+ const descriptor = Object.getOwnPropertyDescriptor(error3, key3);
141019
+ return descriptor !== void 0 && (!("value" in descriptor) || !descriptor.writable);
141020
+ })) {
141021
+ const original = error3;
141022
+ const descriptors2 = Object.getOwnPropertyDescriptors(original);
141023
+ delete descriptors2.name;
141024
+ delete descriptors2.message;
141025
+ delete descriptors2.stack;
141026
+ error3 = Object.defineProperties({}, descriptors2);
141027
+ const errorType = sandboxErrorTypes.get(original);
141028
+ if (errorType !== void 0) sandboxErrorTypes.set(error3, errorType);
141029
+ }
140902
141030
  error3.name = name;
140903
141031
  error3.message = message2;
140904
141032
  error3.stack = budget.allocateString(
140905
141033
  formatErrorStack(name, message2, frames.length > 0 ? frames : [...stackFrames].reverse())
140906
141034
  );
140907
141035
  attachErrorSpan(error3, readErrorSpan(error3) ?? span);
141036
+ return error3;
140908
141037
  } finally {
140909
141038
  resumeChecks();
140910
141039
  }
140911
141040
  }
140912
141041
  function readErrorName(error3) {
140913
- return typeof error3.name === "string" && error3.name.length > 0 ? error3.name : "Error";
141042
+ const name = getSandboxDataProperty(error3, "name");
141043
+ return typeof name === "string" && name.length > 0 ? name : sandboxErrorTypes.get(error3) ?? "Error";
140914
141044
  }
140915
141045
  function readSurfacedErrorMessage(error3, name) {
140916
- const message2 = typeof error3.message === "string" ? error3.message : "";
141046
+ const value = getSandboxDataProperty(error3, "message");
141047
+ const message2 = typeof value === "string" ? value : "";
140917
141048
  if (message2 === "") {
140918
141049
  return `${name} thrown`;
140919
141050
  }
@@ -141202,6 +141333,7 @@ var init_exceptions = __esm({
141202
141333
  init_object2();
141203
141334
  init_resources();
141204
141335
  init_object_model();
141336
+ init_error_prototypes();
141205
141337
  init_property_key();
141206
141338
  init_internal_symbols();
141207
141339
  init_resume_target();
@@ -142858,7 +142990,7 @@ function measureSandboxData(values, options = {}) {
142858
142990
  const descriptor = Object.getOwnPropertyDescriptor(value, key3);
142859
142991
  if (descriptor !== void 0) descriptors2.push([key3, descriptor]);
142860
142992
  }
142861
- const includeNonEnumerable = isSandboxDate(value) || hasManagedDescriptors(value);
142993
+ const includeNonEnumerable = isSandboxDate(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
142862
142994
  for (const [key3, descriptor] of descriptors2) {
142863
142995
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
142864
142996
  usage += 1 + key3.length;
@@ -143191,6 +143323,53 @@ function copyFromSandbox(value, state, path193 = "<root>", options, depth = 0) {
143191
143323
  return options.unwrapHostObject(value);
143192
143324
  }
143193
143325
  if (nodeTypes.isProxy(value)) throw new TypeError("Unsupported proxy sandbox value.");
143326
+ if (sandboxErrorTypes.has(value) && hasExplicitSandboxPrototype(value)) {
143327
+ const prototype = getSandboxPrototype(value);
143328
+ let nativePrototype = null;
143329
+ if (prototype !== null) {
143330
+ const identity = getIntrinsicIdentity(prototype);
143331
+ const nativePrototypes = {
143332
+ Error: Error.prototype,
143333
+ TypeError: TypeError.prototype,
143334
+ RangeError: RangeError.prototype,
143335
+ ReferenceError: ReferenceError.prototype,
143336
+ SyntaxError: SyntaxError.prototype,
143337
+ URIError: URIError.prototype,
143338
+ EvalError: EvalError.prototype,
143339
+ AggregateError: AggregateError.prototype
143340
+ };
143341
+ const path194 = identity === void 0 ? [] : JSON.parse(identity);
143342
+ if (path194.length !== 2 || typeof path194[0] !== "string" || path194[1] !== "prototype" || !Object.hasOwn(nativePrototypes, path194[0]))
143343
+ throw new TypeError("Custom Error prototype links cannot be copied as data.");
143344
+ nativePrototype = nativePrototypes[path194[0]];
143345
+ let current = prototype;
143346
+ let prototypeDepth = 0;
143347
+ while (current !== null) {
143348
+ assertSandboxDataDepth(++prototypeDepth);
143349
+ const objectPrototype = getIntrinsicIdentity(current) === '["Object","prototype"]';
143350
+ const owner = objectPrototype ? Object.getOwnPropertyDescriptor(current, "constructor")?.value : current;
143351
+ if (owner === null || typeof owner !== "object" || objectPrototype && getIntrinsicIdentity(owner) !== '["Object"]' || hasGuestObjectState(owner))
143352
+ throw new TypeError("Modified Error prototype links cannot be copied as data.");
143353
+ current = getSandboxPrototype(current);
143354
+ }
143355
+ }
143356
+ const existing = state.seen.get(value);
143357
+ if (existing !== void 0) return existing;
143358
+ const descriptors2 = Reflect.ownKeys(value).filter((key3) => typeof key3 !== "symbol" || !internalSymbols.has(key3)).map((key3) => [key3, Object.getOwnPropertyDescriptor(value, key3)]);
143359
+ if (descriptors2.some(([, descriptor]) => !("value" in descriptor)))
143360
+ throw new TypeError("Error accessor properties cannot be copied as data.");
143361
+ const copy = new Error();
143362
+ delete copy.stack;
143363
+ Object.setPrototypeOf(copy, nativePrototype);
143364
+ state.seen.set(value, copy);
143365
+ for (const [key3, descriptor] of descriptors2)
143366
+ Object.defineProperty(copy, key3, {
143367
+ ...descriptor,
143368
+ value: copyFromSandbox(descriptor.value, state, joinPath(path193, key3), options, depth + 1)
143369
+ });
143370
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
143371
+ return copy;
143372
+ }
143194
143373
  if (!isSandboxClosure(value) && hasGuestObjectState(value)) {
143195
143374
  throw new TypeError("Guest prototype links and custom descriptors cannot be copied as data.");
143196
143375
  }
@@ -143577,6 +143756,7 @@ var init_values = __esm({
143577
143756
  "use strict";
143578
143757
  init_otel2();
143579
143758
  init_internal_symbols();
143759
+ init_intrinsics();
143580
143760
  init_generator_properties();
143581
143761
  init_regexp_properties();
143582
143762
  init_collection_properties();
@@ -143840,8 +144020,14 @@ function captureGuestHeapNode(value, encode) {
143840
144020
  }
143841
144021
  if (isLiveCapability(value) || isSandboxClosure(value) || isSandboxBox(value) || isSandboxDate(value) || isSandboxRegex(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxPromise(value) || isSandboxGenerator(value) || isSandboxArguments(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value)) return void 0;
143842
144022
  const prototype = Object.getPrototypeOf(value);
143843
- if ((prototype === null || prototype === Object.prototype) && hasGuestObjectState(value))
143844
- return { kind: "guest-object", state: captureObjectState(value, encode) };
144023
+ if ((prototype === null || prototype === Object.prototype) && hasGuestObjectState(value)) {
144024
+ const errorType = sandboxErrorTypes.get(value);
144025
+ return {
144026
+ kind: "guest-object",
144027
+ state: captureObjectState(value, encode),
144028
+ ...errorType === void 0 ? {} : { errorType }
144029
+ };
144030
+ }
143845
144031
  return void 0;
143846
144032
  }
143847
144033
  if (origin.node.nodeId === void 0) throw new TypeError("Guest closures require an AST node identity.");
@@ -143879,6 +144065,7 @@ var init_guest_heap = __esm({
143879
144065
  "use strict";
143880
144066
  init_closure_origin();
143881
144067
  init_bound_function_state();
144068
+ init_shape();
143882
144069
  init_generator_properties();
143883
144070
  init_intrinsics();
143884
144071
  init_object_model();
@@ -144910,9 +145097,11 @@ function createHostErrorValue(reason, stackFrames, budget, span, state = { seen:
144910
145097
  }
144911
145098
  const error3 = reason instanceof Error ? createSubsetErrorValue(reason.name, reason.message, stackFrames, budget, {
144912
145099
  cause: reason,
144913
- chargeBudget
145100
+ chargeBudget,
145101
+ transport: true
144914
145102
  }) : createSubsetErrorValue("Error", describeThrownReason(reason), stackFrames, budget, {
144915
- chargeBudget: false
145103
+ chargeBudget: false,
145104
+ transport: true
144916
145105
  });
144917
145106
  if (reason instanceof Error) {
144918
145107
  state.seen.set(reason, error3);
@@ -147452,7 +147641,7 @@ function createBuiltinBindings(options) {
147452
147641
  Date: createDateGlobal(options),
147453
147642
  Symbol: createSymbolGlobal(options.budget),
147454
147643
  BigInt: createBigIntGlobal(options.budget),
147455
- ...createErrorGlobals(options),
147644
+ ...createErrorGlobals({ ...options, errorPrototypes: options.errorPrototypes !== false }),
147456
147645
  ...createMathGlobals({ random: options.random, budget: options.budget }),
147457
147646
  ...createObjectArrayGlobals(options),
147458
147647
  ...createMiscGlobals(options),
@@ -147461,6 +147650,7 @@ function createBuiltinBindings(options) {
147461
147650
  ...createRegexGlobals(options)
147462
147651
  };
147463
147652
  createFunctionPrototype(options.budget, options.functionHasInstance);
147653
+ if (options.errorPrototypes !== false) createErrorPrototypes(options.budget, bindings);
147464
147654
  createGeneratorPrototypes(options.budget);
147465
147655
  registerBuiltinIdentities(options.budget, bindings);
147466
147656
  return bindings;
@@ -147600,7 +147790,9 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
147600
147790
  }
147601
147791
  state(node.state);
147602
147792
  } else if (node.kind === "guest-object" || node.kind === "guest-array") {
147603
- fields(node, ["kind", "state"], node.kind === "guest-array" ? ["templateNodeId", "templateOwner"] : []);
147793
+ fields(node, ["kind", "state"], node.kind === "guest-array" ? ["templateNodeId", "templateOwner"] : ["errorType"]);
147794
+ if (Object.hasOwn(node, "errorType") && !sandboxErrorNames.includes(node.errorType))
147795
+ throw new TypeError("Invalid guest error type.");
147604
147796
  if (Object.hasOwn(node, "templateOwner")) reference(node.templateOwner, ["guest-array"]);
147605
147797
  if (Object.hasOwn(node, "templateNodeId") && integer2(node.templateNodeId) < 1)
147606
147798
  throw new TypeError("Invalid template source identity.");
@@ -147885,6 +148077,7 @@ var init_guest_heap_validation = __esm({
147885
148077
  "packages/safe-js/src/snapshot/guest-heap-validation.ts"() {
147886
148078
  "use strict";
147887
148079
  init_budget2();
148080
+ init_shape();
147888
148081
  init_raw_json();
147889
148082
  init_globals();
147890
148083
  init_intrinsics();
@@ -148564,7 +148757,7 @@ function validateArrayHeap(record4, path193, state) {
148564
148757
  }
148565
148758
  function validateErrorType(record4, path193) {
148566
148759
  if (!Object.hasOwn(record4, "errorType")) return;
148567
- if (record4.kind !== "object" || !sandboxErrorNames.includes(record4.errorType)) {
148760
+ if (record4.kind !== "object" && record4.kind !== "guest-object" || !sandboxErrorNames.includes(record4.errorType)) {
148568
148761
  fail2("invalidValue", `${path193}.errorType`, "invalid error metadata");
148569
148762
  }
148570
148763
  }