mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
add a "hello-rust" example
This commit is contained in:
parent
fdb44d2e78
commit
45887b6d1a
8 changed files with 135 additions and 0 deletions
58
examples/hello-rust/platform/src/lib.rs
Normal file
58
examples/hello-rust/platform/src/lib.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
#![allow(non_snake_case)]
|
||||
|
||||
use core::ffi::c_void;
|
||||
use core::mem::MaybeUninit;
|
||||
use roc_std::{RocCallResult, RocStr};
|
||||
|
||||
extern "C" {
|
||||
#[link_name = "roc__mainForHost_1_exposed"]
|
||||
fn roc_main(output: *mut RocCallResult<RocStr>) -> ();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void {
|
||||
return libc::malloc(size);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_realloc(
|
||||
c_ptr: *mut c_void,
|
||||
new_size: usize,
|
||||
_old_size: usize,
|
||||
_alignment: u32,
|
||||
) -> *mut c_void {
|
||||
return libc::realloc(c_ptr, new_size);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
|
||||
return libc::free(c_ptr);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn rust_main() -> isize {
|
||||
let mut call_result: MaybeUninit<RocCallResult<RocStr>> = MaybeUninit::uninit();
|
||||
|
||||
unsafe {
|
||||
roc_main(call_result.as_mut_ptr());
|
||||
|
||||
let output = call_result.assume_init();
|
||||
|
||||
match output.into() {
|
||||
Ok(roc_str) => {
|
||||
let len = roc_str.len();
|
||||
let str_bytes = roc_str.get_bytes() as *const libc::c_void;
|
||||
|
||||
if libc::write(1, str_bytes, len) < 0 {
|
||||
panic!("Writing to stdout failed!");
|
||||
}
|
||||
}
|
||||
Err(msg) => {
|
||||
panic!("Roc failed with message: {}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exit code
|
||||
0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue