library_godot_javascript_singleton.js 13 KB

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