Addons.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Addons.h"
  25. #include "Str.h"
  26. #include <cstring>
  27. #include <new>
  28. #include <stdio.h>
  29. // Adapted from Angelscript's scriptarray & scriptstdstring add-ons, but with garbage collection disabled
  30. using namespace std;
  31. /// %Script array buffer.
  32. struct SArrayBuffer
  33. {
  34. asDWORD maxElements;
  35. asDWORD numElements;
  36. asBYTE data[1];
  37. };
  38. /// %Script array cache.
  39. struct SArrayCache
  40. {
  41. asIScriptFunction *cmpFunc;
  42. asIScriptFunction *eqFunc;
  43. };
  44. // We just define a number here that we assume nobody else is using for
  45. // object type user data. The add-ons have reserved the numbers 1000
  46. // through 1999 for this purpose, so we should be fine.
  47. const asPWORD ARRAY_CACHE = 1000;
  48. static void CleanupObjectTypeArrayCache(asIObjectType *type)
  49. {
  50. SArrayCache *cache = reinterpret_cast<SArrayCache*>(type->GetUserData(ARRAY_CACHE));
  51. if( cache )
  52. delete cache;
  53. }
  54. static CScriptArray* ScriptArrayFactory2(asIObjectType *ot, asUINT length)
  55. {
  56. CScriptArray *a = new CScriptArray(length, ot);
  57. // It's possible the constructor raised a script exception, in which case we
  58. // need to free the memory and return null instead, else we get a memory leak.
  59. asIScriptContext *ctx = asGetActiveContext();
  60. if( ctx && ctx->GetState() == asEXECUTION_EXCEPTION )
  61. {
  62. delete a;
  63. return 0;
  64. }
  65. return a;
  66. }
  67. static CScriptArray* ScriptArrayFactoryDefVal(asIObjectType *ot, asUINT length, void *defVal)
  68. {
  69. CScriptArray *a = new CScriptArray(length, defVal, ot);
  70. // It's possible the constructor raised a script exception, in which case we
  71. // need to free the memory and return null instead, else we get a memory leak.
  72. asIScriptContext *ctx = asGetActiveContext();
  73. if( ctx && ctx->GetState() == asEXECUTION_EXCEPTION )
  74. {
  75. delete a;
  76. return 0;
  77. }
  78. return a;
  79. }
  80. static CScriptArray* ScriptArrayFactory(asIObjectType *ot)
  81. {
  82. return ScriptArrayFactory2(ot, 0);
  83. }
  84. // This optional callback is called when the template type is first used by the compiler.
  85. // It allows the application to validate if the template can be instanciated for the requested
  86. // subtype at compile time, instead of at runtime.
  87. static bool ScriptArrayTemplateCallback(asIObjectType *ot)
  88. {
  89. // Make sure the subtype can be instanciated with a default factory/constructor,
  90. // otherwise we won't be able to instanciate the elements.
  91. int typeId = ot->GetSubTypeId();
  92. if( typeId == asTYPEID_VOID )
  93. return false;
  94. if( (typeId & asTYPEID_MASK_OBJECT) && !(typeId & asTYPEID_OBJHANDLE) )
  95. {
  96. asIObjectType *subtype = ot->GetEngine()->GetObjectTypeById(typeId);
  97. asDWORD flags = subtype->GetFlags();
  98. if( (flags & asOBJ_VALUE) && !(flags & asOBJ_POD) )
  99. {
  100. // Verify that there is a default constructor
  101. for( asUINT n = 0; n < subtype->GetBehaviourCount(); n++ )
  102. {
  103. asEBehaviours beh;
  104. asIScriptFunction *func = subtype->GetBehaviourByIndex(n, &beh);
  105. if( beh != asBEHAVE_CONSTRUCT ) continue;
  106. if( func->GetParamCount() == 0 )
  107. {
  108. // Found the default constructor
  109. return true;
  110. }
  111. }
  112. // There is no default constructor
  113. return false;
  114. }
  115. else if( (flags & asOBJ_REF) )
  116. {
  117. // Verify that there is a default factory
  118. for( asUINT n = 0; n < subtype->GetFactoryCount(); n++ )
  119. {
  120. asIScriptFunction *func = subtype->GetFactoryByIndex(n);
  121. if( func->GetParamCount() == 0 )
  122. {
  123. // Found the default factory
  124. return true;
  125. }
  126. }
  127. // No default factory
  128. return false;
  129. }
  130. }
  131. // The type is ok
  132. return true;
  133. }
  134. CScriptArray &CScriptArray::operator=(const CScriptArray &other)
  135. {
  136. // Only perform the copy if the array types are the same
  137. if( &other != this &&
  138. other.GetArrayObjectType() == GetArrayObjectType() )
  139. {
  140. // Make sure the arrays are of the same size
  141. Resize(other.buffer->numElements);
  142. // Copy the value of each element
  143. CopyBuffer(buffer, other.buffer);
  144. }
  145. return *this;
  146. }
  147. CScriptArray::CScriptArray(asUINT length, asIObjectType *ot)
  148. {
  149. refCount = 1;
  150. gcFlag = false;
  151. objType = ot;
  152. objType->AddRef();
  153. buffer = 0;
  154. Precache();
  155. // Determine element size
  156. if( subTypeId & asTYPEID_MASK_OBJECT )
  157. {
  158. elementSize = sizeof(asPWORD);
  159. }
  160. else
  161. {
  162. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
  163. }
  164. // Make sure the array size isn't too large for us to handle
  165. if( !CheckMaxSize(length) )
  166. {
  167. // Don't continue with the initialization
  168. return;
  169. }
  170. CreateBuffer(&buffer, length);
  171. // Urho3D: garbage collection disabled
  172. /*
  173. // Notify the GC of the successful creation
  174. if( objType->GetFlags() & asOBJ_GC )
  175. objType->GetEngine()->NotifyGarbageCollectorOfNewObject(this, objType);
  176. */
  177. }
  178. CScriptArray::CScriptArray(asUINT length, void *defVal, asIObjectType *ot)
  179. {
  180. refCount = 1;
  181. gcFlag = false;
  182. objType = ot;
  183. objType->AddRef();
  184. buffer = 0;
  185. Precache();
  186. // Determine element size
  187. if( subTypeId & asTYPEID_MASK_OBJECT )
  188. {
  189. elementSize = sizeof(asPWORD);
  190. }
  191. else
  192. {
  193. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
  194. }
  195. // Make sure the array size isn't too large for us to handle
  196. if( !CheckMaxSize(length) )
  197. {
  198. // Don't continue with the initialization
  199. return;
  200. }
  201. CreateBuffer(&buffer, length);
  202. // Urho3D: garbage collection disabled
  203. /*
  204. // Notify the GC of the successful creation
  205. if( objType->GetFlags() & asOBJ_GC )
  206. objType->GetEngine()->NotifyGarbageCollectorOfNewObject(this, objType);
  207. */
  208. // Initialize the elements with the default value
  209. for( asUINT n = 0; n < GetSize(); n++ )
  210. SetValue(n, defVal);
  211. }
  212. void CScriptArray::SetValue(asUINT index, void *value)
  213. {
  214. // At() will take care of the out-of-bounds checking, though
  215. // if called from the application then nothing will be done
  216. void *ptr = At(index);
  217. if( ptr == 0 ) return;
  218. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  219. objType->GetEngine()->AssignScriptObject(ptr, value, subTypeId);
  220. else if( subTypeId & asTYPEID_OBJHANDLE )
  221. {
  222. void *tmp = *(void**)ptr;
  223. *(void**)ptr = *(void**)value;
  224. objType->GetEngine()->AddRefScriptObject(*(void**)value, objType->GetSubType());
  225. if( tmp )
  226. objType->GetEngine()->ReleaseScriptObject(*(void**)value, objType->GetSubType());
  227. }
  228. else if( subTypeId == asTYPEID_BOOL ||
  229. subTypeId == asTYPEID_INT8 ||
  230. subTypeId == asTYPEID_UINT8 )
  231. *(char*)ptr = *(char*)value;
  232. else if( subTypeId == asTYPEID_INT16 ||
  233. subTypeId == asTYPEID_UINT16 )
  234. *(short*)ptr = *(short*)value;
  235. else if( subTypeId == asTYPEID_INT32 ||
  236. subTypeId == asTYPEID_UINT32 ||
  237. subTypeId == asTYPEID_FLOAT ||
  238. subTypeId > asTYPEID_DOUBLE ) // enums have a type id larger than doubles
  239. *(int*)ptr = *(int*)value;
  240. else if( subTypeId == asTYPEID_INT64 ||
  241. subTypeId == asTYPEID_UINT64 ||
  242. subTypeId == asTYPEID_DOUBLE )
  243. *(double*)ptr = *(double*)value;
  244. }
  245. CScriptArray::~CScriptArray()
  246. {
  247. if( buffer )
  248. {
  249. DeleteBuffer(buffer);
  250. buffer = 0;
  251. }
  252. if( objType ) objType->Release();
  253. }
  254. asUINT CScriptArray::GetSize() const
  255. {
  256. return buffer->numElements;
  257. }
  258. bool CScriptArray::IsEmpty() const
  259. {
  260. return buffer->numElements == 0;
  261. }
  262. void CScriptArray::Reserve(asUINT maxElements)
  263. {
  264. if( maxElements <= buffer->maxElements )
  265. return;
  266. // Allocate memory for the buffer
  267. SArrayBuffer *newBuffer;
  268. #if defined(AS_MARMALADE)
  269. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*maxElements];
  270. #else
  271. newBuffer = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1 + elementSize*maxElements];
  272. #endif
  273. if( newBuffer )
  274. {
  275. newBuffer->numElements = buffer->numElements;
  276. newBuffer->maxElements = maxElements;
  277. }
  278. else
  279. {
  280. // Out of memory
  281. asIScriptContext *ctx = asGetActiveContext();
  282. if( ctx )
  283. ctx->SetException("Out of memory");
  284. return;
  285. }
  286. memcpy(newBuffer->data, buffer->data, buffer->numElements*elementSize);
  287. // Release the old buffer
  288. delete[] (asBYTE*)buffer;
  289. buffer = newBuffer;
  290. }
  291. void CScriptArray::Resize(asUINT numElements)
  292. {
  293. if( numElements & 0x80000000 )
  294. {
  295. CheckMaxSize(numElements);
  296. return;
  297. }
  298. Resize((int)numElements - (int)buffer->numElements, (asUINT)-1);
  299. }
  300. // Internal
  301. void CScriptArray::Resize(int delta, asUINT at)
  302. {
  303. if( delta < 0 )
  304. {
  305. if( -delta > (int)buffer->numElements )
  306. delta = -(int)buffer->numElements;
  307. if( at > buffer->numElements + delta )
  308. at = buffer->numElements + delta;
  309. }
  310. else if( delta > 0 )
  311. {
  312. // Make sure the array size isn't too large for us to handle
  313. if( delta > 0 && !CheckMaxSize(buffer->numElements + delta) )
  314. return;
  315. if( at > buffer->numElements )
  316. at = buffer->numElements;
  317. }
  318. if( delta == 0 ) return;
  319. if( buffer->maxElements < buffer->numElements + delta )
  320. {
  321. // Allocate memory for the buffer
  322. SArrayBuffer *newBuffer;
  323. #if defined(AS_MARMALADE)
  324. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  325. #else
  326. newBuffer = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  327. #endif
  328. if( newBuffer )
  329. {
  330. newBuffer->numElements = buffer->numElements + delta;
  331. newBuffer->maxElements = newBuffer->numElements;
  332. }
  333. else
  334. {
  335. // Out of memory
  336. asIScriptContext *ctx = asGetActiveContext();
  337. if( ctx )
  338. ctx->SetException("Out of memory");
  339. return;
  340. }
  341. memcpy(newBuffer->data, buffer->data, at*elementSize);
  342. if( at < buffer->numElements )
  343. memcpy(newBuffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements-at)*elementSize);
  344. if( subTypeId & asTYPEID_MASK_OBJECT )
  345. Construct(newBuffer, at, at+delta);
  346. // Release the old buffer
  347. delete[] (asBYTE*)buffer;
  348. buffer = newBuffer;
  349. }
  350. else if( delta < 0 )
  351. {
  352. Destruct(buffer, at, at-delta);
  353. memmove(buffer->data + at*elementSize, buffer->data + (at-delta)*elementSize, (buffer->numElements - (at-delta))*elementSize);
  354. buffer->numElements += delta;
  355. }
  356. else
  357. {
  358. memmove(buffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements - at)*elementSize);
  359. Construct(buffer, at, at+delta);
  360. buffer->numElements += delta;
  361. }
  362. }
  363. // internal
  364. bool CScriptArray::CheckMaxSize(asUINT numElements)
  365. {
  366. // This code makes sure the size of the buffer that is allocated
  367. // for the array doesn't overflow and becomes smaller than requested
  368. asUINT maxSize = 0xFFFFFFFFul - sizeof(SArrayBuffer) + 1;
  369. if( subTypeId & asTYPEID_MASK_OBJECT )
  370. maxSize /= sizeof(void*);
  371. else if( elementSize > 0 )
  372. maxSize /= elementSize;
  373. if( numElements > maxSize )
  374. {
  375. asIScriptContext *ctx = asGetActiveContext();
  376. if( ctx )
  377. {
  378. // Set a script exception
  379. ctx->SetException("Too large array size");
  380. }
  381. return false;
  382. }
  383. // OK
  384. return true;
  385. }
  386. asIObjectType *CScriptArray::GetArrayObjectType() const
  387. {
  388. return objType;
  389. }
  390. int CScriptArray::GetArrayTypeId() const
  391. {
  392. return objType->GetTypeId();
  393. }
  394. int CScriptArray::GetElementTypeId() const
  395. {
  396. return subTypeId;
  397. }
  398. void CScriptArray::InsertAt(asUINT index, void *value)
  399. {
  400. if( index > buffer->numElements )
  401. {
  402. // If this is called from a script we raise a script exception
  403. asIScriptContext *ctx = asGetActiveContext();
  404. if( ctx )
  405. ctx->SetException("Index out of bounds");
  406. return;
  407. }
  408. // Make room for the new element
  409. Resize(1, index);
  410. // Set the value of the new element
  411. SetValue(index, value);
  412. }
  413. void CScriptArray::InsertLast(void *value)
  414. {
  415. InsertAt(buffer->numElements, value);
  416. }
  417. void CScriptArray::RemoveAt(asUINT index)
  418. {
  419. if( index >= buffer->numElements )
  420. {
  421. // If this is called from a script we raise a script exception
  422. asIScriptContext *ctx = asGetActiveContext();
  423. if( ctx )
  424. ctx->SetException("Index out of bounds");
  425. return;
  426. }
  427. // Remove the element
  428. Resize(-1, index);
  429. }
  430. void CScriptArray::RemoveLast()
  431. {
  432. RemoveAt(buffer->numElements-1);
  433. }
  434. // Return a pointer to the array element. Returns 0 if the index is out of bounds
  435. const void *CScriptArray::At(asUINT index) const
  436. {
  437. if( buffer == 0 || index >= buffer->numElements )
  438. {
  439. // If this is called from a script we raise a script exception
  440. asIScriptContext *ctx = asGetActiveContext();
  441. if( ctx )
  442. ctx->SetException("Index out of bounds");
  443. return 0;
  444. }
  445. if( (subTypeId & asTYPEID_MASK_OBJECT) && !(subTypeId & asTYPEID_OBJHANDLE) )
  446. return (void*)((size_t*)buffer->data)[index];
  447. else
  448. return buffer->data + elementSize*index;
  449. }
  450. void *CScriptArray::At(asUINT index)
  451. {
  452. return const_cast<void*>(const_cast<const CScriptArray *>(this)->At(index));
  453. }
  454. // internal
  455. void CScriptArray::CreateBuffer(SArrayBuffer **buf, asUINT numElements)
  456. {
  457. if( subTypeId & asTYPEID_MASK_OBJECT )
  458. {
  459. #if defined( AS_MARMALADE )
  460. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  461. #else
  462. *buf = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  463. #endif
  464. }
  465. else
  466. {
  467. #if defined( AS_MARMALADE )
  468. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  469. #else
  470. *buf = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  471. #endif
  472. }
  473. if( *buf )
  474. {
  475. (*buf)->numElements = numElements;
  476. (*buf)->maxElements = numElements;
  477. Construct(*buf, 0, numElements);
  478. }
  479. else
  480. {
  481. // Oops, out of memory
  482. asIScriptContext *ctx = asGetActiveContext();
  483. if( ctx )
  484. ctx->SetException("Out of memory");
  485. }
  486. }
  487. // internal
  488. void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
  489. {
  490. Destruct(buf, 0, buf->numElements);
  491. // Free the buffer
  492. delete[] (asBYTE*)buf;
  493. }
  494. // internal
  495. void CScriptArray::Construct(SArrayBuffer *buf, asUINT start, asUINT end)
  496. {
  497. if( subTypeId & asTYPEID_OBJHANDLE )
  498. {
  499. // Set all object handles to null
  500. void *d = (void*)(buf->data + start * sizeof(void*));
  501. memset(d, 0, (end-start)*sizeof(void*));
  502. }
  503. else if( subTypeId & asTYPEID_MASK_OBJECT )
  504. {
  505. void **max = (void**)(buf->data + end * sizeof(void*));
  506. void **d = (void**)(buf->data + start * sizeof(void*));
  507. asIScriptEngine *engine = objType->GetEngine();
  508. for( ; d < max; d++ )
  509. *d = (void*)engine->CreateScriptObject(subTypeId);
  510. }
  511. }
  512. // internal
  513. void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
  514. {
  515. if( subTypeId & asTYPEID_MASK_OBJECT )
  516. {
  517. asIScriptEngine *engine = objType->GetEngine();
  518. void **max = (void**)(buf->data + end * sizeof(void*));
  519. void **d = (void**)(buf->data + start * sizeof(void*));
  520. for( ; d < max; d++ )
  521. {
  522. if( *d )
  523. engine->ReleaseScriptObject(*d, objType->GetSubType());
  524. }
  525. }
  526. }
  527. // internal
  528. bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext *ctx)
  529. {
  530. if( !asc )
  531. {
  532. // Swap items
  533. const void *TEMP = a;
  534. a = b;
  535. b = TEMP;
  536. }
  537. if( !(subTypeId & ~asTYPEID_MASK_SEQNBR) )
  538. {
  539. // Simple compare of values
  540. switch( subTypeId )
  541. {
  542. #define COMPARE(T) *((T*)a) < *((T*)b)
  543. case asTYPEID_BOOL: return COMPARE(bool);
  544. case asTYPEID_INT8: return COMPARE(signed char);
  545. case asTYPEID_UINT8: return COMPARE(unsigned char);
  546. case asTYPEID_INT16: return COMPARE(signed short);
  547. case asTYPEID_UINT16: return COMPARE(unsigned short);
  548. case asTYPEID_INT32: return COMPARE(signed int);
  549. case asTYPEID_UINT32: return COMPARE(unsigned int);
  550. case asTYPEID_FLOAT: return COMPARE(float);
  551. case asTYPEID_DOUBLE: return COMPARE(double);
  552. default: return COMPARE(signed int); // All enums fall in this case
  553. #undef COMPARE
  554. }
  555. }
  556. else if( subTypeId & asTYPEID_OBJHANDLE )
  557. {
  558. return *((void**)a) < *((void**)b);
  559. }
  560. else
  561. {
  562. int r = 0;
  563. // Execute object opCmp
  564. SArrayCache *cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  565. if( cache && cache->cmpFunc )
  566. {
  567. // TODO: Add proper error handling
  568. r = ctx->Prepare(cache->cmpFunc); assert(r >= 0);
  569. r = ctx->SetObject((void*)a); assert(r >= 0);
  570. r = ctx->SetArgObject(0, (void*)b); assert(r >= 0);
  571. r = ctx->Execute();
  572. if( r == asEXECUTION_FINISHED )
  573. {
  574. return (int)ctx->GetReturnDWord() < 0;
  575. }
  576. }
  577. }
  578. return false;
  579. }
  580. void CScriptArray::Reverse()
  581. {
  582. asUINT size = GetSize();
  583. if( size >= 2 )
  584. {
  585. asBYTE TEMP[16];
  586. for( asUINT i = 0; i < size / 2; i++ )
  587. {
  588. Copy(TEMP, GetArrayItemPointer(i));
  589. Copy(GetArrayItemPointer(i), GetArrayItemPointer(size - i - 1));
  590. Copy(GetArrayItemPointer(size - i - 1), TEMP);
  591. }
  592. }
  593. }
  594. bool CScriptArray::operator==(const CScriptArray &other) const
  595. {
  596. if( objType != other.objType )
  597. return false;
  598. if( GetSize() != other.GetSize() )
  599. return false;
  600. asIScriptContext *cmpContext = 0;
  601. bool isNested = false;
  602. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  603. {
  604. // Try to reuse the active context
  605. cmpContext = asGetActiveContext();
  606. if( cmpContext )
  607. {
  608. if( cmpContext->PushState() >= 0 )
  609. isNested = true;
  610. else
  611. cmpContext = 0;
  612. }
  613. if( cmpContext == 0 )
  614. {
  615. // TODO: Ideally this context would be retrieved from a pool, so we don't have to
  616. // create a new one everytime. We could keep a context with the array object
  617. // but that would consume a lot of resources as each context is quite heavy.
  618. cmpContext = objType->GetEngine()->CreateContext();
  619. }
  620. }
  621. // Check if all elements are equal
  622. bool isEqual = true;
  623. SArrayCache *cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  624. for( asUINT n = 0; n < GetSize(); n++ )
  625. if( !Equals(At(n), other.At(n), cmpContext, cache) )
  626. {
  627. isEqual = false;
  628. break;
  629. }
  630. if( cmpContext )
  631. if( isNested )
  632. cmpContext->PopState();
  633. else
  634. cmpContext->Release();
  635. return isEqual;
  636. }
  637. // internal
  638. bool CScriptArray::Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const
  639. {
  640. if( !(subTypeId & ~asTYPEID_MASK_SEQNBR) )
  641. {
  642. // Simple compare of values
  643. switch( subTypeId )
  644. {
  645. #define COMPARE(T) *((T*)a) == *((T*)b)
  646. case asTYPEID_BOOL: return COMPARE(bool);
  647. case asTYPEID_INT8: return COMPARE(signed char);
  648. case asTYPEID_UINT8: return COMPARE(unsigned char);
  649. case asTYPEID_INT16: return COMPARE(signed short);
  650. case asTYPEID_UINT16: return COMPARE(unsigned short);
  651. case asTYPEID_INT32: return COMPARE(signed int);
  652. case asTYPEID_UINT32: return COMPARE(unsigned int);
  653. case asTYPEID_FLOAT: return COMPARE(float);
  654. case asTYPEID_DOUBLE: return COMPARE(double);
  655. default: return COMPARE(signed int); // All enums fall here
  656. #undef COMPARE
  657. }
  658. }
  659. else if( subTypeId & asTYPEID_OBJHANDLE )
  660. {
  661. return *((void**)a) == *((void**)b);
  662. }
  663. else
  664. {
  665. int r = 0;
  666. // Execute object opEquals if available
  667. if( cache && cache->eqFunc )
  668. {
  669. // TODO: Add proper error handling
  670. r = ctx->Prepare(cache->eqFunc); assert(r >= 0);
  671. r = ctx->SetObject((void*)a); assert(r >= 0);
  672. r = ctx->SetArgObject(0, (void*)b); assert(r >= 0);
  673. r = ctx->Execute();
  674. if( r == asEXECUTION_FINISHED )
  675. {
  676. return ctx->GetReturnByte() != 0;
  677. }
  678. }
  679. // Execute object opCmp if available
  680. if( cache && cache->cmpFunc >= 0 )
  681. {
  682. // TODO: Add proper error handling
  683. r = ctx->Prepare(cache->cmpFunc); assert(r >= 0);
  684. r = ctx->SetObject((void*)a); assert(r >= 0);
  685. r = ctx->SetArgObject(0, (void*)b); assert(r >= 0);
  686. r = ctx->Execute();
  687. if( r == asEXECUTION_FINISHED )
  688. {
  689. return (int)ctx->GetReturnDWord() == 0;
  690. }
  691. }
  692. }
  693. return false;
  694. }
  695. int CScriptArray::Find(void *value) const
  696. {
  697. return Find(0, value);
  698. }
  699. int CScriptArray::Find(asUINT index, void *value) const
  700. {
  701. // Check if the subtype really supports find()
  702. // TODO: Can't this be done at compile time too by the template callback
  703. SArrayCache *cache = 0;
  704. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  705. {
  706. cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  707. if( !cache || (cache->cmpFunc == 0 && cache->eqFunc == 0) )
  708. {
  709. asIScriptContext *ctx = asGetActiveContext();
  710. asIObjectType* subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  711. // Throw an exception
  712. if( ctx )
  713. {
  714. char tmp[512];
  715. #if defined(_MSC_VER) && _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  716. sprintf_s(tmp, 512, "Type '%s' does not have opEquals / opCmp", subType->GetName());
  717. #else
  718. sprintf(tmp, "Type '%s' does not have opEquals / opCmp", subType->GetName());
  719. #endif
  720. ctx->SetException(tmp);
  721. }
  722. return -1;
  723. }
  724. }
  725. asIScriptContext *cmpContext = 0;
  726. bool isNested = false;
  727. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  728. {
  729. // Try to reuse the active context
  730. cmpContext = asGetActiveContext();
  731. if( cmpContext )
  732. {
  733. if( cmpContext->PushState() >= 0 )
  734. isNested = true;
  735. else
  736. cmpContext = 0;
  737. }
  738. if( cmpContext == 0 )
  739. {
  740. // TODO: Ideally this context would be retrieved from a pool, so we don't have to
  741. // create a new one everytime. We could keep a context with the array object
  742. // but that would consume a lot of resources as each context is quite heavy.
  743. cmpContext = objType->GetEngine()->CreateContext();
  744. }
  745. }
  746. // Find the matching element
  747. int ret = -1;
  748. asUINT size = GetSize();
  749. if( index < size )
  750. {
  751. for( asUINT i = index; i < size; i++ )
  752. {
  753. // value passed by reference
  754. if( Equals(At(i), (value), cmpContext, cache) )
  755. {
  756. ret = (int)i;
  757. break;
  758. }
  759. }
  760. }
  761. if( cmpContext )
  762. if( isNested )
  763. cmpContext->PopState();
  764. else
  765. cmpContext->Release();
  766. return ret;
  767. }
  768. // internal
  769. // Copy object handle or primitive value
  770. void CScriptArray::Copy(void *dst, void *src)
  771. {
  772. memcpy(dst, src, elementSize);
  773. }
  774. // internal
  775. // Return pointer to array item (object handle or primitive value)
  776. void *CScriptArray::GetArrayItemPointer(int index)
  777. {
  778. return buffer->data + index * elementSize;
  779. }
  780. // internal
  781. // Return pointer to data in buffer (object or primitive)
  782. void *CScriptArray::GetDataPointer(void *buffer)
  783. {
  784. if ((subTypeId & asTYPEID_MASK_OBJECT) && !(subTypeId & asTYPEID_OBJHANDLE) )
  785. {
  786. // Real address of object
  787. return reinterpret_cast<void*>(*(size_t*)buffer);
  788. }
  789. else
  790. {
  791. // Primitive is just a raw data
  792. return buffer;
  793. }
  794. }
  795. // Sort ascending
  796. void CScriptArray::SortAsc()
  797. {
  798. Sort(0, GetSize(), true);
  799. }
  800. // Sort ascending
  801. void CScriptArray::SortAsc(asUINT index, asUINT count)
  802. {
  803. Sort(index, count, true);
  804. }
  805. // Sort descending
  806. void CScriptArray::SortDesc()
  807. {
  808. Sort(0, GetSize(), false);
  809. }
  810. // Sort descending
  811. void CScriptArray::SortDesc(asUINT index, asUINT count)
  812. {
  813. Sort(index, count, false);
  814. }
  815. // internal
  816. void CScriptArray::Sort(asUINT index, asUINT count, bool asc)
  817. {
  818. // Subtype isn't primitive and doesn't have opCmp
  819. SArrayCache *cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  820. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  821. {
  822. if( !cache || cache->cmpFunc == 0 )
  823. {
  824. asIScriptContext *ctx = asGetActiveContext();
  825. asIObjectType* subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  826. // Throw an exception
  827. if( ctx )
  828. {
  829. char tmp[512];
  830. #if defined(_MSC_VER) && _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  831. sprintf_s(tmp, 512, "Type '%s' does not have opCmp", subType->GetName());
  832. #else
  833. sprintf(tmp, "Type '%s' does not have opCmp", subType->GetName());
  834. #endif
  835. ctx->SetException(tmp);
  836. }
  837. return;
  838. }
  839. }
  840. // No need to sort
  841. if( count < 2 )
  842. {
  843. return;
  844. }
  845. int start = index;
  846. int end = index + count;
  847. // Check if we could access invalid item while sorting
  848. if( start >= (int)buffer->numElements || end > (int)buffer->numElements )
  849. {
  850. asIScriptContext *ctx = asGetActiveContext();
  851. // Throw an exception
  852. if( ctx )
  853. {
  854. ctx->SetException("Index out of bounds");
  855. }
  856. return;
  857. }
  858. asBYTE tmp[16];
  859. asIScriptContext *cmpContext = 0;
  860. bool isNested = false;
  861. if( (subTypeId & ~asTYPEID_MASK_SEQNBR) && !(subTypeId & asTYPEID_OBJHANDLE) )
  862. {
  863. // Try to reuse the active context
  864. cmpContext = asGetActiveContext();
  865. if( cmpContext )
  866. {
  867. if( cmpContext->PushState() >= 0 )
  868. isNested = true;
  869. else
  870. cmpContext = 0;
  871. }
  872. if( cmpContext == 0 )
  873. {
  874. // TODO: Ideally this context would be retrieved from a pool, so we don't have to
  875. // create a new one everytime. We could keep a context with the array object
  876. // but that would consume a lot of resources as each context is quite heavy.
  877. cmpContext = objType->GetEngine()->CreateContext();
  878. }
  879. }
  880. // Insertion sort
  881. for( int i = start + 1; i < end; i++ )
  882. {
  883. Copy(tmp, GetArrayItemPointer(i));
  884. int j = i - 1;
  885. while( j >= start && Less(GetDataPointer(tmp), At(j), asc, cmpContext) )
  886. {
  887. Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
  888. j--;
  889. }
  890. Copy(GetArrayItemPointer(j + 1), tmp);
  891. }
  892. if( cmpContext )
  893. if( isNested )
  894. cmpContext->PopState();
  895. else
  896. cmpContext->Release();
  897. }
  898. // internal
  899. void CScriptArray::CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src)
  900. {
  901. asIScriptEngine *engine = objType->GetEngine();
  902. if( subTypeId & asTYPEID_OBJHANDLE )
  903. {
  904. // Copy the references and increase the reference counters
  905. if( dst->numElements > 0 && src->numElements > 0 )
  906. {
  907. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  908. void **max = (void**)(dst->data + count * sizeof(void*));
  909. void **d = (void**)dst->data;
  910. void **s = (void**)src->data;
  911. for( ; d < max; d++, s++ )
  912. {
  913. void *tmp = *d;
  914. *d = *s;
  915. if( *d )
  916. engine->AddRefScriptObject(*d, objType->GetSubType());
  917. // Release the old ref after incrementing the new to avoid problem incase it is the same ref
  918. if( tmp )
  919. engine->ReleaseScriptObject(tmp, objType->GetSubType());
  920. }
  921. }
  922. }
  923. else
  924. {
  925. if( dst->numElements > 0 && src->numElements > 0 )
  926. {
  927. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  928. if( subTypeId & asTYPEID_MASK_OBJECT )
  929. {
  930. // Call the assignment operator on all of the objects
  931. void **max = (void**)(dst->data + count * sizeof(void*));
  932. void **d = (void**)dst->data;
  933. void **s = (void**)src->data;
  934. for( ; d < max; d++, s++ )
  935. engine->AssignScriptObject(*d, *s, subTypeId);
  936. }
  937. else
  938. {
  939. // Primitives are copied byte for byte
  940. memcpy(dst->data, src->data, count*elementSize);
  941. }
  942. }
  943. }
  944. }
  945. // internal
  946. // Precache some info
  947. void CScriptArray::Precache()
  948. {
  949. subTypeId = objType->GetSubTypeId();
  950. // Check if it is an array of objects. Only for these do we need to cache anything
  951. // Type ids for primitives and enums only has the sequence number part
  952. if( !(subTypeId & ~asTYPEID_MASK_SEQNBR) )
  953. return;
  954. // The opCmp and opEquals methods are cached because the searching for the
  955. // methods is quite time consuming if a lot of array objects are created.
  956. // First check if a cache already exists for this array type
  957. SArrayCache *cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  958. if( cache ) return;
  959. // We need to make sure the cache is created only once, even
  960. // if multiple threads reach the same point at the same time
  961. asAcquireExclusiveLock();
  962. // Now that we got the lock, we need to check again to make sure the
  963. // cache wasn't created while we were waiting for the lock
  964. cache = reinterpret_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE));
  965. if( cache )
  966. {
  967. asReleaseExclusiveLock();
  968. return;
  969. }
  970. // Create the cache
  971. cache = new SArrayCache();
  972. memset(cache, 0, sizeof(SArrayCache));
  973. asIObjectType *subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  974. if( subType )
  975. {
  976. for( asUINT i = 0; i < subType->GetMethodCount(); i++ )
  977. {
  978. asIScriptFunction *func = subType->GetMethodByIndex(i);
  979. if( func->GetParamCount() == 1 /* && func->IsReadOnly() */ )
  980. {
  981. asDWORD flags = 0;
  982. int returnTypeId = func->GetReturnTypeId();
  983. int paramTypeId = func->GetParamTypeId(0, &flags);
  984. // The parameter must either be a reference to the subtype or a handle to the subtype
  985. if( ((flags & asTM_INREF) && paramTypeId == subTypeId) ||
  986. (flags == 0 &&
  987. (paramTypeId & asTYPEID_OBJHANDLE) &&
  988. (paramTypeId & ~(asTYPEID_OBJHANDLE | asTYPEID_HANDLETOCONST)) == (subTypeId & ~(asTYPEID_OBJHANDLE | asTYPEID_HANDLETOCONST))) )
  989. {
  990. if( returnTypeId == asTYPEID_INT32 && strcmp(func->GetName(), "opCmp") == 0 )
  991. cache->cmpFunc = subType->GetMethodByIndex(i);
  992. if( returnTypeId == asTYPEID_BOOL && strcmp(func->GetName(), "opEquals") == 0 )
  993. cache->eqFunc = subType->GetMethodByIndex(i);
  994. if( cache->cmpFunc && cache->eqFunc )
  995. break;
  996. }
  997. }
  998. }
  999. }
  1000. // Set the user data only at the end so others that retrieve it will know it is complete
  1001. objType->SetUserData(cache, ARRAY_CACHE);
  1002. asReleaseExclusiveLock();
  1003. }
  1004. // GC behaviour
  1005. void CScriptArray::EnumReferences(asIScriptEngine *engine)
  1006. {
  1007. // If the array is holding handles, then we need to notify the GC of them
  1008. if( subTypeId & asTYPEID_MASK_OBJECT )
  1009. {
  1010. void **d = (void**)buffer->data;
  1011. for( asUINT n = 0; n < buffer->numElements; n++ )
  1012. {
  1013. if( d[n] )
  1014. engine->GCEnumCallback(d[n]);
  1015. }
  1016. }
  1017. }
  1018. // GC behaviour
  1019. void CScriptArray::ReleaseAllHandles(asIScriptEngine *engine)
  1020. {
  1021. // Resizing to zero will release everything
  1022. Resize(0);
  1023. }
  1024. void CScriptArray::AddRef() const
  1025. {
  1026. // Clear the GC flag then increase the counter
  1027. gcFlag = false;
  1028. refCount++;
  1029. }
  1030. void CScriptArray::Release() const
  1031. {
  1032. // Now do the actual releasing (clearing the flag set by GC)
  1033. gcFlag = false;
  1034. if( --refCount == 0 )
  1035. {
  1036. delete this;
  1037. }
  1038. }
  1039. // GC behaviour
  1040. int CScriptArray::GetRefCount()
  1041. {
  1042. return refCount;
  1043. }
  1044. // GC behaviour
  1045. void CScriptArray::SetFlag()
  1046. {
  1047. gcFlag = true;
  1048. }
  1049. // GC behaviour
  1050. bool CScriptArray::GetFlag()
  1051. {
  1052. return gcFlag;
  1053. }
  1054. static void ScriptArrayClear(CScriptArray* ptr)
  1055. {
  1056. ptr->Resize(0);
  1057. }
  1058. void RegisterArray(asIScriptEngine* engine)
  1059. {
  1060. // Register the object type user data clean up
  1061. engine->SetObjectTypeUserDataCleanupCallback(CleanupObjectTypeArrayCache, ARRAY_CACHE);
  1062. engine->RegisterObjectType("Array<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE);
  1063. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(ScriptArrayTemplateCallback), asCALL_CDECL);
  1064. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in)", asFUNCTIONPR(ScriptArrayFactory, (asIObjectType*), CScriptArray*), asCALL_CDECL);
  1065. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  1066. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint, const T& in)", asFUNCTIONPR(ScriptArrayFactoryDefVal, (asIObjectType*, asUINT, void *), CScriptArray*), asCALL_CDECL);
  1067. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_LIST_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  1068. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptArray,AddRef), asCALL_THISCALL);
  1069. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL);
  1070. engine->RegisterObjectMethod("Array<T>", "T& opIndex(uint)", asMETHODPR(CScriptArray, At, (unsigned), void*), asCALL_THISCALL);
  1071. engine->RegisterObjectMethod("Array<T>", "const T& opIndex(uint) const", asMETHODPR(CScriptArray, At, (unsigned), void*), asCALL_THISCALL);
  1072. engine->RegisterObjectMethod("Array<T>", "Array<T>& opAssign(const Array<T>& in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL);
  1073. engine->RegisterObjectMethod("Array<T>", "void Insert(uint, const T& in)", asMETHOD(CScriptArray, InsertAt), asCALL_THISCALL);
  1074. engine->RegisterObjectMethod("Array<T>", "void Erase(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL);
  1075. engine->RegisterObjectMethod("Array<T>", "void Push(const T& in)", asMETHOD(CScriptArray, InsertLast), asCALL_THISCALL);
  1076. engine->RegisterObjectMethod("Array<T>", "void Pop()", asMETHOD(CScriptArray, RemoveLast), asCALL_THISCALL);
  1077. engine->RegisterObjectMethod("Array<T>", "void Resize(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  1078. engine->RegisterObjectMethod("Array<T>", "void Clear()", asFUNCTION(ScriptArrayClear), asCALL_CDECL_OBJLAST);
  1079. engine->RegisterObjectMethod("Array<T>", "void Sort()", asMETHODPR(CScriptArray, SortAsc, (), void), asCALL_THISCALL);
  1080. engine->RegisterObjectMethod("Array<T>", "void Sort(uint, uint)", asMETHODPR(CScriptArray, SortAsc, (asUINT, asUINT), void), asCALL_THISCALL);
  1081. engine->RegisterObjectMethod("Array<T>", "void SortReverse()", asMETHODPR(CScriptArray, SortDesc, (), void), asCALL_THISCALL);
  1082. engine->RegisterObjectMethod("Array<T>", "void SortReverse(uint, uint)", asMETHODPR(CScriptArray, SortDesc, (asUINT, asUINT), void), asCALL_THISCALL);
  1083. engine->RegisterObjectMethod("Array<T>", "void Reverse()", asMETHOD(CScriptArray, Reverse), asCALL_THISCALL);
  1084. engine->RegisterObjectMethod("Array<T>", "int Find(const T&in) const", asMETHODPR(CScriptArray, Find, (void*) const, int), asCALL_THISCALL);
  1085. engine->RegisterObjectMethod("Array<T>", "int Find(uint, const T&in) const", asMETHODPR(CScriptArray, Find, (asUINT, void*) const, int), asCALL_THISCALL);
  1086. engine->RegisterObjectMethod("Array<T>", "bool opEquals(const Array<T>&in) const", asMETHOD(CScriptArray, operator==), asCALL_THISCALL);
  1087. engine->RegisterObjectMethod("Array<T>", "uint get_length() const", asMETHOD(CScriptArray, GetSize), asCALL_THISCALL);
  1088. engine->RegisterObjectMethod("Array<T>", "void set_length(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  1089. engine->RegisterObjectMethod("Array<T>", "bool get_empty() const", asMETHOD(CScriptArray, IsEmpty), asCALL_THISCALL);
  1090. engine->RegisterDefaultArrayType("Array<T>");
  1091. }
  1092. static String StringFactory(asUINT length, const char* s)
  1093. {
  1094. return String(s, length);
  1095. }
  1096. static void ConstructString(String* ptr)
  1097. {
  1098. new(ptr) String();
  1099. }
  1100. static void ConstructStringCopy(const String& str, String* ptr)
  1101. {
  1102. new(ptr) String(str);
  1103. }
  1104. static void DestructString(String* ptr)
  1105. {
  1106. ptr->~String();
  1107. }
  1108. static char* StringCharAt(unsigned int i, String& str)
  1109. {
  1110. if (i >= str.Length())
  1111. {
  1112. asIScriptContext* context = asGetActiveContext();
  1113. if (context)
  1114. context->SetException("Index out of bounds");
  1115. return 0;
  1116. }
  1117. return &str[i];
  1118. }
  1119. static int StringCmp(const String& lhs, const String& rhs)
  1120. {
  1121. int cmp = 0;
  1122. if (lhs < rhs)
  1123. cmp = -1;
  1124. else if (lhs > rhs)
  1125. cmp = 1;
  1126. return cmp;
  1127. }
  1128. void StringResize(unsigned newSize, String& str)
  1129. {
  1130. unsigned oldSize = str.Length();
  1131. str.Resize(newSize);
  1132. for (unsigned i = oldSize; i < newSize; ++i)
  1133. str[i] = ' ';
  1134. }
  1135. static void ConstructStringInt(int value, String* ptr)
  1136. {
  1137. new(ptr) String(value);
  1138. }
  1139. static void ConstructStringUInt(unsigned value, String* ptr)
  1140. {
  1141. new(ptr) String(value);
  1142. }
  1143. static void ConstructStringFloat(float value, String* ptr)
  1144. {
  1145. new(ptr) String(value);
  1146. }
  1147. static void ConstructStringBool(bool value, String* ptr)
  1148. {
  1149. new(ptr) String(value);
  1150. }
  1151. static String& StringAssignInt(int value, String& str)
  1152. {
  1153. str = String(value);
  1154. return str;
  1155. }
  1156. static String& StringAddAssignInt(int value, String& str)
  1157. {
  1158. str += String(value);
  1159. return str;
  1160. }
  1161. static String StringAddInt(int value, const String& str)
  1162. {
  1163. return str + String(value);
  1164. }
  1165. static String StringAddIntReverse(int value, const String& str)
  1166. {
  1167. return String(value) + str;
  1168. }
  1169. static String& StringAssignUInt(unsigned value, String& str)
  1170. {
  1171. str = String(value);
  1172. return str;
  1173. }
  1174. static String& StringAddAssignUInt(unsigned value, String& str)
  1175. {
  1176. str += String(value);
  1177. return str;
  1178. }
  1179. static String StringAddUInt(unsigned value, const String& str)
  1180. {
  1181. return str + String(value);
  1182. }
  1183. static String StringAddUIntReverse(unsigned value, const String& str)
  1184. {
  1185. return String(value) + str;
  1186. }
  1187. static String& StringAssignFloat(float value, String& str)
  1188. {
  1189. str = String(value);
  1190. return str;
  1191. }
  1192. static String& StringAddAssignFloat(float value, String& str)
  1193. {
  1194. str += String(value);
  1195. return str;
  1196. }
  1197. static String StringAddFloat(float value, const String& str)
  1198. {
  1199. return str + String(value);
  1200. }
  1201. static String StringAddFloatReverse(float value, const String& str)
  1202. {
  1203. return String(value) + str;
  1204. }
  1205. static String& StringAssignBool(bool value, String& str)
  1206. {
  1207. str = String(value);
  1208. return str;
  1209. }
  1210. static String& StringAddAssignBool(bool value, String& str)
  1211. {
  1212. str += String(value);
  1213. return str;
  1214. }
  1215. static String StringAddBool(bool value, const String& str)
  1216. {
  1217. return str + String(value);
  1218. }
  1219. static String StringAddBoolReverse(bool value, const String& str)
  1220. {
  1221. return String(value) + str;
  1222. }
  1223. static void StringSetUTF8FromLatin1(const String& src, String& str)
  1224. {
  1225. str.SetUTF8FromLatin1(src.CString());
  1226. }
  1227. void RegisterString(asIScriptEngine *engine)
  1228. {
  1229. engine->RegisterObjectType("String", sizeof(String), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
  1230. engine->RegisterStringFactory("String", asFUNCTION(StringFactory), asCALL_CDECL);
  1231. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  1232. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(const String&in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  1233. engine->RegisterObjectBehaviour("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  1234. engine->RegisterObjectMethod("String", "String& opAssign(const String&in)", asMETHODPR(String, operator =, (const String&), String&), asCALL_THISCALL);
  1235. engine->RegisterObjectMethod("String", "String& opAddAssign(const String&in)", asMETHODPR(String, operator +=, (const String&), String&), asCALL_THISCALL);
  1236. engine->RegisterObjectMethod("String", "bool opEquals(const String&in) const", asMETHODPR(String, operator ==, (const String&) const, bool), asCALL_THISCALL);
  1237. engine->RegisterObjectMethod("String", "int opCmp(const String&in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  1238. engine->RegisterObjectMethod("String", "String opAdd(const String&in) const", asMETHODPR(String, operator +, (const String&) const, String), asCALL_THISCALL);
  1239. engine->RegisterObjectMethod("String", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  1240. engine->RegisterObjectMethod("String", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  1241. engine->RegisterObjectMethod("String", "void Replace(uint8, uint8)", asMETHODPR(String, Replace, (char, char), void), asCALL_THISCALL);
  1242. engine->RegisterObjectMethod("String", "void Replace(const String&in, const String&in)", asMETHODPR(String, Replace, (const String&, const String&), void), asCALL_THISCALL);
  1243. engine->RegisterObjectMethod("String", "String Replaced(uint8, uint8) const", asMETHODPR(String, Replaced, (char, char) const, String), asCALL_THISCALL);
  1244. engine->RegisterObjectMethod("String", "String Replaced(const String&in, const String&in) const", asMETHODPR(String, Replaced, (const String&, const String&) const, String), asCALL_THISCALL);
  1245. engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
  1246. engine->RegisterObjectMethod("String", "int Find(const String&in, uint start = 0) const", asMETHODPR(String, Find, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  1247. engine->RegisterObjectMethod("String", "int Find(uint8, uint start = 0) const", asMETHODPR(String, Find, (char, unsigned) const, unsigned), asCALL_THISCALL);
  1248. engine->RegisterObjectMethod("String", "int FindLast(const String&in, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  1249. engine->RegisterObjectMethod("String", "int FindLast(uint8, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (char, unsigned) const, unsigned), asCALL_THISCALL);
  1250. engine->RegisterObjectMethod("String", "bool StartsWith(const String&in) const", asMETHOD(String, StartsWith), asCALL_THISCALL);
  1251. engine->RegisterObjectMethod("String", "bool EndsWith(const String&in) const", asMETHOD(String, EndsWith), asCALL_THISCALL);
  1252. engine->RegisterObjectMethod("String", "String Substring(uint) const", asMETHODPR(String, Substring, (unsigned) const, String), asCALL_THISCALL);
  1253. engine->RegisterObjectMethod("String", "String Substring(uint, uint) const", asMETHODPR(String, Substring, (unsigned, unsigned) const, String), asCALL_THISCALL);
  1254. engine->RegisterObjectMethod("String", "String ToUpper() const", asMETHOD(String, ToUpper), asCALL_THISCALL);
  1255. engine->RegisterObjectMethod("String", "String ToLower() const", asMETHOD(String, ToLower), asCALL_THISCALL);
  1256. engine->RegisterObjectMethod("String", "String Trimmed() const", asMETHOD(String, Trimmed), asCALL_THISCALL);
  1257. engine->RegisterObjectMethod("String", "void SetUTF8FromLatin1(const String& in)", asFUNCTION(StringSetUTF8FromLatin1), asCALL_CDECL_OBJLAST);
  1258. engine->RegisterObjectMethod("String", "uint get_utf8Length() const", asMETHOD(String, LengthUTF8), asCALL_THISCALL);
  1259. engine->RegisterObjectMethod("String", "uint ByteOffsetUTF8(uint) const", asMETHOD(String, ByteOffsetUTF8), asCALL_THISCALL);
  1260. engine->RegisterObjectMethod("String", "uint NextUTF8Char(uint&) const", asMETHOD(String, NextUTF8Char), asCALL_THISCALL);
  1261. engine->RegisterObjectMethod("String", "uint AtUTF8(uint) const", asMETHOD(String, AtUTF8), asCALL_THISCALL);
  1262. engine->RegisterObjectMethod("String", "void ReplaceUTF8(uint, uint)", asMETHOD(String, ReplaceUTF8), asCALL_THISCALL);
  1263. engine->RegisterObjectMethod("String", "void AppendUTF8(uint)", asMETHOD(String, AppendUTF8), asCALL_THISCALL);
  1264. engine->RegisterObjectMethod("String", "String SubstringUTF8(uint) const", asMETHODPR(String, SubstringUTF8, (unsigned) const, String), asCALL_THISCALL);
  1265. engine->RegisterObjectMethod("String", "String SubstringUTF8(uint, uint) const", asMETHODPR(String, SubstringUTF8, (unsigned, unsigned) const, String), asCALL_THISCALL);
  1266. engine->RegisterObjectMethod("String", "uint get_length() const", asMETHOD(String, Length), asCALL_THISCALL);
  1267. engine->RegisterObjectMethod("String", "bool get_empty() const", asMETHOD(String, Empty), asCALL_THISCALL);
  1268. engine->RegisterObjectMethod("String", "int Compare(const String&in, bool caseSensitive = true) const", asMETHODPR(String, Compare, (const String&, bool) const, int), asCALL_THISCALL);
  1269. // Register automatic conversion functions for convenience
  1270. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  1271. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  1272. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  1273. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  1274. engine->RegisterObjectMethod("String", "String& opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  1275. engine->RegisterObjectMethod("String", "String& opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  1276. engine->RegisterObjectMethod("String", "String opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  1277. engine->RegisterObjectMethod("String", "String opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  1278. engine->RegisterObjectMethod("String", "String& opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  1279. engine->RegisterObjectMethod("String", "String& opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  1280. engine->RegisterObjectMethod("String", "String opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  1281. engine->RegisterObjectMethod("String", "String opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  1282. engine->RegisterObjectMethod("String", "String& opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  1283. engine->RegisterObjectMethod("String", "String& opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  1284. engine->RegisterObjectMethod("String", "String opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  1285. engine->RegisterObjectMethod("String", "String opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  1286. engine->RegisterObjectMethod("String", "String& opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  1287. engine->RegisterObjectMethod("String", "String& opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  1288. engine->RegisterObjectMethod("String", "String opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  1289. engine->RegisterObjectMethod("String", "String opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  1290. }