diff --git a/lib/internal/webidl.js b/lib/internal/webidl.js index 351c1398c6d..e57108b14ba 100644 --- a/lib/internal/webidl.js +++ b/lib/internal/webidl.js @@ -29,6 +29,15 @@ const { kEmptyObject } = require('internal/util'); const converters = { __proto__: null }; +const UNDEFINED = 1; +const BOOLEAN = 2; +const STRING = 3; +const SYMBOL = 4; +const NUMBER = 5; +const BIGINT = 6; +const NULL = 7; +const OBJECT = 8; + /** * @see https://webidl.spec.whatwg.org/#es-any * @param {any} V @@ -39,7 +48,7 @@ converters.any = (V) => { }; converters.object = (V, opts = kEmptyObject) => { - if (type(V) !== 'Object') { + if (type(V) !== OBJECT) { throw makeException( 'is not an object', kEmptyObject, @@ -236,37 +245,37 @@ function createEnumConverter(name, values) { // https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values function type(V) { - if (V === null) - return 'Null'; - switch (typeof V) { case 'undefined': - return 'Undefined'; + return UNDEFINED; case 'boolean': - return 'Boolean'; + return BOOLEAN; case 'number': - return 'Number'; + return NUMBER; case 'string': - return 'String'; + return STRING; case 'symbol': - return 'Symbol'; + return SYMBOL; case 'bigint': - return 'BigInt'; + return BIGINT; case 'object': // Fall through case 'function': // Fall through default: + if (V === null) { + return NULL; + } // Per ES spec, typeof returns an implementation-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'; + return OBJECT; } } // https://webidl.spec.whatwg.org/#es-sequence function createSequenceConverter(converter) { return function(V, opts = kEmptyObject) { - if (type(V) !== 'Object') { + if (type(V) !== OBJECT) { throw makeException( 'can not be converted to sequence.', opts);