wasm-bindgen/crates/webidl-tests/array.rs
Alex Crichton 7b495468f6 Implement support for Uint8ClampedArray
This commit implements support for binding APIs that take
`Uint8ClampedArray` in JS. This is pretty rare but comes up in a
`web-sys` binding or two, and we're now able to bind these APIs instead
of having to omit the bindings.

The `Uint8ClampedArray` type is bound by using the `Clamped` marker
struct in Rust. For example this is declaring a JS API that takes
`Uint8ClampedArray`:

    use wasm_bindgen::Clamped;

    #[wasm_bindgen]
    extern {
        fn takes_clamped(a: Clamped<&[u8]>);
    }

The `Clamped` type currently only works when wrapping the `&[u8]`, `&mut
[u8]`, and `Vec<u8>` types. Everything else will produce an error at
`wasm-bindgen` time.

Closes #421
2018-09-24 13:58:37 -07:00

23 lines
813 B
Rust

use js_sys::Object;
use wasm_bindgen::Clamped;
use wasm_bindgen_test::*;
include!(concat!(env!("OUT_DIR"), "/array.rs"));
#[wasm_bindgen_test]
fn take_and_return_a_bunch_of_slices() {
let f = TestArrays::new().unwrap();
assert_eq!(f.strings("y"), "x");
assert_eq!(f.byte_strings("yz"), "xx");
assert_eq!(f.usv_strings("abc"), "efg");
assert_eq!(f.f32(&mut [1.0, 2.0]), [3.0, 4.0, 5.0]);
assert_eq!(f.f64(&mut [1.0, 2.0]), [3.0, 4.0, 5.0]);
assert_eq!(f.i8(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.i16(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.i32(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.u8(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.u16(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.u32(&mut [1, 2]), [3, 4, 5]);
assert_eq!(f.u8_clamped(Clamped(&mut [1, 2])).0, [3, 4, 5]);
}