Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions lib/mu/renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var BUFFER_LENGTH = 1024 * 8;
var MAX_STACK_SIZE = 100;
var MAX_STACK_SIZE = 1000;

var parser = require('./parser');

Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions test/examples/large_array_w_arrays.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#foo}}{{#bar}}{{value}}
{{/bar}}{{/foo}}
16 changes: 16 additions & 0 deletions test/examples/large_array_w_arrays.js
Original file line number Diff line number Diff line change
@@ -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 };
}())
Loading