Skip to content

Commit

Permalink
Migrated Emscripten/Windows to new Context API
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Jun 17, 2022
1 parent aca9e93 commit 76b8738
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 74 deletions.
52 changes: 27 additions & 25 deletions lib/emscripten/src/env/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::os::raw::c_char;
use crate::env::{call_malloc, EmAddrInfo};
use crate::utils::{copy_cstr_into_wasm, read_string_from_wasm};
use crate::EmEnv;
use wasmer::WasmPtr;
use wasmer::{AsContextMut, ContextMut, WasmPtr};

extern "C" {
#[link_name = "_putenv"]
Expand All @@ -17,10 +17,10 @@ extern "C" {

// #[no_mangle]
/// emscripten: _getenv // (name: *const char) -> *const c_char;
pub fn _getenv(ctx: &EmEnv, name: u32) -> u32 {
pub fn _getenv(mut ctx: ContextMut<'_, EmEnv>, name: u32) -> u32 {
debug!("emscripten::_getenv");
let memory = ctx.memory(0);
let name_string = read_string_from_wasm(&memory, name);
let memory = ctx.data().memory(0);
let name_string = read_string_from_wasm(ctx.as_context_mut(), &memory, 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 @@ -30,12 +30,12 @@ pub fn _getenv(ctx: &EmEnv, name: u32) -> u32 {
}

/// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int);
pub fn _setenv(ctx: &EmEnv, name: u32, value: u32, _overwrite: u32) -> c_int {
pub fn _setenv(mut ctx: ContextMut<'_, EmEnv>, name: u32, value: u32, _overwrite: u32) -> c_int {
debug!("emscripten::_setenv");
let memory = ctx.memory(0);
let memory = ctx.data().memory(0);
// 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(ctx.as_context_mut(), &memory, name);
let value = read_string_from_wasm(ctx, &memory, 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 @@ -45,21 +45,21 @@ pub fn _setenv(ctx: &EmEnv, name: u32, value: u32, _overwrite: u32) -> c_int {
}

/// emscripten: _putenv // (name: *const char);
pub fn _putenv(ctx: &EmEnv, name: c_int) -> c_int {
pub fn _putenv(ctx: ContextMut<'_, EmEnv>, name: c_int) -> c_int {
debug!("emscripten::_putenv");
let memory = ctx.memory(0);
let name_addr = emscripten_memory_pointer!(&memory, name) as *const c_char;
let memory = ctx.data().memory(0);
let name_addr = emscripten_memory_pointer!(ctx, &memory, name) as *const c_char;
debug!("=> name({:?})", unsafe {
std::ffi::CStr::from_ptr(name_addr)
});
unsafe { putenv(name_addr) }
}

/// emscripten: _unsetenv // (name: *const char);
pub fn _unsetenv(ctx: &EmEnv, name: u32) -> c_int {
pub fn _unsetenv(ctx: ContextMut<'_, EmEnv>, name: u32) -> c_int {
debug!("emscripten::_unsetenv");
let memory = ctx.memory(0);
let name = read_string_from_wasm(&memory, name);
let memory = ctx.data().memory(0);
let name = read_string_from_wasm(ctx, &memory, 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 All @@ -69,11 +69,11 @@ pub fn _unsetenv(ctx: &EmEnv, name: u32) -> c_int {
}

#[allow(clippy::cast_ptr_alignment)]
pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int {
pub fn _getpwnam(mut ctx: ContextMut<'_, EmEnv>, name_ptr: c_int) -> c_int {
debug!("emscripten::_getpwnam {}", name_ptr);
#[cfg(not(feature = "debug"))]
let _ = name_ptr;
let memory = ctx.memory(0);
let memory = ctx.data().memory(0);

#[repr(C)]
struct GuestPasswd {
Expand All @@ -88,9 +88,10 @@ pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int {

// stub this in windows as it is not valid
unsafe {
let passwd_struct_offset = call_malloc(ctx, mem::size_of::<GuestPasswd>() as _);
let passwd_struct_offset =
call_malloc(ctx.as_context_mut(), mem::size_of::<GuestPasswd>() as _);
let passwd_struct_ptr =
emscripten_memory_pointer!(&memory, passwd_struct_offset) as *mut GuestPasswd;
emscripten_memory_pointer!(ctx, memory, 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 All @@ -104,11 +105,11 @@ pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int {
}

#[allow(clippy::cast_ptr_alignment)]
pub fn _getgrnam(ctx: &EmEnv, name_ptr: c_int) -> c_int {
pub fn _getgrnam(mut ctx: ContextMut<'_, EmEnv>, name_ptr: c_int) -> c_int {
debug!("emscripten::_getgrnam {}", name_ptr);
#[cfg(not(feature = "debug"))]
let _ = name_ptr;
let memory = ctx.memory(0);
let memory = ctx.data().memory(0);

#[repr(C)]
struct GuestGroup {
Expand All @@ -120,9 +121,10 @@ pub fn _getgrnam(ctx: &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(ctx, mem::size_of::<GuestGroup>() as _);
let group_struct_offset =
call_malloc(ctx.as_context_mut(), mem::size_of::<GuestGroup>() as _);
let group_struct_ptr =
emscripten_memory_pointer!(&memory, group_struct_offset) as *mut GuestGroup;
emscripten_memory_pointer!(ctx, memory, 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 All @@ -131,21 +133,21 @@ pub fn _getgrnam(ctx: &EmEnv, name_ptr: c_int) -> c_int {
}
}

pub fn _sysconf(_ctx: &EmEnv, name: c_int) -> c_long {
pub fn _sysconf(_ctx: ContextMut<'_, EmEnv>, name: c_int) -> c_long {
debug!("emscripten::_sysconf {}", name);
#[cfg(not(feature = "debug"))]
let _ = name;
// stub because sysconf is not valid on windows
0
}

pub fn _gai_strerror(_ctx: &EmEnv, _ecode: i32) -> i32 {
pub fn _gai_strerror(_ctx: ContextMut<'_, EmEnv>, _ecode: i32) -> i32 {
debug!("emscripten::_gai_strerror({}) - stub", _ecode);
-1
}

pub fn _getaddrinfo(
_ctx: &EmEnv,
_ctx: ContextMut<'_, EmEnv>,
_node_ptr: WasmPtr<c_char>,
_service_str_ptr: WasmPtr<c_char>,
_hints_ptr: WasmPtr<EmAddrInfo>,
Expand Down
9 changes: 5 additions & 4 deletions lib/emscripten/src/io/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::EmEnv;
use wasmer::ContextMut;

// This may be problematic for msvc which uses inline functions for the printf family
// this cfg_attr will try to link with the legacy lib that does not inline printf
Expand All @@ -14,12 +15,12 @@ use crate::EmEnv;
//}

/// putchar
pub fn putchar(_ctx: &EmEnv, chr: i32) {
pub fn putchar(_ctx: ContextMut<'_, EmEnv>, chr: i32) {
unsafe { libc::putchar(chr) };
}

/// printf
pub fn printf(_ctx: &EmEnv, memory_offset: i32, extra: i32) -> i32 {
pub fn printf(_ctx: ContextMut<'_, EmEnv>, memory_offset: i32, extra: i32) -> i32 {
debug!("emscripten::printf {}, {}", memory_offset, extra);
#[cfg(not(feature = "debug"))]
{
Expand All @@ -34,13 +35,13 @@ pub fn printf(_ctx: &EmEnv, memory_offset: i32, extra: i32) -> i32 {
}

/// chroot
pub fn chroot(_ctx: &EmEnv, _name_ptr: i32) -> i32 {
pub fn chroot(_ctx: ContextMut<'_, EmEnv>, _name_ptr: i32) -> i32 {
debug!("emscripten::chroot");
unimplemented!("emscripten::chroot")
}

/// getpwuid
pub fn getpwuid(_ctx: &EmEnv, _uid: i32) -> i32 {
pub fn getpwuid(_ctx: ContextMut<'_, EmEnv>, _uid: i32) -> i32 {
debug!("emscripten::getpwuid");
unimplemented!("emscripten::getpwuid")
}
Loading

0 comments on commit 76b8738

Please sign in to comment.