From d3cff64ec068dd1f4ee6ecc4ff1f801c7c12c21f Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Tue, 26 Nov 2013 18:31:02 -0800 Subject: [PATCH] dumper should use a new circular-refs cache for every call --- lib/jsdoc/util/dumper.js | 53 ++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/jsdoc/util/dumper.js b/lib/jsdoc/util/dumper.js index 69e27494..2fce415b 100644 --- a/lib/jsdoc/util/dumper.js +++ b/lib/jsdoc/util/dumper.js @@ -8,53 +8,58 @@ var util = require('util'); -var seenItems = []; -function seen(object) { - if (seenItems.indexOf(object) !== -1) { +function ObjectWalker() { + this.seenItems = []; +} + +ObjectWalker.prototype.seen = function(object) { + if (this.seenItems.indexOf(object) !== -1) { return true; } return false; -} +}; // some objects are unwalkable, like Java native objects -function isUnwalkable(o) { +ObjectWalker.prototype.isUnwalkable = function(o) { return (o && typeof o === 'object' && typeof o.constructor === 'undefined'); -} +}; -function isFunction(o) { +ObjectWalker.prototype.isFunction = function(o) { return (o && typeof o === 'function' || o instanceof Function); -} +}; -function isObject(o) { +ObjectWalker.prototype.isObject = function(o) { return o && o instanceof Object || (o && typeof o.constructor !== 'undefined' && o.constructor.name === 'Object'); -} +}; -function checkCircularRefs(o, func) { - if ( seen(o) ) { +ObjectWalker.prototype.checkCircularRefs = function(o, func) { + if ( this.seen(o) ) { return ''; } else { - seenItems.push(o); + this.seenItems.push(o); return func(o); } -} +}; -function walk(o) { +ObjectWalker.prototype.walk = function(o) { var result; - if ( isUnwalkable(o) ) { + var self = this; + + if ( this.isUnwalkable(o) ) { result = ''; } else if ( o === undefined ) { result = 'undefined'; } else if ( Array.isArray(o) ) { - result = checkCircularRefs(o, function(arr) { + result = this.checkCircularRefs(o, function(arr) { var newArray = []; arr.forEach(function(item) { - newArray.push( walk(item) ); + newArray.push( self.walk(item) ); }); return newArray; @@ -69,14 +74,14 @@ function walk(o) { else if ( util.isError(o) ) { result = { message: o.message }; } - else if ( isFunction(o) ) { + else if ( this.isFunction(o) ) { result = ''; } - else if ( isObject(o) && o !== null ) { - result = checkCircularRefs(o, function(obj) { + else if ( this.isObject(o) && o !== null ) { + result = this.checkCircularRefs(o, function(obj) { var newObj = {}; Object.keys(obj).forEach(function(key) { - newObj[key] = walk(obj[key]); + newObj[key] = self.walk(obj[key]); }); return newObj; @@ -88,11 +93,11 @@ function walk(o) { } return result; -} +}; /** * @param {*} object */ exports.dump = function(object) { - return JSON.stringify(walk(object), null, 4); + return JSON.stringify(new ObjectWalker().walk(object), null, 4); };