as_scriptobject.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2019 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. andreas@angelcode.com
  22. */
  23. #include <new>
  24. #include "as_config.h"
  25. #include "as_scriptengine.h"
  26. #include "as_scriptobject.h"
  27. #include "as_texts.h"
  28. BEGIN_AS_NAMESPACE
  29. // This helper function will call the default factory, that is a script function
  30. asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine)
  31. {
  32. asIScriptContext *ctx = 0;
  33. int r = 0;
  34. bool isNested = false;
  35. // Use nested call in the context if there is an active context
  36. ctx = asGetActiveContext();
  37. if( ctx )
  38. {
  39. // It may not always be possible to reuse the current context,
  40. // in which case we'll have to create a new one any way.
  41. if( ctx->GetEngine() == objType->GetEngine() && ctx->PushState() == asSUCCESS )
  42. isNested = true;
  43. else
  44. ctx = 0;
  45. }
  46. if( ctx == 0 )
  47. {
  48. // Request a context from the engine
  49. ctx = engine->RequestContext();
  50. if( ctx == 0 )
  51. {
  52. // TODO: How to best report this failure?
  53. return 0;
  54. }
  55. }
  56. r = ctx->Prepare(engine->scriptFunctions[objType->beh.factory]);
  57. if( r < 0 )
  58. {
  59. if( isNested )
  60. ctx->PopState();
  61. else
  62. engine->ReturnContext(ctx);
  63. return 0;
  64. }
  65. for(;;)
  66. {
  67. r = ctx->Execute();
  68. // We can't allow this execution to be suspended
  69. // so resume the execution immediately
  70. if( r != asEXECUTION_SUSPENDED )
  71. break;
  72. }
  73. if( r != asEXECUTION_FINISHED )
  74. {
  75. if( isNested )
  76. {
  77. ctx->PopState();
  78. // If the execution was aborted or an exception occurred,
  79. // then we should forward that to the outer execution.
  80. if( r == asEXECUTION_EXCEPTION )
  81. {
  82. // TODO: How to improve this exception
  83. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  84. }
  85. else if( r == asEXECUTION_ABORTED )
  86. ctx->Abort();
  87. }
  88. else
  89. engine->ReturnContext(ctx);
  90. return 0;
  91. }
  92. asIScriptObject *ptr = (asIScriptObject*)ctx->GetReturnAddress();
  93. // Increase the reference, because the context will release its pointer
  94. ptr->AddRef();
  95. if( isNested )
  96. ctx->PopState();
  97. else
  98. engine->ReturnContext(ctx);
  99. return ptr;
  100. }
  101. // This helper function will call the copy factory, that is a script function
  102. // TODO: Clean up: This function is almost identical to ScriptObjectFactory. Should make better use of the identical code.
  103. asIScriptObject *ScriptObjectCopyFactory(const asCObjectType *objType, void *origObj, asCScriptEngine *engine)
  104. {
  105. asIScriptContext *ctx = 0;
  106. int r = 0;
  107. bool isNested = false;
  108. // Use nested call in the context if there is an active context
  109. ctx = asGetActiveContext();
  110. if (ctx)
  111. {
  112. // It may not always be possible to reuse the current context,
  113. // in which case we'll have to create a new one any way.
  114. if (ctx->GetEngine() == objType->GetEngine() && ctx->PushState() == asSUCCESS)
  115. isNested = true;
  116. else
  117. ctx = 0;
  118. }
  119. if (ctx == 0)
  120. {
  121. // Request a context from the engine
  122. ctx = engine->RequestContext();
  123. if (ctx == 0)
  124. {
  125. // TODO: How to best report this failure?
  126. return 0;
  127. }
  128. }
  129. r = ctx->Prepare(engine->scriptFunctions[objType->beh.copyfactory]);
  130. if (r < 0)
  131. {
  132. if (isNested)
  133. ctx->PopState();
  134. else
  135. engine->ReturnContext(ctx);
  136. return 0;
  137. }
  138. // Let the context handle the case for argument by ref (&) or by handle (@)
  139. ctx->SetArgObject(0, origObj);
  140. for (;;)
  141. {
  142. r = ctx->Execute();
  143. // We can't allow this execution to be suspended
  144. // so resume the execution immediately
  145. if (r != asEXECUTION_SUSPENDED)
  146. break;
  147. }
  148. if (r != asEXECUTION_FINISHED)
  149. {
  150. if (isNested)
  151. {
  152. ctx->PopState();
  153. // If the execution was aborted or an exception occurred,
  154. // then we should forward that to the outer execution.
  155. if (r == asEXECUTION_EXCEPTION)
  156. {
  157. // TODO: How to improve this exception
  158. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  159. }
  160. else if (r == asEXECUTION_ABORTED)
  161. ctx->Abort();
  162. }
  163. else
  164. engine->ReturnContext(ctx);
  165. return 0;
  166. }
  167. asIScriptObject *ptr = (asIScriptObject*)ctx->GetReturnAddress();
  168. // Increase the reference, because the context will release its pointer
  169. ptr->AddRef();
  170. if (isNested)
  171. ctx->PopState();
  172. else
  173. engine->ReturnContext(ctx);
  174. return ptr;
  175. }
  176. #ifdef AS_MAX_PORTABILITY
  177. static void ScriptObject_AddRef_Generic(asIScriptGeneric *gen)
  178. {
  179. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  180. self->AddRef();
  181. }
  182. static void ScriptObject_Release_Generic(asIScriptGeneric *gen)
  183. {
  184. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  185. self->Release();
  186. }
  187. static void ScriptObject_GetRefCount_Generic(asIScriptGeneric *gen)
  188. {
  189. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  190. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  191. }
  192. static void ScriptObject_SetFlag_Generic(asIScriptGeneric *gen)
  193. {
  194. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  195. self->SetFlag();
  196. }
  197. static void ScriptObject_GetFlag_Generic(asIScriptGeneric *gen)
  198. {
  199. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  200. *(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
  201. }
  202. static void ScriptObject_GetWeakRefFlag_Generic(asIScriptGeneric *gen)
  203. {
  204. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  205. *(asILockableSharedBool**)gen->GetAddressOfReturnLocation() = self->GetWeakRefFlag();
  206. }
  207. static void ScriptObject_EnumReferences_Generic(asIScriptGeneric *gen)
  208. {
  209. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  210. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  211. self->EnumReferences(engine);
  212. }
  213. static void ScriptObject_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  214. {
  215. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  216. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  217. self->ReleaseAllHandles(engine);
  218. }
  219. static void ScriptObject_Assignment_Generic(asIScriptGeneric *gen)
  220. {
  221. asCScriptObject *other = *(asCScriptObject**)gen->GetAddressOfArg(0);
  222. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  223. *self = *other;
  224. *(asCScriptObject**)gen->GetAddressOfReturnLocation() = self;
  225. }
  226. static void ScriptObject_Construct_Generic(asIScriptGeneric *gen)
  227. {
  228. asCObjectType *objType = *(asCObjectType**)gen->GetAddressOfArg(0);
  229. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  230. ScriptObject_Construct(objType, self);
  231. }
  232. #endif
  233. void RegisterScriptObject(asCScriptEngine *engine)
  234. {
  235. // Register the default script class behaviours
  236. int r = 0;
  237. UNUSED_VAR(r); // It is only used in debug mode
  238. engine->scriptTypeBehaviours.engine = engine;
  239. engine->scriptTypeBehaviours.flags = asOBJ_SCRIPT_OBJECT | asOBJ_REF | asOBJ_GC;
  240. engine->scriptTypeBehaviours.name = "$obj";
  241. #ifndef AS_MAX_PORTABILITY
  242. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct), asCALL_CDECL_OBJLAST, 0); asASSERT( r >= 0 );
  243. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptObject,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  244. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptObject,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  245. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  246. // Weakref behaviours
  247. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GET_WEAKREF_FLAG, "int &f()", asMETHOD(asCScriptObject,GetWeakRefFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  248. // Register GC behaviours
  249. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptObject,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  250. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptObject,SetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  251. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptObject,GetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  252. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptObject,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  253. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptObject,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  254. #else
  255. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  256. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptObject_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  257. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptObject_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  258. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  259. // Weakref behaviours
  260. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GET_WEAKREF_FLAG, "int &f()", asFUNCTION(ScriptObject_GetWeakRefFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  261. // Register GC behaviours
  262. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptObject_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  263. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptObject_SetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  264. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptObject_GetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  265. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptObject_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  266. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptObject_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  267. #endif
  268. }
  269. void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self)
  270. {
  271. new(self) asCScriptObject(objType);
  272. }
  273. void ScriptObject_ConstructUnitialized(asCObjectType *objType, asCScriptObject *self)
  274. {
  275. new(self) asCScriptObject(objType, false);
  276. }
  277. asCScriptObject::asCScriptObject(asCObjectType *ot, bool doInitialize)
  278. {
  279. refCount.set(1);
  280. objType = ot;
  281. objType->AddRef();
  282. isDestructCalled = false;
  283. extra = 0;
  284. hasRefCountReachedZero = false;
  285. // Notify the garbage collector of this object
  286. if( objType->flags & asOBJ_GC )
  287. objType->engine->gc.AddScriptObjectToGC(this, objType);
  288. // Initialize members to zero. Technically we only need to zero the pointer
  289. // members, but just the memset is faster than having to loop and check the datatypes
  290. memset((void*)(this+1), 0, objType->size - sizeof(asCScriptObject));
  291. if( doInitialize )
  292. {
  293. #ifdef AS_NO_MEMBER_INIT
  294. // When member initialization is disabled the constructor must make sure
  295. // to allocate and initialize all members with the default constructor
  296. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  297. {
  298. asCObjectProperty *prop = objType->properties[n];
  299. if( prop->type.IsObject() && !prop->type.IsObjectHandle() )
  300. {
  301. if( prop->type.IsReference() || prop->type.GetTypeInfo()->flags & asOBJ_REF )
  302. {
  303. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  304. if( prop->type.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT )
  305. *ptr = (asPWORD)ScriptObjectFactory(prop->type.GetTypeInfo(), ot->engine);
  306. else
  307. *ptr = (asPWORD)AllocateUninitializedObject(prop->type.GetTypeInfo(), ot->engine);
  308. }
  309. }
  310. }
  311. #endif
  312. }
  313. else
  314. {
  315. // When the object is created without initialization, all non-handle members must be allocated, but not initialized
  316. asCScriptEngine *engine = objType->engine;
  317. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  318. {
  319. asCObjectProperty *prop = objType->properties[n];
  320. if( prop->type.IsObject() && !prop->type.IsObjectHandle() )
  321. {
  322. if( prop->type.IsReference() || (prop->type.GetTypeInfo()->flags & asOBJ_REF) )
  323. {
  324. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  325. *ptr = (asPWORD)AllocateUninitializedObject(CastToObjectType(prop->type.GetTypeInfo()), engine);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. void asCScriptObject::Destruct()
  332. {
  333. // Call the destructor, which will also call the GCObject's destructor
  334. this->~asCScriptObject();
  335. // Free the memory
  336. #ifndef WIP_16BYTE_ALIGN
  337. userFree(this);
  338. #else
  339. // Script object memory is allocated through asCScriptEngine::CallAlloc()
  340. // This free call must match the allocator used in CallAlloc().
  341. userFreeAligned(this);
  342. #endif
  343. }
  344. asCScriptObject::~asCScriptObject()
  345. {
  346. if( extra )
  347. {
  348. if( extra->weakRefFlag )
  349. {
  350. extra->weakRefFlag->Release();
  351. extra->weakRefFlag = 0;
  352. }
  353. if( objType->engine )
  354. {
  355. // Clean the user data
  356. for( asUINT n = 0; n < extra->userData.GetLength(); n += 2 )
  357. {
  358. if( extra->userData[n+1] )
  359. {
  360. for( asUINT c = 0; c < objType->engine->cleanScriptObjectFuncs.GetLength(); c++ )
  361. if( objType->engine->cleanScriptObjectFuncs[c].type == extra->userData[n] )
  362. objType->engine->cleanScriptObjectFuncs[c].cleanFunc(this);
  363. }
  364. }
  365. }
  366. asDELETE(extra, SExtra);
  367. }
  368. // The engine pointer should be available from the objectType
  369. asCScriptEngine *engine = objType->engine;
  370. // Destroy all properties
  371. // In most cases the members are initialized in the order they have been declared,
  372. // so it's safer to uninitialize them from last to first. The order may be different
  373. // depending on the use of inheritance and or initialization in the declaration.
  374. // TODO: Should the order of initialization be stored by the compiler so that the
  375. // reverse order can be guaranteed during the destruction?
  376. for( int n = (int)objType->properties.GetLength()-1; n >= 0; n-- )
  377. {
  378. asCObjectProperty *prop = objType->properties[n];
  379. if( prop->type.IsObject() )
  380. {
  381. // Destroy the object
  382. asCObjectType *propType = CastToObjectType(prop->type.GetTypeInfo());
  383. if( prop->type.IsReference() || propType->flags & asOBJ_REF )
  384. {
  385. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  386. if( *ptr )
  387. {
  388. FreeObject(*ptr, propType, engine);
  389. *(asDWORD*)ptr = 0;
  390. }
  391. }
  392. else
  393. {
  394. // The object is allocated inline. As only POD objects may be allocated inline
  395. // it is not a problem to call the destructor even if the object may never have
  396. // been initialized, e.g. if an exception interrupted the constructor.
  397. asASSERT( propType->flags & asOBJ_POD );
  398. void *ptr = (void**)(((char*)this) + prop->byteOffset);
  399. if( propType->beh.destruct )
  400. engine->CallObjectMethod(ptr, propType->beh.destruct);
  401. }
  402. }
  403. else if( prop->type.IsFuncdef() )
  404. {
  405. // Release the function descriptor
  406. asCScriptFunction **ptr = (asCScriptFunction**)(((char*)this) + prop->byteOffset);
  407. if (*ptr)
  408. {
  409. (*ptr)->Release();
  410. *ptr = 0;
  411. }
  412. }
  413. }
  414. objType->Release();
  415. objType = 0;
  416. // Something is really wrong if the refCount is not 0 by now
  417. asASSERT( refCount.get() == 0 );
  418. }
  419. asILockableSharedBool *asCScriptObject::GetWeakRefFlag() const
  420. {
  421. // If the object's refCount has already reached zero then the object is already
  422. // about to be destroyed so it's ok to return null if the weakRefFlag doesn't already
  423. // exist
  424. if( (extra && extra->weakRefFlag) || hasRefCountReachedZero )
  425. return extra->weakRefFlag;
  426. // Lock globally so no other thread can attempt
  427. // to create a shared bool at the same time.
  428. // TODO: runtime optimize: Instead of locking globally, it would be possible to have
  429. // a critical section per object type. This would reduce the
  430. // chances of two threads lock on the same critical section.
  431. asAcquireExclusiveLock();
  432. // Make sure another thread didn't create the
  433. // flag while we waited for the lock
  434. if( !extra )
  435. extra = asNEW(SExtra);
  436. if( !extra->weakRefFlag )
  437. extra->weakRefFlag = asNEW(asCLockableSharedBool);
  438. asReleaseExclusiveLock();
  439. return extra->weakRefFlag;
  440. }
  441. void *asCScriptObject::GetUserData(asPWORD type) const
  442. {
  443. if( !extra )
  444. return 0;
  445. // There may be multiple threads reading, but when
  446. // setting the user data nobody must be reading.
  447. // TODO: runtime optimize: Would it be worth it to have a rwlock per object type?
  448. asAcquireSharedLock();
  449. for( asUINT n = 0; n < extra->userData.GetLength(); n += 2 )
  450. {
  451. if( extra->userData[n] == type )
  452. {
  453. void *userData = reinterpret_cast<void*>(extra->userData[n+1]);
  454. asReleaseSharedLock();
  455. return userData;
  456. }
  457. }
  458. asReleaseSharedLock();
  459. return 0;
  460. }
  461. void *asCScriptObject::SetUserData(void *data, asPWORD type)
  462. {
  463. // Lock globally so no other thread can attempt
  464. // to manipulate the extra data at the same time.
  465. // TODO: runtime optimize: Instead of locking globally, it would be possible to have
  466. // a critical section per object type. This would reduce the
  467. // chances of two threads lock on the same critical section.
  468. asAcquireExclusiveLock();
  469. // Make sure another thread didn't create the
  470. // flag while we waited for the lock
  471. if( !extra )
  472. extra = asNEW(SExtra);
  473. // It is not intended to store a lot of different types of userdata,
  474. // so a more complex structure like a associative map would just have
  475. // more overhead than a simple array.
  476. for( asUINT n = 0; n < extra->userData.GetLength(); n += 2 )
  477. {
  478. if( extra->userData[n] == type )
  479. {
  480. void *oldData = reinterpret_cast<void*>(extra->userData[n+1]);
  481. extra->userData[n+1] = reinterpret_cast<asPWORD>(data);
  482. asReleaseExclusiveLock();
  483. return oldData;
  484. }
  485. }
  486. extra->userData.PushLast(type);
  487. extra->userData.PushLast(reinterpret_cast<asPWORD>(data));
  488. asReleaseExclusiveLock();
  489. return 0;
  490. }
  491. asIScriptEngine *asCScriptObject::GetEngine() const
  492. {
  493. return objType->engine;
  494. }
  495. int asCScriptObject::AddRef() const
  496. {
  497. // Warn in case the application tries to increase the refCount after it has reached zero.
  498. // This may happen for example if the application calls a method on the class while it is
  499. // being destroyed. The application shouldn't do this because it may cause application
  500. // crashes if members that have already been destroyed are accessed accidentally.
  501. if( hasRefCountReachedZero )
  502. {
  503. if( objType && objType->engine )
  504. {
  505. asCString msg;
  506. msg.Format(TXT_RESURRECTING_SCRIPTOBJECT_s, objType->name.AddressOf());
  507. objType->engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
  508. }
  509. }
  510. // Increase counter and clear flag set by GC
  511. gcFlag = false;
  512. return refCount.atomicInc();
  513. }
  514. int asCScriptObject::Release() const
  515. {
  516. // Clear the flag set by the GC
  517. gcFlag = false;
  518. // If the weak ref flag exists it is because someone held a weak ref
  519. // and that someone may add a reference to the object at any time. It
  520. // is ok to check the existance of the weakRefFlag without locking here
  521. // because if the refCount is 1 then no other thread is currently
  522. // creating the weakRefFlag.
  523. if( refCount.get() == 1 && extra && extra->weakRefFlag )
  524. {
  525. // Set the flag to tell others that the object is no longer alive
  526. // We must do this before decreasing the refCount to 0 so we don't
  527. // end up with a race condition between this thread attempting to
  528. // destroy the object and the other that temporary added a strong
  529. // ref from the weak ref.
  530. extra->weakRefFlag->Set(true);
  531. }
  532. // Call the script destructor behaviour if the reference counter is 1.
  533. if( refCount.get() == 1 && !isDestructCalled )
  534. {
  535. // This cast is OK since we are the last reference
  536. const_cast<asCScriptObject*>(this)->CallDestructor();
  537. }
  538. // Now do the actual releasing
  539. int r = refCount.atomicDec();
  540. if( r == 0 )
  541. {
  542. // Flag this object as being destroyed so the application
  543. // can be warned if the code attempts to resurrect the object
  544. // during the destructor. This also avoids a recursive call
  545. // to the destructor which would crash the application if it
  546. // really does resurrect the object.
  547. if( !hasRefCountReachedZero )
  548. {
  549. hasRefCountReachedZero = true;
  550. // This cast is OK since we are the last reference
  551. const_cast<asCScriptObject*>(this)->Destruct();
  552. }
  553. return 0;
  554. }
  555. return r;
  556. }
  557. void asCScriptObject::CallDestructor()
  558. {
  559. // Only allow the destructor to be called once
  560. if( isDestructCalled ) return;
  561. asIScriptContext *ctx = 0;
  562. bool isNested = false;
  563. bool doAbort = false;
  564. // Make sure the destructor is called once only, even if the
  565. // reference count is increased and then decreased again
  566. isDestructCalled = true;
  567. // Call the destructor for this class and all the super classes
  568. asCObjectType *ot = objType;
  569. while( ot )
  570. {
  571. int funcIndex = ot->beh.destruct;
  572. if( funcIndex )
  573. {
  574. if( ctx == 0 )
  575. {
  576. // Check for active context first as it is quicker
  577. // to reuse than to set up a new one.
  578. ctx = asGetActiveContext();
  579. if( ctx )
  580. {
  581. if( ctx->GetEngine() == objType->GetEngine() && ctx->PushState() == asSUCCESS )
  582. isNested = true;
  583. else
  584. ctx = 0;
  585. }
  586. if( ctx == 0 )
  587. {
  588. // Request a context from the engine
  589. ctx = objType->engine->RequestContext();
  590. if( ctx == 0 )
  591. {
  592. // TODO: How to best report this failure?
  593. return;
  594. }
  595. }
  596. }
  597. int r = ctx->Prepare(objType->engine->scriptFunctions[funcIndex]);
  598. if( r >= 0 )
  599. {
  600. ctx->SetObject(this);
  601. for(;;)
  602. {
  603. r = ctx->Execute();
  604. // If the script tries to suspend itself just restart it
  605. if( r != asEXECUTION_SUSPENDED )
  606. break;
  607. }
  608. // Exceptions in the destructor will be ignored, as there is not much
  609. // that can be done about them. However a request to abort the execution
  610. // will be forwarded to the outer execution, in case of a nested call.
  611. if( r == asEXECUTION_ABORTED )
  612. doAbort = true;
  613. // Observe, even though the current destructor was aborted or an exception
  614. // occurred, we still try to execute the base class' destructor if available
  615. // in order to free as many resources as possible.
  616. }
  617. }
  618. ot = ot->derivedFrom;
  619. }
  620. if( ctx )
  621. {
  622. if( isNested )
  623. {
  624. ctx->PopState();
  625. // Forward any request to abort the execution to the outer call
  626. if( doAbort )
  627. ctx->Abort();
  628. }
  629. else
  630. {
  631. // Return the context to engine
  632. objType->engine->ReturnContext(ctx);
  633. }
  634. }
  635. }
  636. asITypeInfo *asCScriptObject::GetObjectType() const
  637. {
  638. return objType;
  639. }
  640. int asCScriptObject::GetRefCount()
  641. {
  642. return refCount.get();
  643. }
  644. void asCScriptObject::SetFlag()
  645. {
  646. gcFlag = true;
  647. }
  648. bool asCScriptObject::GetFlag()
  649. {
  650. return gcFlag;
  651. }
  652. // interface
  653. int asCScriptObject::GetTypeId() const
  654. {
  655. asCDataType dt = asCDataType::CreateType(objType, false);
  656. return objType->engine->GetTypeIdFromDataType(dt);
  657. }
  658. asUINT asCScriptObject::GetPropertyCount() const
  659. {
  660. return asUINT(objType->properties.GetLength());
  661. }
  662. int asCScriptObject::GetPropertyTypeId(asUINT prop) const
  663. {
  664. if( prop >= objType->properties.GetLength() )
  665. return asINVALID_ARG;
  666. return objType->engine->GetTypeIdFromDataType(objType->properties[prop]->type);
  667. }
  668. const char *asCScriptObject::GetPropertyName(asUINT prop) const
  669. {
  670. if( prop >= objType->properties.GetLength() )
  671. return 0;
  672. return objType->properties[prop]->name.AddressOf();
  673. }
  674. void *asCScriptObject::GetAddressOfProperty(asUINT prop)
  675. {
  676. if( prop >= objType->properties.GetLength() )
  677. return 0;
  678. // Objects are stored by reference, so this must be dereferenced
  679. asCDataType *dt = &objType->properties[prop]->type;
  680. if( dt->IsObject() && !dt->IsObjectHandle() &&
  681. (dt->IsReference() || dt->GetTypeInfo()->flags & asOBJ_REF) )
  682. return *(void**)(((char*)this) + objType->properties[prop]->byteOffset);
  683. return (void*)(((char*)this) + objType->properties[prop]->byteOffset);
  684. }
  685. void asCScriptObject::EnumReferences(asIScriptEngine *engine)
  686. {
  687. // We'll notify the GC of all object handles that we're holding
  688. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  689. {
  690. asCObjectProperty *prop = objType->properties[n];
  691. void *ptr = 0;
  692. if (prop->type.IsObject())
  693. {
  694. if (prop->type.IsReference() || (prop->type.GetTypeInfo()->flags & asOBJ_REF))
  695. ptr = *(void**)(((char*)this) + prop->byteOffset);
  696. else
  697. ptr = (void*)(((char*)this) + prop->byteOffset);
  698. // The members of the value type needs to be enumerated
  699. // too, since the value type may be holding a reference.
  700. if ((prop->type.GetTypeInfo()->flags & asOBJ_VALUE) && (prop->type.GetTypeInfo()->flags & asOBJ_GC))
  701. {
  702. reinterpret_cast<asCScriptEngine*>(engine)->CallObjectMethod(ptr, engine, CastToObjectType(prop->type.GetTypeInfo())->beh.gcEnumReferences);
  703. }
  704. }
  705. else if (prop->type.IsFuncdef())
  706. ptr = *(void**)(((char*)this) + prop->byteOffset);
  707. if (ptr)
  708. ((asCScriptEngine*)engine)->GCEnumCallback(ptr);
  709. }
  710. }
  711. void asCScriptObject::ReleaseAllHandles(asIScriptEngine *engine)
  712. {
  713. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  714. {
  715. asCObjectProperty *prop = objType->properties[n];
  716. if (prop->type.IsObject())
  717. {
  718. if (prop->type.IsObjectHandle())
  719. {
  720. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  721. if (*ptr)
  722. {
  723. asASSERT((prop->type.GetTypeInfo()->flags & asOBJ_NOCOUNT) || prop->type.GetBehaviour()->release);
  724. if (prop->type.GetBehaviour()->release)
  725. ((asCScriptEngine*)engine)->CallObjectMethod(*ptr, prop->type.GetBehaviour()->release);
  726. *ptr = 0;
  727. }
  728. }
  729. else if ((prop->type.GetTypeInfo()->flags & asOBJ_VALUE) && (prop->type.GetTypeInfo()->flags & asOBJ_GC))
  730. {
  731. // The members of the members needs to be released
  732. // too, since they may be holding a reference. Even
  733. // if the member is a value type.
  734. void *ptr = 0;
  735. if (prop->type.IsReference())
  736. ptr = *(void**)(((char*)this) + prop->byteOffset);
  737. else
  738. ptr = (void*)(((char*)this) + prop->byteOffset);
  739. reinterpret_cast<asCScriptEngine*>(engine)->CallObjectMethod(ptr, engine, CastToObjectType(prop->type.GetTypeInfo())->beh.gcReleaseAllReferences);
  740. }
  741. }
  742. else if (prop->type.IsFuncdef())
  743. {
  744. asCScriptFunction **ptr = (asCScriptFunction**)(((char*)this) + prop->byteOffset);
  745. if (*ptr)
  746. {
  747. (*ptr)->Release();
  748. *ptr = 0;
  749. }
  750. }
  751. }
  752. }
  753. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self)
  754. {
  755. return (*self = *other);
  756. }
  757. asCScriptObject &asCScriptObject::operator=(const asCScriptObject &other)
  758. {
  759. CopyFromAs(&other, objType);
  760. return *this;
  761. }
  762. // internal
  763. int asCScriptObject::CopyFromAs(const asCScriptObject *other, asCObjectType *in_objType)
  764. {
  765. if( other != this )
  766. {
  767. if( !other->objType->DerivesFrom(in_objType) )
  768. {
  769. // We cannot allow a value assignment from a type that isn't the same or
  770. // derives from this type as the member properties may not have the same layout
  771. asIScriptContext *ctx = asGetActiveContext();
  772. ctx->SetException(TXT_MISMATCH_IN_VALUE_ASSIGN);
  773. return asERROR;
  774. }
  775. // If the script class implements the opAssign method, it should be called
  776. asCScriptEngine *engine = in_objType->engine;
  777. asCScriptFunction *func = engine->scriptFunctions[in_objType->beh.copy];
  778. if( func->funcType == asFUNC_SYSTEM )
  779. {
  780. // If derived, use the base class' assignment operator to copy the inherited
  781. // properties. Then only copy new properties for the derived class
  782. if( in_objType->derivedFrom )
  783. CopyFromAs(other, in_objType->derivedFrom);
  784. for( asUINT n = in_objType->derivedFrom ? in_objType->derivedFrom->properties.GetLength() : 0;
  785. n < in_objType->properties.GetLength();
  786. n++ )
  787. {
  788. asCObjectProperty *prop = in_objType->properties[n];
  789. if( prop->type.IsObject() )
  790. {
  791. void **dst = (void**)(((char*)this) + prop->byteOffset);
  792. void **src = (void**)(((char*)other) + prop->byteOffset);
  793. if( !prop->type.IsObjectHandle() )
  794. {
  795. if( prop->type.IsReference() || (prop->type.GetTypeInfo()->flags & asOBJ_REF) )
  796. CopyObject(*src, *dst, CastToObjectType(prop->type.GetTypeInfo()), engine);
  797. else
  798. CopyObject(src, dst, CastToObjectType(prop->type.GetTypeInfo()), engine);
  799. }
  800. else
  801. CopyHandle((asPWORD*)src, (asPWORD*)dst, CastToObjectType(prop->type.GetTypeInfo()), engine);
  802. }
  803. else if (prop->type.IsFuncdef())
  804. {
  805. asCScriptFunction **dst = (asCScriptFunction**)(((char*)this) + prop->byteOffset);
  806. asCScriptFunction **src = (asCScriptFunction**)(((char*)other) + prop->byteOffset);
  807. if (*dst)
  808. (*dst)->Release();
  809. *dst = *src;
  810. if (*dst)
  811. (*dst)->AddRef();
  812. }
  813. else
  814. {
  815. void *dst = ((char*)this) + prop->byteOffset;
  816. void *src = ((char*)other) + prop->byteOffset;
  817. memcpy(dst, src, prop->type.GetSizeInMemoryBytes());
  818. }
  819. }
  820. }
  821. else
  822. {
  823. // Reuse the active context or create a new one to call the script class' opAssign method
  824. asIScriptContext *ctx = 0;
  825. int r = 0;
  826. bool isNested = false;
  827. ctx = asGetActiveContext();
  828. if( ctx )
  829. {
  830. if( ctx->GetEngine() == engine && ctx->PushState() == asSUCCESS )
  831. isNested = true;
  832. else
  833. ctx = 0;
  834. }
  835. if( ctx == 0 )
  836. {
  837. // Request a context from the engine
  838. ctx = engine->RequestContext();
  839. if( ctx == 0 )
  840. return asERROR;
  841. }
  842. r = ctx->Prepare(engine->scriptFunctions[in_objType->beh.copy]);
  843. if( r < 0 )
  844. {
  845. if( isNested )
  846. ctx->PopState();
  847. else
  848. engine->ReturnContext(ctx);
  849. return r;
  850. }
  851. r = ctx->SetArgAddress(0, const_cast<asCScriptObject*>(other));
  852. asASSERT( r >= 0 );
  853. r = ctx->SetObject(this);
  854. asASSERT( r >= 0 );
  855. for(;;)
  856. {
  857. r = ctx->Execute();
  858. // We can't allow this execution to be suspended
  859. // so resume the execution immediately
  860. if( r != asEXECUTION_SUSPENDED )
  861. break;
  862. }
  863. if( r != asEXECUTION_FINISHED )
  864. {
  865. if( isNested )
  866. {
  867. ctx->PopState();
  868. // If the execution was aborted or an exception occurred,
  869. // then we should forward that to the outer execution.
  870. if( r == asEXECUTION_EXCEPTION )
  871. {
  872. // TODO: How to improve this exception
  873. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  874. }
  875. else if( r == asEXECUTION_ABORTED )
  876. ctx->Abort();
  877. }
  878. else
  879. {
  880. // Return the context to the engine
  881. engine->ReturnContext(ctx);
  882. }
  883. return asERROR;
  884. }
  885. if( isNested )
  886. ctx->PopState();
  887. else
  888. {
  889. // Return the context to the engine
  890. engine->ReturnContext(ctx);
  891. }
  892. }
  893. }
  894. return asSUCCESS;
  895. }
  896. int asCScriptObject::CopyFrom(const asIScriptObject *other)
  897. {
  898. if( other == 0 ) return asINVALID_ARG;
  899. if( GetTypeId() != other->GetTypeId() )
  900. return asINVALID_TYPE;
  901. *this = *(asCScriptObject*)other;
  902. return asSUCCESS;
  903. }
  904. void *asCScriptObject::AllocateUninitializedObject(asCObjectType *in_objType, asCScriptEngine *engine)
  905. {
  906. void *ptr = 0;
  907. if( in_objType->flags & asOBJ_SCRIPT_OBJECT )
  908. {
  909. ptr = engine->CallAlloc(in_objType);
  910. ScriptObject_ConstructUnitialized(in_objType, reinterpret_cast<asCScriptObject*>(ptr));
  911. }
  912. else if( in_objType->flags & asOBJ_TEMPLATE )
  913. {
  914. // Templates store the original factory that takes the object
  915. // type as a hidden parameter in the construct behaviour
  916. ptr = engine->CallGlobalFunctionRetPtr(in_objType->beh.construct, in_objType);
  917. }
  918. else if( in_objType->flags & asOBJ_REF )
  919. {
  920. ptr = engine->CallGlobalFunctionRetPtr(in_objType->beh.factory);
  921. }
  922. else
  923. {
  924. ptr = engine->CallAlloc(in_objType);
  925. int funcIndex = in_objType->beh.construct;
  926. if( funcIndex )
  927. engine->CallObjectMethod(ptr, funcIndex);
  928. }
  929. return ptr;
  930. }
  931. void asCScriptObject::FreeObject(void *ptr, asCObjectType *in_objType, asCScriptEngine *engine)
  932. {
  933. if( in_objType->flags & asOBJ_REF )
  934. {
  935. asASSERT( (in_objType->flags & asOBJ_NOCOUNT) || in_objType->beh.release );
  936. if(in_objType->beh.release )
  937. engine->CallObjectMethod(ptr, in_objType->beh.release);
  938. }
  939. else
  940. {
  941. if( in_objType->beh.destruct )
  942. engine->CallObjectMethod(ptr, in_objType->beh.destruct);
  943. engine->CallFree(ptr);
  944. }
  945. }
  946. void asCScriptObject::CopyObject(const void *src, void *dst, asCObjectType *in_objType, asCScriptEngine *engine)
  947. {
  948. int funcIndex = in_objType->beh.copy;
  949. if( funcIndex )
  950. {
  951. asCScriptFunction *func = engine->scriptFunctions[in_objType->beh.copy];
  952. if( func->funcType == asFUNC_SYSTEM )
  953. engine->CallObjectMethod(dst, const_cast<void*>(src), funcIndex);
  954. else
  955. {
  956. // Call the script class' opAssign method
  957. asASSERT(in_objType->flags & asOBJ_SCRIPT_OBJECT );
  958. reinterpret_cast<asCScriptObject*>(dst)->CopyFrom(reinterpret_cast<const asCScriptObject*>(src));
  959. }
  960. }
  961. else if( in_objType->size && (in_objType->flags & asOBJ_POD) )
  962. memcpy(dst, src, in_objType->size);
  963. }
  964. void asCScriptObject::CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *in_objType, asCScriptEngine *engine)
  965. {
  966. // asOBJ_NOCOUNT doesn't have addref or release behaviours
  967. asASSERT( (in_objType->flags & asOBJ_NOCOUNT) || (in_objType->beh.release && in_objType->beh.addref) );
  968. if( *dst && in_objType->beh.release )
  969. engine->CallObjectMethod(*(void**)dst, in_objType->beh.release);
  970. *dst = *src;
  971. if( *dst && in_objType->beh.addref )
  972. engine->CallObjectMethod(*(void**)dst, in_objType->beh.addref);
  973. }
  974. // TODO: weak: Should move to its own file
  975. asCLockableSharedBool::asCLockableSharedBool() : value(false)
  976. {
  977. refCount.set(1);
  978. }
  979. int asCLockableSharedBool::AddRef() const
  980. {
  981. return refCount.atomicInc();
  982. }
  983. int asCLockableSharedBool::Release() const
  984. {
  985. int r = refCount.atomicDec();
  986. if( r == 0 )
  987. asDELETE(const_cast<asCLockableSharedBool*>(this), asCLockableSharedBool);
  988. return r;
  989. }
  990. bool asCLockableSharedBool::Get() const
  991. {
  992. return value;
  993. }
  994. void asCLockableSharedBool::Set(bool v)
  995. {
  996. // Make sure the value is not changed while another thread
  997. // is inspecting it and taking a decision on what to do.
  998. Lock();
  999. value = v;
  1000. Unlock();
  1001. }
  1002. void asCLockableSharedBool::Lock() const
  1003. {
  1004. ENTERCRITICALSECTION(lock);
  1005. }
  1006. void asCLockableSharedBool::Unlock() const
  1007. {
  1008. LEAVECRITICALSECTION(lock);
  1009. }
  1010. // Interface
  1011. // Auxiliary function to allow applications to create shared
  1012. // booleans without having to implement the logic for them
  1013. AS_API asILockableSharedBool *asCreateLockableSharedBool()
  1014. {
  1015. return asNEW(asCLockableSharedBool);
  1016. }
  1017. END_AS_NAMESPACE