library_godot_javascript_singleton.js 12 KB

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