From acef87c56b87419f50887e98a380b3f882e44eb0 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 21 Jun 2024 20:00:34 -0500 Subject: [PATCH] wip ffi callback --- crates/panorama-app-sdk/src/lib.rs | 29 +++++++++++++++++++--- crates/panorama-core/src/state/apps.rs | 34 -------------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/crates/panorama-app-sdk/src/lib.rs b/crates/panorama-app-sdk/src/lib.rs index 3476a07..58fbfc0 100644 --- a/crates/panorama-app-sdk/src/lib.rs +++ b/crates/panorama-app-sdk/src/lib.rs @@ -1,17 +1,38 @@ use std::ffi::CString; +use sys::RegisterCallback; + pub mod sys { use std::ffi::c_char; + use crate::RegisterContext; + + pub type RegisterCallback = fn(*mut RegisterContext) -> (); + extern "C" { - pub fn register_endpoint(url_len: u64, url: *const c_char); + pub fn register_endpoint( + url_len: u64, + url: *const c_char, + callback: *mut RegisterCallback, + ); } } -pub fn register_endpoint(url: impl AsRef) { +pub struct RegisterContext {} + +pub fn register_endpoint( + url: impl AsRef, + callback: fn(&RegisterContext) -> (), +) { 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()) }; + let mut callback2 = |ctx: *mut RegisterContext| callback(&*ctx); + let result = unsafe { + sys::register_endpoint( + url.len() as u64, + url_cstr.into_raw(), + &mut callback2 as *mut RegisterCallback, + ) + }; println!("Result: {:?}", result); } diff --git a/crates/panorama-core/src/state/apps.rs b/crates/panorama-core/src/state/apps.rs index 9cbb708..a9ee076 100644 --- a/crates/panorama-core/src/state/apps.rs +++ b/crates/panorama-core/src/state/apps.rs @@ -138,40 +138,6 @@ impl AppState { .context("Could not get typed function")?; hello.call(&mut store, ()).context("Could not call")?; - // let mut sources = Sources::new(); - // sources - // .insert(Source::new("register.rn", register_script).into_diagnostic()?) - // .into_diagnostic()?; - - // let mut diagnostics = Diagnostics::new(); - // let register_script_unit = prepare(&mut sources) - // .with_diagnostics(&mut diagnostics) - // .build(); - // if !diagnostics.is_empty() { - // let mut writer = StandardStream::stderr(ColorChoice::Always); - // diagnostics.emit(&mut writer, &sources).into_diagnostic()?; - // } - // let register_script_unit = - // Arc::new(register_script_unit.into_diagnostic()?); - - // let module = Module::new(); - // // let mut ctx = Context::new(); - // let mut ctx = Context::with_default_modules().into_diagnostic()?; - // ctx.install(module).into_diagnostic()?; - - // let rt_ctx = ctx.runtime().into_diagnostic()?; - // let ctx_arc = Arc::new(rt_ctx); - // let mut vm = Vm::new(ctx_arc, register_script_unit); - - // let main = Hash::type_hash(["main"]); - // let result = vm - // .execute(main, ()) - // .into_diagnostic()? - // .complete() - // .into_result() - // .into_diagnostic()?; - // println!("Executed. {result:?}"); - Ok(()) } }