Skip to content

Commit

Permalink
lib: prefer number to string in webidl type function
Browse files Browse the repository at this point in the history
PR-URL: #55489
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jazelly authored and aduh95 committed Nov 6, 2024
1 parent aebf676 commit bf552fa
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bf552fa

Please sign in to comment.