JSIO.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "JSIO.h"
  23. #include "JSVM.h"
  24. #include <Atomic/IO/BufferQueue.h>
  25. #include <Atomic/IO/File.h>
  26. namespace Atomic
  27. {
  28. enum IO_MAGIC_TYPE
  29. {
  30. IO_MAGIC_NONE = 0,
  31. IO_MAGIC_INT,
  32. IO_MAGIC_BOOL,
  33. IO_MAGIC_FLOAT,
  34. IO_MAGIC_STRING,
  35. IO_MAGIC_ZEROSTRING,
  36. IO_MAGIC_BINARY
  37. };
  38. static Serializer* CastToSerializer(duk_context* ctx, int index)
  39. {
  40. Object* o = js_to_class_instance<Object>(ctx, index, 0);
  41. Serializer* serial = NULL;
  42. if (!o)
  43. return NULL;
  44. // Type check! Only File and BufferQueue are supported for now.
  45. StringHash type(o->GetType());
  46. if (type == File::GetTypeStatic())
  47. {
  48. // Cast it!
  49. serial = (Serializer*)((File*)o);
  50. }
  51. else if (type == BufferQueue::GetTypeStatic())
  52. {
  53. // Cast it!
  54. serial = (Serializer*)((BufferQueue*)o);
  55. }
  56. return serial;
  57. }
  58. static Deserializer* CastToDeserializer(duk_context* ctx, int index)
  59. {
  60. Object* o = js_to_class_instance<Object>(ctx, index, 0);
  61. Deserializer* deserial = NULL;
  62. if (!o)
  63. return NULL;
  64. // Type check! Only File and BufferQueue are supported for now.
  65. StringHash type(o->GetType());
  66. if (type == File::GetTypeStatic())
  67. {
  68. // Cast it!
  69. deserial = (Deserializer*)((File*)o);
  70. }
  71. else if (type == BufferQueue::GetTypeStatic())
  72. {
  73. // Cast it!
  74. deserial = (Deserializer*)((BufferQueue*)o);
  75. }
  76. return deserial;
  77. }
  78. static int Serializer_Write(duk_context* ctx)
  79. {
  80. duk_int_t magic = duk_get_current_magic(ctx);
  81. duk_push_this(ctx);
  82. // safe cast based on type check above
  83. Serializer* serial = CastToSerializer(ctx, duk_get_top_index(ctx));
  84. duk_pop(ctx);
  85. if (!serial)
  86. {
  87. duk_push_boolean(ctx, 0);
  88. return 1;
  89. }
  90. const char* str;
  91. size_t length;
  92. IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;
  93. bool success = false;
  94. switch(v)
  95. {
  96. case IO_MAGIC_INT:
  97. success = serial->WriteInt((int) duk_require_number(ctx, 0));
  98. break;
  99. case IO_MAGIC_STRING:
  100. str = duk_require_string(ctx, 0);
  101. length = strlen(str);
  102. success = serial->Write(str, length);
  103. /*
  104. if (length)
  105. {
  106. buffer.Resize(length);
  107. for (size_t i = 0; i < length; i++)
  108. buffer[i] = str[i];
  109. serial->WriteBuffer(buffer);
  110. }
  111. */
  112. break;
  113. case IO_MAGIC_ZEROSTRING:
  114. success = serial->WriteString(duk_require_string(ctx, 0));
  115. break;
  116. case IO_MAGIC_BINARY:
  117. str = (const char*) duk_require_buffer_data(ctx, 0, &length);
  118. success = serial->Write(str, length);
  119. break;
  120. default:
  121. break;
  122. }
  123. duk_push_boolean(ctx, success ? 1 : 0);
  124. return 1;
  125. }
  126. static int Deserializer_Read(duk_context* ctx)
  127. {
  128. duk_int_t magic = duk_get_current_magic(ctx);
  129. duk_push_this(ctx);
  130. // safe cast based on type check above
  131. Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));
  132. duk_pop(ctx);
  133. if (!deserial)
  134. {
  135. duk_push_boolean(ctx, 0);
  136. return 1;
  137. }
  138. char* data;
  139. String str;
  140. size_t length;
  141. IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;
  142. bool success = false;
  143. switch(v)
  144. {
  145. case IO_MAGIC_INT:
  146. duk_push_number(ctx, (double) deserial->ReadInt());
  147. return 1;
  148. case IO_MAGIC_STRING:
  149. length = deserial->GetSize() - deserial->GetPosition();
  150. str.Resize(length + 1);
  151. deserial->Read(&str[0], length);
  152. str[length] = '\0';
  153. duk_push_string(ctx, str.CString());
  154. return 1;
  155. case IO_MAGIC_ZEROSTRING:
  156. success = duk_push_string(ctx, deserial->ReadString().CString());
  157. return 1;
  158. case IO_MAGIC_BINARY:
  159. length = deserial->GetSize() - deserial->GetPosition();
  160. duk_push_fixed_buffer(ctx, length);
  161. duk_push_buffer_object(ctx, -1, 0, length, DUK_BUFOBJ_UINT8ARRAY);
  162. duk_replace(ctx, -2);
  163. data = (char*) duk_require_buffer_data(ctx, 0, &length);
  164. success = deserial->Read(data, length);
  165. return 1;
  166. default:
  167. break;
  168. }
  169. duk_push_undefined(ctx);
  170. return 1;
  171. }
  172. static int Deserializer_GetSize(duk_context* ctx)
  173. {
  174. duk_push_this(ctx);
  175. // safe cast based on type check above
  176. Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));
  177. duk_pop(ctx);
  178. if (!deserial)
  179. {
  180. duk_push_boolean(ctx, 0);
  181. return 1;
  182. }
  183. duk_push_number(ctx, (double)deserial->GetSize());
  184. return 1;
  185. }
  186. static void AddSerializerMixin(duk_context* ctx, const String& package, const String& classname)
  187. {
  188. js_class_get_prototype(ctx, package.CString(), classname.CString());
  189. duk_push_c_function(ctx, Serializer_Write, 1);
  190. duk_set_magic(ctx, -1, (unsigned) VAR_INT);
  191. duk_put_prop_string(ctx, -2, "writeInt");
  192. duk_push_c_function(ctx, Serializer_Write, 1);
  193. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_STRING);
  194. duk_put_prop_string(ctx, -2, "writeString");
  195. duk_push_c_function(ctx, Serializer_Write, 1);
  196. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_ZEROSTRING);
  197. duk_put_prop_string(ctx, -2, "writeZeroString");
  198. duk_push_c_function(ctx, Serializer_Write, 1);
  199. duk_set_magic(ctx, -1, (unsigned)IO_MAGIC_BINARY);
  200. duk_put_prop_string(ctx, -2, "writeBinary");
  201. duk_pop(ctx);
  202. }
  203. static void AddDeserializerMixin(duk_context* ctx, const String& package, const String& classname)
  204. {
  205. js_class_get_prototype(ctx, package.CString(), classname.CString());
  206. duk_push_c_function(ctx, Deserializer_Read, 0);
  207. duk_set_magic(ctx, -1, (unsigned) VAR_INT);
  208. duk_put_prop_string(ctx, -2, "readInt");
  209. duk_push_c_function(ctx, Deserializer_Read, 0);
  210. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_STRING);
  211. duk_put_prop_string(ctx, -2, "readString");
  212. duk_push_c_function(ctx, Deserializer_Read, 0);
  213. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_ZEROSTRING);
  214. duk_put_prop_string(ctx, -2, "readZeroString");
  215. duk_push_c_function(ctx, Deserializer_Read, 0);
  216. duk_set_magic(ctx, -1, (unsigned)IO_MAGIC_BINARY);
  217. duk_put_prop_string(ctx, -2, "readBinary");
  218. duk_push_c_function(ctx, Deserializer_GetSize, 0);
  219. duk_put_prop_string(ctx, -2, "getSize");
  220. duk_pop(ctx);
  221. }
  222. static int File_ReadText(duk_context* ctx)
  223. {
  224. duk_push_this(ctx);
  225. File* file = js_to_class_instance<File>(ctx, -1, 0);
  226. String text;
  227. file->ReadText(text);
  228. duk_push_string(ctx, text.CString());
  229. return 1;
  230. }
  231. void jsapi_init_io(JSVM* vm)
  232. {
  233. duk_context* ctx = vm->GetJSContext();
  234. js_class_get_prototype(ctx, "Atomic", "File");
  235. duk_push_c_function(ctx, File_ReadText, 0);
  236. duk_put_prop_string(ctx, -2, "readText");
  237. duk_pop(ctx);
  238. AddSerializerMixin(ctx, "Atomic", "File");
  239. AddDeserializerMixin(ctx, "Atomic", "File");
  240. AddSerializerMixin(ctx, "Atomic", "BufferQueue");
  241. AddDeserializerMixin(ctx, "Atomic", "BufferQueue");
  242. }
  243. }