summaryrefslogtreecommitdiff
path: root/ext/web/02_structured_clone.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /ext/web/02_structured_clone.js
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/web/02_structured_clone.js')
-rw-r--r--ext/web/02_structured_clone.js239
1 files changed, 118 insertions, 121 deletions
diff --git a/ext/web/02_structured_clone.js b/ext/web/02_structured_clone.js
index 793cb1c75..373ae0ab2 100644
--- a/ext/web/02_structured_clone.js
+++ b/ext/web/02_structured_clone.js
@@ -6,138 +6,135 @@
/// <reference path="../web/internal.d.ts" />
/// <reference path="../web/lib.deno_web.d.ts" />
-"use strict";
+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;
-((window) => {
- const core = window.Deno.core;
- const { DOMException } = window.__bootstrap.domException;
- 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,
- } = window.__bootstrap.primordials;
+const objectCloneMemo = new WeakMap();
- const objectCloneMemo = new WeakMap();
-
- function cloneArrayBuffer(
+function cloneArrayBuffer(
+ srcBuffer,
+ srcByteOffset,
+ srcLength,
+ _cloneConstructor,
+) {
+ // this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
+ return ArrayBufferPrototypeSlice(
srcBuffer,
srcByteOffset,
- srcLength,
- _cloneConstructor,
- ) {
- // this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
- return ArrayBufferPrototypeSlice(
- srcBuffer,
- srcByteOffset,
- srcByteOffset + srcLength,
+ 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,
);
+ WeakMapPrototypeSet(objectCloneMemo, value, cloned);
+ return cloned;
}
- // 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,
+ if (ArrayBufferIsView(value)) {
+ const tag = TypedArrayPrototypeGetSymbolToStringTag(value);
+ // DataView
+ if (tag === undefined) {
+ return new DataView(
+ structuredClone(DataViewPrototypeGetBuffer(value)),
+ DataViewPrototypeGetByteOffset(value),
+ DataViewPrototypeGetByteLength(value),
);
- WeakMapPrototypeSet(objectCloneMemo, value, cloned);
- return cloned;
}
-
- if (ArrayBufferIsView(value)) {
- const tag = TypedArrayPrototypeGetSymbolToStringTag(value);
- // DataView
- if (tag === undefined) {
- return new DataView(
- structuredClone(DataViewPrototypeGetBuffer(value)),
- DataViewPrototypeGetByteOffset(value),
- DataViewPrototypeGetByteLength(value),
- );
- }
- // 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;
- }
- return new Constructor(
- structuredClone(TypedArrayPrototypeGetBuffer(value)),
- TypedArrayPrototypeGetByteOffset(value),
- TypedArrayPrototypeGetLength(value),
- );
+ // 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;
}
+ return new Constructor(
+ structuredClone(TypedArrayPrototypeGetBuffer(value)),
+ TypedArrayPrototypeGetByteOffset(value),
+ TypedArrayPrototypeGetLength(value),
+ );
+ }
- try {
- return core.deserialize(core.serialize(value));
- } catch (e) {
- if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
- throw new DOMException(e.message, "DataCloneError");
- }
- throw e;
+ try {
+ return core.deserialize(core.serialize(value));
+ } catch (e) {
+ if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
+ throw new DOMException(e.message, "DataCloneError");
}
+ throw e;
}
+}
- window.__bootstrap.structuredClone = structuredClone;
-})(globalThis);
+export { structuredClone };