Skip to content

Commit 16691be

Browse files
ExE-Bosstargos
authored andcommitted
lib: fix WebIDL object and dictionary type conversion
PR-URL: #37047 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 1dc7fd2 commit 16691be

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

lib/internal/event_target.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ class Event {
8383
constructor(type, options = null) {
8484
if (arguments.length === 0)
8585
throw new ERR_MISSING_ARGS('type');
86-
if (options !== null)
87-
validateObject(options, 'options');
86+
validateObject(options, 'options', {
87+
allowArray: true, allowFunction: true, nullable: true,
88+
});
8889
const { cancelable, bubbles, composed } = { ...options };
8990
this[kCancelable] = !!cancelable;
9091
this[kBubbles] = !!bubbles;
@@ -542,7 +543,9 @@ function shouldAddListener(listener) {
542543
function validateEventListenerOptions(options) {
543544
if (typeof options === 'boolean')
544545
return { capture: options };
545-
validateObject(options, 'options');
546+
validateObject(options, 'options', {
547+
allowArray: true, allowFunction: true,
548+
});
546549
return {
547550
once: Boolean(options.once),
548551
capture: Boolean(options.capture),

lib/internal/validators.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,16 @@ function validateBoolean(value, name) {
150150
}
151151

152152
const validateObject = hideStackFrames(
153-
(value, name, { nullable = false } = {}) => {
153+
(value, name, {
154+
nullable = false,
155+
allowArray = false,
156+
allowFunction = false,
157+
} = {}) => {
154158
if ((!nullable && value === null) ||
155-
ArrayIsArray(value) ||
156-
typeof value !== 'object') {
159+
(!allowArray && ArrayIsArray(value)) ||
160+
(typeof value !== 'object' && (
161+
!allowFunction || typeof value !== 'function'
162+
))) {
157163
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
158164
}
159165
});

test/parallel/test-eventtarget.js

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ let asyncTest = Promise.resolve();
6161
'foo',
6262
1,
6363
false,
64-
function() {},
6564
].forEach((i) => (
6665
throws(() => new Event('foo', i), {
6766
code: 'ERR_INVALID_ARG_TYPE',

test/parallel/test-validators.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,17 @@ const invalidArgValueError = {
7878
validateObject({}, 'foo');
7979
validateObject({ a: 42, b: 'foo' }, 'foo');
8080

81-
[undefined, null, true, false, 0, 0.0, 42, '', 'string', []]
81+
[undefined, null, true, false, 0, 0.0, 42, '', 'string', [], () => {}]
8282
.forEach((val) => {
8383
assert.throws(() => {
8484
validateObject(val, 'foo');
8585
}, invalidArgTypeError);
8686
});
8787

88+
// validateObject options tests:
8889
validateObject(null, 'foo', { nullable: true });
90+
validateObject([], 'foo', { allowArray: true });
91+
validateObject(() => {}, 'foo', { allowFunction: true });
8992
}
9093

9194
{

0 commit comments

Comments
 (0)