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

@ -4,7 +4,7 @@ use core::mem::{self, ManuallyDrop};
use convert::{Stack, FromWasmAbi, IntoWasmAbi, RefFromWasmAbi};
use convert::{OptionIntoWasmAbi, OptionFromWasmAbi, ReturnWasmAbi};
use convert::traits::WasmAbi;
use JsValue;
use {JsValue, Clamped};
unsafe impl WasmAbi for () {}
@ -373,6 +373,22 @@ impl<T: OptionFromWasmAbi> FromWasmAbi for Option<T> {
}
}
impl<T: IntoWasmAbi> IntoWasmAbi for Clamped<T> {
type Abi = T::Abi;
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
self.0.into_abi(extra)
}
}
impl<T: FromWasmAbi> FromWasmAbi for Clamped<T> {
type Abi = T::Abi;
unsafe fn from_abi(js: T::Abi, extra: &mut Stack) -> Self {
Clamped(T::from_abi(js, extra))
}
}
impl IntoWasmAbi for () {
type Abi = ();