Skip to content

Commit

Permalink
Fix Windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Aug 12, 2022
1 parent 262f8e4 commit 43581f0
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/emscripten/src/env/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ extern "C" {
/// emscripten: _getenv // (name: *const char) -> *const c_char;
pub fn _getenv(mut ctx: FunctionEnvMut<EmEnv>, name: u32) -> u32 {
debug!("emscripten::_getenv");
let memory = ctx.data().memory_view(0, &ctx);
let name_string = read_string_from_wasm(&memory, name);
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
let name_string = read_string_from_wasm(&view, name);
debug!("=> name({:?})", name_string);
let c_str = unsafe { getenv(name_string.as_ptr() as *const libc::c_char) };
if c_str.is_null() {
Expand All @@ -32,10 +33,11 @@ pub fn _getenv(mut ctx: FunctionEnvMut<EmEnv>, name: u32) -> u32 {
/// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int);
pub fn _setenv(ctx: FunctionEnvMut<EmEnv>, name: u32, value: u32, _overwrite: u32) -> c_int {
debug!("emscripten::_setenv");
let memory = ctx.data().memory_view(0, &ctx);
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
// setenv does not exist on windows, so we hack it with _putenv
let name = read_string_from_wasm(&memory, name);
let value = read_string_from_wasm(&memory, value);
let name = read_string_from_wasm(&view, name);
let value = read_string_from_wasm(&view, value);
let putenv_string = format!("{}={}", name, value);
let putenv_cstring = CString::new(putenv_string).unwrap();
let putenv_raw_ptr = putenv_cstring.as_ptr();
Expand All @@ -47,8 +49,9 @@ pub fn _setenv(ctx: FunctionEnvMut<EmEnv>, name: u32, value: u32, _overwrite: u3
/// emscripten: _putenv // (name: *const char);
pub fn _putenv(ctx: FunctionEnvMut<EmEnv>, name: c_int) -> c_int {
debug!("emscripten::_putenv");
let memory = ctx.data().memory_view(0, &ctx);
let name_addr = emscripten_memory_pointer!(&memory, name) as *const c_char;
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
let name_addr = emscripten_memory_pointer!(&view, name) as *const c_char;
debug!("=> name({:?})", unsafe {
std::ffi::CStr::from_ptr(name_addr)
});
Expand All @@ -58,8 +61,9 @@ pub fn _putenv(ctx: FunctionEnvMut<EmEnv>, name: c_int) -> c_int {
/// emscripten: _unsetenv // (name: *const char);
pub fn _unsetenv(ctx: FunctionEnvMut<EmEnv>, name: u32) -> c_int {
debug!("emscripten::_unsetenv");
let memory = ctx.data().memory_view(0, &ctx);
let name = read_string_from_wasm(&memory, name);
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
let name = read_string_from_wasm(&view, name);
// no unsetenv on windows, so use putenv with an empty value
let unsetenv_string = format!("{}=", name);
let unsetenv_cstring = CString::new(unsetenv_string).unwrap();
Expand Down Expand Up @@ -88,9 +92,10 @@ pub fn _getpwnam(mut ctx: FunctionEnvMut<EmEnv>, name_ptr: c_int) -> c_int {
// stub this in windows as it is not valid
unsafe {
let passwd_struct_offset = call_malloc(&mut ctx, mem::size_of::<GuestPasswd>() as _);
let memory = ctx.data().memory_view(0, &ctx);
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
let passwd_struct_ptr =
emscripten_memory_pointer!(&memory, passwd_struct_offset) as *mut GuestPasswd;
emscripten_memory_pointer!(&view, passwd_struct_offset) as *mut GuestPasswd;
(*passwd_struct_ptr).pw_name = 0;
(*passwd_struct_ptr).pw_passwd = 0;
(*passwd_struct_ptr).pw_gecos = 0;
Expand Down Expand Up @@ -120,9 +125,10 @@ pub fn _getgrnam(mut ctx: FunctionEnvMut<EmEnv>, name_ptr: c_int) -> c_int {
// stub the group struct as it is not supported on windows
unsafe {
let group_struct_offset = call_malloc(&mut ctx, mem::size_of::<GuestGroup>() as _);
let memory = ctx.data().memory_view(0, &ctx);
let memory = ctx.data().memory(0);
let view = memory.view(&ctx);
let group_struct_ptr =
emscripten_memory_pointer!(&memory, group_struct_offset) as *mut GuestGroup;
emscripten_memory_pointer!(&view, group_struct_offset) as *mut GuestGroup;
(*group_struct_ptr).gr_name = 0;
(*group_struct_ptr).gr_passwd = 0;
(*group_struct_ptr).gr_gid = 0;
Expand Down

0 comments on commit 43581f0

Please sign in to comment.