Permalink
Browse files

Another fix for the stack overflow problem, this covers all cases (I …

…think), and has immeasurable performance impact
  • Loading branch information...
1 parent e3c9242 commit c90de5a502051421f392dbedaac9b38f9a9069dd @pepve committed Aug 21, 2012
View
@@ -1,5 +1,5 @@
var BUFFER_LENGTH = 1024 * 8;
-var MAX_STACK_SIZE = 100;
+var MAX_STACK_SIZE = 1000;
var parser = require('./parser');
@@ -10,16 +10,15 @@ function render(tokens, context, partials, stream, callback) {
context = [context];
}
- return _render(tokens, context, partials, stream, callback);
+ return _render(tokens, context, partials, stream, { stackSize: 0 }, callback);
}
-function _render(tokens, context, partials, stream, callback) {
+function _render(tokens, context, partials, stream, stackSize, callback) {
if (tokens[0] !== 'multi') {
throw new Error('Mu - WTF did you give me? I expected mustache tokens.');
}
- var i = 1
- , stackSize = 0;
+ var i = 1;
function next() {
try {
@@ -31,7 +30,7 @@ function _render(tokens, context, partials, stream, callback) {
return;
}
- if (++stackSize % MAX_STACK_SIZE == 0) {
+ if (++stackSize.stackSize % MAX_STACK_SIZE == 0) {
process.nextTick(next);
return;
}
@@ -60,15 +59,15 @@ function _render(tokens, context, partials, stream, callback) {
case 'section':
var res = normalize(context, token[2], token[3]);
if (res) {
- return section(context, token[2], res, token[4], partials, stream, next);
+ return section(context, token[2], res, token[4], partials, stream, stackSize, next);
} else {
return next();
}
case 'inverted_section':
var res = normalize(context, token[2], token[3]);
if (!res || res.length === 0) {
- return section(context, token[2], true, token[4], partials, stream, next);
+ return section(context, token[2], true, token[4], partials, stream, stackSize, next);
} else {
return next();
}
@@ -77,7 +76,7 @@ function _render(tokens, context, partials, stream, callback) {
var partial = partials[token[2]];
// console.log(require('util').inspect(partials));
if (partial) {
- return render(partial[0].tokens, context, partials, stream, next);
+ return _render(partial[0].tokens, context, partials, stream, stackSize, next);
} else {
return next();
}
@@ -164,7 +163,7 @@ function smashContext(context) {
return obj;
}
-function section(context, name, val, tokens, partials, stream, callback) {
+function section(context, name, val, tokens, partials, stream, stackSize, callback) {
if (val instanceof Array) {
var i = 0;
@@ -173,14 +172,9 @@ function section(context, name, val, tokens, partials, stream, callback) {
if (item) {
context.push(item);
- _render(tokens, context, partials, stream, function () {
+ _render(tokens, context, partials, stream, stackSize, function () {
context.pop();
-
- if (i % MAX_STACK_SIZE == 0) {
- return process.nextTick(next);
- } else {
- next();
- }
+ next();
});
} else {
callback();
@@ -193,15 +187,15 @@ function section(context, name, val, tokens, partials, stream, callback) {
if (typeof val === 'object') {
context.push(val);
- _render(tokens, context, partials, stream, function () {
+ _render(tokens, context, partials, stream, stackSize, function () {
context.pop();
callback();
});
return;
}
if (val) {
- return _render(tokens, context, partials, stream, callback);
+ return _render(tokens, context, partials, stream, stackSize, callback);
}
return callback();
@@ -0,0 +1,2 @@
+{{#foo}}{{#bar}}{{value}}
+{{/bar}}{{/foo}}
@@ -0,0 +1,16 @@
+(function () {
+ var foo = [];
+ var bar;
+
+ for (var i = 0; i < 50; i++) {
+ bar = [];
+
+ for (var j = 0; j < 50; j++) {
+ bar.push({ value: j });
+ }
+
+ foo.push({ bar: bar });
+ }
+
+ return { foo: foo };
+}())
Oops, something went wrong.

0 comments on commit c90de5a

Please sign in to comment.