mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
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:
1 parent
1dc7fd238c
commit
16691be80e
4 files changed
+19
-8
No files matched your search
@@ -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),
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -61,7 +61,6 @@ let asyncTest = Promise.resolve();
|
||||
'foo',
|
||||
1,
|
||||
false,
|
||||
function() {},
|
||||
].forEach((i) => (
|
||||
throws(() => new Event('foo', i), {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user