Skip to content

Commit cfcf811

Browse files
committed
add import and drop ast in processors
1 parent 9c62d29 commit cfcf811

File tree

7 files changed

+83
-38
lines changed

7 files changed

+83
-38
lines changed

lib/constructs/attribute.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ class Attribute {
6464
}
6565

6666
if (utils.hasCEReactions(this.idl)) {
67-
getterBody = this.ctx.processCEReactions(this.idl, getterBody);
68-
setterBody = this.ctx.processCEReactions(this.idl, setterBody);
67+
const processorConfig = { requires };
68+
69+
getterBody = this.ctx.invokeProcessCEReactions(getterBody, processorConfig);
70+
setterBody = this.ctx.invokeProcessCEReactions(setterBody, processorConfig);
6971
}
7072

7173
addMethod(this.idl.name, [], `

lib/constructs/interface.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class Interface {
6666
this.ctx = ctx;
6767
this.idl = idl;
6868
this.name = idl.name;
69-
this.htmlConstructor = Boolean(utils.getExtAttr(this.idl.extAttrs, "HTMLConstructor"));
7069

7170
for (const member of this.idl.members) {
7271
member.definingInterface = this.name;
@@ -657,7 +656,9 @@ class Interface {
657656
}
658657

659658
if (utils.hasCEReactions(this.indexedSetter)) {
660-
invocation = this.ctx.processCEReactions(this.indexedSetter, invocation);
659+
invocation = this.ctx.invokeProcessCEReactions(invocation, {
660+
requires: this.requires
661+
});
661662
}
662663

663664
return str + invocation;
@@ -694,7 +695,9 @@ class Interface {
694695
}
695696

696697
if (utils.hasCEReactions(this.namedSetter)) {
697-
invocation = this.ctx.processCEReactions(this.namedSetter, invocation);
698+
invocation = this.ctx.invokeProcessCEReactions(invocation, {
699+
requires: this.requires
700+
});
698701
}
699702

700703
return str + invocation;
@@ -1087,7 +1090,9 @@ class Interface {
10871090
}
10881091

10891092
if (utils.hasCEReactions(this.namedDeleter)) {
1090-
invocation = this.ctx.processCEReactions(this.namedDeleter, invocation);
1093+
invocation = this.ctx.invokeProcessCEReactions(invocation, {
1094+
requires: this.requires
1095+
});
10911096
}
10921097

10931098
this.str += invocation;
@@ -1205,6 +1210,12 @@ class Interface {
12051210
`;
12061211
}
12071212

1213+
if (utils.getExtAttr(this.idl.extAttrs, "HTMLConstructor")) {
1214+
body = this.ctx.invokeProcessHTMLConstructor(body, {
1215+
requires: this.requires
1216+
});
1217+
}
1218+
12081219
this.addMethod("prototype", "constructor", argNames, body, "regular", { enumerable: false });
12091220
}
12101221

@@ -1483,10 +1494,6 @@ class Interface {
14831494

14841495
this.generateMixins();
14851496
this.generateRequires();
1486-
1487-
if (this.htmlConstructor) {
1488-
this.str = this.ctx.processHTMLConstructor(this.idl, this.str);
1489-
}
14901497
}
14911498

14921499
toString() {

lib/constructs/operation.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,23 @@ class Operation {
7979
requires.merge(parameterConversions.requires);
8080
str += parameterConversions.body;
8181

82-
let incovation;
82+
let invocation;
8383
if (overloads.every(overload => conversions[overload.operation.idlType.idlType])) {
84-
incovation = `
84+
invocation = `
8585
return ${callOn}.${implFunc}(${argsSpread});
8686
`;
8787
} else {
88-
incovation = `
88+
invocation = `
8989
return utils.tryWrapperForImpl(${callOn}.${implFunc}(${argsSpread}));
9090
`;
9191
}
9292

9393
if (utils.hasCEReactions(this.idls[0])) {
94-
incovation = this.ctx.processCEReactions(this.idls[0], incovation);
94+
invocation = this.ctx.invokeProcessCEReactions(invocation, {
95+
requires
96+
});
9597
}
96-
str += incovation;
98+
str += invocation;
9799

98100
if (this.static) {
99101
this.interface.addStaticMethod(this.name, argNames, str);

lib/context.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ class Context {
5656
}
5757
return undefined;
5858
}
59+
60+
invokeProcessCEReactions(code, config) {
61+
return this._invokeProcessor(this.processCEReactions, code, config);
62+
}
63+
64+
invokeProcessHTMLConstructor(code, config) {
65+
return this._invokeProcessor(this.processHTMLConstructor, code, config);
66+
}
67+
68+
_invokeProcessor(processor, code, config) {
69+
const { requires } = config;
70+
71+
if (!requires) {
72+
throw new TypeError("Internal error: missing requires object in context");
73+
}
74+
75+
const context = {
76+
addImport(source, imported) {
77+
return requires.add(source, imported);
78+
}
79+
};
80+
81+
return processor.call(context, code);
82+
}
5983
}
6084

6185
module.exports = Context;

lib/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ class RequiresMap extends Map {
7272
}
7373

7474
add(type, func = "") {
75-
const key = func + type;
76-
let req = `require("./${type}.js")`;
75+
const key = (func + type).replace(/[./-]/g, "_");
76+
77+
const path = type.startsWith(".") ? type : `./${type}`;
78+
let req = `require("${path}.js")`;
79+
7780
if (func) {
7881
req += `.${func}`;
7982
}
83+
8084
this.addRaw(key, req);
85+
return key;
8186
}
8287

8388
addRaw(key, expr) {

test/__snapshots__/test.js.snap

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ exports[`CEReactions.webidl 1`] = `
66
const conversions = require(\\"webidl-conversions\\");
77
const utils = require(\\"./utils.js\\");
88
9+
const preSteps___CEReactions = require(\\"../CEReactions.js\\").preSteps;
10+
const postSteps___CEReactions = require(\\"../CEReactions.js\\").postSteps;
911
const impl = utils.implSymbol;
1012
const ctorRegistry = utils.ctorRegistrySymbol;
1113
@@ -162,7 +164,7 @@ const iface = {
162164
context: \\"Failed to set the '\\" + P + \\"' property on 'CEReactions': The provided value\\"
163165
});
164166
165-
// CEReactions pre steps
167+
preSteps___CEReactions(globalObject);
166168
try {
167169
const creating = !target[impl][utils.supportsPropertyName](P);
168170
if (creating) {
@@ -171,7 +173,7 @@ const iface = {
171173
target[impl][utils.namedSetExisting](P, namedValue);
172174
}
173175
} finally {
174-
// CEReactions post steps
176+
postSteps___CEReactions(globalObject);
175177
}
176178
177179
return true;
@@ -226,7 +228,7 @@ const iface = {
226228
context: \\"Failed to set the '\\" + P + \\"' property on 'CEReactions': The provided value\\"
227229
});
228230
229-
// CEReactions pre steps
231+
preSteps___CEReactions(globalObject);
230232
try {
231233
const creating = !target[impl][utils.supportsPropertyName](P);
232234
if (creating) {
@@ -235,7 +237,7 @@ const iface = {
235237
target[impl][utils.namedSetExisting](P, namedValue);
236238
}
237239
} finally {
238-
// CEReactions post steps
240+
postSteps___CEReactions(globalObject);
239241
}
240242
241243
return true;
@@ -249,12 +251,12 @@ const iface = {
249251
}
250252
251253
if (target[impl][utils.supportsPropertyName](P) && !(P in target)) {
252-
// CEReactions pre steps
254+
preSteps___CEReactions(globalObject);
253255
try {
254256
target[impl][utils.namedDelete](P);
255257
return true;
256258
} finally {
257-
// CEReactions post steps
259+
postSteps___CEReactions(globalObject);
258260
}
259261
}
260262
@@ -284,11 +286,11 @@ const iface = {
284286
throw new TypeError(\\"Illegal invocation\\");
285287
}
286288
287-
// CEReactions pre steps
289+
preSteps___CEReactions(globalObject);
288290
try {
289291
return this[impl].method();
290292
} finally {
291-
// CEReactions post steps
293+
postSteps___CEReactions(globalObject);
292294
}
293295
}
294296
@@ -297,11 +299,11 @@ const iface = {
297299
throw new TypeError(\\"Illegal invocation\\");
298300
}
299301
300-
// CEReactions pre steps
302+
preSteps___CEReactions(globalObject);
301303
try {
302304
return this[impl][\\"attr\\"];
303305
} finally {
304-
// CEReactions post steps
306+
postSteps___CEReactions(globalObject);
305307
}
306308
}
307309
@@ -314,11 +316,11 @@ const iface = {
314316
context: \\"Failed to set the 'attr' property on 'CEReactions': The provided value\\"
315317
});
316318
317-
// CEReactions pre steps
319+
preSteps___CEReactions(globalObject);
318320
try {
319321
this[impl][\\"attr\\"] = V;
320322
} finally {
321-
// CEReactions post steps
323+
postSteps___CEReactions(globalObject);
322324
}
323325
}
324326
}
@@ -1140,8 +1142,7 @@ exports[`HTMLConstructor.webidl 1`] = `
11401142
const conversions = require(\\"webidl-conversions\\");
11411143
const utils = require(\\"./utils.js\\");
11421144
1143-
// HTMLConstructor
1144-
1145+
const HTMLConstructor___HTMLConstructor = require(\\"../HTMLConstructor.js\\").HTMLConstructor;
11451146
const impl = utils.implSymbol;
11461147
const ctorRegistry = utils.ctorRegistrySymbol;
11471148
@@ -1223,7 +1224,7 @@ const iface = {
12231224
install(globalObject) {
12241225
class HTMLConstructor {
12251226
constructor() {
1226-
throw new TypeError(\\"Illegal constructor\\");
1227+
return HTMLConstructor___HTMLConstructor.HTMLConstructor(globalObject);
12271228
}
12281229
}
12291230
Object.defineProperties(HTMLConstructor.prototype, {

test/test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ const outputDir = path.resolve(__dirname, "output");
1313

1414
beforeAll(() => {
1515
const transformer = new Transformer({
16-
processCEReactions(_, code) {
16+
processCEReactions(code) {
17+
const preSteps = this.addImport("../CEReactions", "preSteps");
18+
const postSteps = this.addImport("../CEReactions", "postSteps");
19+
1720
return `
18-
// CEReactions pre steps
21+
${preSteps}(globalObject);
1922
try {
2023
${code}
2124
} finally {
22-
// CEReactions post steps
25+
${postSteps}(globalObject);
2326
}
2427
`;
2528
},
26-
processHTMLConstructor(_, code) {
29+
processHTMLConstructor() {
30+
const identifier = this.addImport("../HTMLConstructor", "HTMLConstructor");
31+
2732
return `
28-
// HTMLConstructor
29-
${code}
33+
return ${identifier}.HTMLConstructor(globalObject);
3034
`;
3135
}
3236
});

0 commit comments

Comments
 (0)