diff options
Diffstat (limited to 'components/script/body.rs')
-rw-r--r-- | components/script/body.rs | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/components/script/body.rs b/components/script/body.rs index 8546b3f9cd7..e65b6342d6e 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -12,7 +12,7 @@ use js::jsapi::{Heap, JSObject, JS_ClearPendingException, Value as JSValue}; use js::jsval::{JSVal, UndefinedValue}; use js::rust::wrappers::{JS_GetPendingException, JS_ParseJSON}; use js::rust::HandleValue; -use js::typedarray::{ArrayBuffer, CreateWith}; +use js::typedarray::{ArrayBufferU8, Uint8}; use mime::{self, Mime}; use net_traits::request::{ BodyChunkRequest, BodyChunkResponse, BodySource as NetBodySource, RequestBody, @@ -20,6 +20,7 @@ use net_traits::request::{ use script_traits::serializable::BlobImpl; use url::form_urlencoded; +use crate::dom::bindings::buffer_source::create_buffer_source; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::BlobBinding::Blob_Binding::BlobMethods; use crate::dom::bindings::codegen::Bindings::FormDataBinding::FormDataMethods; @@ -572,6 +573,7 @@ impl Extractable for URLSearchParams { #[derive(Clone, Copy, JSTraceable, MallocSizeOf)] pub(crate) enum BodyType { Blob, + Bytes, FormData, Json, Text, @@ -582,6 +584,7 @@ pub(crate) enum FetchedData { Text(String), Json(RootedTraceableBox<Heap<JSValue>>), BlobData(DomRoot<Blob>), + Bytes(RootedTraceableBox<Heap<*mut JSObject>>), FormData(DomRoot<FormData>), ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>), JSException(RootedTraceableBox<Heap<JSVal>>), @@ -633,6 +636,7 @@ impl ConsumeBodyPromiseHandler { FetchedData::Json(j) => self.result_promise.resolve_native(&j), FetchedData::BlobData(b) => self.result_promise.resolve_native(&b), FetchedData::FormData(f) => self.result_promise.resolve_native(&f), + FetchedData::Bytes(b) => self.result_promise.resolve_native(&b), FetchedData::ArrayBuffer(a) => self.result_promise.resolve_native(&a), FetchedData::JSException(e) => self.result_promise.reject_native(&e.handle()), }; @@ -810,6 +814,7 @@ fn run_package_data_algorithm( BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime, can_gc), BodyType::FormData => run_form_data_algorithm(&global, bytes, mime, can_gc), BodyType::ArrayBuffer => run_array_buffer_data_algorithm(cx, bytes), + BodyType::Bytes => run_bytes_data_algorithm(cx, bytes), } } @@ -891,22 +896,25 @@ fn run_form_data_algorithm( Err(Error::Type("Inappropriate MIME-type for Body".to_string())) } -#[allow(unsafe_code)] +fn run_bytes_data_algorithm(cx: JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> { + rooted!(in(*cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>()); + + create_buffer_source::<Uint8>(cx, &bytes, array_buffer_ptr.handle_mut()) + .map_err(|_| Error::JSFailed)?; + + let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get())); + Ok(FetchedData::Bytes(rooted_heap)) +} + pub(crate) fn run_array_buffer_data_algorithm( cx: JSContext, bytes: Vec<u8>, ) -> Fallible<FetchedData> { rooted!(in(*cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>()); - let arraybuffer = unsafe { - ArrayBuffer::create( - *cx, - CreateWith::Slice(&bytes), - array_buffer_ptr.handle_mut(), - ) - }; - if arraybuffer.is_err() { - return Err(Error::JSFailed); - } + + create_buffer_source::<ArrayBufferU8>(cx, &bytes, array_buffer_ptr.handle_mut()) + .map_err(|_| Error::JSFailed)?; + let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get())); Ok(FetchedData::ArrayBuffer(rooted_heap)) } |