fix putenv and setenv for windows (#140)

This commit is contained in:
Mackenzie Clark
2019-02-06 18:57:11 -08:00
committed by GitHub
parent 2fbd38bd5d
commit 83df3ad35f
4 changed files with 243 additions and 4 deletions

View File

@ -4,7 +4,9 @@ use libc::stat;
use std::ffi::CStr;
use std::mem::size_of;
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::slice;
use wasmer_runtime_core::memory::Memory;
use wasmer_runtime_core::{
module::Module,
structures::TypedIndex,
@ -54,11 +56,11 @@ pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, cstr: *const c_char) -> u32 {
let s = CStr::from_ptr(cstr).to_str().unwrap();
let cstr_len = s.len();
let space_offset = env::call_malloc((cstr_len as u32) + 1, ctx);
let raw_memory = emscripten_memory_pointer!(ctx.memory(0), space_offset) as *mut u8;
let raw_memory = emscripten_memory_pointer!(ctx.memory(0), space_offset) as *mut c_char;
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
for (byte, loc) in s.bytes().zip(slice.iter_mut()) {
*loc = byte;
*loc = byte as _;
}
// TODO: Appending null byte won't work, because there is CStr::from_ptr(cstr)
@ -155,6 +157,14 @@ pub unsafe fn copy_stat_into_wasm(ctx: &mut Ctx, buf: u32, stat: &stat) {
(*stat_ptr).st_ino = stat.st_ino as _;
}
pub fn read_string_from_wasm(memory: &Memory, offset: u32) -> String {
memory.view::<u8>()[(offset as usize)..]
.iter()
.take_while(|cell| cell.get() != 0)
.map(|cell| cell.get() as char)
.collect()
}
#[cfg(test)]
mod tests {
use super::is_emscripten_module;