| 'use strict' |
|
|
| const { types } = require('util') |
| const { hasOwn, toUSVString } = require('./util') |
|
|
| |
| const webidl = {} |
| webidl.converters = {} |
| webidl.util = {} |
| webidl.errors = {} |
|
|
| webidl.errors.exception = function (message) { |
| return new TypeError(`${message.header}: ${message.message}`) |
| } |
|
|
| webidl.errors.conversionFailed = function (context) { |
| const plural = context.types.length === 1 ? '' : ' one of' |
| const message = |
| `${context.argument} could not be converted to` + |
| `${plural}: ${context.types.join(', ')}.` |
|
|
| return webidl.errors.exception({ |
| header: context.prefix, |
| message |
| }) |
| } |
|
|
| webidl.errors.invalidArgument = function (context) { |
| return webidl.errors.exception({ |
| header: context.prefix, |
| message: `"${context.value}" is an invalid ${context.type}.` |
| }) |
| } |
|
|
| |
| webidl.brandCheck = function (V, I, opts = undefined) { |
| if (opts?.strict !== false && !(V instanceof I)) { |
| throw new TypeError('Illegal invocation') |
| } else { |
| return V?.[Symbol.toStringTag] === I.prototype[Symbol.toStringTag] |
| } |
| } |
|
|
| webidl.argumentLengthCheck = function ({ length }, min, ctx) { |
| if (length < min) { |
| throw webidl.errors.exception({ |
| message: `${min} argument${min !== 1 ? 's' : ''} required, ` + |
| `but${length ? ' only' : ''} ${length} found.`, |
| ...ctx |
| }) |
| } |
| } |
|
|
| webidl.illegalConstructor = function () { |
| throw webidl.errors.exception({ |
| header: 'TypeError', |
| message: 'Illegal constructor' |
| }) |
| } |
|
|
| |
| webidl.util.Type = function (V) { |
| switch (typeof V) { |
| case 'undefined': return 'Undefined' |
| case 'boolean': return 'Boolean' |
| case 'string': return 'String' |
| case 'symbol': return 'Symbol' |
| case 'number': return 'Number' |
| case 'bigint': return 'BigInt' |
| case 'function': |
| case 'object': { |
| if (V === null) { |
| return 'Null' |
| } |
|
|
| return 'Object' |
| } |
| } |
| } |
|
|
| |
| webidl.util.ConvertToInt = function (V, bitLength, signedness, opts = {}) { |
| let upperBound |
| let lowerBound |
|
|
| |
| if (bitLength === 64) { |
| |
| upperBound = Math.pow(2, 53) - 1 |
|
|
| |
| if (signedness === 'unsigned') { |
| lowerBound = 0 |
| } else { |
| |
| lowerBound = Math.pow(-2, 53) + 1 |
| } |
| } else if (signedness === 'unsigned') { |
| |
|
|
| |
| lowerBound = 0 |
|
|
| |
| upperBound = Math.pow(2, bitLength) - 1 |
| } else { |
| |
|
|
| |
| lowerBound = Math.pow(-2, bitLength) - 1 |
|
|
| |
| upperBound = Math.pow(2, bitLength - 1) - 1 |
| } |
|
|
| |
| let x = Number(V) |
|
|
| |
| if (x === 0) { |
| x = 0 |
| } |
|
|
| |
| |
| if (opts.enforceRange === true) { |
| |
| if ( |
| Number.isNaN(x) || |
| x === Number.POSITIVE_INFINITY || |
| x === Number.NEGATIVE_INFINITY |
| ) { |
| throw webidl.errors.exception({ |
| header: 'Integer conversion', |
| message: `Could not convert ${V} to an integer.` |
| }) |
| } |
|
|
| |
| x = webidl.util.IntegerPart(x) |
|
|
| |
| |
| if (x < lowerBound || x > upperBound) { |
| throw webidl.errors.exception({ |
| header: 'Integer conversion', |
| message: `Value must be between ${lowerBound}-${upperBound}, got ${x}.` |
| }) |
| } |
|
|
| |
| return x |
| } |
|
|
| |
| |
| |
| if (!Number.isNaN(x) && opts.clamp === true) { |
| |
| x = Math.min(Math.max(x, lowerBound), upperBound) |
|
|
| |
| |
| |
| if (Math.floor(x) % 2 === 0) { |
| x = Math.floor(x) |
| } else { |
| x = Math.ceil(x) |
| } |
|
|
| |
| return x |
| } |
|
|
| |
| if ( |
| Number.isNaN(x) || |
| (x === 0 && Object.is(0, x)) || |
| x === Number.POSITIVE_INFINITY || |
| x === Number.NEGATIVE_INFINITY |
| ) { |
| return 0 |
| } |
|
|
| |
| x = webidl.util.IntegerPart(x) |
|
|
| |
| x = x % Math.pow(2, bitLength) |
|
|
| |
| |
| if (signedness === 'signed' && x >= Math.pow(2, bitLength) - 1) { |
| return x - Math.pow(2, bitLength) |
| } |
|
|
| |
| return x |
| } |
|
|
| |
| webidl.util.IntegerPart = function (n) { |
| |
| const r = Math.floor(Math.abs(n)) |
|
|
| |
| if (n < 0) { |
| return -1 * r |
| } |
|
|
| |
| return r |
| } |
|
|
| |
| webidl.sequenceConverter = function (converter) { |
| return (V) => { |
| |
| if (webidl.util.Type(V) !== 'Object') { |
| throw webidl.errors.exception({ |
| header: 'Sequence', |
| message: `Value of type ${webidl.util.Type(V)} is not an Object.` |
| }) |
| } |
|
|
| |
| |
| const method = V?.[Symbol.iterator]?.() |
| const seq = [] |
|
|
| |
| if ( |
| method === undefined || |
| typeof method.next !== 'function' |
| ) { |
| throw webidl.errors.exception({ |
| header: 'Sequence', |
| message: 'Object is not an iterator.' |
| }) |
| } |
|
|
| |
| while (true) { |
| const { done, value } = method.next() |
|
|
| if (done) { |
| break |
| } |
|
|
| seq.push(converter(value)) |
| } |
|
|
| return seq |
| } |
| } |
|
|
| |
| webidl.recordConverter = function (keyConverter, valueConverter) { |
| return (O) => { |
| |
| if (webidl.util.Type(O) !== 'Object') { |
| throw webidl.errors.exception({ |
| header: 'Record', |
| message: `Value of type ${webidl.util.Type(O)} is not an Object.` |
| }) |
| } |
|
|
| |
| const result = {} |
|
|
| if (!types.isProxy(O)) { |
| |
| const keys = Object.keys(O) |
|
|
| for (const key of keys) { |
| |
| const typedKey = keyConverter(key) |
|
|
| |
| |
| const typedValue = valueConverter(O[key]) |
|
|
| |
| result[typedKey] = typedValue |
| } |
|
|
| |
| return result |
| } |
|
|
| |
| const keys = Reflect.ownKeys(O) |
|
|
| |
| for (const key of keys) { |
| |
| const desc = Reflect.getOwnPropertyDescriptor(O, key) |
|
|
| |
| if (desc?.enumerable) { |
| |
| const typedKey = keyConverter(key) |
|
|
| |
| |
| const typedValue = valueConverter(O[key]) |
|
|
| |
| result[typedKey] = typedValue |
| } |
| } |
|
|
| |
| return result |
| } |
| } |
|
|
| webidl.interfaceConverter = function (i) { |
| return (V, opts = {}) => { |
| if (opts.strict !== false && !(V instanceof i)) { |
| throw webidl.errors.exception({ |
| header: i.name, |
| message: `Expected ${V} to be an instance of ${i.name}.` |
| }) |
| } |
|
|
| return V |
| } |
| } |
|
|
| webidl.dictionaryConverter = function (converters) { |
| return (dictionary) => { |
| const type = webidl.util.Type(dictionary) |
| const dict = {} |
|
|
| if (type === 'Null' || type === 'Undefined') { |
| return dict |
| } else if (type !== 'Object') { |
| throw webidl.errors.exception({ |
| header: 'Dictionary', |
| message: `Expected ${dictionary} to be one of: Null, Undefined, Object.` |
| }) |
| } |
|
|
| for (const options of converters) { |
| const { key, defaultValue, required, converter } = options |
|
|
| if (required === true) { |
| if (!hasOwn(dictionary, key)) { |
| throw webidl.errors.exception({ |
| header: 'Dictionary', |
| message: `Missing required key "${key}".` |
| }) |
| } |
| } |
|
|
| let value = dictionary[key] |
| const hasDefault = hasOwn(options, 'defaultValue') |
|
|
| |
| |
| if (hasDefault && value !== null) { |
| value = value ?? defaultValue |
| } |
|
|
| |
| |
| |
| if (required || hasDefault || value !== undefined) { |
| value = converter(value) |
|
|
| if ( |
| options.allowedValues && |
| !options.allowedValues.includes(value) |
| ) { |
| throw webidl.errors.exception({ |
| header: 'Dictionary', |
| message: `${value} is not an accepted type. Expected one of ${options.allowedValues.join(', ')}.` |
| }) |
| } |
|
|
| dict[key] = value |
| } |
| } |
|
|
| return dict |
| } |
| } |
|
|
| webidl.nullableConverter = function (converter) { |
| return (V) => { |
| if (V === null) { |
| return V |
| } |
|
|
| return converter(V) |
| } |
| } |
|
|
| |
| webidl.converters.DOMString = function (V, opts = {}) { |
| |
| |
| |
| |
| if (V === null && opts.legacyNullToEmptyString) { |
| return '' |
| } |
|
|
| |
| if (typeof V === 'symbol') { |
| throw new TypeError('Could not convert argument of type symbol to string.') |
| } |
|
|
| |
| |
| |
| return String(V) |
| } |
|
|
| |
| webidl.converters.ByteString = function (V) { |
| |
| |
| const x = webidl.converters.DOMString(V) |
|
|
| |
| |
| for (let index = 0; index < x.length; index++) { |
| if (x.charCodeAt(index) > 255) { |
| throw new TypeError( |
| 'Cannot convert argument to a ByteString because the character at ' + |
| `index ${index} has a value of ${x.charCodeAt(index)} which is greater than 255.` |
| ) |
| } |
| } |
|
|
| |
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters.USVString = toUSVString |
|
|
| |
| webidl.converters.boolean = function (V) { |
| |
| const x = Boolean(V) |
|
|
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters.any = function (V) { |
| return V |
| } |
|
|
| |
| webidl.converters['long long'] = function (V) { |
| |
| const x = webidl.util.ConvertToInt(V, 64, 'signed') |
|
|
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters['unsigned long long'] = function (V) { |
| |
| const x = webidl.util.ConvertToInt(V, 64, 'unsigned') |
|
|
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters['unsigned long'] = function (V) { |
| |
| const x = webidl.util.ConvertToInt(V, 32, 'unsigned') |
|
|
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters['unsigned short'] = function (V, opts) { |
| |
| const x = webidl.util.ConvertToInt(V, 16, 'unsigned', opts) |
|
|
| |
| |
| return x |
| } |
|
|
| |
| webidl.converters.ArrayBuffer = function (V, opts = {}) { |
| |
| |
| |
| |
| |
| if ( |
| webidl.util.Type(V) !== 'Object' || |
| !types.isAnyArrayBuffer(V) |
| ) { |
| throw webidl.errors.conversionFailed({ |
| prefix: `${V}`, |
| argument: `${V}`, |
| types: ['ArrayBuffer'] |
| }) |
| } |
|
|
| |
| |
| |
| |
| if (opts.allowShared === false && types.isSharedArrayBuffer(V)) { |
| throw webidl.errors.exception({ |
| header: 'ArrayBuffer', |
| message: 'SharedArrayBuffer is not allowed.' |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| return V |
| } |
|
|
| webidl.converters.TypedArray = function (V, T, opts = {}) { |
| |
|
|
| |
| |
| |
| if ( |
| webidl.util.Type(V) !== 'Object' || |
| !types.isTypedArray(V) || |
| V.constructor.name !== T.name |
| ) { |
| throw webidl.errors.conversionFailed({ |
| prefix: `${T.name}`, |
| argument: `${V}`, |
| types: [T.name] |
| }) |
| } |
|
|
| |
| |
| |
| |
| if (opts.allowShared === false && types.isSharedArrayBuffer(V.buffer)) { |
| throw webidl.errors.exception({ |
| header: 'ArrayBuffer', |
| message: 'SharedArrayBuffer is not allowed.' |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| return V |
| } |
|
|
| webidl.converters.DataView = function (V, opts = {}) { |
| |
| |
| if (webidl.util.Type(V) !== 'Object' || !types.isDataView(V)) { |
| throw webidl.errors.exception({ |
| header: 'DataView', |
| message: 'Object is not a DataView.' |
| }) |
| } |
|
|
| |
| |
| |
| |
| if (opts.allowShared === false && types.isSharedArrayBuffer(V.buffer)) { |
| throw webidl.errors.exception({ |
| header: 'ArrayBuffer', |
| message: 'SharedArrayBuffer is not allowed.' |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| return V |
| } |
|
|
| |
| webidl.converters.BufferSource = function (V, opts = {}) { |
| if (types.isAnyArrayBuffer(V)) { |
| return webidl.converters.ArrayBuffer(V, opts) |
| } |
|
|
| if (types.isTypedArray(V)) { |
| return webidl.converters.TypedArray(V, V.constructor) |
| } |
|
|
| if (types.isDataView(V)) { |
| return webidl.converters.DataView(V, opts) |
| } |
|
|
| throw new TypeError(`Could not convert ${V} to a BufferSource.`) |
| } |
|
|
| webidl.converters['sequence<ByteString>'] = webidl.sequenceConverter( |
| webidl.converters.ByteString |
| ) |
|
|
| webidl.converters['sequence<sequence<ByteString>>'] = webidl.sequenceConverter( |
| webidl.converters['sequence<ByteString>'] |
| ) |
|
|
| webidl.converters['record<ByteString, ByteString>'] = webidl.recordConverter( |
| webidl.converters.ByteString, |
| webidl.converters.ByteString |
| ) |
|
|
| module.exports = { |
| webidl |
| } |
|
|