mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 14:54:47 +00:00
add closure example that is passed to the host
This commit is contained in:
parent
00445b3bc6
commit
95a69f5a9b
5 changed files with 89 additions and 0 deletions
38
examples/closure/platform/src/lib.rs
Normal file
38
examples/closure/platform/src/lib.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use std::mem::MaybeUninit;
|
||||
use std::time::SystemTime;
|
||||
|
||||
extern "C" {
|
||||
#[link_name = "closure_1_exposed"]
|
||||
fn closure(output: *mut u8) -> ();
|
||||
|
||||
#[link_name = "closure_1_size"]
|
||||
fn closure_size() -> i64;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn rust_main() -> isize {
|
||||
println!("Running Roc closure");
|
||||
let start_time = SystemTime::now();
|
||||
let (function_pointer, closure_data) = unsafe {
|
||||
let mut output: MaybeUninit<(fn(i64) -> i64, i64)> = MaybeUninit::uninit();
|
||||
|
||||
closure(output.as_mut_ptr() as _);
|
||||
|
||||
output.assume_init()
|
||||
};
|
||||
let answer = function_pointer(closure_data);
|
||||
let end_time = SystemTime::now();
|
||||
let duration = end_time.duration_since(start_time).unwrap();
|
||||
|
||||
println!(
|
||||
"Roc closure took {:.4} ms to compute this answer: {:?}",
|
||||
duration.as_secs_f64() * 1000.0,
|
||||
// truncate the answer, so stdout is not swamped
|
||||
answer
|
||||
);
|
||||
|
||||
println!("closure size {:?}", unsafe { closure_size() });
|
||||
|
||||
// Exit code
|
||||
0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue