From 6fe7f17c89574c419c530bd1cefc1509ef5dbff9 Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Wed, 30 Jan 2013 22:44:30 -0800 Subject: [PATCH] Improved precompile template version check This check reduces duplicated code as well as also failing if the template was precompiled on a version before the check was added. --- dist/handlebars.js | 14 ++++++++------ dist/handlebars.runtime.js | 9 +++++++-- lib/handlebars/compiler/compiler.js | 5 +---- lib/handlebars/runtime.js | 9 +++++++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/dist/handlebars.js b/dist/handlebars.js index 072ae53a..2ee6432c 100644 --- a/dist/handlebars.js +++ b/dist/handlebars.js @@ -1381,10 +1381,7 @@ Handlebars.JavaScriptCompiler = function() {}; var source = this.mergeSource(); if (!this.isChild) { - source = "if (Handlebars.VERSION !== '"+Handlebars.VERSION+"') {\n"+ - "throw 'Template was compiled with "+Handlebars.VERSION+", but runtime is '+Handlebars.VERSION;\n"+ - "}\n"+ - source; + source = "this.compiledVersion = '"+Handlebars.VERSION+"';\n"+source; } if (asObject) { @@ -2126,12 +2123,17 @@ Handlebars.VM = { } }, programWithDepth: Handlebars.VM.programWithDepth, - noop: Handlebars.VM.noop + noop: Handlebars.VM.noop, + compiledVersion: null }; return function(context, options) { options = options || {}; - return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + if (container.compiledVersion !== Handlebars.VERSION) { + throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION; + } + return result; }; }, diff --git a/dist/handlebars.runtime.js b/dist/handlebars.runtime.js index 13d0c78e..0a023eae 100644 --- a/dist/handlebars.runtime.js +++ b/dist/handlebars.runtime.js @@ -249,12 +249,17 @@ Handlebars.VM = { } }, programWithDepth: Handlebars.VM.programWithDepth, - noop: Handlebars.VM.noop + noop: Handlebars.VM.noop, + compiledVersion: null }; return function(context, options) { options = options || {}; - return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + if (container.compiledVersion !== Handlebars.VERSION) { + throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION; + } + return result; }; }, diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index b148bbb9..e38f1575 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -539,10 +539,7 @@ Handlebars.JavaScriptCompiler = function() {}; var source = this.mergeSource(); if (!this.isChild) { - source = "if (Handlebars.VERSION !== '"+Handlebars.VERSION+"') {\n"+ - "throw 'Template was compiled with "+Handlebars.VERSION+", but runtime is '+Handlebars.VERSION;\n"+ - "}\n"+ - source; + source = "this.compiledVersion = '"+Handlebars.VERSION+"';\n"+source; } if (asObject) { diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index f29efe45..66b550f0 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -20,12 +20,17 @@ Handlebars.VM = { } }, programWithDepth: Handlebars.VM.programWithDepth, - noop: Handlebars.VM.noop + noop: Handlebars.VM.noop, + compiledVersion: null }; return function(context, options) { options = options || {}; - return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + if (container.compiledVersion !== Handlebars.VERSION) { + throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION; + } + return result; }; },