| 3915 | //////////////////////////////////// |
| 3916 | |
| 3917 | function createInternalInjector(cache, factory) { |
| 3918 | |
| 3919 | function getService(serviceName) { |
| 3920 | if (cache.hasOwnProperty(serviceName)) { |
| 3921 | if (cache[serviceName] === INSTANTIATING) { |
| 3922 | throw $injectorMinErr('cdep', 'Circular dependency found: {0}', |
| 3923 | serviceName + ' <- ' + path.join(' <- ')); |
| 3924 | } |
| 3925 | return cache[serviceName]; |
| 3926 | } else { |
| 3927 | try { |
| 3928 | path.unshift(serviceName); |
| 3929 | cache[serviceName] = INSTANTIATING; |
| 3930 | return cache[serviceName] = factory(serviceName); |
| 3931 | } catch (err) { |
| 3932 | if (cache[serviceName] === INSTANTIATING) { |
| 3933 | delete cache[serviceName]; |
| 3934 | } |
| 3935 | throw err; |
| 3936 | } finally { |
| 3937 | path.shift(); |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | function invoke(fn, self, locals){ |
| 3943 | var args = [], |
| 3944 | $inject = annotate(fn), |
| 3945 | length, i, |
| 3946 | key; |
| 3947 | |
| 3948 | for(i = 0, length = $inject.length; i < length; i++) { |
| 3949 | key = $inject[i]; |
| 3950 | if (typeof key !== 'string') { |
| 3951 | throw $injectorMinErr('itkn', |
| 3952 | 'Incorrect injection token! Expected service name as string, got {0}', key); |
| 3953 | } |
| 3954 | args.push( |
| 3955 | locals && locals.hasOwnProperty(key) |
| 3956 | ? locals[key] |
| 3957 | : getService(key) |
| 3958 | ); |
| 3959 | } |
| 3960 | if (isArray(fn)) { |
| 3961 | fn = fn[length]; |
| 3962 | } |
| 3963 | |
| 3964 | // http://jsperf.com/angularjs-invoke-apply-vs-switch |
| 3965 | // #5388 |
| 3966 | return fn.apply(self, args); |
| 3967 | } |
| 3968 | |
| 3969 | function instantiate(Type, locals) { |
| 3970 | var Constructor = function() {}, |
| 3971 | instance, returnedValue; |
| 3972 | |
| 3973 | // Check if Type is annotated and use just the given function at n-1 as parameter |
| 3974 | // e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]); |