Skip to content

Commit

Permalink
Merge #330
Browse files Browse the repository at this point in the history
330: implement strftime r=syrusakbary a=MarkMcCaskey



Co-authored-by: Mark McCaskey <mark@wasmer.io>
  • Loading branch information
bors[bot] and Mark McCaskey committed Apr 5, 2019
2 parents b03a05f + b0a8340 commit ea08a96
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions lib/emscripten/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,62 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 {

/// emscripten: _strftime
pub fn _strftime(
_ctx: &mut Ctx,
_s_ptr: c_int,
_maxsize: u32,
_format_ptr: c_int,
_tm_ptr: c_int,
ctx: &mut Ctx,
s_ptr: c_int,
maxsize: u32,
format_ptr: c_int,
tm_ptr: c_int,
) -> i32 {
debug!(
"emscripten::_strftime {} {} {} {}",
_s_ptr, _maxsize, _format_ptr, _tm_ptr
s_ptr, maxsize, format_ptr, tm_ptr
);
0

#[allow(clippy::cast_ptr_alignment)]
let s = emscripten_memory_pointer!(ctx.memory(0), s_ptr) as *mut c_char;
#[allow(clippy::cast_ptr_alignment)]
let format = emscripten_memory_pointer!(ctx.memory(0), format_ptr) as *const c_char;
#[allow(clippy::cast_ptr_alignment)]
let tm = emscripten_memory_pointer!(ctx.memory(0), tm_ptr) as *const guest_tm;

let format_string = unsafe { std::ffi::CStr::from_ptr(format).to_str().unwrap() };

debug!("=> format_string: {:?}", format_string);

let tm = unsafe { &*tm };

let rust_tm = ::time::Tm {
tm_sec: tm.tm_sec,
tm_min: tm.tm_min,
tm_hour: tm.tm_hour,
tm_mday: tm.tm_mday,
tm_mon: tm.tm_mon,
tm_year: tm.tm_year,
tm_wday: tm.tm_wday,
tm_yday: tm.tm_yday,
tm_isdst: tm.tm_isdst,
tm_utcoff: tm.tm_gmtoff,
tm_nsec: 0,
};

let result_str = match ::time::strftime(format_string, &rust_tm) {
Ok(res_string) => res_string,
// TODO: maybe match on e in Err(e) and return different values if required
_ => return 0,
};

// pad for null?
let bytes = result_str.chars().count();
if bytes as u32 > maxsize {
return 0;
} else {
// write output string
for (i, c) in result_str.chars().enumerate() {
unsafe { *s.add(i) = c as c_char };
}
// null terminate?
bytes as i32
}
}

/// emscripten: _strftime_l
Expand Down

0 comments on commit ea08a96

Please sign in to comment.