commit 3dad3d3675fc9320fc8f5c6d844afa7fb865a92a
parent 4f016c6b583d8c7cc7ae7618f503beed330e0d1c
Author: parazyd <parazyd@dyne.org>
Date: Wed, 9 Mar 2022 15:58:21 +0100
runtime: Move logging facility to util module.
Diffstat:
4 files changed, 36 insertions(+), 33 deletions(-)
diff --git a/Makefile b/Makefile
@@ -14,7 +14,7 @@ wabt:
git clone --recursive https://github.com/WebAssembly/wabt $@
$(MAKE) -C $@
-smart_contract.wasm: wabt $(SRC)
+smart_contract.wasm: $(SRC)
cd smart-contract && $(CARGO) build --release --lib --target wasm32-unknown-unknown
cp -f smart-contract/target/wasm32-unknown-unknown/release/$@ $@
diff --git a/drk-sdk/src/log.rs b/drk-sdk/src/log.rs
@@ -17,6 +17,7 @@ pub fn drk_log(message: &str) {
println!("{}", message);
}
+#[cfg(target_arch = "wasm32")]
extern "C" {
pub fn drk_log_(ptr: *const u8, len: usize);
}
diff --git a/src/runtime.rs b/src/runtime.rs
@@ -11,7 +11,7 @@ use wasmer_middlewares::{
Metering,
};
-use crate::memory::MemoryManipulation;
+use crate::{memory::MemoryManipulation, util::drk_log};
/// Function name in our wasm module that allows us to allocate some memory
const WASM_MEM_ALLOC: &str = "__drkruntime_mem_alloc";
@@ -28,6 +28,17 @@ pub struct Env {
pub memory: LazyInit<Memory>,
}
+impl WasmerEnv for Env {
+ fn init_with_instance(
+ &mut self,
+ instance: &Instance,
+ ) -> std::result::Result<(), HostEnvInitError> {
+ let memory: Memory = instance.exports.get_with_generics_weak("memory").unwrap();
+ self.memory.initialize(memory);
+ Ok(())
+ }
+}
+
pub struct Runtime {
pub(crate) instance: Instance,
pub(crate) env: Env,
@@ -151,34 +162,3 @@ impl Runtime {
Ok(self.instance.exports.get_memory(MEMORY)?)
}
}
-
-/// Host function for logging strings. This is injected into the runtime.
-fn drk_log(env: &Env, ptr: u32, len: u32) {
- if let Some(bytes) = env.memory.get_ref().unwrap().read(ptr, len as usize) {
- // Piece the string together
- let msg = match String::from_utf8(bytes.to_vec()) {
- Ok(v) => v,
- Err(e) => {
- println!("Invalid UTF-8 string: {:?}", e);
- return
- }
- };
-
- let mut logs = env.logs.lock().unwrap();
- logs.push(msg);
- return
- }
-
- println!("Failed to read any bytes from VM memory");
-}
-
-impl WasmerEnv for Env {
- fn init_with_instance(
- &mut self,
- instance: &Instance,
- ) -> std::result::Result<(), HostEnvInitError> {
- let memory: Memory = instance.exports.get_with_generics_weak("memory").unwrap();
- self.memory.initialize(memory);
- Ok(())
- }
-}
diff --git a/src/util.rs b/src/util.rs
@@ -1,3 +1,5 @@
+use crate::{memory::MemoryManipulation, runtime::Env};
+
/// Serialize payload to format accepted by the runtime entrypoint
pub fn serialize_payload(payload: &[u8]) -> Vec<u8> {
let mut out = vec![];
@@ -8,3 +10,23 @@ pub fn serialize_payload(payload: &[u8]) -> Vec<u8> {
out
}
+
+/// Host function for logging strings. This is injected into the runtime.
+pub(crate) fn drk_log(env: &Env, ptr: u32, len: u32) {
+ if let Some(bytes) = env.memory.get_ref().unwrap().read(ptr, len as usize) {
+ // Piece the string together
+ let msg = match String::from_utf8(bytes.to_vec()) {
+ Ok(v) => v,
+ Err(e) => {
+ println!("Invalid UTF-8 string: {:?}", e);
+ return
+ }
+ };
+
+ let mut logs = env.logs.lock().unwrap();
+ logs.push(msg);
+ return
+ }
+
+ println!("Failed to read any bytes from VM memory");
+}