servo: Merge #15510 - Rewrite TextEncoder::Encode to use typed array API. Fixes #15504 (from absoludity:master); r=SimonSapin
authorMichael Nelson <absoludity@gmail.com>
Sun, 12 Feb 2017 13:13:47 -0800
changeset 343169 4b84a317c7517d5be38bd268076bc32611ca8296
parent 343168 d2b73aaa759a7d22aabc6d16fa257a7d1ac9638e
child 343170 8e7bb2f3d4d91f12a16a9eeb9e05a00d59af1475
push id31371
push usercbook@mozilla.com
push dateThu, 16 Feb 2017 12:15:11 +0000
treeherdermozilla-central@8c8b54b13be7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersSimonSapin
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
servo: Merge #15510 - Rewrite TextEncoder::Encode to use typed array API. Fixes #15504 (from absoludity:master); r=SimonSapin <!-- Please describe your changes on the following line: --> Rewrite TextEncoder::Encode to use typed array API. Fixes #15504 --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #15504 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [X] These changes do not require tests because the existing tests ensure the behaviour has not changed - `./mach test-wpt tests/wpt/web-platform-tests/encoding` <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: a656782075afb1fbc55e3f0f8bdb5a58010f3af3
servo/components/script/dom/textencoder.rs
--- a/servo/components/script/dom/textencoder.rs
+++ b/servo/components/script/dom/textencoder.rs
@@ -9,18 +9,17 @@ use dom::bindings::error::Fallible;
 use dom::bindings::js::Root;
 use dom::bindings::reflector::{Reflector, reflect_dom_object};
 use dom::bindings::str::{DOMString, USVString};
 use dom::globalscope::GlobalScope;
 use encoding::EncoderTrap;
 use encoding::Encoding;
 use encoding::all::UTF_8;
 use js::jsapi::{JSContext, JSObject};
-use js::jsapi::{JS_GetUint8ArrayData, JS_NewUint8Array};
-use libc::uint8_t;
+use js::typedarray::Uint8Array;
 use std::ptr;
 
 #[dom_struct]
 pub struct TextEncoder {
     reflector_: Reflector,
 }
 
 impl TextEncoder {
@@ -47,18 +46,15 @@ impl TextEncoderMethods for TextEncoder 
     fn Encoding(&self) -> DOMString {
         DOMString::from(UTF_8.name())
     }
 
     #[allow(unsafe_code)]
     // https://encoding.spec.whatwg.org/#dom-textencoder-encode
     unsafe fn Encode(&self, cx: *mut JSContext, input: USVString) -> NonZero<*mut JSObject> {
         let encoded = UTF_8.encode(&input.0, EncoderTrap::Strict).unwrap();
-        let length = encoded.len() as u32;
-        rooted!(in(cx) let js_object = JS_NewUint8Array(cx, length));
-        assert!(!js_object.is_null());
-        let mut is_shared = false;
-        let js_object_data: *mut uint8_t = JS_GetUint8ArrayData(js_object.get(), &mut is_shared, ptr::null());
-        assert!(!is_shared);
-        ptr::copy_nonoverlapping(encoded.as_ptr(), js_object_data, length as usize);
+
+        rooted!(in(cx) let mut js_object = ptr::null_mut());
+        assert!(Uint8Array::create(cx, encoded.len() as u32, Some(encoded.as_slice()), js_object.handle_mut()).is_ok());
+
         NonZero::new(js_object.get())
     }
 }