From 00ba15b0fdc21ae24e95d40ed4f4564fbb2db9eb Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Sun, 22 Dec 2024 18:44:46 +0530 Subject: [PATCH] trying to generalise helpers --- fastn-ds/src/wasm/helpers.rs | 39 ++++++++++++++---------------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/fastn-ds/src/wasm/helpers.rs b/fastn-ds/src/wasm/helpers.rs index 15f66bebe8..d7887e85e6 100644 --- a/fastn-ds/src/wasm/helpers.rs +++ b/fastn-ds/src/wasm/helpers.rs @@ -1,13 +1,10 @@ -pub async fn str( - str: &str, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, -) -> wasmtime::Result { +pub async fn str(str: &str, caller: &mut wasmtime::Caller<'_, S>) -> wasmtime::Result { send_bytes(str.as_bytes(), caller).await } -pub async fn send_bytes( +pub async fn send_bytes( bytes: &[u8], - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result { let ptr = alloc(bytes.len() as i32, caller).await?; @@ -17,36 +14,36 @@ pub async fn send_bytes( Ok(ptr) } -pub fn get_str( +pub fn get_str( ptr: i32, len: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result { get_bytes(ptr, len, caller).map(|v| unsafe { String::from_utf8_unchecked(v) }) } -pub async fn send_json( +pub async fn send_json( t: T, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result { let bytes = serde_json::to_vec(&t).unwrap(); send_bytes(&bytes, caller).await } -pub fn get_json( +pub fn get_json( ptr: i32, len: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result { let bytes = get_bytes(ptr, len, caller)?; Ok(serde_json::from_slice(&bytes).unwrap()) } #[allow(clippy::uninit_vec)] -pub fn get_bytes( +pub fn get_bytes( ptr: i32, len: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result> { let mem = caller.get_export("memory").unwrap().into_memory().unwrap(); let mut buf: Vec = Vec::with_capacity(len as usize); @@ -58,10 +55,7 @@ pub fn get_bytes( Ok(buf) } -async fn _dealloc( - ptr: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, -) -> wasmtime::Result<()> { +async fn _dealloc(ptr: i32, caller: &mut wasmtime::Caller<'_, S>) -> wasmtime::Result<()> { let mut result = vec![wasmtime::Val::I32(0)]; let dealloc = caller .get_export("dealloc") @@ -80,10 +74,10 @@ async fn _dealloc( res } -async fn _dealloc_with_len( +async fn _dealloc_with_len( ptr: i32, len: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, + caller: &mut wasmtime::Caller<'_, S>, ) -> wasmtime::Result<()> { let mut result = vec![wasmtime::Val::I32(0)]; let dealloc_with_len = caller @@ -107,10 +101,7 @@ async fn _dealloc_with_len( res } -async fn alloc( - size: i32, - caller: &mut wasmtime::Caller<'_, fastn_ds::wasm::Store>, -) -> wasmtime::Result { +async fn alloc(size: i32, caller: &mut wasmtime::Caller<'_, S>) -> wasmtime::Result { let mut result = vec![wasmtime::Val::I32(0)]; let alloc = caller .get_export("alloc")