WebIDL: Use ArrayBuffer instead of raw JSObject in bindings (#31202)

* WebIDL: Use ArrayBuffer instead of raw JSObject in bindings

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Convert GPUBufferMapInfo mapping to Arc<Mutex>

* Remove #[allow(unsafe_code)] from GPUBuffer

* Add doc comments

* Implement trace for Arc<Mutex<Vec<T>>>

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Use #[no_trace] for GPUBufferMapInfo.mapping

* Make create_new_external_array_buffer generic

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Address review comments

* Remove HeapTypedArray::new and avoid cloning Arc

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Use expect for GetMappedRange and ReadAsArrayBuffer

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Use doc comments for FileReaderSyncMethods

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Return for Error::JsFailed GetMappedRange and ReadAsArrayBuffer

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Fix detached_internal implementation and comments

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* format code

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Update expectations

---------

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
Co-authored-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Taym Haddadi 2024-02-13 08:58:48 +01:00 committed by GitHub
parent e6baa26ff8
commit 9be989146d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 121 additions and 75 deletions

View file

@ -3,12 +3,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::ptr;
use std::ptr::NonNull;
use dom_struct::dom_struct;
use js::jsapi::JSObject;
use js::rust::HandleObject;
use js::typedarray::{ArrayBuffer, CreateWith};
use js::typedarray::{ArrayBuffer, ArrayBufferU8};
use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use crate::dom::bindings::codegen::Bindings::FileReaderSyncBinding::FileReaderSyncMethods;
@ -16,6 +15,7 @@ use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::typedarrays::create_typed_array;
use crate::dom::blob::Blob;
use crate::dom::filereader::FileReaderSharedFunctionality;
use crate::dom::globalscope::GlobalScope;
@ -51,7 +51,7 @@ impl FileReaderSync {
}
impl FileReaderSyncMethods for FileReaderSync {
// https://w3c.github.io/FileAPI/#readAsBinaryStringSyncSection
/// <https://w3c.github.io/FileAPI/#readAsBinaryStringSyncSection>
fn ReadAsBinaryString(&self, blob: &Blob) -> Fallible<DOMString> {
// step 1
let blob_contents = FileReaderSync::get_blob_bytes(blob)?;
@ -60,7 +60,7 @@ impl FileReaderSyncMethods for FileReaderSync {
Ok(DOMString::from(String::from_utf8_lossy(&blob_contents)))
}
// https://w3c.github.io/FileAPI/#readAsTextSync
/// <https://w3c.github.io/FileAPI/#readAsTextSync>
fn ReadAsText(&self, blob: &Blob, label: Option<DOMString>) -> Fallible<DOMString> {
// step 1
let blob_contents = FileReaderSync::get_blob_bytes(blob)?;
@ -75,7 +75,7 @@ impl FileReaderSyncMethods for FileReaderSync {
Ok(output)
}
// https://w3c.github.io/FileAPI/#readAsDataURLSync-section
/// <https://w3c.github.io/FileAPI/#readAsDataURLSync-section>
fn ReadAsDataURL(&self, blob: &Blob) -> Fallible<DOMString> {
// step 1
let blob_contents = FileReaderSync::get_blob_bytes(blob)?;
@ -87,23 +87,15 @@ impl FileReaderSyncMethods for FileReaderSync {
Ok(output)
}
#[allow(unsafe_code)]
// https://w3c.github.io/FileAPI/#readAsArrayBufferSyncSection
fn ReadAsArrayBuffer(&self, cx: JSContext, blob: &Blob) -> Fallible<NonNull<JSObject>> {
/// <https://w3c.github.io/FileAPI/#readAsArrayBufferSyncSection>
fn ReadAsArrayBuffer(&self, cx: JSContext, blob: &Blob) -> Fallible<ArrayBuffer> {
// step 1
let blob_contents = FileReaderSync::get_blob_bytes(blob)?;
// step 2
unsafe {
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
assert!(ArrayBuffer::create(
*cx,
CreateWith::Slice(&blob_contents),
array_buffer.handle_mut()
)
.is_ok());
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
Ok(NonNull::new_unchecked(array_buffer.get()))
}
create_typed_array::<ArrayBufferU8>(cx, &blob_contents, array_buffer.handle_mut())
.map_err(|_| Error::JSFailed)
}
}