library_godot_eval.js 3.7 KB

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