summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2013-01-16 22:43:25 -0800
committerYehuda Katz <wycats@gmail.com>2013-01-16 22:43:25 -0800
commitccd6a22ea5b89afef77651517b8d0b57e5920ea1 (patch)
treef1c5860a3177ff9f7c4a045d2958bbd6b00b8a03 /lib/handlebars/compiler/compiler.js
parent5e5f0dce9c352f490f1f1e58fd7d0f76dd006cac (diff)
downloadhandlebars.js-ccd6a22ea5b89afef77651517b8d0b57e5920ea1.zip
handlebars.js-ccd6a22ea5b89afef77651517b8d0b57e5920ea1.tar.gz
handlebars.js-ccd6a22ea5b89afef77651517b8d0b57e5920ea1.tar.bz2
Add support for getting types in string mode
This makes it possible to determine whether an argument was passed as a string or as a path when implementing helpers in string mode.
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js49
1 files changed, 39 insertions, 10 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 48a3db5..e1bc01b 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('pushLiteral', '{}');
+ this.opcode('pushHash');
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('pushLiteral', '{}');
+ this.opcode('pushHash');
this.opcode('ambiguousBlockValue');
}
@@ -148,13 +148,18 @@ Handlebars.JavaScriptCompiler = function() {};
hash: function(hash) {
var pairs = hash.pairs, pair, val;
- this.opcode('push', '{}');
+ this.opcode('pushHash');
for(var i=0, l=pairs.length; i<l; i++) {
pair = pairs[i];
val = pair[1];
- this.accept(val);
+ if (this.options.stringParams) {
+ this.opcode('pushStringParam', val.stringModeValue, val.type);
+ } else {
+ this.accept(val);
+ }
+
this.opcode('assignToHash', pair[0]);
}
},
@@ -324,7 +329,7 @@ Handlebars.JavaScriptCompiler = function() {};
}
this.opcode('getContext', param.depth || 0);
- this.opcode('pushStringParam', param.string);
+ this.opcode('pushStringParam', param.stringModeValue, param.type);
} else {
this[param.type](param);
}
@@ -338,7 +343,7 @@ Handlebars.JavaScriptCompiler = function() {};
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushLiteral', '{}');
+ this.opcode('pushHash');
}
return params;
@@ -355,7 +360,7 @@ Handlebars.JavaScriptCompiler = function() {};
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushLiteral', '{}');
+ this.opcode('pushHash');
}
return params;
@@ -677,9 +682,24 @@ Handlebars.JavaScriptCompiler = function() {};
// This opcode is designed for use in string mode, which
// provides the string value of a parameter along with its
// depth rather than resolving it immediately.
- pushStringParam: function(string) {
+ pushStringParam: function(string, type) {
this.pushStackLiteral('depth' + this.lastContext);
- this.pushString(string);
+
+ this.pushString(type);
+
+ if (typeof string === 'string') {
+ this.pushString(string);
+ } else {
+ this.pushStackLiteral(string);
+ }
+ },
+
+ pushHash: function() {
+ this.push('{}');
+
+ if (this.options.stringParams) {
+ this.register('hashTypes', '{}');
+ }
},
// [pushString]
@@ -817,6 +837,12 @@ Handlebars.JavaScriptCompiler = function() {};
// and pushes the hash back onto the stack.
assignToHash: function(key) {
var value = this.popStack();
+
+ if (this.options.stringParams) {
+ var type = this.popStack();
+ this.source.push("hashTypes['" + key + "'] = " + type + ";");
+ }
+
var hash = this.topStack();
this.source.push(hash + "['" + key + "'] = " + value + ";");
@@ -962,7 +988,7 @@ Handlebars.JavaScriptCompiler = function() {};
// the params and contexts arguments are passed in arrays
// to fill in
setupParams: function(paramSize, params) {
- var options = [], contexts = [], param, inverse, program;
+ var options = [], contexts = [], types = [], param, inverse, program;
options.push("hash:" + this.popStack());
@@ -991,12 +1017,15 @@ Handlebars.JavaScriptCompiler = function() {};
params.push(param);
if(this.options.stringParams) {
+ types.push(this.popStack());
contexts.push(this.popStack());
}
}
if (this.options.stringParams) {
options.push("contexts:[" + contexts.join(",") + "]");
+ options.push("types:[" + types.join(",") + "]");
+ options.push("hashTypes:hashTypes");
}
if(this.options.data) {