javascript_singleton.cpp 13 KB

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