diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index ff6bc476c..f0ae5ac85 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -35,7 +35,7 @@ JavaScriptCompiler.prototype = { compilerInfo: function() { var revision = COMPILER_REVISION, versions = REVISION_CHANGES[revision]; - return "this.compilerInfo = ["+revision+",'"+versions+"'];\n"; + return [revision, versions]; }, appendToBuffer: function(string) { @@ -62,6 +62,7 @@ JavaScriptCompiler.prototype = { this.options = options || {}; this.stringParams = this.options.stringParams; this.trackIds = this.options.trackIds; + this.precompile = !asObject; log('debug', this.environment.disassemble() + "\n\n"); @@ -69,14 +70,14 @@ JavaScriptCompiler.prototype = { this.isChild = !!context; this.context = context || { programs: [], - environments: [], - aliases: { } + environments: [] }; this.preamble(); this.stackSlot = 0; this.stackVars = []; + this.aliases = {}; this.registers = { list: [] }; this.hashes = []; this.compileStack = []; @@ -110,22 +111,38 @@ JavaScriptCompiler.prototype = { throw new Exception('Compile completed with content left on stack'); } - return this.createFunctionContext(asObject); - }, + var fn = this.createFunctionContext(asObject); + if (!this.isChild) { + var ret = { + compiler: this.compilerInfo(), + main: fn + }; + this.context.programs.map(function(program, index) { + if (program) { + ret[index] = program; + } + }); - preamble: function() { - var out = []; + if (this.environment.usePartial) { + ret.usePartial = true; + } + if (this.options.data) { + ret.useData = true; + } - if (!this.isChild) { - var namespace = this.namespace; + if (!asObject) { + ret.compiler = JSON.stringify(ret.compiler); + ret = this.objectLiteral(ret); + } - var copies = "helpers = this.merge(helpers, " + namespace + ".helpers);"; - if (this.environment.usePartial) { copies = copies + " partials = this.merge(partials, " + namespace + ".partials);"; } - if (this.options.data) { copies = copies + " data = this.initData(depth0, data);"; } - out.push(copies); + return ret; } else { - out.push(''); + return fn; } + }, + + preamble: function() { + var out = []; if (!this.environment.isSimple) { out.push(", buffer = " + this.initializeBuffer()); @@ -143,32 +160,25 @@ JavaScriptCompiler.prototype = { var locals = this.stackVars.concat(this.registers.list); if(locals.length > 0) { - this.source[1] = this.source[1] + ", " + locals.join(", "); + this.source[0] += ", " + locals.join(", "); } // Generate minimizer alias mappings - if (!this.isChild) { - for (var alias in this.context.aliases) { - if (this.context.aliases.hasOwnProperty(alias)) { - this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias]; - } + for (var alias in this.aliases) { + if (this.aliases.hasOwnProperty(alias)) { + this.source[0] += ', ' + alias + '=' + this.aliases[alias]; } } - if (this.source[1]) { - this.source[1] = "var " + this.source[1].substring(2) + ";"; - } - - // Merge children - if (!this.isChild) { - this.source[1] += '\n' + this.context.programs.join('\n') + '\n'; + if (this.source[0]) { + this.source[0] = "var " + this.source[0].substring(2) + ";"; } if (!this.environment.isSimple) { this.pushSource("return buffer;"); } - var params = this.isChild ? ["depth0", "data"] : [this.namespace, "depth0", "helpers", "partials", "data"]; + var params = ["depth0", "helpers", "partials", "data"]; for(var i=0, l=this.environment.depths.list.length; i= 1.0.0']; -helpers = this.merge(helpers, Handlebars.helpers); data = this.initData(depth0, data); +return templates['empty'] = template({"compiler":[4,">= 1.0.0"],"main":function(depth0,helpers,partials,data) { var buffer = ""; - - return buffer; - }); + },"useData":true}); }); diff --git a/spec/javascript-compiler.js b/spec/javascript-compiler.js index 884abf1c9..a9ae0f8a9 100644 --- a/spec/javascript-compiler.js +++ b/spec/javascript-compiler.js @@ -32,7 +32,7 @@ describe('javascript-compiler api', function() { }); it('should allow compilerInfo override', function() { handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo = function() { - return 'this.compilerInfo = "crazy";'; + return 'crazy'; }; handlebarsEnv.VM.checkRevision = function(compilerInfo) { if (compilerInfo !== 'crazy') {