Addons.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "StringUtils.h"
  26. #include <cstring>
  27. // Adapted from Angelscript's scriptarray & scriptstdstring add-ons, but with garbage collection disabled
  28. /// Script array buffer
  29. struct SArrayBuffer
  30. {
  31. asDWORD numElements;
  32. asBYTE data[1];
  33. };
  34. static CScriptArray* ScriptArrayFactory2(asIObjectType* ot, asUINT length)
  35. {
  36. CScriptArray* a = new CScriptArray(length, ot);
  37. // It's possible the constructor raised a script exception, in which case we
  38. // need to free the memory and return null instead, else we get a memory leak.
  39. asIScriptContext* context = asGetActiveContext();
  40. if (context && context->GetState() == asEXECUTION_EXCEPTION)
  41. {
  42. delete a;
  43. return 0;
  44. }
  45. return a;
  46. }
  47. static CScriptArray* ScriptArrayFactoryDefVal(asIObjectType* ot, asUINT length, void* defVal)
  48. {
  49. CScriptArray* a = new CScriptArray(length, defVal, ot);
  50. // It's possible the constructor raised a script exception, in which case we
  51. // need to free the memory and return null instead, else we get a memory leak.
  52. asIScriptContext* context = asGetActiveContext();
  53. if (context && context->GetState() == asEXECUTION_EXCEPTION)
  54. {
  55. delete a;
  56. return 0;
  57. }
  58. return a;
  59. }
  60. static CScriptArray* ScriptArrayFactory(asIObjectType* ot)
  61. {
  62. return ScriptArrayFactory2(ot, 0);
  63. }
  64. // This optional callback is called when the template type is first used by the compiler.
  65. // It allows the application to validate if the template can be instanciated for the requested
  66. // subtype at compile time, instead of at runtime.
  67. static bool ScriptArrayTemplateCallback(asIObjectType* ot)
  68. {
  69. // Make sure the subtype can be instanciated with a default factory/constructor,
  70. // otherwise we won't be able to instanciate the elements. Script classes always
  71. // have default factories, so we don't have to worry about those.
  72. int typeId = ot->GetSubTypeId();
  73. if ((typeId & asTYPEID_MASK_OBJECT) && !(typeId & asTYPEID_OBJHANDLE) && !(typeId & asTYPEID_SCRIPTOBJECT))
  74. {
  75. asIObjectType *subtype = ot->GetEngine()->GetObjectTypeById(typeId);
  76. asDWORD flags = subtype->GetFlags();
  77. if ((flags & asOBJ_VALUE) && !(flags & asOBJ_POD))
  78. {
  79. // Verify that there is a default constructor
  80. for (int n = 0; n < subtype->GetBehaviourCount(); ++n)
  81. {
  82. asEBehaviours beh;
  83. int funcId = subtype->GetBehaviourByIndex(n, &beh);
  84. if (beh != asBEHAVE_CONSTRUCT)
  85. continue;
  86. asIScriptFunction* func = ot->GetEngine()->GetFunctionDescriptorById(funcId);
  87. if (func->GetParamCount() == 0)
  88. {
  89. // Found the default constructor
  90. return true;
  91. }
  92. }
  93. // There is no default constructor
  94. return false;
  95. }
  96. else if ((flags & asOBJ_REF))
  97. {
  98. // Verify that there is a default factory
  99. for (int n = 0; n < subtype->GetFactoryCount(); ++n)
  100. {
  101. int funcId = subtype->GetFactoryIdByIndex(n);
  102. asIScriptFunction* func = ot->GetEngine()->GetFunctionDescriptorById(funcId);
  103. if (func->GetParamCount() == 0)
  104. {
  105. // Found the default factory
  106. return true;
  107. }
  108. }
  109. // No default factory
  110. return false;
  111. }
  112. }
  113. // The type is ok
  114. return true;
  115. }
  116. CScriptArray& CScriptArray::operator = (const CScriptArray &other)
  117. {
  118. // Only perform the copy if the array types are the same
  119. if (&other != this && other.GetArrayObjectType() == GetArrayObjectType())
  120. {
  121. if (buffer)
  122. {
  123. DeleteBuffer(buffer);
  124. buffer = 0;
  125. }
  126. // Copy all elements from the other array
  127. CreateBuffer(&buffer, other.buffer->numElements);
  128. CopyBuffer(buffer, other.buffer);
  129. }
  130. return *this;
  131. }
  132. CScriptArray::CScriptArray(asUINT length, asIObjectType* ot)
  133. {
  134. refCount = 1;
  135. gcFlag = false;
  136. objType = ot;
  137. objType->AddRef();
  138. buffer = 0;
  139. // Determine element size
  140. // TODO: Should probably store the template sub type id as well
  141. int typeId = objType->GetSubTypeId();
  142. if (typeId & asTYPEID_MASK_OBJECT)
  143. elementSize = sizeof(asPWORD);
  144. else
  145. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(typeId);
  146. isArrayOfHandles = typeId & asTYPEID_OBJHANDLE ? true : false;
  147. // Make sure the array size isn't too large for us to handle
  148. if (!CheckMaxSize(length))
  149. {
  150. // Don't continue with the initialization
  151. return;
  152. }
  153. CreateBuffer(&buffer, length);
  154. }
  155. CScriptArray::CScriptArray(asUINT length, void *defVal, asIObjectType *ot)
  156. {
  157. refCount = 1;
  158. gcFlag = false;
  159. objType = ot;
  160. objType->AddRef();
  161. buffer = 0;
  162. // Determine element size
  163. // TODO: Should probably store the template sub type id as well
  164. int typeId = objType->GetSubTypeId();
  165. if (typeId & asTYPEID_MASK_OBJECT)
  166. elementSize = sizeof(asPWORD);
  167. else
  168. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(typeId);
  169. isArrayOfHandles = typeId & asTYPEID_OBJHANDLE ? true : false;
  170. // Make sure the array size isn't too large for us to handle
  171. if (!CheckMaxSize(length))
  172. {
  173. // Don't continue with the initialization
  174. return;
  175. }
  176. CreateBuffer(&buffer, length);
  177. // Initialize the elements with the default value
  178. for (asUINT n = 0; n < GetSize(); ++n)
  179. SetValue(n, defVal);
  180. }
  181. // Internal
  182. void CScriptArray::SetValue(asUINT index, void *value)
  183. {
  184. int typeId = objType->GetSubTypeId();
  185. if ((typeId & ~0x03FFFFFF) && !(typeId & asTYPEID_OBJHANDLE))
  186. objType->GetEngine()->CopyScriptObject(At(index), value, typeId);
  187. else if (typeId & asTYPEID_OBJHANDLE)
  188. {
  189. *(void**)At(index) = *(void**)value;
  190. objType->GetEngine()->AddRefScriptObject(*(void**)value, typeId);
  191. }
  192. else if (typeId == asTYPEID_BOOL || typeId == asTYPEID_INT8 || typeId == asTYPEID_UINT8)
  193. *(char*)At(index) = *(char*)value;
  194. else if (typeId == asTYPEID_INT16 || typeId == asTYPEID_UINT16)
  195. *(short*)At(index) = *(short*)value;
  196. else if (typeId == asTYPEID_INT32 || typeId == asTYPEID_UINT32 || typeId == asTYPEID_FLOAT)
  197. *(int*)At(index) = *(int*)value;
  198. else if (typeId == asTYPEID_INT64 || typeId == asTYPEID_UINT64 || typeId == asTYPEID_DOUBLE)
  199. *(double*)At(index) = *(double*)value;
  200. }
  201. CScriptArray::~CScriptArray()
  202. {
  203. if (buffer)
  204. {
  205. DeleteBuffer(buffer);
  206. buffer = 0;
  207. }
  208. if (objType)
  209. objType->Release();
  210. }
  211. asUINT CScriptArray::GetSize() const
  212. {
  213. return buffer->numElements;
  214. }
  215. bool CScriptArray::IsEmpty() const
  216. {
  217. return buffer->numElements == 0;
  218. }
  219. void CScriptArray::Resize(asUINT numElements)
  220. {
  221. if (numElements & 0x80000000)
  222. {
  223. CheckMaxSize(numElements);
  224. return;
  225. }
  226. Resize((int)numElements - (int)buffer->numElements, -1);
  227. }
  228. // Internal
  229. void CScriptArray::Resize(int delta, asUINT at)
  230. {
  231. if (delta < 0)
  232. {
  233. if (-delta > (int)buffer->numElements)
  234. delta = -(int)buffer->numElements;
  235. if (at > buffer->numElements + delta)
  236. at = buffer->numElements + delta;
  237. }
  238. else if (delta > 0)
  239. {
  240. // Make sure the array size isn't too large for us to handle
  241. if (delta > 0 && !CheckMaxSize(buffer->numElements + delta))
  242. return;
  243. if (at > buffer->numElements)
  244. at = buffer->numElements;
  245. }
  246. if (delta == 0)
  247. return;
  248. // Allocate memory for the buffer
  249. SArrayBuffer *newBuffer;
  250. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  251. newBuffer->numElements = buffer->numElements + delta;
  252. int c = newBuffer->numElements > buffer->numElements ? buffer->numElements : newBuffer->numElements;
  253. memcpy(newBuffer->data, buffer->data, at*elementSize);
  254. if (delta > 0 && at < buffer->numElements)
  255. memcpy(newBuffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements-at)*elementSize);
  256. else if (delta < 0 && at < buffer->numElements)
  257. memcpy(newBuffer->data + at*elementSize, buffer->data + (at-delta)*elementSize, (buffer->numElements-at+delta)*elementSize);
  258. int typeId = objType->GetSubTypeId();
  259. if (typeId & asTYPEID_MASK_OBJECT)
  260. {
  261. if (delta > 0)
  262. Construct(newBuffer, at, at+delta);
  263. else if (delta < 0)
  264. Destruct(buffer, at, at-delta);
  265. }
  266. // Release the old buffer
  267. delete[] (asBYTE*)buffer;
  268. buffer = newBuffer;
  269. }
  270. // internal
  271. bool CScriptArray::CheckMaxSize(asUINT numElements)
  272. {
  273. // This code makes sure the size of the buffer that is allocated
  274. // for the array doesn't overflow and becomes smaller than requested
  275. asUINT maxSize = 0xFFFFFFFFul - sizeof(SArrayBuffer) + 1;
  276. if (objType->GetSubTypeId() & asTYPEID_MASK_OBJECT)
  277. maxSize /= sizeof(void*);
  278. else
  279. maxSize /= elementSize;
  280. if (numElements > maxSize)
  281. {
  282. asIScriptContext* context = asGetActiveContext();
  283. if (context)
  284. context->SetException("Too large array size");
  285. return false;
  286. }
  287. // OK
  288. return true;
  289. }
  290. asIObjectType *CScriptArray::GetArrayObjectType() const
  291. {
  292. return objType;
  293. }
  294. int CScriptArray::GetArrayTypeId() const
  295. {
  296. return objType->GetTypeId();
  297. }
  298. int CScriptArray::GetElementTypeId() const
  299. {
  300. return objType->GetSubTypeId();
  301. }
  302. void CScriptArray::InsertAt(asUINT index, void *value)
  303. {
  304. if (index > buffer->numElements)
  305. {
  306. asIScriptContext* context = asGetActiveContext();
  307. if (context)
  308. context->SetException("Index out of bounds");
  309. return;
  310. }
  311. // Make room for the new element
  312. Resize(1, index);
  313. // Set the value of the new element
  314. SetValue(index, value);
  315. }
  316. void CScriptArray::InsertLast(void *value)
  317. {
  318. InsertAt(buffer->numElements, value);
  319. }
  320. void CScriptArray::RemoveAt(asUINT index)
  321. {
  322. if (index >= buffer->numElements)
  323. {
  324. asIScriptContext* context = asGetActiveContext();
  325. if (context)
  326. context->SetException("Index out of bounds");
  327. return;
  328. }
  329. // Remove the element
  330. Resize(-1, index);
  331. }
  332. void CScriptArray::RemoveLast()
  333. {
  334. RemoveAt(buffer->numElements-1);
  335. }
  336. // Return a pointer to the array element. Returns 0 if the index is out of bounds
  337. void *CScriptArray::At(asUINT index)
  338. {
  339. if (index >= buffer->numElements)
  340. {
  341. asIScriptContext* context = asGetActiveContext();
  342. if (context)
  343. context->SetException("Index out of bounds");
  344. return 0;
  345. }
  346. else
  347. {
  348. int typeId = objType->GetSubTypeId();
  349. if ((typeId & asTYPEID_MASK_OBJECT) && !isArrayOfHandles)
  350. return (void*)((size_t*)buffer->data)[index];
  351. else
  352. return buffer->data + elementSize*index;
  353. }
  354. }
  355. // internal
  356. void CScriptArray::CreateBuffer(SArrayBuffer **buf, asUINT numElements)
  357. {
  358. int typeId = objType->GetSubTypeId();
  359. if (typeId & asTYPEID_MASK_OBJECT)
  360. {
  361. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  362. (*buf)->numElements = numElements;
  363. }
  364. else
  365. {
  366. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  367. (*buf)->numElements = numElements;
  368. }
  369. Construct(*buf, 0, numElements);
  370. }
  371. // internal
  372. void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
  373. {
  374. Destruct(buf, 0, buf->numElements);
  375. // Free the buffer
  376. delete[] (asBYTE*)buf;
  377. }
  378. // internal
  379. void CScriptArray::Construct(SArrayBuffer *buf, asUINT start, asUINT end)
  380. {
  381. int typeId = objType->GetSubTypeId();
  382. if (isArrayOfHandles)
  383. {
  384. // Set all object handles to null
  385. void* d = (void*)(buf->data + start * sizeof(void*));
  386. memset(d, 0, (end-start)*sizeof(void*));
  387. }
  388. else if (typeId & asTYPEID_MASK_OBJECT)
  389. {
  390. void** max = (void**)(buf->data + end * sizeof(void*));
  391. void** d = (void**)(buf->data + start * sizeof(void*));
  392. asIScriptEngine* engine = objType->GetEngine();
  393. for (; d < max; d++)
  394. *d = (void*)engine->CreateScriptObject(typeId);
  395. }
  396. }
  397. // internal
  398. void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
  399. {
  400. int typeId = objType->GetSubTypeId();
  401. if (typeId & asTYPEID_MASK_OBJECT)
  402. {
  403. asIScriptEngine* engine = objType->GetEngine();
  404. void** max = (void**)(buf->data + end * sizeof(void*));
  405. void** d = (void**)(buf->data + start * sizeof(void*));
  406. for(; d < max; d++ )
  407. {
  408. if( *d )
  409. engine->ReleaseScriptObject(*d, typeId);
  410. }
  411. }
  412. }
  413. // internal
  414. void CScriptArray::CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src)
  415. {
  416. asIScriptEngine* engine = objType->GetEngine();
  417. if(isArrayOfHandles)
  418. {
  419. // Copy the references and increase the reference counters
  420. if (dst->numElements > 0 && src->numElements > 0)
  421. {
  422. int typeId = objType->GetSubTypeId();
  423. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  424. void** max = (void**)(dst->data + count * sizeof(void*));
  425. void** d = (void**)dst->data;
  426. void** s = (void**)src->data;
  427. for (; d < max; d++, s++)
  428. {
  429. *d = *s;
  430. if (*d)
  431. engine->AddRefScriptObject(*d, typeId);
  432. }
  433. }
  434. }
  435. else
  436. {
  437. int typeId = objType->GetSubTypeId();
  438. if (dst->numElements > 0 && src->numElements > 0)
  439. {
  440. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  441. if (typeId & asTYPEID_MASK_OBJECT)
  442. {
  443. // Call the assignment operator on all of the objects
  444. void** max = (void**)(dst->data + count * sizeof(void*));
  445. void** d = (void**)dst->data;
  446. void** s = (void**)src->data;
  447. for(; d < max; d++, s++)
  448. engine->CopyScriptObject(*d, *s, typeId);
  449. }
  450. else
  451. {
  452. // Primitives are copied byte for byte
  453. memcpy(dst->data, src->data, count*elementSize);
  454. }
  455. }
  456. }
  457. }
  458. void CScriptArray::AddRef() const
  459. {
  460. // Clear the GC flag then increase the counter
  461. gcFlag = false;
  462. refCount++;
  463. }
  464. void CScriptArray::Release() const
  465. {
  466. // Now do the actual releasing (clearing the flag set by GC)
  467. gcFlag = false;
  468. if (--refCount == 0)
  469. delete this;
  470. }
  471. void RegisterArray(asIScriptEngine* engine)
  472. {
  473. engine->RegisterObjectType("Array<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE);
  474. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(ScriptArrayTemplateCallback), asCALL_CDECL);
  475. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in)", asFUNCTIONPR(ScriptArrayFactory, (asIObjectType*), CScriptArray*), asCALL_CDECL);
  476. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  477. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint, const T& in)", asFUNCTIONPR(ScriptArrayFactoryDefVal, (asIObjectType*, asUINT, void *), CScriptArray*), asCALL_CDECL);
  478. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_LIST_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  479. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptArray,AddRef), asCALL_THISCALL);
  480. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL);
  481. engine->RegisterObjectMethod("Array<T>", "T& opIndex(uint)", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  482. engine->RegisterObjectMethod("Array<T>", "const T& opIndex(uint) const", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  483. engine->RegisterObjectMethod("Array<T>", "Array<T> &opAssign(const Array<T>& in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL);
  484. engine->RegisterObjectMethod("Array<T>", "void Insert(uint, const T& in)", asMETHOD(CScriptArray, InsertAt), asCALL_THISCALL);
  485. engine->RegisterObjectMethod("Array<T>", "void Remove(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL);
  486. engine->RegisterObjectMethod("Array<T>", "void Push(const T& in)", asMETHOD(CScriptArray, InsertLast), asCALL_THISCALL);
  487. engine->RegisterObjectMethod("Array<T>", "void Pop()", asMETHOD(CScriptArray, RemoveLast), asCALL_THISCALL);
  488. engine->RegisterObjectMethod("Array<T>", "void Resize(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  489. engine->RegisterObjectMethod("Array<T>", "uint get_length() const", asMETHOD(CScriptArray, GetSize), asCALL_THISCALL);
  490. engine->RegisterObjectMethod("Array<T>", "bool get_empty() const", asMETHOD(CScriptArray, IsEmpty), asCALL_THISCALL);
  491. engine->RegisterDefaultArrayType("Array<T>");
  492. }
  493. // Adapted from AngelScript's scriptstdstring add-on
  494. static String StringFactory(asUINT length, const char* s)
  495. {
  496. return String(s, length);
  497. }
  498. static void ConstructString(String* ptr)
  499. {
  500. new(ptr) String();
  501. }
  502. static void ConstructStringCopy(const String& str, String* ptr)
  503. {
  504. new(ptr) String(str);
  505. }
  506. static void DestructString(String* ptr)
  507. {
  508. ptr->~String();
  509. }
  510. static char* StringCharAt(unsigned int i, String& str)
  511. {
  512. if (i >= str.Length())
  513. {
  514. asIScriptContext* context = asGetActiveContext();
  515. if (context)
  516. context->SetException("Index out of bounds");
  517. return 0;
  518. }
  519. return &str[i];
  520. }
  521. static int StringCmp(const String& lhs, const String& rhs)
  522. {
  523. int cmp = 0;
  524. if (lhs < rhs)
  525. cmp = -1;
  526. else if (lhs > rhs)
  527. cmp = 1;
  528. return cmp;
  529. }
  530. void StringResize(unsigned newSize, String& str)
  531. {
  532. unsigned oldSize = str.Length();
  533. str.Resize(newSize);
  534. for (unsigned i = oldSize; i < newSize; ++i)
  535. str[i] = ' ';
  536. }
  537. static void ConstructStringInt(int value, String* ptr)
  538. {
  539. new(ptr) String(value);
  540. }
  541. static void ConstructStringUInt(unsigned value, String* ptr)
  542. {
  543. new(ptr) String(value);
  544. }
  545. static void ConstructStringFloat(float value, String* ptr)
  546. {
  547. new(ptr) String(value);
  548. }
  549. static void ConstructStringBool(bool value, String* ptr)
  550. {
  551. new(ptr) String(value);
  552. }
  553. static String& StringAssignInt(int value, String& str)
  554. {
  555. str = String(value);
  556. return str;
  557. }
  558. static String& StringAddAssignInt(int value, String& str)
  559. {
  560. str += String(value);
  561. return str;
  562. }
  563. static String StringAddInt(int value, const String& str)
  564. {
  565. return str + String(value);
  566. }
  567. static String StringAddIntReverse(int value, const String& str)
  568. {
  569. return String(value) + str;
  570. }
  571. static String& StringAssignUInt(unsigned value, String& str)
  572. {
  573. str = String(value);
  574. return str;
  575. }
  576. static String& StringAddAssignUInt(unsigned value, String& str)
  577. {
  578. str += String(value);
  579. return str;
  580. }
  581. static String StringAddUInt(unsigned value, const String& str)
  582. {
  583. return str + String(value);
  584. }
  585. static String StringAddUIntReverse(unsigned value, const String& str)
  586. {
  587. return String(value) + str;
  588. }
  589. static String& StringAssignFloat(float value, String& str)
  590. {
  591. str = String(value);
  592. return str;
  593. }
  594. static String& StringAddAssignFloat(float value, String& str)
  595. {
  596. str += String(value);
  597. return str;
  598. }
  599. static String StringAddFloat(float value, const String& str)
  600. {
  601. return str + String(value);
  602. }
  603. static String StringAddFloatReverse(float value, const String& str)
  604. {
  605. return String(value) + str;
  606. }
  607. static String& StringAssignBool(bool value, String& str)
  608. {
  609. str = String(value);
  610. return str;
  611. }
  612. static String& StringAddAssignBool(bool value, String& str)
  613. {
  614. str += String(value);
  615. return str;
  616. }
  617. static String StringAddBool(bool value, const String& str)
  618. {
  619. return str + String(value);
  620. }
  621. static String StringAddBoolReverse(bool value, const String& str)
  622. {
  623. return String(value) + str;
  624. }
  625. void RegisterString(asIScriptEngine *engine)
  626. {
  627. engine->RegisterObjectType("String", sizeof(String), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  628. engine->RegisterStringFactory("String", asFUNCTION(StringFactory), asCALL_CDECL);
  629. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  630. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(const String& in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  631. engine->RegisterObjectBehaviour("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  632. engine->RegisterObjectMethod("String", "String &opAssign(const String& in)", asMETHODPR(String, operator =, (const String&), String&), asCALL_THISCALL);
  633. engine->RegisterObjectMethod("String", "String &opAddAssign(const String& in)", asMETHODPR(String, operator +=, (const String&), String&), asCALL_THISCALL);
  634. engine->RegisterObjectMethod("String", "bool opEquals(const String& in) const", asMETHODPR(String, operator ==, (const String&) const, bool), asCALL_THISCALL);
  635. engine->RegisterObjectMethod("String", "int opCmp(const String& in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  636. engine->RegisterObjectMethod("String", "String opAdd(const String& in) const", asMETHODPR(String, operator +, (const String&) const, String), asCALL_THISCALL);
  637. engine->RegisterObjectMethod("String", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  638. engine->RegisterObjectMethod("String", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  639. engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
  640. engine->RegisterObjectMethod("String", "int Find(const String& in, uint start = 0) const", asMETHODPR(String, Find, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  641. engine->RegisterObjectMethod("String", "int Find(uint8, uint start = 0) const", asMETHODPR(String, Find, (char, unsigned) const, unsigned), asCALL_THISCALL);
  642. engine->RegisterObjectMethod("String", "int FindLast(const String& in) const", asMETHODPR(String, FindLast, (const String&) const, unsigned), asCALL_THISCALL);
  643. engine->RegisterObjectMethod("String", "int FindLast(uint8) const", asMETHODPR(String, FindLast, (char) const, unsigned), asCALL_THISCALL);
  644. engine->RegisterObjectMethod("String", "String Replace(uint8, uint8) const", asMETHODPR(String, Replace, (char, char) const, String), asCALL_THISCALL);
  645. engine->RegisterObjectMethod("String", "String Replace(const String&in, const String&in) const", asMETHODPR(String, Replace, (const String&, const String&) const, String), asCALL_THISCALL);
  646. engine->RegisterObjectMethod("String", "String Substring(uint) const", asMETHODPR(String, Substring, (unsigned) const, String), asCALL_THISCALL);
  647. engine->RegisterObjectMethod("String", "String Substring(uint, uint) const", asMETHODPR(String, Substring, (unsigned, unsigned) const, String), asCALL_THISCALL);
  648. engine->RegisterObjectMethod("String", "String ToUpper() const", asMETHOD(String, ToUpper), asCALL_THISCALL);
  649. engine->RegisterObjectMethod("String", "String ToLower() const", asMETHOD(String, ToLower), asCALL_THISCALL);
  650. engine->RegisterObjectMethod("String", "String Trim() const", asMETHOD(String, Trim), asCALL_THISCALL);
  651. engine->RegisterObjectMethod("String", "uint get_length() const", asMETHOD(String, Length), asCALL_THISCALL);
  652. engine->RegisterObjectMethod("String", "bool get_empty() const", asMETHOD(String, Empty), asCALL_THISCALL);
  653. // Register automatic conversion functions for convenience
  654. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  655. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  656. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  657. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  658. engine->RegisterObjectMethod("String", "String &opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  659. engine->RegisterObjectMethod("String", "String &opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  660. engine->RegisterObjectMethod("String", "String opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  661. engine->RegisterObjectMethod("String", "String opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  662. engine->RegisterObjectMethod("String", "String &opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  663. engine->RegisterObjectMethod("String", "String &opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  664. engine->RegisterObjectMethod("String", "String opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  665. engine->RegisterObjectMethod("String", "String opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  666. engine->RegisterObjectMethod("String", "String &opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  667. engine->RegisterObjectMethod("String", "String &opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  668. engine->RegisterObjectMethod("String", "String opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  669. engine->RegisterObjectMethod("String", "String opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  670. engine->RegisterObjectMethod("String", "String &opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  671. engine->RegisterObjectMethod("String", "String &opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  672. engine->RegisterObjectMethod("String", "String opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  673. engine->RegisterObjectMethod("String", "String opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  674. }