diff options
author | Yehuda Katz <wycats@gmail.com> | 2012-05-28 13:03:22 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2012-05-28 13:03:22 -0700 |
commit | 90adaa3cc8772226d25a1a9807cac25cad3fc54d (patch) | |
tree | e0976ecb2215e5e1a9b5a792beb6f18ec8edefbd /lib/handlebars/compiler/compiler.js | |
parent | 8786a6c6e23ec628300ab405fdec71a888eb39e6 (diff) | |
download | handlebars.js-90adaa3cc8772226d25a1a9807cac25cad3fc54d.zip handlebars.js-90adaa3cc8772226d25a1a9807cac25cad3fc54d.tar.gz handlebars.js-90adaa3cc8772226d25a1a9807cac25cad3fc54d.tar.bz2 |
Continue work on cleaning up helpers
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index c36f7e1..6eda2ea 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -28,7 +28,8 @@ Handlebars.JavaScriptCompiler = function() {}; lookupOnContext: 19, resolvePossibleLambda: 20, invokeHelper: 21, - pushLiteral: 22 + pushLiteral: 22, + invokeKnownHelper: 23 }; Compiler.MULTI_PARAM_OPCODES = { @@ -50,7 +51,8 @@ Handlebars.JavaScriptCompiler = function() {}; lookupOnContext: 1, resolvePossibleLambda: 0, invokeHelper: 2, - pushLiteral: 1 + pushLiteral: 1, + invokeKnownHelper: 2 }; Compiler.DISASSEMBLE_MAP = {}; @@ -278,7 +280,13 @@ Handlebars.JavaScriptCompiler = function() {}; var params = this.setupMustacheParams(mustache), name = mustache.id.parts[0]; - this.opcode('invokeHelper', params.length, name); + if (this.options.knownHelpers[name]) { + this.opcode('invokeKnownHelper', params.length, name); + } else if (this.knownHelpersOnly) { + throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name); + } else { + this.opcode('invokeHelper', params.length, name); + } }, ID: function(id) { @@ -677,17 +685,30 @@ Handlebars.JavaScriptCompiler = function() {}; invokeHelper: function(paramSize, name) { this.context.aliases.helperMissing = 'helpers.helperMissing'; - var params = [], contexts = []; - this.setupParams(paramSize, params, contexts); - this.register('foundHelper', this.nameLookup('helpers', name, 'helper')); + var helper = this.setupHelper(paramSize, name); + this.register('foundHelper', helper.name); + + this.pushStack("foundHelper ? foundHelper.call(" + + helper.callParams + ") " + ": helperMissing.call(" + + helper.helperMissingParams + ")"); + }, + + invokeKnownHelper: function(paramSize, name) { + var helper = this.setupHelper(paramSize, name); + this.pushStack(helper.name + ".call(" + helper.callParams + ")"); + }, - var main = ["depth0"].concat(params).join(", "); - var helperMissing = ["depth0", this.quotedString(name)].concat(params).join(", "); - this.pushStack("foundHelper ? foundHelper.call(" + main + ") " + - ": helperMissing.call(" + helperMissing + ")"); + setupHelper: function(paramSize, name) { + var params = []; + this.setupParams(paramSize, params); + var foundHelper = this.nameLookup('helpers', name, 'helper'); - //this.replaceStack(function(current) { - //}); + return { + params: params, + name: foundHelper, + callParams: ["depth0"].concat(params).join(", "), + helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ") + }; }, // the params and contexts arguments are passed in arrays |