Allow reserved words to be used without slowing down most operations in browsers with faster foo.bar vs. foo['bar'].

This commit is contained in:
wycats
2010-12-19 00:06:30 -08:00
parent b418ef6eb2
commit f4ae0c27fe
+26 -10
View File
@@ -1,8 +1,6 @@
var Handlebars = {};
Handlebars.parse = require("handlebars/compiler").Handlebars.parse;
if(typeof puts === "undefined") { var puts = function() {}; };
// BEGIN(BROWSER)
Handlebars.Compiler = function() {};
Handlebars.JavaScriptCompiler = function() {};
@@ -252,8 +250,8 @@ Handlebars.JavaScriptCompiler = function() {};
this.compileChildren(environment);
puts(environment.disassemble());
puts("")
//puts(environment.disassemble());
//puts("")
var opcodes = environment.opcodes;
var opcode, name;
@@ -317,8 +315,8 @@ Handlebars.JavaScriptCompiler = function() {};
var fn = Function.apply(this, params);
fn.displayName = "Handlebars.js"
puts(fn.toString())
puts("")
//puts(fn.toString())
//puts("")
container.render = fn;
@@ -355,11 +353,19 @@ Handlebars.JavaScriptCompiler = function() {};
}
},
nameLookup: function(parent, name) {
if(JavaScriptCompiler.RESERVED_WORDS[name]) {
return parent + "['" + name + "']";
} else {
return parent + "." + name;
}
},
lookupWithFallback: function(name) {
if(name) {
this.pushStack("currentContext." + name);
this.pushStack(this.nameLookup('currentContext', name));
var topStack = this.topStack();
this.source.push("if(" + topStack + " == null) { " + topStack + " = helpers." + name + "; }");
this.source.push("if(" + topStack + " == null) { " + topStack + " = " + this.nameLookup('helpers', name) + "; }");
} else {
this.pushStack("currentContext");
}
@@ -367,7 +373,7 @@ Handlebars.JavaScriptCompiler = function() {};
lookup: function(name) {
var topStack = this.topStack();
this.source.push(topStack + " = " + topStack + "." + name + ";");
this.source.push(topStack + " = " + this.nameLookup(topStack, name) + ";");
},
pushString: function(string) {
@@ -482,7 +488,7 @@ Handlebars.JavaScriptCompiler = function() {};
},
invokePartial: function(context) {
this.pushStack("Handlebars.VM.invokePartial(partials." + context + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
this.pushStack("Handlebars.VM.invokePartial(" + this.nameLookup('partials', context) + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
},
escape: function() {
@@ -535,6 +541,16 @@ Handlebars.JavaScriptCompiler = function() {};
}
}
var reservedWords = ("break case catch continue default delete do else finally " +
"for function if in instanceof new return switch this throw " +
"try typeof var void while with null true false").split(" ");
compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
for(var i=0, l=reservedWords.length; i<l; i++) {
compilerWords[reservedWords[i]] = true;
}
})(Handlebars.Compiler, Handlebars.JavaScriptCompiler)
Handlebars.VM = {