library_godot_javascript_singleton.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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__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. const argsProxy = GodotJSWrapper.MyProxy(args);
  201. func(p_ref, argsProxy.get_id(), args.length);
  202. argsProxy.unref();
  203. const ret = GodotJSWrapper.cb_ret;
  204. GodotJSWrapper.cb_ret = null;
  205. return ret;
  206. };
  207. id = GodotJSWrapper.get_proxied(cb);
  208. return id;
  209. },
  210. godot_js_wrapper_object_set_cb_ret__sig: 'vii',
  211. godot_js_wrapper_object_set_cb_ret: function (p_val_type, p_val_ex) {
  212. GodotJSWrapper.cb_ret = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
  213. },
  214. godot_js_wrapper_object_getvar__sig: 'iiii',
  215. godot_js_wrapper_object_getvar: function (p_id, p_type, p_exchange) {
  216. const obj = GodotJSWrapper.get_proxied_value(p_id);
  217. if (obj === undefined) {
  218. return -1;
  219. }
  220. const prop = GodotJSWrapper.variant2js(p_type, p_exchange);
  221. if (prop === undefined || prop === null) {
  222. return -1;
  223. }
  224. try {
  225. return GodotJSWrapper.js2variant(obj[prop], p_exchange);
  226. } catch (e) {
  227. GodotRuntime.error(`Error getting variable ${prop} on object`, obj, e);
  228. return -1;
  229. }
  230. },
  231. godot_js_wrapper_object_setvar__sig: 'iiiiii',
  232. godot_js_wrapper_object_setvar: function (p_id, p_key_type, p_key_ex, p_val_type, p_val_ex) {
  233. const obj = GodotJSWrapper.get_proxied_value(p_id);
  234. if (obj === undefined) {
  235. return -1;
  236. }
  237. const key = GodotJSWrapper.variant2js(p_key_type, p_key_ex);
  238. try {
  239. obj[key] = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
  240. return 0;
  241. } catch (e) {
  242. GodotRuntime.error(`Error setting variable ${key} on object`, obj);
  243. return -1;
  244. }
  245. },
  246. godot_js_wrapper_create_object__sig: 'iiiiiiii',
  247. godot_js_wrapper_create_object: function (p_object, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
  248. const name = GodotRuntime.parseString(p_object);
  249. if (typeof (window[name]) === 'undefined') {
  250. return -1;
  251. }
  252. const convert = GodotRuntime.get_func(p_convert_callback);
  253. const freeLock = GodotRuntime.get_func(p_free_lock_callback);
  254. const args = new Array(p_argc);
  255. for (let i = 0; i < p_argc; i++) {
  256. const type = convert(p_args, i, p_exchange, p_lock);
  257. const lock = GodotRuntime.getHeapValue(p_lock, '*');
  258. args[i] = GodotJSWrapper.variant2js(type, p_exchange);
  259. if (lock) {
  260. freeLock(p_lock, type);
  261. }
  262. }
  263. try {
  264. const res = new window[name](...args);
  265. return GodotJSWrapper.js2variant(res, p_exchange);
  266. } catch (e) {
  267. GodotRuntime.error(`Error calling constructor ${name} with args:`, args, 'error:', e);
  268. return -1;
  269. }
  270. },
  271. };
  272. autoAddDeps(GodotJSWrapper, '$GodotJSWrapper');
  273. mergeInto(LibraryManager.library, GodotJSWrapper);
  274. const GodotEval = {
  275. godot_js_eval__deps: ['$GodotRuntime'],
  276. godot_js_eval__sig: 'iiiiiii',
  277. godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
  278. const js_code = GodotRuntime.parseString(p_js);
  279. let eval_ret = null;
  280. try {
  281. if (p_use_global_ctx) {
  282. // indirect eval call grants global execution context
  283. const global_eval = eval; // eslint-disable-line no-eval
  284. eval_ret = global_eval(js_code);
  285. } else {
  286. eval_ret = eval(js_code); // eslint-disable-line no-eval
  287. }
  288. } catch (e) {
  289. GodotRuntime.error(e);
  290. }
  291. switch (typeof eval_ret) {
  292. case 'boolean':
  293. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
  294. return 1; // BOOL
  295. case 'number':
  296. GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
  297. return 3; // FLOAT
  298. case 'string':
  299. GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
  300. return 4; // STRING
  301. case 'object':
  302. if (eval_ret === null) {
  303. break;
  304. }
  305. if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
  306. eval_ret = new Uint8Array(eval_ret.buffer);
  307. } else if (eval_ret instanceof ArrayBuffer) {
  308. eval_ret = new Uint8Array(eval_ret);
  309. }
  310. if (eval_ret instanceof Uint8Array) {
  311. const func = GodotRuntime.get_func(p_callback);
  312. const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
  313. HEAPU8.set(eval_ret, bytes_ptr);
  314. return 29; // PACKED_BYTE_ARRAY
  315. }
  316. break;
  317. // no default
  318. }
  319. return 0; // NIL
  320. },
  321. };
  322. mergeInto(LibraryManager.library, GodotEval);