summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/javascript-compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2014-01-19 18:54:13 -0600
committerkpdecker <kpdecker@gmail.com>2014-02-09 14:24:18 -0600
commit7a7ad74ff1c4fe8685d33cceebe03f1474eb865d (patch)
tree6cfdb272042a8a76362678cb343f2e7b262c1c3f /lib/handlebars/compiler/javascript-compiler.js
parentd6e86af41ab987b5ad8be0d4a98f0cc408cb5e1d (diff)
downloadhandlebars.js-7a7ad74ff1c4fe8685d33cceebe03f1474eb865d.zip
handlebars.js-7a7ad74ff1c4fe8685d33cceebe03f1474eb865d.tar.gz
handlebars.js-7a7ad74ff1c4fe8685d33cceebe03f1474eb865d.tar.bz2
Optimize buffer generate first and all edge cases
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js65
1 files changed, 39 insertions, 26 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index 8222506..8781819 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -140,42 +140,27 @@ JavaScriptCompiler.prototype = {
},
preamble: function() {
- var out = [];
-
- if (!this.environment.isSimple) {
- out.push(", buffer = " + this.initializeBuffer());
- } else {
- out.push("");
- }
-
// track the last context pushed into place to allow skipping the
// getContext opcode when it would be a noop
this.lastContext = 0;
- this.source = out;
+ this.source = [];
},
createFunctionContext: function(asObject) {
- var locals = this.stackVars.concat(this.registers.list);
+ var varDeclarations = '';
+ var locals = this.stackVars.concat(this.registers.list);
if(locals.length > 0) {
- this.source[0] += ", " + locals.join(", ");
+ varDeclarations += ", " + locals.join(", ");
}
// Generate minimizer alias mappings
for (var alias in this.aliases) {
if (this.aliases.hasOwnProperty(alias)) {
- this.source[0] += ', ' + alias + '=' + this.aliases[alias];
+ varDeclarations += ', ' + alias + '=' + this.aliases[alias];
}
}
- if (this.source[0]) {
- this.source[0] = "var " + this.source[0].substring(2) + ";";
- }
-
- if (!this.environment.isSimple) {
- this.pushSource("return buffer;");
- }
-
var params = ["depth0", "helpers", "partials", "data"];
for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
@@ -183,7 +168,7 @@ JavaScriptCompiler.prototype = {
}
// Perform a second pass over the output to merge content when possible
- var source = this.mergeSource();
+ var source = this.mergeSource(varDeclarations);
if (asObject) {
params.push(source);
@@ -193,11 +178,12 @@ JavaScriptCompiler.prototype = {
return 'function(' + params.join(',') + ') {\n ' + source + '}';
}
},
- mergeSource: function() {
- // WARN: We are not handling the case where buffer is still populated as the source should
- // not have buffer append operations as their final action.
+ mergeSource: function(varDeclarations) {
var source = '',
- buffer;
+ buffer,
+ appendOnly = !this.forceBuffer,
+ appendFirst;
+
for (var i = 0, len = this.source.length; i < len; i++) {
var line = this.source[i];
if (line.appendToBuffer) {
@@ -208,12 +194,39 @@ JavaScriptCompiler.prototype = {
}
} else {
if (buffer) {
- source += 'buffer += ' + buffer + ';\n ';
+ if (!source) {
+ appendFirst = true;
+ source = buffer + ';\n ';
+ } else {
+ source += 'buffer += ' + buffer + ';\n ';
+ }
buffer = undefined;
}
source += line + '\n ';
+
+ if (!this.environment.isSimple) {
+ appendOnly = false;
+ }
}
}
+
+ if (appendOnly) {
+ if (buffer || !source) {
+ source += 'return ' + (buffer || '""') + ';\n';
+ }
+ } else {
+ varDeclarations += ", buffer = " + (appendFirst ? '' : this.initializeBuffer());
+ if (buffer) {
+ source += 'return buffer + ' + buffer + ';\n';
+ } else {
+ source += 'return buffer;\n';
+ }
+ }
+
+ if (varDeclarations) {
+ source = 'var ' + varDeclarations.substring(2) + (appendFirst ? '' : ';\n ') + source;
+ }
+
return source;
},