mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
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
This commit is contained in:
@ -28,6 +28,7 @@ are:
|
||||
* Borrowed exported structs (`&Foo` or `&mut Bar`)
|
||||
* The `JsValue` type and `&JsValue` (not mutable references)
|
||||
* Vectors and slices of supported integer types and of the `JsValue` type.
|
||||
* Optional vectors/slices
|
||||
|
||||
All of the above can also be returned except borrowed references. Passing
|
||||
`Vec<JsValue>` as an argument to a function is not currently supported. Strings are
|
||||
|
@ -2,20 +2,16 @@
|
||||
|
||||
The table below provides an overview of all the types that wasm-bindgen can send/receive across the wasm ABI boundary.
|
||||
|
||||
| Type | `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value |
|
||||
|:---:|:---:|:---:|:---:|:---:|
|
||||
| `str` | No | Yes | No | Yes |
|
||||
| `char` | Yes | No | No | Yes |
|
||||
| `bool` | Yes | No | No | Yes |
|
||||
| `JsValue` | Yes | Yes | Yes | Yes |
|
||||
| `Box<[JsValue]>` | Yes | No | No | Yes |
|
||||
| `*const T` | Yes | No | No | Yes |
|
||||
| `*mut T` | Yes | No | No | Yes |
|
||||
| `u8` `i8` `u16` `i16` `u64` `i64` `isize` `size` | Yes | No | No | Yes |
|
||||
| `u32` `i32` `f32` `f64` | Yes | Yes | Yes | Yes |
|
||||
| `Box<[u8]>` `Box<[i8]>` `Box<[u16]>` `Box<[i16]>` `Box<[u32]>` `Box<[i32]>` `Box<[u64]>` `Box<[i64]>` | Yes | No | No | Yes |
|
||||
| `Box<[f32]>` `Box<[f64]>` | Yes | No | No | Yes |
|
||||
| `[u8]` `[i8]` `[u16]` `[i16]` `[u32]` `[i32]` `[u64]` `[i64]` | No | Yes | Yes | No |
|
||||
| `&[u8]` `&mut[u8]` `&[i8]` `&mut[i8]` `&[u16]` `&mut[u16]` `&[i16]` `&mut[i16]` `&[u32]` `&mut[u32]` `&[i32]` `&mut[i32]` `&[u64]` `&mut[u64]` `&[i64]` `&mut[i64]` | No | No | No | Yes |
|
||||
| `[f32]` `[f64]` | No | Yes | Yes | No |
|
||||
| `&[f32]` `&mut[f32]` `&[f64]` `&mut[f64]` | No | No | No | Yes |
|
||||
| Type | `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value |
|
||||
|:---:|:---:|:---:|:---:|:---:|:---:|
|
||||
| `str` | No | Yes | No | Yes | Yes | No |
|
||||
| `char` | Yes | No | No | Yes | No | No |
|
||||
| `bool` | Yes | No | No | Yes | No | No |
|
||||
| `JsValue` | Yes | Yes | Yes | Yes | No | No |
|
||||
| `Box<[JsValue]>` | Yes | No | No | Yes | Yes | yes |
|
||||
| `*const T` | Yes | No | No | Yes | No | No |
|
||||
| `*mut T` | Yes | No | No | Yes | No | No |
|
||||
| `u8` `i8` `u16` `i16` `u64` `i64` `isize` `size` | Yes | No | No | Yes | No | No |
|
||||
| `u32` `i32` `f32` `f64` | Yes | Yes | Yes | Yes | No | No |
|
||||
| `Box<[u8]>` `Box<[i8]>` `Box<[u16]>` `Box<[i16]>` `Box<[u32]>` `Box<[i32]>` `Box<[u64]>` `Box<[i64]>` `Box<[f32]>` `Box<[f64]`> | Yes | No | No | Yes | Yes | Yes |
|
||||
| `[u8]` `[i8]` `[u16]` `[i16]` `[u32]` `[i32]` `[u64]` `[i64]` `[f32]` `[f64]` | No | Yes | Yes | No | Yes | No |
|
||||
|
Reference in New Issue
Block a user