Add JavaScriptBridge buffer methods

This commit is contained in:
Adam Scott 2024-08-29 09:50:18 -04:00
parent 99a7a9ccd6
commit a9b934b657
No known key found for this signature in database
GPG key ID: F6BA2A0302E21A77
5 changed files with 81 additions and 0 deletions

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