diff options
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/imagedata.rs | 105 |
1 files changed, 55 insertions, 50 deletions
diff --git a/components/script/dom/imagedata.rs b/components/script/dom/imagedata.rs index 6eb5bfd3489..d89e5e36d1b 100644 --- a/components/script/dom/imagedata.rs +++ b/components/script/dom/imagedata.rs @@ -42,15 +42,14 @@ impl ImageData { unsafe { let cx = global.get_cx(); rooted!(in (*cx) let mut js_object = ptr::null_mut::<JSObject>()); - let data = match data { - Some(ref mut d) => { - d.resize(len as usize, 0); - CreateWith::Slice(&d[..]) - }, - None => CreateWith::Length(len), - }; - Uint8ClampedArray::create(*cx, data, js_object.handle_mut()).unwrap(); - Self::new_with_jsobject(global, width, Some(height), Some(js_object.get())) + if let Some(ref mut d) = data { + d.resize(len as usize, 0); + let data = CreateWith::Slice(&d[..]); + Uint8ClampedArray::create(*cx, data, js_object.handle_mut()).unwrap(); + Self::new_with_jsobject(global, width, Some(height), js_object.get()) + } else { + Self::new_without_jsobject(global, width, height) + } } } @@ -58,43 +57,54 @@ impl ImageData { unsafe fn new_with_jsobject( global: &GlobalScope, width: u32, - mut opt_height: Option<u32>, - opt_jsobject: Option<*mut JSObject>, + opt_height: Option<u32>, + jsobject: *mut JSObject, ) -> Fallible<DomRoot<ImageData>> { - assert!(opt_jsobject.is_some() || opt_height.is_some()); + // checking jsobject type + let cx = global.get_cx(); + typedarray!(in(*cx) let array_res: Uint8ClampedArray = jsobject); + let array = array_res.map_err(|_| { + Error::Type("Argument to Image data is not an Uint8ClampedArray".to_owned()) + })?; + + let byte_len = array.as_slice().len() as u32; + if byte_len == 0 || byte_len % 4 != 0 { + return Err(Error::InvalidState); + } - if width == 0 { + let len = byte_len / 4; + if width == 0 || len % width != 0 { return Err(Error::IndexSize); } - // checking jsobject type and verifying (height * width * 4 == jsobject.byte_len()) - if let Some(jsobject) = opt_jsobject { - let cx = global.get_cx(); - typedarray!(in(*cx) let array_res: Uint8ClampedArray = jsobject); - let array = array_res.map_err(|_| { - Error::Type("Argument to Image data is not an Uint8ClampedArray".to_owned()) - })?; - - let byte_len = array.as_slice().len() as u32; - if byte_len % 4 != 0 || byte_len == 0 { - return Err(Error::InvalidState); - } + let height = len / width; + if opt_height.map_or(false, |x| height != x) { + return Err(Error::IndexSize); + } - let len = byte_len / 4; - if width == 0 || len % width != 0 { - return Err(Error::IndexSize); - } + let imagedata = Box::new(ImageData { + reflector_: Reflector::new(), + width: width, + height: height, + data: Heap::default(), + }); - let height = len / width; - if opt_height.map_or(false, |x| height != x) { - return Err(Error::IndexSize); - } else { - opt_height = Some(height); - } - } + (*imagedata).data.set(jsobject); - let height = opt_height.unwrap(); - if height == 0 { + Ok(reflect_dom_object( + imagedata, + global, + ImageDataBinding::Wrap, + )) + } + + #[allow(unsafe_code)] + unsafe fn new_without_jsobject( + global: &GlobalScope, + width: u32, + height: u32, + ) -> Fallible<DomRoot<ImageData>> { + if width == 0 || height == 0 { return Err(Error::IndexSize); } @@ -105,15 +115,11 @@ impl ImageData { data: Heap::default(), }); - if let Some(jsobject) = opt_jsobject { - (*imagedata).data.set(jsobject); - } else { - let len = width * height * 4; - let cx = global.get_cx(); - rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>()); - Uint8ClampedArray::create(*cx, CreateWith::Length(len), array.handle_mut()).unwrap(); - (*imagedata).data.set(array.get()); - } + let len = width * height * 4; + let cx = global.get_cx(); + rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>()); + Uint8ClampedArray::create(*cx, CreateWith::Length(len), array.handle_mut()).unwrap(); + (*imagedata).data.set(array.get()); Ok(reflect_dom_object( imagedata, @@ -121,11 +127,10 @@ impl ImageData { ImageDataBinding::Wrap, )) } - // https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-3 #[allow(unsafe_code)] pub fn Constructor(global: &GlobalScope, width: u32, height: u32) -> Fallible<DomRoot<Self>> { - unsafe { Self::new_with_jsobject(global, width, Some(height), None) } + unsafe { Self::new_without_jsobject(global, width, height) } } // https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-4 @@ -138,7 +143,7 @@ impl ImageData { width: u32, opt_height: Option<u32>, ) -> Fallible<DomRoot<Self>> { - Self::new_with_jsobject(global, width, opt_height, Some(jsobject)) + Self::new_with_jsobject(global, width, opt_height, jsobject) } /// Nothing must change the array on the JS side while the slice is live. |