如何通过WebAssembly将Rust闭包返回给JavaScript?

dav*_*mer 1 rust webassembly wasm-bindgen

关于closure.rs的注释非常好,但是我不能让它从WebAssembly库中返回一个闭包.

我有这样的功能:

#[wasm_bindgen]
pub fn start_game(
    start_time: f64,
    screen_width: f32,
    screen_height: f32,
    on_render: &js_sys::Function,
    on_collision: &js_sys::Function,
) -> ClosureTypeHere {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

在该函数内部,我做了一个闭包,假设Closure::wrap是一个拼图,并从closure.rs复制):

let cb = Closure::wrap(Box::new(move |time| time * 4.2) as Box<FnMut(f64) -> f64>);
Run Code Online (Sandbox Code Playgroud)

如何从此回调start_game和应该返回什么ClosureTypeHere

我们的想法是start_game创建本地可变对象 - 比如相机,并且JavaScript方应该能够调用Rust返回的函数来更新该相机.

小智 6

这是一个很好的问题,也有一些细微差别!值得在指南中调用闭包示例wasm-bindgen(以及关于将闭包传递给JavaScript部分),如果有必要,还可以回馈它!

但是,为了帮助您入门,您可以执行以下操作:

use wasm_bindgen::{Closure, JsValue};

#[wasm_bindgen]
pub fn start_game(
    start_time: f64,
    screen_width: f32,
    screen_height: f32,
    on_render: &js_sys::Function,
    on_collision: &js_sys::Function,
) -> JsValue {
    let cb = Closure::wrap(Box::new(move |time| {
        time * 4.2
    }) as Box<FnMut(f64) -> f64>);

    // Extract the `JsValue` from this `Closure`, the handle
    // on a JS function representing the closure
    let ret = cb.as_ref().clone();

    // Once `cb` is dropped it'll "neuter" the closure and
    // cause invocations to throw a JS exception. Memory
    // management here will come later, so just leak it
    // for now.
    cb.forget();

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

返回值之上只是一个普通的JS对象(这里是a JsValue),我们用Closure你已经看过的类型创建它.这将允许您快速将闭包返回给JS,并且您也可以从JS调用它.

您还询问了存储可变对象等等,这些都可以通过正常的Rust闭包,捕获等来完成.例如,FnMut(f64) -> f64上面的声明是JS函数的签名,可以是任何类型的集合,例如因为FnMut(String, MyCustomWasmBindgenType, f64) -> Vec<u8>如果你真的想要的.要捕获本地对象,您可以:

let mut camera = Camera::new();
let mut state = State::new();
let cb = Closure::wrap(Box::new(move |arg1, arg2| { // note the `move`
    if arg1 {
        camera.update(&arg2);
    } else {
        state.update(&arg2);
    }
}) as Box<_>);
Run Code Online (Sandbox Code Playgroud)

(或类似的东西)

这里camerastate变量将由闭包拥有并同时丢弃.关于just closures的更多信息可以在Rust书中找到.

这里也值得简要介绍内存管理方面.在上面的例子中,我们调用forget()哪个内存泄漏,如果多次调用Rust函数(因为它会泄漏大量内存),可能会出现问题.这里的基本问题是在创建的JS函数对象引用的WASM堆上分配了内存.理论上这个分配的内存需要在JS函数对象是GC时解除分配,但我们无法知道何时发生(直到WeakRef存在!).

与此同时,我们选择了另一种策略.只要Closure类型本身被丢弃,就会释放相关的内存,从而提供确定性的破坏.然而,这使得难以使用,因为我们需要手动计算何时放弃Closure.如果forget不适用于您的用例,那么删除的一些想法Closure是:

  • 首先,如果它只是一次调用JS闭包,那么你可以使用Rc/ RefCell 来删除Closure闭包本身内部(使用一些内部可变性shenanigans).我们还应该最终提供了原生支持FnOncewasm-bindgen,以及!

  • 接下来,您可以将辅助JS对象返回给Rust,该对象具有手动free 方法.例如,带#[wasm_bindgen]注释的包装器.然后,在适当的时候,需要在JS中手动释放此包装器.

如果你可以forget过去,是目前最容易做的事情,但这绝对是一个痛点!我们不能等待WeakRef存在:)