Move some functions to utils

This commit is contained in:
Steve Akinyemi 2018-11-26 22:15:49 +01:00
parent b1ba24e99d
commit 3f819e3a79
3 changed files with 88 additions and 16 deletions

View File

@ -133,6 +133,16 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
"_getenv",
ImportValue::Func(env::_getenv as *const u8),
);
import_object.set(
"env",
"_getpwnam",
ImportValue::Func(env::_getpwnam as _),
);
import_object.set(
"env",
"_getgrnam",
ImportValue::Func(env::_getgrnam as _),
);
// Errno
import_object.set(
"env",
@ -273,18 +283,7 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
"nullFunc_viiii",
ImportValue::Func(nullfunc::nullfunc_viiii as *const u8),
);
// named syscalls
import_object.set(
"env",
"_getpwnam",
ImportValue::Func(env::_getpwnam as _),
);
import_object.set(
"env",
"_getgrnam",
ImportValue::Func(env::_getgrnam as _),
);
// Time
import_object.set(
"env",
"_gettimeofday",
@ -295,10 +294,20 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
"_clock_gettime",
ImportValue::Func(time::_clock_gettime as _),
);
import_object.set(
"env",
"_localtime",
ImportValue::Func(time::_localtime as _),
);
import_object.set(
"env",
"_time",
ImportValue::Func(time::_time as _),
);
mock_external!(import_object, _waitpid);
mock_external!(import_object, _utimes);
mock_external!(import_object, _usleep);
mock_external!(import_object, _time);
// mock_external!(import_object, _time);
mock_external!(import_object, _sysconf);
mock_external!(import_object, _strftime);
mock_external!(import_object, _sigsuspend);
@ -316,7 +325,7 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
mock_external!(import_object, _raise);
mock_external!(import_object, _mktime);
mock_external!(import_object, _localtime_r);
mock_external!(import_object, _localtime);
// mock_external!(import_object, _localtime);
mock_external!(import_object, _llvm_stacksave);
mock_external!(import_object, _llvm_stackrestore);
mock_external!(import_object, _kill);

View File

@ -1,5 +1,16 @@
use libc::{gettimeofday, timeval, c_int, clock_gettime, clockid_t, timespec};
use std::ptr;
use libc::{
gettimeofday,
timeval,
c_int,
clock_gettime,
clockid_t,
timespec,
tm,
localtime,
time_t,
time
};
use std::{ptr, slice, mem};
use crate::webassembly::Instance;
@ -30,3 +41,23 @@ pub extern "C" fn _clock_gettime(clk_id: clockid_t, tp_offset: c_int, instance:
returned
}
}
/// emscripten: _localtime
pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> *mut tm {
debug!("emscripten::_localtime {}", time_p);
unsafe {
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
localtime(time_p_addr)
}
}
/// emscripten: _time
pub extern "C" fn _time(time_p: u32, instance: &mut Instance) -> time_t {
debug!("emscripten::_time {}", time_p);
unsafe {
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
time(time_p_addr)
}
}

View File

@ -1,4 +1,9 @@
use crate::webassembly::module::Module;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::{slice, mem};
use crate::webassembly::Instance;
/// We check if a provided module is an Emscripten generated one
pub fn is_emscripten_module(module: &Module) -> bool {
@ -10,6 +15,33 @@ pub fn is_emscripten_module(module: &Module) -> bool {
return false;
}
pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char) -> u32 {
let s = CStr::from_ptr(cstr).to_str().unwrap();
let space_offset = (instance.emscripten_data.malloc)(s.len() as _, instance);
let raw_memory = instance.memory_offset_addr(0, space_offset as _) as *mut u8;
let mut slice = slice::from_raw_parts_mut(raw_memory, s.len());
for (byte, loc) in s.bytes().zip(slice.iter_mut()) {
*loc = byte;
}
space_offset
}
pub unsafe fn copy_terminated_array_of_cstrs(instance: &mut Instance, cstrs: *mut *mut c_char) -> u32 {
let total_num = {
let mut ptr = cstrs;
let mut counter = 0;
while !(*ptr).is_null() {
counter += 1;
ptr = ptr.add(1);
}
counter
};
println!("total_num: {}", total_num);
0
}
#[cfg(test)]
mod tests {
use super::super::generate_emscripten_env;