Merge pull request #96286 from adamscott/add-js_buffer_to_packed_byte_array

Add `JavaScriptBridge` buffer methods
This commit is contained in:
Rémi Verschelde 2024-09-17 09:17:42 +02:00
commit 5fc786911f
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 81 additions and 0 deletions

View File

@ -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.
</description>
</method>
<method name="is_js_buffer">
<return type="bool" />
<param index="0" name="javascript_object" type="JavaScriptObject" />
<description>
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].
</description>
</method>
<method name="js_buffer_to_packed_byte_array">
<return type="PackedByteArray" />
<param index="0" name="javascript_buffer" type="JavaScriptObject" />
<description>
Returns a copy of [param javascript_buffer]'s contents as a [PackedByteArray]. See also [method is_js_buffer].
</description>
</method>
<method name="pwa_needs_update" qualifiers="const">
<return type="bool" />
<description>

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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');