Skip to content

Commit 16691be

Browse files
ExE-Bosstargos
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 1dc7fd2 commit 16691be

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎lib/internal/event_target.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ class Event {
8383
constructor(type,options=null){
8484
if(arguments.length===0)
8585
thrownewERR_MISSING_ARGS('type');
86-
if(options!==null)
87-
validateObject(options,'options');
86+
validateObject(options,'options',{
87+
allowArray: true,allowFunction: true,nullable: true,
88+
});
8889
const{ cancelable, bubbles, composed }={ ...options};
8990
this[kCancelable]=!!cancelable;
9091
this[kBubbles]=!!bubbles;
@@ -542,7 +543,9 @@ function shouldAddListener(listener) {
542543
functionvalidateEventListenerOptions(options){
543544
if(typeofoptions==='boolean')
544545
return{capture: options};
545-
validateObject(options,'options');
546+
validateObject(options,'options',{
547+
allowArray: true,allowFunction: true,
548+
});
546549
return{
547550
once: Boolean(options.once),
548551
capture: Boolean(options.capture),

‎lib/internal/validators.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,16 @@ function validateBoolean(value, name) {
150150
}
151151

152152
constvalidateObject=hideStackFrames(
153-
(value,name,{ nullable =false}={})=>{
153+
(value,name,{
154+
nullable =false,
155+
allowArray =false,
156+
allowFunction =false,
157+
}={})=>{
154158
if((!nullable&&value===null)||
155-
ArrayIsArray(value)||
156-
typeofvalue!=='object'){
159+
(!allowArray&&ArrayIsArray(value))||
160+
(typeofvalue!=='object'&&(
161+
!allowFunction||typeofvalue!=='function'
162+
))){
157163
thrownewERR_INVALID_ARG_TYPE(name,'Object',value);
158164
}
159165
});

‎test/parallel/test-eventtarget.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ let asyncTest = Promise.resolve();
6161
'foo',
6262
1,
6363
false,
64-
function(){},
6564
].forEach((i)=>(
6665
throws(()=>newEvent('foo',i),{
6766
code: 'ERR_INVALID_ARG_TYPE',

‎test/parallel/test-validators.js‎

Lines changed: 4 additions & 1 deletion
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)