From c0b5eeef575356b05472a5a6759c9eaedbe9cd4d Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 23 Mar 2018 12:11:41 -0700 Subject: [PATCH 1/4] feat(webidl): expose arraybuffer overload in body idl --- components/script/body.rs | 3 ++- components/script/dom/request.rs | 6 ++++++ components/script/dom/response.rs | 6 ++++++ components/script/dom/webidls/Body.webidl | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/components/script/body.rs b/components/script/body.rs index 21c4d49b58c..435f755aa07 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -27,7 +27,8 @@ pub enum BodyType { Blob, FormData, Json, - Text + Text, + ArrayBuffer } pub enum FetchedData { diff --git a/components/script/dom/request.rs b/components/script/dom/request.rs index 85998533ae9..b3bb2f1afe1 100644 --- a/components/script/dom/request.rs +++ b/components/script/dom/request.rs @@ -611,6 +611,12 @@ impl RequestMethods for Request { fn Json(&self) -> Rc { consume_body(self, BodyType::Json) } + + #[allow(unrooted_must_root)] + // https://fetch.spec.whatwg.org/#dom-body-arraybuffer + fn ArrayBuffer(&self) -> Rc { + consume_body(self, BodyType::ArrayBuffer) + } } impl BodyOperations for Request { diff --git a/components/script/dom/response.rs b/components/script/dom/response.rs index e4af7f48187..ff806a5bf8f 100644 --- a/components/script/dom/response.rs +++ b/components/script/dom/response.rs @@ -354,6 +354,12 @@ impl ResponseMethods for Response { fn Json(&self) -> Rc { consume_body(self, BodyType::Json) } + + #[allow(unrooted_must_root)] + // https://fetch.spec.whatwg.org/#dom-body-arraybuffer + fn ArrayBuffer(&self) -> Rc { + consume_body(self, BodyType::ArrayBuffer) + } } fn serialize_without_fragment(url: &ServoUrl) -> &str { diff --git a/components/script/dom/webidls/Body.webidl b/components/script/dom/webidls/Body.webidl index bb7aa5c6859..1ea45746e73 100644 --- a/components/script/dom/webidls/Body.webidl +++ b/components/script/dom/webidls/Body.webidl @@ -10,7 +10,7 @@ interface Body { readonly attribute boolean bodyUsed; - // [NewObject] Promise arrayBuffer(); + [NewObject] Promise arrayBuffer(); [NewObject] Promise blob(); [NewObject] Promise formData(); [NewObject] Promise json(); From 168f61082183efdd0cb5a505f93d961d3c677ce2 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 23 Mar 2018 15:18:53 -0700 Subject: [PATCH 2/4] feat(consume_body): implement consume_body accepts arraybuffer --- components/script/body.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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), FormData(DomRoot), + ArrayBuffer(RootedTraceableBox>), } // https://fetch.spec.whatwg.org/#concept-body-consume-body @@ -84,6 +90,7 @@ pub fn consume_body_with_promise(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(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, mime: &[u8]) -> F } } +#[allow(unsafe_code)] +unsafe fn run_array_buffer_data_algorithm(cx: *mut JSContext, bytes: Vec) -> Fallible { + rooted!(in(cx) let mut array_buffer_ptr = ptr::null_mut::()); + 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, body_type: BodyType); From e6de213a13d6118a30ae5e90a91ccf81653677a7 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Sat, 24 Mar 2018 08:56:53 -0700 Subject: [PATCH 3/4] refactor(consume_body): json stored in roottraceablebox --- components/script/body.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/script/body.rs b/components/script/body.rs index 43bd0fb5c18..35ee2dae2c6 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -38,7 +38,7 @@ pub enum BodyType { pub enum FetchedData { Text(String), - Json(JSValue), + Json(RootedTraceableBox>), BlobData(DomRoot), FormData(DomRoot), ArrayBuffer(RootedTraceableBox>), @@ -137,7 +137,8 @@ fn run_json_data_algorithm(cx: *mut JSContext, // TODO: See issue #13464. Exception should be thrown instead of cleared. return Err(Error::Type("Failed to parse JSON".to_string())); } - Ok(FetchedData::Json(rval.get())) + let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(rval.get())); + Ok(FetchedData::Json(rooted_heap)) } } From 80304a133f66ba4bda0070d29a36043d8f4b4334 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Fri, 23 Mar 2018 15:19:19 -0700 Subject: [PATCH 4/4] test(wpt): update fetch test metadata --- .../fetch/api/basic/text-utf8.html.ini | 29 ----- .../request/request-consume-empty.html.ini | 6 - .../api/request/request-consume.html.ini | 9 -- .../fetch/api/request/request-idl.html.ini | 3 - .../api/request/request-structure.html.ini | 2 - .../api/response/response-consume.html.ini | 12 -- .../fetch/api/response/response-idl.html.ini | 6 - .../response-stream-disturbed-5.html.ini | 2 - .../fetch/data-urls/base64.any.js.ini | 123 ------------------ .../fetch/data-urls/processing.any.js.ini | 73 ----------- 10 files changed, 265 deletions(-) diff --git a/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini b/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini index 890e7cd7f3c..3648d23af2a 100644 --- a/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini +++ b/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini @@ -36,33 +36,4 @@ [UTF-16 without BOM decoded as UTF-8 with Response.text()] expected: FAIL - [UTF-8 with BOM (Response object)] - expected: FAIL - - [UTF-8 with BOM (Request object)] - expected: FAIL - - [UTF-8 without BOM (Response object)] - expected: FAIL - - [UTF-8 without BOM (Request object)] - expected: FAIL - - [UTF-16BE with BOM decoded as UTF-8 (Response object)] - expected: FAIL - - [UTF-16BE with BOM decoded as UTF-8 (Request object)] - expected: FAIL - - [UTF-16LE with BOM decoded as UTF-8 (Response object)] - expected: FAIL - - [UTF-16LE with BOM decoded as UTF-8 (Request object)] - expected: FAIL - - [UTF-16 without BOM decoded as UTF-8 (Response object)] - expected: FAIL - - [UTF-16 without BOM decoded as UTF-8 (Request object)] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini b/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini index 8ac3c17618d..0f807052fc9 100644 --- a/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini @@ -6,12 +6,6 @@ [Consume request's body as formData] expected: FAIL - [Consume empty blob request body as arrayBuffer] - expected: FAIL - - [Consume empty text request body as arrayBuffer] - expected: FAIL - [Consume request's body as text] expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini index c409d5b0189..611766b22ac 100644 --- a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini @@ -1,17 +1,11 @@ [request-consume.html] type: testharness - [Consume String request's body as arrayBuffer] - expected: FAIL - [Consume String request's body as json] expected: FAIL [Consume String request's body as formData] expected: FAIL - [Consume blob response's body as arrayBuffer] - expected: FAIL - [Trying to consume bad JSON text as JSON: 'undefined'] expected: FAIL @@ -51,9 +45,6 @@ [Consume FormData request's body as FormData] expected: FAIL - [Consume blob response's body as arrayBuffer] - expected: FAIL - [Trying to consume bad JSON text as JSON: 'undefined'] expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/request/request-idl.html.ini b/tests/wpt/metadata/fetch/api/request/request-idl.html.ini index dec37325cb8..f233e7214f9 100644 --- a/tests/wpt/metadata/fetch/api/request/request-idl.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-idl.html.ini @@ -30,6 +30,3 @@ [Request interface: new Request("") must inherit property "body" with the proper type] expected: FAIL - [Request interface: new Request("") must inherit property "arrayBuffer()" with the proper type] - expected: FAIL - diff --git a/tests/wpt/metadata/fetch/api/request/request-structure.html.ini b/tests/wpt/metadata/fetch/api/request/request-structure.html.ini index aa3c47b9ad4..5e5f5ed9b56 100644 --- a/tests/wpt/metadata/fetch/api/request/request-structure.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-structure.html.ini @@ -1,5 +1,3 @@ [request-structure.html] type: testharness - [Request has arrayBuffer method] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/response/response-consume.html.ini b/tests/wpt/metadata/fetch/api/response/response-consume.html.ini index de25ae08c7a..e3efe47c53b 100644 --- a/tests/wpt/metadata/fetch/api/response/response-consume.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-consume.html.ini @@ -24,15 +24,9 @@ [Consume fetched response's body as arrayBuffer] expected: FAIL - [Consume response's body: from text to arrayBuffer] - expected: FAIL - [Consume response's body: from text with correct multipart type to formData] expected: FAIL - [Consume response's body: from blob to arrayBuffer] - expected: FAIL - [Consume response's body: from blob with correct multipart type to formData] expected: FAIL @@ -48,9 +42,6 @@ [Consume response's body: from FormData to arrayBuffer] expected: FAIL - [Consume response's body: from URLSearchParams to arrayBuffer] - expected: FAIL - [Consume response's body: from stream to blob] expected: FAIL @@ -78,9 +69,6 @@ [Consume response's body: from fetch to blob] expected: FAIL - [Consume response's body: from fetch to arrayBuffer] - expected: FAIL - [Consume response's body: from multipart form data blob to formData] expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/response/response-idl.html.ini b/tests/wpt/metadata/fetch/api/response/response-idl.html.ini index d94659d4f16..e0eb81482e9 100644 --- a/tests/wpt/metadata/fetch/api/response/response-idl.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-idl.html.ini @@ -21,9 +21,6 @@ [Response interface: new Response() must inherit property "body" with the proper type (8)] expected: FAIL - [Response interface: new Response() must inherit property "arrayBuffer" with the proper type (11)] - expected: FAIL - [Response interface: new Response() must inherit property "body" with the proper type (9)] expected: FAIL @@ -45,6 +42,3 @@ [Response interface: new Response() must inherit property "body" with the proper type] expected: FAIL - [Response interface: new Response() must inherit property "arrayBuffer()" with the proper type] - expected: FAIL - diff --git a/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini b/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini index 31c366a0035..f70ac4f2ddc 100644 --- a/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini @@ -1,5 +1,3 @@ [response-stream-disturbed-5.html] type: testharness - [Getting a body reader after consuming as arrayBuffer] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini b/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini index 2aa2f25c9a1..7b7a199b2bd 100644 --- a/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini +++ b/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini @@ -1,46 +1,8 @@ [base64.any.worker.html] - [data: URL base64 handling: ""] - expected: FAIL - - [data: URL base64 handling: "abcd"] - expected: FAIL - - [data: URL base64 handling: " abcd"] - expected: FAIL - - [data: URL base64 handling: "abcd "] - expected: FAIL - - [data: URL base64 handling: "ab"] - expected: FAIL - - [data: URL base64 handling: "abc"] - expected: FAIL - [data: URL base64 handling: "ab="] expected: FAIL - - [data: URL base64 handling: "ab=="] - expected: FAIL - - [data: URL base64 handling: "abc="] - expected: FAIL - - [data: URL base64 handling: "ab\\tcd"] - expected: FAIL - - [data: URL base64 handling: "ab\\ncd"] - expected: FAIL - [data: URL base64 handling: "ab\\fcd"] expected: FAIL - - [data: URL base64 handling: "ab\\rcd"] - expected: FAIL - - [data: URL base64 handling: "ab cd"] - expected: FAIL - [data: URL base64 handling: "ab\\t\\n\\f\\r cd"] expected: FAIL @@ -50,74 +12,12 @@ [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] expected: FAIL - [data: URL base64 handling: "/A"] - expected: FAIL - - [data: URL base64 handling: "//A"] - expected: FAIL - - [data: URL base64 handling: "///A"] - expected: FAIL - - [data: URL base64 handling: "A/"] - expected: FAIL - - [data: URL base64 handling: "AA/"] - expected: FAIL - - [data: URL base64 handling: "AAA/"] - expected: FAIL - - [data: URL base64 handling: "YQ"] - expected: FAIL - - [data: URL base64 handling: "YR"] - expected: FAIL - [base64.any.html] - [data: URL base64 handling: ""] - expected: FAIL - - [data: URL base64 handling: "abcd"] - expected: FAIL - - [data: URL base64 handling: " abcd"] - expected: FAIL - - [data: URL base64 handling: "abcd "] - expected: FAIL - - [data: URL base64 handling: "ab"] - expected: FAIL - - [data: URL base64 handling: "abc"] - expected: FAIL - [data: URL base64 handling: "ab="] expected: FAIL - - [data: URL base64 handling: "ab=="] - expected: FAIL - - [data: URL base64 handling: "abc="] - expected: FAIL - - [data: URL base64 handling: "ab\\tcd"] - expected: FAIL - - [data: URL base64 handling: "ab\\ncd"] - expected: FAIL - [data: URL base64 handling: "ab\\fcd"] expected: FAIL - - [data: URL base64 handling: "ab\\rcd"] - expected: FAIL - - [data: URL base64 handling: "ab cd"] - expected: FAIL - [data: URL base64 handling: "ab\\t\\n\\f\\r cd"] expected: FAIL @@ -127,27 +27,4 @@ [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] expected: FAIL - [data: URL base64 handling: "/A"] - expected: FAIL - - [data: URL base64 handling: "//A"] - expected: FAIL - - [data: URL base64 handling: "///A"] - expected: FAIL - - [data: URL base64 handling: "A/"] - expected: FAIL - - [data: URL base64 handling: "AA/"] - expected: FAIL - - [data: URL base64 handling: "AAA/"] - expected: FAIL - - [data: URL base64 handling: "YQ"] - expected: FAIL - - [data: URL base64 handling: "YR"] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini b/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini index 1660d360fd4..1186217c000 100644 --- a/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini +++ b/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini @@ -14,12 +14,8 @@ ["data:,%FF"] expected: FAIL - ["data:text/plain,X"] - expected: FAIL - ["data:text/plain ,X"] expected: FAIL - ["data:text/plain%20,X"] expected: FAIL @@ -29,9 +25,6 @@ ["data:text/plain%0C,X"] expected: FAIL - ["data:text/plain;,X"] - expected: FAIL - ["data:;x=x;charset=x,X"] expected: FAIL @@ -44,12 +37,6 @@ ["data:text/plain;Charset=UTF-8,%C2%B1"] expected: FAIL - ["data:image/gif,%C2%B1"] - expected: FAIL - - ["data:IMAGE/gif,%C2%B1"] - expected: FAIL - ["data:IMAGE/gif;hi=x,%C2%B1"] expected: FAIL @@ -89,15 +76,6 @@ ["data:X,X"] expected: FAIL - ["data:image/png,X X"] - expected: FAIL - - ["data:application/xml,X X"] - expected: FAIL - - ["data:unknown/unknown,X X"] - expected: FAIL - ["data:text/plain;a=\\",\\",X"] expected: FAIL @@ -107,18 +85,12 @@ ["data:;base64;base64,WA"] expected: FAIL - ["data:x/x;base64;base64,WA"] - expected: FAIL - ["data:x/x;base64;charset=x,WA"] expected: FAIL ["data:x/x;base64;charset=x;base64,WA"] expected: FAIL - ["data:x/x;base64;base64x,WA"] - expected: FAIL - ["data:;base64,W%20A"] expected: FAIL @@ -188,15 +160,6 @@ ["data:;CHARSET=\\"X\\",X"] expected: FAIL - ["data:application/javascript,X X"] - expected: FAIL - - ["data:text/javascript,X X"] - expected: FAIL - - ["data:text/plain,X X"] - expected: FAIL - [processing.any.html] ["data://test/,X"] @@ -214,9 +177,6 @@ ["data:,%FF"] expected: FAIL - ["data:text/plain,X"] - expected: FAIL - ["data:text/plain ,X"] expected: FAIL @@ -229,9 +189,6 @@ ["data:text/plain%0C,X"] expected: FAIL - ["data:text/plain;,X"] - expected: FAIL - ["data:;x=x;charset=x,X"] expected: FAIL @@ -244,12 +201,6 @@ ["data:text/plain;Charset=UTF-8,%C2%B1"] expected: FAIL - ["data:image/gif,%C2%B1"] - expected: FAIL - - ["data:IMAGE/gif,%C2%B1"] - expected: FAIL - ["data:IMAGE/gif;hi=x,%C2%B1"] expected: FAIL @@ -289,15 +240,6 @@ ["data:X,X"] expected: FAIL - ["data:image/png,X X"] - expected: FAIL - - ["data:application/xml,X X"] - expected: FAIL - - ["data:unknown/unknown,X X"] - expected: FAIL - ["data:text/plain;a=\\",\\",X"] expected: FAIL @@ -307,18 +249,12 @@ ["data:;base64;base64,WA"] expected: FAIL - ["data:x/x;base64;base64,WA"] - expected: FAIL - ["data:x/x;base64;charset=x,WA"] expected: FAIL ["data:x/x;base64;charset=x;base64,WA"] expected: FAIL - ["data:x/x;base64;base64x,WA"] - expected: FAIL - ["data:;base64,W%20A"] expected: FAIL @@ -388,12 +324,3 @@ ["data:;CHARSET=\\"X\\",X"] expected: FAIL - ["data:application/javascript,X X"] - expected: FAIL - - ["data:text/javascript,X X"] - expected: FAIL - - ["data:text/plain,X X"] - expected: FAIL -