as_scriptobject.cpp 16 KB

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