as_globalproperty.cpp 8.4 KB

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