as_configgroup.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_configgroup.cpp
  25. //
  26. // This class holds configuration groups for the engine
  27. //
  28. #include "as_config.h"
  29. #include "as_configgroup.h"
  30. #include "as_scriptengine.h"
  31. #include "as_texts.h"
  32. BEGIN_AS_NAMESPACE
  33. asCConfigGroup::asCConfigGroup()
  34. {
  35. refCount = 0;
  36. }
  37. asCConfigGroup::~asCConfigGroup()
  38. {
  39. }
  40. int asCConfigGroup::AddRef()
  41. {
  42. refCount++;
  43. return refCount;
  44. }
  45. int asCConfigGroup::Release()
  46. {
  47. // Don't delete the object here, the engine will delete the object when ready
  48. refCount--;
  49. return refCount;
  50. }
  51. asCObjectType *asCConfigGroup::FindType(const char *obj)
  52. {
  53. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  54. if( objTypes[n]->name == obj )
  55. return objTypes[n];
  56. return 0;
  57. }
  58. void asCConfigGroup::RefConfigGroup(asCConfigGroup *group)
  59. {
  60. if( group == this || group == 0 ) return;
  61. // Verify if the group is already referenced
  62. for( asUINT n = 0; n < referencedConfigGroups.GetLength(); n++ )
  63. if( referencedConfigGroups[n] == group )
  64. return;
  65. referencedConfigGroups.PushLast(group);
  66. group->AddRef();
  67. }
  68. bool asCConfigGroup::HasLiveObjects()
  69. {
  70. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  71. if( objTypes[n]->GetRefCount() != 0 )
  72. return true;
  73. return false;
  74. }
  75. void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine, bool notUsed)
  76. {
  77. asASSERT( refCount == 0 );
  78. asUINT n;
  79. // Remove global variables
  80. for( n = 0; n < globalProps.GetLength(); n++ )
  81. {
  82. int index = engine->registeredGlobalProps.GetIndex(globalProps[n]);
  83. if( index >= 0 )
  84. {
  85. globalProps[n]->Release();
  86. // TODO: global: Should compact the registeredGlobalProps array
  87. engine->registeredGlobalProps.Erase(index);
  88. }
  89. }
  90. globalProps.SetLength(0);
  91. // Remove global functions
  92. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  93. {
  94. int index = engine->registeredGlobalFuncs.GetIndex(scriptFunctions[n]);
  95. if( index >= 0 )
  96. engine->registeredGlobalFuncs.Erase(index);
  97. scriptFunctions[n]->Release();
  98. if( engine->stringFactory == scriptFunctions[n] )
  99. engine->stringFactory = 0;
  100. }
  101. scriptFunctions.SetLength(0);
  102. // Remove behaviours and members of object types
  103. for( n = 0; n < objTypes.GetLength(); n++ )
  104. {
  105. asCObjectType *obj = objTypes[n];
  106. obj->ReleaseAllFunctions();
  107. }
  108. // Remove function definitions
  109. for( n = 0; n < funcDefs.GetLength(); n++ )
  110. {
  111. engine->registeredFuncDefs.RemoveValue(funcDefs[n]);
  112. funcDefs[n]->Release();
  113. }
  114. funcDefs.SetLength(0);
  115. engine->ClearUnusedTypes();
  116. // Remove object types (skip this if it is possible other groups are still using the types)
  117. if( !notUsed )
  118. {
  119. for( n = asUINT(objTypes.GetLength()); n-- > 0; )
  120. {
  121. asCObjectType *t = objTypes[n];
  122. asSMapNode<asSNameSpaceNamePair, asCObjectType*> *cursor;
  123. if( engine->allRegisteredTypes.MoveTo(&cursor, asSNameSpaceNamePair(t->nameSpace, t->name)) &&
  124. cursor->value == t )
  125. {
  126. #ifdef AS_DEBUG
  127. ValidateNoUsage(engine, t);
  128. #endif
  129. engine->allRegisteredTypes.Erase(cursor);
  130. if( engine->defaultArrayObjectType == t )
  131. engine->defaultArrayObjectType = 0;
  132. if( t->flags & asOBJ_TYPEDEF )
  133. engine->registeredTypeDefs.RemoveValue(t);
  134. else if( t->flags & asOBJ_ENUM )
  135. engine->registeredEnums.RemoveValue(t);
  136. else if( t->flags & asOBJ_TEMPLATE )
  137. engine->registeredTemplateTypes.RemoveValue(t);
  138. else
  139. engine->registeredObjTypes.RemoveValue(t);
  140. asDELETE(t, asCObjectType);
  141. }
  142. else
  143. {
  144. int idx = engine->templateInstanceTypes.IndexOf(t);
  145. if( idx >= 0 )
  146. {
  147. #ifdef AS_DEBUG
  148. ValidateNoUsage(engine, t);
  149. #endif
  150. engine->templateInstanceTypes.RemoveIndexUnordered(idx);
  151. t->templateSubTypes.SetLength(0);
  152. asDELETE(t, asCObjectType);
  153. }
  154. }
  155. }
  156. objTypes.SetLength(0);
  157. }
  158. // Release other config groups
  159. for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
  160. referencedConfigGroups[n]->refCount--;
  161. referencedConfigGroups.SetLength(0);
  162. }
  163. #ifdef AS_DEBUG
  164. void asCConfigGroup::ValidateNoUsage(asCScriptEngine *engine, asCObjectType *type)
  165. {
  166. for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  167. {
  168. asCScriptFunction *func = engine->scriptFunctions[n];
  169. if( func == 0 ) continue;
  170. // Ignore factory, list factory, and members
  171. if( func->name == "_beh_3_" || func->name == "_beh_4_" || func->objectType == type )
  172. continue;
  173. // Ignore function definitions too, as they aren't released until the engine is destroyed
  174. if( func->funcType == asFUNC_FUNCDEF )
  175. continue;
  176. // Ignore functions whose object type has already reached refCount 0 as they are to be removed
  177. if( func->objectType && func->objectType->GetRefCount() == 0 )
  178. continue;
  179. if( func->returnType.GetObjectType() == type )
  180. {
  181. asCString msg;
  182. // We can only use the function name here, because the types used by the function may have been deleted already
  183. msg.Format(TXT_TYPE_s_IS_STILL_USED_BY_FUNC_s, type->name.AddressOf(), func->GetDeclaration());
  184. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
  185. }
  186. else
  187. {
  188. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  189. {
  190. if( func->parameterTypes[p].GetObjectType() == type )
  191. {
  192. asCString msg;
  193. // We can only use the function name here, because the types used by the function may have been deleted already
  194. msg.Format(TXT_TYPE_s_IS_STILL_USED_BY_FUNC_s, type->name.AddressOf(), func->GetDeclaration());
  195. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. // TODO: Check also usage of the type in global variables
  202. // TODO: Check also usage of the type in local variables in script functions
  203. // TODO: Check also usage of the type as members of classes
  204. // TODO: Check also usage of the type as sub types in other types
  205. }
  206. #endif
  207. END_AS_NAMESPACE