summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2013-01-19 17:35:05 -0600
committerkpdecker <kpdecker@gmail.com>2013-01-19 17:35:05 -0600
commitae954077751ef0103684e840ca4f4d00e9de4cb1 (patch)
treece0b50345beca5e3a7fcf0d89aca7b44f1bfdc87 /lib/handlebars/compiler/compiler.js
parent72772841320bbde601820fae1901622190281221 (diff)
downloadhandlebars.js-ae954077751ef0103684e840ca4f4d00e9de4cb1.zip
handlebars.js-ae954077751ef0103684e840ca4f4d00e9de4cb1.tar.gz
handlebars.js-ae954077751ef0103684e840ca4f4d00e9de4cb1.tar.bz2
Inline stack helper implementation
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js64
1 files changed, 48 insertions, 16 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 9e05a1e..f1d368b 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -419,6 +419,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.stackVars = [];
this.registers = { list: [] };
this.compileStack = [];
+ this.inlineStack = [];
this.compileChildren(environment, options);
@@ -917,11 +918,20 @@ Handlebars.JavaScriptCompiler = function() {};
return item;
},
- pushStack: function(item) {
- var stack = this.incrStack();
- this.source.push(stack + " = " + item + ";");
- this.compileStack.push(stack);
- return stack;
+ pushStack: function(item, inline) {
+ if (inline) {
+ this.inlineStack.push(item);
+ return item;
+ } else {
+ this.flushInline();
+
+ var stack = this.incrStack();
+ if (item) {
+ this.source.push(stack + " = " + item + ";");
+ }
+ this.compileStack.push(stack);
+ return stack;
+ }
},
replaceStack: function(callback) {
@@ -937,33 +947,55 @@ Handlebars.JavaScriptCompiler = function() {};
return stack;
},
- nextStack: function(skipCompileStack) {
- var name = this.incrStack();
- this.compileStack.push(name);
- return name;
+ nextStack: function() {
+ return this.pushStack();
},
incrStack: function() {
this.stackSlot++;
if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
+ return this.topStackName();
+ },
+ topStackName: function() {
return "stack" + this.stackSlot;
},
+ flushInline: function() {
+ var inlineStack = this.inlineStack;
+ if (inlineStack.length) {
+ this.inlineStack = [];
+ for (var i = 0, len = inlineStack.length; i < len; i++) {
+ var entry = inlineStack[i];
+ if (entry instanceof Literal) {
+ this.compileStack.push(entry);
+ } else {
+ this.pushStack(entry);
+ }
+ }
+ }
+ },
+ isInline: function() {
+ return this.inlineStack.length;
+ },
- popStack: function() {
- var item = this.compileStack.pop();
+ popStack: function(wrapped) {
+ var inline = this.isInline(),
+ item = (inline ? this.inlineStack : this.compileStack).pop();
- if (item instanceof Literal) {
+ if (!wrapped && (item instanceof Literal)) {
return item.value;
} else {
- this.stackSlot--;
+ if (!inline) {
+ this.stackSlot--;
+ }
return item;
}
},
- topStack: function() {
- var item = this.compileStack[this.compileStack.length - 1];
+ topStack: function(wrapped) {
+ var stack = (this.isInline() ? this.inlineStack : this.compileStack),
+ item = stack[stack.length - 1];
- if (item instanceof Literal) {
+ if (!wrapped && (item instanceof Literal)) {
return item.value;
} else {
return item;