as_scriptobject.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. // Notify the garbage collector of this object
  204. if( objType->flags & asOBJ_GC )
  205. objType->engine->gc.AddScriptObjectToGC(this, objType);
  206. if( doInitialize )
  207. {
  208. #ifndef AS_NO_MEMBER_INIT
  209. // The actual initialization will be done by the bytecode, so here we should just clear the
  210. // memory to guarantee that no pointers with have scratch values in case of an exception
  211. // TODO: runtime optimize: Is it quicker to just do a memset on the entire object?
  212. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  213. {
  214. asCObjectProperty *prop = objType->properties[n];
  215. if( prop->type.IsObject() )
  216. {
  217. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  218. *ptr = 0;
  219. }
  220. }
  221. #else
  222. // When member initialization is disabled the constructor must make sure
  223. // to allocate and initialize all members with the default constructor
  224. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  225. {
  226. asCObjectProperty *prop = objType->properties[n];
  227. if( prop->type.IsObject() )
  228. {
  229. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  230. if( prop->type.IsObjectHandle() )
  231. *ptr = 0;
  232. else
  233. {
  234. if( prop->type.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT )
  235. *ptr = (asPWORD)ScriptObjectFactory(prop->type.GetObjectType(), ot->engine);
  236. else
  237. *ptr = (asPWORD)AllocateUninitializedObject(prop->type.GetObjectType(), ot->engine);
  238. }
  239. }
  240. }
  241. #endif
  242. }
  243. else
  244. {
  245. // When the object is created without initialization, all non-handle members must be allocated, but not initialized
  246. asCScriptEngine *engine = objType->engine;
  247. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  248. {
  249. asCObjectProperty *prop = objType->properties[n];
  250. if( prop->type.IsObject() )
  251. {
  252. asPWORD *ptr = reinterpret_cast<asPWORD*>(reinterpret_cast<asBYTE*>(this) + prop->byteOffset);
  253. if( prop->type.IsObjectHandle() )
  254. *ptr = 0;
  255. else
  256. *ptr = (asPWORD)AllocateUninitializedObject(prop->type.GetObjectType(), engine);
  257. }
  258. }
  259. }
  260. // Urho3D: initialize userdata
  261. userData = 0;
  262. }
  263. void asCScriptObject::Destruct()
  264. {
  265. // Call the destructor, which will also call the GCObject's destructor
  266. this->~asCScriptObject();
  267. // Free the memory
  268. userFree(this);
  269. }
  270. asCScriptObject::~asCScriptObject()
  271. {
  272. if( weakRefFlag )
  273. weakRefFlag->Release();
  274. // The engine pointer should be available from the objectType
  275. asCScriptEngine *engine = objType->engine;
  276. // Destroy all properties
  277. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  278. {
  279. asCObjectProperty *prop = objType->properties[n];
  280. if( prop->type.IsObject() )
  281. {
  282. // Destroy the object
  283. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  284. if( *ptr )
  285. {
  286. FreeObject(*ptr, prop->type.GetObjectType(), engine);
  287. *(asDWORD*)ptr = 0;
  288. }
  289. }
  290. }
  291. objType->Release();
  292. }
  293. asILockableSharedBool *asCScriptObject::GetWeakRefFlag() const
  294. {
  295. if( weakRefFlag )
  296. return weakRefFlag;
  297. // Lock globally so no other thread can attempt
  298. // to create a shared bool at the same time.
  299. // TODO: runtime optimize: Instead of locking globally, it would be possible to have
  300. // a critical section per object type. This would reduce the
  301. // chances of two threads lock on the same critical section.
  302. asAcquireExclusiveLock();
  303. // Make sure another thread didn't create the
  304. // flag while we waited for the lock
  305. if( !weakRefFlag )
  306. weakRefFlag = asNEW(asCLockableSharedBool);
  307. asReleaseExclusiveLock();
  308. return weakRefFlag;
  309. }
  310. asIScriptEngine *asCScriptObject::GetEngine() const
  311. {
  312. return objType->engine;
  313. }
  314. int asCScriptObject::AddRef() const
  315. {
  316. // Increase counter and clear flag set by GC
  317. gcFlag = false;
  318. return refCount.atomicInc();
  319. }
  320. int asCScriptObject::Release() const
  321. {
  322. // Clear the flag set by the GC
  323. gcFlag = false;
  324. // If the weak ref flag exists it is because someone held a weak ref
  325. // and that someone may add a reference to the object at any time. It
  326. // is ok to check the existance of the weakRefFlag without locking here
  327. // because if the refCount is 1 then no other thread is currently
  328. // creating the weakRefFlag.
  329. if( refCount.get() == 1 && weakRefFlag )
  330. {
  331. // Set the flag to tell others that the object is no longer alive
  332. // We must do this before decreasing the refCount to 0 so we don't
  333. // end up with a race condition between this thread attempting to
  334. // destroy the object and the other that temporary added a strong
  335. // ref from the weak ref.
  336. weakRefFlag->Set(true);
  337. }
  338. // Call the script destructor behaviour if the reference counter is 1.
  339. if( refCount.get() == 1 && !isDestructCalled )
  340. {
  341. // This cast is OK since we are the last reference
  342. const_cast<asCScriptObject*>(this)->CallDestructor();
  343. }
  344. // Now do the actual releasing
  345. int r = refCount.atomicDec();
  346. if( r == 0 )
  347. {
  348. // This cast is OK since we are the last reference
  349. const_cast<asCScriptObject*>(this)->Destruct();
  350. return 0;
  351. }
  352. return r;
  353. }
  354. void asCScriptObject::CallDestructor()
  355. {
  356. // Only allow the destructor to be called once
  357. if( isDestructCalled ) return;
  358. asIScriptContext *ctx = 0;
  359. bool isNested = false;
  360. bool doAbort = false;
  361. // Make sure the destructor is called once only, even if the
  362. // reference count is increased and then decreased again
  363. isDestructCalled = true;
  364. // Call the destructor for this class and all the super classes
  365. asCObjectType *ot = objType;
  366. while( ot )
  367. {
  368. int funcIndex = ot->beh.destruct;
  369. if( funcIndex )
  370. {
  371. if( ctx == 0 )
  372. {
  373. // Check for active context first as it is quicker
  374. // to reuse than to set up a new one.
  375. ctx = asGetActiveContext();
  376. if( ctx )
  377. {
  378. int r = ctx->PushState();
  379. if( r == asSUCCESS )
  380. isNested = true;
  381. else
  382. ctx = 0;
  383. }
  384. if( ctx == 0 )
  385. {
  386. // Setup a context for calling the default constructor
  387. asCScriptEngine *engine = objType->engine;
  388. int r = engine->CreateContext(&ctx, true);
  389. if( r < 0 ) return;
  390. }
  391. }
  392. int r = ctx->Prepare(objType->engine->scriptFunctions[funcIndex]);
  393. if( r >= 0 )
  394. {
  395. ctx->SetObject(this);
  396. for(;;)
  397. {
  398. r = ctx->Execute();
  399. // If the script tries to suspend itself just restart it
  400. if( r != asEXECUTION_SUSPENDED )
  401. break;
  402. }
  403. // Exceptions in the destructor will be ignored, as there is not much
  404. // that can be done about them. However a request to abort the execution
  405. // will be forwarded to the outer execution, in case of a nested call.
  406. if( r == asEXECUTION_ABORTED )
  407. doAbort = true;
  408. // Observe, even though the current destructor was aborted or an exception
  409. // occurred, we still try to execute the base class' destructor if available
  410. // in order to free as many resources as possible.
  411. }
  412. }
  413. ot = ot->derivedFrom;
  414. }
  415. if( ctx )
  416. {
  417. if( isNested )
  418. {
  419. ctx->PopState();
  420. // Forward any request to abort the execution to the outer call
  421. if( doAbort )
  422. ctx->Abort();
  423. }
  424. else
  425. ctx->Release();
  426. }
  427. }
  428. asIObjectType *asCScriptObject::GetObjectType() const
  429. {
  430. return objType;
  431. }
  432. int asCScriptObject::GetRefCount()
  433. {
  434. return refCount.get();
  435. }
  436. void asCScriptObject::SetFlag()
  437. {
  438. gcFlag = true;
  439. }
  440. bool asCScriptObject::GetFlag()
  441. {
  442. return gcFlag;
  443. }
  444. // interface
  445. int asCScriptObject::GetTypeId() const
  446. {
  447. asCDataType dt = asCDataType::CreateObject(objType, false);
  448. return objType->engine->GetTypeIdFromDataType(dt);
  449. }
  450. // Urho3D: added function
  451. void *asCScriptObject::SetUserData(void *data)
  452. {
  453. void *oldData = userData;
  454. userData = data;
  455. return oldData;
  456. }
  457. // Urho3D: added function
  458. void *asCScriptObject::GetUserData() const
  459. {
  460. return userData;
  461. }
  462. asUINT asCScriptObject::GetPropertyCount() const
  463. {
  464. return asUINT(objType->properties.GetLength());
  465. }
  466. int asCScriptObject::GetPropertyTypeId(asUINT prop) const
  467. {
  468. if( prop >= objType->properties.GetLength() )
  469. return asINVALID_ARG;
  470. return objType->engine->GetTypeIdFromDataType(objType->properties[prop]->type);
  471. }
  472. const char *asCScriptObject::GetPropertyName(asUINT prop) const
  473. {
  474. if( prop >= objType->properties.GetLength() )
  475. return 0;
  476. return objType->properties[prop]->name.AddressOf();
  477. }
  478. void *asCScriptObject::GetAddressOfProperty(asUINT prop)
  479. {
  480. if( prop >= objType->properties.GetLength() )
  481. return 0;
  482. // Objects are stored by reference, so this must be dereferenced
  483. asCDataType *dt = &objType->properties[prop]->type;
  484. if( dt->IsObject() && !dt->IsObjectHandle() )
  485. return *(void**)(((char*)this) + objType->properties[prop]->byteOffset);
  486. return (void*)(((char*)this) + objType->properties[prop]->byteOffset);
  487. }
  488. void asCScriptObject::EnumReferences(asIScriptEngine *engine)
  489. {
  490. // We'll notify the GC of all object handles that we're holding
  491. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  492. {
  493. asCObjectProperty *prop = objType->properties[n];
  494. if( prop->type.IsObject() )
  495. {
  496. void *ptr = *(void**)(((char*)this) + prop->byteOffset);
  497. if( ptr )
  498. ((asCScriptEngine*)engine)->GCEnumCallback(ptr);
  499. }
  500. }
  501. }
  502. void asCScriptObject::ReleaseAllHandles(asIScriptEngine *engine)
  503. {
  504. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  505. {
  506. asCObjectProperty *prop = objType->properties[n];
  507. if( prop->type.IsObject() && prop->type.IsObjectHandle() )
  508. {
  509. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  510. if( *ptr )
  511. {
  512. asASSERT( (prop->type.GetObjectType()->flags & asOBJ_NOCOUNT) || prop->type.GetBehaviour()->release );
  513. if( prop->type.GetBehaviour()->release )
  514. ((asCScriptEngine*)engine)->CallObjectMethod(*ptr, prop->type.GetBehaviour()->release);
  515. *ptr = 0;
  516. }
  517. }
  518. }
  519. }
  520. void ScriptObject_Assignment_Generic(asIScriptGeneric *gen)
  521. {
  522. asCScriptObject *other = *(asCScriptObject**)gen->GetAddressOfArg(0);
  523. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  524. *self = *other;
  525. *(asCScriptObject**)gen->GetAddressOfReturnLocation() = self;
  526. }
  527. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self)
  528. {
  529. return (*self = *other);
  530. }
  531. asCScriptObject &asCScriptObject::operator=(const asCScriptObject &other)
  532. {
  533. if( &other != this )
  534. {
  535. asASSERT( other.objType->DerivesFrom(objType) );
  536. // If the script class implements the opAssign method, it should be called
  537. asCScriptEngine *engine = objType->engine;
  538. asCScriptFunction *func = engine->scriptFunctions[objType->beh.copy];
  539. if( func->funcType == asFUNC_SYSTEM )
  540. {
  541. // Copy all properties
  542. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  543. {
  544. asCObjectProperty *prop = objType->properties[n];
  545. if( prop->type.IsObject() )
  546. {
  547. void **dst = (void**)(((char*)this) + prop->byteOffset);
  548. void **src = (void**)(((char*)&other) + prop->byteOffset);
  549. if( !prop->type.IsObjectHandle() )
  550. CopyObject(*src, *dst, prop->type.GetObjectType(), engine);
  551. else
  552. CopyHandle((asPWORD*)src, (asPWORD*)dst, prop->type.GetObjectType(), engine);
  553. }
  554. else
  555. {
  556. void *dst = ((char*)this) + prop->byteOffset;
  557. void *src = ((char*)&other) + prop->byteOffset;
  558. memcpy(dst, src, prop->type.GetSizeInMemoryBytes());
  559. }
  560. }
  561. }
  562. else
  563. {
  564. // Reuse the active context or create a new one to call the script class' opAssign method
  565. asIScriptContext *ctx = 0;
  566. int r = 0;
  567. bool isNested = false;
  568. ctx = asGetActiveContext();
  569. if( ctx )
  570. {
  571. r = ctx->PushState();
  572. if( r == asSUCCESS )
  573. isNested = true;
  574. else
  575. ctx = 0;
  576. }
  577. if( ctx == 0 )
  578. {
  579. r = engine->CreateContext(&ctx, true);
  580. if( r < 0 )
  581. return *this;
  582. }
  583. r = ctx->Prepare(engine->scriptFunctions[objType->beh.copy]);
  584. if( r < 0 )
  585. {
  586. if( isNested )
  587. ctx->PopState();
  588. else
  589. ctx->Release();
  590. return *this;
  591. }
  592. r = ctx->SetArgAddress(0, const_cast<asCScriptObject*>(&other));
  593. asASSERT( r >= 0 );
  594. r = ctx->SetObject(this);
  595. asASSERT( r >= 0 );
  596. for(;;)
  597. {
  598. r = ctx->Execute();
  599. // We can't allow this execution to be suspended
  600. // so resume the execution immediately
  601. if( r != asEXECUTION_SUSPENDED )
  602. break;
  603. }
  604. if( r != asEXECUTION_FINISHED )
  605. {
  606. if( isNested )
  607. {
  608. ctx->PopState();
  609. // If the execution was aborted or an exception occurred,
  610. // then we should forward that to the outer execution.
  611. if( r == asEXECUTION_EXCEPTION )
  612. {
  613. // TODO: How to improve this exception
  614. ctx->SetException(TXT_EXCEPTION_IN_NESTED_CALL);
  615. }
  616. else if( r == asEXECUTION_ABORTED )
  617. ctx->Abort();
  618. }
  619. else
  620. ctx->Release();
  621. return *this;
  622. }
  623. if( isNested )
  624. ctx->PopState();
  625. else
  626. ctx->Release();
  627. }
  628. }
  629. return *this;
  630. }
  631. int asCScriptObject::CopyFrom(asIScriptObject *other)
  632. {
  633. if( other == 0 ) return asINVALID_ARG;
  634. if( GetTypeId() != other->GetTypeId() )
  635. return asINVALID_TYPE;
  636. *this = *(asCScriptObject*)other;
  637. return 0;
  638. }
  639. void *asCScriptObject::AllocateUninitializedObject(asCObjectType *objType, asCScriptEngine *engine)
  640. {
  641. void *ptr = 0;
  642. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  643. {
  644. ptr = engine->CallAlloc(objType);
  645. ScriptObject_ConstructUnitialized(objType, reinterpret_cast<asCScriptObject*>(ptr));
  646. }
  647. else if( objType->flags & asOBJ_TEMPLATE )
  648. {
  649. // Templates store the original factory that takes the object
  650. // type as a hidden parameter in the construct behaviour
  651. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  652. }
  653. else if( objType->flags & asOBJ_REF )
  654. {
  655. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.factory);
  656. }
  657. else
  658. {
  659. ptr = engine->CallAlloc(objType);
  660. int funcIndex = objType->beh.construct;
  661. if( funcIndex )
  662. engine->CallObjectMethod(ptr, funcIndex);
  663. }
  664. return ptr;
  665. }
  666. void asCScriptObject::FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine)
  667. {
  668. if( objType->flags & asOBJ_REF )
  669. {
  670. asASSERT( (objType->flags & asOBJ_NOCOUNT) || objType->beh.release );
  671. if( objType->beh.release )
  672. engine->CallObjectMethod(ptr, objType->beh.release);
  673. }
  674. else
  675. {
  676. if( objType->beh.destruct )
  677. engine->CallObjectMethod(ptr, objType->beh.destruct);
  678. engine->CallFree(ptr);
  679. }
  680. }
  681. void asCScriptObject::CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine)
  682. {
  683. int funcIndex = objType->beh.copy;
  684. if( funcIndex )
  685. {
  686. asCScriptFunction *func = engine->scriptFunctions[objType->beh.copy];
  687. if( func->funcType == asFUNC_SYSTEM )
  688. engine->CallObjectMethod(dst, src, funcIndex);
  689. else
  690. {
  691. // Call the script class' opAssign method
  692. asASSERT( objType->flags & asOBJ_SCRIPT_OBJECT );
  693. reinterpret_cast<asCScriptObject*>(dst)->CopyFrom(reinterpret_cast<asCScriptObject*>(src));
  694. }
  695. }
  696. else if( objType->size && (objType->flags & asOBJ_POD) )
  697. memcpy(dst, src, objType->size);
  698. }
  699. void asCScriptObject::CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine)
  700. {
  701. // asOBJ_NOCOUNT doesn't have addref or release behaviours
  702. asASSERT( (objType->flags & asOBJ_NOCOUNT) || (objType->beh.release && objType->beh.addref) );
  703. if( *dst && objType->beh.release )
  704. engine->CallObjectMethod(*(void**)dst, objType->beh.release);
  705. *dst = *src;
  706. if( *dst && objType->beh.addref )
  707. engine->CallObjectMethod(*(void**)dst, objType->beh.addref);
  708. }
  709. // TODO: weak: Should move to its own file
  710. asCLockableSharedBool::asCLockableSharedBool() : value(false)
  711. {
  712. refCount.set(1);
  713. }
  714. int asCLockableSharedBool::AddRef() const
  715. {
  716. return refCount.atomicInc();
  717. }
  718. int asCLockableSharedBool::Release() const
  719. {
  720. int r = refCount.atomicDec();
  721. if( r == 0 )
  722. asDELETE(const_cast<asCLockableSharedBool*>(this), asCLockableSharedBool);
  723. return r;
  724. }
  725. bool asCLockableSharedBool::Get() const
  726. {
  727. return value;
  728. }
  729. void asCLockableSharedBool::Set(bool v)
  730. {
  731. // Make sure the value is not changed while another thread
  732. // is inspecting it and taking a decision on what to do.
  733. Lock();
  734. value = v;
  735. Unlock();
  736. }
  737. void asCLockableSharedBool::Lock() const
  738. {
  739. ENTERCRITICALSECTION(lock);
  740. }
  741. void asCLockableSharedBool::Unlock() const
  742. {
  743. LEAVECRITICALSECTION(lock);
  744. }
  745. // Interface
  746. // Auxiliary function to allow applications to create shared
  747. // booleans without having to implement the logic for them
  748. AS_API asILockableSharedBool *asCreateLockableSharedBool()
  749. {
  750. return asNEW(asCLockableSharedBool);
  751. }
  752. END_AS_NAMESPACE