Skip to content

Commit

Permalink
lib: fix WebIDL object and dictionary type conversion
Browse files Browse the repository at this point in the history
PR-URL: #37047
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ExE-Boss authored and targos committed Jun 11, 2021
1 parent 1dc7fd2 commit 16691be
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ class Event {
constructor(type, options = null) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
if (options !== null)
validateObject(options, 'options');
validateObject(options, 'options', {
allowArray: true, allowFunction: true, nullable: true,
});
const { cancelable, bubbles, composed } = { ...options };
this[kCancelable] = !!cancelable;
this[kBubbles] = !!bubbles;
Expand Down Expand Up @@ -542,7 +543,9 @@ function shouldAddListener(listener) {
function validateEventListenerOptions(options) {
if (typeof options === 'boolean')
return { capture: options };
validateObject(options, 'options');
validateObject(options, 'options', {
allowArray: true, allowFunction: true,
});
return {
once: Boolean(options.once),
capture: Boolean(options.capture),
Expand Down
12 changes: 9 additions & 3 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ function validateBoolean(value, name) {
}

const validateObject = hideStackFrames(
(value, name, { nullable = false } = {}) => {
(value, name, {
nullable = false,
allowArray = false,
allowFunction = false,
} = {}) => {
if ((!nullable && value === null) ||
ArrayIsArray(value) ||
typeof value !== 'object') {
(!allowArray && ArrayIsArray(value)) ||
(typeof value !== 'object' && (
!allowFunction || typeof value !== 'function'
))) {
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
}
});
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ let asyncTest = Promise.resolve();
'foo',
1,
false,
function() {},
].forEach((i) => (
throws(() => new Event('foo', i), {
code: 'ERR_INVALID_ARG_TYPE',
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ const invalidArgValueError = {
validateObject({}, 'foo');
validateObject({ a: 42, b: 'foo' }, 'foo');

[undefined, null, true, false, 0, 0.0, 42, '', 'string', []]
[undefined, null, true, false, 0, 0.0, 42, '', 'string', [], () => {}]
.forEach((val) => {
assert.throws(() => {
validateObject(val, 'foo');
}, invalidArgTypeError);
});

// validateObject options tests:
validateObject(null, 'foo', { nullable: true });
validateObject([], 'foo', { allowArray: true });
validateObject(() => {}, 'foo', { allowFunction: true });
}

{
Expand Down

0 comments on commit 16691be

Please sign in to comment.