diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index cade01332..29f2f3a4f 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -1,14 +1,33 @@ +mod state; mod syscalls; -use syscalls::*; +use self::state::WasiState; +use self::syscalls::*; + +use std::ffi::c_void; use wasmer_runtime_core::{func, import::ImportObject, imports}; -pub fn generate_import_object() -> ImportObject { +pub fn generate_import_object(args: Vec, envs: Vec) -> ImportObject { + let state_gen = move || { + fn state_dtor(data: *mut c_void) { + unsafe { + drop(Box::from_raw(data as *mut WasiState)); + } + } + + let state = Box::new(WasiState { + args: &args[..], + envs: &envs[..], + }); + + ( + Box::leak(state) as *mut WasiState as *mut c_void, + state_dtor as fn(*mut c_void), + ) + }; imports! { // This generates the wasi state. - || { - // returns (pointer to state, function that can destruct the state). - }, + state_gen, "wasi_unstable" => { "__wasi_args_get" => func!(__wasi_args_get), "__wasi_args_sizes_get" => func!(__wasi_args_sizes_get), diff --git a/lib/wasi/src/state.rs b/lib/wasi/src/state.rs new file mode 100644 index 000000000..df5a7ca1e --- /dev/null +++ b/lib/wasi/src/state.rs @@ -0,0 +1,5 @@ +pub struct WasiState<'a> { + // vfs: Vfs, + pub args: &'a [u8], + pub envs: &'a [u8], +} diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index f765e9c87..d4402e8c0 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -1,5 +1,10 @@ +use crate::state::WasiState; use wasmer_runtime_core::vm::Ctx; +fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState { + unsafe { &mut *(ctx.data as *mut WasiState) } +} + pub fn __wasi_args_get(ctx: &mut Ctx) { unimplemented!() }