mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-25 14:41:32 +00:00
add mapdir for emscripten; implement getdents, etc.
This commit is contained in:
@ -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;
|
||||
|
Reference in New Issue
Block a user