在我的javascript中,在调用wasm之前,我定义了一个函数jalert,稍后我想使用wasm从Rust中调用它。我在wasm-bindgen的文档中找不到如何调用我之前在javascript中定义的任意函数,如下所示。我可以使用像alert和console.log这样的函数,因为它们已经是javascript的一部分,但是我不能让这个函数jalert工作。我在浏览器中得到一个错误,说它没有被定义。有了警报功能,它不会有任何问题。
function jalert(sometext) {
alert(sometext);
}
jalert("I am Claudio");
// This works from Javascript在Rust文件lib.rs中
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
fn jalert(s: &str);
}
#[wasm_bindgen]
pub fn run_alert(item: &str) {
jalert(&format!("This is WASM calling javascript function jalert and {}", item));
alert(&format!("This is WASM and {}", item));
}
// The alert() code works fine. The jalert() call in run_alert() gives me a browser error that jalert is not defined发布于 2020-07-16 22:19:49
我会说你需要将#wasm_bindgen添加到你的方法声明中:
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(method, js_name = alert]
fn alert(s: &str);
#[wasm_bindgen(method, js_name = jalert]
fn jalert(s: &str);
}https://stackoverflow.com/questions/62935592
复制相似问题