2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 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" />
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2021-07-03 15:32:28 -04:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2021-07-06 08:38:12 -04:00
|
|
|
const {
|
|
|
|
ArrayBuffer,
|
|
|
|
ArrayBufferIsView,
|
|
|
|
DataView,
|
|
|
|
TypedArrayPrototypeSlice,
|
|
|
|
TypeError,
|
|
|
|
WeakMap,
|
|
|
|
WeakMapPrototypeSet,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-07 08:19:33 -04:00
|
|
|
|
|
|
|
const objectCloneMemo = new WeakMap();
|
|
|
|
|
|
|
|
function cloneArrayBuffer(
|
|
|
|
srcBuffer,
|
|
|
|
srcByteOffset,
|
|
|
|
srcLength,
|
|
|
|
_cloneConstructor,
|
|
|
|
) {
|
|
|
|
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
|
2021-07-06 08:38:12 -04:00
|
|
|
return TypedArrayPrototypeSlice(
|
|
|
|
srcBuffer,
|
2021-06-07 08:19:33 -04:00
|
|
|
srcByteOffset,
|
|
|
|
srcByteOffset + srcLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clone a value in a similar way to structured cloning. It is similar to a
|
2021-09-02 18:28:12 -04:00
|
|
|
* StructureDeserialize(StructuredSerialize(...)). */
|
2021-06-07 08:19:33 -04:00
|
|
|
function structuredClone(value) {
|
|
|
|
// Performance optimization for buffers, otherwise
|
|
|
|
// `serialize/deserialize` will allocate new buffer.
|
|
|
|
if (value instanceof ArrayBuffer) {
|
|
|
|
const cloned = cloneArrayBuffer(
|
|
|
|
value,
|
|
|
|
0,
|
|
|
|
value.byteLength,
|
|
|
|
ArrayBuffer,
|
|
|
|
);
|
2021-07-06 08:38:12 -04:00
|
|
|
WeakMapPrototypeSet(objectCloneMemo, value, cloned);
|
2021-06-07 08:19:33 -04:00
|
|
|
return cloned;
|
|
|
|
}
|
2021-07-06 08:38:12 -04:00
|
|
|
if (ArrayBufferIsView(value)) {
|
2021-06-07 08:19:33 -04:00
|
|
|
const clonedBuffer = structuredClone(value.buffer);
|
|
|
|
// Use DataViewConstructor type purely for type-checking, can be a
|
|
|
|
// DataView or TypedArray. They use the same constructor signature,
|
|
|
|
// only DataView has a length in bytes and TypedArrays use a length in
|
|
|
|
// terms of elements, so we adjust for that.
|
|
|
|
let length;
|
|
|
|
if (value instanceof DataView) {
|
|
|
|
length = value.byteLength;
|
|
|
|
} else {
|
|
|
|
length = value.length;
|
|
|
|
}
|
|
|
|
return new (value.constructor)(
|
|
|
|
clonedBuffer,
|
|
|
|
value.byteOffset,
|
|
|
|
length,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return core.deserialize(core.serialize(value));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TypeError) {
|
|
|
|
throw new DOMException("Uncloneable value", "DataCloneError");
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.structuredClone = structuredClone;
|
|
|
|
})(globalThis);
|