2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-06-07 08:19:33 -04:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
2021-07-06 08:38:12 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-06-07 08:19:33 -04:00
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
import DOMException from "internal:ext/web/01_dom_exception.js";
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayBuffer,
|
|
|
|
ArrayBufferPrototype,
|
|
|
|
ArrayBufferPrototypeGetByteLength,
|
|
|
|
ArrayBufferPrototypeSlice,
|
|
|
|
ArrayBufferIsView,
|
|
|
|
DataView,
|
|
|
|
DataViewPrototypeGetBuffer,
|
|
|
|
DataViewPrototypeGetByteLength,
|
|
|
|
DataViewPrototypeGetByteOffset,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
TypedArrayPrototypeGetBuffer,
|
|
|
|
TypedArrayPrototypeGetByteOffset,
|
|
|
|
TypedArrayPrototypeGetLength,
|
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
|
|
|
TypeErrorPrototype,
|
|
|
|
WeakMap,
|
|
|
|
WeakMapPrototypeSet,
|
|
|
|
Int8Array,
|
|
|
|
Int16Array,
|
|
|
|
Int32Array,
|
|
|
|
BigInt64Array,
|
|
|
|
Uint8Array,
|
|
|
|
Uint8ClampedArray,
|
|
|
|
Uint16Array,
|
|
|
|
Uint32Array,
|
|
|
|
BigUint64Array,
|
|
|
|
Float32Array,
|
|
|
|
Float64Array,
|
|
|
|
} = primordials;
|
2021-06-07 08:19:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const objectCloneMemo = new WeakMap();
|
2021-06-07 08:19:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function cloneArrayBuffer(
|
|
|
|
srcBuffer,
|
|
|
|
srcByteOffset,
|
|
|
|
srcLength,
|
|
|
|
_cloneConstructor,
|
|
|
|
) {
|
|
|
|
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
|
|
|
|
return ArrayBufferPrototypeSlice(
|
2021-06-07 08:19:33 -04:00
|
|
|
srcBuffer,
|
|
|
|
srcByteOffset,
|
2023-02-07 14:22:46 -05:00
|
|
|
srcByteOffset + srcLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(petamoriken): Resizable ArrayBuffer support in the future
|
|
|
|
/** Clone a value in a similar way to structured cloning. It is similar to a
|
|
|
|
* StructureDeserialize(StructuredSerialize(...)). */
|
|
|
|
function structuredClone(value) {
|
|
|
|
// Performance optimization for buffers, otherwise
|
|
|
|
// `serialize/deserialize` will allocate new buffer.
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, value)) {
|
|
|
|
const cloned = cloneArrayBuffer(
|
|
|
|
value,
|
|
|
|
0,
|
|
|
|
ArrayBufferPrototypeGetByteLength(value),
|
|
|
|
ArrayBuffer,
|
2021-06-07 08:19:33 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
WeakMapPrototypeSet(objectCloneMemo, value, cloned);
|
|
|
|
return cloned;
|
2021-06-07 08:19:33 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ArrayBufferIsView(value)) {
|
|
|
|
const tag = TypedArrayPrototypeGetSymbolToStringTag(value);
|
|
|
|
// DataView
|
|
|
|
if (tag === undefined) {
|
|
|
|
return new DataView(
|
|
|
|
structuredClone(DataViewPrototypeGetBuffer(value)),
|
|
|
|
DataViewPrototypeGetByteOffset(value),
|
|
|
|
DataViewPrototypeGetByteLength(value),
|
2021-06-07 08:19:33 -04:00
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
// TypedArray
|
|
|
|
let Constructor;
|
|
|
|
switch (tag) {
|
|
|
|
case "Int8Array":
|
|
|
|
Constructor = Int8Array;
|
|
|
|
break;
|
|
|
|
case "Int16Array":
|
|
|
|
Constructor = Int16Array;
|
|
|
|
break;
|
|
|
|
case "Int32Array":
|
|
|
|
Constructor = Int32Array;
|
|
|
|
break;
|
|
|
|
case "BigInt64Array":
|
|
|
|
Constructor = BigInt64Array;
|
|
|
|
break;
|
|
|
|
case "Uint8Array":
|
|
|
|
Constructor = Uint8Array;
|
|
|
|
break;
|
|
|
|
case "Uint8ClampedArray":
|
|
|
|
Constructor = Uint8ClampedArray;
|
|
|
|
break;
|
|
|
|
case "Uint16Array":
|
|
|
|
Constructor = Uint16Array;
|
|
|
|
break;
|
|
|
|
case "Uint32Array":
|
|
|
|
Constructor = Uint32Array;
|
|
|
|
break;
|
|
|
|
case "BigUint64Array":
|
|
|
|
Constructor = BigUint64Array;
|
|
|
|
break;
|
|
|
|
case "Float32Array":
|
|
|
|
Constructor = Float32Array;
|
|
|
|
break;
|
|
|
|
case "Float64Array":
|
|
|
|
Constructor = Float64Array;
|
|
|
|
break;
|
2021-06-07 08:19:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return new Constructor(
|
|
|
|
structuredClone(TypedArrayPrototypeGetBuffer(value)),
|
|
|
|
TypedArrayPrototypeGetByteOffset(value),
|
|
|
|
TypedArrayPrototypeGetLength(value),
|
|
|
|
);
|
|
|
|
}
|
2021-06-07 08:19:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
return core.deserialize(core.serialize(value));
|
|
|
|
} catch (e) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
|
|
|
|
throw new DOMException(e.message, "DataCloneError");
|
2021-06-07 08:19:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw e;
|
2021-06-07 08:19:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-07 08:19:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { structuredClone };
|