summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js248
1 files changed, 179 insertions, 69 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 9bcc065..a111ae7 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -129,7 +129,7 @@ Handlebars.JavaScriptCompiler = function() {};
// evaluate it by executing `blockHelperMissing`
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('pushHash');
+ this.opcode('emptyHash');
this.opcode('blockValue');
} else {
this.ambiguousMustache(mustache, program, inverse);
@@ -138,7 +138,7 @@ Handlebars.JavaScriptCompiler = function() {};
// evaluate it by executing `blockHelperMissing`
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('pushHash');
+ this.opcode('emptyHash');
this.opcode('ambiguousBlockValue');
}
@@ -162,6 +162,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.opcode('assignToHash', pair[0]);
}
+ this.opcode('popHash');
},
partial: function(partial) {
@@ -202,14 +203,16 @@ Handlebars.JavaScriptCompiler = function() {};
},
ambiguousMustache: function(mustache, program, inverse) {
- var id = mustache.id, name = id.parts[0];
+ var id = mustache.id,
+ name = id.parts[0],
+ isBlock = program != null || inverse != null;
this.opcode('getContext', id.depth);
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('invokeAmbiguous', name);
+ this.opcode('invokeAmbiguous', name, isBlock);
},
simpleMustache: function(mustache) {
@@ -343,7 +346,7 @@ Handlebars.JavaScriptCompiler = function() {};
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushHash');
+ this.opcode('emptyHash');
}
return params;
@@ -360,7 +363,7 @@ Handlebars.JavaScriptCompiler = function() {};
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushHash');
+ this.opcode('emptyHash');
}
return params;
@@ -389,7 +392,11 @@ Handlebars.JavaScriptCompiler = function() {};
if (this.environment.isSimple) {
return "return " + string + ";";
} else {
- return "buffer += " + string + ";";
+ return {
+ appendToBuffer: true,
+ content: string,
+ toString: function() { return "buffer += " + string + ";"; }
+ };
}
},
@@ -419,6 +426,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.stackVars = [];
this.registers = { list: [] };
this.compileStack = [];
+ this.inlineStack = [];
this.compileChildren(environment, options);
@@ -506,12 +514,34 @@ Handlebars.JavaScriptCompiler = function() {};
params.push("depth" + this.environment.depths.list[i]);
}
+ // Perform a second pass over the output to merge content when possible
+ // WARN: We are not handling the case where buffer is still populated as the source should
+ // not have buffer append operations as their final action.
+ var source = '',
+ buffer;
+ for (var i = 0, len = this.source.length; i < len; i++) {
+ var line = this.source[i];
+ if (line.appendToBuffer) {
+ if (buffer) {
+ buffer = buffer + '\n + ' + line.content;
+ } else {
+ buffer = line.content;
+ }
+ } else {
+ if (buffer) {
+ source += 'buffer += ' + buffer + ';\n ';
+ buffer = undefined;
+ }
+ source += line + '\n ';
+ }
+ }
+
if (asObject) {
- params.push(this.source.join("\n "));
+ params.push(source);
return Function.apply(this, params);
} else {
- var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}';
+ var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
return functionSource;
}
@@ -553,6 +583,9 @@ Handlebars.JavaScriptCompiler = function() {};
var current = this.topStack();
params.splice(1, 0, current);
+ // Use the options value generated from the invocation
+ params[params.length-1] = 'options';
+
this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
},
@@ -576,6 +609,9 @@ Handlebars.JavaScriptCompiler = function() {};
// If `value` is truthy, or 0, it is coerced into a string and appended
// Otherwise, the empty string is appended
append: function() {
+ // Force anything that is inlined onto the stack so we don't have duplication
+ // when we examine local
+ this.flushInline();
var local = this.popStack();
this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
if (this.environment.isSimple) {
@@ -590,15 +626,9 @@ Handlebars.JavaScriptCompiler = function() {};
//
// Escape `value` and append it to the buffer
appendEscaped: function() {
- var opcode = this.nextOpcode(), extra = "";
this.context.aliases.escapeExpression = 'this.escapeExpression';
- if(opcode && opcode.opcode === 'appendContent') {
- extra = " + " + this.quotedString(opcode.args[0]);
- this.eat(opcode);
- }
-
- this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
+ this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
},
// [getContext]
@@ -622,7 +652,7 @@ Handlebars.JavaScriptCompiler = function() {};
// Looks up the value of `name` on the current context and pushes
// it onto the stack.
lookupOnContext: function(name) {
- this.pushStack(this.nameLookup('depth' + this.lastContext, name, 'context'));
+ this.pushStack(this.nameLookup('depth' + this.lastContext, name, 'context'), true);
},
// [pushContext]
@@ -647,7 +677,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.replaceStack(function(current) {
return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
- });
+ }, true);
},
// [lookup]
@@ -660,7 +690,7 @@ Handlebars.JavaScriptCompiler = function() {};
lookup: function(name) {
this.replaceStack(function(current) {
return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
- });
+ }, true);
},
// [lookupData]
@@ -670,7 +700,7 @@ Handlebars.JavaScriptCompiler = function() {};
//
// Push the result of looking up `id` on the current data
lookupData: function(id) {
- this.pushStack(this.nameLookup('data', id, 'data'));
+ this.pushStack(this.nameLookup('data', id, 'data'), true);
},
// [pushStringParam]
@@ -693,13 +723,25 @@ Handlebars.JavaScriptCompiler = function() {};
}
},
- pushHash: function() {
- this.push('{}');
+ emptyHash: function() {
+ this.pushStackLiteral('{}');
if (this.options.stringParams) {
this.register('hashTypes', '{}');
}
},
+ pushHash: function() {
+ this.hash = {values: [], types: []};
+ },
+ popHash: function() {
+ var hash = this.hash;
+ this.hash = undefined;
+
+ if (this.options.stringParams) {
+ this.register('hashTypes', '{' + hash.types.join(',') + '}');
+ }
+ this.pushStack('{\n ' + hash.values.join(',\n ') + '\n }', true);
+ },
// [pushString]
//
@@ -718,7 +760,7 @@ Handlebars.JavaScriptCompiler = function() {};
//
// Push an expression onto the stack
push: function(expr) {
- this.pushStack(expr);
+ this.pushStack(expr, true);
},
// [pushLiteral]
@@ -761,12 +803,14 @@ Handlebars.JavaScriptCompiler = function() {};
invokeHelper: function(paramSize, name) {
this.context.aliases.helperMissing = 'helpers.helperMissing';
- var helper = this.lastHelper = this.setupHelper(paramSize, name);
- this.register('foundHelper', helper.name);
+ var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
- this.pushStack("foundHelper ? foundHelper.call(" +
- helper.callParams + ") " + ": helperMissing.call(" +
- helper.helperMissingParams + ")");
+ this.pushStack(helper.name, true);
+ this.replaceStack(function(name) {
+ return name + ' ? ' + name + '.call(' +
+ helper.callParams + ") " + ": helperMissing.call(" +
+ helper.helperMissingParams + ")";
+ }, true);
},
// [invokeKnownHelper]
@@ -778,7 +822,7 @@ Handlebars.JavaScriptCompiler = function() {};
// so a `helperMissing` fallback is not required.
invokeKnownHelper: function(paramSize, name) {
var helper = this.setupHelper(paramSize, name);
- this.pushStack(helper.name + ".call(" + helper.callParams + ")");
+ this.pushStack(helper.name + ".call(" + helper.callParams + ")", true);
},
// [invokeAmbiguous]
@@ -793,19 +837,18 @@ Handlebars.JavaScriptCompiler = function() {};
// This operation emits more code than the other options,
// and can be avoided by passing the `knownHelpers` and
// `knownHelpersOnly` flags at compile-time.
- invokeAmbiguous: function(name) {
+ invokeAmbiguous: function(name, helperCall) {
this.context.aliases.functionType = '"function"';
this.pushStackLiteral('{}');
- var helper = this.setupHelper(0, name);
+ var helper = this.setupHelper(0, name, helperCall);
var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
- this.register('foundHelper', helperName);
var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
var nextStack = this.nextStack();
- this.source.push('if (foundHelper) { ' + nextStack + ' = foundHelper.call(' + helper.callParams + '); }');
+ this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
},
@@ -835,17 +878,19 @@ Handlebars.JavaScriptCompiler = function() {};
// Pops a value and hash off the stack, assigns `hash[key] = value`
// and pushes the hash back onto the stack.
assignToHash: function(key) {
- var value = this.popStack();
+ var value = this.popStack(),
+ type;
if (this.options.stringParams) {
- var type = this.popStack();
+ type = this.popStack();
this.popStack();
- this.source.push("hashTypes['" + key + "'] = " + type + ";");
}
- var hash = this.topStack();
-
- this.source.push(hash + "['" + key + "'] = " + value + ";");
+ var hash = this.hash;
+ if (type) {
+ hash.types.push("'" + key + "': " + type);
+ }
+ hash.values.push("'" + key + "': (" + value + ")");
},
// HELPERS
@@ -907,57 +952,116 @@ Handlebars.JavaScriptCompiler = function() {};
},
pushStackLiteral: function(item) {
- this.compileStack.push(new Literal(item));
- return item;
+ return this.pushStack(new Literal(item), true);
},
- 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) {
- var stack = this.topStack(),
- item = callback.call(this, stack);
+ replaceStack: function(callback, allowInline) {
+ var prefix = '',
+ inline = this.isInline(),
+ stack;
+
+ // If we are currently inline then we want to merge the inline statement into the
+ // replacement statement via ','
+ if (inline) {
+ var top = this.popStack(true);
+
+ if (top instanceof Literal) {
+ // Literals do not need to be inlined
+ stack = top.value;
+ } else {
+ // Get or create the current stack name for use by the inline
+ var name = allowInline && this.stackSlot ? this.topStackName() : this.incrStack();
- // Prevent modification of the context depth variable. Through replaceStack
- if (/^depth/.test(stack)) {
- stack = this.nextStack();
+ prefix = '(' + this.pushStack(name, true) + ' = ' + top + '),';
+ stack = this.topStack();
+ }
+ } else {
+ stack = this.topStack();
}
- this.source.push(stack + " = " + item + ";");
+ var item = callback.call(this, stack);
+
+ if (inline && allowInline) {
+ if (this.inlineStack.length || this.compileStack.length) {
+ this.popStack();
+ }
+ this.pushStack('(' + prefix + item + ')', true);
+ } else {
+ // Prevent modification of the context depth variable. Through replaceStack
+ if (!/^stack/.test(stack)) {
+ stack = this.nextStack();
+ }
+
+ this.source.push(stack + " = (" + prefix + item + ");");
+ }
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;
@@ -972,22 +1076,22 @@ Handlebars.JavaScriptCompiler = function() {};
.replace(/\r/g, '\\r') + '"';
},
- setupHelper: function(paramSize, name) {
+ setupHelper: function(paramSize, name, missingParams) {
var params = [];
- this.setupParams(paramSize, params);
+ this.setupParams(paramSize, params, missingParams);
var foundHelper = this.nameLookup('helpers', name, 'helper');
return {
params: params,
name: foundHelper,
callParams: ["depth0"].concat(params).join(", "),
- helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ")
+ helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
};
},
// the params and contexts arguments are passed in arrays
// to fill in
- setupParams: function(paramSize, params) {
+ setupParams: function(paramSize, params, useRegister) {
var options = [], contexts = [], types = [], param, inverse, program;
options.push("hash:" + this.popStack());
@@ -1032,7 +1136,13 @@ Handlebars.JavaScriptCompiler = function() {};
options.push("data:data");
}
- params.push("{" + options.join(",") + "}");
+ options = "{" + options.join(",") + "}";
+ if (useRegister) {
+ this.register('options', options);
+ params.push('options');
+ } else {
+ params.push(options);
+ }
return params.join(", ");
}
};