library_godot_javascript_singleton.js 11 KB

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