library_godot_javascript_singleton.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GodotJSWrapper = {
  31. $GodotJSWrapper__deps: ['$GodotRuntime', '$IDHandler'],
  32. $GodotJSWrapper__postset: 'GodotJSWrapper.proxies = new Map();',
  33. $GodotJSWrapper: {
  34. proxies: null,
  35. MyProxy: function (val) {
  36. const id = IDHandler.add(this);
  37. GodotJSWrapper.proxies.set(val, id);
  38. let refs = 1;
  39. this.ref = function () {
  40. refs++;
  41. };
  42. this.unref = function () {
  43. refs--;
  44. if (refs === 0) {
  45. IDHandler.remove(id);
  46. GodotJSWrapper.proxies.delete(val);
  47. }
  48. };
  49. this.get_val = function () {
  50. return val;
  51. };
  52. this.get_id = function () {
  53. return id;
  54. };
  55. },
  56. get_proxied: function (val) {
  57. const id = GodotJSWrapper.proxies.get(val);
  58. if (id === undefined) {
  59. const proxy = new GodotJSWrapper.MyProxy(val);
  60. return proxy.get_id();
  61. }
  62. IDHandler.get(id).ref();
  63. return id;
  64. },
  65. get_proxied_value: function (id) {
  66. const proxy = IDHandler.get(id);
  67. if (proxy === undefined) {
  68. return undefined;
  69. }
  70. return proxy.get_val();
  71. },
  72. variant2js: function (type, val) {
  73. switch (type) {
  74. case 0:
  75. return null;
  76. case 1:
  77. return !!GodotRuntime.getHeapValue(val, 'i64');
  78. case 2:
  79. return GodotRuntime.getHeapValue(val, 'i64');
  80. case 3:
  81. return GodotRuntime.getHeapValue(val, 'double');
  82. case 4:
  83. return GodotRuntime.parseString(GodotRuntime.getHeapValue(val, '*'));
  84. case 17: // OBJECT
  85. return GodotJSWrapper.get_proxied_value(GodotRuntime.getHeapValue(val, 'i64'));
  86. default:
  87. return undefined;
  88. }
  89. },
  90. js2variant: function (p_val, p_exchange) {
  91. if (p_val === undefined || p_val === null) {
  92. return 0; // NIL
  93. }
  94. const type = typeof (p_val);
  95. if (type === 'boolean') {
  96. GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
  97. return 1; // BOOL
  98. } else if (type === 'number') {
  99. if (Number.isInteger(p_val)) {
  100. GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
  101. return 2; // INT
  102. }
  103. GodotRuntime.setHeapValue(p_exchange, p_val, 'double');
  104. return 3; // REAL
  105. } else if (type === 'string') {
  106. const c_str = GodotRuntime.allocString(p_val);
  107. GodotRuntime.setHeapValue(p_exchange, c_str, '*');
  108. return 4; // STRING
  109. }
  110. const id = GodotJSWrapper.get_proxied(p_val);
  111. GodotRuntime.setHeapValue(p_exchange, id, 'i64');
  112. return 17;
  113. },
  114. },
  115. godot_js_wrapper_interface_get__sig: 'ii',
  116. godot_js_wrapper_interface_get: function (p_name) {
  117. const name = GodotRuntime.parseString(p_name);
  118. if (typeof (window[name]) !== 'undefined') {
  119. return GodotJSWrapper.get_proxied(window[name]);
  120. }
  121. return 0;
  122. },
  123. godot_js_wrapper_object_get__sig: 'iiii',
  124. godot_js_wrapper_object_get: function (p_id, p_exchange, p_prop) {
  125. const obj = GodotJSWrapper.get_proxied_value(p_id);
  126. if (obj === undefined) {
  127. return 0;
  128. }
  129. if (p_prop) {
  130. const prop = GodotRuntime.parseString(p_prop);
  131. try {
  132. return GodotJSWrapper.js2variant(obj[prop], p_exchange);
  133. } catch (e) {
  134. GodotRuntime.error(`Error getting variable ${prop} on object`, obj);
  135. return 0; // NIL
  136. }
  137. }
  138. return GodotJSWrapper.js2variant(obj, p_exchange);
  139. },
  140. godot_js_wrapper_object_set__sig: 'viiii',
  141. godot_js_wrapper_object_set: function (p_id, p_name, p_type, p_exchange) {
  142. const obj = GodotJSWrapper.get_proxied_value(p_id);
  143. if (obj === undefined) {
  144. return;
  145. }
  146. const name = GodotRuntime.parseString(p_name);
  147. try {
  148. obj[name] = GodotJSWrapper.variant2js(p_type, p_exchange);
  149. } catch (e) {
  150. GodotRuntime.error(`Error setting variable ${name} on object`, obj);
  151. }
  152. },
  153. godot_js_wrapper_object_call__sig: 'iiiiiiiii',
  154. godot_js_wrapper_object_call: function (p_id, p_method, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
  155. const obj = GodotJSWrapper.get_proxied_value(p_id);
  156. if (obj === undefined) {
  157. return -1;
  158. }
  159. const method = GodotRuntime.parseString(p_method);
  160. const convert = GodotRuntime.get_func(p_convert_callback);
  161. const freeLock = GodotRuntime.get_func(p_free_lock_callback);
  162. const args = new Array(p_argc);
  163. for (let i = 0; i < p_argc; i++) {
  164. const type = convert(p_args, i, p_exchange, p_lock);
  165. const lock = GodotRuntime.getHeapValue(p_lock, '*');
  166. args[i] = GodotJSWrapper.variant2js(type, p_exchange);
  167. if (lock) {
  168. freeLock(p_lock, type);
  169. }
  170. }
  171. try {
  172. const res = obj[method](...args);
  173. return GodotJSWrapper.js2variant(res, p_exchange);
  174. } catch (e) {
  175. GodotRuntime.error(`Error calling method ${method} on:`, obj, 'error:', e);
  176. return -1;
  177. }
  178. },
  179. godot_js_wrapper_object_unref__sig: 'vi',
  180. godot_js_wrapper_object_unref: function (p_id) {
  181. const proxy = IDHandler.get(p_id);
  182. if (proxy !== undefined) {
  183. proxy.unref();
  184. }
  185. },
  186. godot_js_wrapper_create_cb__sig: 'iii',
  187. godot_js_wrapper_create_cb: function (p_ref, p_func) {
  188. const func = GodotRuntime.get_func(p_func);
  189. let id = 0;
  190. const cb = function () {
  191. if (!GodotJSWrapper.get_proxied_value(id)) {
  192. return;
  193. }
  194. const args = Array.from(arguments);
  195. func(p_ref, GodotJSWrapper.get_proxied(args), args.length);
  196. };
  197. id = GodotJSWrapper.get_proxied(cb);
  198. return id;
  199. },
  200. godot_js_wrapper_object_getvar__sig: 'iiii',
  201. godot_js_wrapper_object_getvar: function (p_id, p_type, p_exchange) {
  202. const obj = GodotJSWrapper.get_proxied_value(p_id);
  203. if (obj === undefined) {
  204. return -1;
  205. }
  206. const prop = GodotJSWrapper.variant2js(p_type, p_exchange);
  207. if (prop === undefined || prop === null) {
  208. return -1;
  209. }
  210. try {
  211. return GodotJSWrapper.js2variant(obj[prop], p_exchange);
  212. } catch (e) {
  213. GodotRuntime.error(`Error getting variable ${prop} on object`, obj, e);
  214. return -1;
  215. }
  216. },
  217. godot_js_wrapper_object_setvar__sig: 'iiiiii',
  218. godot_js_wrapper_object_setvar: function (p_id, p_key_type, p_key_ex, p_val_type, p_val_ex) {
  219. const obj = GodotJSWrapper.get_proxied_value(p_id);
  220. if (obj === undefined) {
  221. return -1;
  222. }
  223. const key = GodotJSWrapper.variant2js(p_key_type, p_key_ex);
  224. try {
  225. obj[key] = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
  226. return 0;
  227. } catch (e) {
  228. GodotRuntime.error(`Error setting variable ${key} on object`, obj);
  229. return -1;
  230. }
  231. },
  232. godot_js_wrapper_create_object__sig: 'iiiiiiii',
  233. godot_js_wrapper_create_object: function (p_object, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
  234. const name = GodotRuntime.parseString(p_object);
  235. if (typeof (window[name]) === 'undefined') {
  236. return -1;
  237. }
  238. const convert = GodotRuntime.get_func(p_convert_callback);
  239. const freeLock = GodotRuntime.get_func(p_free_lock_callback);
  240. const args = new Array(p_argc);
  241. for (let i = 0; i < p_argc; i++) {
  242. const type = convert(p_args, i, p_exchange, p_lock);
  243. const lock = GodotRuntime.getHeapValue(p_lock, '*');
  244. args[i] = GodotJSWrapper.variant2js(type, p_exchange);
  245. if (lock) {
  246. freeLock(p_lock, type);
  247. }
  248. }
  249. try {
  250. const res = new window[name](...args);
  251. return GodotJSWrapper.js2variant(res, p_exchange);
  252. } catch (e) {
  253. GodotRuntime.error(`Error calling constructor ${name} with args:`, args, 'error:', e);
  254. return -1;
  255. }
  256. },
  257. };
  258. autoAddDeps(GodotJSWrapper, '$GodotJSWrapper');
  259. mergeInto(LibraryManager.library, GodotJSWrapper);
  260. const GodotEval = {
  261. godot_js_eval__deps: ['$GodotRuntime'],
  262. godot_js_eval__sig: 'iiiiiii',
  263. godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
  264. const js_code = GodotRuntime.parseString(p_js);
  265. let eval_ret = null;
  266. try {
  267. if (p_use_global_ctx) {
  268. // indirect eval call grants global execution context
  269. const global_eval = eval; // eslint-disable-line no-eval
  270. eval_ret = global_eval(js_code);
  271. } else {
  272. eval_ret = eval(js_code); // eslint-disable-line no-eval
  273. }
  274. } catch (e) {
  275. GodotRuntime.error(e);
  276. }
  277. switch (typeof eval_ret) {
  278. case 'boolean':
  279. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
  280. return 1; // BOOL
  281. case 'number':
  282. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
  283. return 3; // REAL
  284. case 'string':
  285. GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
  286. return 4; // STRING
  287. case 'object':
  288. if (eval_ret === null) {
  289. break;
  290. }
  291. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  292. eval_ret = new Uint8Array(eval_ret.buffer);
  293. } else if (eval_ret instanceof ArrayBuffer) {
  294. eval_ret = new Uint8Array(eval_ret);
  295. }
  296. if (eval_ret instanceof Uint8Array) {
  297. const func = GodotRuntime.get_func(p_callback);
  298. const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
  299. HEAPU8.set(eval_ret, bytes_ptr);
  300. return 20; // POOL_BYTE_ARRAY
  301. }
  302. break;
  303. // no default
  304. }
  305. return 0; // NIL
  306. },
  307. };
  308. mergeInto(LibraryManager.library, GodotEval);