Skip to content

Instantly share code, notes, and snippets.

@gliheng
Last active January 20, 2021 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gliheng/9a4ad9c9ee125dbdeabf6d2719f4d6b0 to your computer and use it in GitHub Desktop.
Save gliheng/9a4ad9c9ee125dbdeabf6d2719f4d6b0 to your computer and use it in GitHub Desktop.
export rust function to javascript using cwrap
[target.wasm32-unknown-emscripten]
rustflags = [
"-Clink-args=-s EXPORTED_FUNCTIONS=['_draw'] -s ASSERTIONS=1",
]
Module.addOnPostRun(function() {
let draw = Module.cwrap("draw", null, ["string"]);
draw("#canvas");
});
#[no_mangle]
pub fn draw(sel: *mut c_char) {
unsafe {
match CString::from_raw(sel).into_string() {
Ok(sel) => {
draw_chart(&sel);
// rust cannot free this pointer from js
mem::forget(sel);
},
Err(_) => println!("Cannot find node")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment