From cfcf811103f6bf408dc67e43f38e461f625592b2 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Dartus Date: Thu, 28 Nov 2019 09:00:27 +0100 Subject: [PATCH] add import and drop ast in processors --- lib/constructs/attribute.js | 6 ++++-- lib/constructs/interface.js | 23 +++++++++++++++-------- lib/constructs/operation.js | 12 +++++++----- lib/context.js | 24 ++++++++++++++++++++++++ lib/utils.js | 9 +++++++-- test/__snapshots__/test.js.snap | 31 ++++++++++++++++--------------- test/test.js | 16 ++++++++++------ 7 files changed, 83 insertions(+), 38 deletions(-) diff --git a/lib/constructs/attribute.js b/lib/constructs/attribute.js index 33cebe29..3434dc75 100644 --- a/lib/constructs/attribute.js +++ b/lib/constructs/attribute.js @@ -64,8 +64,10 @@ class Attribute { } if (utils.hasCEReactions(this.idl)) { - getterBody = this.ctx.processCEReactions(this.idl, getterBody); - setterBody = this.ctx.processCEReactions(this.idl, setterBody); + const processorConfig = { requires }; + + getterBody = this.ctx.invokeProcessCEReactions(getterBody, processorConfig); + setterBody = this.ctx.invokeProcessCEReactions(setterBody, processorConfig); } addMethod(this.idl.name, [], ` diff --git a/lib/constructs/interface.js b/lib/constructs/interface.js index 3c4bc6c5..f37d9d5f 100644 --- a/lib/constructs/interface.js +++ b/lib/constructs/interface.js @@ -66,7 +66,6 @@ class Interface { this.ctx = ctx; this.idl = idl; this.name = idl.name; - this.htmlConstructor = Boolean(utils.getExtAttr(this.idl.extAttrs, "HTMLConstructor")); for (const member of this.idl.members) { member.definingInterface = this.name; @@ -657,7 +656,9 @@ class Interface { } if (utils.hasCEReactions(this.indexedSetter)) { - invocation = this.ctx.processCEReactions(this.indexedSetter, invocation); + invocation = this.ctx.invokeProcessCEReactions(invocation, { + requires: this.requires + }); } return str + invocation; @@ -694,7 +695,9 @@ class Interface { } if (utils.hasCEReactions(this.namedSetter)) { - invocation = this.ctx.processCEReactions(this.namedSetter, invocation); + invocation = this.ctx.invokeProcessCEReactions(invocation, { + requires: this.requires + }); } return str + invocation; @@ -1087,7 +1090,9 @@ class Interface { } if (utils.hasCEReactions(this.namedDeleter)) { - invocation = this.ctx.processCEReactions(this.namedDeleter, invocation); + invocation = this.ctx.invokeProcessCEReactions(invocation, { + requires: this.requires + }); } this.str += invocation; @@ -1205,6 +1210,12 @@ class Interface { `; } + if (utils.getExtAttr(this.idl.extAttrs, "HTMLConstructor")) { + body = this.ctx.invokeProcessHTMLConstructor(body, { + requires: this.requires + }); + } + this.addMethod("prototype", "constructor", argNames, body, "regular", { enumerable: false }); } @@ -1483,10 +1494,6 @@ class Interface { this.generateMixins(); this.generateRequires(); - - if (this.htmlConstructor) { - this.str = this.ctx.processHTMLConstructor(this.idl, this.str); - } } toString() { diff --git a/lib/constructs/operation.js b/lib/constructs/operation.js index e76e228c..69d0b6f1 100644 --- a/lib/constructs/operation.js +++ b/lib/constructs/operation.js @@ -79,21 +79,23 @@ class Operation { requires.merge(parameterConversions.requires); str += parameterConversions.body; - let incovation; + let invocation; if (overloads.every(overload => conversions[overload.operation.idlType.idlType])) { - incovation = ` + invocation = ` return ${callOn}.${implFunc}(${argsSpread}); `; } else { - incovation = ` + invocation = ` return utils.tryWrapperForImpl(${callOn}.${implFunc}(${argsSpread})); `; } if (utils.hasCEReactions(this.idls[0])) { - incovation = this.ctx.processCEReactions(this.idls[0], incovation); + invocation = this.ctx.invokeProcessCEReactions(invocation, { + requires + }); } - str += incovation; + str += invocation; if (this.static) { this.interface.addStaticMethod(this.name, argNames, str); diff --git a/lib/context.js b/lib/context.js index 3d7696dd..8b3d4f2a 100644 --- a/lib/context.js +++ b/lib/context.js @@ -56,6 +56,30 @@ class Context { } return undefined; } + + invokeProcessCEReactions(code, config) { + return this._invokeProcessor(this.processCEReactions, code, config); + } + + invokeProcessHTMLConstructor(code, config) { + return this._invokeProcessor(this.processHTMLConstructor, code, config); + } + + _invokeProcessor(processor, code, config) { + const { requires } = config; + + if (!requires) { + throw new TypeError("Internal error: missing requires object in context"); + } + + const context = { + addImport(source, imported) { + return requires.add(source, imported); + } + }; + + return processor.call(context, code); + } } module.exports = Context; diff --git a/lib/utils.js b/lib/utils.js index 7171c255..9dc98652 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -72,12 +72,17 @@ class RequiresMap extends Map { } add(type, func = "") { - const key = func + type; - let req = `require("./${type}.js")`; + const key = (func + type).replace(/[./-]/g, "_"); + + const path = type.startsWith(".") ? type : `./${type}`; + let req = `require("${path}.js")`; + if (func) { req += `.${func}`; } + this.addRaw(key, req); + return key; } addRaw(key, expr) { diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index e34f7fa4..f1d5e00e 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -6,6 +6,8 @@ exports[`CEReactions.webidl 1`] = ` const conversions = require(\\"webidl-conversions\\"); const utils = require(\\"./utils.js\\"); +const preSteps___CEReactions = require(\\"../CEReactions.js\\").preSteps; +const postSteps___CEReactions = require(\\"../CEReactions.js\\").postSteps; const impl = utils.implSymbol; const ctorRegistry = utils.ctorRegistrySymbol; @@ -162,7 +164,7 @@ const iface = { context: \\"Failed to set the '\\" + P + \\"' property on 'CEReactions': The provided value\\" }); - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { const creating = !target[impl][utils.supportsPropertyName](P); if (creating) { @@ -171,7 +173,7 @@ const iface = { target[impl][utils.namedSetExisting](P, namedValue); } } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } return true; @@ -226,7 +228,7 @@ const iface = { context: \\"Failed to set the '\\" + P + \\"' property on 'CEReactions': The provided value\\" }); - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { const creating = !target[impl][utils.supportsPropertyName](P); if (creating) { @@ -235,7 +237,7 @@ const iface = { target[impl][utils.namedSetExisting](P, namedValue); } } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } return true; @@ -249,12 +251,12 @@ const iface = { } if (target[impl][utils.supportsPropertyName](P) && !(P in target)) { - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { target[impl][utils.namedDelete](P); return true; } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } } @@ -284,11 +286,11 @@ const iface = { throw new TypeError(\\"Illegal invocation\\"); } - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { return this[impl].method(); } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } } @@ -297,11 +299,11 @@ const iface = { throw new TypeError(\\"Illegal invocation\\"); } - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { return this[impl][\\"attr\\"]; } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } } @@ -314,11 +316,11 @@ const iface = { context: \\"Failed to set the 'attr' property on 'CEReactions': The provided value\\" }); - // CEReactions pre steps + preSteps___CEReactions(globalObject); try { this[impl][\\"attr\\"] = V; } finally { - // CEReactions post steps + postSteps___CEReactions(globalObject); } } } @@ -1140,8 +1142,7 @@ exports[`HTMLConstructor.webidl 1`] = ` const conversions = require(\\"webidl-conversions\\"); const utils = require(\\"./utils.js\\"); -// HTMLConstructor - +const HTMLConstructor___HTMLConstructor = require(\\"../HTMLConstructor.js\\").HTMLConstructor; const impl = utils.implSymbol; const ctorRegistry = utils.ctorRegistrySymbol; @@ -1223,7 +1224,7 @@ const iface = { install(globalObject) { class HTMLConstructor { constructor() { - throw new TypeError(\\"Illegal constructor\\"); + return HTMLConstructor___HTMLConstructor.HTMLConstructor(globalObject); } } Object.defineProperties(HTMLConstructor.prototype, { diff --git a/test/test.js b/test/test.js index 3876fd82..3c6bd069 100644 --- a/test/test.js +++ b/test/test.js @@ -13,20 +13,24 @@ const outputDir = path.resolve(__dirname, "output"); beforeAll(() => { const transformer = new Transformer({ - processCEReactions(_, code) { + processCEReactions(code) { + const preSteps = this.addImport("../CEReactions", "preSteps"); + const postSteps = this.addImport("../CEReactions", "postSteps"); + return ` - // CEReactions pre steps + ${preSteps}(globalObject); try { ${code} } finally { - // CEReactions post steps + ${postSteps}(globalObject); } `; }, - processHTMLConstructor(_, code) { + processHTMLConstructor() { + const identifier = this.addImport("../HTMLConstructor", "HTMLConstructor"); + return ` - // HTMLConstructor - ${code} + return ${identifier}.HTMLConstructor(globalObject); `; } });