as_scriptobject.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. #include <new>
  24. #include "as_config.h"
  25. #include "as_scriptengine.h"
  26. #include "as_scriptobject.h"
  27. BEGIN_AS_NAMESPACE
  28. // This helper function will call the default factory, that is a script function
  29. asIScriptObject *ScriptObjectFactory(const asCObjectType *objType, asCScriptEngine *engine)
  30. {
  31. asIScriptContext *ctx;
  32. // TODO: optimize: There should be a pool for the context so it doesn't
  33. // have to be allocated just for creating the script object
  34. // TODO: It must be possible for the application to debug the creation of the object too
  35. int r = engine->CreateContext(&ctx, true);
  36. if( r < 0 )
  37. return 0;
  38. r = ctx->Prepare(objType->beh.factory);
  39. if( r < 0 )
  40. {
  41. ctx->Release();
  42. return 0;
  43. }
  44. r = ctx->Execute();
  45. if( r != asEXECUTION_FINISHED )
  46. {
  47. ctx->Release();
  48. return 0;
  49. }
  50. asIScriptObject *ptr = (asIScriptObject*)ctx->GetReturnAddress();
  51. // Increase the reference, because the context will release it's pointer
  52. ptr->AddRef();
  53. ctx->Release();
  54. return ptr;
  55. }
  56. #ifdef AS_MAX_PORTABILITY
  57. static void ScriptObject_AddRef_Generic(asIScriptGeneric *gen)
  58. {
  59. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  60. self->AddRef();
  61. }
  62. static void ScriptObject_Release_Generic(asIScriptGeneric *gen)
  63. {
  64. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  65. self->Release();
  66. }
  67. static void ScriptObject_GetRefCount_Generic(asIScriptGeneric *gen)
  68. {
  69. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  70. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  71. }
  72. static void ScriptObject_SetFlag_Generic(asIScriptGeneric *gen)
  73. {
  74. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  75. self->SetFlag();
  76. }
  77. static void ScriptObject_GetFlag_Generic(asIScriptGeneric *gen)
  78. {
  79. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  80. *(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
  81. }
  82. static void ScriptObject_EnumReferences_Generic(asIScriptGeneric *gen)
  83. {
  84. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  85. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  86. self->EnumReferences(engine);
  87. }
  88. static void ScriptObject_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  89. {
  90. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  91. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  92. self->ReleaseAllHandles(engine);
  93. }
  94. #endif
  95. void RegisterScriptObject(asCScriptEngine *engine)
  96. {
  97. // Register the default script class behaviours
  98. int r = 0;
  99. UNUSED_VAR(r); // It is only used in debug mode
  100. engine->scriptTypeBehaviours.engine = engine;
  101. engine->scriptTypeBehaviours.flags = asOBJ_SCRIPT_OBJECT | asOBJ_REF | asOBJ_GC;
  102. engine->scriptTypeBehaviours.name = "_builtin_object_";
  103. #ifndef AS_MAX_PORTABILITY
  104. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  105. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptObject,AddRef), asCALL_THISCALL); asASSERT( r >= 0 );
  106. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptObject,Release), asCALL_THISCALL); asASSERT( r >= 0 );
  107. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  108. // Register GC behaviours
  109. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptObject,GetRefCount), asCALL_THISCALL); asASSERT( r >= 0 );
  110. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptObject,SetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  111. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptObject,GetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  112. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptObject,EnumReferences), asCALL_THISCALL); asASSERT( r >= 0 );
  113. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptObject,ReleaseAllHandles), asCALL_THISCALL); asASSERT( r >= 0 );
  114. #else
  115. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  116. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptObject_AddRef_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  117. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptObject_Release_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  118. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  119. // Register GC behaviours
  120. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptObject_GetRefCount_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  121. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptObject_SetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  122. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptObject_GetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  123. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptObject_EnumReferences_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  124. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptObject_ReleaseAllHandles_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  125. #endif
  126. }
  127. void ScriptObject_Construct_Generic(asIScriptGeneric *gen)
  128. {
  129. asCObjectType *objType = *(asCObjectType**)gen->GetAddressOfArg(0);
  130. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  131. ScriptObject_Construct(objType, self);
  132. }
  133. void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self)
  134. {
  135. new(self) asCScriptObject(objType);
  136. }
  137. asCScriptObject::asCScriptObject(asCObjectType *ot)
  138. {
  139. refCount.set(1);
  140. objType = ot;
  141. objType->AddRef();
  142. isDestructCalled = false;
  143. // Notify the garbage collector of this object
  144. if( objType->flags & asOBJ_GC )
  145. objType->engine->gc.AddScriptObjectToGC(this, objType);
  146. // Construct all properties
  147. asCScriptEngine *engine = objType->engine;
  148. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  149. {
  150. asCObjectProperty *prop = objType->properties[n];
  151. if( prop->type.IsObject() )
  152. {
  153. asPWORD *ptr = (asPWORD*)(((char*)this) + prop->byteOffset);
  154. if( prop->type.IsObjectHandle() )
  155. *ptr = 0;
  156. else
  157. {
  158. // Allocate the object and call it's constructor
  159. *ptr = (asPWORD)AllocateObject(prop->type.GetObjectType(), engine);
  160. }
  161. }
  162. }
  163. }
  164. void asCScriptObject::Destruct()
  165. {
  166. // Call the destructor, which will also call the GCObject's destructor
  167. this->~asCScriptObject();
  168. // Free the memory
  169. userFree(this);
  170. }
  171. asCScriptObject::~asCScriptObject()
  172. {
  173. objType->Release();
  174. // The engine pointer should be available from the objectType
  175. asCScriptEngine *engine = objType->engine;
  176. // Destroy all properties
  177. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  178. {
  179. asCObjectProperty *prop = objType->properties[n];
  180. if( prop->type.IsObject() )
  181. {
  182. // Destroy the object
  183. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  184. if( *ptr )
  185. {
  186. FreeObject(*ptr, prop->type.GetObjectType(), engine);
  187. *(asDWORD*)ptr = 0;
  188. }
  189. }
  190. }
  191. }
  192. asIScriptEngine *asCScriptObject::GetEngine() const
  193. {
  194. return objType->engine;
  195. }
  196. int asCScriptObject::AddRef() const
  197. {
  198. // Increase counter and clear flag set by GC
  199. gcFlag = false;
  200. return refCount.atomicInc();
  201. }
  202. int asCScriptObject::Release() const
  203. {
  204. // Clear the flag set by the GC
  205. gcFlag = false;
  206. // Call the script destructor behaviour if the reference counter is 1.
  207. if( refCount.get() == 1 && !isDestructCalled )
  208. {
  209. // This cast is OK since we are the last reference
  210. const_cast<asCScriptObject*>(this)->CallDestructor();
  211. }
  212. // Now do the actual releasing
  213. int r = refCount.atomicDec();
  214. if( r == 0 )
  215. {
  216. // This cast is OK since we are the last reference
  217. const_cast<asCScriptObject*>(this)->Destruct();
  218. return 0;
  219. }
  220. return r;
  221. }
  222. void asCScriptObject::CallDestructor()
  223. {
  224. // Make sure the destructor is called once only, even if the
  225. // reference count is increased and then decreased again
  226. isDestructCalled = true;
  227. asIScriptContext *ctx = 0;
  228. // Call the destructor for this class and all the super classes
  229. asCObjectType *ot = objType;
  230. while( ot )
  231. {
  232. int funcIndex = ot->beh.destruct;
  233. if( funcIndex )
  234. {
  235. if( ctx == 0 )
  236. {
  237. // Setup a context for calling the default constructor
  238. asCScriptEngine *engine = objType->engine;
  239. int r = engine->CreateContext(&ctx, true);
  240. if( r < 0 ) return;
  241. }
  242. int r = ctx->Prepare(funcIndex);
  243. if( r >= 0 )
  244. {
  245. ctx->SetObject(this);
  246. ctx->Execute();
  247. // There's not much to do if the execution doesn't
  248. // finish, so we just ignore the result
  249. }
  250. }
  251. ot = ot->derivedFrom;
  252. }
  253. if( ctx )
  254. {
  255. ctx->Release();
  256. }
  257. }
  258. asIObjectType *asCScriptObject::GetObjectType() const
  259. {
  260. return objType;
  261. }
  262. int asCScriptObject::GetRefCount()
  263. {
  264. return refCount.get();
  265. }
  266. void asCScriptObject::SetFlag()
  267. {
  268. gcFlag = true;
  269. }
  270. bool asCScriptObject::GetFlag()
  271. {
  272. return gcFlag;
  273. }
  274. // interface
  275. int asCScriptObject::GetTypeId() const
  276. {
  277. asCDataType dt = asCDataType::CreateObject(objType, false);
  278. return objType->engine->GetTypeIdFromDataType(dt);
  279. }
  280. asUINT asCScriptObject::GetPropertyCount() const
  281. {
  282. return asUINT(objType->properties.GetLength());
  283. }
  284. int asCScriptObject::GetPropertyTypeId(asUINT prop) const
  285. {
  286. if( prop >= objType->properties.GetLength() )
  287. return asINVALID_ARG;
  288. return objType->engine->GetTypeIdFromDataType(objType->properties[prop]->type);
  289. }
  290. const char *asCScriptObject::GetPropertyName(asUINT prop) const
  291. {
  292. if( prop >= objType->properties.GetLength() )
  293. return 0;
  294. return objType->properties[prop]->name.AddressOf();
  295. }
  296. void *asCScriptObject::GetAddressOfProperty(asUINT prop)
  297. {
  298. if( prop >= objType->properties.GetLength() )
  299. return 0;
  300. // Objects are stored by reference, so this must be dereferenced
  301. asCDataType *dt = &objType->properties[prop]->type;
  302. if( dt->IsObject() && !dt->IsObjectHandle() )
  303. return *(void**)(((char*)this) + objType->properties[prop]->byteOffset);
  304. return (void*)(((char*)this) + objType->properties[prop]->byteOffset);
  305. }
  306. void asCScriptObject::EnumReferences(asIScriptEngine *engine)
  307. {
  308. // We'll notify the GC of all object handles that we're holding
  309. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  310. {
  311. asCObjectProperty *prop = objType->properties[n];
  312. if( prop->type.IsObject() )
  313. {
  314. void *ptr = *(void**)(((char*)this) + prop->byteOffset);
  315. if( ptr )
  316. ((asCScriptEngine*)engine)->GCEnumCallback(ptr);
  317. }
  318. }
  319. }
  320. void asCScriptObject::ReleaseAllHandles(asIScriptEngine *engine)
  321. {
  322. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  323. {
  324. asCObjectProperty *prop = objType->properties[n];
  325. if( prop->type.IsObject() && prop->type.IsObjectHandle() )
  326. {
  327. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  328. if( *ptr )
  329. {
  330. asASSERT( (prop->type.GetObjectType()->flags & asOBJ_NOCOUNT) || prop->type.GetBehaviour()->release );
  331. if( prop->type.GetBehaviour()->release )
  332. ((asCScriptEngine*)engine)->CallObjectMethod(*ptr, prop->type.GetBehaviour()->release);
  333. *ptr = 0;
  334. }
  335. }
  336. }
  337. }
  338. void ScriptObject_Assignment_Generic(asIScriptGeneric *gen)
  339. {
  340. asCScriptObject *other = *(asCScriptObject**)gen->GetAddressOfArg(0);
  341. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  342. *self = *other;
  343. *(asCScriptObject**)gen->GetAddressOfReturnLocation() = self;
  344. }
  345. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self)
  346. {
  347. return (*self = *other);
  348. }
  349. asCScriptObject &asCScriptObject::operator=(const asCScriptObject &other)
  350. {
  351. if( &other != this )
  352. {
  353. asASSERT( other.objType->DerivesFrom(objType) );
  354. asCScriptEngine *engine = objType->engine;
  355. // Copy all properties
  356. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  357. {
  358. asCObjectProperty *prop = objType->properties[n];
  359. if( prop->type.IsObject() )
  360. {
  361. void **dst = (void**)(((char*)this) + prop->byteOffset);
  362. void **src = (void**)(((char*)&other) + prop->byteOffset);
  363. if( !prop->type.IsObjectHandle() )
  364. CopyObject(*src, *dst, prop->type.GetObjectType(), engine);
  365. else
  366. CopyHandle((asPWORD*)src, (asPWORD*)dst, prop->type.GetObjectType(), engine);
  367. }
  368. else
  369. {
  370. void *dst = ((char*)this) + prop->byteOffset;
  371. void *src = ((char*)&other) + prop->byteOffset;
  372. memcpy(dst, src, prop->type.GetSizeInMemoryBytes());
  373. }
  374. }
  375. }
  376. return *this;
  377. }
  378. int asCScriptObject::CopyFrom(asIScriptObject *other)
  379. {
  380. if( other == 0 ) return asINVALID_ARG;
  381. if( GetTypeId() != other->GetTypeId() )
  382. return asINVALID_TYPE;
  383. *this = *(asCScriptObject*)other;
  384. return 0;
  385. }
  386. void *asCScriptObject::AllocateObject(asCObjectType *objType, asCScriptEngine *engine)
  387. {
  388. void *ptr = 0;
  389. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  390. {
  391. ptr = ScriptObjectFactory(objType, engine);
  392. }
  393. else if( objType->flags & asOBJ_TEMPLATE )
  394. {
  395. // Templates store the original factory that takes the object
  396. // type as a hidden parameter in the construct behaviour
  397. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  398. }
  399. else if( objType->flags & asOBJ_REF )
  400. {
  401. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.factory);
  402. }
  403. else
  404. {
  405. ptr = engine->CallAlloc(objType);
  406. int funcIndex = objType->beh.construct;
  407. if( funcIndex )
  408. engine->CallObjectMethod(ptr, funcIndex);
  409. }
  410. return ptr;
  411. }
  412. void asCScriptObject::FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine)
  413. {
  414. if( objType->flags & asOBJ_REF )
  415. {
  416. asASSERT( (objType->flags & asOBJ_NOCOUNT) || objType->beh.release );
  417. if( objType->beh.release )
  418. engine->CallObjectMethod(ptr, objType->beh.release);
  419. }
  420. else
  421. {
  422. if( objType->beh.destruct )
  423. engine->CallObjectMethod(ptr, objType->beh.destruct);
  424. engine->CallFree(ptr);
  425. }
  426. }
  427. void asCScriptObject::CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine)
  428. {
  429. // TODO: If the object doesn't have the copy behaviour, and it is not a
  430. // POD object then the copy must not be performed
  431. int funcIndex = objType->beh.copy;
  432. if( funcIndex )
  433. engine->CallObjectMethod(dst, src, funcIndex);
  434. else
  435. memcpy(dst, src, objType->size);
  436. }
  437. void asCScriptObject::CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine)
  438. {
  439. if( *dst )
  440. engine->CallObjectMethod(*(void**)dst, objType->beh.release);
  441. *dst = *src;
  442. if( *dst )
  443. engine->CallObjectMethod(*(void**)dst, objType->beh.addref);
  444. }
  445. END_AS_NAMESPACE