as_scriptobject.cpp 23 KB

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