diff options
author | Adam Scott <ascott.ca@gmail.com> | 2024-08-29 09:50:18 -0400 |
---|---|---|
committer | Adam Scott <ascott.ca@gmail.com> | 2024-09-16 12:13:34 -0400 |
commit | a9b934b65754c93e557c9446e7c01af199bd6b40 (patch) | |
tree | bd692833c21aef532fcfd96a7d895d8df7052016 /platform | |
parent | 99a7a9ccd60fbe4030e067b3c36d54b67737446d (diff) | |
download | redot-engine-a9b934b65754c93e557c9446e7c01af199bd6b40.tar.gz |
Add `JavaScriptBridge` buffer methods
Diffstat (limited to 'platform')
-rw-r--r-- | platform/web/api/api.cpp | 10 | ||||
-rw-r--r-- | platform/web/api/javascript_bridge_singleton.h | 2 | ||||
-rw-r--r-- | platform/web/javascript_bridge_singleton.cpp | 23 | ||||
-rw-r--r-- | platform/web/js/libs/library_godot_javascript_singleton.js | 32 |
4 files changed, 67 insertions, 0 deletions
diff --git a/platform/web/api/api.cpp b/platform/web/api/api.cpp index 9ddbe5d01d..40417bde7e 100644 --- a/platform/web/api/api.cpp +++ b/platform/web/api/api.cpp @@ -66,6 +66,8 @@ void JavaScriptBridge::_bind_methods() { ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScriptBridge::get_interface); ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScriptBridge::create_callback); + ClassDB::bind_method(D_METHOD("is_js_buffer", "javascript_object"), &JavaScriptBridge::is_js_buffer); + ClassDB::bind_method(D_METHOD("js_buffer_to_packed_byte_array", "javascript_buffer"), &JavaScriptBridge::js_buffer_to_packed_byte_array); { MethodInfo mi; mi.name = "create_object"; @@ -93,6 +95,14 @@ Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callab return Ref<JavaScriptObject>(); } +bool JavaScriptBridge::is_js_buffer(Ref<JavaScriptObject> p_js_obj) { + return false; +} + +PackedByteArray JavaScriptBridge::js_buffer_to_packed_byte_array(Ref<JavaScriptObject> p_js_obj) { + return PackedByteArray(); +} + Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; diff --git a/platform/web/api/javascript_bridge_singleton.h b/platform/web/api/javascript_bridge_singleton.h index 456fa6b313..8054440055 100644 --- a/platform/web/api/javascript_bridge_singleton.h +++ b/platform/web/api/javascript_bridge_singleton.h @@ -57,6 +57,8 @@ public: Variant eval(const String &p_code, bool p_use_global_exec_context = false); Ref<JavaScriptObject> get_interface(const String &p_interface); Ref<JavaScriptObject> create_callback(const Callable &p_callable); + bool is_js_buffer(Ref<JavaScriptObject> p_js_obj); + PackedByteArray js_buffer_to_packed_byte_array(Ref<JavaScriptObject> p_js_obj); Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error); void download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime = "application/octet-stream"); bool pwa_needs_update() const; diff --git a/platform/web/javascript_bridge_singleton.cpp b/platform/web/javascript_bridge_singleton.cpp index 502e830f82..869c2e9903 100644 --- a/platform/web/javascript_bridge_singleton.cpp +++ b/platform/web/javascript_bridge_singleton.cpp @@ -59,6 +59,8 @@ extern void godot_js_wrapper_object_unref(int p_id); extern int godot_js_wrapper_create_cb(void *p_ref, void (*p_callback)(void *p_ref, int p_arg_id, int p_argc)); extern void godot_js_wrapper_object_set_cb_ret(int p_type, godot_js_wrapper_ex *p_val); 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); +extern int godot_js_wrapper_object_is_buffer(int p_id); +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)); }; class JavaScriptObjectImpl : public JavaScriptObject { @@ -366,6 +368,27 @@ Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_cont } } +bool JavaScriptBridge::is_js_buffer(Ref<JavaScriptObject> p_js_obj) { + Ref<JavaScriptObjectImpl> obj = p_js_obj; + if (obj.is_null()) { + return false; + } + return godot_js_wrapper_object_is_buffer(obj->_js_id); +} + +PackedByteArray JavaScriptBridge::js_buffer_to_packed_byte_array(Ref<JavaScriptObject> p_js_obj) { + ERR_FAIL_COND_V_MSG(!is_js_buffer(p_js_obj), PackedByteArray(), "The JavaScript object is not a buffer."); + Ref<JavaScriptObjectImpl> obj = p_js_obj; + + PackedByteArray arr; + VectorWriteProxy<uint8_t> arr_write; + + godot_js_wrapper_object_transfer_buffer(obj->_js_id, &arr, &arr_write, resize_PackedByteArray_and_open_write); + + arr_write = VectorWriteProxy<uint8_t>(); + return arr; +} + #endif // JAVASCRIPT_EVAL_ENABLED void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) { diff --git a/platform/web/js/libs/library_godot_javascript_singleton.js b/platform/web/js/libs/library_godot_javascript_singleton.js index 6bb69bca95..3ff6abba60 100644 --- a/platform/web/js/libs/library_godot_javascript_singleton.js +++ b/platform/web/js/libs/library_godot_javascript_singleton.js @@ -127,6 +127,10 @@ const GodotJSWrapper = { GodotRuntime.setHeapValue(p_exchange, id, 'i64'); return 24; // OBJECT }, + + isBuffer: function (obj) { + return obj instanceof ArrayBuffer || ArrayBuffer.isView(obj); + }, }, godot_js_wrapper_interface_get__proxy: 'sync', @@ -303,6 +307,34 @@ const GodotJSWrapper = { return -1; } }, + + godot_js_wrapper_object_is_buffer__proxy: 'sync', + godot_js_wrapper_object_is_buffer__sig: 'ii', + godot_js_wrapper_object_is_buffer: function (p_id) { + const obj = GodotJSWrapper.get_proxied_value(p_id); + return GodotJSWrapper.isBuffer(obj) + ? 1 + : 0; + }, + + godot_js_wrapper_object_transfer_buffer__proxy: 'sync', + godot_js_wrapper_object_transfer_buffer__sig: 'viiii', + godot_js_wrapper_object_transfer_buffer: function (p_id, p_byte_arr, p_byte_arr_write, p_callback) { + let obj = GodotJSWrapper.get_proxied_value(p_id); + if (!GodotJSWrapper.isBuffer(obj)) { + return; + } + + if (ArrayBuffer.isView(obj) && !(obj instanceof Uint8Array)) { + obj = new Uint8Array(obj.buffer); + } else if (obj instanceof ArrayBuffer) { + obj = new Uint8Array(obj); + } + + const resizePackedByteArrayAndOpenWrite = GodotRuntime.get_func(p_callback); + const bytesPtr = resizePackedByteArrayAndOpenWrite(p_byte_arr, p_byte_arr_write, obj.length); + HEAPU8.set(obj, bytesPtr); + }, }; autoAddDeps(GodotJSWrapper, '$GodotJSWrapper'); |