javascript_eval.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*************************************************************************/
  2. /* javascript_eval.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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" {
  34. union js_eval_ret {
  35. uint32_t b;
  36. double d;
  37. char *s;
  38. };
  39. extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len));
  40. }
  41. void *resize_poolbytearray_and_open_write(void *p_arr, void *r_write, int p_len) {
  42. PoolByteArray *arr = (PoolByteArray *)p_arr;
  43. PoolByteArray::Write *write = (PoolByteArray::Write *)r_write;
  44. arr->resize(p_len);
  45. *write = arr->write();
  46. return write->ptr();
  47. }
  48. Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) {
  49. PoolByteArray arr;
  50. PoolByteArray::Write arr_write;
  51. union js_eval_ret js_data;
  52. memset(&js_data, 0, sizeof(js_data));
  53. Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_poolbytearray_and_open_write));
  54. switch (return_type) {
  55. case Variant::BOOL:
  56. return js_data.b == 1;
  57. case Variant::REAL:
  58. return js_data.d;
  59. case Variant::STRING: {
  60. String str = String::utf8(js_data.s);
  61. free(js_data.s); // Must free the string allocated in JS.
  62. return str;
  63. }
  64. case Variant::POOL_BYTE_ARRAY:
  65. arr_write = PoolByteArray::Write();
  66. return arr;
  67. default:
  68. return Variant();
  69. }
  70. }
  71. #endif // JAVASCRIPT_EVAL_ENABLED