2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-19 17:30:59 -04:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function requiredArguments(
|
|
|
|
name,
|
|
|
|
length,
|
|
|
|
required,
|
|
|
|
) {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const objectCloneMemo = new WeakMap();
|
|
|
|
|
|
|
|
function cloneArrayBuffer(
|
|
|
|
srcBuffer,
|
|
|
|
srcByteOffset,
|
|
|
|
srcLength,
|
|
|
|
_cloneConstructor,
|
|
|
|
) {
|
|
|
|
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
|
|
|
|
return srcBuffer.slice(
|
|
|
|
srcByteOffset,
|
|
|
|
srcByteOffset + srcLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clone a value in a similar way to structured cloning. It is similar to a
|
|
|
|
* StructureDeserialize(StructuredSerialize(...)). */
|
|
|
|
function cloneValue(value) {
|
|
|
|
switch (typeof value) {
|
|
|
|
case "number":
|
|
|
|
case "string":
|
|
|
|
case "boolean":
|
|
|
|
case "undefined":
|
|
|
|
case "bigint":
|
|
|
|
return value;
|
|
|
|
case "object": {
|
|
|
|
if (objectCloneMemo.has(value)) {
|
|
|
|
return objectCloneMemo.get(value);
|
|
|
|
}
|
|
|
|
if (value === null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value instanceof Date) {
|
|
|
|
return new Date(value.valueOf());
|
|
|
|
}
|
|
|
|
if (value instanceof RegExp) {
|
|
|
|
return new RegExp(value);
|
|
|
|
}
|
|
|
|
if (value instanceof SharedArrayBuffer) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value instanceof ArrayBuffer) {
|
|
|
|
const cloned = cloneArrayBuffer(
|
|
|
|
value,
|
|
|
|
0,
|
|
|
|
value.byteLength,
|
|
|
|
ArrayBuffer,
|
|
|
|
);
|
|
|
|
objectCloneMemo.set(value, cloned);
|
|
|
|
return cloned;
|
|
|
|
}
|
|
|
|
if (ArrayBuffer.isView(value)) {
|
|
|
|
const clonedBuffer = cloneValue(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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (value instanceof Map) {
|
|
|
|
const clonedMap = new Map();
|
|
|
|
objectCloneMemo.set(value, clonedMap);
|
2020-11-21 11:29:18 -05:00
|
|
|
value.forEach((v, k) => {
|
|
|
|
clonedMap.set(cloneValue(k), cloneValue(v));
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
return clonedMap;
|
|
|
|
}
|
|
|
|
if (value instanceof Set) {
|
2020-11-21 11:29:18 -05:00
|
|
|
// assumes that cloneValue still takes only one argument
|
|
|
|
const clonedSet = new Set([...value].map(cloneValue));
|
2020-07-19 13:49:44 -04:00
|
|
|
objectCloneMemo.set(value, clonedSet);
|
|
|
|
return clonedSet;
|
|
|
|
}
|
|
|
|
|
2020-11-21 11:29:18 -05:00
|
|
|
// default for objects
|
2020-07-19 13:49:44 -04:00
|
|
|
const clonedObj = {};
|
|
|
|
objectCloneMemo.set(value, clonedObj);
|
|
|
|
const sourceKeys = Object.getOwnPropertyNames(value);
|
|
|
|
for (const key of sourceKeys) {
|
|
|
|
clonedObj[key] = cloneValue(value[key]);
|
|
|
|
}
|
2020-11-21 11:29:18 -05:00
|
|
|
Reflect.setPrototypeOf(clonedObj, Reflect.getPrototypeOf(value));
|
2020-07-19 13:49:44 -04:00
|
|
|
return clonedObj;
|
|
|
|
}
|
|
|
|
case "symbol":
|
|
|
|
case "function":
|
|
|
|
default:
|
|
|
|
throw new DOMException("Uncloneable value in stream", "DataCloneError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 22:34:42 -05:00
|
|
|
const handlerSymbol = Symbol("eventHandlers");
|
|
|
|
function makeWrappedHandler(handler) {
|
|
|
|
function wrappedHandler(...args) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wrappedHandler.handler.call(this, ...args);
|
|
|
|
}
|
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
2020-12-07 15:22:58 -05:00
|
|
|
function defineEventHandler(emitter, name, defaultValue = undefined) {
|
2020-11-09 22:34:42 -05:00
|
|
|
// HTML specification section 8.1.5.1
|
|
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
2020-12-07 15:22:58 -05:00
|
|
|
return this[handlerSymbol]?.get(name)?.handler ?? defaultValue;
|
2020-11-09 22:34:42 -05:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
this[handlerSymbol] = new Map();
|
|
|
|
}
|
|
|
|
let handlerWrapper = this[handlerSymbol]?.get(name);
|
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else {
|
|
|
|
handlerWrapper = makeWrappedHandler(value);
|
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
}
|
|
|
|
this[handlerSymbol].set(name, handlerWrapper);
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.webUtil = {
|
2020-09-19 17:30:59 -04:00
|
|
|
illegalConstructorKey,
|
2020-07-19 13:49:44 -04:00
|
|
|
requiredArguments,
|
2020-11-09 22:34:42 -05:00
|
|
|
defineEventHandler,
|
2020-07-19 13:49:44 -04:00
|
|
|
cloneValue,
|
|
|
|
};
|
|
|
|
})(this);
|