javascript_bridge_singleton.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**************************************************************************/
  2. /* javascript_bridge_singleton.cpp */
  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. #include "api/javascript_bridge_singleton.h"
  31. #include "os_web.h"
  32. #include <emscripten.h>
  33. extern "C" {
  34. extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
  35. }
  36. #ifdef JAVASCRIPT_EVAL_ENABLED
  37. extern "C" {
  38. typedef union {
  39. int64_t i;
  40. double r;
  41. void *p;
  42. } godot_js_wrapper_ex;
  43. typedef int (*GodotJSWrapperVariant2JSCallback)(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock);
  44. typedef void (*GodotJSWrapperFreeLockCallback)(void **p_lock, int p_type);
  45. extern int godot_js_wrapper_interface_get(const char *p_name);
  46. extern int godot_js_wrapper_object_call(int p_id, const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback);
  47. extern int godot_js_wrapper_object_get(int p_id, godot_js_wrapper_ex *p_val, const char *p_prop);
  48. extern int godot_js_wrapper_object_getvar(int p_id, int p_type, godot_js_wrapper_ex *p_val);
  49. extern int godot_js_wrapper_object_setvar(int p_id, int p_key_type, godot_js_wrapper_ex *p_key_ex, int p_val_type, godot_js_wrapper_ex *p_val_ex);
  50. extern void godot_js_wrapper_object_set(int p_id, const char *p_name, int p_type, godot_js_wrapper_ex *p_val);
  51. extern void godot_js_wrapper_object_unref(int p_id);
  52. extern int godot_js_wrapper_create_cb(void *p_ref, void (*p_callback)(void *p_ref, int p_arg_id, int p_argc));
  53. extern void godot_js_wrapper_object_set_cb_ret(int p_type, godot_js_wrapper_ex *p_val);
  54. extern int godot_js_wrapper_create_object(const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback);
  55. };
  56. class JavaScriptObjectImpl : public JavaScriptObject {
  57. private:
  58. friend class JavaScriptBridge;
  59. int _js_id = 0;
  60. Callable _callable;
  61. WASM_EXPORT static int _variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock);
  62. WASM_EXPORT static void _free_lock(void **p_lock, int p_type);
  63. WASM_EXPORT static Variant _js2variant(int p_type, godot_js_wrapper_ex *p_val);
  64. WASM_EXPORT static void *_alloc_variants(int p_size);
  65. WASM_EXPORT static void callback(void *p_ref, int p_arg_id, int p_argc);
  66. static void _callback(const JavaScriptObjectImpl *obj, Variant arg);
  67. protected:
  68. bool _set(const StringName &p_name, const Variant &p_value) override;
  69. bool _get(const StringName &p_name, Variant &r_ret) const override;
  70. void _get_property_list(List<PropertyInfo> *p_list) const override;
  71. public:
  72. Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
  73. void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr) override;
  74. Variant callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) override;
  75. JavaScriptObjectImpl() {}
  76. JavaScriptObjectImpl(int p_id) { _js_id = p_id; }
  77. ~JavaScriptObjectImpl() {
  78. if (_js_id) {
  79. godot_js_wrapper_object_unref(_js_id);
  80. }
  81. }
  82. };
  83. bool JavaScriptObjectImpl::_set(const StringName &p_name, const Variant &p_value) {
  84. ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance");
  85. const String name = p_name;
  86. godot_js_wrapper_ex exchange;
  87. void *lock = nullptr;
  88. const Variant *v = &p_value;
  89. int type = _variant2js((const void **)&v, 0, &exchange, &lock);
  90. godot_js_wrapper_object_set(_js_id, name.utf8().get_data(), type, &exchange);
  91. if (lock) {
  92. _free_lock(&lock, type);
  93. }
  94. return true;
  95. }
  96. bool JavaScriptObjectImpl::_get(const StringName &p_name, Variant &r_ret) const {
  97. ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance");
  98. const String name = p_name;
  99. godot_js_wrapper_ex exchange;
  100. int type = godot_js_wrapper_object_get(_js_id, &exchange, name.utf8().get_data());
  101. r_ret = _js2variant(type, &exchange);
  102. return true;
  103. }
  104. Variant JavaScriptObjectImpl::getvar(const Variant &p_key, bool *r_valid) const {
  105. if (r_valid) {
  106. *r_valid = false;
  107. }
  108. godot_js_wrapper_ex exchange;
  109. void *lock = nullptr;
  110. const Variant *v = &p_key;
  111. int prop_type = _variant2js((const void **)&v, 0, &exchange, &lock);
  112. int type = godot_js_wrapper_object_getvar(_js_id, prop_type, &exchange);
  113. if (lock) {
  114. _free_lock(&lock, prop_type);
  115. }
  116. if (type < 0) {
  117. return Variant();
  118. }
  119. if (r_valid) {
  120. *r_valid = true;
  121. }
  122. return _js2variant(type, &exchange);
  123. }
  124. void JavaScriptObjectImpl::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  125. if (r_valid) {
  126. *r_valid = false;
  127. }
  128. godot_js_wrapper_ex kex, vex;
  129. void *klock = nullptr;
  130. void *vlock = nullptr;
  131. const Variant *kv = &p_key;
  132. const Variant *vv = &p_value;
  133. int ktype = _variant2js((const void **)&kv, 0, &kex, &klock);
  134. int vtype = _variant2js((const void **)&vv, 0, &vex, &vlock);
  135. int ret = godot_js_wrapper_object_setvar(_js_id, ktype, &kex, vtype, &vex);
  136. if (klock) {
  137. _free_lock(&klock, ktype);
  138. }
  139. if (vlock) {
  140. _free_lock(&vlock, vtype);
  141. }
  142. if (ret == 0 && r_valid) {
  143. *r_valid = true;
  144. }
  145. }
  146. void JavaScriptObjectImpl::_get_property_list(List<PropertyInfo> *p_list) const {
  147. }
  148. void JavaScriptObjectImpl::_free_lock(void **p_lock, int p_type) {
  149. ERR_FAIL_NULL_MSG(*p_lock, "No lock to free!");
  150. const Variant::Type type = (Variant::Type)p_type;
  151. switch (type) {
  152. case Variant::STRING: {
  153. CharString *cs = (CharString *)(*p_lock);
  154. memdelete(cs);
  155. *p_lock = nullptr;
  156. } break;
  157. default:
  158. ERR_FAIL_MSG("Unknown lock type to free. Likely a bug.");
  159. }
  160. }
  161. Variant JavaScriptObjectImpl::_js2variant(int p_type, godot_js_wrapper_ex *p_val) {
  162. Variant::Type type = (Variant::Type)p_type;
  163. switch (type) {
  164. case Variant::BOOL:
  165. return Variant((bool)p_val->i);
  166. case Variant::INT:
  167. return p_val->i;
  168. case Variant::FLOAT:
  169. return p_val->r;
  170. case Variant::STRING: {
  171. String out = String::utf8((const char *)p_val->p);
  172. free(p_val->p);
  173. return out;
  174. }
  175. case Variant::OBJECT: {
  176. return memnew(JavaScriptObjectImpl(p_val->i));
  177. }
  178. default:
  179. return Variant();
  180. }
  181. }
  182. int JavaScriptObjectImpl::_variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock) {
  183. const Variant **args = (const Variant **)p_args;
  184. const Variant *v = args[p_pos];
  185. Variant::Type type = v->get_type();
  186. switch (type) {
  187. case Variant::BOOL:
  188. r_val->i = v->operator bool() ? 1 : 0;
  189. break;
  190. case Variant::INT: {
  191. const int64_t tmp = v->operator int64_t();
  192. if (tmp >= 1LL << 31) {
  193. r_val->r = (double)tmp;
  194. return Variant::FLOAT;
  195. }
  196. r_val->i = v->operator int64_t();
  197. } break;
  198. case Variant::FLOAT:
  199. r_val->r = v->operator real_t();
  200. break;
  201. case Variant::STRING: {
  202. CharString *cs = memnew(CharString(v->operator String().utf8()));
  203. r_val->p = (void *)cs->get_data();
  204. *p_lock = (void *)cs;
  205. } break;
  206. case Variant::OBJECT: {
  207. JavaScriptObject *js_obj = Object::cast_to<JavaScriptObject>(v->operator Object *());
  208. r_val->i = js_obj != nullptr ? ((JavaScriptObjectImpl *)js_obj)->_js_id : 0;
  209. } break;
  210. default:
  211. break;
  212. }
  213. return type;
  214. }
  215. Variant JavaScriptObjectImpl::callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) {
  216. godot_js_wrapper_ex exchange;
  217. const String method = p_method;
  218. void *lock = nullptr;
  219. const int type = godot_js_wrapper_object_call(_js_id, method.utf8().get_data(), (void **)p_args, p_argc, &_variant2js, &exchange, &lock, &_free_lock);
  220. r_error.error = Callable::CallError::CALL_OK;
  221. if (type < 0) {
  222. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  223. return Variant();
  224. }
  225. return _js2variant(type, &exchange);
  226. }
  227. void JavaScriptObjectImpl::callback(void *p_ref, int p_args_id, int p_argc) {
  228. const JavaScriptObjectImpl *obj = (JavaScriptObjectImpl *)p_ref;
  229. ERR_FAIL_COND_MSG(!obj->_callable.is_valid(), "JavaScript callback failed.");
  230. Vector<const Variant *> argp;
  231. Array arg_arr;
  232. for (int i = 0; i < p_argc; i++) {
  233. godot_js_wrapper_ex exchange;
  234. exchange.i = i;
  235. int type = godot_js_wrapper_object_getvar(p_args_id, Variant::INT, &exchange);
  236. arg_arr.push_back(_js2variant(type, &exchange));
  237. }
  238. Variant arg = arg_arr;
  239. #ifdef PROXY_TO_PTHREAD_ENABLED
  240. if (!Thread::is_main_thread()) {
  241. callable_mp_static(JavaScriptObjectImpl::_callback).bind(obj, arg).call_deferred();
  242. return;
  243. }
  244. #endif
  245. _callback(obj, arg);
  246. }
  247. void JavaScriptObjectImpl::_callback(const JavaScriptObjectImpl *obj, Variant arg) {
  248. obj->_callable.call(arg);
  249. // Set return value
  250. godot_js_wrapper_ex exchange;
  251. void *lock = nullptr;
  252. Variant ret;
  253. const Variant *v = &ret;
  254. int type = _variant2js((const void **)&v, 0, &exchange, &lock);
  255. godot_js_wrapper_object_set_cb_ret(type, &exchange);
  256. if (lock) {
  257. _free_lock(&lock, type);
  258. }
  259. }
  260. Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
  261. Ref<JavaScriptObjectImpl> out = memnew(JavaScriptObjectImpl);
  262. out->_callable = p_callable;
  263. out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::callback);
  264. return out;
  265. }
  266. Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
  267. int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data());
  268. ERR_FAIL_COND_V_MSG(!js_id, Ref<JavaScriptObject>(), "No interface '" + p_interface + "' registered.");
  269. return Ref<JavaScriptObject>(memnew(JavaScriptObjectImpl(js_id)));
  270. }
  271. Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  272. if (p_argcount < 1) {
  273. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  274. r_error.expected = 1;
  275. return Ref<JavaScriptObject>();
  276. }
  277. if (p_args[0]->get_type() != Variant::STRING) {
  278. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  279. r_error.argument = 0;
  280. r_error.expected = Variant::STRING;
  281. return Ref<JavaScriptObject>();
  282. }
  283. godot_js_wrapper_ex exchange;
  284. const String object = *p_args[0];
  285. void *lock = nullptr;
  286. const Variant **args = p_argcount > 1 ? &p_args[1] : nullptr;
  287. const int type = godot_js_wrapper_create_object(object.utf8().get_data(), (void **)args, p_argcount - 1, &JavaScriptObjectImpl::_variant2js, &exchange, &lock, &JavaScriptObjectImpl::_free_lock);
  288. r_error.error = Callable::CallError::CALL_OK;
  289. if (type < 0) {
  290. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  291. return Ref<JavaScriptObject>();
  292. }
  293. return JavaScriptObjectImpl::_js2variant(type, &exchange);
  294. }
  295. extern "C" {
  296. union js_eval_ret {
  297. uint32_t b;
  298. double d;
  299. char *s;
  300. };
  301. extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len));
  302. }
  303. void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) {
  304. PackedByteArray *arr = (PackedByteArray *)p_arr;
  305. VectorWriteProxy<uint8_t> *write = (VectorWriteProxy<uint8_t> *)r_write;
  306. arr->resize(p_len);
  307. *write = arr->write;
  308. return arr->ptrw();
  309. }
  310. Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) {
  311. union js_eval_ret js_data;
  312. PackedByteArray arr;
  313. VectorWriteProxy<uint8_t> arr_write;
  314. Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write));
  315. switch (return_type) {
  316. case Variant::BOOL:
  317. return js_data.b;
  318. case Variant::FLOAT:
  319. return js_data.d;
  320. case Variant::STRING: {
  321. String str = String::utf8(js_data.s);
  322. free(js_data.s); // Must free the string allocated in JS.
  323. return str;
  324. }
  325. case Variant::PACKED_BYTE_ARRAY:
  326. arr_write = VectorWriteProxy<uint8_t>();
  327. return arr;
  328. default:
  329. return Variant();
  330. }
  331. }
  332. #endif // JAVASCRIPT_EVAL_ENABLED
  333. void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
  334. godot_js_os_download_buffer(p_arr.ptr(), p_arr.size(), p_name.utf8().get_data(), p_mime.utf8().get_data());
  335. }
  336. bool JavaScriptBridge::pwa_needs_update() const {
  337. return OS_Web::get_singleton()->pwa_needs_update();
  338. }
  339. Error JavaScriptBridge::pwa_update() {
  340. return OS_Web::get_singleton()->pwa_update();
  341. }
  342. void JavaScriptBridge::force_fs_sync() {
  343. OS_Web::get_singleton()->force_fs_sync();
  344. }