123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- "use strict";
- function makeException(ErrorType, message, options) {
- if (options.globals) {
- ErrorType = options.globals[ErrorType.name];
- }
- return new ErrorType(`${options.context ? options.context : "Value"} ${message}.`);
- }
- function toNumber(value, options) {
- if (typeof value === "bigint") {
- throw makeException(TypeError, "is a BigInt which cannot be converted to a number", options);
- }
- if (!options.globals) {
- return Number(value);
- }
- return options.globals.Number(value);
- }
- // 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)) {
- return censorNegativeZero(Math.floor(x));
- }
- return censorNegativeZero(Math.round(x));
- }
- function integerPart(n) {
- return censorNegativeZero(Math.trunc(n));
- }
- 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, { unsigned }) {
- let lowerBound, upperBound;
- if (unsigned) {
- lowerBound = 0;
- upperBound = 2 ** bitLength - 1;
- } else {
- lowerBound = -(2 ** (bitLength - 1));
- upperBound = 2 ** (bitLength - 1) - 1;
- }
- const twoToTheBitLength = 2 ** bitLength;
- const twoToOneLessThanTheBitLength = 2 ** (bitLength - 1);
- return (value, options = {}) => {
- let x = toNumber(value, options);
- x = censorNegativeZero(x);
- if (options.enforceRange) {
- if (!Number.isFinite(x)) {
- throw makeException(TypeError, "is not a finite number", options);
- }
- x = integerPart(x);
- if (x < lowerBound || x > upperBound) {
- throw makeException(
- TypeError,
- `is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
- options
- );
- }
- return x;
- }
- if (!Number.isNaN(x) && options.clamp) {
- x = Math.min(Math.max(x, lowerBound), upperBound);
- x = evenRound(x);
- return x;
- }
- if (!Number.isFinite(x) || x === 0) {
- 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 (!unsigned && x >= twoToOneLessThanTheBitLength) {
- return x - twoToTheBitLength;
- }
- return x;
- };
- }
- function createLongLongConversion(bitLength, { unsigned }) {
- const upperBound = Number.MAX_SAFE_INTEGER;
- const lowerBound = unsigned ? 0 : Number.MIN_SAFE_INTEGER;
- const asBigIntN = unsigned ? BigInt.asUintN : BigInt.asIntN;
- return (value, options = {}) => {
- let x = toNumber(value, options);
- x = censorNegativeZero(x);
- if (options.enforceRange) {
- if (!Number.isFinite(x)) {
- throw makeException(TypeError, "is not a finite number", options);
- }
- x = integerPart(x);
- if (x < lowerBound || x > upperBound) {
- throw makeException(
- TypeError,
- `is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
- options
- );
- }
- return x;
- }
- if (!Number.isNaN(x) && options.clamp) {
- x = Math.min(Math.max(x, lowerBound), upperBound);
- x = evenRound(x);
- return x;
- }
- if (!Number.isFinite(x) || x === 0) {
- return 0;
- }
- let xBigInt = BigInt(integerPart(x));
- xBigInt = asBigIntN(bitLength, xBigInt);
- return Number(xBigInt);
- };
- }
- exports.any = value => {
- return value;
- };
- exports.undefined = () => {
- return undefined;
- };
- exports.boolean = value => {
- return Boolean(value);
- };
- exports.byte = createIntegerConversion(8, { unsigned: false });
- exports.octet = createIntegerConversion(8, { unsigned: true });
- exports.short = createIntegerConversion(16, { unsigned: false });
- exports["unsigned short"] = createIntegerConversion(16, { unsigned: true });
- exports.long = createIntegerConversion(32, { unsigned: false });
- exports["unsigned long"] = createIntegerConversion(32, { unsigned: true });
- exports["long long"] = createLongLongConversion(64, { unsigned: false });
- exports["unsigned long long"] = createLongLongConversion(64, { unsigned: true });
- exports.double = (value, options = {}) => {
- const x = toNumber(value, options);
- if (!Number.isFinite(x)) {
- throw makeException(TypeError, "is not a finite floating-point value", options);
- }
- return x;
- };
- exports["unrestricted double"] = (value, options = {}) => {
- const x = toNumber(value, options);
- return x;
- };
- exports.float = (value, options = {}) => {
- const x = toNumber(value, options);
- if (!Number.isFinite(x)) {
- throw makeException(TypeError, "is not a finite floating-point value", options);
- }
- if (Object.is(x, -0)) {
- return x;
- }
- const y = Math.fround(x);
- if (!Number.isFinite(y)) {
- throw makeException(TypeError, "is outside the range of a single-precision floating-point value", options);
- }
- return y;
- };
- exports["unrestricted float"] = (value, options = {}) => {
- const x = toNumber(value, options);
- if (isNaN(x)) {
- return x;
- }
- if (Object.is(x, -0)) {
- return x;
- }
- return Math.fround(x);
- };
- exports.DOMString = (value, options = {}) => {
- if (options.treatNullAsEmptyString && value === null) {
- return "";
- }
- if (typeof value === "symbol") {
- throw makeException(TypeError, "is a symbol, which cannot be converted to a string", options);
- }
- const StringCtor = options.globals ? options.globals.String : String;
- return StringCtor(value);
- };
- exports.ByteString = (value, options = {}) => {
- const x = exports.DOMString(value, options);
- let c;
- for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
- if (c > 255) {
- throw makeException(TypeError, "is not a valid ByteString", options);
- }
- }
- return x;
- };
- exports.USVString = (value, options = {}) => {
- const S = exports.DOMString(value, options);
- const n = S.length;
- const U = [];
- for (let i = 0; i < n; ++i) {
- const c = S.charCodeAt(i);
- if (c < 0xD800 || c > 0xDFFF) {
- U.push(String.fromCodePoint(c));
- } else if (0xDC00 <= c && c <= 0xDFFF) {
- U.push(String.fromCodePoint(0xFFFD));
- } else if (i === n - 1) {
- U.push(String.fromCodePoint(0xFFFD));
- } else {
- const d = S.charCodeAt(i + 1);
- if (0xDC00 <= d && d <= 0xDFFF) {
- const a = c & 0x3FF;
- const b = d & 0x3FF;
- U.push(String.fromCodePoint((2 << 15) + ((2 << 9) * a) + b));
- ++i;
- } else {
- U.push(String.fromCodePoint(0xFFFD));
- }
- }
- }
- return U.join("");
- };
- exports.object = (value, options = {}) => {
- if (value === null || (typeof value !== "object" && typeof value !== "function")) {
- throw makeException(TypeError, "is not an object", options);
- }
- return value;
- };
- const abByteLengthGetter =
- Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
- const sabByteLengthGetter =
- typeof SharedArrayBuffer === "function" ?
- Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength").get :
- null;
- function isNonSharedArrayBuffer(value) {
- try {
- // This will throw on SharedArrayBuffers, but not detached ArrayBuffers.
- // (The spec says it should throw, but the spec conflicts with implementations: https://github.com/tc39/ecma262/issues/678)
- abByteLengthGetter.call(value);
- return true;
- } catch {
- return false;
- }
- }
- function isSharedArrayBuffer(value) {
- try {
- sabByteLengthGetter.call(value);
- return true;
- } catch {
- return false;
- }
- }
- function isArrayBufferDetached(value) {
- try {
- // eslint-disable-next-line no-new
- new Uint8Array(value);
- return false;
- } catch {
- return true;
- }
- }
- exports.ArrayBuffer = (value, options = {}) => {
- if (!isNonSharedArrayBuffer(value)) {
- if (options.allowShared && !isSharedArrayBuffer(value)) {
- throw makeException(TypeError, "is not an ArrayBuffer or SharedArrayBuffer", options);
- }
- throw makeException(TypeError, "is not an ArrayBuffer", options);
- }
- if (isArrayBufferDetached(value)) {
- throw makeException(TypeError, "is a detached ArrayBuffer", options);
- }
- return value;
- };
- const dvByteLengthGetter =
- Object.getOwnPropertyDescriptor(DataView.prototype, "byteLength").get;
- exports.DataView = (value, options = {}) => {
- try {
- dvByteLengthGetter.call(value);
- } catch (e) {
- throw makeException(TypeError, "is not a DataView", options);
- }
- if (!options.allowShared && isSharedArrayBuffer(value.buffer)) {
- throw makeException(TypeError, "is backed by a SharedArrayBuffer, which is not allowed", options);
- }
- if (isArrayBufferDetached(value.buffer)) {
- throw makeException(TypeError, "is backed by a detached ArrayBuffer", options);
- }
- return value;
- };
- // 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
- const typedArrayNameGetter = Object.getOwnPropertyDescriptor(
- Object.getPrototypeOf(Uint8Array).prototype,
- Symbol.toStringTag
- ).get;
- [
- Int8Array,
- Int16Array,
- Int32Array,
- Uint8Array,
- Uint16Array,
- Uint32Array,
- Uint8ClampedArray,
- Float32Array,
- Float64Array
- ].forEach(func => {
- const { name } = func;
- const article = /^[AEIOU]/u.test(name) ? "an" : "a";
- exports[name] = (value, options = {}) => {
- if (!ArrayBuffer.isView(value) || typedArrayNameGetter.call(value) !== name) {
- throw makeException(TypeError, `is not ${article} ${name} object`, options);
- }
- if (!options.allowShared && isSharedArrayBuffer(value.buffer)) {
- throw makeException(TypeError, "is a view on a SharedArrayBuffer, which is not allowed", options);
- }
- if (isArrayBufferDetached(value.buffer)) {
- throw makeException(TypeError, "is a view on a detached ArrayBuffer", options);
- }
- return value;
- };
- });
- // Common definitions
- exports.ArrayBufferView = (value, options = {}) => {
- if (!ArrayBuffer.isView(value)) {
- throw makeException(TypeError, "is not a view on an ArrayBuffer or SharedArrayBuffer", options);
- }
- if (!options.allowShared && isSharedArrayBuffer(value.buffer)) {
- throw makeException(TypeError, "is a view on a SharedArrayBuffer, which is not allowed", options);
- }
- if (isArrayBufferDetached(value.buffer)) {
- throw makeException(TypeError, "is a view on a detached ArrayBuffer", options);
- }
- return value;
- };
- exports.BufferSource = (value, options = {}) => {
- if (ArrayBuffer.isView(value)) {
- if (!options.allowShared && isSharedArrayBuffer(value.buffer)) {
- throw makeException(TypeError, "is a view on a SharedArrayBuffer, which is not allowed", options);
- }
- if (isArrayBufferDetached(value.buffer)) {
- throw makeException(TypeError, "is a view on a detached ArrayBuffer", options);
- }
- return value;
- }
- if (!options.allowShared && !isNonSharedArrayBuffer(value)) {
- throw makeException(TypeError, "is not an ArrayBuffer or a view on one", options);
- }
- if (options.allowShared && !isSharedArrayBuffer(value) && !isNonSharedArrayBuffer(value)) {
- throw makeException(TypeError, "is not an ArrayBuffer, SharedArrayBuffer, or a view on one", options);
- }
- if (isArrayBufferDetached(value)) {
- throw makeException(TypeError, "is a detached ArrayBuffer", options);
- }
- return value;
- };
- exports.DOMTimeStamp = exports["unsigned long long"];
|