as_scriptobject.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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;
  99. engine->scriptTypeBehaviours.engine = engine;
  100. engine->scriptTypeBehaviours.flags = asOBJ_SCRIPT_OBJECT | asOBJ_REF | asOBJ_GC;
  101. engine->scriptTypeBehaviours.name = "_builtin_object_";
  102. #ifndef AS_MAX_PORTABILITY
  103. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  104. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptObject,AddRef), asCALL_THISCALL); asASSERT( r >= 0 );
  105. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptObject,Release), asCALL_THISCALL); asASSERT( r >= 0 );
  106. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment), asCALL_CDECL_OBJLAST); asASSERT( r >= 0 );
  107. // Register GC behaviours
  108. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptObject,GetRefCount), asCALL_THISCALL); asASSERT( r >= 0 );
  109. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptObject,SetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  110. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptObject,GetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  111. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptObject,EnumReferences), asCALL_THISCALL); asASSERT( r >= 0 );
  112. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptObject,ReleaseAllHandles), asCALL_THISCALL); asASSERT( r >= 0 );
  113. #else
  114. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_CONSTRUCT, "void f(int&in)", asFUNCTION(ScriptObject_Construct_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  115. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptObject_AddRef_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  116. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptObject_Release_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  117. r = engine->RegisterMethodToObjectType(&engine->scriptTypeBehaviours, "int &opAssign(int &in)", asFUNCTION(ScriptObject_Assignment_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  118. // Register GC behaviours
  119. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptObject_GetRefCount_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  120. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptObject_SetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  121. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptObject_GetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  122. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptObject_EnumReferences_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  123. r = engine->RegisterBehaviourToObjectType(&engine->scriptTypeBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptObject_ReleaseAllHandles_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  124. #endif
  125. }
  126. void ScriptObject_Construct_Generic(asIScriptGeneric *gen)
  127. {
  128. asCObjectType *objType = *(asCObjectType**)gen->GetAddressOfArg(0);
  129. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  130. ScriptObject_Construct(objType, self);
  131. }
  132. void ScriptObject_Construct(asCObjectType *objType, asCScriptObject *self)
  133. {
  134. new(self) asCScriptObject(objType);
  135. }
  136. asCScriptObject::asCScriptObject(asCObjectType *ot)
  137. {
  138. refCount.set(1);
  139. objType = ot;
  140. objType->AddRef();
  141. isDestructCalled = false;
  142. // Notify the garbage collector of this object
  143. if( objType->flags & asOBJ_GC )
  144. objType->engine->gc.AddScriptObjectToGC(this, objType);
  145. // Construct all properties
  146. asCScriptEngine *engine = objType->engine;
  147. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  148. {
  149. asCObjectProperty *prop = objType->properties[n];
  150. if( prop->type.IsObject() )
  151. {
  152. size_t *ptr = (size_t*)(((char*)this) + prop->byteOffset);
  153. if( prop->type.IsObjectHandle() )
  154. *ptr = 0;
  155. else
  156. {
  157. // Allocate the object and call it's constructor
  158. *ptr = (size_t)AllocateObject(prop->type.GetObjectType(), engine);
  159. }
  160. }
  161. }
  162. }
  163. void asCScriptObject::Destruct()
  164. {
  165. // Call the destructor, which will also call the GCObject's destructor
  166. this->~asCScriptObject();
  167. // Free the memory
  168. userFree(this);
  169. }
  170. asCScriptObject::~asCScriptObject()
  171. {
  172. objType->Release();
  173. // The engine pointer should be available from the objectType
  174. asCScriptEngine *engine = objType->engine;
  175. // Destroy all properties
  176. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  177. {
  178. asCObjectProperty *prop = objType->properties[n];
  179. if( prop->type.IsObject() )
  180. {
  181. // Destroy the object
  182. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  183. if( *ptr )
  184. {
  185. FreeObject(*ptr, prop->type.GetObjectType(), engine);
  186. *(asDWORD*)ptr = 0;
  187. }
  188. }
  189. }
  190. }
  191. asIScriptEngine *asCScriptObject::GetEngine() const
  192. {
  193. return objType->engine;
  194. }
  195. int asCScriptObject::AddRef() const
  196. {
  197. // Increase counter and clear flag set by GC
  198. gcFlag = false;
  199. return refCount.atomicInc();
  200. }
  201. int asCScriptObject::Release() const
  202. {
  203. // Clear the flag set by the GC
  204. gcFlag = false;
  205. // Call the script destructor behaviour if the reference counter is 1.
  206. if( refCount.get() == 1 && !isDestructCalled )
  207. {
  208. // This cast is OK since we are the last reference
  209. const_cast<asCScriptObject*>(this)->CallDestructor();
  210. }
  211. // Now do the actual releasing
  212. int r = refCount.atomicDec();
  213. if( r == 0 )
  214. {
  215. // This cast is OK since we are the last reference
  216. const_cast<asCScriptObject*>(this)->Destruct();
  217. return 0;
  218. }
  219. return r;
  220. }
  221. void asCScriptObject::CallDestructor()
  222. {
  223. // Make sure the destructor is called once only, even if the
  224. // reference count is increased and then decreased again
  225. isDestructCalled = true;
  226. asIScriptContext *ctx = 0;
  227. // Call the destructor for this class and all the super classes
  228. asCObjectType *ot = objType;
  229. while( ot )
  230. {
  231. int funcIndex = ot->beh.destruct;
  232. if( funcIndex )
  233. {
  234. if( ctx == 0 )
  235. {
  236. // Setup a context for calling the default constructor
  237. asCScriptEngine *engine = objType->engine;
  238. int r = engine->CreateContext(&ctx, true);
  239. if( r < 0 ) return;
  240. }
  241. int r = ctx->Prepare(funcIndex);
  242. if( r >= 0 )
  243. {
  244. ctx->SetObject(this);
  245. ctx->Execute();
  246. // There's not much to do if the execution doesn't
  247. // finish, so we just ignore the result
  248. }
  249. }
  250. ot = ot->derivedFrom;
  251. }
  252. if( ctx )
  253. {
  254. ctx->Release();
  255. }
  256. }
  257. asIObjectType *asCScriptObject::GetObjectType() const
  258. {
  259. return objType;
  260. }
  261. int asCScriptObject::GetRefCount()
  262. {
  263. return refCount.get();
  264. }
  265. void asCScriptObject::SetFlag()
  266. {
  267. gcFlag = true;
  268. }
  269. bool asCScriptObject::GetFlag()
  270. {
  271. return gcFlag;
  272. }
  273. // interface
  274. int asCScriptObject::GetTypeId() const
  275. {
  276. asCDataType dt = asCDataType::CreateObject(objType, false);
  277. return objType->engine->GetTypeIdFromDataType(dt);
  278. }
  279. asUINT asCScriptObject::GetPropertyCount() const
  280. {
  281. return objType->properties.GetLength();
  282. }
  283. int asCScriptObject::GetPropertyTypeId(asUINT prop) const
  284. {
  285. if( prop >= objType->properties.GetLength() )
  286. return asINVALID_ARG;
  287. return objType->engine->GetTypeIdFromDataType(objType->properties[prop]->type);
  288. }
  289. const char *asCScriptObject::GetPropertyName(asUINT prop) const
  290. {
  291. if( prop >= objType->properties.GetLength() )
  292. return 0;
  293. return objType->properties[prop]->name.AddressOf();
  294. }
  295. void *asCScriptObject::GetAddressOfProperty(asUINT prop)
  296. {
  297. if( prop >= objType->properties.GetLength() )
  298. return 0;
  299. // Objects are stored by reference, so this must be dereferenced
  300. asCDataType *dt = &objType->properties[prop]->type;
  301. if( dt->IsObject() && !dt->IsObjectHandle() )
  302. return *(void**)(((char*)this) + objType->properties[prop]->byteOffset);
  303. return (void*)(((char*)this) + objType->properties[prop]->byteOffset);
  304. }
  305. void asCScriptObject::EnumReferences(asIScriptEngine *engine)
  306. {
  307. // We'll notify the GC of all object handles that we're holding
  308. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  309. {
  310. asCObjectProperty *prop = objType->properties[n];
  311. if( prop->type.IsObject() )
  312. {
  313. void *ptr = *(void**)(((char*)this) + prop->byteOffset);
  314. if( ptr )
  315. ((asCScriptEngine*)engine)->GCEnumCallback(ptr);
  316. }
  317. }
  318. }
  319. void asCScriptObject::ReleaseAllHandles(asIScriptEngine *engine)
  320. {
  321. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  322. {
  323. asCObjectProperty *prop = objType->properties[n];
  324. if( prop->type.IsObject() && prop->type.IsObjectHandle() )
  325. {
  326. void **ptr = (void**)(((char*)this) + prop->byteOffset);
  327. if( *ptr )
  328. {
  329. ((asCScriptEngine*)engine)->CallObjectMethod(*ptr, prop->type.GetBehaviour()->release);
  330. *ptr = 0;
  331. }
  332. }
  333. }
  334. }
  335. void ScriptObject_Assignment_Generic(asIScriptGeneric *gen)
  336. {
  337. asCScriptObject *other = *(asCScriptObject**)gen->GetAddressOfArg(0);
  338. asCScriptObject *self = (asCScriptObject*)gen->GetObject();
  339. *self = *other;
  340. *(asCScriptObject**)gen->GetAddressOfReturnLocation() = self;
  341. }
  342. asCScriptObject &ScriptObject_Assignment(asCScriptObject *other, asCScriptObject *self)
  343. {
  344. return (*self = *other);
  345. }
  346. asCScriptObject &asCScriptObject::operator=(const asCScriptObject &other)
  347. {
  348. if( &other != this )
  349. {
  350. asASSERT( other.objType->DerivesFrom(objType) );
  351. asCScriptEngine *engine = objType->engine;
  352. // Copy all properties
  353. for( asUINT n = 0; n < objType->properties.GetLength(); n++ )
  354. {
  355. asCObjectProperty *prop = objType->properties[n];
  356. if( prop->type.IsObject() )
  357. {
  358. void **dst = (void**)(((char*)this) + prop->byteOffset);
  359. void **src = (void**)(((char*)&other) + prop->byteOffset);
  360. if( !prop->type.IsObjectHandle() )
  361. CopyObject(*src, *dst, prop->type.GetObjectType(), engine);
  362. else
  363. CopyHandle((asPWORD*)src, (asPWORD*)dst, prop->type.GetObjectType(), engine);
  364. }
  365. else
  366. {
  367. void *dst = ((char*)this) + prop->byteOffset;
  368. void *src = ((char*)&other) + prop->byteOffset;
  369. memcpy(dst, src, prop->type.GetSizeInMemoryBytes());
  370. }
  371. }
  372. }
  373. return *this;
  374. }
  375. int asCScriptObject::CopyFrom(asIScriptObject *other)
  376. {
  377. if( other == 0 ) return asINVALID_ARG;
  378. if( GetTypeId() != other->GetTypeId() )
  379. return asINVALID_TYPE;
  380. *this = *(asCScriptObject*)other;
  381. return 0;
  382. }
  383. void *asCScriptObject::AllocateObject(asCObjectType *objType, asCScriptEngine *engine)
  384. {
  385. void *ptr = 0;
  386. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  387. {
  388. ptr = ScriptObjectFactory(objType, engine);
  389. }
  390. else if( objType->flags & asOBJ_TEMPLATE )
  391. {
  392. // Templates store the original factory that takes the object
  393. // type as a hidden parameter in the construct behaviour
  394. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.construct, objType);
  395. }
  396. else if( objType->flags & asOBJ_REF )
  397. {
  398. ptr = engine->CallGlobalFunctionRetPtr(objType->beh.factory);
  399. }
  400. else
  401. {
  402. ptr = engine->CallAlloc(objType);
  403. int funcIndex = objType->beh.construct;
  404. if( funcIndex )
  405. engine->CallObjectMethod(ptr, funcIndex);
  406. }
  407. return ptr;
  408. }
  409. void asCScriptObject::FreeObject(void *ptr, asCObjectType *objType, asCScriptEngine *engine)
  410. {
  411. if( !objType->beh.release )
  412. {
  413. if( objType->beh.destruct )
  414. engine->CallObjectMethod(ptr, objType->beh.destruct);
  415. engine->CallFree(ptr);
  416. }
  417. else
  418. {
  419. engine->CallObjectMethod(ptr, objType->beh.release);
  420. }
  421. }
  422. void asCScriptObject::CopyObject(void *src, void *dst, asCObjectType *objType, asCScriptEngine *engine)
  423. {
  424. // TODO: If the object doesn't have the copy behaviour, and it is not a
  425. // POD object then the copy must not be performed
  426. int funcIndex = objType->beh.copy;
  427. if( funcIndex )
  428. engine->CallObjectMethod(dst, src, funcIndex);
  429. else
  430. memcpy(dst, src, objType->size);
  431. }
  432. void asCScriptObject::CopyHandle(asPWORD *src, asPWORD *dst, asCObjectType *objType, asCScriptEngine *engine)
  433. {
  434. if( *dst )
  435. engine->CallObjectMethod(*(void**)dst, objType->beh.release);
  436. *dst = *src;
  437. if( *dst )
  438. engine->CallObjectMethod(*(void**)dst, objType->beh.addref);
  439. }
  440. END_AS_NAMESPACE