refactor localtime functions to use time crate

lint fixes
This commit is contained in:
Mackenzie Clark 2018-12-27 09:41:29 -08:00
parent 4942a5b260
commit cb65c9dad6
7 changed files with 38 additions and 46 deletions

View File

@ -11,11 +11,12 @@ use std::process::Command;
static BANNER: &str = "// Rust test file autogenerated with cargo build (build/emtests.rs).
// Please do NOT modify it by hand, as it will be reseted on next build.\n";
const TESTS: [&str; 4] = [
const TESTS: [&str; 5] = [
"emtests/env.c",
"emtests/puts.c",
"emtests/printf.c",
"emtests/clock_gettime.c",
"emtests/localtime.c",
];
pub fn compile(file: &str) -> String {

View File

@ -2,19 +2,14 @@
#include <time.h>
int main (int argc, char *argv[]) {
printf("Hello wasmer!\n");
time_t rawtime;
struct tm *info;
char buffer[80];
time( &rawtime );
info = localtime( &rawtime );
printf("Almost!\n");
printf("Current local time and date: %s\n", asctime(info));
printf("Done!\n");
struct tm info2;
time (&rawtime );
struct tm *p = localtime_r(&rawtime, &info2);
printf("localtime\n");
return(0);
}

1
emtests/localtime.output Normal file
View File

@ -0,0 +1 @@
localtime

Binary file not shown.

View File

@ -74,17 +74,15 @@ pub extern "C" fn _difftime(t0: u32, t1: u32) -> c_int {
#[repr(C)]
struct guest_tm {
pub tm_sec: c_int, // 0
pub tm_min: c_int, // 4
pub tm_hour: c_int, // 8
pub tm_mday: c_int, // 12
pub tm_mon: c_int, // 16
pub tm_year: c_int, // 20
pub tm_wday: c_int, // 24
pub tm_yday: c_int, // 28
pub tm_isdst: c_int, // 32
pub tm_gmtoff: c_int, // 36
pub tm_zone: c_int, // 40
pub tm_sec: c_int, // 0
pub tm_min: c_int, // 4
pub tm_hour: c_int, // 8
pub tm_mday: c_int, // 12
pub tm_mon: c_int, // 16
pub tm_year: c_int, // 20
pub tm_wday: c_int, // 24
pub tm_yday: c_int, // 28
pub tm_isdst: c_int, // 32
}
/// emscripten: _tvset
@ -155,21 +153,24 @@ pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> c_int {
// NOTE: emscripten seems to want tzset() called in this function
// https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r
let timespec = unsafe {
let mut time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
let seconds = *time_p_addr.clone();
time::Timespec::new(seconds, 0)
};
let result_tm = time::at(timespec);
unsafe {
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
let result_tm = &*localtime(time_p_addr);
let tm_struct_offset = (instance.emscripten_data.as_ref().unwrap().malloc)(
mem::size_of::<guest_tm>() as _,
instance,
);
let tm_struct_ptr = instance.memory_offset_addr(0, tm_struct_offset as _) as *mut guest_tm;
// debug!(
// ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}",
// result_tm.tm_sec, result_tm.tm_min, result_tm.tm_hour, result_tm.tm_mday,
// result_tm.tm_mon, result_tm.tm_year, result_tm.tm_wday, result_tm.tm_yday,
// );
(*tm_struct_ptr).tm_sec = result_tm.tm_sec;
(*tm_struct_ptr).tm_min = result_tm.tm_min;
(*tm_struct_ptr).tm_hour = result_tm.tm_hour;
@ -179,8 +180,6 @@ pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> c_int {
(*tm_struct_ptr).tm_wday = result_tm.tm_wday;
(*tm_struct_ptr).tm_yday = result_tm.tm_yday;
(*tm_struct_ptr).tm_isdst = result_tm.tm_isdst;
(*tm_struct_ptr).tm_gmtoff = result_tm.tm_gmtoff as i32;
(*tm_struct_ptr).tm_zone = copy_cstr_into_wasm(instance, result_tm.tm_zone) as i32;
tm_struct_offset as _
}
@ -194,30 +193,18 @@ pub extern "C" fn _localtime_r(time_p: u32, result: u32, instance: &mut Instance
unsafe {
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
let result_addr = instance.memory_offset_addr(0, result as _) as *mut guest_tm;
let seconds = *time_p_addr.clone();
let timespec = time::Timespec::new(seconds, 0);
let result_tm = time::at(timespec);
let mut result_tm = tm {
tm_sec: (*result_addr).tm_sec,
tm_min: (*result_addr).tm_min,
tm_hour: (*result_addr).tm_hour,
tm_mday: (*result_addr).tm_mday,
tm_mon: (*result_addr).tm_mon,
tm_year: (*result_addr).tm_year,
tm_wday: (*result_addr).tm_wday,
tm_yday: (*result_addr).tm_yday,
tm_isdst: (*result_addr).tm_isdst,
tm_gmtoff: (*result_addr).tm_gmtoff as _,
tm_zone: instance.memory_offset_addr(0, (*result_addr).tm_zone as _) as _,
};
localtime_r(time_p_addr, &mut result_tm);
// let tm_struct = result_tm;
// debug!(
// ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}",
// result_tm.tm_sec, result_tm.tm_min, result_tm.tm_hour, result_tm.tm_mday,
// result_tm.tm_mon, result_tm.tm_year, result_tm.tm_wday, result_tm.tm_yday,
// );
let result_addr = instance.memory_offset_addr(0, result as _) as *mut guest_tm;
(*result_addr).tm_sec = result_tm.tm_sec;
(*result_addr).tm_min = result_tm.tm_min;
(*result_addr).tm_hour = result_tm.tm_hour;
@ -227,8 +214,6 @@ pub extern "C" fn _localtime_r(time_p: u32, result: u32, instance: &mut Instance
(*result_addr).tm_wday = result_tm.tm_wday;
(*result_addr).tm_yday = result_tm.tm_yday;
(*result_addr).tm_isdst = result_tm.tm_isdst;
(*result_addr).tm_gmtoff = result_tm.tm_gmtoff as _;
(*result_addr).tm_zone = copy_cstr_into_wasm(instance, result_tm.tm_zone) as _;
result as _
}

9
src/emtests/localtime.rs Normal file
View File

@ -0,0 +1,9 @@
#[test]
fn test_localtime() {
assert_emscripten_output!(
"../../emtests/localtime.wasm",
"localtime",
vec![],
"../../emtests/localtime.output"
);
}

View File

@ -6,5 +6,6 @@
mod _common;
mod clock_gettime;
mod env;
mod localtime;
mod printf;
mod puts;