-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for callback functions #194
Implement support for callback functions #194
Conversation
Co-authored-by: Timothy Gu <timothygu99@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for jumping on this!
const thisArg = utils.tryWrapperForImpl(this); | ||
let callResult; | ||
|
||
callResult = Reflect.apply(value, thisArg, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional, but if it's easy, it would be nice to remove the unused callResult
variable from this case (and construct).
Co-authored-by: Domenic Denicola <d@domenic.me>
Be sure to test this on jsdom before making a release. What blocked my other patch was that existing jsdom code didn't like this change. |
I’ll begin work on the JSDOM side of this once jsdom/jsdom#2884 is merged. |
} | ||
} | ||
|
||
CallbackFunction.prototype.type = "callback"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CallbackFunction.prototype.type = "callback"; | |
CallbackFunction.prototype.type = "callback function"; |
Change others as appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to match WebIDL2’s type
property: https://github.com/w3c/webidl2.js/blob/561efd0007038cc94ec2da8fcfa11623fe94101a/lib/productions/callback.js#L22-L24.
Co-authored-by: Timothy Gu <timothygu99@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stress-tested this against whatwg/streams#1035 and found some bugs with the promise conversion code.
|
||
return value; | ||
}, | ||
reason => reason |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is generated by:
Lines 330 to 349 in 7d45dfc
function generatePromise() { | |
let handler; | |
if (idlType.idlType[0].idlType === "void") { | |
// Do nothing. | |
handler = ""; | |
} else { | |
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName, | |
`${errPrefix} + " promise value"`); | |
requires.merge(conv.requires); | |
handler = ` | |
${conv.body} | |
return value; | |
`; | |
} | |
str += ` | |
${name} = Promise.resolve(${name}).then(value => { | |
${handler} | |
}, reason => reason); | |
`; | |
} |
Changing that is outside the scope of this PR.
try { | ||
callResult = Reflect.apply(value, thisArg, []); | ||
|
||
callResult = Promise.resolve(callResult).then( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't implement the semantics described in https://heycam.github.io/webidl/#es-invoking-callback-functions + https://heycam.github.io/webidl/#es-promise . For that the correct code would be something more like
return new Promise(resolve => resolve(callResult))
Note that Promise.resolve()
is inappropriate because it returns its argument in some cases, which is never true for converting to a Web IDL promise type. And note how converting to a Web IDL Promise<T>
type ignores the T
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is generated by:
Lines 330 to 349 in 7d45dfc
function generatePromise() { | |
let handler; | |
if (idlType.idlType[0].idlType === "void") { | |
// Do nothing. | |
handler = ""; | |
} else { | |
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName, | |
`${errPrefix} + " promise value"`); | |
requires.merge(conv.requires); | |
handler = ` | |
${conv.body} | |
return value; | |
`; | |
} | |
str += ` | |
${name} = Promise.resolve(${name}).then(value => { | |
${handler} | |
}, reason => reason); | |
`; | |
} |
Changing that is outside the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I'm not willing to merge this PR if it generates incorrect code that is unusable for consumers of webidl2js, so I guess fixing that is a dependency of this PR then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #219.
I'm unable to push commits to this branch again, despite it seemingly being a normal fork :(. I'll merge directly into master from my local copy. |
Merged as 4a6e558! |
This implements callback functions similarly to how callback interfaces were implemented in #172.
I would’ve preferred if this were included in WebIDL2JS v16.0.
Depends on:
Promise
type conversion #219Supersedes and closes #123
review?(@domenic, @TimothyGu, @Sebmaster)