Skip to content

Commit f6f9af6

Browse files
ExE-Bossdanielleadams
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 8483de4 commit f6f9af6

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

lib/internal/crypto/webcrypto.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ async function deriveKey(
161161
}
162162
if (baseKey.algorithm.name !== algorithm.name)
163163
throw lazyDOMException('Key algorithm mismatch', 'InvalidAccessError');
164-
validateObject(derivedKeyAlgorithm, 'derivedKeyAlgorithm');
164+
validateObject(derivedKeyAlgorithm, 'derivedKeyAlgorithm', {
165+
allowArray: true, allowFunction: true,
166+
});
165167
validateBoolean(extractable, 'extractable');
166168
validateArray(keyUsages, 'keyUsages');
167169

lib/internal/event_target.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ class Event {
8787
constructor(type, options = null) {
8888
if (arguments.length === 0)
8989
throw new ERR_MISSING_ARGS('type');
90-
if (options !== null)
91-
validateObject(options, 'options');
90+
validateObject(options, 'options', {
91+
allowArray: true, allowFunction: true, nullable: true,
92+
});
9293
const { cancelable, bubbles, composed } = { ...options };
9394
this[kCancelable] = !!cancelable;
9495
this[kBubbles] = !!bubbles;
@@ -601,7 +602,9 @@ function shouldAddListener(listener) {
601602
function validateEventListenerOptions(options) {
602603
if (typeof options === 'boolean')
603604
return { capture: options };
604-
validateObject(options, 'options');
605+
validateObject(options, 'options', {
606+
allowArray: true, allowFunction: true,
607+
});
605608
return {
606609
once: Boolean(options.once),
607610
capture: Boolean(options.capture),

lib/internal/validators.js

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

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

test/parallel/test-eventtarget.js

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ let asyncTest = Promise.resolve();
6060
'foo',
6161
1,
6262
false,
63-
function() {},
6463
].forEach((i) => (
6564
throws(() => new Event('foo', i), {
6665
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)