Skip to content

Commit

Permalink
add import and drop ast in processors
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdartus committed Nov 28, 2019
1 parent 9c62d29 commit cfcf811
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 38 deletions.
6 changes: 4 additions & 2 deletions lib/constructs/attribute.js
Expand Up @@ -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, [], `
Expand Down
23 changes: 15 additions & 8 deletions lib/constructs/interface.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
}

Expand Down Expand Up @@ -1483,10 +1494,6 @@ class Interface {

this.generateMixins();
this.generateRequires();

if (this.htmlConstructor) {
this.str = this.ctx.processHTMLConstructor(this.idl, this.str);
}
}

toString() {
Expand Down
12 changes: 7 additions & 5 deletions lib/constructs/operation.js
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions lib/context.js
Expand Up @@ -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;
9 changes: 7 additions & 2 deletions lib/utils.js
Expand Up @@ -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) {
Expand Down
31 changes: 16 additions & 15 deletions test/__snapshots__/test.js.snap
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -171,7 +173,7 @@ const iface = {
target[impl][utils.namedSetExisting](P, namedValue);
}
} finally {
// CEReactions post steps
postSteps___CEReactions(globalObject);
}
return true;
Expand Down Expand Up @@ -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) {
Expand All @@ -235,7 +237,7 @@ const iface = {
target[impl][utils.namedSetExisting](P, namedValue);
}
} finally {
// CEReactions post steps
postSteps___CEReactions(globalObject);
}
return true;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, {
Expand Down
16 changes: 10 additions & 6 deletions test/test.js
Expand Up @@ -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);
`;
}
});
Expand Down

0 comments on commit cfcf811

Please sign in to comment.