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;
#[wasm_bindgen]
pub fn install() -> i32 {
panorama_app_sdk::register_endpoint("/hello");
panorama_app_sdk::register_endpoint("/get_todays_date");
123
}

View file

@ -4,12 +4,14 @@ pub mod sys {
use std::ffi::c_char;
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>) {
let url = CString::new(url.as_ref()).unwrap();
let result = unsafe { sys::register_endpoint(url.into_raw()) };
let url = url.as_ref();
let url_cstr = CString::new(url).unwrap();
let result =
unsafe { sys::register_endpoint(url.len() as u64, url_cstr.into_raw()) };
println!("Result: {:?}", result);
}

View file

@ -106,30 +106,37 @@ impl AppState {
let module = Module::new(&engine, &installer_program)?;
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);
linker.func_wrap(
"env",
"register_endpoint",
|caller: Caller<'_, _>, param: i32| {
println!("Got {} , from WebAssembly", param);
|mut caller: Caller<'_, _>, url_len: i64, url: i32| {
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());
},
)?;
linker.func_wrap(
"env",
"abort",
|caller: Caller<'_, _>, param: i32, _: i32, _: i32, _: i32| {
println!("Oops, aborted.");
},
)?;
let instance = linker.instantiate(&mut store, &module)?;
let hello = instance.get_typed_func::<(), i32>(&mut store, "install")?;
hello.call(&mut store, ())?;
let mut store: Store<_> = Store::new(&engine, wasi);
let instance = linker
.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();
// sources

View file

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

View file

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