Inline stack helper implementation

This commit is contained in:
kpdecker
2013-01-19 17:35:05 -06:00
parent 7277284132
commit ae95407775
+48 -16
View File
@@ -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;