diff options
author | Robert Newson <robert.newson@cloudant.com> | 2011-06-13 14:59:55 +0100 |
---|---|---|
committer | Robert Newson <robert.newson@cloudant.com> | 2011-06-13 14:59:55 +0100 |
commit | e9a5a6f90a021db1db8a7e55ec797a4c86edcad6 (patch) | |
tree | 06d0c28969cfcf54b2f137e7407b097f73aa0f21 /couchjs/js/util.js | |
parent | 266ba88ac6ded40087e0211ad9e75e4ce64e66cb (diff) | |
parent | 3c1a0d7e2c9adef4f8b20c9df205a86e5c0feefb (diff) |
Merge CouchDB 1.1
Diffstat (limited to 'couchjs/js/util.js')
-rw-r--r-- | couchjs/js/util.js | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/couchjs/js/util.js b/couchjs/js/util.js index b55480b9..e4386701 100644 --- a/couchjs/js/util.js +++ b/couchjs/js/util.js @@ -31,16 +31,16 @@ var resolveModule = function(names, mod, root) { } return resolveModule(names, { id : mod.id.slice(0, mod.id.lastIndexOf('/')), - parent : mod.parent.parent.parent, - current : mod.parent.parent.current + parent : mod.parent.parent, + current : mod.parent.current }); } else if (n == '.') { if (!mod.parent) { throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(mod.current)]; } return resolveModule(names, { - parent : mod.parent.parent, - current : mod.parent.current, + parent : mod.parent, + current : mod.current, id : mod.id }); } else if (root) { @@ -66,17 +66,28 @@ var Couch = { try { if (sandbox) { if (ddoc) { + if (!ddoc._module_cache) { + ddoc._module_cache = {}; + } var require = function(name, module) { module = module || {}; - var newModule = resolveModule(name.split('/'), module, ddoc); - var s = "function (module, exports, require) { " + newModule.current + " }"; - try { - var func = sandbox ? evalcx(s, sandbox) : eval(s); - func.apply(sandbox, [newModule, newModule.exports, function(name) {return require(name, newModule)}]); - } catch(e) { - throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()]; + var newModule = resolveModule(name.split('/'), module.parent, ddoc); + if (!ddoc._module_cache.hasOwnProperty(newModule.id)) { + // create empty exports object before executing the module, + // stops circular requires from filling the stack + ddoc._module_cache[newModule.id] = {}; + var s = "function (module, exports, require) { " + newModule.current + " }"; + try { + var func = sandbox ? evalcx(s, sandbox) : eval(s); + func.apply(sandbox, [newModule, newModule.exports, function(name) { + return require(name, newModule); + }]); + } catch(e) { + throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()]; + } + ddoc._module_cache[newModule.id] = newModule.exports; } - return newModule.exports; + return ddoc._module_cache[newModule.id]; } sandbox.require = require; } |