mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Refactor constructor code to align with spec.
This commit is contained in:
parent
7d7c46cbc9
commit
7cd6a8c682
1 changed files with 57 additions and 52 deletions
|
@ -42,15 +42,14 @@ impl ImageData {
|
||||||
unsafe {
|
unsafe {
|
||||||
let cx = global.get_cx();
|
let cx = global.get_cx();
|
||||||
rooted!(in (*cx) let mut js_object = ptr::null_mut::<JSObject>());
|
rooted!(in (*cx) let mut js_object = ptr::null_mut::<JSObject>());
|
||||||
let data = match data {
|
if let Some(ref mut d) = data {
|
||||||
Some(ref mut d) => {
|
|
||||||
d.resize(len as usize, 0);
|
d.resize(len as usize, 0);
|
||||||
CreateWith::Slice(&d[..])
|
let data = CreateWith::Slice(&d[..]);
|
||||||
},
|
|
||||||
None => CreateWith::Length(len),
|
|
||||||
};
|
|
||||||
Uint8ClampedArray::create(*cx, data, js_object.handle_mut()).unwrap();
|
Uint8ClampedArray::create(*cx, data, js_object.handle_mut()).unwrap();
|
||||||
Self::new_with_jsobject(global, width, Some(height), Some(js_object.get()))
|
Self::new_with_jsobject(global, width, Some(height), js_object.get())
|
||||||
|
} else {
|
||||||
|
Self::new_without_jsobject(global, width, height)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,17 +57,10 @@ impl ImageData {
|
||||||
unsafe fn new_with_jsobject(
|
unsafe fn new_with_jsobject(
|
||||||
global: &GlobalScope,
|
global: &GlobalScope,
|
||||||
width: u32,
|
width: u32,
|
||||||
mut opt_height: Option<u32>,
|
opt_height: Option<u32>,
|
||||||
opt_jsobject: Option<*mut JSObject>,
|
jsobject: *mut JSObject,
|
||||||
) -> Fallible<DomRoot<ImageData>> {
|
) -> Fallible<DomRoot<ImageData>> {
|
||||||
assert!(opt_jsobject.is_some() || opt_height.is_some());
|
// checking jsobject type
|
||||||
|
|
||||||
if 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();
|
let cx = global.get_cx();
|
||||||
typedarray!(in(*cx) let array_res: Uint8ClampedArray = jsobject);
|
typedarray!(in(*cx) let array_res: Uint8ClampedArray = jsobject);
|
||||||
let array = array_res.map_err(|_| {
|
let array = array_res.map_err(|_| {
|
||||||
|
@ -76,7 +68,7 @@ impl ImageData {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let byte_len = array.as_slice().len() as u32;
|
let byte_len = array.as_slice().len() as u32;
|
||||||
if byte_len % 4 != 0 || byte_len == 0 {
|
if byte_len == 0 || byte_len % 4 != 0 {
|
||||||
return Err(Error::InvalidState);
|
return Err(Error::InvalidState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,13 +80,31 @@ impl ImageData {
|
||||||
let height = len / width;
|
let height = len / width;
|
||||||
if opt_height.map_or(false, |x| height != x) {
|
if opt_height.map_or(false, |x| height != x) {
|
||||||
return Err(Error::IndexSize);
|
return Err(Error::IndexSize);
|
||||||
} else {
|
|
||||||
opt_height = Some(height);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let height = opt_height.unwrap();
|
let imagedata = Box::new(ImageData {
|
||||||
if height == 0 {
|
reflector_: Reflector::new(),
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
data: Heap::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
(*imagedata).data.set(jsobject);
|
||||||
|
|
||||||
|
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);
|
return Err(Error::IndexSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,15 +115,11 @@ impl ImageData {
|
||||||
data: Heap::default(),
|
data: Heap::default(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(jsobject) = opt_jsobject {
|
|
||||||
(*imagedata).data.set(jsobject);
|
|
||||||
} else {
|
|
||||||
let len = width * height * 4;
|
let len = width * height * 4;
|
||||||
let cx = global.get_cx();
|
let cx = global.get_cx();
|
||||||
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
|
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
|
||||||
Uint8ClampedArray::create(*cx, CreateWith::Length(len), array.handle_mut()).unwrap();
|
Uint8ClampedArray::create(*cx, CreateWith::Length(len), array.handle_mut()).unwrap();
|
||||||
(*imagedata).data.set(array.get());
|
(*imagedata).data.set(array.get());
|
||||||
}
|
|
||||||
|
|
||||||
Ok(reflect_dom_object(
|
Ok(reflect_dom_object(
|
||||||
imagedata,
|
imagedata,
|
||||||
|
@ -121,11 +127,10 @@ impl ImageData {
|
||||||
ImageDataBinding::Wrap,
|
ImageDataBinding::Wrap,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-3
|
// https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-3
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn Constructor(global: &GlobalScope, width: u32, height: u32) -> Fallible<DomRoot<Self>> {
|
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
|
// https://html.spec.whatwg.org/multipage/#pixel-manipulation:dom-imagedata-4
|
||||||
|
@ -138,7 +143,7 @@ impl ImageData {
|
||||||
width: u32,
|
width: u32,
|
||||||
opt_height: Option<u32>,
|
opt_height: Option<u32>,
|
||||||
) -> Fallible<DomRoot<Self>> {
|
) -> 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.
|
/// Nothing must change the array on the JS side while the slice is live.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue