|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const conversions = require("webidl-conversions"); |
| 4 | + |
| 5 | +const utils = require("../utils.js"); |
| 6 | +const Types = require("../types.js"); |
| 7 | +const Constant = require("./constant.js"); |
| 8 | + |
| 9 | +class CallbackInterface { |
| 10 | + constructor(ctx, idl) { |
| 11 | + this.ctx = ctx; |
| 12 | + this.idl = idl; |
| 13 | + this.name = idl.name; |
| 14 | + this.str = null; |
| 15 | + |
| 16 | + this.requires = new utils.RequiresMap(ctx); |
| 17 | + |
| 18 | + this.operation = null; |
| 19 | + this.constants = new Map(); |
| 20 | + |
| 21 | + this._analyzed = false; |
| 22 | + this._outputStaticProperties = new Map(); |
| 23 | + } |
| 24 | + |
| 25 | + _analyzeMembers() { |
| 26 | + for (const member of this.idl.members) { |
| 27 | + switch (member.type) { |
| 28 | + case "operation": |
| 29 | + if (this.operation !== null) { |
| 30 | + throw new Error( |
| 31 | + `Callback interface ${this.name} has more than one operation` |
| 32 | + ); |
| 33 | + } |
| 34 | + this.operation = member; |
| 35 | + break; |
| 36 | + case "const": |
| 37 | + this.constants.set(member.name, new Constant(this.ctx, this, member)); |
| 38 | + break; |
| 39 | + default: |
| 40 | + throw new Error( |
| 41 | + `Illegal IDL member type "${member.type}" in callback interface ${this.name}` |
| 42 | + ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (this.operation === null) { |
| 47 | + throw new Error(`Callback interface ${this.name} has no operation`); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + addAllProperties() { |
| 52 | + for (const member of this.constants.values()) { |
| 53 | + const data = member.generate(); |
| 54 | + this.requires.merge(data.requires); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + addStaticProperty(name, body, { configurable = true, enumerable = typeof name === "string", writable = true } = {}) { |
| 59 | + const descriptor = { configurable, enumerable, writable }; |
| 60 | + this._outputStaticProperties.set(name, { body, descriptor }); |
| 61 | + } |
| 62 | + |
| 63 | + // This is necessary due to usage in the `Constant` and other classes |
| 64 | + // It's empty because callback interfaces don't generate platform objects |
| 65 | + addProperty() {} |
| 66 | + |
| 67 | + generateConversion() { |
| 68 | + const { operation, name } = this; |
| 69 | + const opName = operation.name; |
| 70 | + const isAsync = operation.idlType.generic === "Promise"; |
| 71 | + |
| 72 | + const argNames = operation.arguments.map(arg => arg.name); |
| 73 | + if (operation.arguments.some(arg => arg.optional || arg.variadic)) { |
| 74 | + throw new Error("Internal error: optional/variadic arguments are not implemented for callback interfaces"); |
| 75 | + } |
| 76 | + |
| 77 | + this.str += ` |
| 78 | + exports.convert = function convert(value, { context = "The provided value" } = {}) { |
| 79 | + if (!utils.isObject(value)) { |
| 80 | + throw new TypeError(\`\${context} is not an object.\`); |
| 81 | + } |
| 82 | +
|
| 83 | + function callTheUserObjectsOperation(${argNames.join(", ")}) { |
| 84 | + let thisArg = this; |
| 85 | + let O = value; |
| 86 | + let X = O; |
| 87 | + `; |
| 88 | + |
| 89 | + if (isAsync) { |
| 90 | + this.str += ` |
| 91 | + try { |
| 92 | + `; |
| 93 | + } |
| 94 | + |
| 95 | + this.str += ` |
| 96 | + if (typeof O !== "function") { |
| 97 | + X = O[${utils.stringifyPropertyName(opName)}]; |
| 98 | + if (typeof X !== "function") { |
| 99 | + throw new TypeError(\`\${context} does not correctly implement ${name}.\`) |
| 100 | + } |
| 101 | + thisArg = O; |
| 102 | + } |
| 103 | + `; |
| 104 | + |
| 105 | + // We don't implement all of https://heycam.github.io/webidl/#web-idl-arguments-list-converting since the callers |
| 106 | + // are assumed to always pass the correct number of arguments and we don't support optional/variadic arguments. |
| 107 | + // See also: https://github.com/jsdom/webidl2js/issues/71 |
| 108 | + for (const arg of operation.arguments) { |
| 109 | + const argName = arg.name; |
| 110 | + if (arg.idlType.union ? |
| 111 | + arg.idlType.idlType.some(type => !conversions[type]) : |
| 112 | + !conversions[arg.idlType.idlType]) { |
| 113 | + this.str += ` |
| 114 | + ${argName} = utils.tryWrapperForImpl(${argName}); |
| 115 | + `; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + this.str += ` |
| 120 | + let callResult = Reflect.apply(X, thisArg, [${argNames.join(", ")}]); |
| 121 | + `; |
| 122 | + |
| 123 | + if (operation.idlType.idlType !== "void") { |
| 124 | + const conv = Types.generateTypeConversion(this.ctx, "callResult", operation.idlType, [], name, "context"); |
| 125 | + this.requires.merge(conv.requires); |
| 126 | + this.str += ` |
| 127 | + ${conv.body} |
| 128 | + return callResult; |
| 129 | + `; |
| 130 | + } |
| 131 | + |
| 132 | + if (isAsync) { |
| 133 | + this.str += ` |
| 134 | + } catch (err) { |
| 135 | + return Promise.reject(err); |
| 136 | + } |
| 137 | + `; |
| 138 | + } |
| 139 | + |
| 140 | + this.str += ` |
| 141 | + }; |
| 142 | + `; |
| 143 | + |
| 144 | + // The wrapperSymbol ensures that if the callback interface is used as a return value, e.g. in NodeIterator's filter |
| 145 | + // attribute, that it exposes the original callback back. I.e. it implements the conversion from IDL to JS value in |
| 146 | + // https://heycam.github.io/webidl/#es-callback-interface. |
| 147 | + // |
| 148 | + // The objectReference is used to implement spec text such as that discussed in |
| 149 | + // https://github.com/whatwg/dom/issues/842. |
| 150 | + this.str += ` |
| 151 | + callTheUserObjectsOperation[utils.wrapperSymbol] = value; |
| 152 | + callTheUserObjectsOperation.objectReference = value; |
| 153 | +
|
| 154 | + return callTheUserObjectsOperation; |
| 155 | + }; |
| 156 | + `; |
| 157 | + } |
| 158 | + |
| 159 | + generateOffInstanceAfterClass() { |
| 160 | + const classProps = new Map(); |
| 161 | + |
| 162 | + for (const [name, { body, descriptor }] of this._outputStaticProperties) { |
| 163 | + const descriptorModifier = utils.getPropertyDescriptorModifier( |
| 164 | + utils.defaultDefinePropertyDescriptor, |
| 165 | + descriptor, |
| 166 | + "regular", |
| 167 | + body |
| 168 | + ); |
| 169 | + classProps.set(utils.stringifyPropertyKey(name), descriptorModifier); |
| 170 | + } |
| 171 | + |
| 172 | + if (classProps.size > 0) { |
| 173 | + const props = [...classProps].map(([name, body]) => `${name}: ${body}`); |
| 174 | + this.str += ` |
| 175 | + Object.defineProperties(${this.name}, { ${props.join(", ")} }); |
| 176 | + `; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + generateInstall() { |
| 181 | + this.str += ` |
| 182 | + exports.install = function install(globalObject) { |
| 183 | + `; |
| 184 | + |
| 185 | + if (this.constants.size > 0) { |
| 186 | + const { name } = this; |
| 187 | + |
| 188 | + this.str += ` |
| 189 | + const ${name} = () => { |
| 190 | + throw new TypeError("Illegal invocation"); |
| 191 | + }; |
| 192 | + `; |
| 193 | + |
| 194 | + this.generateOffInstanceAfterClass(); |
| 195 | + |
| 196 | + this.str += ` |
| 197 | + Object.defineProperty(globalObject, ${JSON.stringify(name)}, { |
| 198 | + configurable: true, |
| 199 | + writable: true, |
| 200 | + value: ${name} |
| 201 | + }); |
| 202 | + `; |
| 203 | + } |
| 204 | + |
| 205 | + this.str += ` |
| 206 | + }; |
| 207 | + `; |
| 208 | + } |
| 209 | + |
| 210 | + generateRequires() { |
| 211 | + this.str = ` |
| 212 | + ${this.requires.generate()} |
| 213 | +
|
| 214 | + ${this.str} |
| 215 | + `; |
| 216 | + } |
| 217 | + |
| 218 | + generate() { |
| 219 | + this.generateConversion(); |
| 220 | + this.generateInstall(); |
| 221 | + |
| 222 | + this.generateRequires(); |
| 223 | + } |
| 224 | + |
| 225 | + toString() { |
| 226 | + this.str = ""; |
| 227 | + if (!this._analyzed) { |
| 228 | + this._analyzed = true; |
| 229 | + this._analyzeMembers(); |
| 230 | + } |
| 231 | + this.addAllProperties(); |
| 232 | + this.generate(); |
| 233 | + return this.str; |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +module.exports = CallbackInterface; |
0 commit comments