Skip to content

Commit bf552fa

Browse files
jazellyaduh95
authored andcommittedNov 6, 2024
lib: prefer number to string in webidl type function
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>
1 parent aebf676 commit bf552fa

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed
 

Diff for: ‎lib/internal/webidl.js

+21-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ const { kEmptyObject } = require('internal/util');
2929

3030
const converters = { __proto__: null };
3131

32+
const UNDEFINED = 1;
33+
const BOOLEAN = 2;
34+
const STRING = 3;
35+
const SYMBOL = 4;
36+
const NUMBER = 5;
37+
const BIGINT = 6;
38+
const NULL = 7;
39+
const OBJECT = 8;
40+
3241
/**
3342
* @see https://webidl.spec.whatwg.org/#es-any
3443
* @param {any} V
@@ -39,7 +48,7 @@ converters.any = (V) => {
3948
};
4049

4150
converters.object = (V, opts = kEmptyObject) => {
42-
if (type(V) !== 'Object') {
51+
if (type(V) !== OBJECT) {
4352
throw makeException(
4453
'is not an object',
4554
kEmptyObject,
@@ -236,37 +245,37 @@ function createEnumConverter(name, values) {
236245

237246
// https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
238247
function type(V) {
239-
if (V === null)
240-
return 'Null';
241-
242248
switch (typeof V) {
243249
case 'undefined':
244-
return 'Undefined';
250+
return UNDEFINED;
245251
case 'boolean':
246-
return 'Boolean';
252+
return BOOLEAN;
247253
case 'number':
248-
return 'Number';
254+
return NUMBER;
249255
case 'string':
250-
return 'String';
256+
return STRING;
251257
case 'symbol':
252-
return 'Symbol';
258+
return SYMBOL;
253259
case 'bigint':
254-
return 'BigInt';
260+
return BIGINT;
255261
case 'object': // Fall through
256262
case 'function': // Fall through
257263
default:
264+
if (V === null) {
265+
return NULL;
266+
}
258267
// Per ES spec, typeof returns an implementation-defined value that is not
259268
// any of the existing ones for uncallable non-standard exotic objects.
260269
// Yet Type() which the Web IDL spec depends on returns Object for such
261270
// cases. So treat the default case as an object.
262-
return 'Object';
271+
return OBJECT;
263272
}
264273
}
265274

266275
// https://webidl.spec.whatwg.org/#es-sequence
267276
function createSequenceConverter(converter) {
268277
return function(V, opts = kEmptyObject) {
269-
if (type(V) !== 'Object') {
278+
if (type(V) !== OBJECT) {
270279
throw makeException(
271280
'can not be converted to sequence.',
272281
opts);

0 commit comments

Comments
 (0)