Skip to content

Instantly share code, notes, and snippets.

@jareddlc
Last active May 12, 2021 21:05
Show Gist options
  • Save jareddlc/7c2db101eb94dc2baa19fd955d554840 to your computer and use it in GitHub Desktop.
Save jareddlc/7c2db101eb94dc2baa19fd955d554840 to your computer and use it in GitHub Desktop.
println!("{}", path);
let store = Store::default();
let module = Module::from_file(&store, &path).expect("Failed to load module");
let mut wasi_env = WasiState::new("Gut")
.finalize()
.expect("Failed to create wasi env");
let import_object = wasi_env
.import_object(&module)
.expect("Failed to create import object");
let instance =
Instance::new(&module, &import_object).expect("Failed to create instance");
let start = instance
.exports
.get_function("gut_export_functions")
.unwrap();
let res = start.call(&[]).unwrap();
use std::ffi::CString;
use std::os::raw::c_char;
use std::time::{SystemTime, UNIX_EPOCH};
#[no_mangle]
pub extern "C" fn gut_export_functions() -> *mut c_char {
let c_str_song = CString::new(r#"["time_now"]"#).unwrap();
c_str_song.into_raw()
}
#[no_mangle]
pub extern "C" fn gut_export_descriptions() -> *mut c_char {
let c_str_song = CString::new(r#"["Prints the current time in milliseconds"]"#).unwrap();
c_str_song.into_raw()
}
#[no_mangle]
fn time_now() {
let now = SystemTime::now();
let epoch = now.duration_since(UNIX_EPOCH).expect("Failed to get time");
println!("{}", epoch.as_millis())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment