javascript_bridge_singleton.cpp 15 KB

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