Skip to content

Commit b6eb2bd

Browse files
committed
lib: fix WebIDL object and dictionary type conversion
1 parent 703e566 commit b6eb2bd

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
@@ -85,8 +85,9 @@ class Event {
8585
constructor(type, options = null) {
8686
if (arguments.length === 0)
8787
throw new ERR_MISSING_ARGS('type');
88-
if (options !== null)
89-
validateObject(options, 'options');
88+
validateObject(options, 'options', {
89+
allowArray: true, allowFunction: true, nullable: true,
90+
});
9091
const { cancelable, bubbles, composed } = { ...options };
9192
this[kCancelable] = !!cancelable;
9293
this[kBubbles] = !!bubbles;
@@ -563,7 +564,9 @@ function shouldAddListener(listener) {
563564
function validateEventListenerOptions(options) {
564565
if (typeof options === 'boolean')
565566
return { capture: options };
566-
validateObject(options, 'options');
567+
validateObject(options, 'options', {
568+
allowArray: true, allowFunction: true,
569+
});
567570
return {
568571
once: Boolean(options.once),
569572
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
@@ -57,7 +57,6 @@ let asyncTest = Promise.resolve();
5757
'foo',
5858
1,
5959
false,
60-
function() {},
6160
].forEach((i) => (
6261
throws(() => new Event('foo', i), {
6362
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)