retrieve string from memory

This commit is contained in:
Michael Zhang 2024-06-21 19:48:53 -05:00
parent dff72850ce
commit 1942d22451
6 changed files with 33 additions and 48 deletions

View file

@ -1,9 +1,7 @@
use std::ffi::CString;
use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen] #[wasm_bindgen]
pub fn install() -> i32 { pub fn install() -> i32 {
panorama_app_sdk::register_endpoint("/hello"); panorama_app_sdk::register_endpoint("/get_todays_date");
123 123
} }

View file

@ -4,12 +4,14 @@ pub mod sys {
use std::ffi::c_char; use std::ffi::c_char;
extern "C" { extern "C" {
pub fn register_endpoint(url: *const c_char); pub fn register_endpoint(url_len: u64, url: *const c_char);
} }
} }
pub fn register_endpoint(url: impl AsRef<str>) { pub fn register_endpoint(url: impl AsRef<str>) {
let url = CString::new(url.as_ref()).unwrap(); let url = url.as_ref();
let result = unsafe { sys::register_endpoint(url.into_raw()) }; let url_cstr = CString::new(url).unwrap();
let result =
unsafe { sys::register_endpoint(url.len() as u64, url_cstr.into_raw()) };
println!("Result: {:?}", result); println!("Result: {:?}", result);
} }

View file

@ -106,30 +106,37 @@ impl AppState {
let module = Module::new(&engine, &installer_program)?; let module = Module::new(&engine, &installer_program)?;
let wasi = WasiCtxBuilder::new().inherit_stdio().inherit_args().build(); let wasi = WasiCtxBuilder::new().inherit_stdio().inherit_args().build();
let mut store: Store<_> = Store::new(&engine, wasi);
let ty = MemoryType::new64(0, None);
let memory = Memory::new(&mut store, ty)?;
let mut linker = Linker::new(&engine); let mut linker = Linker::new(&engine);
linker.func_wrap( linker.func_wrap(
"env", "env",
"register_endpoint", "register_endpoint",
|caller: Caller<'_, _>, param: i32| { |mut caller: Caller<'_, _>, url_len: i64, url: i32| {
println!("Got {} , from WebAssembly", param); println!("WTF? {url_len} {url}");
let mem = caller.get_export("memory").and_then(|e| e.into_memory());
if let Some(mem) = mem {
let mut buffer = vec![0; url_len as usize];
mem.read(caller, url as usize, &mut buffer);
let string = String::from_utf8(buffer);
println!("{:?}", string);
}
// println!("my host state is: {}", caller.data()); // println!("my host state is: {}", caller.data());
}, },
)?; )?;
linker.func_wrap(
"env",
"abort",
|caller: Caller<'_, _>, param: i32, _: i32, _: i32, _: i32| {
println!("Oops, aborted.");
},
)?;
let instance = linker.instantiate(&mut store, &module)?; let mut store: Store<_> = Store::new(&engine, wasi);
let hello = instance.get_typed_func::<(), i32>(&mut store, "install")?; let instance = linker
hello.call(&mut store, ())?; .instantiate(&mut store, &module)
.context("Could not instantiate")?;
instance.exports(&mut store).for_each(|export| {
println!("Export: {}", export.name());
});
let hello = instance
.get_typed_func::<(), i32>(&mut store, "install")
.context("Could not get typed function")?;
hello.call(&mut store, ()).context("Could not call")?;
// let mut sources = Sources::new(); // let mut sources = Sources::new();
// sources // sources

View file

@ -1,10 +1,7 @@
use axum::{extract::State, routing::get, Json, Router}; use axum::{Router};
use chrono::Local;
use serde_json::Value;
use utoipa::OpenApi; use utoipa::OpenApi;
use uuid::Uuid;
use crate::{error::AppResult, AppState}; use crate::{AppState};
/// Node API /// Node API
#[derive(OpenApi)] #[derive(OpenApi)]

View file

@ -1,8 +1,4 @@
use axum::{extract::State, Json};
use panorama_core::AppState;
use serde_json::Value;
use crate::error::AppResult;
// pub async fn get_mail_config( // pub async fn get_mail_config(
// State(state): State<AppState>, // State(state): State<AppState>,

View file

@ -1,25 +1,10 @@
use std::{
collections::{BTreeMap, HashMap},
str::FromStr,
};
use axum::{ use axum::{
extract::{Path, Query, State}, Router,
http::StatusCode,
routing::{get, post, put},
Json, Router,
}; };
use chrono::{DateTime, Utc}; use utoipa::{OpenApi};
use itertools::Itertools;
use panorama_core::{
// state::node::{CreateOrUpdate, ExtraData},
NodeId,
};
use serde_json::Value;
use utoipa::{OpenApi, ToSchema};
use uuid::Uuid;
use crate::{error::AppResult, AppState}; use crate::{AppState};
/// Node API /// Node API
#[derive(OpenApi)] #[derive(OpenApi)]