|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const conversions = require("webidl-conversions"); |
| 4 | + |
| 5 | +const utils = require("../utils.js"); |
| 6 | +const Types = require("../types.js"); |
| 7 | + |
| 8 | +class CallbackFunction { |
| 9 | + constructor(ctx, idl) { |
| 10 | + this.ctx = ctx; |
| 11 | + this.idl = idl; |
| 12 | + this.name = idl.name; |
| 13 | + this.str = null; |
| 14 | + |
| 15 | + this.requires = new utils.RequiresMap(ctx); |
| 16 | + |
| 17 | + this.legacyTreatNonObjectAsNull = Boolean(utils.getExtAttr(idl.extAttrs, "LegacyTreatNonObjectAsNull")); |
| 18 | + } |
| 19 | + |
| 20 | + generateConversion() { |
| 21 | + const { idl, legacyTreatNonObjectAsNull } = this; |
| 22 | + const isAsync = idl.idlType.generic === "Promise"; |
| 23 | + |
| 24 | + const assertCallable = legacyTreatNonObjectAsNull ? "" : ` |
| 25 | + if (typeof value !== "function") { |
| 26 | + throw new TypeError(context + " is not a function"); |
| 27 | + } |
| 28 | + `; |
| 29 | + |
| 30 | + let returnIDL = ""; |
| 31 | + if (idl.idlType.idlType !== "void") { |
| 32 | + const conv = Types.generateTypeConversion(this.ctx, "callResult", idl.idlType, [], this.name, "context"); |
| 33 | + this.requires.merge(conv.requires); |
| 34 | + returnIDL = ` |
| 35 | + ${conv.body} |
| 36 | + return callResult; |
| 37 | + `; |
| 38 | + } |
| 39 | + |
| 40 | + // This is a simplification of https://heycam.github.io/webidl/#web-idl-arguments-list-converting that currently |
| 41 | + // fits our needs. |
| 42 | + let argsToES = ""; |
| 43 | + let inputArgs = ""; |
| 44 | + let applyArgs = "[]"; |
| 45 | + |
| 46 | + if (idl.arguments.length > 0) { |
| 47 | + if (idl.arguments.every(arg => !arg.optional && !arg.variadic)) { |
| 48 | + const argNames = idl.arguments.map(arg => arg.name); |
| 49 | + inputArgs = argNames.join(", "); |
| 50 | + applyArgs = `[${inputArgs}]`; |
| 51 | + |
| 52 | + for (const arg of idl.arguments) { |
| 53 | + const argName = arg.name; |
| 54 | + if (arg.idlType.union ? |
| 55 | + arg.idlType.idlType.some(type => !conversions[type.idlType]) : |
| 56 | + !conversions[arg.idlType.idlType]) { |
| 57 | + argsToES += ` |
| 58 | + ${argName} = utils.tryWrapperForImpl(${argName}); |
| 59 | + `; |
| 60 | + } |
| 61 | + } |
| 62 | + } else { |
| 63 | + const maxArgs = idl.arguments.some(arg => arg.variadic) ? Infinity : idl.arguments.length; |
| 64 | + let minArgs = 0; |
| 65 | + |
| 66 | + for (const arg of idl.arguments) { |
| 67 | + if (arg.optional || arg.variadic) { |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + minArgs++; |
| 72 | + } |
| 73 | + |
| 74 | + if (maxArgs > 0) { |
| 75 | + inputArgs = "...args"; |
| 76 | + applyArgs = "args"; |
| 77 | + |
| 78 | + const maxArgsLoop = Number.isFinite(maxArgs) ? |
| 79 | + `Math.min(args.length, ${maxArgs})` : |
| 80 | + "args.length"; |
| 81 | + |
| 82 | + argsToES += ` |
| 83 | + for (let i = 0; i < ${maxArgsLoop}; i++) { |
| 84 | + args[i] = utils.tryWrapperForImpl(args[i]); |
| 85 | + } |
| 86 | + `; |
| 87 | + |
| 88 | + if (minArgs > 0) { |
| 89 | + argsToES += ` |
| 90 | + if (args.length < ${minArgs}) { |
| 91 | + for (let i = args.length; i < ${minArgs}; i++) { |
| 92 | + args[i] = undefined; |
| 93 | + } |
| 94 | + } |
| 95 | + `; |
| 96 | + } |
| 97 | + |
| 98 | + if (Number.isFinite(maxArgs)) { |
| 99 | + argsToES += ` |
| 100 | + ${minArgs > 0 ? "else" : ""} if (args.length > ${maxArgs}) { |
| 101 | + args.length = ${maxArgs}; |
| 102 | + } |
| 103 | + `; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + this.str += ` |
| 110 | + exports.convert = (value, { context = "The provided value" } = {}) => { |
| 111 | + ${assertCallable} |
| 112 | + function invokeTheCallbackFunction(${inputArgs}) { |
| 113 | + if (new.target !== undefined) { |
| 114 | + throw new Error("Internal error: invokeTheCallbackFunction is not a constructor"); |
| 115 | + } |
| 116 | +
|
| 117 | + const thisArg = utils.tryWrapperForImpl(this); |
| 118 | + let callResult; |
| 119 | + `; |
| 120 | + |
| 121 | + if (isAsync) { |
| 122 | + this.str += ` |
| 123 | + try { |
| 124 | + `; |
| 125 | + } |
| 126 | + |
| 127 | + if (legacyTreatNonObjectAsNull) { |
| 128 | + this.str += ` |
| 129 | + if (typeof value === "function") { |
| 130 | + `; |
| 131 | + } |
| 132 | + |
| 133 | + this.str += ` |
| 134 | + ${argsToES} |
| 135 | + callResult = Reflect.apply(value, thisArg, ${applyArgs}); |
| 136 | + `; |
| 137 | + |
| 138 | + if (legacyTreatNonObjectAsNull) { |
| 139 | + this.str += "}"; |
| 140 | + } |
| 141 | + |
| 142 | + this.str += ` |
| 143 | + ${returnIDL} |
| 144 | + `; |
| 145 | + |
| 146 | + if (isAsync) { |
| 147 | + this.str += ` |
| 148 | + } catch (err) { |
| 149 | + return Promise.reject(err); |
| 150 | + } |
| 151 | + `; |
| 152 | + } |
| 153 | + |
| 154 | + this.str += ` |
| 155 | + }; |
| 156 | + `; |
| 157 | + |
| 158 | + // `[TreatNonObjctAsNull]` and `isAsync` don't apply to |
| 159 | + // https://heycam.github.io/webidl/#construct-a-callback-function. |
| 160 | + this.str += ` |
| 161 | + invokeTheCallbackFunction.construct = (${inputArgs}) => { |
| 162 | + ${argsToES} |
| 163 | + let callResult = Reflect.construct(value, ${applyArgs}); |
| 164 | + ${returnIDL} |
| 165 | + }; |
| 166 | + `; |
| 167 | + |
| 168 | + // The wrapperSymbol ensures that if the callback function is used as a return value, that it exposes |
| 169 | + // the original callback back. I.e. it implements the conversion from IDL to JS value in |
| 170 | + // https://heycam.github.io/webidl/#es-callback-function. |
| 171 | + // |
| 172 | + // The objectReference is used to implement spec text such as that discussed in |
| 173 | + // https://github.com/whatwg/dom/issues/842. |
| 174 | + this.str += ` |
| 175 | + invokeTheCallbackFunction[utils.wrapperSymbol] = value; |
| 176 | + invokeTheCallbackFunction.objectReference = value; |
| 177 | +
|
| 178 | + return invokeTheCallbackFunction; |
| 179 | + }; |
| 180 | + `; |
| 181 | + } |
| 182 | + |
| 183 | + generateRequires() { |
| 184 | + this.str = ` |
| 185 | + ${this.requires.generate()} |
| 186 | +
|
| 187 | + ${this.str} |
| 188 | + `; |
| 189 | + } |
| 190 | + |
| 191 | + generate() { |
| 192 | + this.generateConversion(); |
| 193 | + |
| 194 | + this.generateRequires(); |
| 195 | + } |
| 196 | + |
| 197 | + toString() { |
| 198 | + this.str = ""; |
| 199 | + this.generate(); |
| 200 | + return this.str; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +CallbackFunction.prototype.type = "callback"; |
| 205 | + |
| 206 | +module.exports = CallbackFunction; |
0 commit comments