as_scriptobject.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. // Modified by Lasse Oorni for Urho3D
  24. #include <new>
  25. #include "as_config.h"
  26. #include "as_scriptengine.h"
  27. #include "as_scriptobject.h"
  28. #include "as_texts.h"
  29. BEGIN_AS_NAMESPACE
  30. // This helper function will call the default factory, that is a script function
  31. asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine)
  32. {
  33. asIScriptContext *ctx = 0;
  34. int r = 0;
  35. bool isNested = false;
  36. // TODO: runtime optimize: There should be a pool for the context so it doesn't
  37. // have to be allocated just for creating the script object
  38. // TODO: It must be possible for the application to debug the creation of the object too
  39. // Use nested call in the context if there is an active context
  40. ctx = asGetActiveContext();
  41. if( ctx )
  42. {
  43. r = ctx->PushState();
  44. // It may not always be possible to reuse the current context,
  45. // in which case we'll have to create a new one any way.
  46. if( r == asSUCCESS )
  47. isNested = true;
  48. else
  49. ctx = 0;
  50. }
  51. if( ctx == 0 )
  52. {
  53. r = engine->CreateContext(&ctx, true);
  54. if( r < 0 )
  55. return 0;
  56. }
  57. r = ctx->Prepare(engine->scriptFunctions[objType->beh.factory]);
  58. if( r < 0 )
  59. {
  60. if( isNested )
  61. ctx->PopState();
  62. else
  63. ctx->Release();
  64. return 0;
  65. }
  66. for(;;)
  67. {
  68. r = ctx->Execute();
  69. // We can't allow this execution to be suspended
  70. // so resume the execution immediately
  71. if( r != asEXECUTION_SUSPENDED )
  72. break;
  73. }
  74. if( r != asEXECUTION_FINISHED )
  75. {
  76. if( isNested )
  77. {
  78. ctx->PopState();
  79. // If the execution was aborted or an exception occurred,
  80. // then we should forward that to the outer execution.
  81. if( r == asEXECUTION_EXCEPTION )
  82. {
  83. // TODO: How to improve this exception
  84. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  85. }
  86. else if( r == asEXECUTION_ABORTED )
  87. ctx->Abort();
  88. }
  89. else
  90. ctx->Release();
  91. return 0;
  92. }
  93. asIScriptObject *ptr = (asIScriptObject*)ctx->GetReturnAddress();
  94. // Increase the reference, because the context will release its pointer
  95. ptr->AddRef();
  96. if( isNested )
  97. ctx->PopState();
  98. else
  99. ctx->Release();
  100. return ptr;
  101. }
  102. #ifdef AS_MAX_PORTABILITY
  103. static void ScriptObject_AddRef_Generic(asIScriptGeneric *gen)
  104. {
  105. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  106. self->AddRef();
  107. }
  108. static void ScriptObject_Release_Generic(asIScriptGeneric *gen)
  109. {
  110. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  111. self->Release();
  112. }
  113. static void ScriptObject_GetRefCount_Generic(asIScriptGeneric *gen)
  114. {
  115. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  116. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  117. }
  118. static void ScriptObject_SetFlag_Generic(asIScriptGeneric *gen)
  119. {
  120. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  121. self->SetFlag();
  122. }
  123. static void ScriptObject_GetFlag_Generic(asIScriptGeneric *gen)
  124. {
  125. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  126. *(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
  127. }
  128. static void ScriptObject_GetWeakRefFlag_Generic(asIScriptGeneric *gen)
  129. {
  130. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  131. *(asILockableSharedBool**)gen->GetAddressOfReturnLocation() = self->GetWeakRefFlag();
  132. }
  133. static void ScriptObject_EnumReferences_Generic(asIScriptGeneric *gen)
  134. {
  135. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  136. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  137. self->EnumReferences(engine);
  138. }
  139. static void ScriptObject_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  140. {
  141. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  142. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  143. self->ReleaseAllHandles(engine);
  144. }
  145. #endif
  146. void RegisterScriptObject(asCScriptEngine *engine)
  147. {
  148. // Register the default script class behaviours
  149. int r = 0;
  150. UNUSED_VAR(r); // It is only used in debug mode
  151. engine->scriptTypeBehaviours.engine = engine;
  152. engine->scriptTypeBehaviours.flags = asOBJ_SCRIPT_OBJECT | asOBJ_REF | asOBJ_GC;
  153. engine->scriptTypeBehaviours.name = "_builtin_object_";
  154. #ifndef AS_MAX_PORTABILITY
  155. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct), asCALL_CDECL_OBJLAST, 0); asASSERT( r >= 0 );
  156. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptObject,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  157. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptObject,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  158. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  159. // Weakref behaviours
  160. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GET_WEAKREF_FLAG, "int &f()", asMETHOD(asCScriptObject,GetWeakRefFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  161. // Register GC behaviours
  162. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptObject,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  163. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptObject,SetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  164. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptObject,GetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  165. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptObject,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  166. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptObject,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  167. #else
  168. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  169. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptObject_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  170. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptObject_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  171. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  172. // Weakref behaviours
  173. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GET_WEAKREF_FLAG, "int &f()", asFUNCTION(ScriptObject_GetWeakRefFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  174. // Register GC behaviours
  175. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptObject_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  176. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptObject_SetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  177. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptObject_GetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  178. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptObject_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  179. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptObject_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  180. #endif
  181. }
  182. void ScriptObject_Construct_Generic(asIScriptGeneric *gen)
  183. {
  184. asCObjectType *objType = *(asCObjectType**)gen->GetAddressOfArg(0);
  185. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  186. ScriptObject_Construct(objType, self);
  187. }
  188. void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self)
  189. {
  190. new(self) asCScriptObject(objType);
  191. }
  192. void ScriptObject_ConstructUnitialized(asCObjectType *objType, asCScriptObject *self)
  193. {
  194. new(self) asCScriptObject(objType, false);
  195. }
  196. asCScriptObject::asCScriptObject(asCObjectType *ot, bool doInitialize)
  197. {
  198. refCount.set(1);
  199. objType = ot;
  200. objType->AddRef();
  201. isDestructCalled = false;
  202. weakRefFlag = 0;
  203. hasRefCountReachedZero = false;
  204. // Notify the garbage collector of this object
  205. if( objType->flags & asOBJ_GC )
  206. objType->engine->gc.AddScriptObjectToGC(this, objType);
  207. if( doInitialize )
  208. {
  209. #ifndef AS_NO_MEMBER_INIT
  210. // The actual initialization will be done by the bytecode, so here we should just clear the
  211. // memory to guarantee that no pointers with have scratch values in case of an exception
  212. // TODO: runtime optimize: Is it quicker to just do a memset on the entire object?
  213. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  214. {
  215. asCObjectProperty *prop = objType->properties[n];
  216. if( prop->type.IsObject() )
  217. {
  218. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  219. *ptr = 0;
  220. }
  221. }
  222. #else
  223. // When member initialization is disabled the constructor must make sure
  224. // to allocate and initialize all members with the default constructor
  225. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  226. {
  227. asCObjectProperty *prop = objType->properties[n];
  228. if( prop->type.IsObject() )
  229. {
  230. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  231. if( prop->type.IsObjectHandle() )
  232. *ptr = 0;
  233. else
  234. {
  235. if( prop->type.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT )
  236. *ptr = (asPWORD)ScriptObjectFactory(prop->type.GetObjectType(), ot->engine);
  237. else
  238. *ptr = (asPWORD)AllocateUninitializedObject(prop->type.GetObjectType(), ot->engine);
  239. }
  240. }
  241. }
  242. #endif
  243. }
  244. else
  245. {
  246. // When the object is created without initialization, all non-handle members must be allocated, but not initialized
  247. asCScriptEngine *engine = objType->engine;
  248. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  249. {
  250. asCObjectProperty *prop = objType->properties[n];
  251. if( prop->type.IsObject() )
  252. {
  253. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  254. if( prop->type.IsObjectHandle() )
  255. *ptr = 0;
  256. else
  257. *ptr = (asPWORD)AllocateUninitializedObject(prop->type.GetObjectType(), engine);
  258. }
  259. }
  260. }
  261. // Urho3D: initialize userdata
  262. userData = 0;
  263. }
  264. void asCScriptObject::Destruct()
  265. {
  266. // Call the destructor, which will also call the GCObject's destructor
  267. this->~asCScriptObject();
  268. // Free the memory
  269. userFree(this);
  270. }
  271. asCScriptObject::~asCScriptObject()
  272. {
  273. if( weakRefFlag )
  274. {
  275. weakRefFlag->Release();
  276. weakRefFlag = 0;
  277. }
  278. // The engine pointer should be available from the objectType
  279. asCScriptEngine *engine = objType->engine;
  280. // Destroy all properties
  281. // In most cases the members are initialized in the order they have been declared,
  282. // so it's safer to uninitialize them from last to first. The order may be different
  283. // depending on the use of inheritance and or initialization in the declaration.
  284. // TODO: Should the order of initialization be stored by the compiler so that the
  285. // reverse order can be guaranteed during the destruction?
  286. for( int n = (int)objType->properties.GetLength()-1; n >= 0; n-- )
  287. {
  288. asCObjectProperty *prop = objType->properties[n];
  289. if( prop->type.IsObject() )
  290. {
  291. // Destroy the object
  292. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  293. if( *ptr )
  294. {
  295. FreeObject(*ptr, prop->type.GetObjectType(), engine);
  296. *(asDWORD*)ptr = 0;
  297. }
  298. }
  299. }
  300. objType->Release();
  301. objType = 0;
  302. // Something is really wrong if the refCount is not 0 by now
  303. asASSERT( refCount.get() == 0 );
  304. }
  305. asILockableSharedBool *asCScriptObject::GetWeakRefFlag() const
  306. {
  307. // If the object's refCount has already reached zero then the object is already
  308. // about to be destroyed so it's ok to return null if the weakRefFlag doesn't already
  309. // exist
  310. if( weakRefFlag || hasRefCountReachedZero )
  311. return weakRefFlag;
  312. // Lock globally so no other thread can attempt
  313. // to create a shared bool at the same time.
  314. // TODO: runtime optimize: Instead of locking globally, it would be possible to have
  315. // a critical section per object type. This would reduce the
  316. // chances of two threads lock on the same critical section.
  317. asAcquireExclusiveLock();
  318. // Make sure another thread didn't create the
  319. // flag while we waited for the lock
  320. if( !weakRefFlag )
  321. weakRefFlag = asNEW(asCLockableSharedBool);
  322. asReleaseExclusiveLock();
  323. return weakRefFlag;
  324. }
  325. asIScriptEngine *asCScriptObject::GetEngine() const
  326. {
  327. return objType->engine;
  328. }
  329. int asCScriptObject::AddRef() const
  330. {
  331. // Warn in case the application tries to increase the refCount after it has reached zero.
  332. // This may happen for example if the application calls a method on the class while it is
  333. // being destroyed. The application shouldn't do this because it may cause application
  334. // crashes if members that have already been destroyed are accessed accidentally.
  335. if( hasRefCountReachedZero )
  336. {
  337. if( objType && objType->engine )
  338. {
  339. asCString msg;
  340. msg.Format(TXT_RESURRECTING_SCRIPTOBJECT_s, objType->name.AddressOf());
  341. objType->engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
  342. }
  343. }
  344. // Increase counter and clear flag set by GC
  345. gcFlag = false;
  346. return refCount.atomicInc();
  347. }
  348. int asCScriptObject::Release() const
  349. {
  350. // Clear the flag set by the GC
  351. gcFlag = false;
  352. // If the weak ref flag exists it is because someone held a weak ref
  353. // and that someone may add a reference to the object at any time. It
  354. // is ok to check the existance of the weakRefFlag without locking here
  355. // because if the refCount is 1 then no other thread is currently
  356. // creating the weakRefFlag.
  357. if( refCount.get() == 1 && weakRefFlag )
  358. {
  359. // Set the flag to tell others that the object is no longer alive
  360. // We must do this before decreasing the refCount to 0 so we don't
  361. // end up with a race condition between this thread attempting to
  362. // destroy the object and the other that temporary added a strong
  363. // ref from the weak ref.
  364. weakRefFlag->Set(true);
  365. }
  366. // Call the script destructor behaviour if the reference counter is 1.
  367. if( refCount.get() == 1 && !isDestructCalled )
  368. {
  369. // This cast is OK since we are the last reference
  370. const_cast<asCScriptObject*>(this)->CallDestructor();
  371. }
  372. // Now do the actual releasing
  373. int r = refCount.atomicDec();
  374. if( r == 0 )
  375. {
  376. // Flag this object as being destroyed so the application
  377. // can be warned if the code attempts to resurrect the object
  378. // during the destructor. This also avoids a recursive call
  379. // to the destructor which would crash the application if it
  380. // really does resurrect the object.
  381. if( !hasRefCountReachedZero )
  382. {
  383. hasRefCountReachedZero = true;
  384. // This cast is OK since we are the last reference
  385. const_cast<asCScriptObject*>(this)->Destruct();
  386. }
  387. return 0;
  388. }
  389. return r;
  390. }
  391. void asCScriptObject::CallDestructor()
  392. {
  393. // Only allow the destructor to be called once
  394. if( isDestructCalled ) return;
  395. asIScriptContext *ctx = 0;
  396. bool isNested = false;
  397. bool doAbort = false;
  398. // Make sure the destructor is called once only, even if the
  399. // reference count is increased and then decreased again
  400. isDestructCalled = true;
  401. // Call the destructor for this class and all the super classes
  402. asCObjectType *ot = objType;
  403. while( ot )
  404. {
  405. int funcIndex = ot->beh.destruct;
  406. if( funcIndex )
  407. {
  408. if( ctx == 0 )
  409. {
  410. // Check for active context first as it is quicker
  411. // to reuse than to set up a new one.
  412. ctx = asGetActiveContext();
  413. if( ctx )
  414. {
  415. int r = ctx->PushState();
  416. if( r == asSUCCESS )
  417. isNested = true;
  418. else
  419. ctx = 0;
  420. }
  421. if( ctx == 0 )
  422. {
  423. // Setup a context for calling the default constructor
  424. asCScriptEngine *engine = objType->engine;
  425. int r = engine->CreateContext(&ctx, true);
  426. if( r < 0 ) return;
  427. }
  428. }
  429. int r = ctx->Prepare(objType->engine->scriptFunctions[funcIndex]);
  430. if( r >= 0 )
  431. {
  432. ctx->SetObject(this);
  433. for(;;)
  434. {
  435. r = ctx->Execute();
  436. // If the script tries to suspend itself just restart it
  437. if( r != asEXECUTION_SUSPENDED )
  438. break;
  439. }
  440. // Exceptions in the destructor will be ignored, as there is not much
  441. // that can be done about them. However a request to abort the execution
  442. // will be forwarded to the outer execution, in case of a nested call.
  443. if( r == asEXECUTION_ABORTED )
  444. doAbort = true;
  445. // Observe, even though the current destructor was aborted or an exception
  446. // occurred, we still try to execute the base class' destructor if available
  447. // in order to free as many resources as possible.
  448. }
  449. }
  450. ot = ot->derivedFrom;
  451. }
  452. if( ctx )
  453. {
  454. if( isNested )
  455. {
  456. ctx->PopState();
  457. // Forward any request to abort the execution to the outer call
  458. if( doAbort )
  459. ctx->Abort();
  460. }
  461. else
  462. ctx->Release();
  463. }
  464. }
  465. asIObjectType *asCScriptObject::GetObjectType() const
  466. {
  467. return objType;
  468. }
  469. int asCScriptObject::GetRefCount()
  470. {
  471. return refCount.get();
  472. }
  473. void asCScriptObject::SetFlag()
  474. {
  475. gcFlag = true;
  476. }
  477. bool asCScriptObject::GetFlag()
  478. {
  479. return gcFlag;
  480. }
  481. // interface
  482. int asCScriptObject::GetTypeId() const
  483. {
  484. asCDataType dt = asCDataType::CreateObject(objType, false);
  485. return objType->engine->GetTypeIdFromDataType(dt);
  486. }
  487. // Urho3D: added function
  488. void *asCScriptObject::SetUserData(void *data)
  489. {
  490. void *oldData = userData;
  491. userData = data;
  492. return oldData;
  493. }
  494. // Urho3D: added function
  495. void *asCScriptObject::GetUserData() const
  496. {
  497. return userData;
  498. }
  499. asUINT asCScriptObject::GetPropertyCount() const
  500. {
  501. return asUINT(objType->properties.GetLength());
  502. }
  503. int asCScriptObject::GetPropertyTypeId(asUINT prop) const
  504. {
  505. if( prop >= objType->properties.GetLength() )
  506. return asINVALID_ARG;
  507. return objType->engine->GetTypeIdFromDataType(objType->properties[prop]->type);
  508. }
  509. const char *asCScriptObject::GetPropertyName(asUINT prop) const
  510. {
  511. if( prop >= objType->properties.GetLength() )
  512. return 0;
  513. return objType->properties[prop]->name.AddressOf();
  514. }
  515. void *asCScriptObject::GetAddressOfProperty(asUINT prop)
  516. {
  517. if( prop >= objType->properties.GetLength() )
  518. return 0;
  519. // Objects are stored by reference, so this must be dereferenced
  520. asCDataType *dt = &objType->properties[prop]->type;
  521. if( dt->IsObject() && !dt->IsObjectHandle() )
  522. return *(void**)(((char*)this) + objType->properties[prop]->byteOffset);
  523. return (void*)(((char*)this) + objType->properties[prop]->byteOffset);
  524. }
  525. void asCScriptObject::EnumReferences(asIScriptEngine *engine)
  526. {
  527. // We'll notify the GC of all object handles that we're holding
  528. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  529. {
  530. asCObjectProperty *prop = objType->properties[n];
  531. if( prop->type.IsObject() )
  532. {
  533. void *ptr = *(void**)(((char*)this) + prop->byteOffset);
  534. if( ptr )
  535. ((asCScriptEngine*)engine)->GCEnumCallback(ptr);
  536. }
  537. }
  538. }
  539. void asCScriptObject::ReleaseAllHandles(asIScriptEngine *engine)
  540. {
  541. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  542. {
  543. asCObjectProperty *prop = objType->properties[n];
  544. if( prop->type.IsObject() && prop->type.IsObjectHandle() )
  545. {
  546. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  547. if( *ptr )
  548. {
  549. asASSERT( (prop->type.GetObjectType()->flags & asOBJ_NOCOUNT) || prop->type.GetBehaviour()->release );
  550. if( prop->type.GetBehaviour()->release )
  551. ((asCScriptEngine*)engine)->CallObjectMethod(*ptr, prop->type.GetBehaviour()->release);
  552. *ptr = 0;
  553. }
  554. }
  555. }
  556. }
  557. void ScriptObject_Assignment_Generic(asIScriptGeneric *gen)
  558. {
  559. asCScriptObject *other = *(asCScriptObject**)gen->GetAddressOfArg(0);
  560. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  561. *self = *other;
  562. *(asCScriptObject**)gen->GetAddressOfReturnLocation() = self;
  563. }
  564. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self)
  565. {
  566. return (*self = *other);
  567. }
  568. asCScriptObject &asCScriptObject::operator=(const asCScriptObject &other)
  569. {
  570. if( &other != this )
  571. {
  572. asASSERT( other.objType->DerivesFrom(objType) );
  573. // If the script class implements the opAssign method, it should be called
  574. asCScriptEngine *engine = objType->engine;
  575. asCScriptFunction *func = engine->scriptFunctions[objType->beh.copy];
  576. if( func->funcType == asFUNC_SYSTEM )
  577. {
  578. // Copy all properties
  579. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  580. {
  581. asCObjectProperty *prop = objType->properties[n];
  582. if( prop->type.IsObject() )
  583. {
  584. void **dst = (void**)(((char*)this) + prop->byteOffset);
  585. void **src = (void**)(((char*)&other) + prop->byteOffset);
  586. if( !prop->type.IsObjectHandle() )
  587. CopyObject(*src, *dst, prop->type.GetObjectType(), engine);
  588. else
  589. CopyHandle((asPWORD*)src, (asPWORD*)dst, prop->type.GetObjectType(), engine);
  590. }
  591. else
  592. {
  593. void *dst = ((char*)this) + prop->byteOffset;
  594. void *src = ((char*)&other) + prop->byteOffset;
  595. memcpy(dst, src, prop->type.GetSizeInMemoryBytes());
  596. }
  597. }
  598. }
  599. else
  600. {
  601. // Reuse the active context or create a new one to call the script class' opAssign method
  602. asIScriptContext *ctx = 0;
  603. int r = 0;
  604. bool isNested = false;
  605. ctx = asGetActiveContext();
  606. if( ctx )
  607. {
  608. r = ctx->PushState();
  609. if( r == asSUCCESS )
  610. isNested = true;
  611. else
  612. ctx = 0;
  613. }
  614. if( ctx == 0 )
  615. {
  616. r = engine->CreateContext(&ctx, true);
  617. if( r < 0 )
  618. return *this;
  619. }
  620. r = ctx->Prepare(engine->scriptFunctions[objType->beh.copy]);
  621. if( r < 0 )
  622. {
  623. if( isNested )
  624. ctx->PopState();
  625. else
  626. ctx->Release();
  627. return *this;
  628. }
  629. r = ctx->SetArgAddress(0, const_cast<asCScriptObject*>(&other));
  630. asASSERT( r >= 0 );
  631. r = ctx->SetObject(this);
  632. asASSERT( r >= 0 );
  633. for(;;)
  634. {
  635. r = ctx->Execute();
  636. // We can't allow this execution to be suspended
  637. // so resume the execution immediately
  638. if( r != asEXECUTION_SUSPENDED )
  639. break;
  640. }
  641. if( r != asEXECUTION_FINISHED )
  642. {
  643. if( isNested )
  644. {
  645. ctx->PopState();
  646. // If the execution was aborted or an exception occurred,
  647. // then we should forward that to the outer execution.
  648. if( r == asEXECUTION_EXCEPTION )
  649. {
  650. // TODO: How to improve this exception
  651. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  652. }
  653. else if( r == asEXECUTION_ABORTED )
  654. ctx->Abort();
  655. }
  656. else
  657. ctx->Release();
  658. return *this;
  659. }
  660. if( isNested )
  661. ctx->PopState();
  662. else
  663. ctx->Release();
  664. }
  665. }
  666. return *this;
  667. }
  668. int asCScriptObject::CopyFrom(asIScriptObject *other)
  669. {
  670. if( other == 0 ) return asINVALID_ARG;
  671. if( GetTypeId() != other->GetTypeId() )
  672. return asINVALID_TYPE;
  673. *this = *(asCScriptObject*)other;
  674. return 0;
  675. }
  676. void *asCScriptObject::AllocateUninitializedObject(asCObjectType *objType, asCScriptEngine *engine)
  677. {
  678. void *ptr = 0;
  679. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  680. {
  681. ptr = engine->CallAlloc(objType);
  682. ScriptObject_ConstructUnitialized(objType, reinterpret_cast<asCScriptObject*>(ptr));
  683. }
  684. else if( objType->flags & asOBJ_TEMPLATE )
  685. {
  686. // Templates store the original factory that takes the object
  687. // type as a hidden parameter in the construct behaviour
  688. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  689. }
  690. else if( objType->flags & asOBJ_REF )
  691. {
  692. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.factory);
  693. }
  694. else
  695. {
  696. ptr = engine->CallAlloc(objType);
  697. int funcIndex = objType->beh.construct;
  698. if( funcIndex )
  699. engine->CallObjectMethod(ptr, funcIndex);
  700. }
  701. return ptr;
  702. }
  703. void asCScriptObject::FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine)
  704. {
  705. if( objType->flags & asOBJ_REF )
  706. {
  707. asASSERT( (objType->flags & asOBJ_NOCOUNT) || objType->beh.release );
  708. if( objType->beh.release )
  709. engine->CallObjectMethod(ptr, objType->beh.release);
  710. }
  711. else
  712. {
  713. if( objType->beh.destruct )
  714. engine->CallObjectMethod(ptr, objType->beh.destruct);
  715. engine->CallFree(ptr);
  716. }
  717. }
  718. void asCScriptObject::CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine)
  719. {
  720. int funcIndex = objType->beh.copy;
  721. if( funcIndex )
  722. {
  723. asCScriptFunction *func = engine->scriptFunctions[objType->beh.copy];
  724. if( func->funcType == asFUNC_SYSTEM )
  725. engine->CallObjectMethod(dst, src, funcIndex);
  726. else
  727. {
  728. // Call the script class' opAssign method
  729. asASSERT( objType->flags & asOBJ_SCRIPT_OBJECT );
  730. reinterpret_cast<asCScriptObject*>(dst)->CopyFrom(reinterpret_cast<asCScriptObject*>(src));
  731. }
  732. }
  733. else if( objType->size && (objType->flags & asOBJ_POD) )
  734. memcpy(dst, src, objType->size);
  735. }
  736. void asCScriptObject::CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine)
  737. {
  738. // asOBJ_NOCOUNT doesn't have addref or release behaviours
  739. asASSERT( (objType->flags & asOBJ_NOCOUNT) || (objType->beh.release && objType->beh.addref) );
  740. if( *dst && objType->beh.release )
  741. engine->CallObjectMethod(*(void**)dst, objType->beh.release);
  742. *dst = *src;
  743. if( *dst && objType->beh.addref )
  744. engine->CallObjectMethod(*(void**)dst, objType->beh.addref);
  745. }
  746. // TODO: weak: Should move to its own file
  747. asCLockableSharedBool::asCLockableSharedBool() : value(false)
  748. {
  749. refCount.set(1);
  750. }
  751. int asCLockableSharedBool::AddRef() const
  752. {
  753. return refCount.atomicInc();
  754. }
  755. int asCLockableSharedBool::Release() const
  756. {
  757. int r = refCount.atomicDec();
  758. if( r == 0 )
  759. asDELETE(const_cast<asCLockableSharedBool*>(this), asCLockableSharedBool);
  760. return r;
  761. }
  762. bool asCLockableSharedBool::Get() const
  763. {
  764. return value;
  765. }
  766. void asCLockableSharedBool::Set(bool v)
  767. {
  768. // Make sure the value is not changed while another thread
  769. // is inspecting it and taking a decision on what to do.
  770. Lock();
  771. value = v;
  772. Unlock();
  773. }
  774. void asCLockableSharedBool::Lock() const
  775. {
  776. ENTERCRITICALSECTION(lock);
  777. }
  778. void asCLockableSharedBool::Unlock() const
  779. {
  780. LEAVECRITICALSECTION(lock);
  781. }
  782. // Interface
  783. // Auxiliary function to allow applications to create shared
  784. // booleans without having to implement the logic for them
  785. AS_API asILockableSharedBool *asCreateLockableSharedBool()
  786. {
  787. return asNEW(asCLockableSharedBool);
  788. }
  789. END_AS_NAMESPACE