Skip to content
Browse files

Add in basic recursion-detection for Container

  • Loading branch information...
1 parent 21ef760 commit dde3499ad44e451d4f406b1b3452b987dee50a1b @Whiteknight committed Nov 21, 2011
Showing with 24 additions and 2 deletions.
  1. +1 −1 src/container/Argument.winxed
  2. +23 −1 src/container/Container.winxed
View
2 src/container/Argument.winxed
@@ -27,7 +27,7 @@ class Rosella.Container.Argument.Resolve : Rosella.Container.Argument
function get_value(var container)
{
- var obj = container.resolve(self.type);
+ var obj = container.resolve_internal(self.type, []);
return obj;
}
}
View
24 src/container/Container.winxed
@@ -35,12 +35,15 @@ class Rosella.Container
{
var type_registry;
var aliases;
+ var resolve_id;
+ var current_resolve_list;
// Constructor
function Container()
{
self.type_registry = Rosella.get_pmc_keyed_hash();
self.aliases = {};
+ self.resolve_id = 0;
}
/* Public Container API
@@ -124,7 +127,26 @@ class Rosella.Container
var type_class = self.__get_type_class(type);
if (type_class == null)
Rosella.Error.invalid(__FUNCTION__, "Cannot resolve unknown class");
- return self.__resolve_internal(type_class, options);
+ self.resolve_id++;
+ self.current_resolve_list = Rosella.get_pmc_keyed_hash();
+ self.current_resolve_list[type] = 1;
+ var obj = self.__resolve_internal(type_class, options);
+ return obj;
+ }
+
+ // Resolution function for use internally by the Container library. Do not
+ // use this in your own code!
+ function resolve_internal(var type, var options)
+ {
+ var type_class = self.__get_type_class(type);
+ if (type_class == null)
+ Rosella.Error.invalid(__FUNCTION__, "Cannot resolve unknown class");
+ if (exists self.current_resolve_list[type])
+ Rosella.Error.invalid(__FUNCTION__, "Attempt to resolve type '%s' recursively. Infinite loop detected.", type_class);
+ self.current_resolve_list[type] = 1;
+ var obj = self.__resolve_internal(type_class, options);
+ delete self.current_resolve_list[type];
+ return obj;
}
/* Private Helper Methods

0 comments on commit dde3499

Please sign in to comment.
Something went wrong with that request. Please try again.