Addons.cpp 51 KB

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