Skip to content

Instantly share code, notes, and snippets.

@lannonbr
Created January 7, 2020 15:06
Show Gist options
  • Save lannonbr/4538fde99225f65752b474b7983b44ad to your computer and use it in GitHub Desktop.
Save lannonbr/4538fde99225f65752b474b7983b44ad to your computer and use it in GitHub Desktop.
Using fs.readFileSync in Rust with WASM
const fs = require('fs')
function readFile(path) {
return fs.readFileSync(path, { encoding: 'utf8' })
}
module.exports = {
readFile
}
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);
}
// 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