diff --git a/doc/classes/JavaScriptBridge.xml b/doc/classes/JavaScriptBridge.xml
index faf5424c470..ea3ede8857d 100644
--- a/doc/classes/JavaScriptBridge.xml
+++ b/doc/classes/JavaScriptBridge.xml
@@ -60,6 +60,20 @@
Returns an interface to a JavaScript object that can be used by scripts. The [param interface] must be a valid property of the JavaScript [code]window[/code]. The callback must accept a single [Array] argument, which will contain the JavaScript [code]arguments[/code]. See [JavaScriptObject] for usage.
+
+
+
+
+ Returns [code]true[/code] if the given [param javascript_object] is of type [url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer][code]ArrayBuffer[/code][/url], [url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView][code]DataView[/code][/url], or one of the many [url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray]typed array objects[/url].
+
+
+
+
+
+
+ Returns a copy of [param javascript_buffer]'s contents as a [PackedByteArray]. See also [method is_js_buffer].
+
+
diff --git a/platform/web/api/api.cpp b/platform/web/api/api.cpp
index 9ddbe5d01d3..40417bde7e7 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 JavaScriptBridge::create_callback(const Callable &p_callab
return Ref();
}
+bool JavaScriptBridge::is_js_buffer(Ref p_js_obj) {
+ return false;
+}
+
+PackedByteArray JavaScriptBridge::js_buffer_to_packed_byte_array(Ref 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 456fa6b3132..80544400554 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 get_interface(const String &p_interface);
Ref create_callback(const Callable &p_callable);
+ bool is_js_buffer(Ref p_js_obj);
+ PackedByteArray js_buffer_to_packed_byte_array(Ref p_js_obj);
Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
void download_buffer(Vector 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 502e830f824..869c2e9903e 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 p_js_obj) {
+ Ref 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 p_js_obj) {
+ ERR_FAIL_COND_V_MSG(!is_js_buffer(p_js_obj), PackedByteArray(), "The JavaScript object is not a buffer.");
+ Ref obj = p_js_obj;
+
+ PackedByteArray arr;
+ VectorWriteProxy arr_write;
+
+ godot_js_wrapper_object_transfer_buffer(obj->_js_id, &arr, &arr_write, resize_PackedByteArray_and_open_write);
+
+ arr_write = VectorWriteProxy();
+ return arr;
+}
+
#endif // JAVASCRIPT_EVAL_ENABLED
void JavaScriptBridge::download_buffer(Vector 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 6bb69bca95c..3ff6abba600 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');