incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject js commit: Revert "Revert "Make circular require()'s and exception.""
Date Tue, 18 Sep 2012 14:45:37 GMT
Updated Branches:
  refs/heads/master aa15ac60d -> 9878d97f7


Revert "Revert "Make circular require()'s and exception.""

This reverts commit 2aa46aa0eef2ba641cf91793735152d7fb5b6998.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/9878d97f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/9878d97f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/9878d97f

Branch: refs/heads/master
Commit: 9878d97f7acc89a5e90b921a212e49e94cd0becd
Parents: aa15ac6
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Tue Sep 18 10:44:53 2012 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Sep 18 10:44:53 2012 -0400

----------------------------------------------------------------------
 lib/scripts/require.js |   19 ++++++++++++++++++-
 test/test.require.js   |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/9878d97f/lib/scripts/require.js
----------------------------------------------------------------------
diff --git a/lib/scripts/require.js b/lib/scripts/require.js
index 9f5d8fc..3735eac 100644
--- a/lib/scripts/require.js
+++ b/lib/scripts/require.js
@@ -24,6 +24,10 @@ var require,
 
 (function () {
     var modules = {};
+    // Stack of moduleIds currently being built.
+    var requireStack = [];
+    // Map of module ID -> index into requireStack of modules currently being built.
+    var inProgressModules = {};
 
     function build(module) {
         var factory = module.factory;
@@ -36,8 +40,21 @@ var require,
     require = function (id) {
         if (!modules[id]) {
             throw "module " + id + " not found";
+        } else if (id in inProgressModules) {
+            var cycle = requireStack.slice(inProgressModules[id]).join('->') + '->'
+ id;
+            throw "Cycle in require graph: " + cycle;
         }
-        return modules[id].factory ? build(modules[id]) : modules[id].exports;
+        if (modules[id].factory) {
+            try {
+                inProgressModules[id] = requireStack.length;
+                requireStack.push(id);
+                return build(modules[id]);
+            } finally {
+                delete inProgressModules[id];
+                requireStack.pop();
+            }
+        }
+        return modules[id].exports;
     };
 
     define = function (id, factory) {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/9878d97f/test/test.require.js
----------------------------------------------------------------------
diff --git a/test/test.require.js b/test/test.require.js
index b2e804d..7734e41 100644
--- a/test/test.require.js
+++ b/test/test.require.js
@@ -56,6 +56,38 @@ describe("require + define", function () {
             }).toThrow("module your mom not found");
         });
 
+        it("throws an exception when modules depend on each other", function () {
+            define("ModuleA", function(require, exports, module) {
+                require("ModuleB");
+            });
+            define("ModuleB", function(require, exports, module) {
+                require("ModuleA");
+            });
+            expect(function () {
+                require("ModuleA");
+            }).toThrow("Cycle in require graph: ModuleA->ModuleB->ModuleA");
+            define.remove("ModuleA");
+            define.remove("ModuleB");
+        });
+
+        it("throws an exception when a cycle of requires occurs", function () {
+            define("ModuleA", function(require, exports, module) {
+                require("ModuleB");
+            });
+            define("ModuleB", function(require, exports, module) {
+                require("ModuleC");
+            });
+            define("ModuleC", function(require, exports, module) {
+                require("ModuleA");
+            });
+            expect(function () {
+                require("ModuleA");
+            }).toThrow("Cycle in require graph: ModuleA->ModuleB->ModuleC->ModuleA");
+            define.remove("ModuleA");
+            define.remove("ModuleB");
+            define.remove("ModuleC");
+        });
+
         it("calls the factory method when requiring", function () {
             var factory = jasmine.createSpy();
             define("dino", factory);


Mime
View raw message