summaryrefslogtreecommitdiff
path: root/share
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2010-04-16 00:11:31 +0000
committerJohn Christopher Anderson <jchris@apache.org>2010-04-16 00:11:31 +0000
commitc9501ba2a878689cedb87efbb96c5b6637eb271b (patch)
tree16d021489fe0d68167b881705f0a5f8211d9a78b /share
parent9bb6096e300765a88fc4ab48a6038e8b6d78db94 (diff)
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
Diffstat (limited to 'share')
-rw-r--r--share/server/util.js30
-rw-r--r--share/www/script/test/design_docs.js4
2 files changed, 18 insertions, 16 deletions
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;
}
diff --git a/share/www/script/test/design_docs.js b/share/www/script/test/design_docs.js
index efc2e718..e62951ac 100644
--- a/share/www/script/test/design_docs.js
+++ b/share/www/script/test/design_docs.js
@@ -42,7 +42,7 @@ function() {
stringzone : "exports.string = 'plankton';",
commonjs : {
whynot : "exports.test = require('../stringzone')",
- upper : "exports.testing = require('./whynot').test.string.toUpperCase()"
+ upper : "exports.testing = require('./whynot').test.string.toUpperCase()+module.id"
}
},
views: {
@@ -86,7 +86,7 @@ function() {
// test commonjs require
var xhr = CouchDB.request("GET", "/test_suite_db/_design/test/_show/requirey");
T(xhr.status == 200);
- TEquals("PLANKTON", xhr.responseText);
+ TEquals("PLANKTONwhatever/commonjs/upper", xhr.responseText);
// test that we get design doc info back
var dinfo = db.designInfo("_design/test");