From 1cccc2752a11b90d2a519f0e62dba4ca7b8a11c6 Mon Sep 17 00:00:00 2001 From: fgh1999 Date: Mon, 1 Jul 2024 20:35:46 +0800 Subject: [PATCH] Fix memory base type in WASI wrapper rust example --- examples/wasi-wrapper/rust/build.rs | 12 ++----- examples/wasi-wrapper/rust/src/memory.rs | 40 +++++++++--------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/examples/wasi-wrapper/rust/build.rs b/examples/wasi-wrapper/rust/build.rs index e0956ae..e884844 100644 --- a/examples/wasi-wrapper/rust/build.rs +++ b/examples/wasi-wrapper/rust/build.rs @@ -17,21 +17,13 @@ fn main() { .status() .expect("failed to compile target into WASM"); Command::new("wasker") - .args([ - "-o", - &target_obj_name, - "target/wasm32-wasi/debug/rust.wasm", - ]) + .args(["-o", &target_obj_name, "target/wasm32-wasi/debug/rust.wasm"]) .status() .expect("failed to compile target into obj"); let target_lib_name = format!("lib{}.a", target_name); Command::new("ar") - .args([ - "rcs", - &target_lib_name, - &target_obj_name, - ]) + .args(["rcs", &target_lib_name, &target_obj_name]) .status() .expect("failed to convert obj into lib"); diff --git a/examples/wasi-wrapper/rust/src/memory.rs b/examples/wasi-wrapper/rust/src/memory.rs index 919cf68..7783917 100644 --- a/examples/wasi-wrapper/rust/src/memory.rs +++ b/examples/wasi-wrapper/rust/src/memory.rs @@ -9,28 +9,28 @@ static LINEAR_MEMORY_BLOCK_NUM: Mutex = Mutex::new(0); #[inline] pub unsafe fn get_memory_base() -> *mut u8 { - unsafe { LINEAR_MEMORY_BASE } + LINEAR_MEMORY_BASE } unsafe fn alloc_memory() -> *mut u8 { use std::alloc::{alloc, Layout}; - unsafe { - LINEAR_MEMORY_BASE = alloc( - Layout::from_size_align( - (LINEAR_MEMORY_BLOCK_SIZE * LINEAR_MEMORY_BLOCK_NUM_MAX) as usize, - 8, - ) - .unwrap(), - ); - LINEAR_MEMORY_BASE - } + LINEAR_MEMORY_BASE = alloc( + Layout::from_size_align( + (LINEAR_MEMORY_BLOCK_SIZE * LINEAR_MEMORY_BLOCK_NUM_MAX) as usize, + 8, + ) + .unwrap(), + ); + LINEAR_MEMORY_BASE } -// fn get_memory_block_num() -> i32 { -// LINEAR_MEMORY_BLOCK_NUM.lock().unwrap().clone() -// } +#[no_mangle] +pub extern "C" fn memory_base() -> usize { + unsafe { alloc_memory() as usize } +} -fn inc_memory_block_num(block_num: i32) -> i32 { +#[no_mangle] +pub extern "C" fn memory_grow(block_num: i32) -> i32 { assert!( block_num >= 0, "block_num must be greater than or equal to 0" @@ -44,13 +44,3 @@ fn inc_memory_block_num(block_num: i32) -> i32 { *num += block_num; old_val } - -#[no_mangle] -pub extern "C" fn memory_base() -> i32 { - unsafe { alloc_memory() as i32 } -} - -#[no_mangle] -pub extern "C" fn memory_grow(block_num: i32) -> i32 { - inc_memory_block_num(block_num) -}