@poe-platform/safe-js 0.1.364 → 0.1.366

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.
@@ -7001,8 +7001,67 @@ function requireUint8Array(value) {
7001
7001
  return value;
7002
7002
  }
7003
7003
 
7004
- // packages/safe-js/src/interp/date.ts
7004
+ // packages/safe-js/src/interp/data-view.ts
7005
7005
  import { types as types3 } from "node:util";
7006
+ var dataViewPrototypes = /* @__PURE__ */ new WeakMap();
7007
+ var dataViewLayouts = /* @__PURE__ */ new WeakMap();
7008
+ var dataViewGetters = Object.fromEntries(["buffer", "byteOffset", "byteLength"].map((key) => [key, Object.getOwnPropertyDescriptor(DataView.prototype, key).get]));
7009
+ var resize = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resize")?.value;
7010
+ function isSandboxDataView(value) {
7011
+ return types3.isDataView(value) && Object.getPrototypeOf(value) === DataView.prototype;
7012
+ }
7013
+ function dataViewBuffer(value) {
7014
+ const buffer = Reflect.apply(dataViewGetters.buffer, value, []);
7015
+ if (!isSandboxArrayBuffer(buffer)) throw new TypeError("DataView requires a non-shared ArrayBuffer.");
7016
+ return buffer;
7017
+ }
7018
+ function dataViewLayout(value) {
7019
+ const buffer = dataViewBuffer(value);
7020
+ if (arrayBufferDetached(buffer)) return { byteOffset: 0, byteLength: 0 };
7021
+ if (arrayBufferOptions(buffer) !== void 0) {
7022
+ const layout = dataViewLayouts.get(value);
7023
+ if (layout === void 0) throw new TypeError("Resizable DataView persistence requires known view layout.");
7024
+ return layout;
7025
+ }
7026
+ return {
7027
+ byteOffset: Reflect.apply(dataViewGetters.byteOffset, value, []),
7028
+ byteLength: Reflect.apply(dataViewGetters.byteLength, value, [])
7029
+ };
7030
+ }
7031
+ function restoreDataView(buffer, byteOffset, byteLength, budget) {
7032
+ const originalLength = arrayBufferLength(buffer);
7033
+ const required = byteOffset + (byteLength ?? 0);
7034
+ const options = arrayBufferOptions(buffer);
7035
+ const grow = required > originalLength;
7036
+ if (grow) {
7037
+ if (resize === void 0 || options === void 0 || required > options.maxByteLength)
7038
+ throw new RangeError("DataView layout exceeds backing capacity.");
7039
+ budget?.allocateArrayLength(required);
7040
+ budget?.provisionDataUsage(required - originalLength)();
7041
+ Reflect.apply(resize, buffer, [required]);
7042
+ }
7043
+ try {
7044
+ const view = new DataView(buffer, byteOffset, byteLength);
7045
+ if (options !== void 0) dataViewLayouts.set(view, { byteOffset, ...byteLength === void 0 ? {} : { byteLength } });
7046
+ return view;
7047
+ } finally {
7048
+ if (grow) Reflect.apply(resize, buffer, [originalLength]);
7049
+ }
7050
+ }
7051
+ function copyDataViewStorage(value, state) {
7052
+ const layout = dataViewLayout(value);
7053
+ return restoreDataView(copyArrayBufferStorage(dataViewBuffer(value), state), layout.byteOffset, layout.byteLength);
7054
+ }
7055
+ function dataViewDataProperties(value) {
7056
+ return Reflect.ownKeys(value).map((key) => {
7057
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
7058
+ if (!("value" in descriptor)) throw new TypeError("DataView accessors cannot be copied as data.");
7059
+ return [key, descriptor];
7060
+ });
7061
+ }
7062
+
7063
+ // packages/safe-js/src/interp/date.ts
7064
+ import { types as types4 } from "node:util";
7006
7065
  var NativeDate = Date;
7007
7066
  var readTime = NativeDate.prototype.getTime;
7008
7067
  var writeTime = NativeDate.prototype.setTime;
@@ -7019,7 +7078,7 @@ function dateTime(value) {
7019
7078
  return Reflect.apply(readTime, value, []);
7020
7079
  }
7021
7080
  function copyNativeDate(value) {
7022
- if (!types3.isDate(value)) return void 0;
7081
+ if (!types4.isDate(value)) return void 0;
7023
7082
  if (Object.getPrototypeOf(value) !== NativeDate.prototype)
7024
7083
  throw new TypeError("Date subclasses are not supported.");
7025
7084
  return createSandboxDate(dateTime(value));
@@ -7454,7 +7513,7 @@ function copySandboxArgumentProperties(source, target, copyValue) {
7454
7513
  }
7455
7514
 
7456
7515
  // packages/safe-js/src/interp/boxed.ts
7457
- import { types as types4 } from "node:util";
7516
+ import { types as types5 } from "node:util";
7458
7517
  var boxes = /* @__PURE__ */ new WeakSet();
7459
7518
  var numberValue = Number.prototype.valueOf;
7460
7519
  var stringValue = String.prototype.valueOf;
@@ -7462,11 +7521,11 @@ var booleanValue = Boolean.prototype.valueOf;
7462
7521
  var symbolValue = Symbol.prototype.valueOf;
7463
7522
  var bigintValue = BigInt.prototype.valueOf;
7464
7523
  function nativeBoxedValue(value) {
7465
- if (types4.isNumberObject(value)) return Reflect.apply(numberValue, value, []);
7466
- if (types4.isStringObject(value)) return Reflect.apply(stringValue, value, []);
7467
- if (types4.isBooleanObject(value)) return Reflect.apply(booleanValue, value, []);
7468
- if (types4.isSymbolObject(value)) return Reflect.apply(symbolValue, value, []);
7469
- if (types4.isBigIntObject(value)) return Reflect.apply(bigintValue, value, []);
7524
+ if (types5.isNumberObject(value)) return Reflect.apply(numberValue, value, []);
7525
+ if (types5.isStringObject(value)) return Reflect.apply(stringValue, value, []);
7526
+ if (types5.isBooleanObject(value)) return Reflect.apply(booleanValue, value, []);
7527
+ if (types5.isSymbolObject(value)) return Reflect.apply(symbolValue, value, []);
7528
+ if (types5.isBigIntObject(value)) return Reflect.apply(bigintValue, value, []);
7470
7529
  return void 0;
7471
7530
  }
7472
7531
  function createSandboxBox(value) {
@@ -7645,7 +7704,7 @@ async function withRunResources(signal, execute) {
7645
7704
  }
7646
7705
 
7647
7706
  // packages/safe-js/src/interp/host-capabilities.ts
7648
- import { types as types5 } from "node:util";
7707
+ import { types as types6 } from "node:util";
7649
7708
  var MAX_INDEXED_LENGTH = 65536;
7650
7709
  var MAX_NAMED_KEYS = 65536;
7651
7710
  var MAX_NAMED_KEY_CODE_UNITS = 1048576;
@@ -7700,7 +7759,7 @@ function createLiveHostObject(definition, controller) {
7700
7759
  for (const name of ["keys", "get", "set", "delete"]) {
7701
7760
  const operation = data[name];
7702
7761
  if (operation === void 0 && (name === "set" || name === "delete")) continue;
7703
- if (typeof operation !== "function" || types5.isProxy(operation) || types5.isAsyncFunction(operation) || types5.isGeneratorFunction(operation))
7762
+ if (typeof operation !== "function" || types6.isProxy(operation) || types6.isAsyncFunction(operation) || types6.isGeneratorFunction(operation))
7704
7763
  throw new TypeError(`Named ${name} must be a synchronous non-generator function, not a proxy.`);
7705
7764
  }
7706
7765
  if (typeof data.maxKeys !== "number" || !Number.isInteger(data.maxKeys) || data.maxKeys < 1 || data.maxKeys > MAX_NAMED_KEYS)
@@ -7747,13 +7806,13 @@ function createLiveHostObject(definition, controller) {
7747
7806
  controller.chargeWork(properties.size + Object.keys(operations).length + 1);
7748
7807
  const host = Object.freeze(/* @__PURE__ */ Object.create(null));
7749
7808
  const guest = Object.freeze(/* @__PURE__ */ Object.create(null));
7750
- const methods = new Map(
7809
+ const methods2 = new Map(
7751
7810
  Object.entries(operations).map(([name, operation]) => [
7752
7811
  name,
7753
7812
  controller.method(operation)
7754
7813
  ])
7755
7814
  );
7756
- const state = { host, guest, controller, properties, methods, indexed, named };
7815
+ const state = { host, guest, controller, properties, methods: methods2, indexed, named };
7757
7816
  hostObjects.set(host, state);
7758
7817
  guestObjects.set(guest, state);
7759
7818
  return host;
@@ -7955,7 +8014,7 @@ function namedMutationKeys(state, key, create) {
7955
8014
  function namedKeys(state) {
7956
8015
  const named = state.named;
7957
8016
  return state.controller.read(named.keys, (value) => {
7958
- if (!Array.isArray(value) || types5.isProxy(value))
8017
+ if (!Array.isArray(value) || types6.isProxy(value))
7959
8018
  throw new TypeError("Named keys must be a dense own-data array of strings, not a proxy.");
7960
8019
  const length = Object.getOwnPropertyDescriptor(value, "length").value;
7961
8020
  if (length > named.maxKeys) throw new RangeError("Named keys exceed maxKeys.");
@@ -8141,11 +8200,11 @@ function isIntrinsicFunction(value) {
8141
8200
  function registerIntrinsicPrototype(budget, prototype, constructor) {
8142
8201
  if (constructor.name === void 0) throw new TypeError("Intrinsic constructors require an installation name.");
8143
8202
  registerBuiltinIdentities(budget, { [constructor.name]: constructor });
8144
- const methods = [prototype, materializeFunctionProperties(constructor)].flatMap((owner) => Reflect.ownKeys(owner).flatMap((key) => {
8203
+ const methods2 = [prototype, materializeFunctionProperties(constructor)].flatMap((owner) => Reflect.ownKeys(owner).flatMap((key) => {
8145
8204
  const descriptor = Object.getOwnPropertyDescriptor(owner, key);
8146
8205
  return [descriptor.value, ...retainedAccessorClosures(descriptor)];
8147
8206
  })).filter(isGuestClosure);
8148
- trackIntrinsicState(budget, prototype, constructor, [prototype, constructor, ...methods]);
8207
+ trackIntrinsicState(budget, prototype, constructor, [prototype, constructor, ...methods2]);
8149
8208
  }
8150
8209
  function registerIntrinsicFunction(budget, closure) {
8151
8210
  if (closure.name === void 0) throw new TypeError("Intrinsic functions require an installation name.");
@@ -8154,8 +8213,8 @@ function registerIntrinsicFunction(budget, closure) {
8154
8213
  trackIntrinsicState(budget, closure, closure, [closure]);
8155
8214
  }
8156
8215
  function registerIntrinsicObject(budget, value) {
8157
- const methods = Object.values(Object.getOwnPropertyDescriptors(value)).map((descriptor) => descriptor.value).filter(isGuestClosure);
8158
- trackIntrinsicState(budget, value, value, [value, ...methods]);
8216
+ const methods2 = Object.values(Object.getOwnPropertyDescriptors(value)).map((descriptor) => descriptor.value).filter(isGuestClosure);
8217
+ trackIntrinsicState(budget, value, value, [value, ...methods2]);
8159
8218
  }
8160
8219
  function trackIntrinsicState(budget, root, owner, targets) {
8161
8220
  let roots = intrinsicPrototypeRoots.get(budget);
@@ -8228,6 +8287,7 @@ function getSandboxPrototype(value, budget) {
8228
8287
  if (prototypes.has(value)) return prototypes.get(value) ?? null;
8229
8288
  if (budget === void 0) return null;
8230
8289
  if (isSandboxArrayBuffer(value)) return budget === void 0 ? null : arrayBufferPrototypes.get(budget) ?? null;
8290
+ if (isSandboxDataView(value)) return dataViewPrototypes.get(budget) ?? null;
8231
8291
  if (isNumericTypedArray(value)) return budget === void 0 ? null : typedArrayPrototypes.get(budget)?.get(typedArrayStorage(value).Native) ?? null;
8232
8292
  const errorType = sandboxErrorTypes.get(value);
8233
8293
  if (budget !== void 0 && errorType !== void 0) {
@@ -8338,6 +8398,7 @@ function setSandboxPrototype(value, prototype, budget) {
8338
8398
  prototypes.set(value, prototype);
8339
8399
  }
8340
8400
  function isPrototypeRecord(value) {
8401
+ if (isSandboxDataView(value)) return true;
8341
8402
  if (isSandboxArrayBuffer(value)) return true;
8342
8403
  if (isNumericTypedArray(value)) return true;
8343
8404
  if (isGuestClosure(value)) return true;
@@ -8603,7 +8664,7 @@ function restoreRegexProperties(value, data, decode) {
8603
8664
  }
8604
8665
 
8605
8666
  // packages/safe-js/src/snapshot/validation.ts
8606
- import { types as types7 } from "node:util";
8667
+ import { types as types8 } from "node:util";
8607
8668
 
8608
8669
  // packages/safe-js/src/interp/builtin-call.ts
8609
8670
  async function invokeBuiltinClosure(closure, args, budget, context, thisValue, construct = false) {
@@ -11017,7 +11078,7 @@ function formatNumber(value, methodName, argument, budget) {
11017
11078
  }
11018
11079
 
11019
11080
  // packages/safe-js/src/interp/methods/string.ts
11020
- import { types as types6 } from "node:util";
11081
+ import { types as types7 } from "node:util";
11021
11082
 
11022
11083
  // packages/safe-js/src/interp/methods/string-locale.ts
11023
11084
  var collatorOptions = [
@@ -11168,7 +11229,7 @@ function callStringMethod(value, methodName, args, budget, callClosure = async (
11168
11229
  };
11169
11230
  const symbol = methodName === "match" ? Symbol.match : methodName === "search" ? Symbol.search : methodName === "matchAll" ? Symbol.matchAll : methodName === "replace" || methodName === "replaceAll" ? Symbol.replace : methodName === "split" ? Symbol.split : void 0;
11170
11231
  const pattern = args[0];
11171
- if (symbol !== void 0 && types6.isRegExp(pattern))
11232
+ if (symbol !== void 0 && types7.isRegExp(pattern))
11172
11233
  throw new TypeError(`String#${methodName} does not accept unbranded host RegExp values.`);
11173
11234
  if (symbol === void 0 || pattern === null || pattern === void 0) return fallback();
11174
11235
  const readProperty = (key) => context?.getProperty !== void 0 ? context.getProperty(pattern, key) : key === "flags" && isSandboxRegex(pattern) && getSandboxPropertyDescriptor(pattern, key, budget) === void 0 ? getRegexMember(pattern, key, budget, context) : getSandboxDataProperty(pattern, key, budget);
@@ -11920,7 +11981,7 @@ function createPrimitiveConstructor(options, budget) {
11920
11981
  writable: isSandboxClosure(value),
11921
11982
  configurable: isSandboxClosure(value)
11922
11983
  });
11923
- const methods = /* @__PURE__ */ new Map([
11984
+ const methods2 = /* @__PURE__ */ new Map([
11924
11985
  [
11925
11986
  "valueOf",
11926
11987
  createSandboxClosure({
@@ -11932,9 +11993,9 @@ function createPrimitiveConstructor(options, budget) {
11932
11993
  ]
11933
11994
  ]);
11934
11995
  if (kind === "number") {
11935
- for (const name of numberMethodNames) methods.set(name, getNumberMember(name, budget));
11996
+ for (const name of numberMethodNames) methods2.set(name, getNumberMember(name, budget));
11936
11997
  } else {
11937
- methods.set(
11998
+ methods2.set(
11938
11999
  "toString",
11939
12000
  createSandboxClosure({
11940
12001
  sandbox: true,
@@ -11944,9 +12005,9 @@ function createPrimitiveConstructor(options, budget) {
11944
12005
  })
11945
12006
  );
11946
12007
  if (kind === "string")
11947
- for (const name of stringMethodNames) methods.set(name, getStringMember("", name, budget));
12008
+ for (const name of stringMethodNames) methods2.set(name, getStringMember("", name, budget));
11948
12009
  }
11949
- for (const [name, value] of methods)
12010
+ for (const [name, value] of methods2)
11950
12011
  Object.defineProperty(prototype, name, { value, writable: true, configurable: true });
11951
12012
  installBoxedPrototype(budget, prototype, constructor);
11952
12013
  return constructor;
@@ -19804,7 +19865,7 @@ function toPropertyKey(value, budget, context) {
19804
19865
  }
19805
19866
 
19806
19867
  // packages/safe-js/src/interp/globals/object.ts
19807
- function createObjectGlobal(methods, budget) {
19868
+ function createObjectGlobal(methods2, budget) {
19808
19869
  const construct = ([value]) => {
19809
19870
  if (value === null || value === void 0) {
19810
19871
  budget.chargeDataUsage(1);
@@ -19839,7 +19900,7 @@ function createObjectGlobal(methods, budget) {
19839
19900
  const properties = materializeFunctionProperties(constructor);
19840
19901
  const prototype = properties.prototype;
19841
19902
  Object.defineProperty(properties, "prototype", { writable: false });
19842
- for (const [name, method] of Object.entries(methods)) {
19903
+ for (const [name, method] of Object.entries(methods2)) {
19843
19904
  Object.defineProperty(properties, name, { value: method, writable: true, configurable: true });
19844
19905
  }
19845
19906
  const prototypeMethods = {
@@ -21896,6 +21957,39 @@ function snapshotTypedArrayConstructor(value) {
21896
21957
  return numericTypedArrayConstructors[value.arrayType];
21897
21958
  }
21898
21959
 
21960
+ // packages/safe-js/src/snapshot/data-view.ts
21961
+ function encodeDataViewLayout(value) {
21962
+ const layout = dataViewLayout(value);
21963
+ return {
21964
+ kind: "dataview",
21965
+ byteOffset: layout.byteOffset,
21966
+ byteLength: layout.byteLength ?? 0,
21967
+ ...layout.byteLength === void 0 ? { lengthTracking: true } : {}
21968
+ };
21969
+ }
21970
+ function captureDataViewState(value, encode) {
21971
+ return {
21972
+ properties: serializePropertyDescriptors(value, encode),
21973
+ ...hasExplicitSandboxPrototype(value) ? { prototype: encode(getSandboxPrototype(value)) } : {}
21974
+ };
21975
+ }
21976
+ function validateDataViewStorage(value) {
21977
+ if (!Number.isSafeInteger(value.byteOffset) || Number(value.byteOffset) < 0 || !Number.isSafeInteger(value.byteLength) || Number(value.byteLength) < 0 || !Number.isSafeInteger(Number(value.byteOffset) + Number(value.byteLength)))
21978
+ throw new TypeError("Invalid DataView dimensions.");
21979
+ if (!Object.hasOwn(value, "buffer") || Object.hasOwn(value, "bytes")) throw new TypeError("DataView requires a backing buffer reference.");
21980
+ if (Object.hasOwn(value, "lengthTracking") && (value.lengthTracking !== true || value.byteLength !== 0))
21981
+ throw new TypeError("Invalid DataView length-tracking layout.");
21982
+ }
21983
+ function decodeDataViewStorage(value, resolve, budget) {
21984
+ validateDataViewStorage(value);
21985
+ const buffer = resolve(value.buffer);
21986
+ if (!isSandboxArrayBuffer(buffer)) throw new TypeError("Invalid DataView backing reference.");
21987
+ if (value.lengthTracking === true && arrayBufferOptions(buffer) === void 0)
21988
+ throw new TypeError("Length-tracking DataView requires resizable storage.");
21989
+ budget?.provisionDataUsage(1)();
21990
+ return restoreDataView(buffer, Number(value.byteOffset), value.lengthTracking === true ? void 0 : Number(value.byteLength), budget);
21991
+ }
21992
+
21899
21993
  // packages/safe-js/src/snapshot/array-buffer.ts
21900
21994
  function captureArrayBufferState(value, encode) {
21901
21995
  return {
@@ -22077,10 +22171,10 @@ function serializeDumpValue(value, path, state) {
22077
22171
  }
22078
22172
  return { kind: "ref", id };
22079
22173
  }
22080
- if (hasGuestObjectState(value) && !(state.trustedRunReplay && (isSandboxDate(value) || isSandboxPromise(value) || isPromiseResolvingFunction(value)))) {
22174
+ if (hasGuestObjectState(value) && !isSandboxDataView(value) && !(state.trustedRunReplay && (isSandboxDate(value) || isSandboxPromise(value) || isPromiseResolvingFunction(value)))) {
22081
22175
  throw new TypeError("Guest function properties and prototype links cannot be serialized.");
22082
22176
  }
22083
- if (isSandboxArrayBuffer(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isNumericTypedArray(value)) return serializeHeapReference(value, path, state);
22177
+ if (isSandboxDataView(value) || isSandboxArrayBuffer(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isNumericTypedArray(value)) return serializeHeapReference(value, path, state);
22084
22178
  if (Array.isArray(value)) {
22085
22179
  const reference2 = serializeHeapReference(value, path, state);
22086
22180
  if (reference2 !== void 0) {
@@ -22152,6 +22246,18 @@ function serializeHeapReference(value, path, state) {
22152
22246
  const serialized = serializeDumpValue(entry, `${path}.<date-property>`, state);
22153
22247
  return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
22154
22248
  });
22249
+ } else if (isSandboxDataView(value)) {
22250
+ state.heap[String(id)] = {
22251
+ ...encodeDataViewLayout(value),
22252
+ buffer: serializeHeapReference(dataViewBuffer(value), `${path}.buffer`, state),
22253
+ state: captureDataViewState(value, (entry) => {
22254
+ if (entry === void 0) return { kind: "undefined" };
22255
+ if (Object.is(entry, -0)) return { kind: "number", value: "-0" };
22256
+ const serialized = serializeDumpValue(entry, `${path}.<view>`, state);
22257
+ if (serialized === SKIP_VALUE) throw new TypeError("Unsupported DataView property in public dump.");
22258
+ return serialized;
22259
+ })
22260
+ };
22155
22261
  } else if (isSandboxArrayBuffer(value)) {
22156
22262
  state.heap[String(id)] = {
22157
22263
  ...encodeArrayBufferStorage(value, id, state.float32Buffers, (id2) => ({ kind: "ref", id: id2 })),
@@ -22231,7 +22337,7 @@ function indexHeapContainers(snapshot) {
22231
22337
  const heapIds = /* @__PURE__ */ new Map();
22232
22338
  let nextId = 1;
22233
22339
  for (const [value, stat2] of stats.entries()) {
22234
- if (stat2.count > 1 || stat2.forceHeap === true || guestValues.has(value) || stat2.cyclic || hasNullObjectPrototype(value) || ownSerializableSymbolKeys(value).length > 0 || isSandboxBox(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxDate(value) || isNumericTypedArray(value) || isSandboxArrayBuffer(value) || Array.isArray(value) && requiresArrayEntries(value) || isSandboxArguments(value) || sandboxErrorTypes.has(value)) {
22340
+ if (stat2.count > 1 || stat2.forceHeap === true || guestValues.has(value) || stat2.cyclic || hasNullObjectPrototype(value) || ownSerializableSymbolKeys(value).length > 0 || isSandboxBox(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxDate(value) || isNumericTypedArray(value) || isSandboxArrayBuffer(value) || isSandboxDataView(value) || Array.isArray(value) && requiresArrayEntries(value) || isSandboxArguments(value) || sandboxErrorTypes.has(value)) {
22235
22341
  heapIds.set(value, nextId);
22236
22342
  nextId += 1;
22237
22343
  }
@@ -22263,8 +22369,12 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
22263
22369
  stat2.expanded = true;
22264
22370
  ancestors.add(value);
22265
22371
  const guestEntries = [];
22372
+ if (isSandboxDataView(value)) guestEntries.push(dataViewBuffer(value));
22266
22373
  if (isNumericTypedArray(value)) guestEntries.push(typedArrayStorage(value).buffer);
22267
- const guest = isSandboxArrayBuffer(value) ? { kind: "arraybuffer", state: captureArrayBufferState(value, (entry) => {
22374
+ const guest = isSandboxDataView(value) ? { kind: "dataview", state: captureDataViewState(value, (entry) => {
22375
+ guestEntries.push(entry);
22376
+ return null;
22377
+ }) } : isSandboxArrayBuffer(value) ? { kind: "arraybuffer", state: captureArrayBufferState(value, (entry) => {
22268
22378
  guestEntries.push(entry);
22269
22379
  return null;
22270
22380
  }) } : captureGuestHeapNode(value, (entry) => {
@@ -22272,7 +22382,7 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
22272
22382
  return null;
22273
22383
  });
22274
22384
  if (guest !== void 0) {
22275
- if (!isSandboxArrayBuffer(value)) guestValues.add(value);
22385
+ if (!isSandboxArrayBuffer(value) && !isSandboxDataView(value)) guestValues.add(value);
22276
22386
  for (const entry of guestEntries) {
22277
22387
  collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
22278
22388
  if (entry !== null && typeof entry === "object") stats.get(entry).forceHeap = true;
@@ -22600,10 +22710,10 @@ function encodeReplayData(value, options = {}) {
22600
22710
  ...symbolProperties.length === 0 ? {} : { symbolProperties },
22601
22711
  ...Object.isExtensible(entry) ? {} : { extensible: false }
22602
22712
  };
22603
- } else if (isSandboxArrayBuffer(entry)) {
22604
- const storage = encodeArrayBufferStorage(entry, id, float32Buffers, (id2) => ({ tag: "ref", id: id2 }));
22713
+ } else if (isSandboxArrayBuffer(entry) || isSandboxDataView(entry)) {
22714
+ const storage = isSandboxDataView(entry) ? { ...encodeDataViewLayout(entry), buffer: child(dataViewBuffer(entry), "<buffer>") } : encodeArrayBufferStorage(entry, id, float32Buffers, (id2) => ({ tag: "ref", id: id2 }));
22605
22715
  const properties = /* @__PURE__ */ Object.create(null);
22606
- for (const [key, descriptor] of arrayBufferDataProperties(entry)) {
22716
+ for (const [key, descriptor] of isSandboxDataView(entry) ? dataViewDataProperties(entry) : arrayBufferDataProperties(entry)) {
22607
22717
  if (typeof key === "symbol") continue;
22608
22718
  properties[key] = {
22609
22719
  value: child(descriptor.value, key),
@@ -22846,9 +22956,9 @@ function decodeReplayData(input, options = {}, parent) {
22846
22956
  if (node.extensible === false) Object.preventExtensions(result3);
22847
22957
  return result3;
22848
22958
  }
22849
- if (kind === "arraybuffer") {
22959
+ if (kind === "arraybuffer" || kind === "dataview") {
22850
22960
  if (typeof node.extensible !== "boolean") throw new TypeError("Invalid ArrayBuffer extensibility.");
22851
- const result3 = decodeArrayBufferStorage(node, child, compilation.owner?.budget, detachBuffers);
22961
+ const result3 = kind === "dataview" ? decodeDataViewStorage(node, child, compilation.owner?.budget) : decodeArrayBufferStorage(node, child, compilation.owner?.budget, detachBuffers);
22852
22962
  restored.set(id, result3);
22853
22963
  initializeValues.push(() => {
22854
22964
  defineProperties(result3, record2(own(node, "properties")), child, node.symbolEntries);
@@ -23746,6 +23856,38 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
23746
23856
  state.seen.set(value, date);
23747
23857
  return date;
23748
23858
  }
23859
+ if (isSandboxDataView(value)) {
23860
+ const existing = state.seen.get(value);
23861
+ if (existing !== void 0) return existing;
23862
+ const buffer = dataViewBuffer(value);
23863
+ const length = arrayBufferLength(buffer);
23864
+ budget.allocateArrayLength(arrayBufferOptions(buffer)?.maxByteLength ?? length);
23865
+ budget.provisionDataUsage(length + 2)();
23866
+ const copy = copyDataViewStorage(value, state);
23867
+ state.seen.set(value, copy);
23868
+ copyHostValueToSandbox(
23869
+ buffer,
23870
+ stackFrames,
23871
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], "buffer"] },
23872
+ state,
23873
+ `${path}.buffer`
23874
+ );
23875
+ for (const [key, descriptor] of dataViewDataProperties(value)) {
23876
+ if (typeof key !== "string") throw new TypeError("Host DataView symbol properties require an explicit capability path.");
23877
+ Object.defineProperty(copy, key, {
23878
+ ...descriptor,
23879
+ value: copyHostValueToSandbox(
23880
+ descriptor.value,
23881
+ stackFrames,
23882
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], key] },
23883
+ state,
23884
+ joinPath(path, key)
23885
+ )
23886
+ });
23887
+ }
23888
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
23889
+ return copy;
23890
+ }
23749
23891
  if (isSandboxArrayBuffer(value)) {
23750
23892
  const existing = state.seen.get(value);
23751
23893
  if (existing !== void 0) return existing;
@@ -24135,9 +24277,9 @@ function createDateGlobal(options) {
24135
24277
  call: (args, context) => coerceDateParts(args, true, options.budget, context)
24136
24278
  })
24137
24279
  };
24138
- const methods = /* @__PURE__ */ new Map();
24280
+ const methods2 = /* @__PURE__ */ new Map();
24139
24281
  for (const [name, method] of dateMethods)
24140
- methods.set(
24282
+ methods2.set(
24141
24283
  name,
24142
24284
  createSandboxClosure({
24143
24285
  guest: true,
@@ -24157,8 +24299,8 @@ function createDateGlobal(options) {
24157
24299
  }
24158
24300
  })
24159
24301
  );
24160
- methods.set("toGMTString", methods.get("toUTCString"));
24161
- methods.set(Symbol.toPrimitive, createSandboxClosure({
24302
+ methods2.set("toGMTString", methods2.get("toUTCString"));
24303
+ methods2.set(Symbol.toPrimitive, createSandboxClosure({
24162
24304
  guest: true,
24163
24305
  sandbox: true,
24164
24306
  name: "[Symbol.toPrimitive]",
@@ -24174,7 +24316,7 @@ function createDateGlobal(options) {
24174
24316
  for (const [key, value] of Object.entries(staticProperties))
24175
24317
  Object.defineProperty(properties, key, { value, writable: key !== "prototype", configurable: key !== "prototype" });
24176
24318
  Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
24177
- for (const [key, value] of methods)
24319
+ for (const [key, value] of methods2)
24178
24320
  Object.defineProperty(prototype, key, { value, writable: key !== Symbol.toPrimitive, configurable: true });
24179
24321
  installDatePrototype(options.budget, prototype, constructor);
24180
24322
  return constructor;
@@ -24626,7 +24768,7 @@ function installCollectionPrototypes(budget, mapConstructor, setConstructor) {
24626
24768
  return context.thisValue.values.size;
24627
24769
  }
24628
24770
  });
24629
- for (const { name, constructor, methods, size, iterator } of [
24771
+ for (const { name, constructor, methods: methods2, size, iterator } of [
24630
24772
  { name: "Map", constructor: mapConstructor, methods: mapMethods, size: mapSize, iterator: mapMethods.entries },
24631
24773
  { name: "Set", constructor: setConstructor, methods: setMethods, size: setSize, iterator: setMethods.values }
24632
24774
  ]) {
@@ -24641,13 +24783,13 @@ function installCollectionPrototypes(budget, mapConstructor, setConstructor) {
24641
24783
  Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
24642
24784
  Object.defineProperty(materializeFunctionProperties(constructor), Symbol.species, { get: accessorAdapter(species, "get"), configurable: true });
24643
24785
  Object.defineProperty(prototype, "constructor", { value: constructor, writable: true, configurable: true });
24644
- for (const [key, method] of Object.entries(methods))
24786
+ for (const [key, method] of Object.entries(methods2))
24645
24787
  Object.defineProperty(prototype, key, { value: method, writable: true, configurable: true });
24646
24788
  Object.defineProperty(prototype, "size", { get: accessorAdapter(size, "get"), configurable: true });
24647
24789
  Object.defineProperty(prototype, Symbol.toStringTag, { value: name, configurable: true });
24648
24790
  Object.defineProperty(prototype, Symbol.iterator, { value: iterator, writable: true, configurable: true });
24649
24791
  installCollectionPrototype(budget, name, prototype, constructor);
24650
- for (const method of /* @__PURE__ */ new Set([...Object.values(methods), size, species])) registerIntrinsicFunction(budget, method);
24792
+ for (const method of /* @__PURE__ */ new Set([...Object.values(methods2), size, species])) registerIntrinsicFunction(budget, method);
24651
24793
  }
24652
24794
  }
24653
24795
 
@@ -25413,55 +25555,114 @@ function createRegexGlobals(options) {
25413
25555
  return { RegExp: constructor };
25414
25556
  }
25415
25557
 
25416
- // packages/safe-js/src/interp/globals/misc.ts
25417
- function createMiscGlobals(options) {
25418
- return {
25419
- structuredClone: createSandboxClosure({
25420
- sandbox: true,
25421
- call: ([value], context) => structuredCloneSandboxValue(value, options.budget, context?.compilation),
25422
- name: "structuredClone"
25423
- }),
25424
- ...createNumericParsers(options.budget),
25425
- isNaN: createSandboxClosure({
25426
- sandbox: true,
25427
- call: ([value], context) => {
25428
- const number = sandboxNumber(value, options.budget, context);
25429
- return typeof number === "number" ? Number.isNaN(number) : number.then(Number.isNaN);
25430
- },
25431
- name: "isNaN"
25432
- }),
25433
- isFinite: createSandboxClosure({
25434
- sandbox: true,
25435
- call: ([value], context) => {
25436
- const number = sandboxNumber(value, options.budget, context);
25437
- return typeof number === "number" ? Number.isFinite(number) : number.then(Number.isFinite);
25438
- },
25439
- name: "isFinite"
25558
+ // packages/safe-js/src/interp/globals/structured-clone.ts
25559
+ var transferBuffer = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "transfer")?.value;
25560
+ function createStructuredCloneGlobal(budget) {
25561
+ return createSandboxClosure({
25562
+ guest: true,
25563
+ sandbox: true,
25564
+ name: "structuredClone",
25565
+ length: 1,
25566
+ call: (args, context) => {
25567
+ if (args.length === 0) throw new TypeError("structuredClone requires a value argument.");
25568
+ const [value, options] = args;
25569
+ if (options === void 0 || options === null) return cloneStructuredValue(value, [], budget, context);
25570
+ if (typeof options !== "object") throw new TypeError("structuredClone options must be an object.");
25571
+ return cloneWithOptions(value, options, budget, context);
25572
+ }
25573
+ });
25574
+ }
25575
+ async function cloneWithOptions(value, options, budget, context) {
25576
+ const bridge = {
25577
+ ...context,
25578
+ stack: context?.stack ?? [],
25579
+ thisValue: void 0,
25580
+ getProperty: context?.getProperty ?? ((object, key) => {
25581
+ const descriptor = getSandboxPropertyDescriptor(object, key, budget);
25582
+ return descriptor === void 0 ? void 0 : readPropertyDescriptor(descriptor, object, context);
25440
25583
  })
25441
25584
  };
25585
+ let current;
25586
+ let iterator;
25587
+ const items = [];
25588
+ const release = retainValues(budget, () => [value, options, current, items, iterator?.retainedValue]);
25589
+ try {
25590
+ current = await bridge.getProperty(options, "transfer");
25591
+ if (current !== void 0) {
25592
+ if (current === null || typeof current !== "object") throw new TypeError("Transfer list must be an iterable object.");
25593
+ iterator = await acquireSandboxIterator(current, budget, bridge);
25594
+ if (iterator === void 0) throw new TypeError("Transfer list must be iterable.");
25595
+ for (; ; ) {
25596
+ budget.visitNode();
25597
+ const result = await iterator.next();
25598
+ current = result;
25599
+ if ((await readIteratorResult(iterator, result, "done")).value) break;
25600
+ current = (await readIteratorResult(iterator, result, "value")).value;
25601
+ if (current === null || typeof current !== "object") {
25602
+ throw new TypeError("Transfer list elements must be objects.");
25603
+ }
25604
+ budget.allocateArrayLength(items.length + 1);
25605
+ budget.provisionDataUsage(items.length + 2)();
25606
+ items.push(current);
25607
+ }
25608
+ }
25609
+ const seen = /* @__PURE__ */ new Set();
25610
+ const transfers = [];
25611
+ for (const item of items) {
25612
+ budget.visitNode();
25613
+ if (!isSandboxArrayBuffer(item) || seen.has(item))
25614
+ throw new DOMException("Invalid or duplicate transferable.", "DataCloneError");
25615
+ seen.add(item);
25616
+ transfers.push(item);
25617
+ }
25618
+ return cloneStructuredValue(value, transfers, budget, context);
25619
+ } finally {
25620
+ release();
25621
+ }
25442
25622
  }
25443
- function structuredCloneSandboxValue(value, budget, parent) {
25623
+ function cloneStructuredValue(value, transfers, budget, context) {
25624
+ const parent = context?.compilation;
25444
25625
  const operation = budget.acquireCompileOwner(false, parent?.owner);
25445
25626
  const compilation = parent?.owner === operation.owner ? parent : new CompileScope(operation.owner);
25627
+ const buffers = /* @__PURE__ */ new WeakMap();
25628
+ let clone;
25629
+ const release = retainValues(budget, () => [value, clone, transfers]);
25446
25630
  try {
25447
- const clone = cloneSandboxValue(value, { compilation, resetRegexLastIndex: true, structuredClone: true });
25631
+ let transferSize = 0;
25632
+ for (const buffer of transfers) {
25633
+ const length = arrayBufferLength(buffer);
25634
+ budget.allocateArrayLength(arrayBufferOptions(buffer)?.maxByteLength ?? length);
25635
+ transferSize += length + 1;
25636
+ if (arrayBufferDetached(buffer)) buffers.set(buffer, new ArrayBuffer(0));
25637
+ }
25638
+ budget.provisionDataUsage(transferSize)();
25639
+ clone = cloneSandboxValue(value, { compilation, resetRegexLastIndex: true, structuredClone: true, float32Buffers: buffers });
25448
25640
  assertSandboxGraphDepth(clone);
25449
25641
  assertStructuredCloneable(clone, /* @__PURE__ */ new WeakSet());
25450
25642
  allocateProducedSandboxValue(clone, budget);
25451
25643
  if (compilation !== parent) reconcileCompiledValues(budget, [clone], compilation);
25644
+ if (transfers.length > 0) {
25645
+ createDataCheckpoint(budget, context)(clone, 0, true);
25646
+ budget.visitNode(transferSize + transfers.length);
25647
+ budget.provisionDataUsage(transferBuffer === void 0 ? transferSize : transfers.length)();
25648
+ for (const buffer of transfers) {
25649
+ if (arrayBufferDetached(buffer)) throw new DOMException("Cannot transfer a detached ArrayBuffer.", "DataCloneError");
25650
+ if (transferBuffer !== void 0) Reflect.apply(transferBuffer, buffer, [0]);
25651
+ else structuredClone(buffer, { transfer: [buffer] });
25652
+ if (!arrayBufferDetached(buffer)) throw new DOMException("ArrayBuffer cannot be transferred.", "DataCloneError");
25653
+ }
25654
+ }
25452
25655
  return clone;
25453
25656
  } finally {
25657
+ release();
25454
25658
  if (compilation !== parent) compilation.dispose();
25455
25659
  operation.release();
25456
25660
  }
25457
25661
  }
25458
25662
  function assertStructuredCloneable(value, seen) {
25459
- if (isSandboxClosure(value) || isSandboxPromise(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value)) {
25460
- throw new TypeError("structuredClone() cannot clone closures, promises, or collection iterators.");
25461
- }
25462
- if (typeof value !== "object" || value === null || seen.has(value)) {
25463
- return;
25464
- }
25663
+ if (isSandboxClosure(value) || isSandboxPromise(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value))
25664
+ throw new DOMException("Cannot clone closures, promises, or collection iterators.", "DataCloneError");
25665
+ if (typeof value !== "object" || value === null || seen.has(value)) return;
25465
25666
  seen.add(value);
25466
25667
  if (isSandboxMap(value)) {
25467
25668
  for (const [key, entry] of value.entries) {
@@ -25471,14 +25672,34 @@ function assertStructuredCloneable(value, seen) {
25471
25672
  return;
25472
25673
  }
25473
25674
  if (isSandboxSet(value)) {
25474
- for (const entry of value.values) {
25475
- assertStructuredCloneable(entry, seen);
25476
- }
25675
+ for (const entry of value.values) assertStructuredCloneable(entry, seen);
25477
25676
  return;
25478
25677
  }
25479
- for (const entry of Object.values(value)) {
25480
- assertStructuredCloneable(entry, seen);
25481
- }
25678
+ for (const entry of Object.values(value)) assertStructuredCloneable(entry, seen);
25679
+ }
25680
+
25681
+ // packages/safe-js/src/interp/globals/misc.ts
25682
+ function createMiscGlobals(options) {
25683
+ return {
25684
+ structuredClone: createStructuredCloneGlobal(options.budget),
25685
+ ...createNumericParsers(options.budget),
25686
+ isNaN: createSandboxClosure({
25687
+ sandbox: true,
25688
+ call: ([value], context) => {
25689
+ const number = sandboxNumber(value, options.budget, context);
25690
+ return typeof number === "number" ? Number.isNaN(number) : number.then(Number.isNaN);
25691
+ },
25692
+ name: "isNaN"
25693
+ }),
25694
+ isFinite: createSandboxClosure({
25695
+ sandbox: true,
25696
+ call: ([value], context) => {
25697
+ const number = sandboxNumber(value, options.budget, context);
25698
+ return typeof number === "number" ? Number.isFinite(number) : number.then(Number.isFinite);
25699
+ },
25700
+ name: "isFinite"
25701
+ })
25702
+ };
25482
25703
  }
25483
25704
 
25484
25705
  // packages/safe-js/src/interp/globals/uri.ts
@@ -25665,7 +25886,7 @@ function createSymbolGlobal(budget) {
25665
25886
  });
25666
25887
  const properties = materializeFunctionProperties(constructor);
25667
25888
  Object.defineProperty(properties, "prototype", { value: prototype });
25668
- const methods = {
25889
+ const methods2 = {
25669
25890
  for: createSandboxClosure({
25670
25891
  sandbox: true,
25671
25892
  name: "for",
@@ -25691,7 +25912,7 @@ function createSymbolGlobal(budget) {
25691
25912
  }
25692
25913
  })
25693
25914
  };
25694
- for (const [name, method] of Object.entries(methods)) {
25915
+ for (const [name, method] of Object.entries(methods2)) {
25695
25916
  Object.defineProperty(properties, name, { value: method, writable: true, configurable: true });
25696
25917
  }
25697
25918
  for (const [name, value] of Object.entries(wellKnownSymbols)) {
@@ -25707,28 +25928,28 @@ function createSymbolGlobal(budget) {
25707
25928
  }
25708
25929
 
25709
25930
  // packages/safe-js/src/interp/globals/bigint.ts
25931
+ async function sandboxBigInt(value, budget, context, allowNumber = false) {
25932
+ const primitive = value !== null && typeof value === "object" ? await objectToPrimitive(value, budget, context, /* @__PURE__ */ new Set(), "number") : value;
25933
+ if (typeof primitive === "bigint") return primitive;
25934
+ if (typeof primitive !== "string" && typeof primitive !== "boolean" && !(allowNumber && typeof primitive === "number"))
25935
+ throw new TypeError("Cannot convert value to BigInt.");
25936
+ const size = typeof primitive === "string" ? primitive.length : 256;
25937
+ budget.visitNode(size);
25938
+ const allocation = {};
25939
+ budget.setRetainedDataUsage(allocation, size);
25940
+ try {
25941
+ return BigInt(primitive);
25942
+ } finally {
25943
+ budget.setRetainedDataUsage(allocation, 0);
25944
+ }
25945
+ }
25710
25946
  function createBigIntGlobal(budget) {
25711
- const convert = async (value, context, allowNumber = false) => {
25712
- const primitive = value !== null && typeof value === "object" ? await objectToPrimitive(value, budget, context, /* @__PURE__ */ new Set(), "number") : value;
25713
- if (typeof primitive === "bigint") return primitive;
25714
- if (typeof primitive !== "string" && typeof primitive !== "boolean" && !(allowNumber && typeof primitive === "number"))
25715
- throw new TypeError("Cannot convert value to BigInt.");
25716
- const size = typeof primitive === "string" ? primitive.length : 256;
25717
- budget.visitNode(size);
25718
- const allocation = {};
25719
- budget.setRetainedDataUsage(allocation, size);
25720
- try {
25721
- return BigInt(primitive);
25722
- } finally {
25723
- budget.setRetainedDataUsage(allocation, 0);
25724
- }
25725
- };
25726
25947
  const constructor = createSandboxClosure({
25727
25948
  guest: true,
25728
25949
  sandbox: true,
25729
25950
  name: "BigInt",
25730
25951
  length: 1,
25731
- call: ([value], context) => convert(value, context, true)
25952
+ call: ([value], context) => sandboxBigInt(value, budget, context, true)
25732
25953
  });
25733
25954
  const prototype = /* @__PURE__ */ Object.create(null);
25734
25955
  Object.defineProperties(prototype, {
@@ -25792,7 +26013,7 @@ function createBigIntGlobal(budget) {
25792
26013
  const number = await sandboxNumber(bits, budget, context);
25793
26014
  const width = Number.isNaN(number) ? 0 : Math.trunc(number);
25794
26015
  if (width < 0 || !Number.isSafeInteger(width)) throw new RangeError("Invalid BigInt width.");
25795
- const value = await convert(input, context);
26016
+ const value = await sandboxBigInt(input, budget, context);
25796
26017
  const size = value.toString(16).length;
25797
26018
  budget.visitNode(size);
25798
26019
  if (width >= size * 4 + 1 && (name === "asIntN" || value >= 0n)) return value;
@@ -25813,7 +26034,7 @@ function createBigIntGlobal(budget) {
25813
26034
 
25814
26035
  // packages/safe-js/src/interp/globals/array-buffer.ts
25815
26036
  var resizeBuffer2 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resize")?.value;
25816
- var transferBuffer = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "transfer")?.value;
26037
+ var transferBuffer2 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "transfer")?.value;
25817
26038
  function createArrayBufferGlobal(budget) {
25818
26039
  const prototype = /* @__PURE__ */ Object.create(null);
25819
26040
  const constructor = createSandboxClosure({
@@ -25986,9 +26207,9 @@ function createArrayBufferGlobal(budget) {
25986
26207
  const count = Math.min(length, arrayBufferLength(receiver));
25987
26208
  budget.visitNode(count);
25988
26209
  new Uint8Array(result, 0, count).set(new Uint8Array(receiver, 0, count));
25989
- if (transferBuffer !== void 0) {
26210
+ if (transferBuffer2 !== void 0) {
25990
26211
  budget.provisionDataUsage(1)();
25991
- Reflect.apply(transferBuffer, receiver, [0]);
26212
+ Reflect.apply(transferBuffer2, receiver, [0]);
25992
26213
  } else {
25993
26214
  budget.provisionDataUsage(arrayBufferLength(receiver) + 1)();
25994
26215
  budget.visitNode(arrayBufferLength(receiver));
@@ -26056,6 +26277,129 @@ function createArrayBufferGlobal(budget) {
26056
26277
  return constructor;
26057
26278
  }
26058
26279
 
26280
+ // packages/safe-js/src/interp/globals/data-view.ts
26281
+ var methods = Object.fromEntries(["Int8", "Uint8", "Int16", "Uint16", "Int32", "Uint32", "Float32", "Float64", "BigInt64", "BigUint64"].flatMap((type) => ["get", "set"].map((operation) => [operation + type, Object.getOwnPropertyDescriptor(DataView.prototype, operation + type).value])));
26282
+ var readUint16 = DataView.prototype.getUint16;
26283
+ var writeUint16 = DataView.prototype.setUint16;
26284
+ methods.getFloat16 = function(...args) {
26285
+ const bits = Reflect.apply(readUint16, this, args);
26286
+ const sign = bits & 32768 ? -1 : 1;
26287
+ const exponent = bits >> 10 & 31, fraction = bits & 1023;
26288
+ if (exponent === 31) return fraction === 0 ? sign * Infinity : NaN;
26289
+ return sign * (exponent === 0 ? fraction * 2 ** -24 : (1 + fraction / 1024) * 2 ** (exponent - 15));
26290
+ };
26291
+ methods.setFloat16 = function(...args) {
26292
+ const rounded = f16round(args[1]);
26293
+ const sign = rounded < 0 || Object.is(rounded, -0) ? 32768 : 0;
26294
+ const magnitude = Math.abs(rounded);
26295
+ let bits;
26296
+ if (Number.isNaN(magnitude)) bits = 32256;
26297
+ else if (magnitude === Infinity) bits = 31744;
26298
+ else if (magnitude < 2 ** -14) bits = magnitude * 2 ** 24;
26299
+ else {
26300
+ const exponent = Math.floor(Math.log2(magnitude));
26301
+ bits = exponent + 15 << 10 | (magnitude / 2 ** exponent - 1) * 1024;
26302
+ }
26303
+ return Reflect.apply(writeUint16, this, [args[0], sign | bits, args[2]]);
26304
+ };
26305
+ function createDataViewGlobal(budget) {
26306
+ const prototype = /* @__PURE__ */ Object.create(null);
26307
+ const constructor = createSandboxClosure({
26308
+ guest: true,
26309
+ sandbox: true,
26310
+ name: "DataView",
26311
+ length: 1,
26312
+ call: () => {
26313
+ throw new TypeError("DataView requires new.");
26314
+ },
26315
+ construct: async (args, context) => {
26316
+ const buffer = args[0];
26317
+ if (!isSandboxArrayBuffer(buffer)) throw new TypeError("DataView requires an ArrayBuffer.");
26318
+ let selected;
26319
+ const release = retainValues(budget, () => [...args, selected]);
26320
+ try {
26321
+ const offset = await dataViewIndex(args[1], budget, context);
26322
+ if (arrayBufferDetached(buffer)) throw new TypeError("Cannot view a detached buffer.");
26323
+ const length = arrayBufferLength(buffer);
26324
+ if (offset > length) throw new RangeError("DataView offset exceeds buffer length.");
26325
+ const size = args[2] === void 0 ? void 0 : await dataViewIndex(args[2], budget, context);
26326
+ if (size !== void 0 && offset + size > length) throw new RangeError("DataView length exceeds buffer length.");
26327
+ const target = context?.newTarget ?? constructor;
26328
+ if (context?.getProperty !== void 0) selected = await context.getProperty(target, "prototype");
26329
+ else {
26330
+ const descriptor = getSandboxPropertyDescriptor(target, "prototype", budget);
26331
+ selected = descriptor === void 0 ? void 0 : await readPropertyDescriptor(descriptor, target, context);
26332
+ }
26333
+ budget.provisionDataUsage(1)();
26334
+ const value = new DataView(buffer, offset, size);
26335
+ if (arrayBufferOptions(buffer) !== void 0) dataViewLayouts.set(value, { byteOffset: offset, ...size === void 0 ? {} : { byteLength: size } });
26336
+ setSandboxPrototype(value, selected !== null && typeof selected === "object" ? selected : prototype, budget);
26337
+ return value;
26338
+ } finally {
26339
+ release();
26340
+ }
26341
+ }
26342
+ });
26343
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype });
26344
+ Object.defineProperties(prototype, {
26345
+ constructor: { value: constructor, writable: true, configurable: true },
26346
+ [Symbol.toStringTag]: { value: "DataView", configurable: true }
26347
+ });
26348
+ const getters = [];
26349
+ for (const [name, getter] of Object.entries(dataViewGetters)) {
26350
+ const closure = createSandboxClosure({
26351
+ guest: true,
26352
+ sandbox: true,
26353
+ name: `get ${name}`,
26354
+ length: 0,
26355
+ call: (_args, context) => {
26356
+ if (!isSandboxDataView(context?.thisValue)) throw new TypeError("DataView getter requires a DataView receiver.");
26357
+ return Reflect.apply(getter, context.thisValue, []);
26358
+ }
26359
+ });
26360
+ Object.defineProperty(prototype, name, { get: accessorAdapter(closure, "get"), configurable: true });
26361
+ getters.push(closure);
26362
+ }
26363
+ for (const [name, method] of Object.entries(methods)) {
26364
+ const setter = name.startsWith("set");
26365
+ Object.defineProperty(prototype, name, {
26366
+ writable: true,
26367
+ configurable: true,
26368
+ value: createSandboxClosure({
26369
+ guest: true,
26370
+ sandbox: true,
26371
+ name,
26372
+ length: setter ? 2 : 1,
26373
+ call: async (args, context) => {
26374
+ const receiver = context?.thisValue;
26375
+ if (!isSandboxDataView(receiver)) throw new TypeError("DataView method requires a DataView receiver.");
26376
+ const release = retainValues(budget, () => [receiver, ...args]);
26377
+ try {
26378
+ const offset = await dataViewIndex(args[0], budget, context);
26379
+ const value = setter ? name.startsWith("setBig") ? await sandboxBigInt(args[1], budget, context) : await sandboxNumber(args[1], budget, context) : void 0;
26380
+ return Reflect.apply(method, receiver, setter ? [offset, value, Boolean(args[2])] : [offset, Boolean(args[1])]);
26381
+ } finally {
26382
+ release();
26383
+ }
26384
+ }
26385
+ })
26386
+ });
26387
+ }
26388
+ setSandboxPrototype(prototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
26389
+ dataViewPrototypes.set(budget, prototype);
26390
+ registerBuiltinIdentities(budget, { DataView: constructor });
26391
+ registerIntrinsicFunction(budget, constructor);
26392
+ for (const getter of getters) registerIntrinsicFunction(budget, getter);
26393
+ registerIntrinsicObject(budget, prototype);
26394
+ return constructor;
26395
+ }
26396
+ async function dataViewIndex(value, budget, context) {
26397
+ const number = await sandboxNumber(value, budget, context);
26398
+ const index = Number.isNaN(number) ? 0 : Math.trunc(number);
26399
+ if (!Number.isSafeInteger(index) || index < 0) throw new RangeError("Invalid DataView index.");
26400
+ return index;
26401
+ }
26402
+
26059
26403
  // packages/safe-js/src/interp/globals.ts
26060
26404
  function createBuiltinBindings(options) {
26061
26405
  const bindings = {
@@ -26069,6 +26413,7 @@ function createBuiltinBindings(options) {
26069
26413
  ...createMathGlobals({ random: options.random, budget: options.budget }),
26070
26414
  ...createObjectArrayGlobals(options),
26071
26415
  ArrayBuffer: createArrayBufferGlobal(options.budget),
26416
+ DataView: createDataViewGlobal(options.budget),
26072
26417
  ...createMiscGlobals(options),
26073
26418
  ...createUriGlobals(options.budget),
26074
26419
  ...createPromiseGlobals(options),
@@ -26805,8 +27150,9 @@ function validateDumpHeap(root, state) {
26805
27150
  validateDateRecord(entry, path);
26806
27151
  continue;
26807
27152
  }
26808
- if (entry.kind === "arraybuffer") {
26809
- validateArrayBufferStorage(entry);
27153
+ if (entry.kind === "arraybuffer" || entry.kind === "dataview") {
27154
+ if (entry.kind === "dataview") validateDataViewStorage(entry);
27155
+ else validateArrayBufferStorage(entry);
26810
27156
  validateGuestHeapNode({ kind: "guest-object", state: entry.state }, heap, state.limits.maxEntries);
26811
27157
  continue;
26812
27158
  }
@@ -27033,7 +27379,7 @@ function validateHeapValue(value, path, state, heap) {
27033
27379
  fail("invalidValue", path, String(error));
27034
27380
  }
27035
27381
  validateErrorType(record4, path);
27036
- if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "typedarray", "arraybuffer", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
27382
+ if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "typedarray", "arraybuffer", "dataview", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
27037
27383
  fail("unknownTag", `${path}.kind`, "unknown heap tag");
27038
27384
  validateValue(record4, path, 1, state);
27039
27385
  if (record4.kind === "symbol") validateSymbolRecord(record4, path, state);
@@ -27042,8 +27388,9 @@ function validateHeapValue(value, path, state, heap) {
27042
27388
  if (record4.kind === "object") requireRecord(record4.entries, `${path}.entries`);
27043
27389
  if (record4.kind === "boxed") validateBoxedRecord(record4, path, heap);
27044
27390
  if (record4.kind === "date") validateDateRecord(record4, path);
27045
- if (record4.kind === "arraybuffer") {
27046
- validateArrayBufferStorage(record4);
27391
+ if (record4.kind === "arraybuffer" || record4.kind === "dataview") {
27392
+ if (record4.kind === "dataview") validateDataViewStorage(record4);
27393
+ else validateArrayBufferStorage(record4);
27047
27394
  validateGuestHeapNode({ kind: "guest-object", state: record4.state }, heap, state.limits.maxEntries);
27048
27395
  }
27049
27396
  if (record4.kind === "float32array" || record4.kind === "typedarray") {
@@ -27242,7 +27589,7 @@ function validateGenericValue(value, path, depth, state) {
27242
27589
  if (typeof value === "object" && value !== null && hasGuestObjectState(value)) {
27243
27590
  fail("invalidState", path, "guest function properties, prototype links and custom descriptors cannot be restored");
27244
27591
  }
27245
- if (state.dataPropertiesOnly && types7.isProxy(value)) {
27592
+ if (state.dataPropertiesOnly && types8.isProxy(value)) {
27246
27593
  fail("invalidType", path, "proxy objects are not snapshot data");
27247
27594
  }
27248
27595
  if (depth > state.limits.maxDepth)
@@ -28042,6 +28389,7 @@ function measureSandboxData(values, options = {}) {
28042
28389
  const prototype = getSandboxPrototype(value);
28043
28390
  if (prototype !== null) visit(prototype, depth + 1);
28044
28391
  if (isSandboxArrayBuffer(value)) usage += arrayBufferLength(value);
28392
+ if (isSandboxDataView(value)) visit(dataViewBuffer(value), depth + 1);
28045
28393
  if (isNumericTypedArray(value)) {
28046
28394
  const storage = typedArrayStorage(value);
28047
28395
  visit(storage.buffer, depth + 1);
@@ -28187,7 +28535,7 @@ function measureSandboxData(values, options = {}) {
28187
28535
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
28188
28536
  if (descriptor !== void 0) descriptors2.push([key, descriptor]);
28189
28537
  }
28190
- const includeNonEnumerable = isSandboxDate(value) || isSandboxArrayBuffer(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
28538
+ const includeNonEnumerable = isSandboxDate(value) || isSandboxArrayBuffer(value) || isSandboxDataView(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
28191
28539
  for (const [key, descriptor] of descriptors2) {
28192
28540
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
28193
28541
  usage += 1 + key.length;
@@ -28319,7 +28667,7 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
28319
28667
  }
28320
28668
  return copy;
28321
28669
  }
28322
- if (typeof value === "object" && value !== null && hasGuestObjectState(value) && !(state.structuredClone && (isSandboxDate(value) || isSandboxArrayBuffer(value) || isNumericTypedArray(value)))) {
28670
+ if (typeof value === "object" && value !== null && hasGuestObjectState(value) && !(state.structuredClone && (isSandboxDate(value) || isSandboxArrayBuffer(value) || isSandboxDataView(value) || isNumericTypedArray(value)))) {
28323
28671
  throw new TypeError("Guest prototype links and custom descriptors cannot be copied as data.");
28324
28672
  }
28325
28673
  const primitive = nativeBoxedValue(value);
@@ -28411,6 +28759,30 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
28411
28759
  if (!state.structuredClone && !Object.isExtensible(value)) Object.preventExtensions(copy);
28412
28760
  return copy;
28413
28761
  }
28762
+ if (isSandboxDataView(value)) {
28763
+ const existing = state.seen.get(value);
28764
+ if (existing !== void 0) return existing;
28765
+ if (state.structuredClone) {
28766
+ try {
28767
+ Reflect.apply(dataViewGetters.byteLength, value, []);
28768
+ } catch (error) {
28769
+ if (!(error instanceof TypeError)) throw error;
28770
+ throw new DOMException("Cannot clone an out-of-bounds DataView.", "DataCloneError");
28771
+ }
28772
+ }
28773
+ const copy = copyDataViewStorage(value, state);
28774
+ state.seen.set(value, copy);
28775
+ if (state.structuredClone) return copy;
28776
+ copyToSandbox(dataViewBuffer(value), state, `${path}.buffer`, cloneSandboxCollections, depth + 1);
28777
+ for (const [key, descriptor] of dataViewDataProperties(value)) {
28778
+ Object.defineProperty(copy, key, {
28779
+ ...descriptor,
28780
+ value: copyToSandbox(descriptor.value, state, joinPath2(path, key), cloneSandboxCollections, depth + 1)
28781
+ });
28782
+ }
28783
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
28784
+ return copy;
28785
+ }
28414
28786
  if (isNumericTypedArray(value)) {
28415
28787
  const existing = state.seen.get(value);
28416
28788
  if (existing !== void 0) return existing;
@@ -28657,12 +29029,13 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
28657
29029
  if (!Object.isExtensible(value)) Object.preventExtensions(copy);
28658
29030
  return copy;
28659
29031
  }
28660
- if (isSandboxArrayBuffer(value)) {
29032
+ if (isSandboxArrayBuffer(value) || isSandboxDataView(value)) {
28661
29033
  const existing = state.seen.get(value);
28662
29034
  if (existing !== void 0) return existing;
28663
- const copy = copyArrayBufferStorage(value, state);
29035
+ const copy = isSandboxDataView(value) ? copyDataViewStorage(value, state) : copyArrayBufferStorage(value, state);
28664
29036
  state.seen.set(value, copy);
28665
- for (const [key, descriptor] of arrayBufferDataProperties(value)) {
29037
+ if (isSandboxDataView(value)) copyFromSandbox(dataViewBuffer(value), state, `${path}.buffer`, options, depth + 1);
29038
+ for (const [key, descriptor] of isSandboxDataView(value) ? dataViewDataProperties(value) : arrayBufferDataProperties(value)) {
28666
29039
  Object.defineProperty(copy, key, {
28667
29040
  ...descriptor,
28668
29041
  value: copyFromSandbox(descriptor.value, state, joinPath2(path, key), options, depth + 1)
@@ -28832,6 +29205,16 @@ function allocateSandboxValue2(value, budget, seen) {
28832
29205
  budget.allocateString(value);
28833
29206
  return;
28834
29207
  }
29208
+ if (isSandboxDataView(value)) {
29209
+ if (seen.has(value)) return;
29210
+ seen.add(value);
29211
+ allocateSandboxValue2(dataViewBuffer(value), budget, seen);
29212
+ for (const [key, descriptor] of dataViewDataProperties(value)) {
29213
+ if (typeof key === "string") budget.allocateString(key);
29214
+ allocateSandboxValue2(descriptor.value, budget, seen);
29215
+ }
29216
+ return;
29217
+ }
28835
29218
  if (isSandboxArrayBuffer(value)) {
28836
29219
  if (seen.has(value)) return;
28837
29220
  seen.add(value);
@@ -29590,6 +29973,21 @@ function normalize(value, seen) {
29590
29973
  try {
29591
29974
  const date = copyNativeDate(value);
29592
29975
  if (date !== void 0) return Object.assign(/* @__PURE__ */ Object.create(null), { $type: "date", time: serializedDateTime(date) });
29976
+ if (isSandboxDataView(value)) {
29977
+ const properties = /* @__PURE__ */ Object.create(null);
29978
+ const entries = dataViewDataProperties(value);
29979
+ if (entries.some(([key]) => typeof key !== "string")) throw new TypeError("DataView symbol properties require an explicit host-call identity.");
29980
+ for (const [key, descriptor] of entries.sort(([left], [right]) => String(left) < String(right) ? -1 : String(left) > String(right) ? 1 : 0))
29981
+ defineOwnDataProperty(properties, String(key), normalize(descriptor.value, seen));
29982
+ const layout = dataViewLayout(value);
29983
+ return Object.assign(/* @__PURE__ */ Object.create(null), {
29984
+ $type: "dataview",
29985
+ byteOffset: layout.byteOffset,
29986
+ byteLength: layout.byteLength ?? null,
29987
+ buffer: normalize(dataViewBuffer(value), seen),
29988
+ properties
29989
+ });
29990
+ }
29593
29991
  if (isSandboxArrayBuffer(value)) {
29594
29992
  arrayBufferLength(value);
29595
29993
  const properties = /* @__PURE__ */ Object.create(null);
@@ -30582,6 +30980,7 @@ var KNOWN_RUNTIME_GLOBALS = [
30582
30980
  "AggregateError",
30583
30981
  "Array",
30584
30982
  "ArrayBuffer",
30983
+ "DataView",
30585
30984
  "Boolean",
30586
30985
  "BigInt",
30587
30986
  "Date",
@@ -40982,7 +41381,7 @@ function createReplayableRandom(options = {}) {
40982
41381
 
40983
41382
  // packages/safe-js/src/realm.ts
40984
41383
  import { AsyncLocalStorage as AsyncLocalStorage6 } from "node:async_hooks";
40985
- import { types as types8 } from "node:util";
41384
+ import { types as types9 } from "node:util";
40986
41385
 
40987
41386
  // packages/safe-js/src/modules/registry.ts
40988
41387
  function createUnknownModuleMessage2(moduleName, moduleNames) {
@@ -41121,7 +41520,7 @@ var RealmState = class {
41121
41520
  throw new TypeError("Realm limits must be positive safe integers with supported names.");
41122
41521
  this.limits[name] = Number(value);
41123
41522
  }
41124
- if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types8.isProxy(options.extensions)))
41523
+ if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types9.isProxy(options.extensions)))
41125
41524
  throw new TypeError("Extensions must be a registration array.");
41126
41525
  const registrations = options.extensions ?? [];
41127
41526
  const extensions = [];
@@ -41356,7 +41755,7 @@ var RealmState = class {
41356
41755
  return this.phase.run(phase, () => {
41357
41756
  try {
41358
41757
  const result = call();
41359
- if (types8.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
41758
+ if (types9.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
41360
41759
  return Promise.resolve(result).then(
41361
41760
  async (value) => {
41362
41761
  await Promise.allSettled(phase.pending);
@@ -41425,7 +41824,7 @@ var RealmState = class {
41425
41824
  },
41426
41825
  read: (operation, validate) => {
41427
41826
  const value = this.invokeHost(operation, operation);
41428
- if (types8.isPromise(value)) {
41827
+ if (types9.isPromise(value)) {
41429
41828
  void Promise.resolve(value).catch(() => void 0);
41430
41829
  throw new TypeError("Live property operations must be synchronous.");
41431
41830
  }
@@ -41433,7 +41832,7 @@ var RealmState = class {
41433
41832
  },
41434
41833
  write: (operation, value) => {
41435
41834
  const result = this.invokeHost(operation, () => operation(this.exportValue(value)));
41436
- if (types8.isPromise(result)) {
41835
+ if (types9.isPromise(result)) {
41437
41836
  void Promise.resolve(result).catch(() => void 0);
41438
41837
  throw new TypeError("Live property setters must be synchronous.");
41439
41838
  }
@@ -41642,7 +42041,7 @@ var RealmState = class {
41642
42041
  }
41643
42042
  });
41644
42043
  const output = getExtensionSetup(extension)(context);
41645
- if (types8.isPromise(output)) {
42044
+ if (types9.isPromise(output)) {
41646
42045
  void Promise.resolve(output).catch(() => void 0);
41647
42046
  throw new TypeError("Extension setup must be synchronous.");
41648
42047
  }
@@ -41834,7 +42233,7 @@ var RealmState = class {
41834
42233
  };
41835
42234
  function readModules(input) {
41836
42235
  const entries = (value, label) => {
41837
- if (types8.isMap(value) && !types8.isProxy(value)) {
42236
+ if (types9.isMap(value) && !types9.isProxy(value)) {
41838
42237
  const result = [...Map.prototype.entries.call(value)];
41839
42238
  if (result.length > 4096 || result.some(([key]) => typeof key !== "string" || key.length === 0))
41840
42239
  throw new TypeError(`${label} requires bounded string keys.`);
@@ -43103,4 +43502,4 @@ export {
43103
43502
  FileSnapshotBackend,
43104
43503
  run
43105
43504
  };
43106
- //# sourceMappingURL=chunk-OTFEHCGJ.js.map
43505
+ //# sourceMappingURL=chunk-BKYW2WCU.js.map