Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit bec17d1

Browse files
wycatstilde-engineering
authored andcommitted
Remove unnecessary inlined render function
Now that the template is largely just a data structure that describes its statements and expressions, the `render` function is no longer necessary. Instead, the runtime `render` function uses the data structure to decide what to do. At the moment, it simply delegates to the hooks that were already there, but the plan is to allow runtimes to completely swap out the expression visitor to do more exotic things like building and caching streams.
1 parent 804c5ae commit bec17d1

File tree

3 files changed

+63
-176
lines changed

3 files changed

+63
-176
lines changed

packages/htmlbars-compiler/lib/hydration-javascript-compiler.js

Lines changed: 19 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { processOpcodes } from "./utils";
2-
import { string, array } from "../htmlbars-util/quoting";
2+
import { array } from "../htmlbars-util/quoting";
33

44
function HydrationJavaScriptCompiler() {
55
this.stack = [];
@@ -86,106 +86,50 @@ prototype.compile = function(opcodes, options) {
8686

8787
prototype.prepareArray = function(length) {
8888
var values = [];
89-
var expressionValues = [];
9089

9190
for (var i = 0; i < length; i++) {
92-
values.push(this.stack.pop());
93-
expressionValues.push(this.expressionStack.pop());
91+
values.push(this.expressionStack.pop());
9492
}
9593

96-
this.expressionStack.push(expressionValues);
97-
this.stack.push('[' + values.join(', ') + ']');
94+
this.expressionStack.push(values);
9895
};
9996

10097
prototype.prepareObject = function(size) {
10198
var pairs = [];
102-
var expressionPairs = [];
10399

104100
for (var i = 0; i < size; i++) {
105-
pairs.push(this.stack.pop() + ': ' + this.stack.pop());
106-
expressionPairs.push(this.expressionStack.pop(), this.expressionStack.pop());
101+
pairs.push(this.expressionStack.pop(), this.expressionStack.pop());
107102
}
108103

109-
this.expressionStack.push(expressionPairs);
110-
this.stack.push('{' + pairs.join(', ') + '}');
111-
};
112-
113-
prototype.pushRaw = function(value) {
114-
this.expressionStack.push(value);
115-
this.stack.push(value);
104+
this.expressionStack.push(pairs);
116105
};
117106

118107
prototype.pushLiteral = function(value) {
119108
this.expressionStack.push(value);
120-
121-
if (typeof value === 'string') {
122-
this.stack.push(string(value));
123-
} else {
124-
this.stack.push(value.toString());
125-
}
126-
};
127-
128-
prototype.pushHook = function(name, args) {
129-
this.hooks[name] = true;
130-
this.stack.push(name + '(' + args.join(', ') + ')');
131109
};
132110

133-
prototype.pushGetHook = function(path, morphNum) {
111+
prototype.pushGetHook = function(path) {
134112
this.expressionStack.push([ 'get', path ]);
135-
136-
this.pushHook('get', [
137-
'env',
138-
'morphs[' + morphNum + ']',
139-
'context',
140-
string(path)
141-
]);
142113
};
143114

144-
prototype.pushSexprHook = function(morphNum) {
115+
prototype.pushSexprHook = function() {
145116
this.expressionStack.push([
146117
'subexpr',
147118
this.expressionStack.pop(),
148119
this.expressionStack.pop(),
149120
this.expressionStack.pop()
150121
]);
151-
152-
this.pushHook('subexpr', [
153-
'env',
154-
'morphs[' + morphNum + ']',
155-
'context',
156-
this.stack.pop(), // path
157-
this.stack.pop(), // params
158-
this.stack.pop() // hash
159-
]);
160122
};
161123

162-
prototype.pushConcatHook = function(morphNum) {
124+
prototype.pushConcatHook = function() {
163125
this.expressionStack.push([ 'concat', this.expressionStack.pop() ]);
164-
165-
this.pushHook('concat', [
166-
'env',
167-
'morphs[' + morphNum + ']',
168-
this.stack.pop() // parts
169-
]);
170-
};
171-
172-
prototype.printHook = function(name, args) {
173-
this.hooks[name] = true;
174-
this.source.push(this.indent + ' ' + name + '(' + args.join(', ') + ');\n');
175126
};
176127

177-
prototype.printSetHook = function(name, index) {
128+
prototype.printSetHook = function(name) {
178129
this.augmentContext.push(name);
179-
180-
this.printHook('set', [
181-
'env',
182-
'context',
183-
string(name),
184-
'blockArguments[' + index + ']'
185-
]);
186130
};
187131

188-
prototype.printBlockHook = function(morphNum, templateId, inverseId) {
132+
prototype.printBlockHook = function(templateId, inverseId) {
189133
this.statements.push([
190134
'block',
191135
this.expressionStack.pop(), // path
@@ -194,100 +138,44 @@ prototype.printBlockHook = function(morphNum, templateId, inverseId) {
194138
templateId,
195139
inverseId
196140
]);
197-
198-
this.printHook('block', [
199-
'env',
200-
'morphs[' + morphNum + ']',
201-
'context',
202-
this.stack.pop(), // path
203-
this.stack.pop(), // params
204-
this.stack.pop(), // hash
205-
templateId === null ? 'null' : 'child' + templateId,
206-
inverseId === null ? 'null' : 'child' + inverseId
207-
]);
208141
};
209142

210-
prototype.printInlineHook = function(morphNum) {
211-
var path = this.stack.pop();
212-
var params = this.stack.pop();
213-
var hash = this.stack.pop();
143+
prototype.printInlineHook = function() {
144+
var path = this.expressionStack.pop();
145+
var params = this.expressionStack.pop();
146+
var hash = this.expressionStack.pop();
214147

215-
var exprPath = this.expressionStack.pop();
216-
var exprParams = this.expressionStack.pop();
217-
var exprHash = this.expressionStack.pop();
218-
219-
this.statements.push([ 'inline', exprPath, exprParams, exprHash ]);
220-
221-
this.printHook('inline', [
222-
'env',
223-
'morphs[' + morphNum + ']',
224-
'context',
225-
path,
226-
params,
227-
hash
228-
]);
148+
this.statements.push([ 'inline', path, params, hash ]);
229149
};
230150

231-
prototype.printContentHook = function(morphNum) {
151+
prototype.printContentHook = function() {
232152
this.statements.push([ 'content', this.expressionStack.pop() ]);
233-
234-
this.printHook('content', [
235-
'env',
236-
'morphs[' + morphNum + ']',
237-
'context',
238-
this.stack.pop() // path
239-
]);
240153
};
241154

242-
prototype.printComponentHook = function(morphNum, templateId) {
155+
prototype.printComponentHook = function(templateId) {
243156
this.statements.push([
244157
'component',
245158
this.expressionStack.pop(), // path
246159
this.expressionStack.pop(), // attrs
247160
templateId
248161
]);
249-
250-
this.printHook('component', [
251-
'env',
252-
'morphs[' + morphNum + ']',
253-
'context',
254-
this.stack.pop(), // path
255-
this.stack.pop(), // attrs
256-
templateId === null ? 'null' : 'child' + templateId
257-
]);
258162
};
259163

260-
prototype.printAttributeHook = function(attrMorphNum) {
164+
prototype.printAttributeHook = function() {
261165
this.statements.push([
262166
'attribute',
263167
this.expressionStack.pop(), // name
264168
this.expressionStack.pop() // value;
265169
]);
266-
267-
this.printHook('attribute', [
268-
'env',
269-
'morphs[' + attrMorphNum + ']',
270-
this.stack.pop(), // name
271-
this.stack.pop() // value
272-
]);
273170
};
274171

275-
prototype.printElementHook = function(morphNum) {
172+
prototype.printElementHook = function() {
276173
this.statements.push([
277174
'element',
278175
this.expressionStack.pop(), // path
279176
this.expressionStack.pop(), // params
280177
this.expressionStack.pop() // hash
281178
]);
282-
283-
this.printHook('element', [
284-
'env',
285-
'morphs[' + morphNum + ']',
286-
'context',
287-
this.stack.pop(), // path
288-
this.stack.pop(), // params
289-
this.stack.pop() // hash
290-
]);
291179
};
292180

293181
prototype.createMorph = function(morphNum, parentPath, startIndex, endIndex, escaped) {

packages/htmlbars-compiler/lib/hydration-opcode-compiler.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function HydrationOpcodeCompiler() {
2828
this.currentDOMChildIndex = 0;
2929
this.morphs = [];
3030
this.morphNum = 0;
31-
this.elementMorphNum = null;
3231
this.element = null;
3332
this.elementNum = -1;
3433
}
@@ -136,7 +135,7 @@ HydrationOpcodeCompiler.prototype.mustache = function(mustache) {
136135
var end = this.currentDOMChildIndex;
137136
this.morphs.push([morphNum, this.paths.slice(), start, end, mustache.escaped]);
138137

139-
this.opcode(opcode, morphNum);
138+
this.opcode(opcode);
140139
};
141140

142141
HydrationOpcodeCompiler.prototype.block = function(block) {
@@ -153,7 +152,7 @@ HydrationOpcodeCompiler.prototype.block = function(block) {
153152
var templateId = this.templateId++;
154153
var inverseId = block.inverse === null ? null : this.templateId++;
155154

156-
this.opcode('printBlockHook', morphNum, templateId, inverseId);
155+
this.opcode('printBlockHook', templateId, inverseId);
157156
};
158157

159158
HydrationOpcodeCompiler.prototype.component = function(component) {
@@ -187,7 +186,7 @@ HydrationOpcodeCompiler.prototype.component = function(component) {
187186

188187
this.opcode('prepareObject', attrs.length);
189188
this.opcode('pushLiteral', component.tag);
190-
this.opcode('printComponentHook', morphNum, this.templateId++, blockParams.length);
189+
this.opcode('printComponentHook', this.templateId++, blockParams.length);
191190
};
192191

193192
HydrationOpcodeCompiler.prototype.attribute = function(attr) {
@@ -215,7 +214,7 @@ HydrationOpcodeCompiler.prototype.attribute = function(attr) {
215214
}
216215

217216
this.opcode('createAttrMorph', attrMorphNum, this.elementNum, attr.name, escaped, namespace);
218-
this.opcode('printAttributeHook', attrMorphNum);
217+
this.opcode('printAttributeHook');
219218
};
220219

221220
HydrationOpcodeCompiler.prototype.elementHelper = function(sexpr) {
@@ -227,7 +226,7 @@ HydrationOpcodeCompiler.prototype.elementHelper = function(sexpr) {
227226
}
228227

229228
publishElementMorph(this);
230-
this.opcode('printElementHook', this.elementMorphNum);
229+
this.opcode('printElementHook');
231230
};
232231

233232
HydrationOpcodeCompiler.prototype.pushMorphPlaceholderNode = function() {
@@ -236,11 +235,11 @@ HydrationOpcodeCompiler.prototype.pushMorphPlaceholderNode = function() {
236235

237236
HydrationOpcodeCompiler.prototype.SubExpression = function(sexpr) {
238237
prepareSexpr(this, sexpr);
239-
this.opcode('pushSexprHook', this.morphNum);
238+
this.opcode('pushSexprHook');
240239
};
241240

242241
HydrationOpcodeCompiler.prototype.PathExpression = function(path) {
243-
this.opcode('pushGetHook', path.original, this.morphNum);
242+
this.opcode('pushGetHook', path.original);
244243
};
245244

246245
HydrationOpcodeCompiler.prototype.StringLiteral = function(node) {
@@ -294,7 +293,7 @@ function shareElement(compiler) {
294293
}
295294

296295
function publishElementMorph(compiler) {
297-
var morphNum = compiler.elementMorphNum = compiler.morphNum++;
296+
var morphNum = compiler.morphNum++;
298297
compiler.opcode('createElementMorph', morphNum, compiler.elementNum);
299298
}
300299

0 commit comments

Comments
 (0)