代码是基本的,我从 js 调用一个函数,它带有一个在 lib.rs 中声明的字符串输入,但是该字符串没有传递给 wasm 中的函数我收到一个空字符串,反向也不起作用我不是也可以将字符串从 wasm 传递给 js。
wasm 文件看起来像这样
use wasm_bindgen::prelude::*;
use web_sys::console;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn hello(v: String) {
console::log_1(&v.into()); //prints => empty string
}
js 看起来这个 wasm 已经成功启动了其他功能工作正常
wasm.hello("hello")
货物 toml 看起来像这样
[package]
name = "painter"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2"
#web-sys = "0.3.39"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
[dependencies.web-sys]
version = "0.3.39"
features = [
"console",
"Document",
"Element",
"HtmlElement",
"Node",
"Window"
] # Do you have this line in your Cargo.toml?
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.2"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"