diff options
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 5 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 23 | ||||
-rw-r--r-- | lib/handlebars/compiler/printer.js | 4 |
3 files changed, 27 insertions, 5 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index d61377e..25abe0a 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -93,6 +93,11 @@ var Handlebars = require('./base'); this.isSimple = parts.length === 1 && !this.isScoped && depth === 0; }; + Handlebars.AST.DataNode = function(id) { + this.type = "DATA"; + this.id = id; + }; + Handlebars.AST.StringNode = function(string) { this.type = "STRING"; this.string = string; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 6807494..ae48c69 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -96,7 +96,7 @@ Handlebars.JavaScriptCompiler = function() {}; this.children[guid] = result; for(var i=0, l=result.depths.list.length; i<l; i++) { - var depth = result.depths.list[i]; + depth = result.depths.list[i]; if(depth < 2) { continue; } else { this.addDepth(depth - 1); } @@ -210,10 +210,14 @@ Handlebars.JavaScriptCompiler = function() {}; simpleMustache: function(mustache, program, inverse) { var id = mustache.id; - this.addDepth(id.depth); - this.opcode('getContext', id.depth); + if (id.type === 'ID') { + this.addDepth(id.depth); + this.opcode('getContext', id.depth); + } - if (id.parts.length) { + if (id.type === 'DATA') { + this.opcode('lookupData', id.id); + } else if (id.parts.length) { this.opcode('lookupOnContext', id.parts[0]); for(var i=1, l=id.parts.length; i<l; i++) { this.opcode('lookup', id.parts[i]); @@ -247,6 +251,10 @@ Handlebars.JavaScriptCompiler = function() {}; } }, + DATA: function(data) { + this.opcode('lookupData', data.id); + }, + STRING: function(string) { this.opcode('pushString', string.string); }, @@ -271,6 +279,7 @@ Handlebars.JavaScriptCompiler = function() {}; }, addDepth: function(depth) { + if(isNaN(depth)) { throw new Error("EWOT"); } if(depth === 0) { return; } if(!this.depths[depth]) { @@ -646,6 +655,10 @@ Handlebars.JavaScriptCompiler = function() {}; }); }, + lookupData: function(id) { + this.pushStack(this.nameLookup('data', id, 'data')); + }, + // [pushStringParam] // // On stack, before: ... @@ -831,7 +844,7 @@ Handlebars.JavaScriptCompiler = function() {}; var programParams = [child.index, child.name, "data"]; for(var i=0, l = depths.length; i<l; i++) { - var depth = depths[i]; + depth = depths[i]; if(depth === 1) { programParams.push("depth0"); } else { programParams.push("depth" + (depth - 1)); } diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index 8157190..7a42a66 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -111,6 +111,10 @@ Handlebars.PrintVisitor.prototype.ID = function(id) { } }; +Handlebars.PrintVisitor.prototype.DATA = function(data) { + return "@" + data.id; +}; + Handlebars.PrintVisitor.prototype.content = function(content) { return this.pad("CONTENT[ '" + content.string + "' ]"); }; |