/
inliner.js
255 lines (217 loc) · 6.82 KB
/
inliner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var estraverse = require('estraverse');
function Inliner() {
this.lastBlock = null;
this.lastStmt = null;
this.blockHistory = [];
this.stmtHistory = [];
this.blockSubsts = [];
this.localRef = {};
};
exports.Inliner = Inliner;
Inliner.create = function create() {
return new Inliner();
};
Inliner.inline = function inline(ast) {
return new Inliner().inline(ast);
};
Inliner.prototype.inline = function run(ast) {
estraverse.replace(ast, {
enter: this.enter.bind(this),
leave: this.leave.bind(this)
});
return ast;
};
Inliner.prototype.enter = function enter(node, parent) {
// while (...) (function() {})()
// for (...) (function() {})()
if ((node.type === 'WhileStatement' ||
node.type === 'ForStatement') &&
node.body.type === 'ExpressionStatement') {
var block = { type: 'BlockStatement', body: [ node.body ] };
return {
type: node.type,
init: node.init,
test: node.test,
update: node.update,
body: block
};
// if (...) (function() {})()
} else if (node.type === 'IfStatement') {
var clone = {
type: node.type,
test: node.test,
consequent: node.consequent,
alternate: node.alternate
};
var change = false;
if (clone.consequent.type === 'ExpressionStatement') {
change = true;
clone.consequent = { type: 'BlockStatement', body: [ clone.consequent ] };
}
if (clone.alternate && clone.alternate.type === 'ExpressionStatement') {
change = true;
clone.alternate = { type: 'BlockStatement', body: [ clone.alternate ] };
}
if (change)
return clone;
}
if (Array.isArray(node.body)) {
this.blockHistory.push(node);
this.lastBlock = node;
}
if (/Statement/.test(node.type) || node.type === 'VariableDeclaration') {
this.stmtHistory.push(node);
this.lastStmt = node;
}
};
Inliner.prototype.leave = function leave(node) {
// Inline stmts if we're leaving block
this.tryInline(node);
if (Array.isArray(node.body) && node === this.lastBlock) {
this.blockHistory.pop();
this.lastBlock = this.blockHistory[this.blockHistory.length - 1];
}
if (node === this.lastStmt) {
this.stmtHistory.pop();
this.lastStmt = this.stmtHistory[this.stmtHistory.length - 1];
}
// Try inlining closure
this.inlineClosure(node);
};
Inliner.prototype.inlineClosure = function inlineClosure(node) {
if (node.type !== 'CallExpression')
return;
var callee = node.callee;
var isCallThis = callee.type === 'MemberExpression' &&
callee.object.type === 'FunctionExpression' &&
((callee.property.name ||
callee.property.value) === 'call') &&
node.arguments.length >= 1 &&
node.arguments[0].type === 'ThisExpression';
var noThis = callee.type === 'FunctionExpression' &&
node.arguments.length === 0 &&
this.noThisUsage(callee);
if (!isCallThis && !noThis)
return;
// Skip nested blocks
// TODO(indutny): figure out where they're created
var stmts = isCallThis ? node.callee.object.body : node.callee.body;
do {
var last = stmts;
while (stmts.type === 'BlockStatement') stmts = stmts.body;
while (Array.isArray(stmts) &&
stmts.length === 1 &&
stmts[0].type === 'BlockStatement') {
stmts = stmts[0].body;
}
} while (last !== stmts);
var body;
var result;
if (stmts.length === 1 && stmts[0].type === 'ReturnStatement') {
// local()(function() { return expr })
result = stmts[0].argument;
} else {
var pre = stmts.slice(0, -1),
last = stmts[stmts.length - 1];
if (pre.every(this.isNonReturn.bind(this))) {
if (this.isNonReturn(last)) {
// (function() { non-return* }).call(this)
body = stmts;
} else if (last.type === 'ReturnStatement') {
// (function() { non-return*; return ... }).call(this)
body = pre;
result = last.argument;
}
}
}
if (!body && !result)
return;
// Inline function's body into last block
if (!this.lastBlock || !this.lastStmt)
return;
var index = this.lastBlock.body.indexOf(this.lastStmt);
if (index === -1)
return;
body = body || [];
result = result || { type: 'Identifier', name: 'undefined' };
// Try replacing statement when leaving it
this.blockSubsts.push({
index: index,
block: this.lastBlock,
from: node,
body: body,
result: result
});
// Sort by decreasing index to ensure replacement order
// XXX: Use insertion sort
this.blockSubsts.sort(function(a, b) {
return b.index - a.index;
});
};
Inliner.prototype.tryInline = function tryInline(node) {
if (node !== this.lastBlock || !this.blockSubsts.length)
return;
this.blockSubsts = this.blockSubsts.filter(function(item) {
if (item.block !== this.lastBlock) return true;
var stmt = item.block.body[item.index],
subst = null;
var stmts = item.body.concat(stmt),
res = item.result;
if (stmt.type === 'ExpressionStatement' &&
stmt.expression.type === 'AssignmentExpression' &&
stmt.expression.right === item.from) {
subst = stmts;
stmt.expression.right = res;
} else if (stmt.type === 'VariableDeclaration' &&
stmt.declarations.length === 1 &&
stmt.declarations[0].init === item.from) {
subst = stmts;
stmt.declarations[0].init = res;
} else if (stmt.type === 'ExpressionStatement' &&
stmt.expression === item.from) {
subst = stmts.slice(0, -1);
} else if (stmt.type === 'ReturnStatement' &&
stmt.argument === item.from) {
stmt.argument = res;
subst = stmts;
}
if (subst !== null) {
Array.prototype.splice.apply(item.block.body,
[item.index, 1].concat(subst));
}
return false;
}, this);
};
Inliner.prototype.isNonReturn = function isNonReturn(ast) {
if (!ast)
return true;
if (ast.type === 'BlockStatement')
return ast.body.every(this.isNonReturn.bind(this));
if (ast.type === 'IfStatement')
return this.isNonReturn(ast.consequent) && this.isNonReturn(ast.alternate);
if (ast.type === 'WhileStatement' ||
ast.type === 'DoWhileStatement' ||
ast.type === 'ForStatement' ||
ast.type === 'ForInStatement')
return this.isNonReturn(ast.body);
if (ast.type === 'VarStatement') {
return ast.declarations.every(function(decl) {
return /^__\$/.test(decl.id.name);
});
}
return ast.type !== 'ReturnStatement';
};
Inliner.prototype.noThisUsage = function noThisUsage(ast) {
var res = true;
estraverse.traverse(ast, {
enter: function(ast) {
if (ast.type === 'ThisExpression') {
res = false;
this.skip();
} else if (ast.type === 'FunctionExpression') {
this.skip();
}
}
});
return res;
};