javascript_eval.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* javascript_eval.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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;
  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 BYTEARRAY_PTR = $3;
  52. const BYTEARRAY_WRITE_PTR = $4;
  53. var eval_ret;
  54. try {
  55. if (USE_GLOBAL_EXEC_CONTEXT) {
  56. // indirect eval call grants global execution context
  57. var global_eval = eval;
  58. eval_ret = global_eval(UTF8ToString(CODE));
  59. } else {
  60. eval_ret = eval(UTF8ToString(CODE));
  61. }
  62. } catch (e) {
  63. console.warn(e);
  64. eval_ret = null;
  65. }
  66. switch (typeof eval_ret) {
  67. case 'boolean':
  68. setValue(PTR, eval_ret, 'i32');
  69. return 1; // BOOL
  70. case 'number':
  71. setValue(PTR, eval_ret, 'double');
  72. return 3; // REAL
  73. case 'string':
  74. var array_len = lengthBytesUTF8(eval_ret)+1;
  75. var array_ptr = _malloc(array_len);
  76. try {
  77. if (array_ptr===0) {
  78. throw new Error('String allocation failed (probably out of memory)');
  79. }
  80. setValue(PTR, array_ptr , '*');
  81. stringToUTF8(eval_ret, array_ptr, array_len);
  82. return 4; // STRING
  83. } catch (e) {
  84. if (array_ptr!==0) {
  85. _free(array_ptr)
  86. }
  87. console.warn(e);
  88. // fall through
  89. }
  90. break;
  91. case 'object':
  92. if (eval_ret === null) {
  93. break;
  94. }
  95. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  96. eval_ret = new Uint8Array(eval_ret.buffer);
  97. }
  98. else if (eval_ret instanceof ArrayBuffer) {
  99. eval_ret = new Uint8Array(eval_ret);
  100. }
  101. if (eval_ret instanceof Uint8Array) {
  102. var bytes_ptr = ccall('resize_poolbytearray_and_open_write', 'number', ['number', 'number' ,'number'], [BYTEARRAY_PTR, BYTEARRAY_WRITE_PTR, eval_ret.length]);
  103. HEAPU8.set(eval_ret, bytes_ptr);
  104. return 20; // POOL_BYTE_ARRAY
  105. }
  106. break;
  107. }
  108. return 0; // NIL
  109. }, p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write));
  110. /* clang-format on */
  111. switch (return_type) {
  112. case Variant::BOOL:
  113. return js_data.b;
  114. case Variant::REAL:
  115. return js_data.d;
  116. case Variant::STRING: {
  117. String str = String::utf8(js_data.s);
  118. /* clang-format off */
  119. EM_ASM_({ _free($0); }, js_data.s);
  120. /* clang-format on */
  121. return str;
  122. }
  123. case Variant::POOL_BYTE_ARRAY:
  124. arr_write = PoolByteArray::Write();
  125. return arr;
  126. default:
  127. return Variant();
  128. }
  129. }
  130. #endif // JAVASCRIPT_EVAL_ENABLED