hbs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ require.paths.push("lib");
2
+ require.paths.push("vendor");
3
+ require.paths.push("vendor/dustjs/lib");
4
+ require.paths.push("vendor/coffee/lib");
5
+ require.paths.push("vendor/eco/lib");
6
+
7
+
8
+ var BenchWarmer = require("./benchwarmer");
9
+ Handlebars = require("handlebars");
10
+
11
+ var dust = require("dust");
12
+ var Mustache = require("mustache");
13
+ var ecoExports = require("eco");
14
+
15
+ eco = function(str) {
16
+ var module = {};
17
+ var template = new Function("module", ecoExports.compile(str));
18
+ template(module);
19
+ return module.exports;
20
+ }
21
+
22
+ var benchDetails = {
23
+ string: {
24
+ context: {},
25
+ handlebars: "Hello world",
26
+ dust: "Hello world",
27
+ mustache: "Hello world",
28
+ eco: "Hello world"
29
+ },
30
+ variables: {
31
+ context: {name: "Mick", count: 30},
32
+ handlebars: "Hello {{name}}! You have {{count}} new messages.",
33
+ dust: "Hello {name}! You have {count} new messages.",
34
+ mustache: "Hello {{name}}! You have {{count}} new messages.",
35
+ eco: "Hello <%= @name %>! You have <%= @count %> new messages."
36
+ },
37
+ object: {
38
+ context: { person: { name: "Larry", age: 45 } },
39
+ handlebars: "{{#with person}}{{name}}{{age}}{{/with}}",
40
+ dust: "{#person}{name}{age}{/person}",
41
+ mustache: "{{#person}}{{name}}{{age}}{{/person}}"
42
+ },
43
+ array: {
44
+ context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] },
45
+ handlebars: "{{#each names}}{{name}}{{/each}}",
46
+ dust: "{#names}{name}{/names}",
47
+ mustache: "{{#names}}{{name}}{{/names}}",
48
+ eco: "<% for item in @names: %><%= item.name %><% end %>"
49
+ },
50
+ partial: {
51
+ context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
52
+ partials: {
53
+ mustache: { variables: "Hello {{name}}! You have {{count}} new messages." },
54
+ handlebars: { variables: "Hello {{name}}! You have {{count}} new messages." }
55
+ },
56
+ handlebars: "{{#each peeps}}{{>variables}}{{/each}}",
57
+ dust: "{#peeps}{>variables/}{/peeps}",
58
+ mustache: "{{#peeps}}{{>variables}}{{/peeps}}"
59
+ },
60
+ recursion: {
61
+ context: { name: '1', kids: [{ name: '1.1', kids: [{name: '1.1.1', kids: []}] }] },
62
+ partials: {
63
+ mustache: { recursion: "{{name}}{{#kids}}{{>recursion}}{{/kids}}" },
64
+ handlebars: { recursion: "{{name}}{{#each kids}}{{>recursion}}{{/each}}" }
65
+ },
66
+ handlebars: "{{name}}{{#each kids}}{{>recursion}}{{/each}}",
67
+ dust: "{name}{#kids}{>recursion:./}{/kids}",
68
+ mustache: "{{name}}{{#kids}}{{>recursion}}{{/kids}}"
69
+ },
70
+ complex: {
71
+ handlebars: "<h1>{{header}}</h1>{{#if items}}<ul>{{#each items}}{{#if current}}" +
72
+ "<li><strong>{{name}}</strong></li>{{^}}" +
73
+ "<li><a href=\"{{url}}\">{{name}}</a></li>{{/if}}" +
74
+ "{{/each}}</ul>{{^}}<p>The list is empty.</p>{{/if}}",
75
+
76
+ dust: "<h1>{header}</h1>\n" +
77
+ "{?items}\n" +
78
+ " <ul>\n" +
79
+ " {#items}\n" +
80
+ " {#current}\n" +
81
+ " <li><strong>{name}</strong></li>\n" +
82
+ " {:else}\n" +
83
+ " <li><a href=\"{url}\">{name}</a></li>\n" +
84
+ " {/current}\n" +
85
+ " {/items}\n" +
86
+ " </ul>\n" +
87
+ "{:else}\n" +
88
+ " <p>The list is empty.</p>\n" +
89
+ "{/items}",
90
+ context: {
91
+ header: function() {
92
+ return "Colors";
93
+ },
94
+ items: [
95
+ {name: "red", current: true, url: "#Red"},
96
+ {name: "green", current: false, url: "#Green"},
97
+ {name: "blue", current: false, url: "#Blue"}
98
+ ]
99
+ }
100
+ }
101
+
102
+ };
103
+
104
+ handlebarsTemplates = {};
105
+ ecoTemplates = {};
106
+
107
+ var warmer = new BenchWarmer();
108
+
109
+ var makeSuite = function(name) {
110
+ warmer.suite(name, function(bench) {
111
+ var templateName = name;
112
+ var details = benchDetails[templateName];
113
+ var mustachePartials = details.partials && details.partials.mustache;
114
+ var mustacheSource = details.mustache;
115
+ var context = details.context;
116
+
117
+ var error = function() { throw new Error("EWOT"); };
118
+
119
+
120
+ //bench("dust", function() {
121
+ //dust.render(templateName, context, function(err, out) { });
122
+ //});
123
+
124
+ bench("handlebars", function() {
125
+ handlebarsTemplates[templateName](context);
126
+ });
127
+
128
+ //if(ecoTemplates[templateName]) {
129
+ //bench("eco", function() {
130
+ //ecoTemplates[templateName](context);
131
+ //});
132
+ //} else {
133
+ //bench("eco", error);
134
+ //}
135
+
136
+ //if(mustacheSource) {
137
+ //bench("mustache", function() {
138
+ //Mustache.to_html(mustacheSource, context, mustachePartials);
139
+ //});
140
+ //} else {
141
+ //bench("mustache", error);
142
+ //}
143
+ });
144
+ }
145
+
146
+ for(var name in benchDetails) {
147
+ if(benchDetails.hasOwnProperty(name)) {
148
+ dust.loadSource(dust.compile(benchDetails[name].dust, name));
149
+ handlebarsTemplates[name] = Handlebars.compile(benchDetails[name].handlebars);
150
+
151
+ if(benchDetails[name].eco) { ecoTemplates[name] = eco(benchDetails[name].eco); }
152
+
153
+ var partials = benchDetails[name].partials;
154
+ if(partials) {
155
+ for(var partialName in partials.handlebars) {
156
+ if(partials.handlebars.hasOwnProperty(partialName)) {
157
+ Handlebars.registerPartial(partialName, partials.handlebars[partialName]);
158
+ }
159
+ }
160
+ }
161
+
162
+ makeSuite(name);
163
+ }
164
+ }
165
+
166
+ warmer.bench();
@@ -0,0 +1,15 @@
1
+ var Handlebars = require("handlebars/base");
2
+ module.exports = Handlebars;
3
+
4
+ require("handlebars/utils");
5
+
6
+ require("handlebars/ast");
7
+ require("handlebars/printer");
8
+ require("handlebars/visitor");
9
+
10
+ require("handlebars/compiler");
11
+
12
+ // BEGIN(BROWSER)
13
+
14
+ // END(BROWSER)
15
+
@@ -0,0 +1,103 @@
1
+ var Handlebars = require("handlebars");
2
+
3
+ // BEGIN(BROWSER)
4
+ (function() {
5
+
6
+ Handlebars.AST = {};
7
+
8
+ Handlebars.AST.ProgramNode = function(statements, inverse) {
9
+ this.type = "program";
10
+ this.statements = statements;
11
+ if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
12
+ };
13
+
14
+ Handlebars.AST.MustacheNode = function(params, hash, unescaped) {
15
+ this.type = "mustache";
16
+ this.id = params[0];
17
+ this.params = params.slice(1);
18
+ this.hash = hash;
19
+ this.escaped = !unescaped;
20
+ };
21
+
22
+ Handlebars.AST.PartialNode = function(id, context) {
23
+ this.type = "partial";
24
+
25
+ // TODO: disallow complex IDs
26
+
27
+ this.id = id;
28
+ this.context = context;
29
+ };
30
+
31
+ var verifyMatch = function(open, close) {
32
+ if(open.original !== close.original) {
33
+ throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
34
+ }
35
+ };
36
+
37
+ Handlebars.AST.BlockNode = function(mustache, program, close) {
38
+ verifyMatch(mustache.id, close);
39
+ this.type = "block";
40
+ this.mustache = mustache;
41
+ this.program = program;
42
+ };
43
+
44
+ Handlebars.AST.InverseNode = function(mustache, program, close) {
45
+ verifyMatch(mustache.id, close);
46
+ this.type = "inverse";
47
+ this.mustache = mustache;
48
+ this.program = program;
49
+ };
50
+
51
+ Handlebars.AST.ContentNode = function(string) {
52
+ this.type = "content";
53
+ this.string = string;
54
+ };
55
+
56
+ Handlebars.AST.HashNode = function(pairs) {
57
+ this.type = "hash";
58
+ this.pairs = pairs;
59
+ };
60
+
61
+ Handlebars.AST.IdNode = function(parts) {
62
+ this.type = "ID";
63
+ this.original = parts.join(".");
64
+
65
+ var dig = [], depth = 0;
66
+
67
+ for(var i=0,l=parts.length; i<l; i++) {
68
+ var part = parts[i];
69
+
70
+ if(part === "..") { depth++; }
71
+ else if(part === "." || part === "this") { continue; }
72
+ else { dig.push(part); }
73
+ }
74
+
75
+ this.parts = dig;
76
+ this.string = dig.join('.');
77
+ this.depth = depth;
78
+ this.isSimple = (dig.length === 1) && (depth === 0);
79
+ };
80
+
81
+ Handlebars.AST.StringNode = function(string) {
82
+ this.type = "STRING";
83
+ this.string = string;
84
+ };
85
+
86
+ Handlebars.AST.IntegerNode = function(integer) {
87
+ this.type = "INTEGER";
88
+ this.integer = integer;
89
+ };
90
+
91
+ Handlebars.AST.BooleanNode = function(bool) {
92
+ this.type = "BOOLEAN";
93
+ this.bool = bool;
94
+ };
95
+
96
+ Handlebars.AST.CommentNode = function(comment) {
97
+ this.type = "comment";
98
+ this.comment = comment;
99
+ };
100
+
101
+ })();
102
+ // END(BROWSER)
103
+
@@ -0,0 +1,114 @@
1
+ var handlebars = require("handlebars/parser").parser;
2
+
3
+ // BEGIN(BROWSER)
4
+ var Handlebars = {};
5
+
6
+ Handlebars.VERSION = "1.0.beta.2";
7
+
8
+ Handlebars.Parser = handlebars;
9
+
10
+ Handlebars.parse = function(string) {
11
+ Handlebars.Parser.yy = Handlebars.AST;
12
+ return Handlebars.Parser.parse(string);
13
+ };
14
+
15
+ Handlebars.print = function(ast) {
16
+ return new Handlebars.PrintVisitor().accept(ast);
17
+ };
18
+
19
+ Handlebars.helpers = {};
20
+ Handlebars.partials = {};
21
+
22
+ Handlebars.registerHelper = function(name, fn, inverse) {
23
+ if(inverse) { fn.not = inverse; }
24
+ this.helpers[name] = fn;
25
+ };
26
+
27
+ Handlebars.registerPartial = function(name, str) {
28
+ this.partials[name] = str;
29
+ };
30
+
31
+ Handlebars.registerHelper('helperMissing', function(arg) {
32
+ if(arguments.length === 2) {
33
+ return undefined;
34
+ } else {
35
+ throw new Error("Could not find property '" + arg + "'");
36
+ }
37
+ });
38
+
39
+ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
40
+ var inverse = options.inverse || function() {}, fn = options.fn;
41
+
42
+
43
+ var ret = "";
44
+ var type = Object.prototype.toString.call(context);
45
+
46
+ if(type === "[object Function]") {
47
+ context = context();
48
+ }
49
+
50
+ if(context === true) {
51
+ return fn(this);
52
+ } else if(context === false || context == null) {
53
+ return inverse(this);
54
+ } else if(type === "[object Array]") {
55
+ if(context.length > 0) {
56
+ for(var i=0, j=context.length; i<j; i++) {
57
+ ret = ret + fn(context[i]);
58
+ }
59
+ } else {
60
+ ret = inverse(this);
61
+ }
62
+ return ret;
63
+ } else {
64
+ return fn(context);
65
+ }
66
+ });
67
+
68
+ Handlebars.registerHelper('each', function(context, options) {
69
+ var fn = options.fn, inverse = options.inverse;
70
+ var ret = "";
71
+
72
+ if(context && context.length > 0) {
73
+ for(var i=0, j=context.length; i<j; i++) {
74
+ ret = ret + fn(context[i]);
75
+ }
76
+ } else {
77
+ ret = inverse(this);
78
+ }
79
+ return ret;
80
+ });
81
+
82
+ Handlebars.registerHelper('if', function(context, options) {
83
+ if(!context || Handlebars.Utils.isEmpty(context)) {
84
+ return options.inverse(this);
85
+ } else {
86
+ return options.fn(this);
87
+ }
88
+ });
89
+
90
+ Handlebars.registerHelper('unless', function(context, options) {
91
+ var fn = options.fn, inverse = options.inverse;
92
+ options.fn = inverse;
93
+ options.inverse = fn;
94
+
95
+ return Handlebars.helpers['if'].call(this, context, options);
96
+ });
97
+
98
+ Handlebars.registerHelper('with', function(context, options) {
99
+ return options.fn(context);
100
+ });
101
+
102
+ Handlebars.logger = {
103
+ DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
104
+
105
+ // override in the host environment
106
+ log: function(level, str) {}
107
+ };
108
+
109
+ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
110
+
111
+ // END(BROWSER)
112
+
113
+ module.exports = Handlebars;
114
+
@@ -0,0 +1,739 @@
1
+ var Handlebars = require("handlebars");
2
+
3
+ // BEGIN(BROWSER)
4
+ Handlebars.Compiler = function() {};
5
+ Handlebars.JavaScriptCompiler = function() {};
6
+
7
+ (function(Compiler, JavaScriptCompiler) {
8
+ Compiler.OPCODE_MAP = {
9
+ appendContent: 1,
10
+ getContext: 2,
11
+ lookupWithHelpers: 3,
12
+ lookup: 4,
13
+ append: 5,
14
+ invokeMustache: 6,
15
+ appendEscaped: 7,
16
+ pushString: 8,
17
+ truthyOrFallback: 9,
18
+ functionOrFallback: 10,
19
+ invokeProgram: 11,
20
+ invokePartial: 12,
21
+ push: 13,
22
+ assignToHash: 15,
23
+ pushStringParam: 16
24
+ };
25
+
26
+ Compiler.MULTI_PARAM_OPCODES = {
27
+ appendContent: 1,
28
+ getContext: 1,
29
+ lookupWithHelpers: 1,
30
+ lookup: 1,
31
+ invokeMustache: 2,
32
+ pushString: 1,
33
+ truthyOrFallback: 1,
34
+ functionOrFallback: 1,
35
+ invokeProgram: 2,
36
+ invokePartial: 1,
37
+ push: 1,
38
+ assignToHash: 1,
39
+ pushStringParam: 1
40
+ };
41
+
42
+ Compiler.DISASSEMBLE_MAP = {};
43
+
44
+ for(var prop in Compiler.OPCODE_MAP) {
45
+ var value = Compiler.OPCODE_MAP[prop];
46
+ Compiler.DISASSEMBLE_MAP[value] = prop;
47
+ }
48
+
49
+ Compiler.multiParamSize = function(code) {
50
+ return Compiler.MULTI_PARAM_OPCODES[Compiler.DISASSEMBLE_MAP[code]];
51
+ };
52
+
53
+ Compiler.prototype = {
54
+ compiler: Compiler,
55
+
56
+ disassemble: function() {
57
+ var opcodes = this.opcodes, opcode, nextCode;
58
+ var out = [], str, name, value;
59
+
60
+ for(var i=0, l=opcodes.length; i<l; i++) {
61
+ opcode = opcodes[i];
62
+
63
+ if(opcode === 'DECLARE') {
64
+ name = opcodes[++i];
65
+ value = opcodes[++i];
66
+ out.push("DECLARE " + name + " = " + value);
67
+ } else {
68
+ str = Compiler.DISASSEMBLE_MAP[opcode];
69
+
70
+ var extraParams = Compiler.multiParamSize(opcode);
71
+ var codes = [];
72
+
73
+ for(var j=0; j<extraParams; j++) {
74
+ nextCode = opcodes[++i];
75
+
76
+ if(typeof nextCode === "string") {
77
+ nextCode = "\"" + nextCode.replace("\n", "\\n") + "\"";
78
+ }
79
+
80
+ codes.push(nextCode);
81
+ }
82
+
83
+ str = str + " " + codes.join(" ");
84
+
85
+ out.push(str);
86
+ }
87
+ }
88
+
89
+ return out.join("\n");
90
+ },
91
+
92
+ guid: 0,
93
+
94
+ compile: function(program, options) {
95
+ this.children = [];
96
+ this.depths = {list: []};
97
+ this.options = options || {};
98
+ return this.program(program);
99
+ },
100
+
101
+ accept: function(node) {
102
+ return this[node.type](node);
103
+ },
104
+
105
+ program: function(program) {
106
+ var statements = program.statements, statement;
107
+ this.opcodes = [];
108
+
109
+ for(var i=0, l=statements.length; i<l; i++) {
110
+ statement = statements[i];
111
+ this[statement.type](statement);
112
+ }
113
+
114
+ this.depths.list = this.depths.list.sort(function(a, b) {
115
+ return a - b;
116
+ });
117
+
118
+ return this;
119
+ },
120
+
121
+ compileProgram: function(program) {
122
+ var result = new this.compiler().compile(program, this.options);
123
+ var guid = this.guid++;
124
+
125
+ this.usePartial = this.usePartial || result.usePartial;
126
+
127
+ this.children[guid] = result;
128
+
129
+ for(var i=0, l=result.depths.list.length; i<l; i++) {
130
+ depth = result.depths.list[i];
131
+
132
+ if(depth < 2) { continue; }
133
+ else { this.addDepth(depth - 1); }
134
+ }
135
+
136
+ return guid;
137
+ },
138
+
139
+ block: function(block) {
140
+ var mustache = block.mustache;
141
+ var depth, child, inverse, inverseGuid;
142
+
143
+ var params = this.setupStackForMustache(mustache);
144
+
145
+ var programGuid = this.compileProgram(block.program);
146
+
147
+ if(block.program.inverse) {
148
+ inverseGuid = this.compileProgram(block.program.inverse);
149
+ this.declare('inverse', inverseGuid);
150
+ }
151
+
152
+ this.opcode('invokeProgram', programGuid, params.length);
153
+ this.declare('inverse', null);
154
+ this.opcode('append');
155
+ },
156
+
157
+ inverse: function(block) {
158
+ var params = this.setupStackForMustache(block.mustache);
159
+
160
+ var programGuid = this.compileProgram(block.program);
161
+
162
+ this.declare('inverse', programGuid);
163
+
164
+ this.opcode('invokeProgram', null, params.length);
165
+ this.opcode('append');
166
+ },
167
+
168
+ hash: function(hash) {
169
+ var pairs = hash.pairs, pair, val;
170
+
171
+ this.opcode('push', '{}');
172
+
173
+ for(var i=0, l=pairs.length; i<l; i++) {
174
+ pair = pairs[i];
175
+ val = pair[1];
176
+
177
+ this.accept(val);
178
+ this.opcode('assignToHash', pair[0]);
179
+ }
180
+ },
181
+
182
+ partial: function(partial) {
183
+ var id = partial.id;
184
+ this.usePartial = true;
185
+
186
+ if(partial.context) {
187
+ this.ID(partial.context);
188
+ } else {
189
+ this.opcode('push', 'context');
190
+ }
191
+
192
+ this.opcode('invokePartial', id.original);
193
+ this.opcode('append');
194
+ },
195
+
196
+ content: function(content) {
197
+ this.opcode('appendContent', content.string);
198
+ },
199
+
200
+ mustache: function(mustache) {
201
+ var params = this.setupStackForMustache(mustache);
202
+
203
+ this.opcode('invokeMustache', params.length, mustache.id.original);
204
+
205
+ if(mustache.escaped) {
206
+ this.opcode('appendEscaped');
207
+ } else {
208
+ this.opcode('append');
209
+ }
210
+ },
211
+
212
+ ID: function(id) {
213
+ this.addDepth(id.depth);
214
+
215
+ this.opcode('getContext', id.depth);
216
+
217
+ this.opcode('lookupWithHelpers', id.parts[0] || null);
218
+
219
+ for(var i=1, l=id.parts.length; i<l; i++) {
220
+ this.opcode('lookup', id.parts[i]);
221
+ }
222
+ },
223
+
224
+ STRING: function(string) {
225
+ this.opcode('pushString', string.string);
226
+ },
227
+
228
+ INTEGER: function(integer) {
229
+ this.opcode('push', integer.integer);
230
+ },
231
+
232
+ BOOLEAN: function(bool) {
233
+ this.opcode('push', bool.bool);
234
+ },
235
+
236
+ comment: function() {},
237
+
238
+ // HELPERS
239
+ pushParams: function(params) {
240
+ var i = params.length, param;
241
+
242
+ while(i--) {
243
+ param = params[i];
244
+
245
+ if(this.options.stringParams) {
246
+ if(param.depth) {
247
+ this.addDepth(param.depth);
248
+ }
249
+
250
+ this.opcode('getContext', param.depth || 0);
251
+ this.opcode('pushStringParam', param.string);
252
+ } else {
253
+ this[param.type](param);
254
+ }
255
+ }
256
+ },
257
+
258
+ opcode: function(name, val1, val2) {
259
+ this.opcodes.push(Compiler.OPCODE_MAP[name]);
260
+ if(val1 !== undefined) { this.opcodes.push(val1); }
261
+ if(val2 !== undefined) { this.opcodes.push(val2); }
262
+ },
263
+
264
+ declare: function(name, value) {
265
+ this.opcodes.push('DECLARE');
266
+ this.opcodes.push(name);
267
+ this.opcodes.push(value);
268
+ },
269
+
270
+ addDepth: function(depth) {
271
+ if(depth === 0) { return; }
272
+
273
+ if(!this.depths[depth]) {
274
+ this.depths[depth] = true;
275
+ this.depths.list.push(depth);
276
+ }
277
+ },
278
+
279
+ setupStackForMustache: function(mustache) {
280
+ var params = mustache.params;
281
+
282
+ this.pushParams(params);
283
+
284
+ if(mustache.hash) {
285
+ this.hash(mustache.hash);
286
+ } else {
287
+ this.opcode('push', '{}');
288
+ }
289
+
290
+ this.ID(mustache.id);
291
+
292
+ return params;
293
+ }
294
+ };
295
+
296
+ JavaScriptCompiler.prototype = {
297
+ // PUBLIC API: You can override these methods in a subclass to provide
298
+ // alternative compiled forms for name lookup and buffering semantics
299
+ nameLookup: function(parent, name, type) {
300
+ if(JavaScriptCompiler.RESERVED_WORDS[name] || name.indexOf('-') !== -1 || !isNaN(name)) {
301
+ return parent + "['" + name + "']";
302
+ } else if (/^[0-9]+$/.test(name)) {
303
+ return parent + "[" + name + "]";
304
+ } else {
305
+ return parent + "." + name;
306
+ }
307
+ },
308
+
309
+ appendToBuffer: function(string) {
310
+ return "buffer = buffer + " + string + ";";
311
+ },
312
+
313
+ initializeBuffer: function() {
314
+ return this.quotedString("");
315
+ },
316
+ // END PUBLIC API
317
+
318
+ compile: function(environment, options) {
319
+ this.environment = environment;
320
+ this.options = options || {};
321
+
322
+ this.preamble();
323
+
324
+ this.stackSlot = 0;
325
+ this.stackVars = [];
326
+ this.registers = {list: []};
327
+
328
+ this.compileChildren(environment, options);
329
+
330
+ Handlebars.log(Handlebars.logger.DEBUG, environment.disassemble() + "\n\n");
331
+
332
+ var opcodes = environment.opcodes, opcode, name, declareName, declareVal;
333
+
334
+ this.i = 0;
335
+
336
+ for(l=opcodes.length; this.i<l; this.i++) {
337
+ opcode = this.nextOpcode(0);
338
+
339
+ if(opcode[0] === 'DECLARE') {
340
+ this.i = this.i + 2;
341
+ this[opcode[1]] = opcode[2];
342
+ } else {
343
+ this.i = this.i + opcode[1].length;
344
+ this[opcode[0]].apply(this, opcode[1]);
345
+ }
346
+ }
347
+
348
+ return this.createFunction();
349
+ },
350
+
351
+ nextOpcode: function(n) {
352
+ var opcodes = this.environment.opcodes, opcode = opcodes[this.i + n], name, val;
353
+ var extraParams, codes;
354
+
355
+ if(opcode === 'DECLARE') {
356
+ name = opcodes[this.i + 1];
357
+ val = opcodes[this.i + 2];
358
+ return ['DECLARE', name, val];
359
+ } else {
360
+ name = Compiler.DISASSEMBLE_MAP[opcode];
361
+
362
+ extraParams = Compiler.multiParamSize(opcode);
363
+ codes = [];
364
+
365
+ for(var j=0; j<extraParams; j++) {
366
+ codes.push(opcodes[this.i + j + 1 + n]);
367
+ }
368
+
369
+ return [name, codes];
370
+ }
371
+ },
372
+
373
+ eat: function(opcode) {
374
+ this.i = this.i + opcode.length;
375
+ },
376
+
377
+ preamble: function() {
378
+ var out = [];
379
+ out.push("var buffer = " + this.initializeBuffer() + ", currentContext = context");
380
+
381
+ var copies = "helpers = helpers || Handlebars.helpers;";
382
+ if(this.environment.usePartial) { copies = copies + " partials = partials || Handlebars.partials;"; }
383
+ out.push(copies);
384
+
385
+ // track the last context pushed into place to allow skipping the
386
+ // getContext opcode when it would be a noop
387
+ this.lastContext = 0;
388
+ this.source = out;
389
+ },
390
+
391
+ createFunction: function() {
392
+ var container = {
393
+ escapeExpression: Handlebars.Utils.escapeExpression,
394
+ invokePartial: Handlebars.VM.invokePartial,
395
+ programs: [],
396
+ program: function(i, helpers, partials, data) {
397
+ var programWrapper = this.programs[i];
398
+ if(data) {
399
+ return Handlebars.VM.program(this.children[i], helpers, partials, data);
400
+ } else if(programWrapper) {
401
+ return programWrapper;
402
+ } else {
403
+ programWrapper = this.programs[i] = Handlebars.VM.program(this.children[i], helpers, partials);
404
+ return programWrapper;
405
+ }
406
+ },
407
+ programWithDepth: Handlebars.VM.programWithDepth,
408
+ noop: Handlebars.VM.noop
409
+ };
410
+ var locals = this.stackVars.concat(this.registers.list);
411
+
412
+ if(locals.length > 0) {
413
+ this.source[0] = this.source[0] + ", " + locals.join(", ");
414
+ }
415
+
416
+ this.source[0] = this.source[0] + ";";
417
+
418
+ this.source.push("return buffer;");
419
+
420
+ var params = ["Handlebars", "context", "helpers", "partials"];
421
+
422
+ if(this.options.data) { params.push("data"); }
423
+
424
+ for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
425
+ params.push("depth" + this.environment.depths.list[i]);
426
+ }
427
+
428
+ if(params.length === 4 && !this.environment.usePartial) { params.pop(); }
429
+
430
+ params.push(this.source.join("\n"));
431
+
432
+ var fn = Function.apply(this, params);
433
+ fn.displayName = "Handlebars.js";
434
+
435
+ Handlebars.log(Handlebars.logger.DEBUG, fn.toString() + "\n\n");
436
+
437
+ container.render = fn;
438
+
439
+ container.children = this.environment.children;
440
+
441
+ return function(context, options, $depth) {
442
+ options = options || {};
443
+ var args = [Handlebars, context, options.helpers, options.partials, options.data];
444
+ var depth = Array.prototype.slice.call(arguments, 2);
445
+ args = args.concat(depth);
446
+ return container.render.apply(container, args);
447
+ };
448
+ },
449
+
450
+ appendContent: function(content) {
451
+ this.source.push(this.appendToBuffer(this.quotedString(content)));
452
+ },
453
+
454
+ append: function() {
455
+ var local = this.popStack();
456
+ this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
457
+ },
458
+
459
+ appendEscaped: function() {
460
+ var opcode = this.nextOpcode(1), extra = "";
461
+
462
+ if(opcode[0] === 'appendContent') {
463
+ extra = " + " + this.quotedString(opcode[1][0]);
464
+ this.eat(opcode);
465
+ }
466
+
467
+ this.source.push(this.appendToBuffer("this.escapeExpression(" + this.popStack() + ")" + extra));
468
+ },
469
+
470
+ getContext: function(depth) {
471
+ if(this.lastContext !== depth) {
472
+ this.lastContext = depth;
473
+
474
+ if(depth === 0) {
475
+ this.source.push("currentContext = context;");
476
+ } else {
477
+ this.source.push("currentContext = depth" + depth + ";");
478
+ }
479
+ }
480
+ },
481
+
482
+ lookupWithHelpers: function(name) {
483
+ if(name) {
484
+ var topStack = this.nextStack();
485
+
486
+ var toPush = "if('" + name + "' in helpers) { " + topStack +
487
+ " = " + this.nameLookup('helpers', name, 'helper') +
488
+ "; } else { " + topStack + " = " +
489
+ this.nameLookup('currentContext', name, 'context') +
490
+ "; }";
491
+
492
+ this.source.push(toPush);
493
+ } else {
494
+ this.pushStack("currentContext");
495
+ }
496
+ },
497
+
498
+ lookup: function(name) {
499
+ var topStack = this.topStack();
500
+ this.source.push(topStack + " = " + this.nameLookup(topStack, name, 'context') + ";");
501
+ },
502
+
503
+ pushStringParam: function(string) {
504
+ this.pushStack("currentContext");
505
+ this.pushString(string);
506
+ },
507
+
508
+ pushString: function(string) {
509
+ this.pushStack(this.quotedString(string));
510
+ },
511
+
512
+ push: function(name) {
513
+ this.pushStack(name);
514
+ },
515
+
516
+ invokeMustache: function(paramSize, original) {
517
+ this.populateParams(paramSize, this.quotedString(original), "{}", null, function(nextStack, helperMissingString, id) {
518
+ this.source.push("else if(" + id + "=== undefined) { " + nextStack + " = helpers.helperMissing.call(" + helperMissingString + "); }");
519
+ this.source.push("else { " + nextStack + " = " + id + "; }");
520
+ });
521
+ },
522
+
523
+ invokeProgram: function(guid, paramSize) {
524
+ var inverse = this.programExpression(this.inverse);
525
+ var mainProgram = this.programExpression(guid);
526
+
527
+ this.populateParams(paramSize, null, mainProgram, inverse, function(nextStack, helperMissingString, id) {
528
+ this.source.push("else { " + nextStack + " = helpers.blockHelperMissing.call(" + helperMissingString + "); }");
529
+ });
530
+ },
531
+
532
+ populateParams: function(paramSize, helperId, program, inverse, fn) {
533
+ var id = this.popStack(), nextStack;
534
+ var params = [], param, stringParam;
535
+
536
+ var hash = this.popStack();
537
+
538
+ this.register('tmp1', program);
539
+ this.source.push('tmp1.hash = ' + hash + ';');
540
+
541
+ if(this.options.stringParams) {
542
+ this.source.push('tmp1.contexts = [];');
543
+ }
544
+
545
+ for(var i=0; i<paramSize; i++) {
546
+ param = this.popStack();
547
+ params.push(param);
548
+
549
+ if(this.options.stringParams) {
550
+ this.source.push('tmp1.contexts.push(' + this.popStack() + ');');
551
+ }
552
+ }
553
+
554
+ if(inverse) {
555
+ this.source.push('tmp1.fn = tmp1;');
556
+ this.source.push('tmp1.inverse = ' + inverse + ';');
557
+ }
558
+
559
+ if(this.options.data) {
560
+ this.source.push('tmp1.data = data;');
561
+ }
562
+
563
+ params.push('tmp1');
564
+
565
+ this.populateCall(params, id, helperId || id, fn);
566
+ },
567
+
568
+ populateCall: function(params, id, helperId, fn) {
569
+ var paramString = ["context"].concat(params).join(", ");
570
+ var helperMissingString = ["context"].concat(helperId).concat(params).join(", ");
571
+
572
+ nextStack = this.nextStack();
573
+
574
+ this.source.push("if(typeof " + id + " === 'function') { " + nextStack + " = " + id + ".call(" + paramString + "); }");
575
+ fn.call(this, nextStack, helperMissingString, id);
576
+ },
577
+
578
+ invokePartial: function(context) {
579
+ this.pushStack("this.invokePartial(" + this.nameLookup('partials', context, 'partial') + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
580
+ },
581
+
582
+ assignToHash: function(key) {
583
+ var value = this.popStack();
584
+ var hash = this.topStack();
585
+
586
+ this.source.push(hash + "['" + key + "'] = " + value + ";");
587
+ },
588
+
589
+ // HELPERS
590
+
591
+ compiler: JavaScriptCompiler,
592
+
593
+ compileChildren: function(environment, options) {
594
+ var children = environment.children, child, compiler;
595
+ var compiled = [];
596
+
597
+ for(var i=0, l=children.length; i<l; i++) {
598
+ child = children[i];
599
+ compiler = new this.compiler();
600
+
601
+ compiled[i] = compiler.compile(child, options);
602
+ }
603
+
604
+ environment.rawChildren = children;
605
+ environment.children = compiled;
606
+ },
607
+
608
+ programExpression: function(guid) {
609
+ if(guid == null) { return "this.noop"; }
610
+
611
+ var programParams = [guid, "helpers", "partials"];
612
+
613
+ var depths = this.environment.rawChildren[guid].depths.list;
614
+
615
+ if(this.options.data) { programParams.push("data"); }
616
+
617
+ for(var i=0, l = depths.length; i<l; i++) {
618
+ depth = depths[i];
619
+
620
+ if(depth === 1) { programParams.push("context"); }
621
+ else { programParams.push("depth" + (depth - 1)); }
622
+ }
623
+
624
+ if(!this.environment.usePartial) {
625
+ if(programParams[3]) {
626
+ programParams[2] = "null";
627
+ } else {
628
+ programParams.pop();
629
+ }
630
+ }
631
+
632
+ if(depths.length === 0) {
633
+ return "this.program(" + programParams.join(", ") + ")";
634
+ } else {
635
+ programParams[0] = "this.children[" + guid + "]";
636
+ return "this.programWithDepth(" + programParams.join(", ") + ")";
637
+ }
638
+ },
639
+
640
+ register: function(name, val) {
641
+ this.useRegister(name);
642
+ this.source.push(name + " = " + val + ";");
643
+ },
644
+
645
+ useRegister: function(name) {
646
+ if(!this.registers[name]) {
647
+ this.registers[name] = true;
648
+ this.registers.list.push(name);
649
+ }
650
+ },
651
+
652
+ pushStack: function(item) {
653
+ this.source.push(this.nextStack() + " = " + item + ";");
654
+ return "stack" + this.stackSlot;
655
+ },
656
+
657
+ nextStack: function() {
658
+ this.stackSlot++;
659
+ if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
660
+ return "stack" + this.stackSlot;
661
+ },
662
+
663
+ popStack: function() {
664
+ return "stack" + this.stackSlot--;
665
+ },
666
+
667
+ topStack: function() {
668
+ return "stack" + this.stackSlot;
669
+ },
670
+
671
+ quotedString: function(str) {
672
+ return '"' + str
673
+ .replace(/\\/g, '\\\\')
674
+ .replace(/"/g, '\\"')
675
+ .replace(/\n/g, '\\n')
676
+ .replace(/\r/g, '\\r') + '"';
677
+ }
678
+ };
679
+
680
+ var reservedWords = ("break case catch continue default delete do else finally " +
681
+ "for function if in instanceof new return switch this throw " +
682
+ "try typeof var void while with null true false").split(" ");
683
+
684
+ compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
685
+
686
+ for(var i=0, l=reservedWords.length; i<l; i++) {
687
+ compilerWords[reservedWords[i]] = true;
688
+ }
689
+
690
+ })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
691
+
692
+ Handlebars.VM = {
693
+ programWithDepth: function(fn, helpers, partials, data, $depth) {
694
+ var args = Array.prototype.slice.call(arguments, 4);
695
+
696
+ return function(context, options) {
697
+ options = options || {};
698
+
699
+ options = {
700
+ helpers: options.helpers || helpers,
701
+ partials: options.partials || partials,
702
+ data: options.data || data
703
+ };
704
+
705
+ return fn.apply(this, [context, options].concat(args));
706
+ };
707
+ },
708
+ program: function(fn, helpers, partials, data) {
709
+ return function(context, options) {
710
+ options = options || {};
711
+
712
+ return fn(context, {
713
+ helpers: options.helpers || helpers,
714
+ partials: options.partials || partials,
715
+ data: options.data || data
716
+ });
717
+ };
718
+ },
719
+ noop: function() { return ""; },
720
+ compile: function(string, options) {
721
+ var ast = Handlebars.parse(string);
722
+ var environment = new Handlebars.Compiler().compile(ast, options);
723
+ return new Handlebars.JavaScriptCompiler().compile(environment, options);
724
+ },
725
+ invokePartial: function(partial, name, context, helpers, partials) {
726
+ if(partial === undefined) {
727
+ throw new Handlebars.Exception("The partial " + name + " could not be found");
728
+ } else if(partial instanceof Function) {
729
+ return partial(context, {helpers: helpers, partials: partials});
730
+ } else {
731
+ partials[name] = Handlebars.VM.compile(partial);
732
+ return partials[name](context, {helpers: helpers, partials: partials});
733
+ }
734
+ }
735
+ };
736
+
737
+ Handlebars.compile = Handlebars.VM.compile;
738
+ // END(BROWSER)
739
+