5

I have programmatically constructed a C function in memory, and I'm able to call it in Rust like so.

type AddFn = extern "C" fn(isize, isize) -> isize;
let Add = build_function::<AddFn>(&region, code);

fn build_function<T>(region: &MappedRegion, contents: &[u8]) -> Box<T> {
    unsafe {
        ...
        mem::transmute(Box::new(region.addr))
    }
}  

This requires me to know the signature for my generated function, in this case AddFn. I would like to be able to construct this signature at runtime e.g. let x = 3u8; macro!(x, f64) == fn(f64, f64, f64) -> f64. I can find no means to generate functions in this manner. A related issue is the inability to call functions generated at runtime. Without partial application or a means to accomplish the logic above I cannot figure out how to call a generated function without a known signature. Is it possible to generate a function signature programmatically this way in Rust?

UPDATE: Rust's support for variadic C functions (extern "C" fn(x:int, ...) allows for arbitrary sysv ABI compatible function signatures. Now the only issue is calling them at runtime. Is there any way to perform partial application on named functions?

ragingSloth
  • 1,094
  • 8
  • 22
  • This is just my opinion, but it really feels like you are trying to bend Rust in ways that it wasn't meant to be used. I view Rust as an excellent statically typed language, but that typing is just that — static. It's an interesting question, but you *might* get more useful answers if you include what goal you are trying to accomplish, using your work to date as an example of what you've tried. – Shepmaster Aug 15 '15 at 12:50
  • I'm trying to JIT compile some code. Programmatic signature creation makes that easier to do while maintaining sysv ABI compatability. There are other ways to skin the cat, but being able to build a function signature at runtime seems like the most elegant as it saves me the trouble of manually setting up registers and stack, i.e. generic dynamic dispatch. I've mostly worked with macros, trying to turn tuples into arguments, I was hoping for something like *args, but as you said it breaks the typing. I think closure currying could work, but I'm not sure it can maintain ABI compatability. – ragingSloth Aug 15 '15 at 19:50
  • 1
    https://github.com/rust-lang/rfcs/issues/376 seems relevant – ragingSloth Aug 15 '15 at 20:06

0 Answers0