lib: prefer number to string in webidl type function

PR-URL: https://github.com/nodejs/node/pull/55489
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Jason Zhang 2024-10-24 23:36:58 +10:30 committed by Antoine du Hamel
parent 03dcd7077a
commit 270da88228
No known key found for this signature in database
GPG Key ID: 21D900FFDB233756

View File

@ -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);