Files
wasm-bindgen/src/describe.rs
Alex Crichton cbeb301371 Add support for optional slice types (#507)
* Shard the `convert.rs` module into sub-modules

Hopefully this'll make the organization a little nicer over time!

* Start adding support for optional types

This commit starts adding support for optional types to wasm-bindgen as
arguments/return values to functions. The strategy here is to add two new
traits, `OptionIntoWasmAbi` and `OptionFromWasmAbi`. These two traits are used
as a blanket impl to implement `IntoWasmAbi` and `FromWasmAbi` for `Option<T>`.

Some consequences of this design:

* It should be possible to ensure `Option<SomeForeignType>` implements to/from
  wasm traits. This is because the option-based traits can be implemented for
  foreign types.
* A specialized implementation is possible for all types, so there's no need for
  `Option<T>` to introduce unnecessary overhead.
* Two new traits is a bit unforutnate but I can't currently think of an
  alternative design that works for the above two constraints, although it
  doesn't mean one doesn't exist!
* The error messages for "can't use this type here" is actually halfway decent
  because it says these new traits need to be implemented, which provides a good
  place to document and talk about what's going on here!
* Nested references like `Option<&T>` can't implement `FromWasmAbi`. This means
  that you can't define a function in Rust which takes `Option<&str>`. It may be
  possible to do this one day but it'll likely require more trait trickery than
  I'm capable of right now.

* Add support for optional slices

This commit adds support for optional slice types, things like strings and
arrays. The null representation of these has a pointer value of 0, which should
never happen in normal Rust. Otherwise the various plumbing is done throughout
the tooling to enable these types in all locations.

* Fix `takeObject` on global sentinels

These don't have a reference count as they're always expected to work, so avoid
actually dropping a reference on them.

* Remove some no longer needed bindings

* Add support for optional anyref types

This commit adds support for optional imported class types. Each type imported
with `#[wasm_bindgen]` automatically implements the relevant traits and now
supports `Option<Foo>` in various argument/return positions.

* Fix building without the `std` feature

* Actually fix the build...

* Add support for optional types to WebIDL

Closes #502
2018-07-19 14:44:23 -05:00

206 lines
4.0 KiB
Rust

//! This is an internal module, no stability guarantees are provided. Use at
//! your own risk.
#![doc(hidden)]
use JsValue;
macro_rules! tys {
($($a:ident)*) => (tys! { @ ($($a)*) 0 });
(@ () $v:expr) => {};
(@ ($a:ident $($b:ident)*) $v:expr) => {
pub const $a: u32 = $v;
tys!(@ ($($b)*) $v+1);
}
}
// NB: this list must be kept in sync with `crates/cli-support/src/descriptor.rs`
tys! {
I8
U8
I16
U16
I32
U32
I64
U64
F32
F64
BOOLEAN
FUNCTION
CLOSURE
STRING
REF
REFMUT
SLICE
VECTOR
ANYREF
ENUM
RUST_STRUCT
CHAR
OPTIONAL
}
pub fn inform(a: u32) {
unsafe { super::__wbindgen_describe(a) }
}
pub trait WasmDescribe {
fn describe();
}
macro_rules! simple {
($($t:ident => $d:ident)*) => ($(
impl WasmDescribe for $t {
fn describe() { inform($d) }
}
)*)
}
simple! {
i8 => I8
u8 => U8
i16 => I16
u16 => U16
i32 => I32
u32 => U32
i64 => I64
u64 => U64
isize => I32
usize => U32
f32 => F32
f64 => F64
bool => BOOLEAN
char => CHAR
str => STRING
JsValue => ANYREF
}
impl<T> WasmDescribe for *const T {
fn describe() {
inform(I32)
}
}
impl<T> WasmDescribe for *mut T {
fn describe() {
inform(I32)
}
}
impl<T: WasmDescribe> WasmDescribe for [T] {
fn describe() {
inform(SLICE);
T::describe();
}
}
impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a T {
fn describe() {
inform(REF);
T::describe();
}
}
impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a mut T {
fn describe() {
inform(REFMUT);
T::describe();
}
}
if_std! {
use std::prelude::v1::*;
impl WasmDescribe for String {
fn describe() { inform(STRING) }
}
impl<T: WasmDescribe> WasmDescribe for Box<[T]> {
fn describe() {
inform(VECTOR);
T::describe();
}
}
impl<T> WasmDescribe for Vec<T> where Box<[T]>: WasmDescribe {
fn describe() {
<Box<[T]>>::describe();
}
}
}
fn _cnt<T: WasmDescribe>() -> u32 {
1
}
macro_rules! doit {
($( ($($var:ident)*))*) => ($(
impl<'a, $($var,)* R> WasmDescribe for Fn($($var),*) -> R + 'a
where $($var: WasmDescribe,)*
R: WasmDescribe
{
fn describe() {
inform(FUNCTION);
inform(0 $(+ _cnt::<$var>())*);
$(<$var as WasmDescribe>::describe();)*
inform(1);
<R as WasmDescribe>::describe();
}
}
impl<'a, $($var,)* > WasmDescribe for Fn($($var),*) + 'a
where $($var: WasmDescribe,)*
{
fn describe() {
inform(FUNCTION);
inform(0 $(+ _cnt::<$var>())*);
$(<$var as WasmDescribe>::describe();)*
inform(0);
}
}
impl<'a, $($var,)* R> WasmDescribe for FnMut($($var),*) -> R + 'a
where $($var: WasmDescribe,)*
R: WasmDescribe
{
fn describe() {
inform(FUNCTION);
inform(0 $(+ _cnt::<$var>())*);
$(<$var as WasmDescribe>::describe();)*
inform(1);
<R as WasmDescribe>::describe();
}
}
impl<'a, $($var,)* > WasmDescribe for FnMut($($var),*) + 'a
where $($var: WasmDescribe,)*
{
fn describe() {
inform(FUNCTION);
inform(0 $(+ _cnt::<$var>())*);
$(<$var as WasmDescribe>::describe();)*
inform(0);
}
}
)*)
}
doit! {
()
(A)
(A B)
(A B C)
(A B C D)
(A B C D E)
(A B C D E F)
(A B C D E F G)
}
impl<T: WasmDescribe> WasmDescribe for Option<T> {
fn describe() {
inform(OPTIONAL);
T::describe();
}
}