summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2011-07-31 21:01:40 -0500
committerkpdecker <kpdecker@gmail.com>2011-07-31 21:01:40 -0500
commit6349c1a0c709845837ac85f4b382a09b7e45c3f5 (patch)
treedd7dc4d812750444afb8ab88837dd46d44855bea /lib/handlebars/compiler/compiler.js
parentf53737ef23e5d37ecce932fb7dfb6ae720cbcad7 (diff)
downloadhandlebars.js-6349c1a0c709845837ac85f4b382a09b7e45c3f5.zip
handlebars.js-6349c1a0c709845837ac85f4b382a09b7e45c3f5.tar.gz
handlebars.js-6349c1a0c709845837ac85f4b382a09b7e45c3f5.tar.bz2
Optimize the populate call logic for the simple cases
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js38
1 files changed, 21 insertions, 17 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 6d8c603..a887ba3 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -28,11 +28,11 @@ Handlebars.JavaScriptCompiler = function() {};
getContext: 1,
lookupWithHelpers: 2,
lookup: 1,
- invokeMustache: 2,
+ invokeMustache: 3,
pushString: 1,
truthyOrFallback: 1,
functionOrFallback: 1,
- invokeProgram: 2,
+ invokeProgram: 3,
invokePartial: 1,
push: 1,
assignToHash: 1,
@@ -166,7 +166,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.declare('inverse', inverseGuid);
}
- this.opcode('invokeProgram', programGuid, params.length);
+ this.opcode('invokeProgram', programGuid, params.length, !!mustache.hash);
this.declare('inverse', null);
this.opcode('append');
},
@@ -178,7 +178,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.declare('inverse', programGuid);
- this.opcode('invokeProgram', null, params.length);
+ this.opcode('invokeProgram', null, params.length, !!block.mustache.hash);
this.opcode('append');
},
@@ -217,7 +217,7 @@ Handlebars.JavaScriptCompiler = function() {};
mustache: function(mustache) {
var params = this.setupStackForMustache(mustache);
- this.opcode('invokeMustache', params.length, mustache.id.original);
+ this.opcode('invokeMustache', params.length, mustache.id.original, !!mustache.hash);
if(mustache.escaped) {
this.opcode('appendEscaped');
@@ -272,10 +272,11 @@ Handlebars.JavaScriptCompiler = function() {};
}
},
- opcode: function(name, val1, val2) {
+ opcode: function(name, val1, val2, val3) {
this.opcodes.push(Compiler.OPCODE_MAP[name]);
if(val1 !== undefined) { this.opcodes.push(val1); }
if(val2 !== undefined) { this.opcodes.push(val2); }
+ if(val3 !== undefined) { this.opcodes.push(val3); }
},
declare: function(name, value) {
@@ -300,8 +301,6 @@ Handlebars.JavaScriptCompiler = function() {};
if(mustache.hash) {
this.hash(mustache.hash);
- } else {
- this.opcode('push', '{}');
}
this.ID(mustache.id);
@@ -531,8 +530,8 @@ Handlebars.JavaScriptCompiler = function() {};
this.pushStack(name);
},
- invokeMustache: function(paramSize, original) {
- this.populateParams(paramSize, this.quotedString(original), "{}", null, function(nextStack, helperMissingString, id) {
+ invokeMustache: function(paramSize, original, hasHash) {
+ this.populateParams(paramSize, this.quotedString(original), "{}", null, hasHash, function(nextStack, helperMissingString, id) {
if (!this.usingKnownHelper) {
this.context.aliases.helperMissing = 'helpers.helperMissing';
this.context.aliases.undef = 'void 0';
@@ -542,11 +541,11 @@ Handlebars.JavaScriptCompiler = function() {};
});
},
- invokeProgram: function(guid, paramSize) {
+ invokeProgram: function(guid, paramSize, hasHash) {
var inverse = this.programExpression(this.inverse);
var mainProgram = this.programExpression(guid);
- this.populateParams(paramSize, null, mainProgram, inverse, function(nextStack, helperMissingString, id) {
+ this.populateParams(paramSize, null, mainProgram, inverse, hasHash, function(nextStack, helperMissingString, id) {
if (!this.usingKnownHelper) {
this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
this.source.push("else { " + nextStack + " = blockHelperMissing.call(" + helperMissingString + "); }");
@@ -554,14 +553,19 @@ Handlebars.JavaScriptCompiler = function() {};
});
},
- populateParams: function(paramSize, helperId, program, inverse, fn) {
+ populateParams: function(paramSize, helperId, program, inverse, hasHash, fn) {
+ var needsRegister = hasHash || this.options.stringParams || inverse || this.options.data;
var id = this.popStack(), nextStack;
var params = [], param, stringParam;
- var hash = this.popStack();
+ if (needsRegister) {
+ this.register('tmp1', program);
+ }
- this.register('tmp1', program);
- this.source.push('tmp1.hash = ' + hash + ';');
+ if (hasHash) {
+ var hash = this.popStack();
+ this.source.push('tmp1.hash = ' + hash + ';');
+ }
if(this.options.stringParams) {
this.source.push('tmp1.contexts = [];');
@@ -585,7 +589,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.source.push('tmp1.data = data;');
}
- params.push('tmp1');
+ params.push(needsRegister ? 'tmp1' : '{}');
this.populateCall(params, id, helperId || id, fn);
},