JSIO.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/File.h>
  25. namespace Atomic
  26. {
  27. enum IO_MAGIC_TYPE
  28. {
  29. IO_MAGIC_NONE = 0,
  30. IO_MAGIC_INT,
  31. IO_MAGIC_BOOL,
  32. IO_MAGIC_FLOAT,
  33. IO_MAGIC_STRING,
  34. IO_MAGIC_ZEROSTRING
  35. };
  36. static Serializer* CastToSerializer(duk_context* ctx, int index)
  37. {
  38. Object* o = js_to_class_instance<Object>(ctx, index, 0);
  39. Serializer* serial = NULL;
  40. if (!o)
  41. return NULL;
  42. // type check! file supported for now
  43. if (o->GetType() == File::GetTypeStatic())
  44. {
  45. // cast it!
  46. serial = (Serializer*) ((File*)o);
  47. }
  48. return serial;
  49. }
  50. static Deserializer* CastToDeserializer(duk_context* ctx, int index)
  51. {
  52. Object* o = js_to_class_instance<Object>(ctx, index, 0);
  53. Deserializer* deserial = NULL;
  54. if (!o)
  55. return NULL;
  56. // type check! file supported for now
  57. if (o->GetType() == File::GetTypeStatic())
  58. {
  59. // cast it!
  60. deserial = (Deserializer*) ((File*)o);
  61. }
  62. return deserial;
  63. }
  64. static int Serializer_Write(duk_context* ctx)
  65. {
  66. duk_int_t magic = duk_get_current_magic(ctx);
  67. duk_push_this(ctx);
  68. // safe cast based on type check above
  69. Serializer* serial = CastToSerializer(ctx, duk_get_top_index(ctx));
  70. duk_pop(ctx);
  71. if (!serial)
  72. {
  73. duk_push_boolean(ctx, 0);
  74. return 1;
  75. }
  76. const char* str;
  77. size_t length;
  78. IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;
  79. bool success = false;
  80. switch(v)
  81. {
  82. case IO_MAGIC_INT:
  83. success = serial->WriteInt((int) duk_require_number(ctx, 0));
  84. break;
  85. case IO_MAGIC_STRING:
  86. str = duk_require_string(ctx, 0);
  87. length = strlen(str);
  88. success = serial->Write(str, length);
  89. /*
  90. if (length)
  91. {
  92. buffer.Resize(length);
  93. for (size_t i = 0; i < length; i++)
  94. buffer[i] = str[i];
  95. serial->WriteBuffer(buffer);
  96. }
  97. */
  98. break;
  99. case IO_MAGIC_ZEROSTRING:
  100. success = serial->WriteString(duk_require_string(ctx, 0));
  101. break;
  102. default:
  103. break;
  104. }
  105. duk_push_boolean(ctx, success ? 1 : 0);
  106. return 1;
  107. }
  108. static int Deserializer_Read(duk_context* ctx)
  109. {
  110. duk_int_t magic = duk_get_current_magic(ctx);
  111. duk_push_this(ctx);
  112. // safe cast based on type check above
  113. Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));
  114. duk_pop(ctx);
  115. if (!deserial)
  116. {
  117. duk_push_boolean(ctx, 0);
  118. return 1;
  119. }
  120. PODVector<unsigned char> buffer;
  121. String str;
  122. size_t length;
  123. IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;
  124. bool success = false;
  125. switch(v)
  126. {
  127. case IO_MAGIC_INT:
  128. duk_push_number(ctx, (double) deserial->ReadInt());
  129. return 1;
  130. case IO_MAGIC_STRING:
  131. length = deserial->GetSize() - deserial->GetPosition();
  132. str.Resize(length + 1);
  133. deserial->Read(&str[0], length);
  134. str[length] = '\0';
  135. duk_push_string(ctx, str.CString());
  136. return 1;
  137. case IO_MAGIC_ZEROSTRING:
  138. success = duk_push_string(ctx, deserial->ReadString().CString());
  139. return 1;
  140. default:
  141. break;
  142. }
  143. duk_push_undefined(ctx);
  144. return 1;
  145. }
  146. static void AddSerializerMixin(duk_context* ctx, const String& package, const String& classname)
  147. {
  148. js_class_get_prototype(ctx, package.CString(), classname.CString());
  149. duk_push_c_function(ctx, Serializer_Write, 1);
  150. duk_set_magic(ctx, -1, (unsigned) VAR_INT);
  151. duk_put_prop_string(ctx, -2, "writeInt");
  152. duk_push_c_function(ctx, Serializer_Write, 1);
  153. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_STRING);
  154. duk_put_prop_string(ctx, -2, "writeString");
  155. duk_push_c_function(ctx, Serializer_Write, 1);
  156. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_ZEROSTRING);
  157. duk_put_prop_string(ctx, -2, "writeZeroString");
  158. duk_pop(ctx);
  159. }
  160. static void AddDeserializerMixin(duk_context* ctx, const String& package, const String& classname)
  161. {
  162. js_class_get_prototype(ctx, package.CString(), classname.CString());
  163. duk_push_c_function(ctx, Deserializer_Read, 0);
  164. duk_set_magic(ctx, -1, (unsigned) VAR_INT);
  165. duk_put_prop_string(ctx, -2, "readInt");
  166. duk_push_c_function(ctx, Deserializer_Read, 0);
  167. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_STRING);
  168. duk_put_prop_string(ctx, -2, "readString");
  169. duk_push_c_function(ctx, Deserializer_Read, 0);
  170. duk_set_magic(ctx, -1, (unsigned) IO_MAGIC_ZEROSTRING);
  171. duk_put_prop_string(ctx, -2, "readZeroString");
  172. duk_pop(ctx);
  173. }
  174. static int File_ReadText(duk_context* ctx)
  175. {
  176. duk_push_this(ctx);
  177. File* file = js_to_class_instance<File>(ctx, -1, 0);
  178. String text;
  179. file->ReadText(text);
  180. duk_push_string(ctx, text.CString());
  181. return 1;
  182. }
  183. void jsapi_init_io(JSVM* vm)
  184. {
  185. duk_context* ctx = vm->GetJSContext();
  186. js_class_get_prototype(ctx, "Atomic", "File");
  187. duk_push_c_function(ctx, File_ReadText, 0);
  188. duk_put_prop_string(ctx, -2, "readText");
  189. duk_pop(ctx);
  190. AddSerializerMixin(ctx, "Atomic", "File");
  191. AddDeserializerMixin(ctx, "Atomic", "File");
  192. }
  193. }