poe-code 14.0.80 → 14.0.81

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 (40) hide show
  1. package/dist/index.js +500 -221
  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-34G5WW2O.js → chunk-2LNBLFJ3.js} +2 -2
  6. package/packages/safe-js/dist/chunks/{chunk-M4L3EZHL.js → chunk-ZSYBRZU6.js} +431 -206
  7. package/packages/safe-js/dist/chunks/chunk-ZSYBRZU6.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/async.d.ts +1 -1
  12. package/packages/safe-js/dist/interp/async.js +65 -43
  13. package/packages/safe-js/dist/interp/bound-function-state.d.ts +7 -0
  14. package/packages/safe-js/dist/interp/bound-function-state.js +1 -0
  15. package/packages/safe-js/dist/interp/bound-function.d.ts +4 -0
  16. package/packages/safe-js/dist/interp/bound-function.js +18 -0
  17. package/packages/safe-js/dist/interp/generator-prototypes.d.ts +6 -0
  18. package/packages/safe-js/dist/interp/generator-prototypes.js +1 -0
  19. package/packages/safe-js/dist/interp/globals/function.d.ts +1 -1
  20. package/packages/safe-js/dist/interp/globals/function.js +10 -2
  21. package/packages/safe-js/dist/interp/globals/generator.d.ts +2 -0
  22. package/packages/safe-js/dist/interp/globals/generator.js +51 -0
  23. package/packages/safe-js/dist/interp/globals/object-array.js +19 -1
  24. package/packages/safe-js/dist/interp/globals.d.ts +1 -0
  25. package/packages/safe-js/dist/interp/globals.js +3 -1
  26. package/packages/safe-js/dist/interp/instanceof.d.ts +4 -0
  27. package/packages/safe-js/dist/interp/instanceof.js +58 -0
  28. package/packages/safe-js/dist/interp/interpreter.js +3 -40
  29. package/packages/safe-js/dist/interp/iteration.js +44 -6
  30. package/packages/safe-js/dist/interp/methods/function.d.ts +1 -1
  31. package/packages/safe-js/dist/interp/methods/function.js +53 -40
  32. package/packages/safe-js/dist/interp/methods/generator.d.ts +4 -1
  33. package/packages/safe-js/dist/interp/methods/generator.js +12 -6
  34. package/packages/safe-js/dist/interp/object-model.js +2 -0
  35. package/packages/safe-js/dist/snapshot/guest-heap-validation.js +24 -2
  36. package/packages/safe-js/dist/snapshot/guest-heap.d.ts +8 -0
  37. package/packages/safe-js/dist/snapshot/guest-heap.js +7 -0
  38. package/packages/safe-js/dist/snapshot/restore.js +53 -7
  39. package/packages/safe-js/dist/chunks/chunk-M4L3EZHL.js.map +0 -7
  40. /package/packages/safe-js/dist/chunks/{chunk-34G5WW2O.js.map → chunk-2LNBLFJ3.js.map} +0 -0
package/dist/index.js CHANGED
@@ -127116,6 +127116,15 @@ var init_graph_depth = __esm({
127116
127116
  }
127117
127117
  });
127118
127118
 
127119
+ // packages/safe-js/src/interp/generator-prototypes.ts
127120
+ var generatorPrototypes;
127121
+ var init_generator_prototypes = __esm({
127122
+ "packages/safe-js/src/interp/generator-prototypes.ts"() {
127123
+ "use strict";
127124
+ generatorPrototypes = /* @__PURE__ */ new WeakMap();
127125
+ }
127126
+ });
127127
+
127119
127128
  // packages/safe-js/src/interp/resources.ts
127120
127129
  import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
127121
127130
  function retainValues(budget, values) {
@@ -127863,6 +127872,7 @@ function releaseObjectPrototype(budget) {
127863
127872
  datePrototypes.delete(budget);
127864
127873
  arrayPrototypes.delete(budget);
127865
127874
  functionPrototypes.delete(budget);
127875
+ generatorPrototypes.delete(budget);
127866
127876
  initialRegexDescriptors.delete(budget);
127867
127877
  intrinsicPrototypes.delete(budget);
127868
127878
  }
@@ -127997,6 +128007,7 @@ var init_object_model = __esm({
127997
128007
  "use strict";
127998
128008
  init_graph_depth();
127999
128009
  init_generator_properties();
128010
+ init_generator_prototypes();
128000
128011
  init_resources();
128001
128012
  init_intrinsics();
128002
128013
  init_template_objects();
@@ -130945,13 +130956,12 @@ async function acquireSandboxIterator(value, budget, context, asyncProtocol = fa
130945
130956
  return asyncProtocol ? getSandboxAsyncIterator(value, budget, context, signal) : getSandboxIterator(value, budget, context);
130946
130957
  if (getSandboxPropertyDescriptor(value, key3, budget) === void 0 && !(isSandboxRegExpIterator(value) && !asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)) {
130947
130958
  if (isSandboxGenerator(value)) {
130948
- if (hasExplicitSandboxPrototype(value)) return void 0;
130949
- if (value.async === true === asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)
130959
+ if (!hasExplicitSandboxPrototype(value) && value.async === true === asyncProtocol && getSandboxPropertyDescriptor(value, "next", budget) !== void 0)
130950
130960
  return guestIterator(value, await context.getProperty(value, "next"), asyncProtocol, budget, context, signal);
130951
130961
  }
130952
130962
  if (!asyncProtocol && Array.isArray(value) && getSandboxPrototype(value, budget) !== null) return void 0;
130953
130963
  if (!asyncProtocol) return getSandboxIterator(value, budget, context);
130954
- if (isSandboxGenerator(value) && value.async)
130964
+ if (isSandboxGenerator(value) && value.async && !hasExplicitSandboxPrototype(value))
130955
130965
  return getSandboxAsyncIterator(value, budget, context, signal);
130956
130966
  const iterator2 = await acquireSandboxIterator(value, budget, context);
130957
130967
  return iterator2 === void 0 ? void 0 : asyncFromSyncIterator(iterator2, budget, signal, context);
@@ -131013,7 +131023,7 @@ async function readIteratorResult(iterator, result, property) {
131013
131023
  }
131014
131024
  function getSandboxAsyncIterator(value, budget, context, signal) {
131015
131025
  if (isSandboxGenerator(value) && value.async) {
131016
- return hasExplicitSandboxPrototype(value) ? void 0 : { ...generatorObjectIterator(value, budget, context), asyncProtocol: true };
131026
+ return hasExplicitSandboxPrototype(value) ? context?.getProperty === void 0 ? generatorProtocolAdapter(value, budget, context, true, signal) : void 0 : { ...generatorObjectIterator(value, budget, context), asyncProtocol: true };
131017
131027
  }
131018
131028
  if (value !== null && (typeof value === "object" || typeof value === "function") && !isGuestHostObject(value)) {
131019
131029
  const method = value[Symbol.asyncIterator];
@@ -131163,7 +131173,9 @@ function getSandboxIterator(value, budget, context) {
131163
131173
  return syncIterator(Float32Array.prototype.values.call(value));
131164
131174
  }
131165
131175
  if (isSandboxGenerator(value)) {
131166
- return value.async || hasExplicitSandboxPrototype(value) ? void 0 : generatorObjectIterator(value, budget, context);
131176
+ if (value.async) return void 0;
131177
+ if (hasExplicitSandboxPrototype(value)) return context?.getProperty === void 0 ? generatorProtocolAdapter(value, budget ?? new Budget(), context, false) : void 0;
131178
+ return generatorObjectIterator(value, budget, context);
131167
131179
  }
131168
131180
  if (typeof value === "string") {
131169
131181
  const iterator2 = value[Symbol.iterator]();
@@ -131239,6 +131251,38 @@ function collectionIterator(collection, value) {
131239
131251
  adapter.snapshot = () => ({ kind: "builtin", value, index: adapter.snapshotIndex() });
131240
131252
  return adapter;
131241
131253
  }
131254
+ function generatorProtocolAdapter(value, budget, context, asyncProtocol, signal) {
131255
+ const bridge = {
131256
+ ...context,
131257
+ stack: context?.stack ?? [],
131258
+ thisValue: value,
131259
+ invokeClosure: (closure, args, receiver, construct) => invokeBuiltinClosure(closure, args, budget, context, receiver, construct),
131260
+ getProperty: (target, key3) => {
131261
+ const descriptor = getSandboxPropertyDescriptor(target, key3, budget);
131262
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, target, bridge, true);
131263
+ }
131264
+ };
131265
+ let resolved;
131266
+ let pending;
131267
+ const initialize = () => pending ??= acquireSandboxIterator(value, budget, bridge, asyncProtocol, signal).then((iterator) => {
131268
+ if (iterator === void 0) throw new TypeError("Generator does not provide an iterator protocol.");
131269
+ return resolved = iterator;
131270
+ });
131271
+ return {
131272
+ asynchronous: true,
131273
+ ...asyncProtocol ? { asyncProtocol: true } : {},
131274
+ get retainedValue() {
131275
+ return [value, resolved?.retainedValue];
131276
+ },
131277
+ snapshot: () => resolved?.snapshot?.() ?? { kind: "unsupported" },
131278
+ next: async (...args) => (await initialize()).next(...args),
131279
+ getOperation: async (method) => {
131280
+ const iterator = await initialize();
131281
+ return iterator.getOperation === void 0 ? iterator[method] : iterator.getOperation(method);
131282
+ },
131283
+ readResultProperty: async (result, property) => readIteratorResult(await initialize(), result, property)
131284
+ };
131285
+ }
131242
131286
  function generatorObjectIterator(generator, budget, context) {
131243
131287
  const iterator = generatorIterator(generator, budget, context);
131244
131288
  if (context?.getProperty === void 0) return iterator;
@@ -131343,6 +131387,7 @@ var init_iteration = __esm({
131343
131387
  init_builtin_call();
131344
131388
  init_object_model();
131345
131389
  init_intrinsics();
131390
+ init_accessors();
131346
131391
  asyncGeneratorRequests = /* @__PURE__ */ new WeakMap();
131347
131392
  }
131348
131393
  });
@@ -131759,12 +131804,12 @@ async function evaluateFunctionExpression(node, context, evaluateNode3) {
131759
131804
  value: closure
131760
131805
  };
131761
131806
  }
131762
- function createInterpretedClosure(node, context, evaluateNode3, homeObject) {
131807
+ function createInterpretedClosure(node, context, evaluateNode3, homeObject, initializeGeneratorPrototype = true) {
131763
131808
  if (node.type !== "ArrowFunctionExpression") {
131764
131809
  context = { ...context, functionEnvironment: { homeObject } };
131765
131810
  }
131766
131811
  if (node.type !== "ArrowFunctionExpression" && node.generator) {
131767
- return createGeneratorClosure(node, context, evaluateNode3);
131812
+ return createGeneratorClosure(node, context, evaluateNode3, initializeGeneratorPrototype);
131768
131813
  }
131769
131814
  const construct = node.type !== "ArrowFunctionExpression" && !(node.type === "FunctionExpression" && node.method === true) && !node.async ? async (args, callContext) => {
131770
131815
  const thisValue = {};
@@ -131851,7 +131896,8 @@ function executeAsyncFunction(execute2, budget, signal, callContext) {
131851
131896
  });
131852
131897
  return createSandboxPromise(promise, { synchronousPrefix });
131853
131898
  }
131854
- function createGeneratorClosure(node, context, evaluateNode3) {
131899
+ function createGeneratorClosure(node, context, evaluateNode3, initializePrototype) {
131900
+ const prototypes2 = !initializePrototype || runResources.getStore()?.functionSourceText === false ? void 0 : generatorPrototypes.get(context.budget)?.get(node.async === true);
131855
131901
  const closure = createSandboxClosure({
131856
131902
  sourceRange: functionSources.get(node),
131857
131903
  guest: true,
@@ -131861,54 +131907,69 @@ function createGeneratorClosure(node, context, evaluateNode3) {
131861
131907
  ...node.id === void 0 ? { name: context.inferredName } : { name: node.id.name },
131862
131908
  retainedValues: () => [...context.scope.retainedValues(), context.functionEnvironment?.homeObject, context.functionEnvironment?.newTarget],
131863
131909
  call: async (args, callContext) => {
131864
- const closureContext = {
131865
- ...context,
131866
- compilation: callContext?.compilation ?? context.compilation,
131867
- callStack: [...callContext?.stack ?? context.callStack]
131868
- };
131869
- const scope = await createClosureScope(
131870
- node,
131871
- args,
131872
- callContext?.thisValue,
131873
- closureContext,
131874
- evaluateNode3
131875
- );
131876
- const channel = createGeneratorChannel((generatorYield) => {
131877
- const execute2 = async () => {
131878
- const result = await evaluateNode3(node.body, {
131879
- ...closureContext,
131880
- functionBody: node.body,
131881
- asyncGenerator: node.async,
131882
- generatorBlockScopes: /* @__PURE__ */ new Map(),
131883
- generatorExpressionStates: /* @__PURE__ */ new Map(),
131884
- captureGeneratorScope: (suspendedScope, blocks, completions, expressions) => {
131885
- origin.suspendedScope = suspendedScope;
131886
- origin.blockScopes = blocks;
131887
- origin.finallyCompletions = completions;
131888
- origin.expressionStates = expressions;
131889
- },
131890
- generatorYield: (value2, yieldNodeId) => {
131891
- generator.state = "suspended";
131892
- return generatorYield(value2, yieldNodeId);
131893
- },
131894
- scope
131895
- });
131896
- if (result.kind === "error") {
131897
- throw result.error;
131898
- }
131899
- if (result.kind === "throw") {
131900
- throw result.value;
131901
- }
131902
- const value = result.hasValue ? result.value : void 0;
131903
- return node.async ? awaitSandboxValue(value, context.signal, context.budget, createCoercionContext(context)) : value;
131910
+ const candidate = prototypes2 === void 0 ? void 0 : getGuestFunctionProperty(closure, "prototype");
131911
+ const prototype = typeof candidate === "object" && candidate !== null ? candidate : prototypes2?.instancePrototype;
131912
+ const releasePrototype = prototype === void 0 ? void 0 : retainValues(context.budget, () => [prototype]);
131913
+ try {
131914
+ const closureContext = {
131915
+ ...context,
131916
+ compilation: callContext?.compilation ?? context.compilation,
131917
+ callStack: [...callContext?.stack ?? context.callStack]
131904
131918
  };
131905
- return node.async ? runAsyncPrefix(execute2) : execute2();
131906
- });
131907
- const generator = createSandboxGenerator(channel, { async: node.async });
131908
- const origin = registerGeneratorOrigin(generator, node, scope, closureContext);
131909
- return generator;
131919
+ const scope = await createClosureScope(
131920
+ node,
131921
+ args,
131922
+ callContext?.thisValue,
131923
+ closureContext,
131924
+ evaluateNode3
131925
+ );
131926
+ const channel = createGeneratorChannel((generatorYield) => {
131927
+ const execute2 = async () => {
131928
+ const result = await evaluateNode3(node.body, {
131929
+ ...closureContext,
131930
+ functionBody: node.body,
131931
+ asyncGenerator: node.async,
131932
+ generatorBlockScopes: /* @__PURE__ */ new Map(),
131933
+ generatorExpressionStates: /* @__PURE__ */ new Map(),
131934
+ captureGeneratorScope: (suspendedScope, blocks, completions, expressions) => {
131935
+ origin.suspendedScope = suspendedScope;
131936
+ origin.blockScopes = blocks;
131937
+ origin.finallyCompletions = completions;
131938
+ origin.expressionStates = expressions;
131939
+ },
131940
+ generatorYield: (value2, yieldNodeId) => {
131941
+ generator.state = "suspended";
131942
+ return generatorYield(value2, yieldNodeId);
131943
+ },
131944
+ scope
131945
+ });
131946
+ if (result.kind === "error") {
131947
+ throw result.error;
131948
+ }
131949
+ if (result.kind === "throw") {
131950
+ throw result.value;
131951
+ }
131952
+ const value = result.hasValue ? result.value : void 0;
131953
+ return node.async ? awaitSandboxValue(value, context.signal, context.budget, createCoercionContext(context)) : value;
131954
+ };
131955
+ return node.async ? runAsyncPrefix(execute2) : execute2();
131956
+ });
131957
+ const generator = createSandboxGenerator(channel, { async: node.async });
131958
+ if (prototype !== void 0) setSandboxPrototype(generator, prototype, context.budget);
131959
+ const origin = registerGeneratorOrigin(generator, node, scope, closureContext);
131960
+ return generator;
131961
+ } finally {
131962
+ releasePrototype?.();
131963
+ }
131910
131964
  }
131911
131965
  });
131966
+ if (prototypes2 !== void 0) {
131967
+ const prototype = /* @__PURE__ */ Object.create(null);
131968
+ markDescriptorObject(prototype);
131969
+ setSandboxPrototype(prototype, prototypes2.instancePrototype);
131970
+ Object.defineProperty(materializeFunctionProperties(closure), "prototype", { value: prototype, writable: true });
131971
+ setSandboxPrototype(closure, prototypes2.functionPrototype);
131972
+ }
131912
131973
  registerClosureOrigin(closure, node, context);
131913
131974
  return closure;
131914
131975
  }
@@ -132065,6 +132126,8 @@ var init_async = __esm({
132065
132126
  init_var_hoist();
132066
132127
  init_interpreter();
132067
132128
  init_object_model();
132129
+ init_generator_prototypes();
132130
+ init_resources();
132068
132131
  init_function_source();
132069
132132
  init_closure_origin();
132070
132133
  init_bindings();
@@ -134924,6 +134987,32 @@ function createArrayGlobal(budget) {
134924
134987
  length: 0,
134925
134988
  call: (_args, context) => context?.thisValue
134926
134989
  }), writable: true, configurable: true });
134990
+ const iteratorTagGetter = createSandboxClosure({
134991
+ guest: true,
134992
+ sandbox: true,
134993
+ name: "get [Symbol.toStringTag]",
134994
+ length: 0,
134995
+ call: () => "Iterator"
134996
+ });
134997
+ const iteratorTagSetter = createSandboxClosure({
134998
+ guest: true,
134999
+ sandbox: true,
135000
+ name: "set [Symbol.toStringTag]",
135001
+ length: 1,
135002
+ call: ([value], context) => {
135003
+ const receiver = context?.thisValue;
135004
+ if (receiver === iterablePrototype || receiver === null || typeof receiver !== "object")
135005
+ throw new TypeError("Iterator tag setter requires a distinct object receiver.");
135006
+ defineOwnDataProperty(objectProperties(receiver, true), Symbol.toStringTag, value);
135007
+ createDataCheckpoint(budget, context)(receiver, 0, true);
135008
+ return void 0;
135009
+ }
135010
+ });
135011
+ Object.defineProperty(iterablePrototype, Symbol.toStringTag, {
135012
+ get: accessorAdapter(iteratorTagGetter, "get"),
135013
+ set: accessorAdapter(iteratorTagSetter, "set"),
135014
+ configurable: true
135015
+ });
134927
135016
  setSandboxPrototype(iterablePrototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
134928
135017
  setSandboxPrototype(iteratorPrototype, iterablePrototype);
134929
135018
  Object.defineProperties(iteratorPrototype, {
@@ -134937,6 +135026,9 @@ function createArrayGlobal(budget) {
134937
135026
  [Symbol.toStringTag]: { value: "Array Iterator", configurable: true }
134938
135027
  });
134939
135028
  registerBuiltinIdentities(budget, { "%ArrayIteratorPrototype%": iteratorPrototype, "%IteratorPrototype%": iterablePrototype });
135029
+ registerIntrinsicFunction(budget, iterablePrototype[Symbol.iterator]);
135030
+ registerIntrinsicFunction(budget, iteratorTagGetter);
135031
+ registerIntrinsicFunction(budget, iteratorTagSetter);
134940
135032
  registerIntrinsicObject(budget, iterablePrototype);
134941
135033
  registerIntrinsicObject(budget, iteratorPrototype);
134942
135034
  for (const method of ["keys", "values", "entries"]) {
@@ -135718,6 +135810,47 @@ var init_function_string = __esm({
135718
135810
  }
135719
135811
  });
135720
135812
 
135813
+ // packages/safe-js/src/interp/bound-function-state.ts
135814
+ var boundFunctionStates;
135815
+ var init_bound_function_state = __esm({
135816
+ "packages/safe-js/src/interp/bound-function-state.ts"() {
135817
+ "use strict";
135818
+ boundFunctionStates = /* @__PURE__ */ new WeakMap();
135819
+ }
135820
+ });
135821
+
135822
+ // packages/safe-js/src/interp/bound-function.ts
135823
+ function createBoundFunction(state, name, length, callClosure) {
135824
+ const bound = createSandboxClosure({
135825
+ guest: true,
135826
+ sandbox: true,
135827
+ name,
135828
+ length,
135829
+ boundTarget: state.target,
135830
+ retainedValues: () => [state.target, state.thisValue, ...state.args, bound.name],
135831
+ call: (args, context) => callClosure(state.target, [...state.args, ...args], context?.stack ?? [], state.thisValue),
135832
+ ...state.target.construct === void 0 ? {} : {
135833
+ construct: (args, context) => callClosure(
135834
+ state.target,
135835
+ [...state.args, ...args],
135836
+ context?.stack ?? [],
135837
+ void 0,
135838
+ true,
135839
+ context?.newTarget === bound ? state.target : context?.newTarget
135840
+ )
135841
+ }
135842
+ });
135843
+ boundFunctionStates.set(bound, state);
135844
+ return bound;
135845
+ }
135846
+ var init_bound_function = __esm({
135847
+ "packages/safe-js/src/interp/bound-function.ts"() {
135848
+ "use strict";
135849
+ init_values();
135850
+ init_bound_function_state();
135851
+ }
135852
+ });
135853
+
135721
135854
  // packages/safe-js/src/interp/methods/function.ts
135722
135855
  function getFunctionMember(target, property, options) {
135723
135856
  let current = target;
@@ -135768,28 +135901,11 @@ function callFunctionMethod(target, methodName, args, options, stack, context) {
135768
135901
  const thisValue = args[0];
135769
135902
  if (methodName === "bind") {
135770
135903
  const boundArgs = args.slice(1);
135904
+ const explicitPrototype = hasExplicitSandboxPrototype(target);
135905
+ const prototype = getSandboxPrototype(target, options.budget);
135771
135906
  const bind2 = (length, name) => {
135772
- const bound = createSandboxClosure({
135773
- guest: true,
135774
- sandbox: true,
135775
- name: `bound ${name}`,
135776
- length,
135777
- boundTarget: target,
135778
- retainedValues: () => [target, thisValue, ...boundArgs, bound.name],
135779
- call: (callArgs, context2) => options.callClosure(target, [...boundArgs, ...callArgs], context2?.stack ?? [], thisValue),
135780
- ...target.construct === void 0 ? {} : {
135781
- construct: (callArgs, context2) => options.callClosure(
135782
- target,
135783
- [...boundArgs, ...callArgs],
135784
- context2?.stack ?? [],
135785
- void 0,
135786
- true,
135787
- context2?.newTarget === bound ? target : context2?.newTarget
135788
- )
135789
- }
135790
- });
135791
- if (hasExplicitSandboxPrototype(target))
135792
- setSandboxPrototype(bound, getSandboxPrototype(target, options.budget), options.budget);
135907
+ const bound = createBoundFunction({ target, thisValue, args: boundArgs }, `bound ${name}`, length, options.callClosure);
135908
+ if (explicitPrototype) setSandboxPrototype(bound, prototype, options.budget);
135793
135909
  return bound;
135794
135910
  };
135795
135911
  if (context?.getProperty === void 0)
@@ -135798,15 +135914,20 @@ function callFunctionMethod(target, methodName, args, options, stack, context) {
135798
135914
  target.name ?? ""
135799
135915
  );
135800
135916
  return (async () => {
135801
- const properties = target.properties;
135802
- const defaultName = target.name;
135803
- const hasLength = !isGuestClosure(target) || target.properties === void 0 || Object.hasOwn(target.properties, "length");
135804
- const length = hasLength ? await context.getProperty(target, "length") : void 0;
135805
- const name = !isGuestClosure(target) && !Object.hasOwn(properties ?? {}, "name") ? defaultName : await context.getProperty(target, "name");
135806
- return bind2(
135807
- typeof length === "number" && !Number.isNaN(length) ? Math.max(0, Math.trunc(length) - boundArgs.length) : 0,
135808
- typeof name === "string" ? name : ""
135809
- );
135917
+ const release2 = options.budget !== void 0 && explicitPrototype && prototype !== null ? retainValues(options.budget, () => [prototype]) : void 0;
135918
+ try {
135919
+ const properties = target.properties;
135920
+ const defaultName = target.name;
135921
+ const hasLength = !isGuestClosure(target) || target.properties === void 0 || Object.hasOwn(target.properties, "length");
135922
+ const length = hasLength ? await context.getProperty(target, "length") : void 0;
135923
+ const name = !isGuestClosure(target) && !Object.hasOwn(properties ?? {}, "name") ? defaultName : await context.getProperty(target, "name");
135924
+ return bind2(
135925
+ typeof length === "number" && !Number.isNaN(length) ? Math.max(0, Math.trunc(length) - boundArgs.length) : 0,
135926
+ typeof name === "string" ? name : ""
135927
+ );
135928
+ } finally {
135929
+ release2?.();
135930
+ }
135810
135931
  })();
135811
135932
  }
135812
135933
  if (methodName === "call") {
@@ -135816,25 +135937,37 @@ function callFunctionMethod(target, methodName, args, options, stack, context) {
135816
135937
  if (applyArgs === null || applyArgs === void 0) {
135817
135938
  return options.callClosure(target, [], stack, thisValue);
135818
135939
  }
135819
- if (!Array.isArray(applyArgs)) {
135820
- throw new TypeError("Function#apply requires an array or nullish arguments value.");
135940
+ if (typeof applyArgs !== "object") {
135941
+ throw new TypeError("Function#apply requires an object or nullish arguments value.");
135821
135942
  }
135822
- if (context?.getProperty === void 0)
135943
+ if (context?.getProperty === void 0 && Array.isArray(applyArgs))
135823
135944
  return options.callClosure(target, applyArgs, stack, thisValue);
135824
135945
  return (async () => {
135946
+ const budget = options.budget ?? new Budget();
135947
+ const invocationContext = {
135948
+ ...context,
135949
+ stack,
135950
+ thisValue: context?.thisValue,
135951
+ invokeClosure: context?.invokeClosure ?? ((callee, values2, receiver) => Promise.resolve(options.callClosure(callee, values2, stack, receiver)))
135952
+ };
135953
+ const read = (value, key3) => {
135954
+ if (context?.getProperty !== void 0) return context.getProperty(value, key3);
135955
+ const descriptor = getSandboxPropertyDescriptor(value, key3, budget);
135956
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, value, invocationContext);
135957
+ };
135825
135958
  const values = [];
135826
- const length = applyArgs.length;
135827
- options.budget?.allocateArrayLength(length);
135828
- const retained = {};
135829
- options.budget?.setRetainedValues(retained, () => values);
135959
+ const release2 = retainValues(budget, () => [applyArgs, ...values]);
135830
135960
  try {
135961
+ const number = await sandboxNumber(await read(applyArgs, "length"), budget, invocationContext);
135962
+ const length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
135963
+ budget.allocateArrayLength(length);
135831
135964
  for (let index = 0; index < length; index++) {
135832
- options.budget?.visitNode();
135833
- values.push(await context.getProperty(applyArgs, index));
135965
+ budget.visitNode();
135966
+ values.push(await read(applyArgs, String(index)));
135834
135967
  }
135835
135968
  return await options.callClosure(target, values, stack, thisValue);
135836
135969
  } finally {
135837
- options.budget?.setRetainedValues(retained, void 0);
135970
+ release2();
135838
135971
  }
135839
135972
  })();
135840
135973
  }
@@ -135845,8 +135978,12 @@ var init_function = __esm({
135845
135978
  init_values();
135846
135979
  init_object_model();
135847
135980
  init_graph_depth();
135981
+ init_budget2();
135848
135982
  init_function_string();
135849
135983
  init_resources();
135984
+ init_bound_function();
135985
+ init_string_coercion();
135986
+ init_accessors();
135850
135987
  functionMethodNames = /* @__PURE__ */ new Set(["apply", "bind", "call", "toString"]);
135851
135988
  }
135852
135989
  });
@@ -136019,16 +136156,19 @@ function getGeneratorMember(target, property, budget) {
136019
136156
  return createSandboxClosure({
136020
136157
  sandbox: true,
136021
136158
  name: property,
136022
- call: ([value], context) => {
136023
- const iterator = generatorIterator(target, budget, context);
136024
- const result = Promise.resolve(iterator[property](value)).then((result2) => allocateProducedSandboxValue(
136025
- { value: result2.value, done: result2.done === true },
136026
- budget
136027
- ));
136028
- return target.async ? createSandboxPromise(result) : result;
136029
- }
136159
+ call: ([value], context) => callGeneratorMethod(target, property, value, budget, context, target.async === true)
136030
136160
  });
136031
136161
  }
136162
+ function callGeneratorMethod(target, method, value, budget, context, async) {
136163
+ const result = (async () => {
136164
+ if (!isSandboxGenerator(target) || target.async === true !== async)
136165
+ throw new TypeError(`${async ? "AsyncGenerator" : "Generator"}.${method} requires a matching generator receiver.`);
136166
+ const iterator = generatorIterator(target, budget, context);
136167
+ const result2 = await iterator[method](value);
136168
+ return allocateProducedSandboxValue({ value: result2.value, done: result2.done === true }, budget);
136169
+ })();
136170
+ return async ? createSandboxPromise(result) : result;
136171
+ }
136032
136172
  var generatorMethodNames;
136033
136173
  var init_generator2 = __esm({
136034
136174
  "packages/safe-js/src/interp/methods/generator.ts"() {
@@ -136127,89 +136267,6 @@ var init_set = __esm({
136127
136267
  }
136128
136268
  });
136129
136269
 
136130
- // packages/safe-js/src/interp/globals/error.ts
136131
- function createErrorGlobals(options) {
136132
- return Object.fromEntries(
136133
- errorNames.map((name) => [name, createErrorConstructor(name, options.budget)])
136134
- );
136135
- }
136136
- function isSandboxErrorConstructorInstance2(value, constructor) {
136137
- if (!isSandboxClosure(constructor)) {
136138
- throw new TypeError("Right-hand side of 'instanceof' is not a function.");
136139
- }
136140
- const name = errorConstructorNames.get(constructor);
136141
- if (name === void 0) {
136142
- return false;
136143
- }
136144
- return isSandboxErrorConstructorInstance(value, name);
136145
- }
136146
- function createErrorConstructor(name, budget) {
136147
- const call = (args, context) => createSubsetError(name, args, context?.stack ?? [], budget);
136148
- const closure = createSandboxClosure({ sandbox: true, call, construct: call, name });
136149
- errorConstructorNames.set(closure, name);
136150
- return closure;
136151
- }
136152
- function createSubsetError(name, args, stackFrames, budget) {
136153
- const message2 = name === "AggregateError" ? args[1] : args[0];
136154
- const options = name === "AggregateError" ? args[2] : args[1];
136155
- const error3 = createSubsetErrorValue(name, message2, stackFrames, budget);
136156
- if (name === "AggregateError") {
136157
- const errors2 = Array.isArray(args[0]) ? [...args[0]] : [];
136158
- budget.allocateArrayLength(errors2.length);
136159
- error3.errors = errors2;
136160
- }
136161
- if (isObjectLike(options) && Object.prototype.hasOwnProperty.call(options, "cause")) {
136162
- error3.cause = options.cause;
136163
- }
136164
- return error3;
136165
- }
136166
- function isObjectLike(value) {
136167
- return typeof value === "object" && value !== null;
136168
- }
136169
- var errorConstructorNames, errorNames;
136170
- var init_error = __esm({
136171
- "packages/safe-js/src/interp/globals/error.ts"() {
136172
- "use strict";
136173
- init_exceptions();
136174
- init_values();
136175
- errorConstructorNames = /* @__PURE__ */ new WeakMap();
136176
- errorNames = [
136177
- "Error",
136178
- "TypeError",
136179
- "RangeError",
136180
- "ReferenceError",
136181
- "SyntaxError",
136182
- "URIError",
136183
- "EvalError",
136184
- "AggregateError"
136185
- ];
136186
- }
136187
- });
136188
-
136189
- // packages/safe-js/src/interp/methods/collection-iterator.ts
136190
- function getCollectionIteratorMember(target, property, budget) {
136191
- if (Object.hasOwn(target, property)) return target[property];
136192
- if (property !== "next") return void 0;
136193
- const { collectionKind } = collectionIteratorState(target);
136194
- return createSandboxClosure({
136195
- sandbox: true,
136196
- name: "next",
136197
- call: (_args, context) => {
136198
- const receiver = context?.thisValue;
136199
- if (!isSandboxCollectionIterator(receiver) || collectionIteratorState(receiver).collectionKind !== collectionKind)
136200
- throw new TypeError("Iterator next requires a matching collection iterator receiver.");
136201
- return nextCollectionIterator(receiver, budget);
136202
- }
136203
- });
136204
- }
136205
- var init_collection_iterator2 = __esm({
136206
- "packages/safe-js/src/interp/methods/collection-iterator.ts"() {
136207
- "use strict";
136208
- init_collection_iterator();
136209
- init_values();
136210
- }
136211
- });
136212
-
136213
136270
  // packages/safe-js/src/interp/globals/float32array.ts
136214
136271
  function createFloat32ArrayGlobal(budget) {
136215
136272
  const constructor = createSandboxClosure({
@@ -136343,6 +136400,147 @@ var init_float32array2 = __esm({
136343
136400
  }
136344
136401
  });
136345
136402
 
136403
+ // packages/safe-js/src/interp/globals/error.ts
136404
+ function createErrorGlobals(options) {
136405
+ return Object.fromEntries(
136406
+ errorNames.map((name) => [name, createErrorConstructor(name, options.budget)])
136407
+ );
136408
+ }
136409
+ function isSandboxErrorConstructorInstance2(value, constructor) {
136410
+ if (!isSandboxClosure(constructor)) {
136411
+ throw new TypeError("Right-hand side of 'instanceof' is not a function.");
136412
+ }
136413
+ const name = errorConstructorNames.get(constructor);
136414
+ if (name === void 0) {
136415
+ return false;
136416
+ }
136417
+ return isSandboxErrorConstructorInstance(value, name);
136418
+ }
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 });
136422
+ errorConstructorNames.set(closure, name);
136423
+ return closure;
136424
+ }
136425
+ function createSubsetError(name, args, stackFrames, budget) {
136426
+ const message2 = name === "AggregateError" ? args[1] : args[0];
136427
+ const options = name === "AggregateError" ? args[2] : args[1];
136428
+ const error3 = createSubsetErrorValue(name, message2, stackFrames, budget);
136429
+ if (name === "AggregateError") {
136430
+ const errors2 = Array.isArray(args[0]) ? [...args[0]] : [];
136431
+ budget.allocateArrayLength(errors2.length);
136432
+ error3.errors = errors2;
136433
+ }
136434
+ if (isObjectLike(options) && Object.prototype.hasOwnProperty.call(options, "cause")) {
136435
+ error3.cause = options.cause;
136436
+ }
136437
+ return error3;
136438
+ }
136439
+ function isObjectLike(value) {
136440
+ return typeof value === "object" && value !== null;
136441
+ }
136442
+ var errorConstructorNames, errorNames;
136443
+ var init_error = __esm({
136444
+ "packages/safe-js/src/interp/globals/error.ts"() {
136445
+ "use strict";
136446
+ init_exceptions();
136447
+ init_values();
136448
+ errorConstructorNames = /* @__PURE__ */ new WeakMap();
136449
+ errorNames = [
136450
+ "Error",
136451
+ "TypeError",
136452
+ "RangeError",
136453
+ "ReferenceError",
136454
+ "SyntaxError",
136455
+ "URIError",
136456
+ "EvalError",
136457
+ "AggregateError"
136458
+ ];
136459
+ }
136460
+ });
136461
+
136462
+ // packages/safe-js/src/interp/instanceof.ts
136463
+ async function evaluateInstanceof(value, constructor, budget, context) {
136464
+ if (constructor === null || typeof constructor !== "object")
136465
+ throw new TypeError("Right-hand side of 'instanceof' must be an object.");
136466
+ const method = await readInstanceProperty(constructor, Symbol.hasInstance, budget, context);
136467
+ if (method !== void 0 && method !== null) {
136468
+ if (!isSandboxClosure(method)) throw new TypeError("Symbol.hasInstance must be callable.");
136469
+ return Boolean(await invokeBuiltinClosure(method, [value], budget, context, constructor));
136470
+ }
136471
+ if (!isSandboxClosure(constructor))
136472
+ throw new TypeError("Right-hand side of 'instanceof' is not a function.");
136473
+ return ordinaryHasInstance(value, constructor, budget, context);
136474
+ }
136475
+ async function ordinaryHasInstance(value, constructor, budget, context) {
136476
+ if (!isSandboxClosure(constructor)) return false;
136477
+ if (constructor.boundTarget !== void 0) {
136478
+ budget.visitNode();
136479
+ return evaluateInstanceof(value, constructor.boundTarget, budget, context);
136480
+ }
136481
+ if (isFloat32ArrayConstructor(constructor)) return isFloat32Array(value);
136482
+ if (isSandboxErrorConstructorInstance2(value, constructor)) return true;
136483
+ if (!isGuestClosure(constructor) || typeof value !== "object" || value === null) return false;
136484
+ const prototype = await readInstanceProperty(constructor, "prototype", budget, context);
136485
+ if (typeof prototype !== "object" || prototype === null)
136486
+ throw new TypeError("Function has a non-object prototype in instanceof check.");
136487
+ let depth = 0;
136488
+ for (let current = getSandboxPrototype(value, budget); current !== null; current = getSandboxPrototype(current, budget)) {
136489
+ budget.visitNode();
136490
+ assertSandboxDataDepth(depth++);
136491
+ if (current === prototype) return true;
136492
+ }
136493
+ return false;
136494
+ }
136495
+ async function readInstanceProperty(value, key3, budget, context) {
136496
+ if (context?.getProperty !== void 0) return context.getProperty(value, key3);
136497
+ const descriptor = getSandboxPropertyDescriptor(value, key3, budget);
136498
+ if (descriptor === void 0) return void 0;
136499
+ return readPropertyDescriptor(descriptor, value, {
136500
+ ...context,
136501
+ stack: context?.stack ?? [],
136502
+ thisValue: value,
136503
+ invokeClosure: context?.invokeClosure ?? ((closure, args, receiver) => invokeBuiltinClosure(closure, args, budget, context, receiver))
136504
+ });
136505
+ }
136506
+ var init_instanceof = __esm({
136507
+ "packages/safe-js/src/interp/instanceof.ts"() {
136508
+ "use strict";
136509
+ init_graph_depth();
136510
+ init_accessors();
136511
+ init_builtin_call();
136512
+ init_float32();
136513
+ init_float32array2();
136514
+ init_error();
136515
+ init_object_model();
136516
+ init_values();
136517
+ }
136518
+ });
136519
+
136520
+ // packages/safe-js/src/interp/methods/collection-iterator.ts
136521
+ function getCollectionIteratorMember(target, property, budget) {
136522
+ if (Object.hasOwn(target, property)) return target[property];
136523
+ if (property !== "next") return void 0;
136524
+ const { collectionKind } = collectionIteratorState(target);
136525
+ return createSandboxClosure({
136526
+ sandbox: true,
136527
+ name: "next",
136528
+ call: (_args, context) => {
136529
+ const receiver = context?.thisValue;
136530
+ if (!isSandboxCollectionIterator(receiver) || collectionIteratorState(receiver).collectionKind !== collectionKind)
136531
+ throw new TypeError("Iterator next requires a matching collection iterator receiver.");
136532
+ return nextCollectionIterator(receiver, budget);
136533
+ }
136534
+ });
136535
+ }
136536
+ var init_collection_iterator2 = __esm({
136537
+ "packages/safe-js/src/interp/methods/collection-iterator.ts"() {
136538
+ "use strict";
136539
+ init_collection_iterator();
136540
+ init_values();
136541
+ }
136542
+ });
136543
+
136346
136544
  // packages/safe-js/src/interp/scope.ts
136347
136545
  function defineSnapshotBinding(target, name, value) {
136348
136546
  Object.defineProperty(target, name, {
@@ -139147,42 +139345,11 @@ function applyBinaryOperator(node, left, right, context) {
139147
139345
  case ">>>":
139148
139346
  return toNumber(left) >>> toNumber(right);
139149
139347
  case "instanceof":
139150
- return evaluateInstanceof(left, right, context);
139348
+ return evaluateInstanceof(left, right, context.budget, createCoercionContext(context));
139151
139349
  case "in":
139152
139350
  return hasSandboxProperty(right, left, context);
139153
139351
  }
139154
139352
  }
139155
- async function evaluateInstanceof(left, right, context) {
139156
- while (true) {
139157
- if (right === null || typeof right !== "object" && typeof right !== "function")
139158
- throw new TypeError("Right-hand side of 'instanceof' must be an object.");
139159
- const method = await getPropertyValue(right, Symbol.hasInstance, context);
139160
- if (method !== void 0 && method !== null) {
139161
- if (!isSandboxClosure(method)) throw new TypeError("Symbol.hasInstance must be callable.");
139162
- return Boolean(await invokeSandboxClosure(method, [left], context, context.callStack, void 0, right));
139163
- }
139164
- if (!isSandboxClosure(right))
139165
- throw new TypeError("Right-hand side of 'instanceof' is not a function.");
139166
- if (right.boundTarget === void 0) break;
139167
- context.budget.visitNode();
139168
- right = right.boundTarget;
139169
- }
139170
- if (isFloat32ArrayConstructor(right)) return isFloat32Array(left);
139171
- if (isSandboxErrorConstructorInstance2(left, right)) return true;
139172
- if (isGuestClosure(right)) {
139173
- if (typeof left !== "object" || left === null) return false;
139174
- const prototype = await getPropertyValue(right, "prototype", context);
139175
- if (typeof prototype !== "object" || prototype === null)
139176
- throw new TypeError("Function has a non-object prototype in instanceof check.");
139177
- let depth = 0;
139178
- for (let current = getSandboxPrototype(left, context.budget); current !== null; current = getSandboxPrototype(current, context.budget)) {
139179
- context.budget.visitNode();
139180
- assertSandboxDataDepth(depth++);
139181
- if (current === prototype) return true;
139182
- }
139183
- }
139184
- return false;
139185
- }
139186
139353
  function createCoercionContext(context) {
139187
139354
  return {
139188
139355
  stack: context.callStack,
@@ -139844,7 +140011,7 @@ var init_interpreter = __esm({
139844
140011
  init_patterns();
139845
140012
  init_string();
139846
140013
  init_set();
139847
- init_error();
140014
+ init_instanceof();
139848
140015
  init_object2();
139849
140016
  init_collection_iterator();
139850
140017
  init_collection_iterator2();
@@ -143518,6 +143685,18 @@ function captureGuestHeapNode(value, encode) {
143518
143685
  const state2 = captureObjectState(value, encode);
143519
143686
  return { kind: "intrinsic", id: intrinsic, ...state2 === void 0 ? {} : { state: state2 } };
143520
143687
  }
143688
+ const bound = boundFunctionStates.get(value);
143689
+ if (bound !== void 0 && isSandboxClosure(value)) {
143690
+ return {
143691
+ kind: "bound-function",
143692
+ target: encode(bound.target),
143693
+ thisValue: encode(bound.thisValue),
143694
+ args: bound.args.map(encode),
143695
+ length: encode(value.length),
143696
+ ...value.name === void 0 ? {} : { name: value.name },
143697
+ state: captureObjectState(value, encode)
143698
+ };
143699
+ }
143521
143700
  if (isSandboxMap(value)) return { kind: "map", entries: [...value.entries].map(([key3, entry]) => [encode(key3), encode(entry)]), ...serializeCollectionProperties(value, encode) };
143522
143701
  if (isSandboxSet(value)) return { kind: "set", values: [...value.values].map(encode), ...serializeCollectionProperties(value, encode) };
143523
143702
  if (isSandboxArrayIterator(value)) {
@@ -143699,6 +143878,7 @@ var init_guest_heap = __esm({
143699
143878
  "packages/safe-js/src/snapshot/guest-heap.ts"() {
143700
143879
  "use strict";
143701
143880
  init_closure_origin();
143881
+ init_bound_function_state();
143702
143882
  init_generator_properties();
143703
143883
  init_intrinsics();
143704
143884
  init_object_model();
@@ -146915,7 +147095,7 @@ var init_uri = __esm({
146915
147095
  });
146916
147096
 
146917
147097
  // packages/safe-js/src/interp/globals/function.ts
146918
- function createFunctionPrototype(budget) {
147098
+ function createFunctionPrototype(budget, installHasInstance = true) {
146919
147099
  const prototype = createSandboxClosure({ guest: true, sandbox: true, name: "", length: 0, call: () => void 0 });
146920
147100
  const properties = materializeFunctionProperties(prototype);
146921
147101
  Object.defineProperty(properties, "constructor", { value: void 0, writable: true, configurable: true });
@@ -146935,8 +147115,17 @@ function createFunctionPrototype(budget) {
146935
147115
  })
146936
147116
  });
146937
147117
  }
147118
+ const hasInstance = installHasInstance ? createSandboxClosure({
147119
+ guest: true,
147120
+ sandbox: true,
147121
+ name: "[Symbol.hasInstance]",
147122
+ length: 1,
147123
+ call: ([value], context) => ordinaryHasInstance(value, context?.thisValue, budget, context)
147124
+ }) : void 0;
147125
+ if (hasInstance !== void 0) Object.defineProperty(properties, Symbol.hasInstance, { value: hasInstance });
146938
147126
  setSandboxPrototype(prototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
146939
147127
  installFunctionPrototype(budget, prototype);
147128
+ if (hasInstance !== void 0) registerIntrinsicFunction(budget, hasInstance);
146940
147129
  }
146941
147130
  var init_function2 = __esm({
146942
147131
  "packages/safe-js/src/interp/globals/function.ts"() {
@@ -146945,6 +147134,77 @@ var init_function2 = __esm({
146945
147134
  init_function();
146946
147135
  init_object_model();
146947
147136
  init_values();
147137
+ init_instanceof();
147138
+ }
147139
+ });
147140
+
147141
+ // packages/safe-js/src/interp/globals/generator.ts
147142
+ function createGeneratorPrototypes(budget) {
147143
+ const asyncIteratorPrototype = /* @__PURE__ */ Object.create(null);
147144
+ const asyncIterator = createSandboxClosure({
147145
+ guest: true,
147146
+ sandbox: true,
147147
+ name: "[Symbol.asyncIterator]",
147148
+ length: 0,
147149
+ call: (_args, context) => context?.thisValue
147150
+ });
147151
+ Object.defineProperty(asyncIteratorPrototype, Symbol.asyncIterator, {
147152
+ value: asyncIterator,
147153
+ writable: true,
147154
+ configurable: true
147155
+ });
147156
+ setSandboxPrototype(asyncIteratorPrototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
147157
+ registerBuiltinIdentities(budget, { "%AsyncIteratorPrototype%": asyncIteratorPrototype });
147158
+ registerIntrinsicObject(budget, asyncIteratorPrototype);
147159
+ registerIntrinsicFunction(budget, asyncIterator);
147160
+ const state = /* @__PURE__ */ new Map();
147161
+ for (const async of [false, true]) {
147162
+ const tag = async ? "AsyncGenerator" : "Generator";
147163
+ const functionPrototype = /* @__PURE__ */ Object.create(null);
147164
+ const instancePrototype = /* @__PURE__ */ Object.create(null);
147165
+ setSandboxPrototype(functionPrototype, resolveIntrinsicIdentity(budget, JSON.stringify(["%FunctionPrototype%"])));
147166
+ setSandboxPrototype(instancePrototype, async ? asyncIteratorPrototype : resolveIntrinsicIdentity(budget, JSON.stringify(["%IteratorPrototype%"])));
147167
+ Object.defineProperties(functionPrototype, {
147168
+ prototype: { value: instancePrototype, configurable: true },
147169
+ // Do not expose native dynamic-source constructors through this graph.
147170
+ constructor: { value: void 0, configurable: true },
147171
+ [Symbol.toStringTag]: { value: `${tag}Function`, configurable: true }
147172
+ });
147173
+ Object.defineProperties(instancePrototype, {
147174
+ constructor: { value: functionPrototype, configurable: true },
147175
+ [Symbol.toStringTag]: { value: tag, configurable: true }
147176
+ });
147177
+ for (const method of ["next", "return", "throw"]) {
147178
+ Object.defineProperty(instancePrototype, method, {
147179
+ writable: true,
147180
+ configurable: true,
147181
+ value: createSandboxClosure({
147182
+ guest: true,
147183
+ sandbox: true,
147184
+ name: method,
147185
+ length: 1,
147186
+ call: ([value], context) => callGeneratorMethod(context?.thisValue, method, value, budget, context, async)
147187
+ })
147188
+ });
147189
+ }
147190
+ registerBuiltinIdentities(budget, {
147191
+ [`%${tag}Prototype%`]: instancePrototype,
147192
+ [`%${tag}FunctionPrototype%`]: functionPrototype
147193
+ });
147194
+ registerIntrinsicObject(budget, functionPrototype);
147195
+ registerIntrinsicObject(budget, instancePrototype);
147196
+ state.set(async, { functionPrototype, instancePrototype });
147197
+ }
147198
+ generatorPrototypes.set(budget, state);
147199
+ }
147200
+ var init_generator3 = __esm({
147201
+ "packages/safe-js/src/interp/globals/generator.ts"() {
147202
+ "use strict";
147203
+ init_generator_prototypes();
147204
+ init_intrinsics();
147205
+ init_generator2();
147206
+ init_object_model();
147207
+ init_values();
146948
147208
  }
146949
147209
  });
146950
147210
 
@@ -147200,7 +147460,8 @@ function createBuiltinBindings(options) {
147200
147460
  ...createPromiseGlobals(options),
147201
147461
  ...createRegexGlobals(options)
147202
147462
  };
147203
- createFunctionPrototype(options.budget);
147463
+ createFunctionPrototype(options.budget, options.functionHasInstance);
147464
+ createGeneratorPrototypes(options.budget);
147204
147465
  registerBuiltinIdentities(options.budget, bindings);
147205
147466
  return bindings;
147206
147467
  }
@@ -147217,6 +147478,7 @@ var init_globals = __esm({
147217
147478
  init_uri();
147218
147479
  init_object_array();
147219
147480
  init_function2();
147481
+ init_generator3();
147220
147482
  init_promise();
147221
147483
  init_date2();
147222
147484
  init_symbol();
@@ -147270,7 +147532,7 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
147270
147532
  createRawJson(node.text);
147271
147533
  return true;
147272
147534
  }
147273
- if (!["intrinsic", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "array-iterator", "map", "set"].includes(String(node.kind))) return false;
147535
+ if (!["intrinsic", "bound-function", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "array-iterator", "map", "set"].includes(String(node.kind))) return false;
147274
147536
  const reference = (value, kinds) => {
147275
147537
  const ref = record3(value);
147276
147538
  fields(ref, ["kind", "id"]);
@@ -147281,7 +147543,7 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
147281
147543
  };
147282
147544
  const callable = (value) => {
147283
147545
  if (absent(value)) return;
147284
- const target = reference(value, ["intrinsic", "guest-function", "guest-class"]);
147546
+ const target = reference(value, ["intrinsic", "bound-function", "guest-function", "guest-class"]);
147285
147547
  if (target.kind === "intrinsic" && intrinsicCatalogue().get(String(target.id)) !== true)
147286
147548
  throw new TypeError("Guest accessor reference is not callable.");
147287
147549
  };
@@ -147377,6 +147639,22 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
147377
147639
  if (descriptor.kind !== "data" || descriptor.writable !== false || descriptor.enumerable !== false || descriptor.configurable !== false)
147378
147640
  throw new TypeError("Invalid class prototype descriptor.");
147379
147641
  reference(descriptor.value, ["object", "guest-object"]);
147642
+ } else if (node.kind === "bound-function") {
147643
+ fields(node, ["kind", "target", "thisValue", "args", "length", "state"], ["name"]);
147644
+ if (Object.hasOwn(node, "name") && typeof node.name !== "string") throw new TypeError("Invalid bound function name.");
147645
+ if (array(node.args).length > maxArrayLength) throw new TypeError("Bound arguments exceed allocation limit.");
147646
+ if (!absent(node.length) && !(typeof node.length === "number" && node.length >= 0) && !(record3(node.length).kind === "number" && record3(node.length).value === "Infinity"))
147647
+ throw new TypeError("Invalid bound function length.");
147648
+ const visited = /* @__PURE__ */ new Set([raw]);
147649
+ let current = node;
147650
+ while (current.kind === "bound-function") {
147651
+ assertSnapshotDataDepth(visited.size, "<bound-target>");
147652
+ callable(current.target);
147653
+ current = reference(current.target);
147654
+ if (visited.has(current)) throw new TypeError("Cyclic bound function target.");
147655
+ visited.add(current);
147656
+ }
147657
+ state(node.state);
147380
147658
  } else if (node.kind === "guest-function") {
147381
147659
  fields(node, ["kind", "astNodeId", "scope", "state"], ["name", "environment"]);
147382
147660
  if (integer2(node.astNodeId) < 1) throw new TypeError("Invalid guest AST identity.");
@@ -147612,6 +147890,7 @@ var init_guest_heap_validation = __esm({
147612
147890
  init_intrinsics();
147613
147891
  init_object_model();
147614
147892
  init_values();
147893
+ init_graph_depth();
147615
147894
  }
147616
147895
  });
147617
147896