Auto merge of #20405 - christianpoveda:issue_20349, r=jdm

Add constructors for typed arrays in DOMMatrix and DOMMatrixReadOnly

<!-- Please describe your changes on the following line: -->

- DOMMatrix now have the functions: FromFloat32Array, FromFloat64Array.
- DOMMatrixReadOnly now have the functions: FromFloat32Array, FromFloat64Array, ToFloat32Array, ToFloat64Array.

r? @jdm

---
<!-- 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 #20349 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20405)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-03-24 12:41:44 -04:00 committed by GitHub
commit 2d78964634
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 328 additions and 431 deletions

View file

@ -12,7 +12,8 @@ use dom::dommatrixreadonly::{dommatrixinit_to_matrix, DOMMatrixReadOnly, entries
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use euclid::Transform3D;
use js::rust::CustomAutoRooterGuard;
use js::typedarray::{Float32Array, Float64Array};
#[dom_struct]
pub struct DOMMatrix {
@ -56,6 +57,24 @@ impl DOMMatrix {
pub fn from_readonly(global: &GlobalScope, ro: &DOMMatrixReadOnly) -> DomRoot<Self> {
Self::new(global, ro.is_2d(), ro.matrix().clone())
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrix-fromfloat32array
pub fn FromFloat32Array(
global: &GlobalScope,
mut array: CustomAutoRooterGuard<Float32Array>)
-> Fallible<DomRoot<DOMMatrix>> {
let vec: Vec<f64> = array.to_vec().iter().map(|&x| x as f64).collect();
DOMMatrix::Constructor_(global, vec)
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrix-fromfloat64array
pub fn FromFloat64Array(
global: &GlobalScope,
mut array: CustomAutoRooterGuard<Float64Array>)
-> Fallible<DomRoot<DOMMatrix>> {
let vec: Vec<f64> = array.to_vec();
DOMMatrix::Constructor_(global, vec)
}
}
impl DOMMatrixMethods for DOMMatrix {

View file

@ -15,8 +15,14 @@ use dom::dompoint::DOMPoint;
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use euclid::{Transform3D, Angle};
use js::jsapi::{JSObject, JSContext};
use js::rust::CustomAutoRooterGuard;
use js::typedarray::{Float32Array, Float64Array};
use js::typedarray::CreateWith;
use std::cell::{Cell, Ref};
use std::f64;
use std::ptr;
use std::ptr::NonNull;
#[dom_struct]
pub struct DOMMatrixReadOnly {
@ -334,6 +340,27 @@ impl DOMMatrixReadOnly {
})
// Step 3 in DOMMatrix.InvertSelf
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-fromfloat32array
#[allow(unsafe_code)]
pub fn FromFloat32Array(
global: &GlobalScope,
mut array: CustomAutoRooterGuard<Float32Array>)
-> Fallible<DomRoot<DOMMatrixReadOnly>> {
let vec: Vec<f64> = array.to_vec().iter().map(|&x| x as f64).collect();
DOMMatrixReadOnly::Constructor_(global, vec)
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-fromfloat64array
#[allow(unsafe_code)]
pub fn FromFloat64Array(
global: &GlobalScope,
mut array: CustomAutoRooterGuard<Float64Array>)
-> Fallible<DomRoot<DOMMatrixReadOnly>> {
let vec: Vec<f64> = array.to_vec();
DOMMatrixReadOnly::Constructor_(global, vec)
}
}
@ -553,6 +580,27 @@ impl DOMMatrixReadOnlyMethods for DOMMatrixReadOnly {
DOMPoint::new(&self.global(), x, y, z, w)
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-tofloat32array
#[allow(unsafe_code)]
unsafe fn ToFloat32Array(&self, cx: *mut JSContext) -> NonNull<JSObject> {
let vec: Vec<f32> = self.matrix
.borrow().to_row_major_array().iter().map(|&x| x as f32).collect();
rooted!(in (cx) let mut array = ptr::null_mut::<JSObject>());
let _ = Float32Array::create(cx, CreateWith::Slice(&vec), array.handle_mut())
.unwrap();
NonNull::new_unchecked(array.get())
}
// https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-tofloat64array
#[allow(unsafe_code)]
unsafe fn ToFloat64Array(&self, cx: *mut JSContext) -> NonNull<JSObject> {
let arr = self.matrix.borrow().to_row_major_array();
rooted!(in (cx) let mut array = ptr::null_mut::<JSObject>());
let _ = Float64Array::create(cx, CreateWith::Slice(&arr), array.handle_mut())
.unwrap();
NonNull::new_unchecked(array.get())
}
}

View file

@ -17,8 +17,8 @@
interface DOMMatrix : DOMMatrixReadOnly {
[NewObject, Throws] static DOMMatrix fromMatrix(optional DOMMatrixInit other);
// [NewObject] static DOMMatrix fromFloat32Array(Float32Array array32);
// [NewObject] static DOMMatrix fromFloat64Array(Float64Array array64);
[NewObject, Throws] static DOMMatrix fromFloat32Array(Float32Array array32);
[NewObject, Throws] static DOMMatrix fromFloat64Array(Float64Array array64);
// These attributes are simple aliases for certain elements of the 4x4 matrix
inherit attribute unrestricted double a;

View file

@ -17,8 +17,8 @@
interface DOMMatrixReadOnly {
[NewObject, Throws] static DOMMatrixReadOnly fromMatrix(optional DOMMatrixInit other);
// [NewObject] static DOMMatrixReadOnly fromFloat32Array(Float32Array array32);
// [NewObject] static DOMMatrixReadOnly fromFloat64Array(Float64Array array64);
[NewObject, Throws] static DOMMatrixReadOnly fromFloat32Array(Float32Array array32);
[NewObject, Throws] static DOMMatrixReadOnly fromFloat64Array(Float64Array array64);
// These attributes are simple aliases for certain elements of the 4x4 matrix
readonly attribute unrestricted double a;
@ -79,8 +79,8 @@ interface DOMMatrixReadOnly {
DOMMatrix inverse();
DOMPoint transformPoint(optional DOMPointInit point);
// Float32Array toFloat32Array();
// Float64Array toFloat64Array();
Float32Array toFloat32Array();
Float64Array toFloat64Array();
// stringifier;
// serializer = { attribute };

View file

@ -1,13 +0,0 @@
[DOMMatrix-newobject.html]
[DOMMatrix toFloat32Array]
expected: FAIL
[DOMMatrix toFloat64Array]
expected: FAIL
[DOMMatrixReadOnly toFloat32Array]
expected: FAIL
[DOMMatrixReadOnly toFloat64Array]
expected: FAIL

View file

@ -276,102 +276,24 @@
[DOMRectList interface: calling item(unsigned long) on [object DOMRect\] with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: operation fromFloat32Array(Float32Array)]
expected: FAIL
[DOMMatrixReadOnly interface: operation fromFloat64Array(Float64Array)]
expected: FAIL
[DOMMatrixReadOnly interface: operation toFloat32Array()]
expected: FAIL
[DOMMatrixReadOnly interface: operation toFloat64Array()]
expected: FAIL
[DOMMatrixReadOnly interface: stringifier]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on new DOMMatrixReadOnly() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on new DOMMatrixReadOnly() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrixReadOnly() must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrixReadOnly() must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on DOMMatrixReadOnly.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on DOMMatrixReadOnly.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrixReadOnly.fromMatrix({is2D: false}) must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrixReadOnly.fromMatrix({is2D: false}) must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrix interface: legacy window alias]
expected: FAIL
[DOMMatrix interface: operation fromFloat32Array(Float32Array)]
expected: FAIL
[DOMMatrix interface: operation fromFloat64Array(Float64Array)]
expected: FAIL
[DOMMatrix interface: operation setMatrixValue(DOMString)]
expected: FAIL
[DOMMatrix interface: calling fromFloat32Array(Float32Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: calling fromFloat64Array(Float64Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: new DOMMatrix() must inherit property "setMatrixValue(DOMString)" with the proper type]
expected: FAIL
[DOMMatrix interface: calling setMatrixValue(DOMString) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrix() must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrix() must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrix interface: calling fromFloat32Array(Float32Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: calling fromFloat64Array(Float64Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: DOMMatrix.fromMatrix({is2D: false}) must inherit property "setMatrixValue(DOMString)" with the proper type]
expected: FAIL
[DOMMatrix interface: calling setMatrixValue(DOMString) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrix.fromMatrix({is2D: false}) must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrix.fromMatrix({is2D: false}) must inherit property "toFloat64Array()" with the proper type]
expected: FAIL

View file

@ -43,82 +43,3 @@
[DOMRectReadOnly interface: calling fromRect(DOMRectInit) on new DOMRect() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: operation fromFloat32Array(Float32Array)]
expected: FAIL
[DOMMatrixReadOnly interface: operation fromFloat64Array(Float64Array)]
expected: FAIL
[DOMMatrixReadOnly interface: operation toFloat32Array()]
expected: FAIL
[DOMMatrixReadOnly interface: operation toFloat64Array()]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on new DOMMatrixReadOnly() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on new DOMMatrixReadOnly() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrixReadOnly() must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrixReadOnly() must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on DOMMatrixReadOnly.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on DOMMatrixReadOnly.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrixReadOnly.fromMatrix({is2D: false}) must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrixReadOnly.fromMatrix({is2D: false}) must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrix interface: operation fromFloat32Array(Float32Array)]
expected: FAIL
[DOMMatrix interface: operation fromFloat64Array(Float64Array)]
expected: FAIL
[DOMMatrix interface: calling fromFloat32Array(Float32Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: calling fromFloat64Array(Float64Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on new DOMMatrix() with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrix() must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: new DOMMatrix() must inherit property "toFloat64Array()" with the proper type]
expected: FAIL
[DOMMatrix interface: calling fromFloat32Array(Float32Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrix interface: calling fromFloat64Array(Float64Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat32Array(Float32Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: calling fromFloat64Array(Float64Array) on DOMMatrix.fromMatrix({is2D: false}) with too few arguments must throw TypeError]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrix.fromMatrix({is2D: false}) must inherit property "toFloat32Array()" with the proper type]
expected: FAIL
[DOMMatrixReadOnly interface: DOMMatrix.fromMatrix({is2D: false}) must inherit property "toFloat64Array()" with the proper type]
expected: FAIL