-
-
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 callback interfaces #178
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d673c4
feat(constructs): Add support for callback interfaces
ExE-Boss 48d968f
fix(callback‑interface): Generate `requires` block
ExE-Boss 2b1f360
fix(callback‑interface): Expose wrapped value
ExE-Boss 2f01f5a
fix(callback‑interface): Address review comments
ExE-Boss 9cd8797
fix(callback‑interface): Update README and remove non‑standard cache
ExE-Boss a8d0154
Nits, mostly formatting
domenic 28543f0
Small tweaks to generated code
domenic 38e0248
Avoid loop in generated code, loop in source instead
domenic 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
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,233 @@ | ||
"use strict"; | ||
|
||
const utils = require("../utils.js"); | ||
const Types = require("../types.js"); | ||
const Constant = require("./constant.js"); | ||
|
||
class CallbackInterface { | ||
constructor(ctx, idl) { | ||
this.ctx = ctx; | ||
this.idl = idl; | ||
this.name = idl.name; | ||
this.str = null; | ||
|
||
this.requires = new utils.RequiresMap(ctx); | ||
|
||
this.operation = null; | ||
this.constants = new Map(); | ||
|
||
this._analyzed = false; | ||
this._outputStaticProperties = new Map(); | ||
} | ||
|
||
_analyzeMembers() { | ||
for (const member of this.idl.members) { | ||
switch (member.type) { | ||
case "operation": | ||
if (this.operation !== null) { | ||
throw new Error( | ||
`Callback interface ${this.name} has more than one operation` | ||
); | ||
} | ||
this.operation = member; | ||
break; | ||
case "const": | ||
this.constants.set(member.name, new Constant(this.ctx, this, member)); | ||
break; | ||
default: | ||
if (!this.ctx.options.suppressErrors) { | ||
throw new Error( | ||
`Unknown IDL member type "${member.type}" in callback interface ${this.name}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (this.operation === null) { | ||
throw new Error(`Callback interface ${this.name} has no operation`); | ||
} | ||
} | ||
|
||
addAllProperties() { | ||
for (const member of this.constants.values()) { | ||
const data = member.generate(); | ||
this.requires.merge(data.requires); | ||
} | ||
} | ||
|
||
addStaticProperty(name, body, { configurable = true, enumerable = typeof name === "string", writable = true } = {}) { | ||
const descriptor = { configurable, enumerable, writable }; | ||
this._outputStaticProperties.set(name, { body, descriptor }); | ||
} | ||
|
||
addProperty() {} | ||
|
||
generateConversion() { | ||
const { operation, name } = this; | ||
const opName = operation.name; | ||
|
||
const isAsyncInUnion = operation.idlType.union && | ||
operation.idlType.idlType.some(idlType => idlType.generic === "Promise"); | ||
const isAsync = isAsyncInUnion || operation.idlType.generic === "Promise"; | ||
|
||
const argNames = operation.arguments.map(arg => arg.name); | ||
if (operation.arguments.some(arg => arg.optional || arg.variadic)) { | ||
throw new Error("Internal error: optional/variadic arguments are not implemented for callback interfaces"); | ||
} | ||
|
||
this.str += ` | ||
exports.convert = function convert(value, { context = "The provided value" } = {}) { | ||
if (!utils.isObject(value)) { | ||
throw new TypeError(\`\${context} is not an object.\`); | ||
} | ||
|
||
function callTheUserObjectsOperation(${argNames.join(", ")}) { | ||
let thisArg = this; | ||
let O = value; | ||
let X = O; | ||
`; | ||
|
||
if (isAsync) { | ||
this.str += ` | ||
try { | ||
`; | ||
} | ||
|
||
this.str += ` | ||
if (typeof O !== "function") { | ||
X = O[${utils.stringifyPropertyName(opName)}]; | ||
if (typeof X !== "function") { | ||
throw new TypeError(\`\${context} does not correctly implement ${name}.\`) | ||
} | ||
thisArg = O; | ||
} | ||
`; | ||
|
||
// We don't implement all of https://heycam.github.io/webidl/#web-idl-arguments-list-converting since the callers | ||
// are assumed to always pass the correct number of arguments and we don't support optional/variadic arguments. | ||
for (const argName of argNames) { | ||
this.str += ` | ||
${argName} = utils.tryWrapperForImpl(${argName}); | ||
`; | ||
} | ||
|
||
const argsToReflectCall = argNames.length > 0 ? `, ${argNames.join(", ")}` : ""; | ||
this.str += ` | ||
let callResult = Reflect.call(X, thisArg${argsToReflectCall}); | ||
`; | ||
|
||
if (operation.idlType.idlType !== "void") { | ||
const conv = Types.generateTypeConversion(this.ctx, "callResult", operation.idlType, [], name, "context"); | ||
this.requires.merge(conv.requires); | ||
this.str += ` | ||
${conv.body} | ||
return callResult; | ||
`; | ||
} | ||
|
||
if (isAsync) { | ||
this.str += ` | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
`; | ||
} | ||
|
||
this.str += ` | ||
}; | ||
`; | ||
|
||
// The wrapperSymbol ensures that if the callback interface is used as a return value, e.g. in NodeIterator's filter | ||
// attribute, 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-interface. | ||
// | ||
// The objectReference is used to implement spec text such as that discussed in | ||
// https://github.com/whatwg/dom/issues/842. | ||
this.str += ` | ||
callTheUserObjectsOperation[utils.wrapperSymbol] = value; | ||
callTheUserObjectsOperation.objectReference = value; | ||
|
||
return callTheUserObjectsOperation; | ||
}; | ||
`; | ||
} | ||
|
||
generateOffInstanceAfterClass() { | ||
const classProps = new Map(); | ||
|
||
for (const [name, { body, descriptor }] of this._outputStaticProperties) { | ||
const descriptorModifier = utils.getPropertyDescriptorModifier( | ||
utils.defaultDefinePropertyDescriptor, | ||
descriptor, | ||
"regular", | ||
body | ||
); | ||
classProps.set(utils.stringifyPropertyKey(name), descriptorModifier); | ||
} | ||
|
||
if (classProps.size > 0) { | ||
const props = [...classProps].map(([name, body]) => `${name}: ${body}`); | ||
this.str += ` | ||
Object.defineProperties(${this.name}, { ${props.join(", ")} }); | ||
`; | ||
} | ||
} | ||
|
||
generateInstall() { | ||
this.str += ` | ||
exports.install = function install(globalObject) { | ||
`; | ||
|
||
if (this.constants.size > 0) { | ||
const { name } = this; | ||
|
||
this.str += ` | ||
const ${name} = () => { | ||
throw new TypeError("Illegal invocation"); | ||
}; | ||
`; | ||
|
||
this.generateOffInstanceAfterClass(); | ||
|
||
this.str += ` | ||
Object.defineProperty(globalObject, ${JSON.stringify(name)}, { | ||
configurable: true, | ||
writable: true, | ||
value: ${name} | ||
}); | ||
`; | ||
} | ||
|
||
this.str += ` | ||
}; | ||
`; | ||
} | ||
|
||
generateRequires() { | ||
this.str = ` | ||
${this.requires.generate()} | ||
|
||
${this.str} | ||
`; | ||
} | ||
|
||
generate() { | ||
this.generateConversion(); | ||
this.generateInstall(); | ||
|
||
this.generateRequires(); | ||
} | ||
|
||
toString() { | ||
this.str = ""; | ||
if (!this._analyzed) { | ||
this._analyzed = true; | ||
this._analyzeMembers(); | ||
} | ||
this.addAllProperties(); | ||
this.generate(); | ||
return this.str; | ||
} | ||
} | ||
|
||
module.exports = CallbackInterface; |
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.
There is no such thing as
Reflect.call(…)
, so this line will throw aTypeError
.I’ve fixed that in #172.