From 9c96f014946b230b73dc888eda9f6cb75d0c26b6 Mon Sep 17 00:00:00 2001 From: Syrus Date: Sun, 7 Jul 2019 15:55:52 -0700 Subject: [PATCH] Fixed emscripten ns collision. Added ctime implementation --- lib/emscripten/src/lib.rs | 6 +++++- lib/emscripten/src/time.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 60625255b..84c55bd3c 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -804,6 +804,8 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject "_strftime_l" => func!(crate::time::_strftime_l), "_localtime_r" => func!(crate::time::_localtime_r), "_gmtime_r" => func!(crate::time::_gmtime_r), + "_ctime" => func!(crate::time::_ctime), + "_ctime_r" => func!(crate::time::_ctime_r), "_mktime" => func!(crate::time::_mktime), "_gmtime" => func!(crate::time::_gmtime), @@ -981,7 +983,9 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject for (k, v) in env_ns.get_exports() { if k.starts_with("_") { let k = &k[1..]; - env_ns.insert(k, v.to_export()); + if !env_ns.contains_key(k) { + env_ns.insert(k, v.to_export()); + } } } diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index 845bc7adc..dc12abfe3 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -1,4 +1,5 @@ use super::utils::{copy_cstr_into_wasm, write_to_buf}; +use crate::allocate_on_stack; use libc::{c_char, c_int}; // use libc::{c_char, c_int, clock_getres, clock_settime}; use std::mem; @@ -313,6 +314,23 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 { } } +pub fn _ctime_r(ctx: &mut Ctx, time_p: u32, buf: u32) -> u32 { + debug!("emscripten::_ctime_r {} {}", time_p, buf); + + // var stack = stackSave(); + let (result_offset, _result_slice): (u32, &mut [u8]) = unsafe { allocate_on_stack(ctx, 44) }; + let time = _localtime_r(ctx, time_p, result_offset) as u32; + let rv = _asctime_r(ctx, time, buf); + // stackRestore(stack); + rv +} + +pub fn _ctime(ctx: &mut Ctx, time_p: u32) -> u32 { + debug!("emscripten::_ctime {}", time_p); + let tm_current = 2414544; + _ctime_r(ctx, time_p, tm_current) +} + /// emscripten: _timegm #[cfg(not(target_os = "windows"))] #[allow(clippy::cast_ptr_alignment)]