Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add check to prevent the stack from overflowing. Fix #43.

commit ce52cf6129b09f61ec9d8204aaca29eee65de54e 1 parent fc77a5c
@raycmorgan authored
Showing with 15 additions and 7 deletions.
  1. +15 −7 lib/mu/renderer.js
View
22 lib/mu/renderer.js
@@ -1,4 +1,5 @@
var BUFFER_LENGTH = 1024 * 8;
+var MAX_STACK_SIZE = 100;
var parser = require('./parser');
@@ -12,16 +13,13 @@ function render(tokens, context, partials, stream, callback) {
return _render(tokens, context, partials, stream, callback);
}
-function _render() {
- process.nextTick(Function.prototype.bind.apply(__render, [this].concat(Array.prototype.slice.call(arguments))));
-}
-
-function __render(tokens, context, partials, stream, callback) {
+function _render(tokens, context, partials, stream, callback) {
if (tokens[0] !== 'multi') {
throw new Error('Mu - WTF did you give me? I expected mustache tokens.');
}
- var i = 1;
+ var i = 1
+ , stackSize = 0;
function next() {
try {
@@ -32,6 +30,11 @@ function __render(tokens, context, partials, stream, callback) {
});
return;
}
+
+ if (++stackSize % MAX_STACK_SIZE == 0) {
+ process.nextTick(next);
+ return;
+ }
var token = tokens[i++];
@@ -172,7 +175,12 @@ function section(context, name, val, tokens, partials, stream, callback) {
context.push(item);
_render(tokens, context, partials, stream, function () {
context.pop();
- next();
+
+ if (i % MAX_STACK_SIZE == 0) {
+ return process.nextTick(next);
+ } else {
+ next();
+ }
});
} else {
callback();
Please sign in to comment.
Something went wrong with that request. Please try again.