aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/body.rs
diff options
context:
space:
mode:
authorOJ Kwon <kwon.ohjoong@gmail.com>2018-03-23 15:18:53 -0700
committerOJ Kwon <kwon.ohjoong@gmail.com>2018-03-24 08:33:01 -0700
commit168f61082183efdd0cb5a505f93d961d3c677ce2 (patch)
tree0ff4467238290fc9c85f61e78a8cc03254df4302 /components/script/body.rs
parentc0b5eeef575356b05472a5a6759c9eaedbe9cd4d (diff)
downloadservo-168f61082183efdd0cb5a505f93d961d3c677ce2.tar.gz
servo-168f61082183efdd0cb5a505f93d961d3c677ce2.zip
feat(consume_body): implement consume_body accepts arraybuffer
Diffstat (limited to 'components/script/body.rs')
-rw-r--r--components/script/body.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/components/script/body.rs b/components/script/body.rs
index 435f755aa07..43bd0fb5c18 100644
--- a/components/script/body.rs
+++ b/components/script/body.rs
@@ -7,17 +7,22 @@ use dom::bindings::error::{Error, Fallible};
use dom::bindings::reflector::DomObject;
use dom::bindings::root::DomRoot;
use dom::bindings::str::USVString;
+use dom::bindings::trace::RootedTraceableBox;
use dom::blob::{Blob, BlobImpl};
use dom::formdata::FormData;
use dom::globalscope::GlobalScope;
use dom::promise::Promise;
+use js::jsapi::Heap;
use js::jsapi::JSContext;
+use js::jsapi::JSObject;
use js::jsapi::JS_ClearPendingException;
use js::jsapi::JS_ParseJSON;
use js::jsapi::Value as JSValue;
use js::jsval::UndefinedValue;
+use js::typedarray::{ArrayBuffer, CreateWith};
use mime::{Mime, TopLevel, SubLevel};
use std::cell::Ref;
+use std::ptr;
use std::rc::Rc;
use std::str;
use url::form_urlencoded;
@@ -36,6 +41,7 @@ pub enum FetchedData {
Json(JSValue),
BlobData(DomRoot<Blob>),
FormData(DomRoot<FormData>),
+ ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>),
}
// https://fetch.spec.whatwg.org/#concept-body-consume-body
@@ -84,6 +90,7 @@ pub fn consume_body_with_promise<T: BodyOperations + DomObject>(object: &T,
FetchedData::Json(j) => promise.resolve_native(&j),
FetchedData::BlobData(b) => promise.resolve_native(&b),
FetchedData::FormData(f) => promise.resolve_native(&f),
+ FetchedData::ArrayBuffer(a) => promise.resolve_native(&a)
};
},
Err(err) => promise.reject_error(err),
@@ -105,6 +112,9 @@ fn run_package_data_algorithm<T: BodyOperations + DomObject>(object: &T,
BodyType::Json => run_json_data_algorithm(cx, bytes),
BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime),
BodyType::FormData => run_form_data_algorithm(&global, bytes, mime),
+ BodyType::ArrayBuffer => unsafe {
+ run_array_buffer_data_algorithm(cx, bytes)
+ }
}
}
@@ -167,6 +177,17 @@ fn run_form_data_algorithm(root: &GlobalScope, bytes: Vec<u8>, mime: &[u8]) -> F
}
}
+#[allow(unsafe_code)]
+unsafe fn run_array_buffer_data_algorithm(cx: *mut JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> {
+ rooted!(in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
+ let arraybuffer = ArrayBuffer::create(cx, CreateWith::Slice(&bytes), array_buffer_ptr.handle_mut());
+ if arraybuffer.is_err() {
+ return Err(Error::JSFailed);
+ }
+ let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
+ Ok(FetchedData::ArrayBuffer(rooted_heap))
+}
+
pub trait BodyOperations {
fn get_body_used(&self) -> bool;
fn set_body_promise(&self, p: &Rc<Promise>, body_type: BodyType);