as_globalproperty.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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 = 0;
  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 (memoryAllocated || realAddress) ? memory : &storage;
  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::SetInitFunc(asCScriptFunction *initFunc)
  119. {
  120. // This should only be done once
  121. asASSERT( this->initFunc == 0 );
  122. this->initFunc = initFunc;
  123. this->initFunc->AddRef();
  124. // When there is an initialization function there is a chance that
  125. // a circular reference is created, so it is necessary to notify the
  126. // GC of this property.
  127. initFunc->engine->gc.AddScriptObjectToGC(this, &initFunc->engine->globalPropertyBehaviours);
  128. }
  129. asCScriptFunction *asCGlobalProperty::GetInitFunc()
  130. {
  131. return initFunc;
  132. }
  133. #ifdef AS_MAX_PORTABILITY
  134. static void GlobalProperty_AddRef_Generic(asIScriptGeneric *gen)
  135. {
  136. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  137. self->AddRef();
  138. }
  139. static void GlobalProperty_Release_Generic(asIScriptGeneric *gen)
  140. {
  141. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  142. self->Release();
  143. }
  144. static void GlobalProperty_GetRefCount_Generic(asIScriptGeneric *gen)
  145. {
  146. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  147. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  148. }
  149. static void GlobalProperty_SetGCFlag_Generic(asIScriptGeneric *gen)
  150. {
  151. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  152. self->SetGCFlag();
  153. }
  154. static void GlobalProperty_GetGCFlag_Generic(asIScriptGeneric *gen)
  155. {
  156. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  157. *(bool*)gen->GetAddressOfReturnLocation() = self->GetGCFlag();
  158. }
  159. static void GlobalProperty_EnumReferences_Generic(asIScriptGeneric *gen)
  160. {
  161. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  162. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  163. self->EnumReferences(engine);
  164. }
  165. static void GlobalProperty_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  166. {
  167. asCGlobalProperty *self = (asCGlobalProperty*)gen->GetObject();
  168. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  169. self->ReleaseAllHandles(engine);
  170. }
  171. #endif
  172. void asCGlobalProperty::RegisterGCBehaviours(asCScriptEngine *engine)
  173. {
  174. // Register the gc behaviours for the global properties
  175. int r;
  176. engine->globalPropertyBehaviours.engine = engine;
  177. engine->globalPropertyBehaviours.flags = asOBJ_REF | asOBJ_GC;
  178. engine->globalPropertyBehaviours.name = "_builtin_globalprop_";
  179. #ifndef AS_MAX_PORTABILITY
  180. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCGlobalProperty,AddRef), asCALL_THISCALL); asASSERT( r >= 0 );
  181. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCGlobalProperty,Release), asCALL_THISCALL); asASSERT( r >= 0 );
  182. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCGlobalProperty,GetRefCount), asCALL_THISCALL); asASSERT( r >= 0 );
  183. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCGlobalProperty,SetGCFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  184. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCGlobalProperty,GetGCFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  185. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCGlobalProperty,EnumReferences), asCALL_THISCALL); asASSERT( r >= 0 );
  186. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCGlobalProperty,ReleaseAllHandles), asCALL_THISCALL); asASSERT( r >= 0 );
  187. #else
  188. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(GlobalProperty_AddRef_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  189. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(GlobalProperty_Release_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  190. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(GlobalProperty_GetRefCount_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  191. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(GlobalProperty_SetGCFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  192. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(GlobalProperty_GetGCFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  193. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(GlobalProperty_EnumReferences_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  194. r = engine->RegisterBehaviourToObjectType(&engine->globalPropertyBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(GlobalProperty_ReleaseAllHandles_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  195. #endif
  196. }
  197. END_AS_NAMESPACE