library_godot_javascript_singleton.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**************************************************************************/
  2. /* library_godot_javascript_singleton.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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; // FLOAT
  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__proxy: 'sync',
  117. godot_js_wrapper_interface_get__sig: 'ii',
  118. godot_js_wrapper_interface_get: function (p_name) {
  119. const name = GodotRuntime.parseString(p_name);
  120. if (typeof (window[name]) !== 'undefined') {
  121. return GodotJSWrapper.get_proxied(window[name]);
  122. }
  123. return 0;
  124. },
  125. godot_js_wrapper_object_get__proxy: 'sync',
  126. godot_js_wrapper_object_get__sig: 'iiii',
  127. godot_js_wrapper_object_get: function (p_id, p_exchange, p_prop) {
  128. const obj = GodotJSWrapper.get_proxied_value(p_id);
  129. if (obj === undefined) {
  130. return 0;
  131. }
  132. if (p_prop) {
  133. const prop = GodotRuntime.parseString(p_prop);
  134. try {
  135. return GodotJSWrapper.js2variant(obj[prop], p_exchange);
  136. } catch (e) {
  137. GodotRuntime.error(`Error getting variable ${prop} on object`, obj);
  138. return 0; // NIL
  139. }
  140. }
  141. return GodotJSWrapper.js2variant(obj, p_exchange);
  142. },
  143. godot_js_wrapper_object_set__proxy: 'sync',
  144. godot_js_wrapper_object_set__sig: 'viiii',
  145. godot_js_wrapper_object_set: function (p_id, p_name, p_type, p_exchange) {
  146. const obj = GodotJSWrapper.get_proxied_value(p_id);
  147. if (obj === undefined) {
  148. return;
  149. }
  150. const name = GodotRuntime.parseString(p_name);
  151. try {
  152. obj[name] = GodotJSWrapper.variant2js(p_type, p_exchange);
  153. } catch (e) {
  154. GodotRuntime.error(`Error setting variable ${name} on object`, obj);
  155. }
  156. },
  157. godot_js_wrapper_object_call__proxy: 'sync',
  158. godot_js_wrapper_object_call__sig: 'iiiiiiiii',
  159. 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) {
  160. const obj = GodotJSWrapper.get_proxied_value(p_id);
  161. if (obj === undefined) {
  162. return -1;
  163. }
  164. const method = GodotRuntime.parseString(p_method);
  165. const convert = GodotRuntime.get_func(p_convert_callback);
  166. const freeLock = GodotRuntime.get_func(p_free_lock_callback);
  167. const args = new Array(p_argc);
  168. for (let i = 0; i < p_argc; i++) {
  169. const type = convert(p_args, i, p_exchange, p_lock);
  170. const lock = GodotRuntime.getHeapValue(p_lock, '*');
  171. args[i] = GodotJSWrapper.variant2js(type, p_exchange);
  172. if (lock) {
  173. freeLock(p_lock, type);
  174. }
  175. }
  176. try {
  177. const res = obj[method](...args);
  178. return GodotJSWrapper.js2variant(res, p_exchange);
  179. } catch (e) {
  180. GodotRuntime.error(`Error calling method ${method} on:`, obj, 'error:', e);
  181. return -1;
  182. }
  183. },
  184. godot_js_wrapper_object_unref__proxy: 'sync',
  185. godot_js_wrapper_object_unref__sig: 'vi',
  186. godot_js_wrapper_object_unref: function (p_id) {
  187. const proxy = IDHandler.get(p_id);
  188. if (proxy !== undefined) {
  189. proxy.unref();
  190. }
  191. },
  192. godot_js_wrapper_create_cb__proxy: 'sync',
  193. godot_js_wrapper_create_cb__sig: 'iii',
  194. godot_js_wrapper_create_cb: function (p_ref, p_func) {
  195. const func = GodotRuntime.get_func(p_func);
  196. let id = 0;
  197. const cb = function () {
  198. if (!GodotJSWrapper.get_proxied_value(id)) {
  199. return undefined;
  200. }
  201. // The callback will store the returned value in this variable via
  202. // "godot_js_wrapper_object_set_cb_ret" upon calling the user function.
  203. // This is safe! JavaScript is single threaded (and using it in threads is not a good idea anyway).
  204. GodotJSWrapper.cb_ret = null;
  205. const args = Array.from(arguments);
  206. const argsProxy = new GodotJSWrapper.MyProxy(args);
  207. func(p_ref, argsProxy.get_id(), args.length);
  208. argsProxy.unref();
  209. const ret = GodotJSWrapper.cb_ret;
  210. GodotJSWrapper.cb_ret = null;
  211. return ret;
  212. };
  213. id = GodotJSWrapper.get_proxied(cb);
  214. return id;
  215. },
  216. godot_js_wrapper_object_set_cb_ret__proxy: 'sync',
  217. godot_js_wrapper_object_set_cb_ret__sig: 'vii',
  218. godot_js_wrapper_object_set_cb_ret: function (p_val_type, p_val_ex) {
  219. GodotJSWrapper.cb_ret = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
  220. },
  221. godot_js_wrapper_object_getvar__proxy: 'sync',
  222. godot_js_wrapper_object_getvar__sig: 'iiii',
  223. godot_js_wrapper_object_getvar: function (p_id, p_type, p_exchange) {
  224. const obj = GodotJSWrapper.get_proxied_value(p_id);
  225. if (obj === undefined) {
  226. return -1;
  227. }
  228. const prop = GodotJSWrapper.variant2js(p_type, p_exchange);
  229. if (prop === undefined || prop === null) {
  230. return -1;
  231. }
  232. try {
  233. return GodotJSWrapper.js2variant(obj[prop], p_exchange);
  234. } catch (e) {
  235. GodotRuntime.error(`Error getting variable ${prop} on object`, obj, e);
  236. return -1;
  237. }
  238. },
  239. godot_js_wrapper_object_setvar__proxy: 'sync',
  240. godot_js_wrapper_object_setvar__sig: 'iiiiii',
  241. godot_js_wrapper_object_setvar: function (p_id, p_key_type, p_key_ex, p_val_type, p_val_ex) {
  242. const obj = GodotJSWrapper.get_proxied_value(p_id);
  243. if (obj === undefined) {
  244. return -1;
  245. }
  246. const key = GodotJSWrapper.variant2js(p_key_type, p_key_ex);
  247. try {
  248. obj[key] = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
  249. return 0;
  250. } catch (e) {
  251. GodotRuntime.error(`Error setting variable ${key} on object`, obj);
  252. return -1;
  253. }
  254. },
  255. godot_js_wrapper_create_object__proxy: 'sync',
  256. godot_js_wrapper_create_object__sig: 'iiiiiiii',
  257. godot_js_wrapper_create_object: function (p_object, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
  258. const name = GodotRuntime.parseString(p_object);
  259. if (typeof (window[name]) === 'undefined') {
  260. return -1;
  261. }
  262. const convert = GodotRuntime.get_func(p_convert_callback);
  263. const freeLock = GodotRuntime.get_func(p_free_lock_callback);
  264. const args = new Array(p_argc);
  265. for (let i = 0; i < p_argc; i++) {
  266. const type = convert(p_args, i, p_exchange, p_lock);
  267. const lock = GodotRuntime.getHeapValue(p_lock, '*');
  268. args[i] = GodotJSWrapper.variant2js(type, p_exchange);
  269. if (lock) {
  270. freeLock(p_lock, type);
  271. }
  272. }
  273. try {
  274. const res = new window[name](...args);
  275. return GodotJSWrapper.js2variant(res, p_exchange);
  276. } catch (e) {
  277. GodotRuntime.error(`Error calling constructor ${name} with args:`, args, 'error:', e);
  278. return -1;
  279. }
  280. },
  281. };
  282. autoAddDeps(GodotJSWrapper, '$GodotJSWrapper');
  283. mergeInto(LibraryManager.library, GodotJSWrapper);
  284. const GodotEval = {
  285. godot_js_eval__deps: ['$GodotRuntime'],
  286. godot_js_eval__sig: 'iiiiiii',
  287. godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
  288. const js_code = GodotRuntime.parseString(p_js);
  289. let eval_ret = null;
  290. try {
  291. if (p_use_global_ctx) {
  292. // indirect eval call grants global execution context
  293. const global_eval = eval; // eslint-disable-line no-eval
  294. eval_ret = global_eval(js_code);
  295. } else {
  296. eval_ret = eval(js_code); // eslint-disable-line no-eval
  297. }
  298. } catch (e) {
  299. GodotRuntime.error(e);
  300. }
  301. switch (typeof eval_ret) {
  302. case 'boolean':
  303. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
  304. return 1; // BOOL
  305. case 'number':
  306. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
  307. return 3; // FLOAT
  308. case 'string':
  309. GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
  310. return 4; // STRING
  311. case 'object':
  312. if (eval_ret === null) {
  313. break;
  314. }
  315. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  316. eval_ret = new Uint8Array(eval_ret.buffer);
  317. } else if (eval_ret instanceof ArrayBuffer) {
  318. eval_ret = new Uint8Array(eval_ret);
  319. }
  320. if (eval_ret instanceof Uint8Array) {
  321. const func = GodotRuntime.get_func(p_callback);
  322. const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
  323. HEAPU8.set(eval_ret, bytes_ptr);
  324. return 29; // PACKED_BYTE_ARRAY
  325. }
  326. break;
  327. // no default
  328. }
  329. return 0; // NIL
  330. },
  331. };
  332. mergeInto(LibraryManager.library, GodotEval);