library_godot_javascript_singleton.js 11 KB

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