mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-28 04:01:33 +00:00
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:
@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut};
|
||||
use std::slice;
|
||||
use std::str;
|
||||
|
||||
use super::JsValue;
|
||||
use {JsValue, throw};
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone)]
|
||||
pub struct Descriptor {
|
||||
@ -21,6 +21,9 @@ pub const DESCRIPTOR_BOOLEAN: Descriptor = Descriptor { __x: *b" 5", };
|
||||
pub const DESCRIPTOR_JS_OWNED: Descriptor = Descriptor { __x: *b" 22", };
|
||||
pub const DESCRIPTOR_JS_REF: Descriptor = Descriptor { __x: *b" 23", };
|
||||
|
||||
pub const DESCRIPTOR_STACK_FUNC0: Descriptor = Descriptor { __x: *b" 24", };
|
||||
pub const DESCRIPTOR_STACK_FUNC1: Descriptor = Descriptor { __x: *b" 25", };
|
||||
|
||||
pub trait WasmBoundary {
|
||||
type Abi: WasmAbi;
|
||||
const DESCRIPTOR: Descriptor;
|
||||
@ -337,3 +340,71 @@ impl Stack for GlobalStack {
|
||||
pub unsafe extern fn __wbindgen_global_argument_ptr() -> *mut u32 {
|
||||
GLOBAL_STACK.as_mut_ptr()
|
||||
}
|
||||
|
||||
macro_rules! stack_closures {
|
||||
($(
|
||||
($($var:ident)*) => $descriptor:ident
|
||||
)*) => ($(
|
||||
impl<'a, $($var,)* R> ToRefWasmBoundary for Fn($($var),*) -> R + 'a
|
||||
where $($var: WasmAbi,)*
|
||||
R: WasmAbi
|
||||
{
|
||||
type Abi = u32;
|
||||
const DESCRIPTOR: Descriptor = $descriptor;
|
||||
|
||||
fn to_abi_ref(&self, extra: &mut Stack) -> u32 {
|
||||
#[allow(non_snake_case)]
|
||||
unsafe extern fn invoke<$($var,)* R>(
|
||||
a: usize,
|
||||
b: usize,
|
||||
$($var: $var),*
|
||||
) -> R {
|
||||
if a == 0 {
|
||||
throw("stack closure has been destroyed already");
|
||||
}
|
||||
let f: &Fn($($var),*) -> R = mem::transmute((a, b));
|
||||
f($($var),*)
|
||||
}
|
||||
unsafe {
|
||||
let (a, b): (usize, usize) = mem::transmute(self);
|
||||
extra.push(a as u32);
|
||||
extra.push(b as u32);
|
||||
invoke::<$($var,)* R> as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $($var,)*> ToRefWasmBoundary for Fn($($var),*) + 'a
|
||||
where $($var: WasmAbi,)*
|
||||
{
|
||||
type Abi = u32;
|
||||
const DESCRIPTOR: Descriptor = $descriptor;
|
||||
|
||||
fn to_abi_ref(&self, extra: &mut Stack) -> u32 {
|
||||
#[allow(non_snake_case)]
|
||||
unsafe extern fn invoke<$($var,)* >(
|
||||
a: usize,
|
||||
b: usize,
|
||||
$($var: $var),*
|
||||
) {
|
||||
if a == 0 {
|
||||
throw("stack closure has been destroyed already");
|
||||
}
|
||||
let f: &Fn($($var),*) = mem::transmute((a, b));
|
||||
f($($var),*)
|
||||
}
|
||||
unsafe {
|
||||
let (a, b): (usize, usize) = mem::transmute(self);
|
||||
extra.push(a as u32);
|
||||
extra.push(b as u32);
|
||||
invoke::<$($var,)*> as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
)*)
|
||||
}
|
||||
|
||||
stack_closures! {
|
||||
() => DESCRIPTOR_STACK_FUNC0
|
||||
(A) => DESCRIPTOR_STACK_FUNC1
|
||||
}
|
||||
|
Reference in New Issue
Block a user