as_globalproperty.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 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. #include "as_config.h"
  24. #include "as_property.h"
  25. #include "as_scriptengine.h"
  26. BEGIN_AS_NAMESPACE
  27. asCGlobalProperty::asCGlobalProperty()
  28. {
  29. memory = &storage;
  30. memoryAllocated = false;
  31. realAddress = 0;
  32. initFunc = 0;
  33. accessMask = 0xFFFFFFFF;
  34. refCount.set(1);
  35. }
  36. asCGlobalProperty::~asCGlobalProperty()
  37. {
  38. if( memoryAllocated ) { asDELETEARRAY(memory); }
  39. if( initFunc )
  40. initFunc->Release();
  41. }
  42. void asCGlobalProperty::AddRef()
  43. {
  44. gcFlag = false;
  45. refCount.atomicInc();
  46. }
  47. void asCGlobalProperty::Release()
  48. {
  49. gcFlag = false;
  50. // The property doesn't delete itself. The
  51. // engine will do that at a later time
  52. if( refCount.atomicDec() == 2 && initFunc )
  53. {
  54. // Since the initFunc holds references to the property,
  55. // we'll release it when we reach refCount 2. This will
  56. // break the circle and allow the engine to free the property
  57. // without the need for the GC to attempt finding circular
  58. // references.
  59. initFunc->Release();
  60. initFunc = 0;
  61. }
  62. }
  63. void *asCGlobalProperty::GetAddressOfValue()
  64. {
  65. return memory;
  66. }
  67. // The global property structure is responsible for allocating the storage
  68. // method for script declared variables. Each allocation is independent of
  69. // other global properties, so that variables can be added and removed at
  70. // any time.
  71. void asCGlobalProperty::AllocateMemory()
  72. {
  73. if( type.GetSizeOnStackDWords() > 2 )
  74. {
  75. memory = asNEWARRAY(asDWORD, type.GetSizeOnStackDWords());
  76. memoryAllocated = true;
  77. }
  78. }
  79. void asCGlobalProperty::SetRegisteredAddress(void *p)
  80. {
  81. realAddress = p;
  82. if( type.IsObject() && !type.IsReference() && !type.IsObjectHandle() )
  83. {
  84. // The global property is a pointer to a pointer
  85. memory = &realAddress;
  86. }
  87. else
  88. memory = p;
  89. }
  90. void *asCGlobalProperty::GetRegisteredAddress() const
  91. {
  92. return realAddress;
  93. }
  94. int asCGlobalProperty::GetRefCount()
  95. {
  96. return refCount.get();
  97. }
  98. void asCGlobalProperty::SetGCFlag()
  99. {
  100. gcFlag = true;
  101. }
  102. bool asCGlobalProperty::GetGCFlag()
  103. {
  104. return gcFlag;
  105. }
  106. void asCGlobalProperty::EnumReferences(asIScriptEngine *engine)
  107. {
  108. engine->GCEnumCallback(initFunc);
  109. }
  110. void asCGlobalProperty::ReleaseAllHandles(asIScriptEngine *)
  111. {
  112. if( initFunc )
  113. {
  114. initFunc->Release();
  115. initFunc = 0;
  116. }
  117. }
  118. void asCGlobalProperty::Orphan(asCModule *module)
  119. {
  120. if( initFunc && initFunc->module == module )
  121. {
  122. // The owning module is releasing the property, so we need to notify
  123. // the GC in order to resolve any circular references that may exists
  124. // This will add the property
  125. initFunc->engine->gc.AddScriptObjectToGC(this, &initFunc->engine->globalPropertyBehaviours);
  126. // This will add the function
  127. initFunc->AddRef();
  128. initFunc->Orphan(module);
  129. }
  130. Release();
  131. }
  132. void asCGlobalProperty::SetInitFunc(asCScriptFunction *initFunc)
  133. {
  134. // This should only be done once
  135. asASSERT( this->initFunc == 0 );
  136. this->initFunc = initFunc;
  137. this->initFunc->AddRef();
  138. }
  139. asCScriptFunction *asCGlobalProperty::GetInitFunc()
  140. {
  141. return initFunc;
  142. }
  143. #ifdef AS_MAX_PORTABILITY
  144. static void GlobalProperty_AddRef_Generic(asIScriptGeneric *gen)
  145. {
  146. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  147. self->AddRef();
  148. }
  149. static void GlobalProperty_Release_Generic(asIScriptGeneric *gen)
  150. {
  151. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  152. self->Release();
  153. }
  154. static void GlobalProperty_GetRefCount_Generic(asIScriptGeneric *gen)
  155. {
  156. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  157. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  158. }
  159. static void GlobalProperty_SetGCFlag_Generic(asIScriptGeneric *gen)
  160. {
  161. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  162. self->SetGCFlag();
  163. }
  164. static void GlobalProperty_GetGCFlag_Generic(asIScriptGeneric *gen)
  165. {
  166. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  167. *(bool*)gen->GetAddressOfReturnLocation() = self->GetGCFlag();
  168. }
  169. static void GlobalProperty_EnumReferences_Generic(asIScriptGeneric *gen)
  170. {
  171. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  172. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  173. self->EnumReferences(engine);
  174. }
  175. static void GlobalProperty_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  176. {
  177. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  178. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  179. self->ReleaseAllHandles(engine);
  180. }
  181. #endif
  182. void asCGlobalProperty::RegisterGCBehaviours(asCScriptEngine *engine)
  183. {
  184. // Register the gc behaviours for the global properties
  185. int r = 0;
  186. UNUSED_VAR(r); // It is only used in debug mode
  187. engine->globalPropertyBehaviours.engine = engine;
  188. engine->globalPropertyBehaviours.flags = asOBJ_REF | asOBJ_GC;
  189. engine->globalPropertyBehaviours.name = "_builtin_globalprop_";
  190. #ifndef AS_MAX_PORTABILITY
  191. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCGlobalProperty,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  192. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCGlobalProperty,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  193. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCGlobalProperty,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  194. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCGlobalProperty,SetGCFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  195. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCGlobalProperty,GetGCFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  196. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCGlobalProperty,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  197. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCGlobalProperty,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  198. #else
  199. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(GlobalProperty_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  200. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(GlobalProperty_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  201. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(GlobalProperty_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  202. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(GlobalProperty_SetGCFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  203. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(GlobalProperty_GetGCFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  204. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(GlobalProperty_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  205. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(GlobalProperty_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  206. #endif
  207. }
  208. END_AS_NAMESPACE