diff --git a/bench/throughput.js b/bench/throughput.js index 308446a09..d27a94d31 100644 --- a/bench/throughput.js +++ b/bench/throughput.js @@ -28,11 +28,13 @@ function makeSuite(bench, name, template, handlebarsOnly) { partials = template.partials, handlebarsOut, + compatOut, dustOut, ecoOut, mustacheOut; var handlebar = Handlebars.compile(template.handlebars, {data: false}), + compat = Handlebars.compile(template.handlebars, {data: false, compat: true}), options = {helpers: template.helpers}; _.each(template.partials && template.partials.handlebars, function(partial, name) { Handlebars.registerPartial(name, Handlebars.compile(partial, {data: false})); @@ -43,6 +45,11 @@ function makeSuite(bench, name, template, handlebarsOnly) { handlebar(context, options); }); + compatOut = compat(context, options); + bench("compat", function() { + compat(context, options); + }); + if (handlebarsOnly) { return; } @@ -107,6 +114,7 @@ function makeSuite(bench, name, template, handlebarsOnly) { } } + compare(compatOut, 'compat'); compare(dustOut, 'dust'); compare(ecoOut, 'eco'); compare(mustacheOut, 'mustache'); diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 131cb981b..ffddec432 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -214,7 +214,8 @@ Compiler.prototype = { if (partial.context) { this.accept(partial.context); } else { - this.opcode('push', 'depth0'); + this.opcode('getContext', 0); + this.opcode('pushContext'); } this.opcode('invokePartial', partialName.name, partial.indent || ''); @@ -307,7 +308,7 @@ Compiler.prototype = { // Context reference, i.e. `{{foo .}}` or `{{foo ..}}` this.opcode('pushContext'); } else { - this.opcode('lookupOnContext', id.parts, id.falsy); + this.opcode('lookupOnContext', id.parts, id.falsy, id.isScoped); } }, @@ -423,6 +424,9 @@ export function precompile(input, options, env) { if (!('data' in options)) { options.data = true; } + if (options.compat) { + options.useDepths = true; + } var ast = env.parse(input); var environment = new env.Compiler().compile(ast, options); @@ -439,6 +443,9 @@ export function compile(input, options, env) { if (!('data' in options)) { options.data = true; } + if (options.compat) { + options.useDepths = true; + } var compiled; diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index c92cf58ad..b9b858dbd 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -17,6 +17,11 @@ JavaScriptCompiler.prototype = { return parent + "['" + name + "']"; } }, + depthedLookup: function(name) { + this.aliases.lookup = 'this.lookup'; + + return 'lookup(depths, "' + name + '")'; + }, compilerInfo: function() { var revision = COMPILER_REVISION, @@ -69,6 +74,8 @@ JavaScriptCompiler.prototype = { this.compileChildren(environment, options); + this.useDepths = this.useDepths || environment.depths.list.length || this.options.compat; + var opcodes = environment.opcodes, opcode, i, @@ -115,6 +122,9 @@ JavaScriptCompiler.prototype = { if (this.options.data) { ret.useData = true; } + if (this.useDepths) { + ret.depths = true; + } if (!asObject) { ret.compiler = JSON.stringify(ret.compiler); @@ -151,8 +161,8 @@ JavaScriptCompiler.prototype = { var params = ["depth0", "helpers", "partials", "data"]; - for(var i=0, l=this.environment.depths.list.length; i