add mapdir for emscripten; implement getdents, etc.

This commit is contained in:
Mark McCaskey
2019-05-24 18:00:07 -07:00
parent dcf0a7cc4b
commit fc15bc58f6
8 changed files with 176 additions and 31 deletions

View File

@ -5,6 +5,7 @@ use libc::stat;
use std::ffi::CStr;
use std::mem::size_of;
use std::os::raw::c_char;
use std::path::PathBuf;
use std::slice;
use wasmer_runtime_core::memory::Memory;
use wasmer_runtime_core::{
@ -204,6 +205,37 @@ pub fn read_string_from_wasm(memory: &Memory, offset: u32) -> String {
String::from_utf8_lossy(&v).to_owned().to_string()
}
/// This function trys to find an entry in mapdir
/// translating paths into their correct value
pub fn get_cstr_path(ctx: &mut Ctx, path: *const i8) -> Option<std::ffi::CString> {
let path_str = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }.to_string();
if let Some(val) = get_emscripten_data(ctx).mapped_dirs.get(&path_str) {
std::ffi::CString::new(val.to_string_lossy().as_bytes()).ok()
} else {
None
}
}
/// gets the current directory
/// handles mapdir logic
pub fn get_current_directory(ctx: &mut Ctx) -> Option<PathBuf> {
if let Some(val) = get_emscripten_data(ctx).mapped_dirs.get(".") {
return Some(val.clone());
}
std::env::current_dir()
.map(|cwd| {
if let Some(val) = get_emscripten_data(ctx)
.mapped_dirs
.get(&cwd.to_string_lossy().to_string())
{
val.clone()
} else {
cwd
}
})
.ok()
}
#[cfg(test)]
mod tests {
use super::is_emscripten_module;