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
This commit is contained in:
Alex Crichton
2018-09-24 13:49:12 -07:00
parent d10ca579e4
commit 7b495468f6
15 changed files with 169 additions and 60 deletions

View File

@ -1173,6 +1173,11 @@ impl<'a> Context<'a> {
self.arrayget("getArrayU8FromWasm", "getUint8Memory", 1);
}
fn expose_get_clamped_array_u8_from_wasm(&mut self) {
self.expose_clamped_uint8_memory();
self.arrayget("getClampedArrayU8FromWasm", "getUint8ClampedMemory", 1);
}
fn expose_get_array_i16_from_wasm(&mut self) {
self.expose_int16_memory();
self.arrayget("getArrayI16FromWasm", "getInt16Memory", 2);
@ -1237,6 +1242,10 @@ impl<'a> Context<'a> {
self.memview("getUint8Memory", "Uint8Array");
}
fn expose_clamped_uint8_memory(&mut self) {
self.memview("getUint8ClampedMemory", "Uint8ClampedArray");
}
fn expose_int16_memory(&mut self) {
self.memview("getInt16Memory", "Int16Array");
}
@ -1283,6 +1292,10 @@ impl<'a> Context<'a> {
self.expose_uint8_memory();
"getUint8Memory"
}
VectorKind::ClampedU8 => {
self.expose_clamped_uint8_memory();
"getUint8ClampedMemory"
}
VectorKind::I16 => {
self.expose_int16_memory();
"getInt16Memory"
@ -1444,7 +1457,7 @@ impl<'a> Context<'a> {
self.expose_pass_string_to_wasm()?;
"passStringToWasm"
}
VectorKind::I8 | VectorKind::U8 => {
VectorKind::I8 | VectorKind::U8 | VectorKind::ClampedU8 => {
self.expose_pass_array8_to_wasm()?;
"passArray8ToWasm"
}
@ -1490,6 +1503,10 @@ impl<'a> Context<'a> {
self.expose_get_array_u8_from_wasm();
"getArrayU8FromWasm"
}
VectorKind::ClampedU8 => {
self.expose_get_clamped_array_u8_from_wasm();
"getClampedArrayU8FromWasm"
}
VectorKind::I16 => {
self.expose_get_array_i16_from_wasm();
"getArrayI16FromWasm"