Created
January 7, 2020 15:06
-
-
Save lannonbr/4538fde99225f65752b474b7983b44ad to your computer and use it in GitHub Desktop.
Using fs.readFileSync in Rust with WASM
This file contains 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
const fs = require('fs') | |
function readFile(path) { | |
return fs.readFileSync(path, { encoding: 'utf8' }) | |
} | |
module.exports = { | |
readFile | |
} |
This file contains 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
mod utils; | |
extern crate wasm_bindgen; | |
use wasm_bindgen::prelude::*; | |
#[wasm_bindgen(module = "/lib.js")] | |
extern "C" { | |
#[wasm_bindgen(catch)] | |
fn readFile(path: &str) -> Result<String, JsValue>; | |
} | |
#[wasm_bindgen] | |
extern "C" { | |
#[wasm_bindgen(js_namespace = console)] | |
fn log(s: &str); | |
} | |
macro_rules! console_log { | |
($($t:tt)*) => (log(&format_args!($($t)*).to_string())) | |
} | |
#[wasm_bindgen(start)] | |
pub fn start() { | |
utils::set_panic_hook(); | |
let val: String = readFile("src/lib.rs").unwrap(); | |
console_log!("{}", val); | |
} |
This file contains 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
// Runs lib.rs | |
const f = require('./pkg/hello_wasm.js') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment