mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
71 lines
1.6 KiB
Rust
71 lines
1.6 KiB
Rust
#![allow(non_snake_case)]
|
|
|
|
use core::ffi::c_void;
|
|
use roc_std::RocStr;
|
|
use std::ffi::CStr;
|
|
use std::os::raw::c_char;
|
|
|
|
extern "C" {
|
|
#[link_name = "roc__mainForHost_1_exposed"]
|
|
fn roc_main() -> RocStr;
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void {
|
|
return libc::malloc(size);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" 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 extern "C" fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
|
|
return libc::free(c_ptr);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_panic(c_ptr: *mut c_void, tag_id: u32) {
|
|
match tag_id {
|
|
0 => {
|
|
let slice = CStr::from_ptr(c_ptr as *const c_char);
|
|
let string = slice.to_str().unwrap();
|
|
eprintln!("Roc hit a panic: {}", string);
|
|
std::process::exit(1);
|
|
}
|
|
_ => todo!(),
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_memcpy(dst: *mut c_void, src: *mut c_void, n: usize) -> *mut c_void {
|
|
libc::memcpy(dst, src, n)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut c_void {
|
|
libc::memset(dst, c, n)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn rust_main() -> i32 {
|
|
unsafe {
|
|
let roc_str = roc_main();
|
|
|
|
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!");
|
|
}
|
|
}
|
|
|
|
// Exit code
|
|
0
|
|
}
|