Initial support for closures

This commit starts wasm-bindgen down the path of supporting closures. We
discussed this at the recent Rust All-Hands but I ended up needing to pretty
significantly scale back the ambitions of what closures are supported. This
commit is just the initial support and provides only a small amount of support
but will hopefully provide a good basis for future implementations.

Specifically this commit adds support for passing `&Fn(...)` to an *imported
function*, but nothing elese. The `&Fn` type can have any lifetime and the JS
object is invalidated as soon as the import returns. The arguments and return
value of `Fn` must currently implement the `WasmAbi` trait, aka they can't
require any conversions like strings/types/etc.

I'd like to soon expand this to `&mut FnMut` as well as `'static` closures that
can be passed around for a long time in JS, but for now I'm putting that off
until later. I'm not currently sure how to implement richer argument types, but
hopefully that can be figured out at some point!
This commit is contained in:
Alex Crichton
2018-04-04 08:24:19 -07:00
parent 9b46c8831d
commit c0cad447c1
8 changed files with 347 additions and 64 deletions

View File

@ -146,8 +146,10 @@ pub const TYPE_SLICE_F64: u32 = 20;
pub const TYPE_VECTOR_F64: u32 = 21;
pub const TYPE_JS_OWNED: u32 = 22;
pub const TYPE_JS_REF: u32 = 23;
pub const TYPE_STACK_FUNC0: u32 = 24;
pub const TYPE_STACK_FUNC1: u32 = 25;
pub const TYPE_CUSTOM_START: u32 = 24;
pub const TYPE_CUSTOM_START: u32 = 26;
pub const TYPE_CUSTOM_REF_FLAG: u32 = 1;
pub fn name_to_descriptor(name: &str) -> u32 {