servo: Merge #14927 - Use the typed array APIs in ImageData (from servo:ImageData-data); r=jdm
authorMs2ger <Ms2ger@gmail.com>
Mon, 09 Jan 2017 09:29:46 -0800
changeset 340520 711b089b7cf97cc828c17a10897952d3ff08274a
parent 340519 affe1a63afa4052f06f06c90a331c11a4979497f
child 340521 785904cba0107cbf31b3d8ffd0cef3c9e08806d5
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
servo: Merge #14927 - Use the typed array APIs in ImageData (from servo:ImageData-data); r=jdm Source-Repo: https://github.com/servo/servo Source-Revision: 9482467addebf6469f86331b34aaf7aab72fa0eb
servo/components/script/dom/imagedata.rs
--- a/servo/components/script/dom/imagedata.rs
+++ b/servo/components/script/dom/imagedata.rs
@@ -5,21 +5,20 @@
 use core::nonzero::NonZero;
 use dom::bindings::codegen::Bindings::ImageDataBinding;
 use dom::bindings::codegen::Bindings::ImageDataBinding::ImageDataMethods;
 use dom::bindings::js::Root;
 use dom::bindings::reflector::{Reflector, reflect_dom_object};
 use dom::globalscope::GlobalScope;
 use euclid::size::Size2D;
 use js::jsapi::{Heap, JSContext, JSObject};
-use js::jsapi::{JS_GetUint8ClampedArrayData, JS_NewUint8ClampedArray};
-use libc::uint8_t;
+use js::rust::Runtime;
+use js::typedarray::Uint8ClampedArray;
 use std::default::Default;
 use std::ptr;
-use std::slice;
 use std::vec::Vec;
 
 #[dom_struct]
 pub struct ImageData {
     reflector_: Reflector,
     width: u32,
     height: u32,
     data: Heap<*mut JSObject>,
@@ -32,43 +31,35 @@ impl ImageData {
             reflector_: Reflector::new(),
             width: width,
             height: height,
             data: Heap::default(),
         };
 
         unsafe {
             let cx = global.get_cx();
-            let js_object: *mut JSObject = JS_NewUint8ClampedArray(cx, width * height * 4);
-            assert!(!js_object.is_null());
-
-            if let Some(vec) = data {
-                let mut is_shared = false;
-                let js_object_data: *mut uint8_t = JS_GetUint8ClampedArrayData(js_object, &mut is_shared, ptr::null());
-                assert!(!is_shared);
-                ptr::copy_nonoverlapping(vec.as_ptr(), js_object_data, vec.len())
-            }
-            (*imagedata).data.set(js_object);
+            rooted!(in (cx) let mut js_object = ptr::null_mut());
+            let data = data.as_ref().map(|d| &d[..]);
+            Uint8ClampedArray::create(cx, width * height * 4, data, js_object.handle_mut()).unwrap();
+            (*imagedata).data.set(js_object.get());
         }
 
         reflect_dom_object(imagedata,
                            global, ImageDataBinding::Wrap)
     }
 
     #[allow(unsafe_code)]
     pub fn get_data_array(&self) -> Vec<u8> {
         unsafe {
-            let mut is_shared = false;
             assert!(!self.data.get().is_null());
-            let data: *const uint8_t =
-                JS_GetUint8ClampedArrayData(self.data.get(), &mut is_shared, ptr::null()) as *const uint8_t;
-            assert!(!data.is_null());
-            assert!(!is_shared);
-            let len = self.Width() * self.Height() * 4;
-            slice::from_raw_parts(data, len as usize).to_vec()
+            let cx = Runtime::get();
+            assert!(!cx.is_null());
+            typedarray!(in(cx) let array: Uint8ClampedArray = self.data.get());
+            let vec = array.unwrap().as_slice().to_vec();
+            vec
         }
     }
 
     pub fn get_size(&self) -> Size2D<i32> {
         Size2D::new(self.Width() as i32, self.Height() as i32)
     }
 }