Last active
January 20, 2021 16:16
-
-
Save gliheng/9a4ad9c9ee125dbdeabf6d2719f4d6b0 to your computer and use it in GitHub Desktop.
export rust function to javascript using cwrap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[target.wasm32-unknown-emscripten] | |
rustflags = [ | |
"-Clink-args=-s EXPORTED_FUNCTIONS=['_draw'] -s ASSERTIONS=1", | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Module.addOnPostRun(function() { | |
let draw = Module.cwrap("draw", null, ["string"]); | |
draw("#canvas"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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