Skip to content

Instantly share code, notes, and snippets.

@JCBurnside
Last active February 26, 2024 14:53
Show Gist options
  • Save JCBurnside/6cef4a4e912c01c67b2efbab739db505 to your computer and use it in GitHub Desktop.
Save JCBurnside/6cef4a4e912c01c67b2efbab739db505 to your computer and use it in GitHub Desktop.
trait TupleCall {
type Args;
type Output;
unsafe fn call_tuple(&self,args:Self::Args)->Self::Output;
}
macro_rules! impl_unsafe_fn {
(@recurse $first:ident $( , $rest:ident )*) => {
impl_unsafe_fn!($( $rest ),*);
};
(@recurse) => {};
($( $param:ident ),*) => {
impl<Output, $( $param ),*> TupleCall for JitFunction<'_,unsafe extern "C" fn($( $param ),*) -> Output> {
type Args = ( $($param),* );
type Output = Output;
unsafe fn call_tuple(&self,($($param),*):($($param),*)) -> Output {
self.call($($param),*)
}
}
impl_unsafe_fn!(@recurse $( $param ),*);
};
}
impl_unsafe_fn!(A,B,C,D,E,F,G,H,I,J,K,L,M);
unsafe fn run_function<'this,'a,F:UnsafeFunctionPointer>(
&'this mut self,
name:&str,
args: <JitFunction<'a,F> as TupleCall>::Args
) -> <JitFunction<'a,F> as TupleCall>::Output
where JitFunction<'a,F> : TupleCall, 'this:'a {
self.with_dependent_mut(move |ctx,BoundItems {generator,engine, modules}| {
let new_module = ctx.create_module("");
let module = generator.replace_module(new_module);
engine.add_module(&module).unwrap();
modules.push(module);
let fun = engine.get_function::<F>(name).unwrap();
fun.call_tuple(args)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment