diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 9bcc065..9e05a1e 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -129,7 +129,7 @@ Handlebars.JavaScriptCompiler = function() {}; // evaluate it by executing `blockHelperMissing` this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); - this.opcode('pushHash'); + this.opcode('emptyHash'); this.opcode('blockValue'); } else { this.ambiguousMustache(mustache, program, inverse); @@ -138,7 +138,7 @@ Handlebars.JavaScriptCompiler = function() {}; // evaluate it by executing `blockHelperMissing` this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); - this.opcode('pushHash'); + this.opcode('emptyHash'); this.opcode('ambiguousBlockValue'); } @@ -343,7 +343,7 @@ Handlebars.JavaScriptCompiler = function() {}; if(mustache.hash) { this.hash(mustache.hash); } else { - this.opcode('pushHash'); + this.opcode('emptyHash'); } return params; @@ -360,7 +360,7 @@ Handlebars.JavaScriptCompiler = function() {}; if(mustache.hash) { this.hash(mustache.hash); } else { - this.opcode('pushHash'); + this.opcode('emptyHash'); } return params; @@ -693,6 +693,13 @@ Handlebars.JavaScriptCompiler = function() {}; } }, + emptyHash: function() { + this.pushStackLiteral('{}'); + + if (this.options.stringParams) { + this.register('hashTypes', '{}'); + } + }, pushHash: function() { this.push('{}'); @@ -761,10 +768,10 @@ Handlebars.JavaScriptCompiler = function() {}; invokeHelper: function(paramSize, name) { this.context.aliases.helperMissing = 'helpers.helperMissing'; - var helper = this.lastHelper = this.setupHelper(paramSize, name); - this.register('foundHelper', helper.name); + var helper = this.lastHelper = this.setupHelper(paramSize, name, true); + this.useRegister('foundHelper'); - this.pushStack("foundHelper ? foundHelper.call(" + + this.pushStack("(foundHelper = " + helper.name + ") ? foundHelper.call(" + helper.callParams + ") " + ": helperMissing.call(" + helper.helperMissingParams + ")"); }, @@ -800,12 +807,11 @@ Handlebars.JavaScriptCompiler = function() {}; var helper = this.setupHelper(0, name); var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper'); - this.register('foundHelper', helperName); var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context'); var nextStack = this.nextStack(); - this.source.push('if (foundHelper) { ' + nextStack + ' = foundHelper.call(' + helper.callParams + '); }'); + this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }'); this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }'); }, @@ -972,22 +978,22 @@ Handlebars.JavaScriptCompiler = function() {}; .replace(/\r/g, '\\r') + '"'; }, - setupHelper: function(paramSize, name) { + setupHelper: function(paramSize, name, missingParams) { var params = []; - this.setupParams(paramSize, params); + this.setupParams(paramSize, params, missingParams); var foundHelper = this.nameLookup('helpers', name, 'helper'); return { params: params, name: foundHelper, callParams: ["depth0"].concat(params).join(", "), - helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ") + helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ") }; }, // the params and contexts arguments are passed in arrays // to fill in - setupParams: function(paramSize, params) { + setupParams: function(paramSize, params, useRegister) { var options = [], contexts = [], types = [], param, inverse, program; options.push("hash:" + this.popStack()); @@ -1032,7 +1038,13 @@ Handlebars.JavaScriptCompiler = function() {}; options.push("data:data"); } - params.push("{" + options.join(",") + "}"); + options = "{" + options.join(",") + "}"; + if (useRegister) { + this.register('options', options); + params.push('options'); + } else { + params.push(options); + } return params.join(", "); } }; |