kp and kl are the key pointer and length in wasm memory, vp and vl are for the return value if read value is bigger than vl then it will be truncated to vl, returns read bytes
(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32)
| 11 | /// kp and kl are the key pointer and length in wasm memory, vp and vl are for the return value |
| 12 | /// if read value is bigger than vl then it will be truncated to vl, returns read bytes |
| 13 | fn kv_get(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 { |
| 14 | let (c, s) = ctx.data_and_store_mut(); |
| 15 | let mut key = vec![0u8; kl as usize]; |
| 16 | if c.mem |
| 17 | .as_ref() |
| 18 | .unwrap() |
| 19 | .view(&s) |
| 20 | .read(kp as u64, &mut key) |
| 21 | .is_err() |
| 22 | { |
| 23 | return -1; |
| 24 | } |
| 25 | match c.kv.get(&key) { |
| 26 | Some(val) => { |
| 27 | let len = val.len().min(vl as usize); |
| 28 | if c.mem |
| 29 | .as_ref() |
| 30 | .unwrap() |
| 31 | .view(&s) |
| 32 | .write(vp as u64, &val[..len]) |
| 33 | .is_err() |
| 34 | { |
| 35 | return -1; |
| 36 | } |
| 37 | len as i32 |
| 38 | } |
| 39 | None => 0, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// kp and kl are the key pointer and length in wasm memory, vp and vl are for the value |
| 44 | fn kv_put(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 { |