javascript_eval.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************/
  2. /* javascript_eval.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef JAVASCRIPT_EVAL_ENABLED
  31. #include "api/javascript_eval.h"
  32. #include "emscripten.h"
  33. extern "C" EMSCRIPTEN_KEEPALIVE uint8_t *resize_poolbytearray_and_open_write(PoolByteArray *p_arr, PoolByteArray::Write *r_write, int p_len) {
  34. p_arr->resize(p_len);
  35. *r_write = p_arr->write();
  36. return r_write->ptr();
  37. }
  38. Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
  39. union {
  40. bool b;
  41. double d;
  42. char *s;
  43. } js_data[4];
  44. PoolByteArray arr;
  45. PoolByteArray::Write arr_write;
  46. /* clang-format off */
  47. Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({
  48. const CODE = $0;
  49. const USE_GLOBAL_EXEC_CONTEXT = $1;
  50. const PTR = $2;
  51. const ELEM_LEN = $3;
  52. const BYTEARRAY_PTR = $4;
  53. const BYTEARRAY_WRITE_PTR = $5;
  54. var eval_ret;
  55. try {
  56. if (USE_GLOBAL_EXEC_CONTEXT) {
  57. // indirect eval call grants global execution context
  58. var global_eval = eval;
  59. eval_ret = global_eval(UTF8ToString(CODE));
  60. } else {
  61. eval_ret = eval(UTF8ToString(CODE));
  62. }
  63. } catch (e) {
  64. Module.printErr(e);
  65. eval_ret = null;
  66. }
  67. switch (typeof eval_ret) {
  68. case 'boolean':
  69. setValue(PTR, eval_ret, 'i32');
  70. return 1; // BOOL
  71. case 'number':
  72. setValue(PTR, eval_ret, 'double');
  73. return 3; // REAL
  74. case 'string':
  75. var array_len = lengthBytesUTF8(eval_ret)+1;
  76. var array_ptr = _malloc(array_len);
  77. try {
  78. if (array_ptr===0) {
  79. throw new Error('String allocation failed (probably out of memory)');
  80. }
  81. setValue(PTR, array_ptr , '*');
  82. stringToUTF8(eval_ret, array_ptr, array_len);
  83. return 4; // STRING
  84. } catch (e) {
  85. if (array_ptr!==0) {
  86. _free(array_ptr)
  87. }
  88. Module.printErr(e);
  89. // fall through
  90. }
  91. break;
  92. case 'object':
  93. if (eval_ret === null) {
  94. break;
  95. }
  96. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  97. eval_ret = new Uint8Array(eval_ret.buffer);
  98. }
  99. else if (eval_ret instanceof ArrayBuffer) {
  100. eval_ret = new Uint8Array(eval_ret);
  101. }
  102. if (eval_ret instanceof Uint8Array) {
  103. var bytes_ptr = ccall('resize_poolbytearray_and_open_write', 'number', ['number', 'number' ,'number'], [BYTEARRAY_PTR, BYTEARRAY_WRITE_PTR, eval_ret.length]);
  104. HEAPU8.set(eval_ret, bytes_ptr);
  105. return 20; // POOL_BYTE_ARRAY
  106. }
  107. if (typeof eval_ret.x==='number' && typeof eval_ret.y==='number') {
  108. setValue(PTR, eval_ret.x, 'double');
  109. setValue(PTR + ELEM_LEN, eval_ret.y, 'double');
  110. if (typeof eval_ret.z==='number') {
  111. setValue(PTR + ELEM_LEN*2, eval_ret.z, 'double');
  112. return 7; // VECTOR3
  113. }
  114. else if (typeof eval_ret.width==='number' && typeof eval_ret.height==='number') {
  115. setValue(PTR + ELEM_LEN*2, eval_ret.width, 'double');
  116. setValue(PTR + ELEM_LEN*3, eval_ret.height, 'double');
  117. return 6; // RECT2
  118. }
  119. return 5; // VECTOR2
  120. }
  121. if (typeof eval_ret.r === 'number' && typeof eval_ret.g === 'number' && typeof eval_ret.b === 'number') {
  122. setValue(PTR, eval_ret.r, 'double');
  123. setValue(PTR + ELEM_LEN, eval_ret.g, 'double');
  124. setValue(PTR + ELEM_LEN*2, eval_ret.b, 'double');
  125. setValue(PTR + ELEM_LEN*3, typeof eval_ret.a === 'number' ? eval_ret.a : 1, 'double');
  126. return 14; // COLOR
  127. }
  128. break;
  129. }
  130. return 0; // NIL
  131. }, p_code.utf8().get_data(), p_use_global_exec_context, js_data, sizeof *js_data, &arr, &arr_write));
  132. /* clang-format on */
  133. switch (return_type) {
  134. case Variant::BOOL:
  135. return js_data->b;
  136. case Variant::REAL:
  137. return js_data->d;
  138. case Variant::STRING: {
  139. String str = String::utf8(js_data->s);
  140. /* clang-format off */
  141. EM_ASM_({ _free($0); }, js_data->s);
  142. /* clang-format on */
  143. return str;
  144. }
  145. case Variant::VECTOR2:
  146. return Vector2(js_data[0].d, js_data[1].d);
  147. case Variant::VECTOR3:
  148. return Vector3(js_data[0].d, js_data[1].d, js_data[2].d);
  149. case Variant::RECT2:
  150. return Rect2(js_data[0].d, js_data[1].d, js_data[2].d, js_data[3].d);
  151. case Variant::COLOR:
  152. return Color(js_data[0].d, js_data[1].d, js_data[2].d, js_data[3].d);
  153. case Variant::POOL_BYTE_ARRAY:
  154. arr_write = PoolByteArray::Write();
  155. return arr;
  156. }
  157. return Variant();
  158. }
  159. #endif // JAVASCRIPT_EVAL_ENABLED