Migrate wasmer-cli to new Context API

This commit is contained in:
ptitSeb
2022-06-01 15:43:07 +02:00
committed by Manos Pitsidianakis
parent cf85615e55
commit bc58b713db
9 changed files with 133 additions and 76 deletions

View File

@@ -17,12 +17,12 @@ pub fn exit_with_live_runtime(_ctx: ContextMut<'_, EmEnv>) {
pub fn setTempRet0(ctx: ContextMut<'_, EmEnv>, val: i32) {
trace!("emscripten::setTempRet0: {}", val);
get_emscripten_data(&ctx).temp_ret_0 = val;
get_emscripten_data(&ctx).as_mut().unwrap().temp_ret_0 = val;
}
pub fn getTempRet0(ctx: ContextMut<'_, EmEnv>) -> i32 {
trace!("emscripten::getTempRet0");
get_emscripten_data(&ctx).temp_ret_0
get_emscripten_data(&ctx).as_ref().unwrap().temp_ret_0
}
pub fn _alarm(_ctx: ContextMut<'_, EmEnv>, _seconds: u32) -> i32 {

View File

@@ -45,7 +45,7 @@ pub fn call_memset(mut ctx: ContextMut<'_, EmEnv>, pointer: u32, value: u32, siz
pub(crate) fn get_emscripten_data<'a>(
ctx: &'a ContextMut<'_, EmEnv>,
) -> MutexGuard<'a, EmscriptenData> {
) -> MutexGuard<'a, Option<EmscriptenData>> {
ctx.data().data.lock().unwrap()
}

View File

@@ -78,15 +78,22 @@ pub use self::utils::{
/// The environment provided to the Emscripten imports.
pub struct EmEnv {
memory: Arc<RwLock<Option<Memory>>>,
data: Arc<Mutex<EmscriptenData>>,
data: Arc<Mutex<Option<EmscriptenData>>>,
funcs: Arc<Mutex<EmscriptenFunctions>>,
}
impl Default for EmEnv {
fn default() -> Self {
Self::new()
}
}
impl EmEnv {
pub fn new(data: &EmscriptenGlobalsData, mapped_dirs: HashMap<String, PathBuf>) -> Self {
/// Create a new EmEnv, with default value to be set later (set_memory, set_functions and set_data)
pub fn new() -> Self {
Self {
memory: Arc::new(RwLock::new(None)),
data: Arc::new(Mutex::new(EmscriptenData::new(data.clone(), mapped_dirs))),
data: Arc::new(Mutex::new(None)),
funcs: Arc::new(Mutex::new(EmscriptenFunctions::new())),
}
}
@@ -105,11 +112,14 @@ impl EmEnv {
self.funcs = Arc::new(Mutex::new(funcs));
}
// pub fn init_with_instance(&mut self, instance: &Instance) -> Result<(), wasmer::HostEnvInitError> {
// let mut ed = self.data.lock().unwrap();
// ed.init_with_instance(instance)?;
// Ok(())
// }
pub fn set_data(
&mut self,
data: &EmscriptenGlobalsData,
mapped_dirs: HashMap<String, PathBuf>,
) {
let mut w = self.data.lock().unwrap();
*w = Some(EmscriptenData::new(data.clone(), mapped_dirs));
}
}
#[derive(Debug, Clone)]

View File

@@ -88,7 +88,11 @@ pub fn sbrk(mut ctx: ContextMut<'_, EmEnv>, increment: i32) -> i32 {
// let old_dynamic_top = 0;
// let new_dynamic_top = 0;
let memory = ctx.data().memory(0);
let top_ptr = get_emscripten_data(&ctx).globals.dynamictop_ptr;
let top_ptr = get_emscripten_data(&ctx)
.as_ref()
.unwrap()
.globals
.dynamictop_ptr;
let dynamictop_ptr = WasmPtr::<i32>::new(top_ptr).deref(&ctx, &memory);
let old_dynamic_top = dynamictop_ptr.read().unwrap();
let new_dynamic_top: i32 = old_dynamic_top + increment;

View File

@@ -1059,7 +1059,8 @@ pub fn ___syscall220(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarAr
let dirp = emscripten_memory_pointer!(ctx, ctx.data().memory(0), dirp_addr) as *mut u8;
let opened_dirs = &mut get_emscripten_data(&ctx).opened_dirs;
let data = &mut get_emscripten_data(&ctx);
let opened_dirs = &mut data.as_mut().unwrap().opened_dirs;
// need to persist stream across calls?
// let dir: *mut libc::DIR = unsafe { libc::fdopendir(fd) };

View File

@@ -258,6 +258,8 @@ pub fn get_cstr_path(ctx: ContextMut<'_, EmEnv>, path: *const i8) -> Option<std:
for c in components.into_iter() {
cumulative_path.push(c);
if let Some(val) = data
.as_ref()
.unwrap()
.mapped_dirs
.get(&cumulative_path.to_string_lossy().to_string())
{
@@ -276,12 +278,19 @@ pub fn get_cstr_path(ctx: ContextMut<'_, EmEnv>, path: *const i8) -> Option<std:
/// gets the current directory
/// handles mapdir logic
pub fn get_current_directory(ctx: ContextMut<'_, EmEnv>) -> Option<PathBuf> {
if let Some(val) = get_emscripten_data(&ctx).mapped_dirs.get(".") {
if let Some(val) = get_emscripten_data(&ctx)
.as_ref()
.unwrap()
.mapped_dirs
.get(".")
{
return Some(val.clone());
}
std::env::current_dir()
.map(|cwd| {
if let Some(val) = get_emscripten_data(&ctx)
.as_ref()
.unwrap()
.mapped_dirs
.get(&cwd.to_string_lossy().to_string())
{