diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 145 |
1 files changed, 25 insertions, 120 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 6eda2ea..4580f31 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -7,65 +7,6 @@ Handlebars.Compiler = function() {}; Handlebars.JavaScriptCompiler = function() {}; (function(Compiler, JavaScriptCompiler) { - Compiler.OPCODE_MAP = { - appendContent: 1, - getContext: 2, - lookupWithHelpers: 3, - lookup: 4, - append: 5, - invokeMustache: 6, - appendEscaped: 7, - pushString: 8, - truthyOrFallback: 9, - functionOrFallback: 10, - invokeProgram: 11, - invokePartial: 12, - push: 13, - assignToHash: 15, - pushStringParam: 16, - knownHelper: 17, - pushContext: 18, - lookupOnContext: 19, - resolvePossibleLambda: 20, - invokeHelper: 21, - pushLiteral: 22, - invokeKnownHelper: 23 - }; - - Compiler.MULTI_PARAM_OPCODES = { - appendContent: 1, - getContext: 1, - lookupWithHelpers: 1, - lookup: 1, - invokeMustache: 2, - pushString: 1, - truthyOrFallback: 1, - functionOrFallback: 1, - invokeProgram: 3, - invokePartial: 1, - push: 1, - assignToHash: 1, - pushStringParam: 1, - knownHelper: 1, - pushContext: 0, - lookupOnContext: 1, - resolvePossibleLambda: 0, - invokeHelper: 2, - pushLiteral: 1, - invokeKnownHelper: 2 - }; - - Compiler.DISASSEMBLE_MAP = {}; - - for(var prop in Compiler.OPCODE_MAP) { - var value = Compiler.OPCODE_MAP[prop]; - Compiler.DISASSEMBLE_MAP[value] = prop; - } - - Compiler.multiParamSize = function(code) { - return Compiler.MULTI_PARAM_OPCODES[Compiler.DISASSEMBLE_MAP[code]]; - }; - // the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which // requires that context functions in blocks are evaluated by blockHelperMissing, @@ -75,35 +16,23 @@ Handlebars.JavaScriptCompiler = function() {}; compiler: Compiler, disassemble: function() { - var opcodes = this.opcodes, opcode, nextCode; - var out = [], str, name, value; + var opcodes = this.opcodes, opcode, out = [], params, param; - for(var i=0, l=opcodes.length; i<l; i++) { + for (var i=0, l=opcodes.length; i<l; i++) { opcode = opcodes[i]; - if(opcode === 'DECLARE') { - name = opcodes[++i]; - value = opcodes[++i]; - out.push("DECLARE " + name + " = " + value); + if (opcode.opcode === 'DECLARE') { + out.push("DECLARE " + opcode.name + "=" + opcode.value); } else { - str = Compiler.DISASSEMBLE_MAP[opcode]; - - var extraParams = Compiler.multiParamSize(opcode); - var codes = []; - - for(var j=0; j<extraParams; j++) { - nextCode = opcodes[++i]; - - if(typeof nextCode === "string") { - nextCode = "\"" + nextCode.replace("\n", "\\n") + "\""; + params = []; + for (var j=0; j<opcode.args.length; j++) { + param = opcode.args[j]; + if (typeof param === "string") { + param = "\"" + param.replace("\n", "\\n") + "\""; } - - codes.push(nextCode); + params.push(param); } - - str = str + " " + codes.join(" "); - - out.push(str); + out.push(opcode.opcode + " " + params.join(" ")); } } @@ -328,17 +257,12 @@ Handlebars.JavaScriptCompiler = function() {}; comment: function() {}, // HELPERS - 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); } + opcode: function(name) { + this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) }); }, declare: function(name, value) { - this.opcodes.push('DECLARE'); - this.opcodes.push(name); - this.opcodes.push(value); + this.opcodes.push({ opcode: 'DECLARE', name: name, value: value }); }, addDepth: function(depth) { @@ -451,44 +375,25 @@ Handlebars.JavaScriptCompiler = function() {}; this.i = 0; for(l=opcodes.length; this.i<l; this.i++) { - opcode = this.nextOpcode(0); + opcode = opcodes[this.i]; - if(opcode[0] === 'DECLARE') { - this.i = this.i + 2; - this[opcode[1]] = opcode[2]; + if(opcode.opcode === 'DECLARE') { + this[opcode.name] = opcode.value; } else { - this.i = this.i + opcode[1].length; - this[opcode[0]].apply(this, opcode[1]); + this[opcode.opcode].apply(this, opcode.args); } } return this.createFunctionContext(asObject); }, - nextOpcode: function(n) { - var opcodes = this.environment.opcodes, opcode = opcodes[this.i + n], name, val; - var extraParams, codes; - - if(opcode === 'DECLARE') { - name = opcodes[this.i + 1]; - val = opcodes[this.i + 2]; - return ['DECLARE', name, val]; - } else { - name = Compiler.DISASSEMBLE_MAP[opcode]; - - extraParams = Compiler.multiParamSize(opcode); - codes = []; - - for(var j=0; j<extraParams; j++) { - codes.push(opcodes[this.i + j + 1 + n]); - } - - return [name, codes]; - } + nextOpcode: function() { + var opcodes = this.environment.opcodes, opcode = opcodes[this.i + 1]; + return opcodes[this.i + 1]; }, eat: function(opcode) { - this.i = this.i + opcode.length; + this.i = this.i + 1; }, preamble: function() { @@ -576,11 +481,11 @@ Handlebars.JavaScriptCompiler = function() {}; }, appendEscaped: function() { - var opcode = this.nextOpcode(1), extra = ""; + var opcode = this.nextOpcode(), extra = ""; this.context.aliases.escapeExpression = 'this.escapeExpression'; - if(opcode[0] === 'appendContent') { - extra = " + " + this.quotedString(opcode[1][0]); + if(opcode && opcode.opcode === 'appendContent') { + extra = " + " + this.quotedString(opcode.args[0]); this.eat(opcode); } |