sqstdblob.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* see copyright notice in squirrel.h */
  2. #include <new>
  3. #include <squirrel.h>
  4. #include <sqstdio.h>
  5. #include <string.h>
  6. #include <sqstdblob.h>
  7. #include "sqstdstream.h"
  8. #include "sqstdblobimpl.h"
  9. #define SQSTD_BLOB_TYPE_TAG ((SQUnsignedInteger)(SQSTD_STREAM_TYPE_TAG | 0x00000002))
  10. //Blob
  11. #define SETUP_BLOB(v) \
  12. SQBlob *self = NULL; \
  13. { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG,SQFalse))) \
  14. return sq_throwerror(v,_SC("invalid type tag")); } \
  15. if(!self || !self->IsValid()) \
  16. return sq_throwerror(v,_SC("the blob is invalid"));
  17. static SQInteger _blob_resize(HSQUIRRELVM v)
  18. {
  19. SETUP_BLOB(v);
  20. SQInteger size;
  21. sq_getinteger(v,2,&size);
  22. if(!self->Resize(size))
  23. return sq_throwerror(v,_SC("resize failed"));
  24. return 0;
  25. }
  26. static void __swap_dword(unsigned int *n)
  27. {
  28. *n=(unsigned int)(((*n&0xFF000000)>>24) |
  29. ((*n&0x00FF0000)>>8) |
  30. ((*n&0x0000FF00)<<8) |
  31. ((*n&0x000000FF)<<24));
  32. }
  33. static void __swap_word(unsigned short *n)
  34. {
  35. *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
  36. }
  37. static SQInteger _blob_swap4(HSQUIRRELVM v)
  38. {
  39. SETUP_BLOB(v);
  40. SQInteger num=(self->Len()-(self->Len()%4))>>2;
  41. unsigned int *t=(unsigned int *)self->GetBuf();
  42. for(SQInteger i = 0; i < num; i++) {
  43. __swap_dword(&t[i]);
  44. }
  45. return 0;
  46. }
  47. static SQInteger _blob_swap2(HSQUIRRELVM v)
  48. {
  49. SETUP_BLOB(v);
  50. SQInteger num=(self->Len()-(self->Len()%2))>>1;
  51. unsigned short *t = (unsigned short *)self->GetBuf();
  52. for(SQInteger i = 0; i < num; i++) {
  53. __swap_word(&t[i]);
  54. }
  55. return 0;
  56. }
  57. static SQInteger _blob__set(HSQUIRRELVM v)
  58. {
  59. SETUP_BLOB(v);
  60. SQInteger idx,val;
  61. sq_getinteger(v,2,&idx);
  62. sq_getinteger(v,3,&val);
  63. if(idx < 0 || idx >= self->Len())
  64. return sq_throwerror(v,_SC("index out of range"));
  65. ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
  66. sq_push(v,3);
  67. return 1;
  68. }
  69. static SQInteger _blob__get(HSQUIRRELVM v)
  70. {
  71. SETUP_BLOB(v);
  72. SQInteger idx;
  73. if ((sq_gettype(v, 2) & SQOBJECT_NUMERIC) == 0)
  74. {
  75. sq_pushnull(v);
  76. return sq_throwobject(v);
  77. }
  78. sq_getinteger(v,2,&idx);
  79. if(idx < 0 || idx >= self->Len())
  80. return sq_throwerror(v,_SC("index out of range"));
  81. sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
  82. return 1;
  83. }
  84. static SQInteger _blob__nexti(HSQUIRRELVM v)
  85. {
  86. SETUP_BLOB(v);
  87. if(sq_gettype(v,2) == OT_NULL) {
  88. sq_pushinteger(v, 0);
  89. return 1;
  90. }
  91. SQInteger idx;
  92. if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
  93. if(idx+1 < self->Len()) {
  94. sq_pushinteger(v, idx+1);
  95. return 1;
  96. }
  97. sq_pushnull(v);
  98. return 1;
  99. }
  100. return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
  101. }
  102. static SQInteger _blob__typeof(HSQUIRRELVM v)
  103. {
  104. sq_pushstring(v,_SC("blob"),-1);
  105. return 1;
  106. }
  107. static SQInteger _blob_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
  108. {
  109. SQBlob *self = (SQBlob*)p;
  110. self->~SQBlob();
  111. sq_free(self,sizeof(SQBlob));
  112. return 1;
  113. }
  114. static SQInteger _blob_constructor(HSQUIRRELVM v)
  115. {
  116. SQInteger nparam = sq_gettop(v);
  117. SQInteger size = 0;
  118. if(nparam == 2) {
  119. sq_getinteger(v, 2, &size);
  120. }
  121. if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
  122. //SQBlob *b = new SQBlob(size);
  123. SQBlob *b = new (sq_malloc(sizeof(SQBlob)))SQBlob(size);
  124. if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
  125. b->~SQBlob();
  126. sq_free(b,sizeof(SQBlob));
  127. return sq_throwerror(v, _SC("cannot create blob"));
  128. }
  129. sq_setreleasehook(v,1,_blob_releasehook);
  130. return 0;
  131. }
  132. static SQInteger _blob__cloned(HSQUIRRELVM v)
  133. {
  134. SQBlob *other = NULL;
  135. {
  136. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG,SQFalse)))
  137. return SQ_ERROR;
  138. }
  139. //SQBlob *thisone = new SQBlob(other->Len());
  140. SQBlob *thisone = new (sq_malloc(sizeof(SQBlob)))SQBlob(other->Len());
  141. memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
  142. if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
  143. thisone->~SQBlob();
  144. sq_free(thisone,sizeof(SQBlob));
  145. return sq_throwerror(v, _SC("cannot clone blob"));
  146. }
  147. sq_setreleasehook(v,1,_blob_releasehook);
  148. return 0;
  149. }
  150. #define _DECL_BLOB_FUNC(name,nparamsmin,nparamsmax,typecheck) {_SC(#name),_blob_##name,nparamsmin,nparamsmax,typecheck}
  151. static const SQRegFunction _blob_methods[] = {
  152. _DECL_BLOB_FUNC(constructor,1,0,_SC("xn")),
  153. _DECL_BLOB_FUNC(resize,2,2,_SC("xn")),
  154. _DECL_BLOB_FUNC(swap2,1,1,_SC("x")),
  155. _DECL_BLOB_FUNC(swap4,1,1,_SC("x")),
  156. _DECL_BLOB_FUNC(_set,3,3,_SC("xnn")),
  157. _DECL_BLOB_FUNC(_get,2,2,_SC("x.")),
  158. _DECL_BLOB_FUNC(_typeof,1,1,_SC("x")),
  159. _DECL_BLOB_FUNC(_nexti,2,2,_SC("x")),
  160. _DECL_BLOB_FUNC(_cloned,2,2,_SC("xx")),
  161. {NULL,(SQFUNCTION)0,0,0,NULL}
  162. };
  163. //GLOBAL FUNCTIONS
  164. static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
  165. {
  166. SQInteger i;
  167. sq_getinteger(v,2,&i);
  168. sq_pushfloat(v,*((const SQFloat *)&i));
  169. return 1;
  170. }
  171. static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
  172. {
  173. SQFloat f;
  174. sq_getfloat(v,2,&f);
  175. sq_pushinteger(v,*((const SQInteger *)&f));
  176. return 1;
  177. }
  178. static SQInteger _g_blob_swap2(HSQUIRRELVM v)
  179. {
  180. SQInteger i;
  181. sq_getinteger(v,2,&i);
  182. unsigned short s = (unsigned short)i;
  183. sq_pushinteger(v, ((s << 8) | ((s >> 8) & 0x00FFu)) & 0xFFFFu);
  184. return 1;
  185. }
  186. static SQInteger _g_blob_swap4(HSQUIRRELVM v)
  187. {
  188. SQInteger i;
  189. sq_getinteger(v,2,&i);
  190. unsigned int t4 = (unsigned int)i;
  191. __swap_dword(&t4);
  192. sq_pushinteger(v,(SQInteger)t4);
  193. return 1;
  194. }
  195. static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
  196. {
  197. SQFloat f;
  198. sq_getfloat(v,2,&f);
  199. __swap_dword((unsigned int *)&f);
  200. sq_pushfloat(v,f);
  201. return 1;
  202. }
  203. #define _DECL_GLOBALBLOB_FUNC(name,nparamsmin,nparamsmax,typecheck) {_SC(#name),_g_blob_##name,nparamsmin,nparamsmax,typecheck}
  204. static const SQRegFunction bloblib_funcs[]={
  205. _DECL_GLOBALBLOB_FUNC(casti2f,2,2,_SC(".n")),
  206. _DECL_GLOBALBLOB_FUNC(castf2i,2,2,_SC(".n")),
  207. _DECL_GLOBALBLOB_FUNC(swap2,2,2,_SC(".n")),
  208. _DECL_GLOBALBLOB_FUNC(swap4,2,2,_SC(".n")),
  209. _DECL_GLOBALBLOB_FUNC(swapfloat,2,2,_SC(".n")),
  210. {NULL,(SQFUNCTION)0,0,0,NULL}
  211. };
  212. SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
  213. {
  214. SQBlob *blob;
  215. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG,SQTrue)))
  216. return -1;
  217. *ptr = blob->GetBuf();
  218. return SQ_OK;
  219. }
  220. SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
  221. {
  222. SQBlob *blob;
  223. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG,SQTrue)))
  224. return -1;
  225. return blob->Len();
  226. }
  227. SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
  228. {
  229. SQInteger top = sq_gettop(v);
  230. sq_pushregistrytable(v);
  231. sq_pushstring(v,_SC("std_blob"),-1);
  232. if(SQ_SUCCEEDED(sq_get(v,-2))) {
  233. sq_remove(v,-2); //removes the registry
  234. sq_push(v,1); // push the this
  235. sq_pushinteger(v,size); //size
  236. SQBlob *blob = NULL;
  237. if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
  238. && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG,SQTrue))) {
  239. sq_remove(v,-2);
  240. return blob->GetBuf();
  241. }
  242. }
  243. sq_settop(v,top);
  244. return NULL;
  245. }
  246. SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
  247. {
  248. return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
  249. }