jsdom
Version:
A JavaScript implementation of many web standards
685 lines (616 loc) • 20.3 kB
JavaScript
;
const conversions = require("webidl-conversions");
// Returns "Type(value) is Object" in ES terminology.
function isObject(value) {
return (typeof value === "object" && value !== null) || typeof value === "function";
}
const call = Function.call.bind(Function.call);
// Like `Object.assign`, but using `[[GetOwnProperty]]` and `[[DefineOwnProperty]]`
// instead of `[[Get]]` and `[[Set]]` and only allowing objects
function define(target, source) {
for (const key of Reflect.ownKeys(source)) {
const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
if (descriptor && !Reflect.defineProperty(target, key, descriptor)) {
throw new TypeError(`Cannot redefine property: ${String(key)}`);
}
}
}
function newObjectInRealm(globalObject, object) {
const ctorRegistry = initCtorRegistry(globalObject);
return Object.defineProperties(
Object.create(ctorRegistry["%Object.prototype%"]),
Object.getOwnPropertyDescriptors(object),
);
}
const wrapperSymbol = Symbol("wrapper");
const sameObjectCaches = Symbol("SameObject caches");
const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");
const AsyncIteratorPrototype = Object.getPrototypeOf(
Object.getPrototypeOf(async function* () {}).prototype,
);
function initCtorRegistry(globalObject) {
if (Object.hasOwn(globalObject, ctorRegistrySymbol)) {
return globalObject[ctorRegistrySymbol];
}
const ctorRegistry = Object.create(null);
// In addition to registering all the WebIDL2JS-generated types in the constructor registry,
// we also register a few intrinsics that we make use of in generated code, since they are not
// easy to grab from the globalObject variable.
ctorRegistry["%Object.prototype%"] = globalObject.Object.prototype;
ctorRegistry["%Array.prototype%"] = globalObject.Array.prototype;
ctorRegistry["%IteratorPrototype%"] = Object.getPrototypeOf(
Object.getPrototypeOf(new globalObject.Array()[Symbol.iterator]()),
);
try {
ctorRegistry["%AsyncIteratorPrototype%"] = Object.getPrototypeOf(
Object.getPrototypeOf(globalObject.eval("(async function* () {})").prototype),
);
} catch {
ctorRegistry["%AsyncIteratorPrototype%"] = AsyncIteratorPrototype;
}
globalObject[ctorRegistrySymbol] = ctorRegistry;
return ctorRegistry;
}
function getSameObject(wrapper, prop, creator) {
if (!wrapper[sameObjectCaches]) {
wrapper[sameObjectCaches] = Object.create(null);
}
if (prop in wrapper[sameObjectCaches]) {
return wrapper[sameObjectCaches][prop];
}
wrapper[sameObjectCaches][prop] = creator();
return wrapper[sameObjectCaches][prop];
}
function createInterfaceDescriptor(getParent) {
// A parent's module can import its child, so resolve ancestry only after the modules have loaded.
const descriptor = {
get inclusiveInheritedInterfaces() {
const interfaces = getParent
? [...getParent().inclusiveInheritedInterfaces, descriptor]
: [descriptor];
Object.defineProperties(descriptor, {
inclusiveInheritedInterfaces: { value: interfaces },
inheritanceDepth: { value: interfaces.length - 1 },
});
return interfaces;
},
get inheritanceDepth() {
return descriptor.inclusiveInheritedInterfaces.length - 1;
},
};
return descriptor;
}
class ReturnValue {
constructor(value) {
// Returning `value` lets derived classes stamp private fields onto that object.
// eslint-disable-next-line no-constructor-return
return value;
}
}
class WrapperData extends ReturnValue {
#impl;
#interfaceDescriptor;
constructor(wrapper, impl, interfaceDescriptor) {
super(wrapper);
this.#impl = impl;
this.#interfaceDescriptor = interfaceDescriptor;
}
static registerWrapper(wrapper, impl, interfaceDescriptor) {
// Private fields make registration one-time: an existing object's brand cannot be replaced.
return new WrapperData(wrapper, impl, interfaceDescriptor);
}
static implForWrapper(wrapper) {
if (!isObject(wrapper) || !(#impl in wrapper)) {
return null;
}
return wrapper.#impl;
}
static implForWrapperWithInterface(wrapper, interfaceDescriptor) {
if (!isObject(wrapper) || !(#impl in wrapper)) {
return null;
}
if (
wrapper.#interfaceDescriptor?.inclusiveInheritedInterfaces[
interfaceDescriptor.inheritanceDepth
] !== interfaceDescriptor
) {
return null;
}
return wrapper.#impl;
}
}
const { registerWrapper, implForWrapper, implForWrapperWithInterface } = WrapperData;
function requireImplForWrapper(wrapper, interfaceDescriptor, globalObject, interfaceName, context) {
const impl = implForWrapperWithInterface(wrapper, interfaceDescriptor);
if (impl === null) {
throw new globalObject.TypeError(
`'${context}' called on an object that is not a valid instance of ${interfaceName}.`,
);
}
return impl;
}
function wrapperForImpl(impl) {
return impl?.[wrapperSymbol] ?? null;
}
function tryWrapperForImpl(impl) {
const wrapper = wrapperForImpl(impl);
return wrapper !== null ? wrapper : impl;
}
function tryImplForWrapper(wrapper) {
const impl = implForWrapper(wrapper);
return impl !== null ? impl : wrapper;
}
const iterInternalSymbol = Symbol("internal");
function isArrayIndexPropName(P) {
if (typeof P !== "string") {
return false;
}
const i = P >>> 0;
if (i === 2 ** 32 - 1) {
return false;
}
const s = `${i}`;
if (P !== s) {
return false;
}
return true;
}
function createObservableArraySymbolFactory(algorithm) {
const symbols = new Map();
return (name) => {
if (!symbols.has(name)) {
symbols.set(name, Symbol(`${name} observable array ${algorithm}`));
}
return symbols.get(name);
};
}
const observableArraySet = createObservableArraySymbolFactory("set");
const observableArrayDelete = createObservableArraySymbolFactory("delete");
const observableArrays = new WeakMap();
// The target stays empty: indexed values and length are supplied by the implementation's backing list.
// https://webidl.spec.whatwg.org/#observable-array-exotic-object
class ObservableArrayHandler {
#backingList;
// https://webidl.spec.whatwg.org/#creating-an-observable-array-exotic-object
constructor(impl, globalObject, name, convert) {
this.impl = impl;
this.globalObject = globalObject;
this.name = name;
this.convert = convert;
this.setSymbol = observableArraySet(name);
this.deleteSymbol = observableArrayDelete(name);
const target = Object.setPrototypeOf([], initCtorRegistry(globalObject)["%Array.prototype%"]);
this.proxy = new Proxy(target, this);
}
get backingList() {
// `new()` skips the implementation constructor, so the list can be initialized after proxy creation.
this.#backingList ??= this.impl[this.name];
return this.#backingList;
}
// https://webidl.spec.whatwg.org/#es-observable-array-defineProperty
defineProperty(target, property, descriptor) {
if (property === "length") {
if (isAccessorDescriptor(descriptor)) {
return false;
}
if (descriptor.configurable === true) {
return false;
}
if (descriptor.enumerable === true) {
return false;
}
if (descriptor.writable === false) {
return false;
}
if (Object.hasOwn(descriptor, "value")) {
return this.setLength(descriptor.value);
}
return true;
}
if (isArrayIndexPropName(property)) {
if (isAccessorDescriptor(descriptor)) {
return false;
}
if (descriptor.configurable === false) {
return false;
}
if (descriptor.enumerable === false) {
return false;
}
if (descriptor.writable === false) {
return false;
}
if (Object.hasOwn(descriptor, "value")) {
return this.setIndexedValue(property, descriptor.value);
}
return true;
}
return Reflect.defineProperty(target, property, descriptor);
}
// https://webidl.spec.whatwg.org/#es-observable-array-deleteProperty
deleteProperty(target, property) {
if (property === "length") {
return false;
}
if (isArrayIndexPropName(property)) {
const list = this.backingList;
const index = Number(property);
if (index !== list.length - 1) {
return false;
}
this.impl[this.deleteSymbol]?.(list[index], index);
list.pop();
return true;
}
return Reflect.deleteProperty(target, property);
}
// https://webidl.spec.whatwg.org/#es-observable-array-get
get(target, property, receiver) {
if (property === "length") {
return this.backingList.length;
}
if (isArrayIndexPropName(property)) {
const list = this.backingList;
const index = Number(property);
return index < list.length ? tryWrapperForImpl(list[index]) : undefined;
}
return Reflect.get(target, property, receiver);
}
// https://webidl.spec.whatwg.org/#es-observable-array-getOwnPropertyDescriptor
getOwnPropertyDescriptor(target, property) {
if (property === "length") {
return {
value: this.backingList.length,
writable: true,
enumerable: false,
configurable: false,
};
}
if (isArrayIndexPropName(property)) {
const list = this.backingList;
const index = Number(property);
if (index >= list.length) {
return undefined;
}
return {
value: tryWrapperForImpl(list[index]),
writable: true,
enumerable: true,
configurable: true,
};
}
return Reflect.getOwnPropertyDescriptor(target, property);
}
// https://webidl.spec.whatwg.org/#es-observable-array-has
has(target, property) {
if (property === "length") {
return true;
}
if (isArrayIndexPropName(property)) {
return Number(property) < this.backingList.length;
}
return Reflect.has(target, property);
}
// https://webidl.spec.whatwg.org/#es-observable-array-ownKeys
ownKeys(target) {
const keys = [];
const { length } = this.backingList;
for (let index = 0; index < length; ++index) {
keys.push(String(index));
}
return keys.concat(Reflect.ownKeys(target));
}
// https://webidl.spec.whatwg.org/#es-observable-array-preventExtensions
preventExtensions() {
return false;
}
// https://webidl.spec.whatwg.org/#es-observable-array-set
set(target, property, value, receiver) {
if (property === "length") {
return this.setLength(value);
}
if (isArrayIndexPropName(property)) {
return this.setIndexedValue(property, value);
}
return Reflect.set(target, property, value, receiver);
}
// https://webidl.spec.whatwg.org/#observable-array-exotic-object-set-the-length
setLength(value) {
const options = { globals: this.globalObject };
const uint32Len = conversions["unsigned long"](value, options);
const numberLen = conversions["unrestricted double"](value, options);
if (uint32Len !== numberLen) {
throw new this.globalObject.RangeError("Invalid array length");
}
const list = this.backingList;
if (uint32Len > list.length) {
return false;
}
for (let index = list.length - 1; index >= uint32Len; --index) {
this.impl[this.deleteSymbol]?.(list[index], index);
list.pop();
}
return true;
}
// https://webidl.spec.whatwg.org/#observable-array-exotic-object-set-the-indexed-value
setIndexedValue(property, value) {
const list = this.backingList;
const oldLen = list.length;
const index = Number(property);
if (index > oldLen) {
return false;
}
const idlValue = this.convert(value);
if (index < oldLen) {
this.impl[this.deleteSymbol]?.(list[index], index);
}
this.impl[this.setSymbol]?.(idlValue, index);
if (index === oldLen) {
list.push(idlValue);
} else {
list[index] = idlValue;
}
return true;
}
// https://webidl.spec.whatwg.org/#dfn-attribute-setter
replace(values) {
this.setLength(0);
let index = 0;
for (const value of values) {
this.impl[this.setSymbol]?.(value, index);
this.backingList.push(value);
++index;
}
}
}
Object.setPrototypeOf(ObservableArrayHandler.prototype, null);
function initObservableArray(impl, globalObject, name, convert) {
if (!observableArrays.has(impl)) {
observableArrays.set(impl, new Map());
}
observableArrays
.get(impl)
.set(name, new ObservableArrayHandler(impl, globalObject, name, convert));
}
function getObservableArray(impl, name) {
return observableArrays.get(impl).get(name).proxy;
}
function setObservableArray(impl, name, values) {
observableArrays.get(impl).get(name).replace(values);
}
const arrayBufferByteLengthGetter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype,
"byteLength",
).get;
function isArrayBuffer(value) {
try {
arrayBufferByteLengthGetter.call(value);
return true;
} catch {
return false;
}
}
const sharedArrayBufferByteLengthGetter = Object.getOwnPropertyDescriptor(
SharedArrayBuffer.prototype,
"byteLength",
).get;
function isSharedArrayBuffer(value) {
try {
sharedArrayBufferByteLengthGetter.call(value);
return true;
} catch {
return false;
}
}
function iteratorResult([key, value], kind) {
let result;
switch (kind) {
case "key":
result = key;
break;
case "value":
result = value;
break;
case "key+value":
result = [key, value];
break;
}
return { value: result, done: false };
}
function ordinarySetWithOwnDescriptor(target, property, value, receiver, ownDesc) {
if (ownDesc === undefined) {
const parent = Reflect.getPrototypeOf(target);
if (parent !== null) {
return Reflect.set(parent, property, value, receiver);
}
ownDesc = { writable: true, enumerable: true, configurable: true, value: undefined };
}
if (isDataDescriptor(ownDesc)) {
if (!ownDesc.writable) {
return false;
}
if (!isObject(receiver)) {
return false;
}
const existingDesc = Reflect.getOwnPropertyDescriptor(receiver, property);
if (existingDesc !== undefined) {
if (isAccessorDescriptor(existingDesc)) {
return false;
}
if (existingDesc.writable === false) {
return false;
}
const valueDesc = { value };
return Reflect.defineProperty(receiver, property, valueDesc);
}
return Reflect.defineProperty(receiver, property, {
value,
writable: true,
enumerable: true,
configurable: true,
});
}
const setter = ownDesc.set;
if (setter === undefined) {
return false;
}
call(setter, receiver, value);
return true;
}
function isDataDescriptor(desc) {
return Object.hasOwn(desc, "value") || Object.hasOwn(desc, "writable");
}
function isAccessorDescriptor(desc) {
return Object.hasOwn(desc, "get") || Object.hasOwn(desc, "set");
}
function getMethod(value, property, errPrefix = "The provided value") {
const func = value[property];
if (func === undefined || func === null) {
return undefined;
}
if (typeof func !== "function") {
throw new TypeError(`${errPrefix}'s ${property} property is not a function.`);
}
return func;
}
function createAsyncFromSyncIterator(syncIterator) {
// Instead of re-implementing CreateAsyncFromSyncIterator and %AsyncFromSyncIteratorPrototype%,
// we use yield* inside an async generator function to achieve the same result.
// Wrap the sync iterator inside a sync iterable, so we can use it with yield*.
const syncIterable = {
[Symbol.iterator]: () => syncIterator,
};
// Create an async generator function and immediately invoke it.
const asyncIterator = (async function* () {
return yield* syncIterable;
})();
// Return as an async iterator record.
return asyncIterator;
}
function convertAsyncSequence(object, itemConverter, errPrefix = "The provided value") {
if (!isObject(object)) {
throw new TypeError(`${errPrefix} is not an object.`);
}
let method = getMethod(object, Symbol.asyncIterator, errPrefix);
let type = "async";
if (method === undefined) {
method = getMethod(object, Symbol.iterator, errPrefix);
if (method === undefined) {
throw new TypeError(`${errPrefix} is not an async iterable object.`);
}
type = "sync";
}
return {
object,
method,
type,
// The wrapperSymbol ensures that if the async sequence is used as a return value,
// that it exposes the original JavaScript value.
// https://webidl.spec.whatwg.org/#js-async-iterable
[wrapperSymbol]: object,
// Implement the async iterator protocol, so users can iterate
// the async sequence directly (e.g. with for await...of)
// instead of needing to call a separate helper function to open the async sequence.
// https://webidl.spec.whatwg.org/#async-sequence-open
[Symbol.asyncIterator]() {
return openAsyncSequence(object, method, type, itemConverter, `${errPrefix}'s iterator`);
},
};
}
function openAsyncSequence(object, method, type, itemConverter, errPrefix = "The provided value") {
let iterator = call(method, object);
if (!isObject(iterator)) {
throw new TypeError(`${errPrefix}'s method must return an object`);
}
if (type === "sync") {
iterator = createAsyncFromSyncIterator(iterator);
}
const nextMethod = iterator.next;
return {
async next() {
const nextResult = await call(nextMethod, iterator);
if (!isObject(nextResult)) {
throw new TypeError(`${errPrefix}'s next method must return an object`);
}
const { done, value } = nextResult;
if (done) {
return { done: true, value: undefined };
}
return { done: false, value: itemConverter(value) };
},
async return(reason) {
const returnMethod = getMethod(iterator, "return", errPrefix);
if (returnMethod === undefined) {
return { done: true, value: undefined };
}
const returnResult = await call(returnMethod, iterator, reason);
if (!isObject(returnResult)) {
throw new TypeError(`${errPrefix}'s return method must return an object`);
}
return { done: true, value: undefined };
},
[Symbol.asyncIterator]() {
return this;
},
};
}
const supportsPropertyIndex = Symbol("supports property index");
const supportedPropertyIndices = Symbol("supported property indices");
const supportsPropertyName = Symbol("supports property name");
const supportedPropertyNames = Symbol("supported property names");
const indexedGet = Symbol("indexed property get");
const indexedSetNew = Symbol("indexed property set new");
const indexedSetExisting = Symbol("indexed property set existing");
const namedGet = Symbol("named property get");
const namedSetNew = Symbol("named property set new");
const namedSetExisting = Symbol("named property set existing");
const namedDelete = Symbol("named property delete");
const asyncIteratorNext = Symbol("async iterator get the next iteration result");
const asyncIteratorReturn = Symbol("async iterator return steps");
const asyncIteratorInit = Symbol("async iterator initialization steps");
const asyncIteratorEOI = Symbol("async iterator end of iteration");
module.exports = exports = {
isObject,
define,
newObjectInRealm,
wrapperSymbol,
createInterfaceDescriptor,
registerWrapper,
getSameObject,
ctorRegistrySymbol,
initCtorRegistry,
wrapperForImpl,
implForWrapper,
implForWrapperWithInterface,
requireImplForWrapper,
tryWrapperForImpl,
tryImplForWrapper,
iterInternalSymbol,
isArrayBuffer,
isSharedArrayBuffer,
isArrayIndexPropName,
observableArraySet,
observableArrayDelete,
initObservableArray,
getObservableArray,
setObservableArray,
getMethod,
convertAsyncSequence,
supportsPropertyIndex,
supportedPropertyIndices,
supportsPropertyName,
supportedPropertyNames,
indexedGet,
indexedSetNew,
indexedSetExisting,
namedGet,
namedSetNew,
namedSetExisting,
namedDelete,
asyncIteratorNext,
asyncIteratorReturn,
asyncIteratorInit,
asyncIteratorEOI,
iteratorResult,
ordinarySetWithOwnDescriptor,
};