[英]How to call an arbitrary previously defined javascript function Rust using wasm-bindgen?
在我的 javascript 中,在調用 wasm 之前,我定義了一個 function jalert,稍后我想使用 wasm 從 Rust 調用它。 我在 wasm-bindgen 的文檔中找不到如何調用我之前在 javascript 中定義的任意 function,如下所示。 我得到了像 alert 和 console.log 這樣的功能,因為它們已經是 javascript 的一部分,但我不能讓這個 function jalert 工作。 我在瀏覽器中收到一個錯誤,說它沒有定義。 使用警報 function,它不會抱怨。
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
我會說您需要將 #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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.