Addons.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 "StringBase.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 (unsigned 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()->GetFunctionById(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 (unsigned n = 0; n < subtype->GetFactoryCount(); ++n)
  100. {
  101. int funcId = subtype->GetFactoryIdByIndex(n);
  102. asIScriptFunction* func = ot->GetEngine()->GetFunctionById(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. void CScriptArray::Clear()
  229. {
  230. Resize(0);
  231. }
  232. // Internal
  233. void CScriptArray::Resize(int delta, asUINT at)
  234. {
  235. if (delta < 0)
  236. {
  237. if (-delta > (int)buffer->numElements)
  238. delta = -(int)buffer->numElements;
  239. if (at > buffer->numElements + delta)
  240. at = buffer->numElements + delta;
  241. }
  242. else if (delta > 0)
  243. {
  244. // Make sure the array size isn't too large for us to handle
  245. if (delta > 0 && !CheckMaxSize(buffer->numElements + delta))
  246. return;
  247. if (at > buffer->numElements)
  248. at = buffer->numElements;
  249. }
  250. if (delta == 0)
  251. return;
  252. // Allocate memory for the buffer
  253. SArrayBuffer *newBuffer;
  254. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  255. newBuffer->numElements = buffer->numElements + delta;
  256. int c = newBuffer->numElements > buffer->numElements ? buffer->numElements : newBuffer->numElements;
  257. memcpy(newBuffer->data, buffer->data, at*elementSize);
  258. if (delta > 0 && at < buffer->numElements)
  259. memcpy(newBuffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements-at)*elementSize);
  260. else if (delta < 0 && at < buffer->numElements)
  261. memcpy(newBuffer->data + at*elementSize, buffer->data + (at-delta)*elementSize, (buffer->numElements-at+delta)*elementSize);
  262. int typeId = objType->GetSubTypeId();
  263. if (typeId & 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 (objType->GetSubTypeId() & asTYPEID_MASK_OBJECT)
  281. maxSize /= sizeof(void*);
  282. else
  283. maxSize /= elementSize;
  284. if (numElements > maxSize)
  285. {
  286. asIScriptContext* context = asGetActiveContext();
  287. if (context)
  288. context->SetException("Too large array size");
  289. return false;
  290. }
  291. // OK
  292. return true;
  293. }
  294. asIObjectType *CScriptArray::GetArrayObjectType() const
  295. {
  296. return objType;
  297. }
  298. int CScriptArray::GetArrayTypeId() const
  299. {
  300. return objType->GetTypeId();
  301. }
  302. int CScriptArray::GetElementTypeId() const
  303. {
  304. return objType->GetSubTypeId();
  305. }
  306. void CScriptArray::InsertAt(asUINT index, void *value)
  307. {
  308. if (index > buffer->numElements)
  309. {
  310. asIScriptContext* context = asGetActiveContext();
  311. if (context)
  312. context->SetException("Index out of bounds");
  313. return;
  314. }
  315. // Make room for the new element
  316. Resize(1, index);
  317. // Set the value of the new element
  318. SetValue(index, value);
  319. }
  320. void CScriptArray::InsertLast(void *value)
  321. {
  322. InsertAt(buffer->numElements, value);
  323. }
  324. void CScriptArray::RemoveAt(asUINT index)
  325. {
  326. if (index >= buffer->numElements)
  327. {
  328. asIScriptContext* context = asGetActiveContext();
  329. if (context)
  330. context->SetException("Index out of bounds");
  331. return;
  332. }
  333. // Remove the element
  334. Resize(-1, index);
  335. }
  336. void CScriptArray::RemoveLast()
  337. {
  338. RemoveAt(buffer->numElements-1);
  339. }
  340. // Return a pointer to the array element. Returns 0 if the index is out of bounds
  341. void *CScriptArray::At(asUINT index)
  342. {
  343. if (index >= buffer->numElements)
  344. {
  345. asIScriptContext* context = asGetActiveContext();
  346. if (context)
  347. context->SetException("Index out of bounds");
  348. return 0;
  349. }
  350. else
  351. {
  352. int typeId = objType->GetSubTypeId();
  353. if ((typeId & asTYPEID_MASK_OBJECT) && !isArrayOfHandles)
  354. return (void*)((size_t*)buffer->data)[index];
  355. else
  356. return buffer->data + elementSize*index;
  357. }
  358. }
  359. // internal
  360. void CScriptArray::CreateBuffer(SArrayBuffer **buf, asUINT numElements)
  361. {
  362. int typeId = objType->GetSubTypeId();
  363. if (typeId & asTYPEID_MASK_OBJECT)
  364. {
  365. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  366. (*buf)->numElements = numElements;
  367. }
  368. else
  369. {
  370. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  371. (*buf)->numElements = numElements;
  372. }
  373. Construct(*buf, 0, numElements);
  374. }
  375. // internal
  376. void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
  377. {
  378. Destruct(buf, 0, buf->numElements);
  379. // Free the buffer
  380. delete[] (asBYTE*)buf;
  381. }
  382. // internal
  383. void CScriptArray::Construct(SArrayBuffer *buf, asUINT start, asUINT end)
  384. {
  385. int typeId = objType->GetSubTypeId();
  386. if (isArrayOfHandles)
  387. {
  388. // Set all object handles to null
  389. void* d = (void*)(buf->data + start * sizeof(void*));
  390. memset(d, 0, (end-start)*sizeof(void*));
  391. }
  392. else if (typeId & asTYPEID_MASK_OBJECT)
  393. {
  394. void** max = (void**)(buf->data + end * sizeof(void*));
  395. void** d = (void**)(buf->data + start * sizeof(void*));
  396. asIScriptEngine* engine = objType->GetEngine();
  397. for (; d < max; d++)
  398. *d = (void*)engine->CreateScriptObject(typeId);
  399. }
  400. }
  401. // internal
  402. void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
  403. {
  404. int typeId = objType->GetSubTypeId();
  405. if (typeId & asTYPEID_MASK_OBJECT)
  406. {
  407. asIScriptEngine* engine = objType->GetEngine();
  408. void** max = (void**)(buf->data + end * sizeof(void*));
  409. void** d = (void**)(buf->data + start * sizeof(void*));
  410. for(; d < max; d++ )
  411. {
  412. if( *d )
  413. engine->ReleaseScriptObject(*d, typeId);
  414. }
  415. }
  416. }
  417. // internal
  418. void CScriptArray::CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src)
  419. {
  420. asIScriptEngine* engine = objType->GetEngine();
  421. if(isArrayOfHandles)
  422. {
  423. // Copy the references and increase the reference counters
  424. if (dst->numElements > 0 && src->numElements > 0)
  425. {
  426. int typeId = objType->GetSubTypeId();
  427. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  428. void** max = (void**)(dst->data + count * sizeof(void*));
  429. void** d = (void**)dst->data;
  430. void** s = (void**)src->data;
  431. for (; d < max; d++, s++)
  432. {
  433. *d = *s;
  434. if (*d)
  435. engine->AddRefScriptObject(*d, typeId);
  436. }
  437. }
  438. }
  439. else
  440. {
  441. int typeId = objType->GetSubTypeId();
  442. if (dst->numElements > 0 && src->numElements > 0)
  443. {
  444. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  445. if (typeId & asTYPEID_MASK_OBJECT)
  446. {
  447. // Call the assignment operator on all of the objects
  448. void** max = (void**)(dst->data + count * sizeof(void*));
  449. void** d = (void**)dst->data;
  450. void** s = (void**)src->data;
  451. for(; d < max; d++, s++)
  452. engine->CopyScriptObject(*d, *s, typeId);
  453. }
  454. else
  455. {
  456. // Primitives are copied byte for byte
  457. memcpy(dst->data, src->data, count*elementSize);
  458. }
  459. }
  460. }
  461. }
  462. void CScriptArray::AddRef() const
  463. {
  464. // Clear the GC flag then increase the counter
  465. gcFlag = false;
  466. refCount++;
  467. }
  468. void CScriptArray::Release() const
  469. {
  470. // Now do the actual releasing (clearing the flag set by GC)
  471. gcFlag = false;
  472. if (--refCount == 0)
  473. delete this;
  474. }
  475. void RegisterArray(asIScriptEngine* engine)
  476. {
  477. engine->RegisterObjectType("Array<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE);
  478. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(ScriptArrayTemplateCallback), asCALL_CDECL);
  479. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in)", asFUNCTIONPR(ScriptArrayFactory, (asIObjectType*), CScriptArray*), asCALL_CDECL);
  480. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  481. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint, const T& in)", asFUNCTIONPR(ScriptArrayFactoryDefVal, (asIObjectType*, asUINT, void *), CScriptArray*), asCALL_CDECL);
  482. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_LIST_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  483. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptArray,AddRef), asCALL_THISCALL);
  484. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL);
  485. engine->RegisterObjectMethod("Array<T>", "T& opIndex(uint)", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  486. engine->RegisterObjectMethod("Array<T>", "const T& opIndex(uint) const", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  487. engine->RegisterObjectMethod("Array<T>", "Array<T>& opAssign(const Array<T>& in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL);
  488. engine->RegisterObjectMethod("Array<T>", "void Insert(uint, const T& in)", asMETHOD(CScriptArray, InsertAt), asCALL_THISCALL);
  489. engine->RegisterObjectMethod("Array<T>", "void Erase(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL);
  490. engine->RegisterObjectMethod("Array<T>", "void Push(const T& in)", asMETHOD(CScriptArray, InsertLast), asCALL_THISCALL);
  491. engine->RegisterObjectMethod("Array<T>", "void Pop()", asMETHOD(CScriptArray, RemoveLast), asCALL_THISCALL);
  492. engine->RegisterObjectMethod("Array<T>", "void Resize(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  493. engine->RegisterObjectMethod("Array<T>", "void Clear()", asMETHOD(CScriptArray, Clear), asCALL_THISCALL);
  494. engine->RegisterObjectMethod("Array<T>", "uint get_length() const", asMETHOD(CScriptArray, GetSize), asCALL_THISCALL);
  495. engine->RegisterObjectMethod("Array<T>", "bool get_empty() const", asMETHOD(CScriptArray, IsEmpty), asCALL_THISCALL);
  496. engine->RegisterDefaultArrayType("Array<T>");
  497. }
  498. static String StringFactory(asUINT length, const char* s)
  499. {
  500. return String(s, length);
  501. }
  502. static void ConstructString(String* ptr)
  503. {
  504. new(ptr) String();
  505. }
  506. static void ConstructStringCopy(const String& str, String* ptr)
  507. {
  508. new(ptr) String(str);
  509. }
  510. static void DestructString(String* ptr)
  511. {
  512. ptr->~String();
  513. }
  514. static char* StringCharAt(unsigned int i, String& str)
  515. {
  516. if (i >= str.Length())
  517. {
  518. asIScriptContext* context = asGetActiveContext();
  519. if (context)
  520. context->SetException("Index out of bounds");
  521. return 0;
  522. }
  523. return &str[i];
  524. }
  525. static int StringCmp(const String& lhs, const String& rhs)
  526. {
  527. int cmp = 0;
  528. if (lhs < rhs)
  529. cmp = -1;
  530. else if (lhs > rhs)
  531. cmp = 1;
  532. return cmp;
  533. }
  534. void StringResize(unsigned newSize, String& str)
  535. {
  536. unsigned oldSize = str.Length();
  537. str.Resize(newSize);
  538. for (unsigned i = oldSize; i < newSize; ++i)
  539. str[i] = ' ';
  540. }
  541. static void ConstructStringInt(int value, String* ptr)
  542. {
  543. new(ptr) String(value);
  544. }
  545. static void ConstructStringUInt(unsigned value, String* ptr)
  546. {
  547. new(ptr) String(value);
  548. }
  549. static void ConstructStringFloat(float value, String* ptr)
  550. {
  551. new(ptr) String(value);
  552. }
  553. static void ConstructStringBool(bool value, String* ptr)
  554. {
  555. new(ptr) String(value);
  556. }
  557. static String& StringAssignInt(int value, String& str)
  558. {
  559. str = String(value);
  560. return str;
  561. }
  562. static String& StringAddAssignInt(int value, String& str)
  563. {
  564. str += String(value);
  565. return str;
  566. }
  567. static String StringAddInt(int value, const String& str)
  568. {
  569. return str + String(value);
  570. }
  571. static String StringAddIntReverse(int value, const String& str)
  572. {
  573. return String(value) + str;
  574. }
  575. static String& StringAssignUInt(unsigned value, String& str)
  576. {
  577. str = String(value);
  578. return str;
  579. }
  580. static String& StringAddAssignUInt(unsigned value, String& str)
  581. {
  582. str += String(value);
  583. return str;
  584. }
  585. static String StringAddUInt(unsigned value, const String& str)
  586. {
  587. return str + String(value);
  588. }
  589. static String StringAddUIntReverse(unsigned value, const String& str)
  590. {
  591. return String(value) + str;
  592. }
  593. static String& StringAssignFloat(float value, String& str)
  594. {
  595. str = String(value);
  596. return str;
  597. }
  598. static String& StringAddAssignFloat(float value, String& str)
  599. {
  600. str += String(value);
  601. return str;
  602. }
  603. static String StringAddFloat(float value, const String& str)
  604. {
  605. return str + String(value);
  606. }
  607. static String StringAddFloatReverse(float value, const String& str)
  608. {
  609. return String(value) + str;
  610. }
  611. static String& StringAssignBool(bool value, String& str)
  612. {
  613. str = String(value);
  614. return str;
  615. }
  616. static String& StringAddAssignBool(bool value, String& str)
  617. {
  618. str += String(value);
  619. return str;
  620. }
  621. static String StringAddBool(bool value, const String& str)
  622. {
  623. return str + String(value);
  624. }
  625. static String StringAddBoolReverse(bool value, const String& str)
  626. {
  627. return String(value) + str;
  628. }
  629. void RegisterString(asIScriptEngine *engine)
  630. {
  631. engine->RegisterObjectType("String", sizeof(String), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  632. engine->RegisterStringFactory("String", asFUNCTION(StringFactory), asCALL_CDECL);
  633. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  634. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(const String&in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  635. engine->RegisterObjectBehaviour("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  636. engine->RegisterObjectMethod("String", "String& opAssign(const String&in)", asMETHODPR(String, operator =, (const String&), String&), asCALL_THISCALL);
  637. engine->RegisterObjectMethod("String", "String& opAddAssign(const String&in)", asMETHODPR(String, operator +=, (const String&), String&), asCALL_THISCALL);
  638. engine->RegisterObjectMethod("String", "bool opEquals(const String&in) const", asMETHODPR(String, operator ==, (const String&) const, bool), asCALL_THISCALL);
  639. engine->RegisterObjectMethod("String", "int opCmp(const String&in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  640. engine->RegisterObjectMethod("String", "String opAdd(const String&in) const", asMETHODPR(String, operator +, (const String&) const, String), asCALL_THISCALL);
  641. engine->RegisterObjectMethod("String", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  642. engine->RegisterObjectMethod("String", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  643. engine->RegisterObjectMethod("String", "void Replace(uint8, uint8)", asMETHODPR(String, Replace, (char, char), void), asCALL_THISCALL);
  644. engine->RegisterObjectMethod("String", "void Replace(const String&in, const String&in)", asMETHODPR(String, Replace, (const String&, const String&), void), asCALL_THISCALL);
  645. engine->RegisterObjectMethod("String", "String Replaced(uint8, uint8) const", asMETHODPR(String, Replaced, (char, char) const, String), asCALL_THISCALL);
  646. engine->RegisterObjectMethod("String", "String Replaced(const String&in, const String&in) const", asMETHODPR(String, Replaced, (const String&, const String&) const, String), asCALL_THISCALL);
  647. engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
  648. engine->RegisterObjectMethod("String", "int Find(const String&in, uint start = 0) const", asMETHODPR(String, Find, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  649. engine->RegisterObjectMethod("String", "int Find(uint8, uint start = 0) const", asMETHODPR(String, Find, (char, unsigned) const, unsigned), asCALL_THISCALL);
  650. engine->RegisterObjectMethod("String", "int FindLast(const String&in, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (const String&, unsigned) const, unsigned), asCALL_THISCALL);
  651. engine->RegisterObjectMethod("String", "int FindLast(uint8, uint start = 0xffffffff) const", asMETHODPR(String, FindLast, (char, unsigned) const, unsigned), asCALL_THISCALL);
  652. engine->RegisterObjectMethod("String", "bool StartsWith(const String&in) const", asMETHOD(String, StartsWith), asCALL_THISCALL);
  653. engine->RegisterObjectMethod("String", "bool EndsWith(const String&in) const", asMETHOD(String, EndsWith), asCALL_THISCALL);
  654. engine->RegisterObjectMethod("String", "String Substring(uint) const", asMETHODPR(String, Substring, (unsigned) const, String), asCALL_THISCALL);
  655. engine->RegisterObjectMethod("String", "String Substring(uint, uint) const", asMETHODPR(String, Substring, (unsigned, unsigned) const, String), asCALL_THISCALL);
  656. engine->RegisterObjectMethod("String", "String ToUpper() const", asMETHOD(String, ToUpper), asCALL_THISCALL);
  657. engine->RegisterObjectMethod("String", "String ToLower() const", asMETHOD(String, ToLower), asCALL_THISCALL);
  658. engine->RegisterObjectMethod("String", "String Trimmed() const", asMETHOD(String, Trimmed), asCALL_THISCALL);
  659. engine->RegisterObjectMethod("String", "uint get_length() const", asMETHOD(String, Length), asCALL_THISCALL);
  660. engine->RegisterObjectMethod("String", "bool get_empty() const", asMETHOD(String, Empty), asCALL_THISCALL);
  661. engine->RegisterObjectMethod("String", "int Compare(const String&in, bool caseSensitive = true) const", asMETHODPR(String, Compare, (const String&, bool) const, int), asCALL_THISCALL);
  662. // Register automatic conversion functions for convenience
  663. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  664. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  665. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  666. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  667. engine->RegisterObjectMethod("String", "String& opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  668. engine->RegisterObjectMethod("String", "String& opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  669. engine->RegisterObjectMethod("String", "String opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  670. engine->RegisterObjectMethod("String", "String opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  671. engine->RegisterObjectMethod("String", "String& opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  672. engine->RegisterObjectMethod("String", "String& opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  673. engine->RegisterObjectMethod("String", "String opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  674. engine->RegisterObjectMethod("String", "String opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  675. engine->RegisterObjectMethod("String", "String& opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  676. engine->RegisterObjectMethod("String", "String& opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  677. engine->RegisterObjectMethod("String", "String opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  678. engine->RegisterObjectMethod("String", "String opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  679. engine->RegisterObjectMethod("String", "String& opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  680. engine->RegisterObjectMethod("String", "String& opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  681. engine->RegisterObjectMethod("String", "String opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  682. engine->RegisterObjectMethod("String", "String opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  683. }