2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
// Adapted from https://github.com/jsdom/webidl-conversions.
|
|
|
|
// Copyright Domenic Denicola. Licensed under BSD-2-Clause License.
|
|
|
|
// Original license at https://github.com/jsdom/webidl-conversions/blob/master/LICENSE.md.
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2021-02-13 09:58:12 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
2021-10-04 09:39:32 -04:00
|
|
|
const core = window.Deno.core;
|
2021-07-02 10:37:41 -04:00
|
|
|
const {
|
2022-02-01 12:06:11 -05:00
|
|
|
ArrayBufferPrototype,
|
2021-07-02 10:37:41 -04:00
|
|
|
ArrayBufferIsView,
|
2021-07-26 07:52:59 -04:00
|
|
|
ArrayPrototypeForEach,
|
2021-07-02 10:37:41 -04:00
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSort,
|
2021-07-26 07:52:59 -04:00
|
|
|
ArrayIteratorPrototype,
|
2021-07-02 10:37:41 -04:00
|
|
|
BigInt,
|
|
|
|
BigIntAsIntN,
|
|
|
|
BigIntAsUintN,
|
|
|
|
Float32Array,
|
|
|
|
Float64Array,
|
|
|
|
FunctionPrototypeBind,
|
|
|
|
Int16Array,
|
|
|
|
Int32Array,
|
|
|
|
Int8Array,
|
|
|
|
isNaN,
|
|
|
|
MathFloor,
|
|
|
|
MathFround,
|
|
|
|
MathMax,
|
|
|
|
MathMin,
|
|
|
|
MathPow,
|
|
|
|
MathRound,
|
|
|
|
MathTrunc,
|
|
|
|
Number,
|
|
|
|
NumberIsFinite,
|
|
|
|
NumberIsNaN,
|
|
|
|
// deno-lint-ignore camelcase
|
|
|
|
NumberMAX_SAFE_INTEGER,
|
|
|
|
// deno-lint-ignore camelcase
|
|
|
|
NumberMIN_SAFE_INTEGER,
|
2021-09-30 15:33:12 -04:00
|
|
|
ObjectAssign,
|
2021-07-02 10:37:41 -04:00
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetOwnPropertyDescriptors,
|
|
|
|
ObjectGetPrototypeOf,
|
2021-10-04 09:39:32 -04:00
|
|
|
ObjectPrototypeHasOwnProperty,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-02 10:37:41 -04:00
|
|
|
ObjectIs,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
PromiseReject,
|
|
|
|
PromiseResolve,
|
|
|
|
ReflectApply,
|
|
|
|
ReflectDefineProperty,
|
|
|
|
ReflectGetOwnPropertyDescriptor,
|
2022-04-16 08:09:07 -04:00
|
|
|
ReflectHas,
|
2021-07-02 10:37:41 -04:00
|
|
|
ReflectOwnKeys,
|
|
|
|
RegExpPrototypeTest,
|
2022-12-19 21:37:50 -05:00
|
|
|
SafeArrayIterator,
|
2021-07-02 10:37:41 -04:00
|
|
|
Set,
|
|
|
|
// TODO(lucacasonato): add SharedArrayBuffer to primordials
|
|
|
|
// SharedArrayBuffer,
|
|
|
|
String,
|
|
|
|
StringFromCodePoint,
|
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
Symbol,
|
|
|
|
SymbolIterator,
|
|
|
|
SymbolToStringTag,
|
|
|
|
TypeError,
|
|
|
|
Uint16Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint8Array,
|
|
|
|
Uint8ClampedArray,
|
|
|
|
} = window.__bootstrap.primordials;
|
|
|
|
|
2021-02-13 09:58:12 -05:00
|
|
|
function makeException(ErrorType, message, opts = {}) {
|
|
|
|
return new ErrorType(
|
|
|
|
`${opts.prefix ? opts.prefix + ": " : ""}${
|
|
|
|
opts.context ? opts.context : "Value"
|
2021-02-28 19:25:31 -05:00
|
|
|
} ${message}`,
|
2021-02-13 09:58:12 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
function toNumber(value) {
|
2021-02-13 09:58:12 -05:00
|
|
|
if (typeof value === "bigint") {
|
2021-07-02 10:37:41 -04:00
|
|
|
throw TypeError("Cannot convert a BigInt value to a number");
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
2021-07-02 10:37:41 -04:00
|
|
|
return Number(value);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function type(V) {
|
|
|
|
if (V === null) {
|
|
|
|
return "Null";
|
|
|
|
}
|
|
|
|
switch (typeof V) {
|
|
|
|
case "undefined":
|
|
|
|
return "Undefined";
|
|
|
|
case "boolean":
|
|
|
|
return "Boolean";
|
|
|
|
case "number":
|
|
|
|
return "Number";
|
|
|
|
case "string":
|
|
|
|
return "String";
|
|
|
|
case "symbol":
|
|
|
|
return "Symbol";
|
|
|
|
case "bigint":
|
|
|
|
return "BigInt";
|
|
|
|
case "object":
|
|
|
|
// Falls through
|
|
|
|
case "function":
|
|
|
|
// Falls through
|
|
|
|
default:
|
|
|
|
// Per ES spec, typeof returns an implemention-defined value that is not any of the existing ones for
|
|
|
|
// uncallable non-standard exotic objects. Yet Type() which the Web IDL spec depends on returns Object for
|
|
|
|
// such cases. So treat the default case as an object.
|
|
|
|
return "Object";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round x to the nearest integer, choosing the even integer if it lies halfway between two.
|
|
|
|
function evenRound(x) {
|
|
|
|
// There are four cases for numbers with fractional part being .5:
|
|
|
|
//
|
|
|
|
// case | x | floor(x) | round(x) | expected | x <> 0 | x % 1 | x & 1 | example
|
|
|
|
// 1 | 2n + 0.5 | 2n | 2n + 1 | 2n | > | 0.5 | 0 | 0.5 -> 0
|
|
|
|
// 2 | 2n + 1.5 | 2n + 1 | 2n + 2 | 2n + 2 | > | 0.5 | 1 | 1.5 -> 2
|
|
|
|
// 3 | -2n - 0.5 | -2n - 1 | -2n | -2n | < | -0.5 | 0 | -0.5 -> 0
|
|
|
|
// 4 | -2n - 1.5 | -2n - 2 | -2n - 1 | -2n - 2 | < | -0.5 | 1 | -1.5 -> -2
|
|
|
|
// (where n is a non-negative integer)
|
|
|
|
//
|
|
|
|
// Branch here for cases 1 and 4
|
|
|
|
if (
|
|
|
|
(x > 0 && x % 1 === +0.5 && (x & 1) === 0) ||
|
|
|
|
(x < 0 && x % 1 === -0.5 && (x & 1) === 1)
|
|
|
|
) {
|
2021-07-02 10:37:41 -04:00
|
|
|
return censorNegativeZero(MathFloor(x));
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
return censorNegativeZero(MathRound(x));
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function integerPart(n) {
|
2021-07-02 10:37:41 -04:00
|
|
|
return censorNegativeZero(MathTrunc(n));
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function sign(x) {
|
|
|
|
return x < 0 ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function modulo(x, y) {
|
|
|
|
// https://tc39.github.io/ecma262/#eqn-modulo
|
|
|
|
// Note that http://stackoverflow.com/a/4467559/3191 does NOT work for large modulos
|
|
|
|
const signMightNotMatch = x % y;
|
|
|
|
if (sign(y) !== sign(signMightNotMatch)) {
|
|
|
|
return signMightNotMatch + y;
|
|
|
|
}
|
|
|
|
return signMightNotMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
function censorNegativeZero(x) {
|
|
|
|
return x === 0 ? 0 : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createIntegerConversion(bitLength, typeOpts) {
|
|
|
|
const isSigned = !typeOpts.unsigned;
|
|
|
|
|
|
|
|
let lowerBound;
|
|
|
|
let upperBound;
|
|
|
|
if (bitLength === 64) {
|
2021-07-02 10:37:41 -04:00
|
|
|
upperBound = NumberMAX_SAFE_INTEGER;
|
|
|
|
lowerBound = !isSigned ? 0 : NumberMIN_SAFE_INTEGER;
|
2021-02-13 09:58:12 -05:00
|
|
|
} else if (!isSigned) {
|
|
|
|
lowerBound = 0;
|
2021-07-02 10:37:41 -04:00
|
|
|
upperBound = MathPow(2, bitLength) - 1;
|
2021-02-13 09:58:12 -05:00
|
|
|
} else {
|
2021-07-02 10:37:41 -04:00
|
|
|
lowerBound = -MathPow(2, bitLength - 1);
|
|
|
|
upperBound = MathPow(2, bitLength - 1) - 1;
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
const twoToTheBitLength = MathPow(2, bitLength);
|
|
|
|
const twoToOneLessThanTheBitLength = MathPow(2, bitLength - 1);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
return (V, opts = {}) => {
|
2022-03-19 05:26:54 -04:00
|
|
|
let x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
x = censorNegativeZero(x);
|
|
|
|
|
|
|
|
if (opts.enforceRange) {
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(TypeError, "is not a finite number", opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
x = integerPart(x);
|
|
|
|
|
|
|
|
if (x < lowerBound || x > upperBound) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
`is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsNaN(x) && opts.clamp) {
|
|
|
|
x = MathMin(MathMax(x, lowerBound), upperBound);
|
2021-02-13 09:58:12 -05:00
|
|
|
x = evenRound(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x) || x === 0) {
|
2021-02-13 09:58:12 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x = integerPart(x);
|
|
|
|
|
|
|
|
// Math.pow(2, 64) is not accurately representable in JavaScript, so try to avoid these per-spec operations if
|
|
|
|
// possible. Hopefully it's an optimization for the non-64-bitLength cases too.
|
|
|
|
if (x >= lowerBound && x <= upperBound) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These will not work great for bitLength of 64, but oh well. See the README for more details.
|
|
|
|
x = modulo(x, twoToTheBitLength);
|
|
|
|
if (isSigned && x >= twoToOneLessThanTheBitLength) {
|
|
|
|
return x - twoToTheBitLength;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createLongLongConversion(bitLength, { unsigned }) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const upperBound = NumberMAX_SAFE_INTEGER;
|
|
|
|
const lowerBound = unsigned ? 0 : NumberMIN_SAFE_INTEGER;
|
|
|
|
const asBigIntN = unsigned ? BigIntAsUintN : BigIntAsIntN;
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
return (V, opts = {}) => {
|
2022-03-19 05:26:54 -04:00
|
|
|
let x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
x = censorNegativeZero(x);
|
|
|
|
|
|
|
|
if (opts.enforceRange) {
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(TypeError, "is not a finite number", opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
x = integerPart(x);
|
|
|
|
|
|
|
|
if (x < lowerBound || x > upperBound) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
`is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsNaN(x) && opts.clamp) {
|
|
|
|
x = MathMin(MathMax(x, lowerBound), upperBound);
|
2021-02-13 09:58:12 -05:00
|
|
|
x = evenRound(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x) || x === 0) {
|
2021-02-13 09:58:12 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let xBigInt = BigInt(integerPart(x));
|
|
|
|
xBigInt = asBigIntN(bitLength, xBigInt);
|
|
|
|
return Number(xBigInt);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const converters = [];
|
|
|
|
|
|
|
|
converters.any = (V) => {
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.boolean = function (val) {
|
|
|
|
return !!val;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.byte = createIntegerConversion(8, { unsigned: false });
|
|
|
|
converters.octet = createIntegerConversion(8, { unsigned: true });
|
|
|
|
|
|
|
|
converters.short = createIntegerConversion(16, { unsigned: false });
|
|
|
|
converters["unsigned short"] = createIntegerConversion(16, {
|
|
|
|
unsigned: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
converters.long = createIntegerConversion(32, { unsigned: false });
|
|
|
|
converters["unsigned long"] = createIntegerConversion(32, { unsigned: true });
|
|
|
|
|
|
|
|
converters["long long"] = createLongLongConversion(64, { unsigned: false });
|
|
|
|
converters["unsigned long long"] = createLongLongConversion(64, {
|
|
|
|
unsigned: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
converters.float = (V, opts) => {
|
2022-03-19 05:26:54 -04:00
|
|
|
const x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not a finite floating-point value",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (ObjectIs(x, -0)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
const y = MathFround(x);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(y)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is outside the range of a single-precision floating-point value",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return y;
|
|
|
|
};
|
|
|
|
|
2022-03-19 05:26:54 -04:00
|
|
|
converters["unrestricted float"] = (V, _opts) => {
|
|
|
|
const x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
if (isNaN(x)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (ObjectIs(x, -0)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
return MathFround(x);
|
2021-02-13 09:58:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
converters.double = (V, opts) => {
|
2022-03-19 05:26:54 -04:00
|
|
|
const x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!NumberIsFinite(x)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not a finite floating-point value",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
|
2022-03-19 05:26:54 -04:00
|
|
|
converters["unrestricted double"] = (V, _opts) => {
|
|
|
|
const x = toNumber(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.DOMString = function (V, opts = {}) {
|
2021-09-29 12:06:29 -04:00
|
|
|
if (typeof V === "string") {
|
|
|
|
return V;
|
|
|
|
} else if (V === null && opts.treatNullAsEmptyString) {
|
2021-02-13 09:58:12 -05:00
|
|
|
return "";
|
2021-09-29 12:06:29 -04:00
|
|
|
} else if (typeof V === "symbol") {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is a symbol, which cannot be converted to a string",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
return String(V);
|
2021-02-13 09:58:12 -05:00
|
|
|
};
|
|
|
|
|
2021-09-26 14:40:37 -04:00
|
|
|
// deno-lint-ignore no-control-regex
|
|
|
|
const IS_BYTE_STRING = /^[\x00-\xFF]*$/;
|
2021-02-13 09:58:12 -05:00
|
|
|
converters.ByteString = (V, opts) => {
|
|
|
|
const x = converters.DOMString(V, opts);
|
2021-09-26 14:40:37 -04:00
|
|
|
if (!RegExpPrototypeTest(IS_BYTE_STRING, x)) {
|
|
|
|
throw makeException(TypeError, "is not a valid ByteString", opts);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.USVString = (V, opts) => {
|
|
|
|
const S = converters.DOMString(V, opts);
|
|
|
|
const n = S.length;
|
2021-06-06 10:56:44 -04:00
|
|
|
let U = "";
|
2021-02-13 09:58:12 -05:00
|
|
|
for (let i = 0; i < n; ++i) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const c = StringPrototypeCharCodeAt(S, i);
|
2021-02-13 09:58:12 -05:00
|
|
|
if (c < 0xd800 || c > 0xdfff) {
|
2021-07-02 10:37:41 -04:00
|
|
|
U += StringFromCodePoint(c);
|
2021-02-13 09:58:12 -05:00
|
|
|
} else if (0xdc00 <= c && c <= 0xdfff) {
|
2021-07-02 10:37:41 -04:00
|
|
|
U += StringFromCodePoint(0xfffd);
|
2021-02-13 09:58:12 -05:00
|
|
|
} else if (i === n - 1) {
|
2021-07-02 10:37:41 -04:00
|
|
|
U += StringFromCodePoint(0xfffd);
|
2021-02-13 09:58:12 -05:00
|
|
|
} else {
|
2021-07-02 10:37:41 -04:00
|
|
|
const d = StringPrototypeCharCodeAt(S, i + 1);
|
2021-02-13 09:58:12 -05:00
|
|
|
if (0xdc00 <= d && d <= 0xdfff) {
|
|
|
|
const a = c & 0x3ff;
|
|
|
|
const b = d & 0x3ff;
|
2021-07-02 10:37:41 -04:00
|
|
|
U += StringFromCodePoint((2 << 15) + (2 << 9) * a + b);
|
2021-02-13 09:58:12 -05:00
|
|
|
++i;
|
|
|
|
} else {
|
2021-07-02 10:37:41 -04:00
|
|
|
U += StringFromCodePoint(0xfffd);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 10:56:44 -04:00
|
|
|
return U;
|
2021-02-13 09:58:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
converters.object = (V, opts) => {
|
|
|
|
if (type(V) !== "Object") {
|
|
|
|
throw makeException(TypeError, "is not an object", opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Not exported, but used in Function and VoidFunction.
|
|
|
|
|
|
|
|
// Neither Function nor VoidFunction is defined with [TreatNonObjectAsNull], so
|
|
|
|
// handling for that is omitted.
|
|
|
|
function convertCallbackFunction(V, opts) {
|
|
|
|
if (typeof V !== "function") {
|
|
|
|
throw makeException(TypeError, "is not a function", opts);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNonSharedArrayBuffer(V) {
|
2022-02-01 12:06:11 -05:00
|
|
|
return ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSharedArrayBuffer(V) {
|
2022-02-01 12:06:11 -05:00
|
|
|
return ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
converters.ArrayBuffer = (V, opts = {}) => {
|
|
|
|
if (!isNonSharedArrayBuffer(V)) {
|
|
|
|
if (opts.allowShared && !isSharedArrayBuffer(V)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not an ArrayBuffer or SharedArrayBuffer",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw makeException(TypeError, "is not an ArrayBuffer", opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.DataView = (V, opts = {}) => {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (!(ObjectPrototypeIsPrototypeOf(DataViewPrototype, V))) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(TypeError, "is not a DataView", opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.allowShared && isSharedArrayBuffer(V.buffer)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is backed by a SharedArrayBuffer, which is not allowed",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the unforgeable `TypedArray` constructor name or `undefined`,
|
|
|
|
// if the `this` value isn't a valid `TypedArray` object.
|
|
|
|
//
|
|
|
|
// https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag
|
2021-07-02 10:37:41 -04:00
|
|
|
const typedArrayNameGetter = ObjectGetOwnPropertyDescriptor(
|
|
|
|
ObjectGetPrototypeOf(Uint8Array).prototype,
|
|
|
|
SymbolToStringTag,
|
2021-02-13 09:58:12 -05:00
|
|
|
).get;
|
2021-07-26 07:52:59 -04:00
|
|
|
ArrayPrototypeForEach(
|
|
|
|
[
|
|
|
|
Int8Array,
|
|
|
|
Int16Array,
|
|
|
|
Int32Array,
|
|
|
|
Uint8Array,
|
|
|
|
Uint16Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint8ClampedArray,
|
|
|
|
Float32Array,
|
|
|
|
Float64Array,
|
|
|
|
],
|
|
|
|
(func) => {
|
|
|
|
const name = func.name;
|
|
|
|
const article = RegExpPrototypeTest(/^[AEIOU]/, name) ? "an" : "a";
|
|
|
|
converters[name] = (V, opts = {}) => {
|
|
|
|
if (!ArrayBufferIsView(V) || typedArrayNameGetter.call(V) !== name) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
`is not ${article} ${name} object`,
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!opts.allowShared && isSharedArrayBuffer(V.buffer)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is a view on a SharedArrayBuffer, which is not allowed",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2021-07-26 07:52:59 -04:00
|
|
|
return V;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
// Common definitions
|
|
|
|
|
|
|
|
converters.ArrayBufferView = (V, opts = {}) => {
|
2021-07-02 10:37:41 -04:00
|
|
|
if (!ArrayBufferIsView(V)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not a view on an ArrayBuffer or SharedArrayBuffer",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.allowShared && isSharedArrayBuffer(V.buffer)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is a view on a SharedArrayBuffer, which is not allowed",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.BufferSource = (V, opts = {}) => {
|
2021-07-02 10:37:41 -04:00
|
|
|
if (ArrayBufferIsView(V)) {
|
2021-02-13 09:58:12 -05:00
|
|
|
if (!opts.allowShared && isSharedArrayBuffer(V.buffer)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is a view on a SharedArrayBuffer, which is not allowed",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.allowShared && !isNonSharedArrayBuffer(V)) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not an ArrayBuffer or a view on one",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
opts.allowShared &&
|
|
|
|
!isSharedArrayBuffer(V) &&
|
|
|
|
!isNonSharedArrayBuffer(V)
|
|
|
|
) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"is not an ArrayBuffer, SharedArrayBuffer, or a view on one",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
|
|
|
|
converters.DOMTimeStamp = converters["unsigned long long"];
|
2021-07-05 07:17:11 -04:00
|
|
|
converters.DOMHighResTimeStamp = converters["double"];
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
converters.Function = convertCallbackFunction;
|
|
|
|
|
|
|
|
converters.VoidFunction = convertCallbackFunction;
|
|
|
|
|
2021-03-08 08:22:05 -05:00
|
|
|
converters["UVString?"] = createNullableConverter(
|
|
|
|
converters.USVString,
|
|
|
|
);
|
|
|
|
converters["sequence<double>"] = createSequenceConverter(
|
2021-06-21 13:53:52 -04:00
|
|
|
converters.double,
|
|
|
|
);
|
|
|
|
converters["sequence<object>"] = createSequenceConverter(
|
|
|
|
converters.object,
|
2021-03-08 08:22:05 -05:00
|
|
|
);
|
2021-06-15 07:46:02 -04:00
|
|
|
converters["Promise<undefined>"] = createPromiseConverter(() => undefined);
|
2021-03-08 08:22:05 -05:00
|
|
|
|
2021-06-16 12:40:35 -04:00
|
|
|
converters["sequence<ByteString>"] = createSequenceConverter(
|
|
|
|
converters.ByteString,
|
|
|
|
);
|
|
|
|
converters["sequence<sequence<ByteString>>"] = createSequenceConverter(
|
|
|
|
converters["sequence<ByteString>"],
|
|
|
|
);
|
|
|
|
converters["record<ByteString, ByteString>"] = createRecordConverter(
|
|
|
|
converters.ByteString,
|
|
|
|
converters.ByteString,
|
|
|
|
);
|
|
|
|
|
2021-06-24 05:38:37 -04:00
|
|
|
converters["sequence<USVString>"] = createSequenceConverter(
|
|
|
|
converters.USVString,
|
|
|
|
);
|
|
|
|
converters["sequence<sequence<USVString>>"] = createSequenceConverter(
|
|
|
|
converters["sequence<USVString>"],
|
|
|
|
);
|
|
|
|
converters["record<USVString, USVString>"] = createRecordConverter(
|
|
|
|
converters.USVString,
|
|
|
|
converters.USVString,
|
|
|
|
);
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
converters["sequence<DOMString>"] = createSequenceConverter(
|
|
|
|
converters.DOMString,
|
|
|
|
);
|
|
|
|
|
2021-02-13 09:58:12 -05:00
|
|
|
function requiredArguments(length, required, opts = {}) {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${
|
|
|
|
opts.prefix ? opts.prefix + ": " : ""
|
|
|
|
}${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
2021-02-28 19:25:31 -05:00
|
|
|
} required, but only ${length} present.`;
|
2021-02-13 09:58:12 -05:00
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDictionaryConverter(name, ...dictionaries) {
|
2021-04-20 08:47:22 -04:00
|
|
|
let hasRequiredKey = false;
|
2021-04-07 19:23:26 -04:00
|
|
|
const allMembers = [];
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const members of new SafeArrayIterator(dictionaries)) {
|
|
|
|
for (const member of new SafeArrayIterator(members)) {
|
2021-04-20 08:47:22 -04:00
|
|
|
if (member.required) {
|
|
|
|
hasRequiredKey = true;
|
|
|
|
}
|
2021-07-02 10:37:41 -04:00
|
|
|
ArrayPrototypePush(allMembers, member);
|
2021-04-07 19:23:26 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 10:37:41 -04:00
|
|
|
ArrayPrototypeSort(allMembers, (a, b) => {
|
2021-04-07 19:23:26 -04:00
|
|
|
if (a.key == b.key) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return a.key < b.key ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
const defaultValues = {};
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const member of new SafeArrayIterator(allMembers)) {
|
2022-04-16 08:09:07 -04:00
|
|
|
if (ReflectHas(member, "defaultValue")) {
|
2021-04-20 08:47:22 -04:00
|
|
|
const idlMemberValue = member.defaultValue;
|
|
|
|
const imvType = typeof idlMemberValue;
|
|
|
|
// Copy by value types can be directly assigned, copy by reference types
|
|
|
|
// need to be re-created for each allocation.
|
|
|
|
if (
|
|
|
|
imvType === "number" || imvType === "boolean" ||
|
|
|
|
imvType === "string" || imvType === "bigint" ||
|
|
|
|
imvType === "undefined"
|
|
|
|
) {
|
2021-08-24 14:32:25 -04:00
|
|
|
defaultValues[member.key] = member.converter(idlMemberValue, {});
|
2021-04-20 08:47:22 -04:00
|
|
|
} else {
|
2021-07-02 10:37:41 -04:00
|
|
|
ObjectDefineProperty(defaultValues, member.key, {
|
2021-04-20 08:47:22 -04:00
|
|
|
get() {
|
2021-08-24 14:32:25 -04:00
|
|
|
return member.converter(idlMemberValue, member.defaultValue);
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2021-06-21 13:53:52 -04:00
|
|
|
enumerable: true,
|
2021-04-20 08:47:22 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 09:58:12 -05:00
|
|
|
return function (V, opts = {}) {
|
|
|
|
const typeV = type(V);
|
|
|
|
switch (typeV) {
|
|
|
|
case "Undefined":
|
|
|
|
case "Null":
|
|
|
|
case "Object":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"can not be converted to a dictionary",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const esDict = V;
|
|
|
|
|
2021-09-30 15:33:12 -04:00
|
|
|
const idlDict = ObjectAssign({}, defaultValues);
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2021-06-15 07:46:02 -04:00
|
|
|
// NOTE: fast path Null and Undefined.
|
|
|
|
if ((V === undefined || V === null) && !hasRequiredKey) {
|
2021-04-20 08:47:22 -04:00
|
|
|
return idlDict;
|
|
|
|
}
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const member of new SafeArrayIterator(allMembers)) {
|
2021-04-07 19:23:26 -04:00
|
|
|
const key = member.key;
|
|
|
|
|
|
|
|
let esMemberValue;
|
|
|
|
if (typeV === "Undefined" || typeV === "Null") {
|
|
|
|
esMemberValue = undefined;
|
|
|
|
} else {
|
|
|
|
esMemberValue = esDict[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esMemberValue !== undefined) {
|
2021-04-20 08:47:22 -04:00
|
|
|
const context = `'${key}' of '${name}'${
|
|
|
|
opts.context ? ` (${opts.context})` : ""
|
|
|
|
}`;
|
2021-04-07 19:23:26 -04:00
|
|
|
const converter = member.converter;
|
2021-04-20 08:47:22 -04:00
|
|
|
const idlMemberValue = converter(esMemberValue, { ...opts, context });
|
2021-04-07 19:23:26 -04:00
|
|
|
idlDict[key] = idlMemberValue;
|
|
|
|
} else if (member.required) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
`can not be converted to '${name}' because '${key}' is required in '${name}'.`,
|
2022-03-19 05:26:54 -04:00
|
|
|
opts,
|
2021-04-07 19:23:26 -04:00
|
|
|
);
|
2021-02-13 09:58:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idlDict;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:13:46 -05:00
|
|
|
// https://heycam.github.io/webidl/#es-enumeration
|
|
|
|
function createEnumConverter(name, values) {
|
|
|
|
const E = new Set(values);
|
|
|
|
|
|
|
|
return function (V, opts = {}) {
|
|
|
|
const S = String(V);
|
|
|
|
|
|
|
|
if (!E.has(S)) {
|
2021-02-28 19:25:31 -05:00
|
|
|
throw new TypeError(
|
|
|
|
`${
|
|
|
|
opts.prefix ? opts.prefix + ": " : ""
|
|
|
|
}The provided value '${S}' is not a valid enum value of type ${name}.`,
|
2021-02-15 18:13:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNullableConverter(converter) {
|
|
|
|
return (V, opts = {}) => {
|
|
|
|
// FIXME: If Type(V) is not Object, and the conversion to an IDL value is
|
|
|
|
// being performed due to V being assigned to an attribute whose type is a
|
|
|
|
// nullable callback function that is annotated with
|
|
|
|
// [LegacyTreatNonObjectAsNull], then return the IDL nullable type T?
|
|
|
|
// value null.
|
|
|
|
|
|
|
|
if (V === null || V === undefined) return null;
|
|
|
|
return converter(V, opts);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-28 19:25:31 -05:00
|
|
|
// https://heycam.github.io/webidl/#es-sequence
|
|
|
|
function createSequenceConverter(converter) {
|
|
|
|
return function (V, opts = {}) {
|
2021-06-16 12:40:35 -04:00
|
|
|
if (type(V) !== "Object") {
|
2021-02-28 19:25:31 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"can not be converted to sequence.",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
2021-07-02 10:37:41 -04:00
|
|
|
const iter = V?.[SymbolIterator]?.();
|
2021-02-28 19:25:31 -05:00
|
|
|
if (iter === undefined) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"can not be converted to sequence.",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const array = [];
|
|
|
|
while (true) {
|
|
|
|
const res = iter?.next?.();
|
|
|
|
if (res === undefined) {
|
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"can not be converted to sequence.",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (res.done === true) break;
|
|
|
|
const val = converter(res.value, {
|
|
|
|
...opts,
|
|
|
|
context: `${opts.context}, index ${array.length}`,
|
|
|
|
});
|
2021-07-02 10:37:41 -04:00
|
|
|
ArrayPrototypePush(array, val);
|
2021-02-28 19:25:31 -05:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-08 08:22:05 -05:00
|
|
|
function createRecordConverter(keyConverter, valueConverter) {
|
|
|
|
return (V, opts) => {
|
2021-06-16 12:40:35 -04:00
|
|
|
if (type(V) !== "Object") {
|
2021-03-08 08:22:05 -05:00
|
|
|
throw makeException(
|
|
|
|
TypeError,
|
|
|
|
"can not be converted to dictionary.",
|
|
|
|
opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const result = {};
|
2021-10-04 09:39:32 -04:00
|
|
|
// Fast path for common case (not a Proxy)
|
|
|
|
if (!core.isProxy(V)) {
|
|
|
|
for (const key in V) {
|
|
|
|
if (!ObjectPrototypeHasOwnProperty(V, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const typedKey = keyConverter(key, opts);
|
|
|
|
const value = V[key];
|
|
|
|
const typedValue = valueConverter(value, opts);
|
|
|
|
result[typedKey] = typedValue;
|
|
|
|
}
|
2021-10-05 17:37:50 -04:00
|
|
|
return result;
|
2021-10-04 09:39:32 -04:00
|
|
|
}
|
|
|
|
// Slow path if Proxy (e.g: in WPT tests)
|
|
|
|
const keys = ReflectOwnKeys(V);
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const key of new SafeArrayIterator(keys)) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const desc = ObjectGetOwnPropertyDescriptor(V, key);
|
2021-04-18 19:00:13 -04:00
|
|
|
if (desc !== undefined && desc.enumerable === true) {
|
|
|
|
const typedKey = keyConverter(key, opts);
|
|
|
|
const value = V[key];
|
|
|
|
const typedValue = valueConverter(value, opts);
|
|
|
|
result[typedKey] = typedValue;
|
|
|
|
}
|
2021-03-08 08:22:05 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-15 07:46:02 -04:00
|
|
|
function createPromiseConverter(converter) {
|
2021-07-02 10:37:41 -04:00
|
|
|
return (V, opts) =>
|
|
|
|
PromisePrototypeThen(PromiseResolve(V), (V) => converter(V, opts));
|
2021-06-15 07:46:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function invokeCallbackFunction(
|
|
|
|
callable,
|
|
|
|
args,
|
|
|
|
thisArg,
|
|
|
|
returnValueConverter,
|
|
|
|
opts,
|
|
|
|
) {
|
|
|
|
try {
|
2021-07-02 10:37:41 -04:00
|
|
|
const rv = ReflectApply(callable, thisArg, args);
|
2021-06-15 07:46:02 -04:00
|
|
|
return returnValueConverter(rv, {
|
|
|
|
prefix: opts.prefix,
|
|
|
|
context: "return value",
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (opts.returnsPromise === true) {
|
2021-07-02 10:37:41 -04:00
|
|
|
return PromiseReject(err);
|
2021-06-15 07:46:02 -04:00
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 19:25:31 -05:00
|
|
|
const brand = Symbol("[[webidl.brand]]");
|
|
|
|
|
|
|
|
function createInterfaceConverter(name, prototype) {
|
|
|
|
return (V, opts) => {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (!ObjectPrototypeIsPrototypeOf(prototype, V) || V[brand] !== brand) {
|
2021-02-28 19:25:31 -05:00
|
|
|
throw makeException(TypeError, `is not of type ${name}.`, opts);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:37:41 -04:00
|
|
|
// TODO(lucacasonato): have the user pass in the prototype, and not the type.
|
2021-02-28 19:25:31 -05:00
|
|
|
function createBranded(Type) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const t = ObjectCreate(Type.prototype);
|
2021-02-28 19:25:31 -05:00
|
|
|
t[brand] = brand;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertBranded(self, prototype) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
!ObjectPrototypeIsPrototypeOf(prototype, self) || self[brand] !== brand
|
|
|
|
) {
|
2021-02-28 19:25:31 -05:00
|
|
|
throw new TypeError("Illegal invocation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function illegalConstructor() {
|
|
|
|
throw new TypeError("Illegal constructor");
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:00:13 -04:00
|
|
|
function define(target, source) {
|
2022-12-19 21:37:50 -05:00
|
|
|
for (const key of new SafeArrayIterator(ReflectOwnKeys(source))) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const descriptor = ReflectGetOwnPropertyDescriptor(source, key);
|
|
|
|
if (descriptor && !ReflectDefineProperty(target, key, descriptor)) {
|
2021-04-18 19:00:13 -04:00
|
|
|
throw new TypeError(`Cannot redefine property: ${String(key)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const _iteratorInternal = Symbol("iterator internal");
|
|
|
|
|
2021-07-26 07:52:59 -04:00
|
|
|
const globalIteratorPrototype = ObjectGetPrototypeOf(ArrayIteratorPrototype);
|
2021-04-18 19:00:13 -04:00
|
|
|
|
2021-04-14 16:49:16 -04:00
|
|
|
function mixinPairIterable(name, prototype, dataSymbol, keyKey, valueKey) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const iteratorPrototype = ObjectCreate(globalIteratorPrototype, {
|
2021-07-05 04:26:25 -04:00
|
|
|
[SymbolToStringTag]: { configurable: true, value: `${name} Iterator` },
|
2021-04-18 19:00:13 -04:00
|
|
|
});
|
|
|
|
define(iteratorPrototype, {
|
|
|
|
next() {
|
|
|
|
const internal = this && this[_iteratorInternal];
|
|
|
|
if (!internal) {
|
|
|
|
throw new TypeError(
|
|
|
|
`next() called on a value that is not a ${name} iterator object`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { target, kind, index } = internal;
|
|
|
|
const values = target[dataSymbol];
|
|
|
|
const len = values.length;
|
|
|
|
if (index >= len) {
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
const pair = values[index];
|
|
|
|
internal.index = index + 1;
|
|
|
|
let result;
|
|
|
|
switch (kind) {
|
|
|
|
case "key":
|
|
|
|
result = pair[keyKey];
|
|
|
|
break;
|
|
|
|
case "value":
|
|
|
|
result = pair[valueKey];
|
|
|
|
break;
|
|
|
|
case "key+value":
|
|
|
|
result = [pair[keyKey], pair[valueKey]];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return { value: result, done: false };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
function createDefaultIterator(target, kind) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const iterator = ObjectCreate(iteratorPrototype);
|
|
|
|
ObjectDefineProperty(iterator, _iteratorInternal, {
|
2021-04-18 19:00:13 -04:00
|
|
|
value: { target, kind, index: 0 },
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:40:35 -04:00
|
|
|
function entries() {
|
2022-02-01 12:06:11 -05:00
|
|
|
assertBranded(this, prototype.prototype);
|
2021-06-16 12:40:35 -04:00
|
|
|
return createDefaultIterator(this, "key+value");
|
|
|
|
}
|
|
|
|
|
|
|
|
const properties = {
|
|
|
|
entries: {
|
|
|
|
value: entries,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2021-04-14 16:49:16 -04:00
|
|
|
},
|
2021-07-02 10:37:41 -04:00
|
|
|
[SymbolIterator]: {
|
2021-06-16 12:40:35 -04:00
|
|
|
value: entries,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2021-04-14 16:49:16 -04:00
|
|
|
},
|
2021-06-16 12:40:35 -04:00
|
|
|
keys: {
|
|
|
|
value: function keys() {
|
2022-02-01 12:06:11 -05:00
|
|
|
assertBranded(this, prototype.prototype);
|
2021-06-16 12:40:35 -04:00
|
|
|
return createDefaultIterator(this, "key");
|
|
|
|
},
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2021-04-14 16:49:16 -04:00
|
|
|
},
|
2021-06-16 12:40:35 -04:00
|
|
|
values: {
|
|
|
|
value: function values() {
|
2022-02-01 12:06:11 -05:00
|
|
|
assertBranded(this, prototype.prototype);
|
2021-06-16 12:40:35 -04:00
|
|
|
return createDefaultIterator(this, "value");
|
|
|
|
},
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2021-04-14 16:49:16 -04:00
|
|
|
},
|
2021-06-16 12:40:35 -04:00
|
|
|
forEach: {
|
|
|
|
value: function forEach(idlCallback, thisArg = undefined) {
|
2022-02-01 12:06:11 -05:00
|
|
|
assertBranded(this, prototype.prototype);
|
2021-06-16 12:40:35 -04:00
|
|
|
const prefix = `Failed to execute 'forEach' on '${name}'`;
|
|
|
|
requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
idlCallback = converters["Function"](idlCallback, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2021-07-02 10:37:41 -04:00
|
|
|
idlCallback = FunctionPrototypeBind(
|
|
|
|
idlCallback,
|
|
|
|
thisArg ?? globalThis,
|
|
|
|
);
|
2021-06-16 12:40:35 -04:00
|
|
|
const pairs = this[dataSymbol];
|
|
|
|
for (let i = 0; i < pairs.length; i++) {
|
|
|
|
const entry = pairs[i];
|
|
|
|
idlCallback(entry[valueKey], entry[keyKey], this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2021-04-14 16:49:16 -04:00
|
|
|
},
|
|
|
|
};
|
2021-07-02 10:37:41 -04:00
|
|
|
return ObjectDefineProperties(prototype.prototype, properties);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-07 04:04:10 -04:00
|
|
|
function configurePrototype(prototype) {
|
2021-07-02 10:37:41 -04:00
|
|
|
const descriptors = ObjectGetOwnPropertyDescriptors(prototype.prototype);
|
2021-06-07 04:04:10 -04:00
|
|
|
for (const key in descriptors) {
|
2022-12-21 20:54:38 -05:00
|
|
|
if (!ObjectPrototypeHasOwnProperty(descriptors, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-07 04:04:10 -04:00
|
|
|
if (key === "constructor") continue;
|
|
|
|
const descriptor = descriptors[key];
|
2022-04-16 08:09:07 -04:00
|
|
|
if (
|
|
|
|
ReflectHas(descriptor, "value") &&
|
|
|
|
typeof descriptor.value === "function"
|
|
|
|
) {
|
2021-07-02 10:37:41 -04:00
|
|
|
ObjectDefineProperty(prototype.prototype, key, {
|
2021-06-07 04:04:10 -04:00
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
2022-04-16 08:09:07 -04:00
|
|
|
} else if (ReflectHas(descriptor, "get")) {
|
2021-07-02 10:37:41 -04:00
|
|
|
ObjectDefineProperty(prototype.prototype, key, {
|
2021-06-07 04:04:10 -04:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 13:07:22 -04:00
|
|
|
ObjectDefineProperty(prototype.prototype, SymbolToStringTag, {
|
|
|
|
value: prototype.name,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
writable: false,
|
|
|
|
});
|
2021-06-07 04:04:10 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 18:13:46 -05:00
|
|
|
window.__bootstrap ??= {};
|
2021-02-13 09:58:12 -05:00
|
|
|
window.__bootstrap.webidl = {
|
2021-06-16 12:40:35 -04:00
|
|
|
type,
|
2021-03-08 08:22:05 -05:00
|
|
|
makeException,
|
2021-02-13 09:58:12 -05:00
|
|
|
converters,
|
|
|
|
requiredArguments,
|
|
|
|
createDictionaryConverter,
|
2021-02-15 18:13:46 -05:00
|
|
|
createEnumConverter,
|
|
|
|
createNullableConverter,
|
2021-02-28 19:25:31 -05:00
|
|
|
createSequenceConverter,
|
2021-03-08 08:22:05 -05:00
|
|
|
createRecordConverter,
|
2021-06-15 07:46:02 -04:00
|
|
|
createPromiseConverter,
|
|
|
|
invokeCallbackFunction,
|
2021-02-28 19:25:31 -05:00
|
|
|
createInterfaceConverter,
|
|
|
|
brand,
|
|
|
|
createBranded,
|
|
|
|
assertBranded,
|
|
|
|
illegalConstructor,
|
2021-04-14 16:49:16 -04:00
|
|
|
mixinPairIterable,
|
2021-06-07 04:04:10 -04:00
|
|
|
configurePrototype,
|
2021-02-13 09:58:12 -05:00
|
|
|
};
|
|
|
|
})(this);
|