diff options
| author | Misko Hevery | 2012-02-22 11:31:49 -0800 |
|---|---|---|
| committer | Misko Hevery | 2012-02-22 11:32:09 -0800 |
| commit | fbcb7fdd141c277d326dc3ed34545210c4d5628f (patch) | |
| tree | c69844d52dd52bbff0b8179098f305c0054241a0 | |
| parent | fa69d10122458fb92b311ca5e30a5804c03c0778 (diff) | |
| download | angular.js-fbcb7fdd141c277d326dc3ed34545210c4d5628f.tar.bz2 | |
fix($injector): circular dependency instatiation
| -rw-r--r-- | src/Injector.js | 7 | ||||
| -rw-r--r-- | test/InjectorSpec.js | 21 |
2 files changed, 27 insertions, 1 deletions
diff --git a/src/Injector.js b/src/Injector.js index a9ec6e5d..c24cbb51 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -273,7 +273,8 @@ function inferInjectionArgs(fn) { function createInjector(modulesToLoad) { - var providerSuffix = 'Provider', + var INSTANTIATING = {}, + providerSuffix = 'Provider', path = [], loadedModules = new HashMap(), providerCache = { @@ -394,10 +395,14 @@ function createInjector(modulesToLoad) { throw Error('Service name expected'); } if (cache.hasOwnProperty(serviceName)) { + if (cache[serviceName] === INSTANTIATING) { + throw Error('Circular dependency: ' + path.join(' <- ')); + } return cache[serviceName]; } else { try { path.unshift(serviceName); + cache[serviceName] = INSTANTIATING; return cache[serviceName] = factory(serviceName); } finally { path.shift(); diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index df326580..e0cabbac 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -483,6 +483,27 @@ describe('injector', function() { createInjector([['$injector', myModule]]); }).toThrow('Unknown provider: $injector from ' + myModule); }); + + + it('should throw error when trying to inject oneself', function() { + expect(function() { + createInjector([function($provide){ + $provide.factory('service', function(service){}); + return function(service) {} + }]) + }).toThrow('Circular dependency: service'); + }); + + + it('should throw error when trying to inject circular dependency', function() { + expect(function() { + createInjector([function($provide){ + $provide.factory('a', function(b){}); + $provide.factory('b', function(a){}); + return function(a) {} + }]) + }).toThrow('Circular dependency: b <- a'); + }); }); }); |
