library_godot_eval.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*************************************************************************/
  2. /* library_godot_eval.js */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. const GodotEval = {
  31. godot_js_eval__deps: ['$GodotRuntime'],
  32. godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
  33. const js_code = GodotRuntime.parseString(p_js);
  34. let eval_ret = null;
  35. try {
  36. if (p_use_global_ctx) {
  37. // indirect eval call grants global execution context
  38. const global_eval = eval; // eslint-disable-line no-eval
  39. eval_ret = global_eval(js_code);
  40. } else {
  41. eval_ret = eval(js_code); // eslint-disable-line no-eval
  42. }
  43. } catch (e) {
  44. GodotRuntime.error(e);
  45. }
  46. switch (typeof eval_ret) {
  47. case 'boolean':
  48. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
  49. return 1; // BOOL
  50. case 'number':
  51. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
  52. return 3; // REAL
  53. case 'string':
  54. GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
  55. return 4; // STRING
  56. case 'object':
  57. if (eval_ret === null) {
  58. break;
  59. }
  60. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  61. eval_ret = new Uint8Array(eval_ret.buffer);
  62. } else if (eval_ret instanceof ArrayBuffer) {
  63. eval_ret = new Uint8Array(eval_ret);
  64. }
  65. if (eval_ret instanceof Uint8Array) {
  66. const func = GodotRuntime.get_func(p_callback);
  67. const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
  68. HEAPU8.set(eval_ret, bytes_ptr);
  69. return 20; // POOL_BYTE_ARRAY
  70. }
  71. break;
  72. // no default
  73. }
  74. return 0; // NIL
  75. },
  76. };
  77. mergeInto(LibraryManager.library, GodotEval);