wasi_binio_shared_mods/lib.rs
1//! # wasi_binio_shared for wasi_binio_host and wasi_binio_wasm
2//!
3//! `wasi_binio_wasm` is the host crate of wasm_binio. Another crate is `wasi_binio_host` which used in host.
4//! wasi_binio_host creates a call_stub so that host can call a webassembly function with complex data structure arguments and results.
5//! wasi_binio_wasm prepare the linear memory buffer and exports required wasm function so that the host can call.
6//!
7//! As of today, wasm function can only take i32 i64 f32 f64 types. If you have a function in wasm like this
8//! In wasm: ` do_compute(point1: &Point, point2: &Point)->Rect {...}`
9//! there is no way to call it directly from host.
10//! With the help from wasm_binio, we can instead call the call_stub function and send arguments and results like this
11//!
12//!
13
14/// Combain two i32 into one i64
15pub fn join_i32_to_i64( a:i32, b:i32)->i64 {
16 //((a as i64) << 32) | (b as i64)
17 (((a as u64) << 32) | (b as u64) & 0x00000000ffffffff) as i64
18}
19/// Split one i64 into two i32
20pub fn split_i64_to_i32( r: i64)->(i32,i32){
21 ( (((r as u64) & 0xffffffff00000000) >> 32) as i32 , ((r as u64) & 0x00000000ffffffff) as i32)
22}
23
24#[cfg(test)]
25mod test{
26
27 use super::*;
28 #[test]
29
30 fn test_i64_i32_convertor(){
31 let a = (i32::MAX, i32::MAX);
32 assert_eq!(a, split_i64_to_i32(join_i32_to_i64(a.0, a.1)));
33
34 let a = (i32::MIN, i32::MAX);
35 assert_eq!(a, split_i64_to_i32(join_i32_to_i64(a.0, a.1)));
36
37 let a = (i32::MAX, i32::MIN);
38 assert_eq!(a, split_i64_to_i32(join_i32_to_i64(a.0, a.1)));
39 let a = (i32::MIN, i32::MIN);
40 assert_eq!(a, split_i64_to_i32(join_i32_to_i64(a.0, a.1)));
41 let a = (23, -32);
42 assert_eq!(a, split_i64_to_i32(join_i32_to_i64(a.0, a.1)));
43 }
44
45}