as_scriptobject.cpp 26 KB

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