JSIO.cpp 5.4 KB

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