wip ffi callback

This commit is contained in:
Michael Zhang 2024-06-21 20:00:34 -05:00
parent 1942d22451
commit acef87c56b
2 changed files with 25 additions and 38 deletions

View file

@ -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<str>) {
pub struct RegisterContext {}
pub fn register_endpoint(
url: impl AsRef<str>,
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);
}

View file

@ -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(())
}
}