-
-
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
Closed
ExE-Boss
wants to merge
12
commits into
jsdom:master
from
ExE-Boss:feat/constructs/callback-function
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6d46828
Implement support for callback functions
ExE-Boss 46226f4
Avoid loop in generated code where possible
ExE-Boss 513cc89
Merge branch 'master' into feat/constructs/callback-function
ExE-Boss 989760c
Update tests
ExE-Boss e8d7a27
Rename `[TreatNonObjectAsNull]` to `[LegacyTreatNonObjectAsNull]`
ExE-Boss 62e9994
Fix nullable [LegacyTreatNonObjectAsNull] callback type conversion
ExE-Boss 28683ce
Merge branch 'master' into feat/constructs/callback-function
ExE-Boss 6fc5107
Update README.md
ExE-Boss fa4c9b1
Address review comments
ExE-Boss 4e93f13
Use `Function.convert(…)` in key+value `forEach(…)` implementation
ExE-Boss 61dcfd1
Throw when calling `new invokeTheCallbackFunction(…)`
ExE-Boss 7f28137
Merge branch 'master' into feat/constructs/callback-function
ExE-Boss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
"use strict"; | ||
|
||
const utils = require("../utils.js"); | ||
const Types = require("../types.js"); | ||
|
||
class CallbackFunction { | ||
/** | ||
* @param {import("../context.js")} ctx | ||
* @param {import("webidl2").CallbackType} idl | ||
*/ | ||
constructor(ctx, idl) { | ||
this.ctx = ctx; | ||
this.idl = idl; | ||
this.name = idl.name; | ||
this.str = null; | ||
|
||
this.requires = new utils.RequiresMap(ctx); | ||
} | ||
|
||
generateConversion() { | ||
const { idl } = this; | ||
const isAsync = idl.idlType.generic === "Promise"; | ||
|
||
const treatNonObjectAsNull = Boolean(utils.getExtAttr(idl.extAttrs, "TreatNonObjectAsNull")); | ||
const assertCallable = treatNonObjectAsNull ? "" : ` | ||
if (typeof value !== "function") { | ||
throw new TypeError(context + " is not a function"); | ||
} | ||
`; | ||
|
||
let returnIDL = ""; | ||
if (idl.idlType.idlType !== "void") { | ||
const conv = Types.generateTypeConversion(this.ctx, "callResult", idl.idlType, [], this.name, "context"); | ||
this.requires.merge(conv.requires); | ||
returnIDL = ` | ||
${conv.body} | ||
return callResult; | ||
`; | ||
} | ||
|
||
// This is a simplification of https://heycam.github.io/webidl/#web-idl-arguments-list-converting that currently | ||
// fits our needs. | ||
const maxArgs = idl.arguments.some(arg => arg.variadic) ? Infinity : idl.arguments.length; | ||
let minArgs = 0; | ||
for (const arg of idl.arguments) { | ||
if (arg.optional || arg.variadic) { | ||
break; | ||
} | ||
|
||
minArgs++; | ||
} | ||
|
||
let argsToES = ""; | ||
if (maxArgs > 0) { | ||
argsToES += ` | ||
for (let i = 0; i < ${Number.isFinite(maxArgs) ? `Math.min(args.length, ${maxArgs})` : "args.length"}; i++) { | ||
args[i] = utils.tryWrapperForImpl(args[i]); | ||
} | ||
`; | ||
|
||
if (minArgs > 0) { | ||
argsToES += ` | ||
if (args.length < ${minArgs}) { | ||
for (let i = args.length; i < ${minArgs}; i++) { | ||
args[i] = undefined; | ||
} | ||
} | ||
`; | ||
} | ||
|
||
if (Number.isFinite(maxArgs)) { | ||
argsToES += ` | ||
${minArgs > 0 ? "else" : ""} if (args.length > ${maxArgs}) { | ||
args.length = ${maxArgs}; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
this.str += ` | ||
exports.convert = (value, { context = "The provided value" } = {}) => { | ||
${assertCallable} | ||
function invokeTheCallbackFunction(${maxArgs > 0 ? "...args" : ""}) { | ||
const thisArg = utils.tryWrapperForImpl(this); | ||
let callResult; | ||
`; | ||
|
||
if (isAsync) { | ||
this.str += ` | ||
try { | ||
`; | ||
} | ||
|
||
if (treatNonObjectAsNull) { | ||
this.str += ` | ||
if (typeof value === "function") { | ||
`; | ||
} | ||
|
||
this.str += ` | ||
${argsToES} | ||
callResult = Reflect.apply(value, thisArg, ${maxArgs > 0 ? "args" : "[]"}); | ||
`; | ||
|
||
if (treatNonObjectAsNull) { | ||
this.str += "}"; | ||
} | ||
|
||
this.str += ` | ||
${returnIDL} | ||
`; | ||
|
||
if (isAsync) { | ||
this.str += ` | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
`; | ||
} | ||
|
||
this.str += ` | ||
}; | ||
`; | ||
|
||
// `[TreatNonObjctAsNull]` and `isAsync` don't apply to | ||
// https://heycam.github.io/webidl/#construct-a-callback-function. | ||
this.str += ` | ||
invokeTheCallbackFunction.construct = (${maxArgs > 0 ? "...args" : ""}) => { | ||
${argsToES} | ||
let callResult = Reflect.construct(value, ${maxArgs > 0 ? "args" : "[]"}); | ||
${returnIDL} | ||
}; | ||
`; | ||
|
||
// The wrapperSymbol ensures that if the callback function is used as a return value, that it exposes | ||
// the original callback back. I.e. it implements the conversion from IDL to JS value in | ||
// https://heycam.github.io/webidl/#es-callback-function. | ||
// | ||
// The objectReference is used to implement spec text such as that discussed in | ||
// https://github.com/whatwg/dom/issues/842. | ||
this.str += ` | ||
invokeTheCallbackFunction[utils.wrapperSymbol] = value; | ||
invokeTheCallbackFunction.objectReference = value; | ||
|
||
return invokeTheCallbackFunction; | ||
}; | ||
`; | ||
} | ||
|
||
generateRequires() { | ||
this.str = ` | ||
${this.requires.generate()} | ||
|
||
${this.str} | ||
`; | ||
} | ||
|
||
generate() { | ||
this.generateConversion(); | ||
|
||
this.generateRequires(); | ||
} | ||
|
||
toString() { | ||
this.str = ""; | ||
this.generate(); | ||
return this.str; | ||
} | ||
} | ||
|
||
CallbackFunction.prototype.type = "callback"; | ||
|
||
module.exports = CallbackFunction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.