相关疑难解决方法(0)

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

关于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返回的函数来更新该相机.

rust webassembly wasm-bindgen

1
推荐指数
1
解决办法
462
查看次数

标签 统计

rust ×1

wasm-bindgen ×1

webassembly ×1