curryrs icon indicating copy to clipboard operation
curryrs copied to clipboard

Implement a Haskell trait

Open mgattozzi opened this issue 7 years ago • 4 comments

Not sure if this is feasible but it might be worth looking into.

https://www.reddit.com/r/rust/comments/59fm88/curryrs_020_released_it_can_now_call_the_haskell/d989jc7/

mgattozzi avatar Oct 26 '16 11:10 mgattozzi

That's basically what I was hoping the binding generator will be able to do if std::sync::Once or similar is fast enough. There's not much point in shutting down the runtime before the end of the program though, as GHC can't currently start it back up after it's been shutdown.

nanotech avatar Oct 29 '16 01:10 nanotech

That would explain the error I ran into then. So if we had a way to start it the first time a Haskell function is called then automatically stopping it at the end of the program that would be good. We could start it in it's own thread I guess and have a cleanup function?

mgattozzi avatar Oct 29 '16 16:10 mgattozzi

libc::atexit works:

extern crate libc;

extern fn goodbye() {
    unsafe {
        libc::puts(b"Goodbye, world!\0" as *const u8 as *const i8);
    }
}

fn main() {
    unsafe { libc::atexit(goodbye) };
    println!("Hello, world!");
}
$ cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/rust-atexit`
Hello, world!
Goodbye, world!

I needed to use libc::puts, as using println! crashes with a double-free. I guess the Rust stdio has already been shutdown at that point. Calling hs_exit there should be fine though.

nanotech avatar Oct 30 '16 00:10 nanotech

Ah okay I see. So we could use libc to have soemthing run at the end of the code. Would putting that in hsrt::start be a good idea? I think giving users two options, manually start and stop, or start and have the code stop on exit would be a good idea. With the manual one we should leave a warning about it not starting back up again.

mgattozzi avatar Oct 30 '16 17:10 mgattozzi