You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by jc...@apache.org on 2010/04/16 02:11:32 UTC

svn commit: r934652 - in /couchdb/trunk/share: server/util.js www/script/test/design_docs.js

Author: jchris
Date: Fri Apr 16 00:11:31 2010
New Revision: 934652

URL: http://svn.apache.org/viewvc?rev=934652&view=rev
Log:
upgrade CommonJS modules support to 1.1.1 - thanks Mikeal. closes COUCHDB-739

Modified:
    couchdb/trunk/share/server/util.js
    couchdb/trunk/share/www/script/test/design_docs.js

Modified: couchdb/trunk/share/server/util.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/server/util.js?rev=934652&r1=934651&r2=934652&view=diff
==============================================================================
--- couchdb/trunk/share/server/util.js (original)
+++ couchdb/trunk/share/server/util.js Fri Apr 16 00:11:31 2010
@@ -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, pare
     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;
         }

Modified: couchdb/trunk/share/www/script/test/design_docs.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/design_docs.js?rev=934652&r1=934651&r2=934652&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/design_docs.js (original)
+++ couchdb/trunk/share/www/script/test/design_docs.js Fri Apr 16 00:11:31 2010
@@ -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");