lib: fix WebIDL object and dictionary type conversion

PR-URL: https://github.com/nodejs/node/pull/37047
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
ExE Boss authored and Michaël Zasso committed 2021-06-11 09:29:59 +02:00
1 parent 1dc7fd238c
commit 16691be80e
4 files changed
+19 -8

No files matched your search

+6 -3
View File
@@ -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;
@@ -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),
+9 -3
View File
@@ -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);
}
});
-1
View File
@@ -61,7 +61,6 @@ let asyncTest = Promise.resolve();
'foo',
1,
false,
function() {},
].forEach((i) => (
throws(() => new Event('foo', i), {
code: 'ERR_INVALID_ARG_TYPE',
+4 -1
View File
@@ -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 });
}
{