couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jch...@apache.org
Subject svn commit: r996269 - in /couchdb/branches/1.0.x/share: server/util.js www/script/test/design_docs.js
Date Sun, 12 Sep 2010 09:03:39 GMT
Author: jchris
Date: Sun Sep 12 09:03:38 2010
New Revision: 996269

URL: http://svn.apache.org/viewvc?rev=996269&view=rev
Log:
commonjs require no longer creates circular references

Modified:
    couchdb/branches/1.0.x/share/server/util.js
    couchdb/branches/1.0.x/share/www/script/test/design_docs.js

Modified: couchdb/branches/1.0.x/share/server/util.js
URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/share/server/util.js?rev=996269&r1=996268&r2=996269&view=diff
==============================================================================
--- couchdb/branches/1.0.x/share/server/util.js (original)
+++ couchdb/branches/1.0.x/share/server/util.js Sun Sep 12 09:03:38 2010
@@ -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);
-  }
-  if (!current[n]) {
-    throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(current)];
-  }
-  var p = current;
-  current = current[n];
-  current.parent = p;
-  path = path ? path + '/' + n : n;
-  return resolveModule(names, p, current, path);
+    return resolveModule(names, {
+      parent : mod.parent.parent,
+      current : mod.parent.current,
+      id : mod.id
+    });
+  } else if (root) {
+    mod = {current : root};
+  }
+  if (!mod.current[n]) {
+    throw ["error", "invalid_require_path", 'Object has no property "'+n+'". '+JSON.stringify(mod.current)];
+  }
+  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;
         }

Modified: couchdb/branches/1.0.x/share/www/script/test/design_docs.js
URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/share/www/script/test/design_docs.js?rev=996269&r1=996268&r2=996269&view=diff
==============================================================================
--- couchdb/branches/1.0.x/share/www/script/test/design_docs.js (original)
+++ couchdb/branches/1.0.x/share/www/script/test/design_docs.js Sun Sep 12 09:03:38 2010
@@ -41,8 +41,8 @@ function() {
     whatever : {
       stringzone : "exports.string = 'plankton';",
       commonjs : {
-        whynot : "exports.test = require('../stringzone')",
-        upper : "exports.testing = require('./whynot').test.string.toUpperCase()+module.id"
+        whynot : "exports.test = require('../stringzone'); exports.foo = require('whatever/stringzone');",
+        upper : "exports.testing = require('./whynot').test.string.toUpperCase()+module.id+require('./whynot').foo.string"
       }
     },
     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("PLANKTONwhatever/commonjs/upper", xhr.responseText);
+  TEquals("PLANKTONwhatever/commonjs/upperplankton", xhr.responseText);
 
   // test that we get design doc info back
   var dinfo = db.designInfo("_design/test");



Mime
View raw message