summaryrefslogtreecommitdiff
path: root/share/server/util.js
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2010-09-12 08:56:17 +0000
committerJohn Christopher Anderson <jchris@apache.org>2010-09-12 08:56:17 +0000
commite9256caf158304f1d827d5f168ef4bfc9490d59f (patch)
tree9254a7811926eb101aa9da249e684a4196fc1891 /share/server/util.js
parent7c832ed49cf55878c8582cb368d4dc5ce70a1f6a (diff)
commonjs require no longer creates circular references
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@996266 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share/server/util.js')
-rw-r--r--share/server/util.js64
1 files changed, 38 insertions, 26 deletions
diff --git a/share/server/util.js b/share/server/util.js
index 9cc464c3..77b934ed 100644
--- a/share/server/util.js
+++ b/share/server/util.js
@@ -10,36 +10,50 @@
// License for the specific language governing permissions and limitations under
// the License.
-var resolveModule = function(names, parent, current, path) {
+var resolveModule = function(names, mod, root) {
if (names.length == 0) {
- if (typeof current != "string") {
+ if (typeof mod.current != "string") {
throw ["error","invalid_require_path",
- 'Must require a JavaScript string, not: '+(typeof current)];
+ 'Must require a JavaScript string, not: '+(typeof mod.current)];
+ }
+ return {
+ current : mod.current,
+ parent : mod.parent,
+ id : mod.id,
+ exports : {}
}
- return [current, parent, path];
}
// we need to traverse the path
var n = names.shift();
if (n == '..') {
- if (!(parent && parent.parent)) {
- throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
+ if (!(mod.parent && mod.parent.parent)) {
+ throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(mod.current)];
}
- path = path.slice(0, path.lastIndexOf('/'));
- return resolveModule(names, parent.parent.parent, parent.parent, path);
+ return resolveModule(names, {
+ id : mod.id.slice(0, mod.id.lastIndexOf('/')),
+ parent : mod.parent.parent.parent,
+ current : mod.parent.parent.current
+ });
} else if (n == '.') {
- if (!parent) {
- throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(current)];
+ if (!mod.parent) {
+ throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(mod.current)];
}
- return resolveModule(names, parent.parent, parent, path);
+ return resolveModule(names, {
+ parent : mod.parent.parent,
+ current : mod.parent.current,
+ id : mod.id
+ });
+ } else if (root) {
+ mod = {current : root};
}
- if (!current[n]) {
- throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
+ if (!mod.current[n]) {
+ throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(mod.current)];
}
- var p = current;
- current = current[n];
- current.parent = p;
- path = path ? path + '/' + n : n;
- return resolveModule(names, p, current, path);
+ return resolveModule(names, {
+ current : mod.current[n],
+ parent : mod,
+ id : mod.id ? mod.id + '/' + n : n
+ });
};
var Couch = {
@@ -52,19 +66,17 @@ var Couch = {
try {
if (sandbox) {
if (ddoc) {
- var require = function(name, parent) {
- 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 = {};
+ 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, [module, module.exports, function(name) {return require(name, module)}]);
+ 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()];
}
- return module.exports;
+ return newModule.exports;
}
sandbox.require = require;
}