From c9501ba2a878689cedb87efbb96c5b6637eb271b Mon Sep 17 00:00:00 2001 From: John Christopher Anderson Date: Fri, 16 Apr 2010 00:11:31 +0000 Subject: upgrade CommonJS modules support to 1.1.1 - thanks Mikeal. closes COUCHDB-739 git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@934652 13f79535-47bb-0310-9956-ffa450edef68 --- share/server/util.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'share/server') diff --git a/share/server/util.js b/share/server/util.js index d017b094..9cc464c3 100644 --- a/share/server/util.js +++ b/share/server/util.js @@ -10,13 +10,13 @@ // License for the specific language governing permissions and limitations under // the License. -var resolveModule = function(names, parent, current) { +var resolveModule = function(names, parent, current, path) { if (names.length == 0) { if (typeof current != "string") { throw ["error","invalid_require_path", 'Must require a JavaScript string, not: '+(typeof current)]; } - return [current, parent]; + return [current, parent, path]; } // we need to traverse the path var n = names.shift(); @@ -24,21 +24,23 @@ var resolveModule = function(names, parent, current) { if (!(parent && parent.parent)) { throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)]; } - return resolveModule(names, parent.parent.parent, parent.parent); + path = path.slice(0, path.lastIndexOf('/')); + return resolveModule(names, parent.parent.parent, parent.parent, path); } else if (n == '.') { if (!parent) { throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)]; } - return resolveModule(names, parent.parent, parent); + return resolveModule(names, parent.parent, parent, path); } if (!current[n]) { throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)]; } - var p = current + var p = current; current = current[n]; current.parent = p; - return resolveModule(names, p, current) -} + path = path ? path + '/' + n : n; + return resolveModule(names, p, current, path); +}; var Couch = { // moving this away from global so we can move to json2.js later @@ -51,18 +53,18 @@ var Couch = { if (sandbox) { if (ddoc) { var require = function(name, parent) { - var exports = {}; - var resolved = resolveModule(name.split('/'), parent, ddoc); - var source = resolved[0]; - parent = resolved[1]; - var s = "function (exports, require) { " + source + " }"; + if (!parent) {parent = {}}; + var resolved = resolveModule(name.split('/'), parent.actual, ddoc, parent.id); + var s = "function (module, exports, require) { " + resolved[0] + " }"; + var module = {id:resolved[2], actual:resolved[1]}; + module.exports = {}; try { var func = sandbox ? evalcx(s, sandbox) : eval(s); - func.apply(sandbox, [exports, function(name) {return require(name, parent, source)}]); + func.apply(sandbox, [module, module.exports, function(name) {return require(name, module)}]); } catch(e) { throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()]; } - return exports; + return module.exports; } sandbox.require = require; } -- cgit v1.2.3