{
const buffer = new ArrayBuffer(16);
const object1 = {
buffer,
};
const object2 = structuredClone(object1, { transfer: [buffer] });
const int32View2 = new Int32Array(object2.buffer);
int32View2[0] = 42;
assertEq(int32View2[0], 42);
assertThrows(() => {
const int32View1 = new Int32Array(object1.buffer);
});
}
{
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])] }));