Addons.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 <stdio.h>
  28. // Adapted from Angelscript's scriptarray & scriptstdstring add-ons, but with garbage collection disabled
  29. /// %Script array buffer.
  30. struct SArrayBuffer
  31. {
  32. asDWORD numElements;
  33. asBYTE data[1];
  34. };
  35. static CScriptArray* ScriptArrayFactory2(asIObjectType *ot, asUINT length)
  36. {
  37. CScriptArray *a = new CScriptArray(length, ot);
  38. // It's possible the constructor raised a script exception, in which case we
  39. // need to free the memory and return null instead, else we get a memory leak.
  40. asIScriptContext *ctx = asGetActiveContext();
  41. if( ctx && ctx->GetState() == asEXECUTION_EXCEPTION )
  42. {
  43. delete a;
  44. return 0;
  45. }
  46. return a;
  47. }
  48. static CScriptArray* ScriptArrayFactoryDefVal(asIObjectType *ot, asUINT length, void *defVal)
  49. {
  50. CScriptArray *a = new CScriptArray(length, defVal, ot);
  51. // It's possible the constructor raised a script exception, in which case we
  52. // need to free the memory and return null instead, else we get a memory leak.
  53. asIScriptContext *ctx = asGetActiveContext();
  54. if( ctx && ctx->GetState() == asEXECUTION_EXCEPTION )
  55. {
  56. delete a;
  57. return 0;
  58. }
  59. return a;
  60. }
  61. static CScriptArray* ScriptArrayFactory(asIObjectType *ot)
  62. {
  63. return ScriptArrayFactory2(ot, 0);
  64. }
  65. // This optional callback is called when the template type is first used by the compiler.
  66. // It allows the application to validate if the template can be instanciated for the requested
  67. // subtype at compile time, instead of at runtime.
  68. static bool ScriptArrayTemplateCallback(asIObjectType *ot)
  69. {
  70. // Make sure the subtype can be instanciated with a default factory/constructor,
  71. // otherwise we won't be able to instanciate the elements. Script classes always
  72. // have default factories, so we don't have to worry about those.
  73. int typeId = ot->GetSubTypeId();
  74. if( typeId == asTYPEID_VOID )
  75. return false;
  76. if( (typeId & asTYPEID_MASK_OBJECT) && !(typeId & asTYPEID_OBJHANDLE) && !(typeId & asTYPEID_SCRIPTOBJECT) )
  77. {
  78. asIObjectType *subtype = ot->GetEngine()->GetObjectTypeById(typeId);
  79. asDWORD flags = subtype->GetFlags();
  80. if( (flags & asOBJ_VALUE) && !(flags & asOBJ_POD) )
  81. {
  82. // Verify that there is a default constructor
  83. for( asUINT n = 0; n < subtype->GetBehaviourCount(); n++ )
  84. {
  85. asEBehaviours beh;
  86. int funcId = subtype->GetBehaviourByIndex(n, &beh);
  87. if( beh != asBEHAVE_CONSTRUCT ) continue;
  88. asIScriptFunction *func = ot->GetEngine()->GetFunctionById(funcId);
  89. if( func->GetParamCount() == 0 )
  90. {
  91. // Found the default constructor
  92. return true;
  93. }
  94. }
  95. // There is no default constructor
  96. return false;
  97. }
  98. else if( (flags & asOBJ_REF) )
  99. {
  100. // Verify that there is a default factory
  101. for( asUINT n = 0; n < subtype->GetFactoryCount(); n++ )
  102. {
  103. asIScriptFunction *func = subtype->GetFactoryByIndex(n);
  104. if( func->GetParamCount() == 0 )
  105. {
  106. // Found the default factory
  107. return true;
  108. }
  109. }
  110. // No default factory
  111. return false;
  112. }
  113. }
  114. // The type is ok
  115. return true;
  116. }
  117. CScriptArray &CScriptArray::operator=(const CScriptArray &other)
  118. {
  119. // Only perform the copy if the array types are the same
  120. if( &other != this &&
  121. other.GetArrayObjectType() == GetArrayObjectType() )
  122. {
  123. if( buffer )
  124. {
  125. DeleteBuffer(buffer);
  126. buffer = 0;
  127. }
  128. // Copy all elements from the other array
  129. CreateBuffer(&buffer, other.buffer->numElements);
  130. CopyBuffer(buffer, other.buffer);
  131. }
  132. return *this;
  133. }
  134. CScriptArray::CScriptArray(asUINT length, asIObjectType *ot)
  135. {
  136. refCount = 1;
  137. gcFlag = false;
  138. objType = ot;
  139. objType->AddRef();
  140. buffer = 0;
  141. Precache();
  142. // Determine element size
  143. if( subTypeId & asTYPEID_MASK_OBJECT )
  144. {
  145. elementSize = sizeof(asPWORD);
  146. }
  147. else
  148. {
  149. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
  150. }
  151. // Make sure the array size isn't too large for us to handle
  152. if( !CheckMaxSize(length) )
  153. {
  154. // Don't continue with the initialization
  155. return;
  156. }
  157. CreateBuffer(&buffer, length);
  158. }
  159. CScriptArray::CScriptArray(asUINT length, void *defVal, asIObjectType *ot)
  160. {
  161. refCount = 1;
  162. gcFlag = false;
  163. objType = ot;
  164. objType->AddRef();
  165. buffer = 0;
  166. Precache();
  167. // Determine element size
  168. if( subTypeId & asTYPEID_MASK_OBJECT )
  169. {
  170. elementSize = sizeof(asPWORD);
  171. }
  172. else
  173. {
  174. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(subTypeId);
  175. }
  176. // Make sure the array size isn't too large for us to handle
  177. if( !CheckMaxSize(length) )
  178. {
  179. // Don't continue with the initialization
  180. return;
  181. }
  182. CreateBuffer(&buffer, length);
  183. // Initialize the elements with the default value
  184. for( asUINT n = 0; n < GetSize(); n++ )
  185. SetValue(n, defVal);
  186. }
  187. // Internal
  188. void CScriptArray::SetValue(asUINT index, void *value)
  189. {
  190. if( (subTypeId & ~0x03FFFFFF) && !(subTypeId & asTYPEID_OBJHANDLE) )
  191. objType->GetEngine()->CopyScriptObject(At(index), value, subTypeId);
  192. else if( subTypeId & asTYPEID_OBJHANDLE )
  193. {
  194. *(void**)At(index) = *(void**)value;
  195. objType->GetEngine()->AddRefScriptObject(*(void**)value, objType->GetSubType());
  196. }
  197. else if( subTypeId == asTYPEID_BOOL ||
  198. subTypeId == asTYPEID_INT8 ||
  199. subTypeId == asTYPEID_UINT8 )
  200. *(char*)At(index) = *(char*)value;
  201. else if( subTypeId == asTYPEID_INT16 ||
  202. subTypeId == asTYPEID_UINT16 )
  203. *(short*)At(index) = *(short*)value;
  204. else if( subTypeId == asTYPEID_INT32 ||
  205. subTypeId == asTYPEID_UINT32 ||
  206. subTypeId == asTYPEID_FLOAT )
  207. *(int*)At(index) = *(int*)value;
  208. else if( subTypeId == asTYPEID_INT64 ||
  209. subTypeId == asTYPEID_UINT64 ||
  210. subTypeId == asTYPEID_DOUBLE )
  211. *(double*)At(index) = *(double*)value;
  212. }
  213. CScriptArray::~CScriptArray()
  214. {
  215. if( buffer )
  216. {
  217. DeleteBuffer(buffer);
  218. buffer = 0;
  219. }
  220. if( objType ) objType->Release();
  221. }
  222. asUINT CScriptArray::GetSize() const
  223. {
  224. return buffer->numElements;
  225. }
  226. void CScriptArray::Resize(asUINT numElements)
  227. {
  228. if( numElements & 0x80000000 )
  229. {
  230. CheckMaxSize(numElements);
  231. return;
  232. }
  233. Resize((int)numElements - (int)buffer->numElements, (asUINT)-1);
  234. }
  235. // Internal
  236. void CScriptArray::Resize(int delta, asUINT at)
  237. {
  238. if( delta < 0 )
  239. {
  240. if( -delta > (int)buffer->numElements )
  241. delta = -(int)buffer->numElements;
  242. if( at > buffer->numElements + delta )
  243. at = buffer->numElements + delta;
  244. }
  245. else if( delta > 0 )
  246. {
  247. // Make sure the array size isn't too large for us to handle
  248. if( delta > 0 && !CheckMaxSize(buffer->numElements + delta) )
  249. return;
  250. if( at > buffer->numElements )
  251. at = buffer->numElements;
  252. }
  253. if( delta == 0 ) return;
  254. // Allocate memory for the buffer
  255. SArrayBuffer *newBuffer;
  256. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  257. newBuffer->numElements = buffer->numElements + delta;
  258. memcpy(newBuffer->data, buffer->data, at*elementSize);
  259. if( delta > 0 && at < buffer->numElements )
  260. memcpy(newBuffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements-at)*elementSize);
  261. else if( delta < 0 && at < buffer->numElements )
  262. memcpy(newBuffer->data + at*elementSize, buffer->data + (at-delta)*elementSize, (buffer->numElements-at+delta)*elementSize);
  263. if( subTypeId & asTYPEID_MASK_OBJECT )
  264. {
  265. if( delta > 0 )
  266. Construct(newBuffer, at, at+delta);
  267. else if( delta < 0 )
  268. Destruct(buffer, at, at-delta);
  269. }
  270. // Release the old buffer
  271. delete[] (asBYTE*)buffer;
  272. buffer = newBuffer;
  273. }
  274. // internal
  275. bool CScriptArray::CheckMaxSize(asUINT numElements)
  276. {
  277. // This code makes sure the size of the buffer that is allocated
  278. // for the array doesn't overflow and becomes smaller than requested
  279. asUINT maxSize = 0xFFFFFFFFul - sizeof(SArrayBuffer) + 1;
  280. if( subTypeId & asTYPEID_MASK_OBJECT )
  281. maxSize /= sizeof(void*);
  282. else if( elementSize > 0 )
  283. maxSize /= elementSize;
  284. if( numElements > maxSize )
  285. {
  286. asIScriptContext *ctx = asGetActiveContext();
  287. if( ctx )
  288. {
  289. // Set a script exception
  290. ctx->SetException("Too large array size");
  291. }
  292. return false;
  293. }
  294. // OK
  295. return true;
  296. }
  297. asIObjectType *CScriptArray::GetArrayObjectType() const
  298. {
  299. return objType;
  300. }
  301. int CScriptArray::GetArrayTypeId() const
  302. {
  303. return objType->GetTypeId();
  304. }
  305. int CScriptArray::GetElementTypeId() const
  306. {
  307. return subTypeId;
  308. }
  309. void CScriptArray::InsertAt(asUINT index, void *value)
  310. {
  311. if( index > buffer->numElements )
  312. {
  313. // If this is called from a script we raise a script exception
  314. asIScriptContext *ctx = asGetActiveContext();
  315. if( ctx )
  316. ctx->SetException("Index out of bounds");
  317. return;
  318. }
  319. // Make room for the new element
  320. Resize(1, index);
  321. // Set the value of the new element
  322. SetValue(index, value);
  323. }
  324. void CScriptArray::InsertLast(void *value)
  325. {
  326. InsertAt(buffer->numElements, value);
  327. }
  328. void CScriptArray::RemoveAt(asUINT index)
  329. {
  330. if( index >= buffer->numElements )
  331. {
  332. // If this is called from a script we raise a script exception
  333. asIScriptContext *ctx = asGetActiveContext();
  334. if( ctx )
  335. ctx->SetException("Index out of bounds");
  336. return;
  337. }
  338. // Remove the element
  339. Resize(-1, index);
  340. }
  341. void CScriptArray::RemoveLast()
  342. {
  343. RemoveAt(buffer->numElements-1);
  344. }
  345. // Return a pointer to the array element. Returns 0 if the index is out of bounds
  346. void *CScriptArray::At(asUINT index)
  347. {
  348. if( index >= buffer->numElements )
  349. {
  350. // If this is called from a script we raise a script exception
  351. asIScriptContext *ctx = asGetActiveContext();
  352. if( ctx )
  353. ctx->SetException("Index out of bounds");
  354. return 0;
  355. }
  356. else
  357. {
  358. if( (subTypeId & asTYPEID_MASK_OBJECT) && !(subTypeId & asTYPEID_OBJHANDLE) )
  359. return (void*)((size_t*)buffer->data)[index];
  360. else
  361. return buffer->data + elementSize*index;
  362. }
  363. }
  364. // internal
  365. void CScriptArray::CreateBuffer(SArrayBuffer **buf, asUINT numElements)
  366. {
  367. if( subTypeId & asTYPEID_MASK_OBJECT )
  368. {
  369. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  370. (*buf)->numElements = numElements;
  371. }
  372. else
  373. {
  374. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  375. (*buf)->numElements = numElements;
  376. }
  377. Construct(*buf, 0, numElements);
  378. }
  379. // internal
  380. void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
  381. {
  382. Destruct(buf, 0, buf->numElements);
  383. // Free the buffer
  384. delete[] (asBYTE*)buf;
  385. }
  386. // internal
  387. void CScriptArray::Construct(SArrayBuffer *buf, asUINT start, asUINT end)
  388. {
  389. if( subTypeId & asTYPEID_OBJHANDLE )
  390. {
  391. // Set all object handles to null
  392. void *d = (void*)(buf->data + start * sizeof(void*));
  393. memset(d, 0, (end-start)*sizeof(void*));
  394. }
  395. else if( subTypeId & asTYPEID_MASK_OBJECT )
  396. {
  397. void **max = (void**)(buf->data + end * sizeof(void*));
  398. void **d = (void**)(buf->data + start * sizeof(void*));
  399. asIScriptEngine *engine = objType->GetEngine();
  400. for( ; d < max; d++ )
  401. *d = (void*)engine->CreateScriptObject(subTypeId);
  402. }
  403. }
  404. // internal
  405. void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
  406. {
  407. if( subTypeId & asTYPEID_MASK_OBJECT )
  408. {
  409. asIScriptEngine *engine = objType->GetEngine();
  410. void **max = (void**)(buf->data + end * sizeof(void*));
  411. void **d = (void**)(buf->data + start * sizeof(void*));
  412. for( ; d < max; d++ )
  413. {
  414. if( *d )
  415. engine->ReleaseScriptObject(*d, objType->GetSubType());
  416. }
  417. }
  418. }
  419. // internal
  420. bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext *ctx)
  421. {
  422. if( !asc )
  423. {
  424. // Swap items
  425. const void *TEMP = a;
  426. a = b;
  427. b = TEMP;
  428. }
  429. if( subTypeId <= asTYPEID_DOUBLE )
  430. {
  431. // Simple compare of values
  432. switch( subTypeId )
  433. {
  434. #define COMPARE(T) *((T*)a) < *((T*)b)
  435. case asTYPEID_BOOL: return COMPARE(bool);
  436. case asTYPEID_INT8: return COMPARE(signed char);
  437. case asTYPEID_UINT8: return COMPARE(unsigned char);
  438. case asTYPEID_INT16: return COMPARE(signed short);
  439. case asTYPEID_UINT16: return COMPARE(unsigned short);
  440. case asTYPEID_INT32: return COMPARE(signed int);
  441. case asTYPEID_UINT32: return COMPARE(unsigned int);
  442. case asTYPEID_FLOAT: return COMPARE(float);
  443. case asTYPEID_DOUBLE: return COMPARE(double);
  444. #undef COMPARE
  445. }
  446. }
  447. else
  448. {
  449. int r = 0;
  450. // Execute object opCmp
  451. // TODO: Add proper error handling
  452. r = ctx->Prepare(cmpFunc); assert(r >= 0);
  453. r = ctx->SetObject((void*)a); assert(r >= 0);
  454. r = ctx->SetArgAddress(0, (void*)b); assert(r >= 0);
  455. r = ctx->Execute();
  456. if( r == asEXECUTION_FINISHED )
  457. {
  458. return (int)ctx->GetReturnDWord() < 0;
  459. }
  460. }
  461. return false;
  462. }
  463. void CScriptArray::Reverse()
  464. {
  465. asUINT size = GetSize();
  466. if( size >= 2 )
  467. {
  468. asBYTE TEMP[16];
  469. for( asUINT i = 0; i < size / 2; i++ )
  470. {
  471. Copy(TEMP, GetArrayItemPointer(i));
  472. Copy(GetArrayItemPointer(i), GetArrayItemPointer(size - i - 1));
  473. Copy(GetArrayItemPointer(size - i - 1), TEMP);
  474. }
  475. }
  476. }
  477. // internal
  478. bool CScriptArray::Equals(const void *a, const void *b, asIScriptContext *ctx)
  479. {
  480. if( subTypeId <= asTYPEID_DOUBLE )
  481. {
  482. // Simple compare of values
  483. switch( subTypeId )
  484. {
  485. #define COMPARE(T) *((T*)a) == *((T*)b)
  486. case asTYPEID_BOOL: return COMPARE(bool);
  487. case asTYPEID_INT8: return COMPARE(signed char);
  488. case asTYPEID_UINT8: return COMPARE(unsigned char);
  489. case asTYPEID_INT16: return COMPARE(signed short);
  490. case asTYPEID_UINT16: return COMPARE(unsigned short);
  491. case asTYPEID_INT32: return COMPARE(signed int);
  492. case asTYPEID_UINT32: return COMPARE(unsigned int);
  493. case asTYPEID_FLOAT: return COMPARE(float);
  494. case asTYPEID_DOUBLE: return COMPARE(double);
  495. #undef COMPARE
  496. }
  497. }
  498. else
  499. {
  500. int r = 0;
  501. // Execute object opEquals if available
  502. if( eqFunc )
  503. {
  504. // TODO: Add proper error handling
  505. r = ctx->Prepare(eqFunc); assert(r >= 0);
  506. r = ctx->SetObject((void*)a); assert(r >= 0);
  507. r = ctx->SetArgAddress(0, (void*)b); assert(r >= 0);
  508. r = ctx->Execute();
  509. if( r == asEXECUTION_FINISHED )
  510. {
  511. return ctx->GetReturnByte() != 0;
  512. }
  513. }
  514. // Execute object opCmp if available
  515. if( cmpFunc )
  516. {
  517. // TODO: Add proper error handling
  518. r = ctx->Prepare(cmpFunc); assert(r >= 0);
  519. r = ctx->SetObject((void*)a); assert(r >= 0);
  520. r = ctx->SetArgAddress(0, (void*)b); assert(r >= 0);
  521. r = ctx->Execute();
  522. if( r == asEXECUTION_FINISHED )
  523. {
  524. return (int)ctx->GetReturnDWord() == 0;
  525. }
  526. }
  527. }
  528. return false;
  529. }
  530. int CScriptArray::Find(void *value)
  531. {
  532. return Find(0, value);
  533. }
  534. int CScriptArray::Find(asUINT index, void *value)
  535. {
  536. // Subtype isn't primitive and doesn't have opEquals / opCmp
  537. if( subTypeId > asTYPEID_DOUBLE && (cmpFunc == 0 && eqFunc == 0) )
  538. {
  539. asIScriptContext *ctx = asGetActiveContext();
  540. asIObjectType* subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  541. // Throw an exception
  542. if( ctx )
  543. {
  544. char tmp[512];
  545. #if defined(_MSC_VER) && _MSC_VER >= 1500
  546. sprintf_s(tmp, 512, "Type '%s' does not have opEquals / opCmp", subType->GetName());
  547. #else
  548. sprintf(tmp, "Type '%s' does not have opEquals / opCmp", subType->GetName());
  549. #endif
  550. ctx->SetException(tmp);
  551. }
  552. return -1;
  553. }
  554. asIScriptContext *cmpContext = 0;
  555. if( subTypeId > asTYPEID_DOUBLE )
  556. {
  557. // TODO: Ideally this context would be retrieved from a pool, so we don't have to
  558. // create a new one everytime. We could keep a context with the array object
  559. // but that would consume a lot of resources as each context is quite heavy.
  560. cmpContext = objType->GetEngine()->CreateContext();
  561. }
  562. int ret = -1;
  563. asUINT size = GetSize();
  564. if( index < size )
  565. {
  566. for( asUINT i = index; i < size; i++ )
  567. {
  568. // value passed by reference
  569. if( Equals(At(i), (value), cmpContext) )
  570. {
  571. ret = (int)i;
  572. break;
  573. }
  574. }
  575. }
  576. if( cmpContext )
  577. cmpContext->Release();
  578. return ret;
  579. }
  580. // internal
  581. // Copy object handle or primitive value
  582. void CScriptArray::Copy(void *dst, void *src)
  583. {
  584. memcpy(dst, src, elementSize);
  585. }
  586. // internal
  587. // Return pointer to array item (object handle or primitive value)
  588. void *CScriptArray::GetArrayItemPointer(int index)
  589. {
  590. return buffer->data + index * elementSize;
  591. }
  592. // internal
  593. // Return pointer to data in buffer (object or primitive)
  594. void *CScriptArray::GetDataPointer(void *buffer)
  595. {
  596. if ((subTypeId & asTYPEID_MASK_OBJECT) && !(subTypeId & asTYPEID_OBJHANDLE) )
  597. {
  598. // Real address of object
  599. return reinterpret_cast<void*>(*(size_t*)buffer);
  600. }
  601. else
  602. {
  603. // Primitive is just a raw data
  604. return buffer;
  605. }
  606. }
  607. // Sort ascending
  608. void CScriptArray::SortAsc()
  609. {
  610. Sort(0, GetSize(), true);
  611. }
  612. // Sort ascending
  613. void CScriptArray::SortAsc(asUINT index, asUINT count)
  614. {
  615. Sort(index, count, true);
  616. }
  617. // Sort descending
  618. void CScriptArray::SortDesc()
  619. {
  620. Sort(0, GetSize(), false);
  621. }
  622. // Sort descending
  623. void CScriptArray::SortDesc(asUINT index, asUINT count)
  624. {
  625. Sort(index, count, false);
  626. }
  627. // internal
  628. void CScriptArray::Sort(asUINT index, asUINT count, bool asc)
  629. {
  630. // Subtype isn't primitive and doesn't have opCmp
  631. if( subTypeId > asTYPEID_DOUBLE && cmpFunc == 0 )
  632. {
  633. asIScriptContext *ctx = asGetActiveContext();
  634. asIObjectType* subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  635. // Throw an exception
  636. if( ctx )
  637. {
  638. char tmp[512];
  639. #if defined(_MSC_VER) && _MSC_VER >= 1500
  640. sprintf_s(tmp, 512, "Type '%s' does not have opCmp", subType->GetName());
  641. #else
  642. sprintf(tmp, "Type '%s' does not have opCmp", subType->GetName());
  643. #endif
  644. ctx->SetException(tmp);
  645. }
  646. return;
  647. }
  648. // No need to sort
  649. if( count < 2 )
  650. {
  651. return;
  652. }
  653. int start = index;
  654. int end = index + count;
  655. // Check if we could access invalid item while sorting
  656. if( start >= (int)buffer->numElements || end > (int)buffer->numElements )
  657. {
  658. asIScriptContext *ctx = asGetActiveContext();
  659. // Throw an exception
  660. if( ctx )
  661. {
  662. ctx->SetException("Index out of bounds");
  663. }
  664. return;
  665. }
  666. asBYTE tmp[16];
  667. asIScriptContext *cmpContext = 0;
  668. if( subTypeId > asTYPEID_DOUBLE )
  669. {
  670. // TODO: Ideally this context would be retrieved from a pool, so we don't have to
  671. // create a new one everytime. We could keep a context with the array object
  672. // but that would consume a lot of resources as each context is quite heavy.
  673. cmpContext = objType->GetEngine()->CreateContext();
  674. }
  675. // Insertion sort
  676. for( int i = start + 1; i < end; i++ )
  677. {
  678. Copy(tmp, GetArrayItemPointer(i));
  679. int j = i - 1;
  680. while( j >= start && Less(GetDataPointer(tmp), At(j), asc, cmpContext) )
  681. {
  682. Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
  683. j--;
  684. }
  685. Copy(GetArrayItemPointer(j + 1), tmp);
  686. }
  687. if( cmpContext )
  688. cmpContext->Release();
  689. }
  690. // internal
  691. void CScriptArray::CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src)
  692. {
  693. asIScriptEngine *engine = objType->GetEngine();
  694. if( subTypeId & asTYPEID_OBJHANDLE )
  695. {
  696. // Copy the references and increase the reference counters
  697. if( dst->numElements > 0 && src->numElements > 0 )
  698. {
  699. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  700. void **max = (void**)(dst->data + count * sizeof(void*));
  701. void **d = (void**)dst->data;
  702. void **s = (void**)src->data;
  703. for( ; d < max; d++, s++ )
  704. {
  705. *d = *s;
  706. if( *d )
  707. engine->AddRefScriptObject(*d, objType->GetSubType());
  708. }
  709. }
  710. }
  711. else
  712. {
  713. if( dst->numElements > 0 && src->numElements > 0 )
  714. {
  715. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  716. if( subTypeId & asTYPEID_MASK_OBJECT )
  717. {
  718. // Call the assignment operator on all of the objects
  719. void **max = (void**)(dst->data + count * sizeof(void*));
  720. void **d = (void**)dst->data;
  721. void **s = (void**)src->data;
  722. for( ; d < max; d++, s++ )
  723. engine->CopyScriptObject(*d, *s, subTypeId);
  724. }
  725. else
  726. {
  727. // Primitives are copied byte for byte
  728. memcpy(dst->data, src->data, count*elementSize);
  729. }
  730. }
  731. }
  732. }
  733. // internal
  734. // Precache some info
  735. void CScriptArray::Precache()
  736. {
  737. // TODO: optimize: This information could be stored in the object type as user data,
  738. // then it wouldn't be necessary to look for this for each array initialization
  739. subTypeId = objType->GetSubTypeId();
  740. cmpFunc = 0;
  741. eqFunc = 0;
  742. // Object - search for opCmp / opEquals
  743. if( subTypeId > asTYPEID_DOUBLE )
  744. {
  745. asIObjectType *subType = objType->GetEngine()->GetObjectTypeById(subTypeId);
  746. if( subType )
  747. {
  748. for( asUINT i = 0; i < subType->GetMethodCount(); i++ )
  749. {
  750. asIScriptFunction *func = subType->GetMethodByIndex(i);
  751. if( func->GetParamCount() == 1 /* && func->IsReadOnly() */ )
  752. {
  753. asDWORD flags = 0;
  754. int returnTypeId = func->GetReturnTypeId();
  755. int paramTypeId = func->GetParamTypeId(0, &flags);
  756. if( flags == asTM_INREF && paramTypeId == subTypeId )
  757. {
  758. if( returnTypeId == asTYPEID_INT32 && strcmp(func->GetName(), "opCmp") == 0 )
  759. {
  760. cmpFunc = subType->GetMethodByIndex(i);
  761. }
  762. if( returnTypeId == asTYPEID_BOOL && strcmp(func->GetName(), "opEquals") == 0 )
  763. {
  764. eqFunc = subType->GetMethodByIndex(i);
  765. }
  766. if( cmpFunc != 0 && eqFunc != 0 )
  767. {
  768. break;
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. void CScriptArray::AddRef() const
  777. {
  778. // Clear the GC flag then increase the counter
  779. gcFlag = false;
  780. refCount++;
  781. }
  782. void CScriptArray::Release() const
  783. {
  784. // Now do the actual releasing (clearing the flag set by GC)
  785. gcFlag = false;
  786. if( --refCount == 0 )
  787. {
  788. delete this;
  789. }
  790. }
  791. static void ScriptArrayClear(CScriptArray* ptr)
  792. {
  793. ptr->Resize(0);
  794. }
  795. static bool ScriptArrayIsEmpty(CScriptArray* ptr)
  796. {
  797. return ptr->GetSize() == 0;
  798. }
  799. void RegisterArray(asIScriptEngine* engine)
  800. {
  801. engine->RegisterObjectType("Array<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE);
  802. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(ScriptArrayTemplateCallback), asCALL_CDECL);
  803. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in)", asFUNCTIONPR(ScriptArrayFactory, (asIObjectType*), CScriptArray*), asCALL_CDECL);
  804. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  805. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint, const T& in)", asFUNCTIONPR(ScriptArrayFactoryDefVal, (asIObjectType*, asUINT, void *), CScriptArray*), asCALL_CDECL);
  806. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_LIST_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  807. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptArray,AddRef), asCALL_THISCALL);
  808. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL);
  809. engine->RegisterObjectMethod("Array<T>", "T& opIndex(uint)", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  810. engine->RegisterObjectMethod("Array<T>", "const T& opIndex(uint) const", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  811. engine->RegisterObjectMethod("Array<T>", "Array<T>& opAssign(const Array<T>& in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL);
  812. engine->RegisterObjectMethod("Array<T>", "void Insert(uint, const T& in)", asMETHOD(CScriptArray, InsertAt), asCALL_THISCALL);
  813. engine->RegisterObjectMethod("Array<T>", "void Erase(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL);
  814. engine->RegisterObjectMethod("Array<T>", "void Push(const T& in)", asMETHOD(CScriptArray, InsertLast), asCALL_THISCALL);
  815. engine->RegisterObjectMethod("Array<T>", "void Pop()", asMETHOD(CScriptArray, RemoveLast), asCALL_THISCALL);
  816. engine->RegisterObjectMethod("Array<T>", "void Resize(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  817. engine->RegisterObjectMethod("Array<T>", "void Clear()", asFUNCTION(ScriptArrayClear), asCALL_CDECL_OBJLAST);
  818. engine->RegisterObjectMethod("Array<T>", "void Sort()", asMETHODPR(CScriptArray, SortAsc, (), void), asCALL_THISCALL);
  819. engine->RegisterObjectMethod("Array<T>", "void Sort(uint, uint)", asMETHODPR(CScriptArray, SortAsc, (asUINT, asUINT), void), asCALL_THISCALL);
  820. engine->RegisterObjectMethod("Array<T>", "void SortReverse()", asMETHODPR(CScriptArray, SortDesc, (), void), asCALL_THISCALL);
  821. engine->RegisterObjectMethod("Array<T>", "void SortReverse(uint, uint)", asMETHODPR(CScriptArray, SortDesc, (asUINT, asUINT), void), asCALL_THISCALL);
  822. engine->RegisterObjectMethod("Array<T>", "void Reverse()", asMETHOD(CScriptArray, Reverse), asCALL_THISCALL);
  823. engine->RegisterObjectMethod("Array<T>", "int Find(const T&in) const", asMETHODPR(CScriptArray, Find, (void*), int), asCALL_THISCALL);
  824. engine->RegisterObjectMethod("Array<T>", "int Find(uint, const T&in) const", asMETHODPR(CScriptArray, Find, (asUINT, void*), int), asCALL_THISCALL);
  825. engine->RegisterObjectMethod("Array<T>", "uint get_length() const", asMETHOD(CScriptArray, GetSize), asCALL_THISCALL);
  826. engine->RegisterObjectMethod("Array<T>", "void set_length(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  827. engine->RegisterObjectMethod("Array<T>", "bool get_empty() const", asFUNCTION(ScriptArrayIsEmpty), asCALL_CDECL_OBJLAST);
  828. engine->RegisterDefaultArrayType("Array<T>");
  829. }
  830. static String StringFactory(asUINT length, const char* s)
  831. {
  832. return String(s, length);
  833. }
  834. static void ConstructString(String* ptr)
  835. {
  836. new(ptr) String();
  837. }
  838. static void ConstructStringCopy(const String& str, String* ptr)
  839. {
  840. new(ptr) String(str);
  841. }
  842. static void DestructString(String* ptr)
  843. {
  844. ptr->~String();
  845. }
  846. static char* StringCharAt(unsigned int i, String& str)
  847. {
  848. if (i >= str.Length())
  849. {
  850. asIScriptContext* context = asGetActiveContext();
  851. if (context)
  852. context->SetException("Index out of bounds");
  853. return 0;
  854. }
  855. return &str[i];
  856. }
  857. static int StringCmp(const String& lhs, const String& rhs)
  858. {
  859. int cmp = 0;
  860. if (lhs < rhs)
  861. cmp = -1;
  862. else if (lhs > rhs)
  863. cmp = 1;
  864. return cmp;
  865. }
  866. void StringResize(unsigned newSize, String& str)
  867. {
  868. unsigned oldSize = str.Length();
  869. str.Resize(newSize);
  870. for (unsigned i = oldSize; i < newSize; ++i)
  871. str[i] = ' ';
  872. }
  873. static void ConstructStringInt(int value, String* ptr)
  874. {
  875. new(ptr) String(value);
  876. }
  877. static void ConstructStringUInt(unsigned value, String* ptr)
  878. {
  879. new(ptr) String(value);
  880. }
  881. static void ConstructStringFloat(float value, String* ptr)
  882. {
  883. new(ptr) String(value);
  884. }
  885. static void ConstructStringBool(bool value, String* ptr)
  886. {
  887. new(ptr) String(value);
  888. }
  889. static String& StringAssignInt(int value, String& str)
  890. {
  891. str = String(value);
  892. return str;
  893. }
  894. static String& StringAddAssignInt(int value, String& str)
  895. {
  896. str += String(value);
  897. return str;
  898. }
  899. static String StringAddInt(int value, const String& str)
  900. {
  901. return str + String(value);
  902. }
  903. static String StringAddIntReverse(int value, const String& str)
  904. {
  905. return String(value) + str;
  906. }
  907. static String& StringAssignUInt(unsigned value, String& str)
  908. {
  909. str = String(value);
  910. return str;
  911. }
  912. static String& StringAddAssignUInt(unsigned value, String& str)
  913. {
  914. str += String(value);
  915. return str;
  916. }
  917. static String StringAddUInt(unsigned value, const String& str)
  918. {
  919. return str + String(value);
  920. }
  921. static String StringAddUIntReverse(unsigned value, const String& str)
  922. {
  923. return String(value) + str;
  924. }
  925. static String& StringAssignFloat(float value, String& str)
  926. {
  927. str = String(value);
  928. return str;
  929. }
  930. static String& StringAddAssignFloat(float value, String& str)
  931. {
  932. str += String(value);
  933. return str;
  934. }
  935. static String StringAddFloat(float value, const String& str)
  936. {
  937. return str + String(value);
  938. }
  939. static String StringAddFloatReverse(float value, const String& str)
  940. {
  941. return String(value) + str;
  942. }
  943. static String& StringAssignBool(bool value, String& str)
  944. {
  945. str = String(value);
  946. return str;
  947. }
  948. static String& StringAddAssignBool(bool value, String& str)
  949. {
  950. str += String(value);
  951. return str;
  952. }
  953. static String StringAddBool(bool value, const String& str)
  954. {
  955. return str + String(value);
  956. }
  957. static String StringAddBoolReverse(bool value, const String& str)
  958. {
  959. return String(value) + str;
  960. }
  961. static void StringSetUTF8FromLatin1(const String& src, String& str)
  962. {
  963. str.SetUTF8FromLatin1(src.CString());
  964. }
  965. void RegisterString(asIScriptEngine *engine)
  966. {
  967. engine->RegisterObjectType("String", sizeof(String), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
  968. engine->RegisterStringFactory("String", asFUNCTION(StringFactory), asCALL_CDECL);
  969. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  970. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(const String&in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  971. engine->RegisterObjectBehaviour("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  972. engine->RegisterObjectMethod("String", "String& opAssign(const String&in)", asMETHODPR(String, operator =, (const String&), String&), asCALL_THISCALL);
  973. engine->RegisterObjectMethod("String", "String& opAddAssign(const String&in)", asMETHODPR(String, operator +=, (const String&), String&), asCALL_THISCALL);
  974. engine->RegisterObjectMethod("String", "bool opEquals(const String&in) const", asMETHODPR(String, operator ==, (const String&) const, bool), asCALL_THISCALL);
  975. engine->RegisterObjectMethod("String", "int opCmp(const String&in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  976. engine->RegisterObjectMethod("String", "String opAdd(const String&in) const", asMETHODPR(String, operator +, (const String&) const, String), asCALL_THISCALL);
  977. engine->RegisterObjectMethod("String", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  978. engine->RegisterObjectMethod("String", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  979. engine->RegisterObjectMethod("String", "void Replace(uint8, uint8)", asMETHODPR(String, Replace, (char, char), void), asCALL_THISCALL);
  980. engine->RegisterObjectMethod("String", "void Replace(const String&in, const String&in)", asMETHODPR(String, Replace, (const String&, const String&), void), asCALL_THISCALL);
  981. engine->RegisterObjectMethod("String", "String Replaced(uint8, uint8) const", asMETHODPR(String, Replaced, (char, char) const, String), asCALL_THISCALL);
  982. engine->RegisterObjectMethod("String", "String Replaced(const String&in, const String&in) const", asMETHODPR(String, Replaced, (const String&, const String&) const, String), asCALL_THISCALL);
  983. engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
  984. engine->RegisterObjectMethod("String", "int Find(const String&in, uint start = 0) const", asMETHODPR(String, Find, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  985. engine->RegisterObjectMethod("String", "int Find(uint8, uint start = 0) const", asMETHODPR(String, Find, (char, unsigned) const, unsigned), asCALL_THISCALL);
  986. engine->RegisterObjectMethod("String", "int FindLast(const String&in, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  987. engine->RegisterObjectMethod("String", "int FindLast(uint8, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (char, unsigned) const, unsigned), asCALL_THISCALL);
  988. engine->RegisterObjectMethod("String", "bool StartsWith(const String&in) const", asMETHOD(String, StartsWith), asCALL_THISCALL);
  989. engine->RegisterObjectMethod("String", "bool EndsWith(const String&in) const", asMETHOD(String, EndsWith), asCALL_THISCALL);
  990. engine->RegisterObjectMethod("String", "String Substring(uint) const", asMETHODPR(String, Substring, (unsigned) const, String), asCALL_THISCALL);
  991. engine->RegisterObjectMethod("String", "String Substring(uint, uint) const", asMETHODPR(String, Substring, (unsigned, unsigned) const, String), asCALL_THISCALL);
  992. engine->RegisterObjectMethod("String", "String ToUpper() const", asMETHOD(String, ToUpper), asCALL_THISCALL);
  993. engine->RegisterObjectMethod("String", "String ToLower() const", asMETHOD(String, ToLower), asCALL_THISCALL);
  994. engine->RegisterObjectMethod("String", "String Trimmed() const", asMETHOD(String, Trimmed), asCALL_THISCALL);
  995. engine->RegisterObjectMethod("String", "void SetUTF8FromLatin1(const String& in)", asFUNCTION(StringSetUTF8FromLatin1), asCALL_CDECL_OBJLAST);
  996. engine->RegisterObjectMethod("String", "uint get_utf8Length() const", asMETHOD(String, LengthUTF8), asCALL_THISCALL);
  997. engine->RegisterObjectMethod("String", "uint ByteOffsetUTF8(uint) const", asMETHOD(String, ByteOffsetUTF8), asCALL_THISCALL);
  998. engine->RegisterObjectMethod("String", "uint NextUTF8Char(uint&) const", asMETHOD(String, NextUTF8Char), asCALL_THISCALL);
  999. engine->RegisterObjectMethod("String", "uint AtUTF8(uint) const", asMETHOD(String, AtUTF8), asCALL_THISCALL);
  1000. engine->RegisterObjectMethod("String", "void ReplaceUTF8(uint, uint)", asMETHOD(String, ReplaceUTF8), asCALL_THISCALL);
  1001. engine->RegisterObjectMethod("String", "void AppendUTF8(uint)", asMETHOD(String, AppendUTF8), asCALL_THISCALL);
  1002. engine->RegisterObjectMethod("String", "String SubstringUTF8(uint) const", asMETHODPR(String, SubstringUTF8, (unsigned) const, String), asCALL_THISCALL);
  1003. engine->RegisterObjectMethod("String", "String SubstringUTF8(uint, uint) const", asMETHODPR(String, SubstringUTF8, (unsigned, unsigned) const, String), asCALL_THISCALL);
  1004. engine->RegisterObjectMethod("String", "uint get_length() const", asMETHOD(String, Length), asCALL_THISCALL);
  1005. engine->RegisterObjectMethod("String", "bool get_empty() const", asMETHOD(String, Empty), asCALL_THISCALL);
  1006. engine->RegisterObjectMethod("String", "int Compare(const String&in, bool caseSensitive = true) const", asMETHODPR(String, Compare, (const String&, bool) const, int), asCALL_THISCALL);
  1007. // Register automatic conversion functions for convenience
  1008. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  1009. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  1010. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  1011. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  1012. engine->RegisterObjectMethod("String", "String& opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  1013. engine->RegisterObjectMethod("String", "String& opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  1014. engine->RegisterObjectMethod("String", "String opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  1015. engine->RegisterObjectMethod("String", "String opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  1016. engine->RegisterObjectMethod("String", "String& opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  1017. engine->RegisterObjectMethod("String", "String& opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  1018. engine->RegisterObjectMethod("String", "String opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  1019. engine->RegisterObjectMethod("String", "String opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  1020. engine->RegisterObjectMethod("String", "String& opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  1021. engine->RegisterObjectMethod("String", "String& opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  1022. engine->RegisterObjectMethod("String", "String opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  1023. engine->RegisterObjectMethod("String", "String opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  1024. engine->RegisterObjectMethod("String", "String& opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  1025. engine->RegisterObjectMethod("String", "String& opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  1026. engine->RegisterObjectMethod("String", "String opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  1027. engine->RegisterObjectMethod("String", "String opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  1028. }