From c53f03ad06c844388d6f4ce845a67333b8340ee1 Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 6 Oct 2020 12:32:15 +0300 Subject: [PATCH] update --- crates/main/src/lib.rs | 9 ++------- crates/main/src/logger.rs | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/main/src/lib.rs b/crates/main/src/lib.rs index 92ea33b..6ff74fc 100644 --- a/crates/main/src/lib.rs +++ b/crates/main/src/lib.rs @@ -53,13 +53,8 @@ pub use result::set_result_size; pub(crate) fn log>(msg: S) { // logs will be printed only if debug feature is enabled #[cfg(feature = "debug")] - unsafe { + { let msg = msg.as_ref(); - - if cfg!(target_arch = "wasm32") { - logger::log_utf8_string(msg.as_ptr() as i32, msg.len() as i32); - } else { - println!("{}", msg); - } + logger::log_utf8_string(msg.as_ptr() as i32, msg.len() as i32); } } diff --git a/crates/main/src/logger.rs b/crates/main/src/logger.rs index e2f3a64..e86453a 100644 --- a/crates/main/src/logger.rs +++ b/crates/main/src/logger.rs @@ -144,12 +144,7 @@ impl log::Log for WasmLogger { record.args() ); - // this allows building sdk for x86_64 - if cfg!(target_arch = "wasm32") { - unsafe { log_utf8_string(log_msg.as_ptr() as i32, log_msg.len() as i32) }; - } else { - println!("{}", log_msg); - } + log_utf8_string(log_msg.as_ptr() as _, log_msg.len() as _); } // in our case flushing is performed by the VM itself @@ -157,10 +152,22 @@ impl log::Log for WasmLogger { fn flush(&self) {} } +#[cfg(target_arch = "wasm32")] +pub fn log_utf8_string(ptr: i32, size: i32) { + unsafe { log_utf8_string_impl(ptr, size) }; +} + +#[cfg(not(target_arch = "wasm32"))] +pub fn log_utf8_string(ptr: i32, size: i32) { + let msg = unsafe { from_utf8_unchecked(core::slice::from_raw_parts(ptr as _, size as _)) }; + println!("{}", msg); +} + /// log_utf8_string should be provided directly by a host. #[cfg(target_arch = "wasm32")] #[link(wasm_import_module = "host")] extern "C" { // Writes a byte string of size bytes that starts from ptr to a logger - pub fn log_utf8_string(ptr: i32, size: i32); + #[link_name = "log_utf8_string"] + fn log_utf8_string_impl(ptr: i32, size: i32); }