boa_runtime 0.21.1

Example runtime for the Boa JavaScript engine.
Documentation
// https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone#transferring_an_object

{
  // Create an ArrayBuffer with a size in bytes
  const buffer = new ArrayBuffer(16);

  const object1 = {
    buffer,
  };

  // Clone the object containing the buffer, and transfer it
  const object2 = structuredClone(object1, { transfer: [buffer] });

  // Create an array from the cloned buffer
  const int32View2 = new Int32Array(object2.buffer);
  int32View2[0] = 42;
  assertEq(int32View2[0], 42);

  // Creating an array from the original buffer throws a TypeError
  assertThrows(() => {
    const int32View1 = new Int32Array(object1.buffer);
  });
}

{
  // Verify we can transfer the buffer of a typed array.
  const array = new Uint8Array([1, 2, 3, 4]);
  const object1 = {
    array,
  };

  const object2 = structuredClone(object1, { transfer: [array.buffer] });

  assert(object2.array !== array);
  assertEq(object1.array.byteLength, 0);
  assertArrayEqual(object2.array, [1, 2, 3, 4]);
}

assertThrows(() => structuredClone({}, { transfer: [1] }));
assertThrows(() => structuredClone({}, { transfer: ["error"] }));
assertThrows(() => structuredClone({}, { transfer: [{}] }));
assertThrows(() => structuredClone({}, { transfer: [new Uint8Array([1, 2])] }));