11#![ allow( non_snake_case) ]
2-
2+ #! [ allow ( improper_ctypes ) ]
33use core:: alloc:: Layout ;
44use core:: ffi:: c_void;
55use core:: mem:: MaybeUninit ;
@@ -43,16 +43,19 @@ extern "C" {
4343 #[ allow( dead_code) ]
4444 #[ link_name = "roc__mainForHost_0_size" ]
4545 fn size_Fx ( ) -> i64 ;
46-
47- #[ link_name = "roc__mainForHost_0_result_size" ]
48- fn size_Fx_result ( ) -> i64 ;
4946}
5047
48+ /// # Safety
49+ ///
50+ /// This function is unsafe.
5151#[ no_mangle]
5252pub unsafe extern "C" fn roc_alloc ( size : usize , _alignment : u32 ) -> * mut c_void {
5353 libc:: malloc ( size)
5454}
5555
56+ /// # Safety
57+ ///
58+ /// This function is unsafe.
5659#[ no_mangle]
5760pub unsafe extern "C" fn roc_realloc (
5861 c_ptr : * mut c_void ,
@@ -63,11 +66,17 @@ pub unsafe extern "C" fn roc_realloc(
6366 libc:: realloc ( c_ptr, new_size)
6467}
6568
69+ /// # Safety
70+ ///
71+ /// This function is unsafe.
6672#[ no_mangle]
6773pub unsafe extern "C" fn roc_dealloc ( c_ptr : * mut c_void , _alignment : u32 ) {
6874 libc:: free ( c_ptr)
6975}
7076
77+ /// # Safety
78+ ///
79+ /// This function is unsafe.
7180#[ no_mangle]
7281pub unsafe extern "C" fn roc_panic ( msg : & RocStr , tag_id : u32 ) {
7382 _ = crossterm:: terminal:: disable_raw_mode ( ) ;
@@ -88,17 +97,26 @@ pub unsafe extern "C" fn roc_panic(msg: &RocStr, tag_id: u32) {
8897 }
8998}
9099
100+ /// # Safety
101+ ///
102+ /// This function is unsafe.
91103#[ no_mangle]
92104pub unsafe extern "C" fn roc_dbg ( loc : & RocStr , msg : & RocStr , src : & RocStr ) {
93105 eprintln ! ( "[{}] {} = {}" , loc, src, msg) ;
94106}
95107
108+ /// # Safety
109+ ///
110+ /// This function is unsafe.
96111#[ cfg( unix) ]
97112#[ no_mangle]
98113pub unsafe extern "C" fn roc_getppid ( ) -> libc:: pid_t {
99114 libc:: getppid ( )
100115}
101116
117+ /// # Safety
118+ ///
119+ /// This function should be called with a valid addr pointer.
102120#[ cfg( unix) ]
103121#[ no_mangle]
104122pub unsafe extern "C" fn roc_mmap (
@@ -112,6 +130,9 @@ pub unsafe extern "C" fn roc_mmap(
112130 libc:: mmap ( addr, len, prot, flags, fd, offset)
113131}
114132
133+ /// # Safety
134+ ///
135+ /// This function should be called with a valid name pointer.
115136#[ cfg( unix) ]
116137#[ no_mangle]
117138pub unsafe extern "C" fn roc_shm_open (
@@ -141,9 +162,10 @@ fn print_backtrace() {
141162 let fn_name = fn_name. to_string ( ) ;
142163
143164 if should_show_in_backtrace ( & fn_name) {
144- let mut entry: Entry = Default :: default ( ) ;
145-
146- entry. fn_name = format_fn_name ( & fn_name) ;
165+ let mut entry = Entry {
166+ fn_name : format_fn_name ( & fn_name) ,
167+ ..Default :: default ( )
168+ } ;
147169
148170 if let Some ( path) = symbol. filename ( ) {
149171 entry. filename = Some ( path. to_string_lossy ( ) . into_owned ( ) ) ;
@@ -190,7 +212,7 @@ fn should_show_in_backtrace(fn_name: &str) -> bool {
190212fn format_fn_name ( fn_name : & str ) -> String {
191213 // e.g. convert "_Num_sub_a0c29024d3ec6e3a16e414af99885fbb44fa6182331a70ab4ca0886f93bad5"
192214 // to ["Num", "sub", "a0c29024d3ec6e3a16e414af99885fbb44fa6182331a70ab4ca0886f93bad5"]
193- let mut pieces_iter = fn_name. split ( "_" ) ;
215+ let mut pieces_iter = fn_name. split ( '_' ) ;
194216
195217 if let ( _, Some ( module_name) , Some ( name) ) =
196218 ( pieces_iter. next ( ) , pieces_iter. next ( ) , pieces_iter. next ( ) )
@@ -217,6 +239,9 @@ fn display_roc_fn(module_name: &str, fn_name: &str) -> String {
217239 format ! ( "\u{001B} [36m{module_name}\u{001B} [39m.{fn_name}" )
218240}
219241
242+ /// # Safety
243+ ///
244+ /// This function should be provided a valid dst pointer.
220245#[ no_mangle]
221246pub unsafe extern "C" fn roc_memset ( dst : * mut c_void , c : i32 , n : usize ) -> * mut c_void {
222247 libc:: memset ( dst, c, n)
@@ -294,10 +319,13 @@ pub extern "C" fn rust_main() -> i32 {
294319
295320 std:: alloc:: dealloc ( buffer, layout) ;
296321
297- return out;
322+ out
298323 }
299324}
300325
326+ /// # Safety
327+ ///
328+ /// This function should be passed a pointer to a closure data buffer.
301329pub unsafe fn call_the_closure ( closure_data_ptr : * const u8 ) -> i32 {
302330 // Main always returns an i32. just allocate for that.
303331 let mut out: RocResult < ( ) , i32 > = RocResult :: ok ( ( ) ) ;
@@ -364,13 +392,13 @@ pub extern "C" fn roc_fx_exePath(_roc_str: &RocStr) -> RocResult<RocList<u8>, ()
364392/// See docs in `platform/Stdin.roc` for descriptions
365393fn handleStdinErr ( io_err : std:: io:: Error ) -> RocStr {
366394 match io_err. kind ( ) {
367- ErrorKind :: BrokenPipe => RocStr :: from ( "ErrorKind::BrokenPipe" ) ,
368- ErrorKind :: UnexpectedEof => RocStr :: from ( "ErrorKind::UnexpectedEof" ) ,
369- ErrorKind :: InvalidInput => RocStr :: from ( "ErrorKind::InvalidInput" ) ,
370- ErrorKind :: OutOfMemory => RocStr :: from ( "ErrorKind::OutOfMemory" ) ,
371- ErrorKind :: Interrupted => RocStr :: from ( "ErrorKind::Interrupted" ) ,
372- ErrorKind :: Unsupported => RocStr :: from ( "ErrorKind::Unsupported" ) ,
373- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
395+ ErrorKind :: BrokenPipe => "ErrorKind::BrokenPipe" . into ( ) ,
396+ ErrorKind :: UnexpectedEof => "ErrorKind::UnexpectedEof" . into ( ) ,
397+ ErrorKind :: InvalidInput => "ErrorKind::InvalidInput" . into ( ) ,
398+ ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
399+ ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
400+ ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
401+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
374402 }
375403}
376404
@@ -392,20 +420,20 @@ pub extern "C" fn roc_fx_stdinBytes() -> RocList<u8> {
392420
393421 match stdin. lock ( ) . read ( & mut buffer) {
394422 Ok ( bytes_read) => RocList :: from ( & buffer[ 0 ..bytes_read] ) ,
395- Err ( _) => RocList :: from ( ( & [ ] ) . as_slice ( ) ) ,
423+ Err ( _) => RocList :: from ( ( [ ] ) . as_slice ( ) ) ,
396424 }
397425}
398426
399427/// See docs in `platform/Stdout.roc` for descriptions
400428fn handleStdoutErr ( io_err : std:: io:: Error ) -> RocStr {
401429 match io_err. kind ( ) {
402- ErrorKind :: BrokenPipe => RocStr :: from ( "ErrorKind::BrokenPipe" ) ,
403- ErrorKind :: WouldBlock => RocStr :: from ( "ErrorKind::WouldBlock" ) ,
404- ErrorKind :: WriteZero => RocStr :: from ( "ErrorKind::WriteZero" ) ,
405- ErrorKind :: Unsupported => RocStr :: from ( "ErrorKind::Unsupported" ) ,
406- ErrorKind :: Interrupted => RocStr :: from ( "ErrorKind::Interrupted" ) ,
407- ErrorKind :: OutOfMemory => RocStr :: from ( "ErrorKind::OutOfMemory" ) ,
408- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
430+ ErrorKind :: BrokenPipe => "ErrorKind::BrokenPipe" . into ( ) ,
431+ ErrorKind :: WouldBlock => "ErrorKind::WouldBlock" . into ( ) ,
432+ ErrorKind :: WriteZero => "ErrorKind::WriteZero" . into ( ) ,
433+ ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
434+ ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
435+ ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
436+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
409437 }
410438}
411439
@@ -439,13 +467,13 @@ pub extern "C" fn roc_fx_stdoutWrite(text: &RocStr) -> RocResult<(), RocStr> {
439467/// See docs in `platform/Stdout.roc` for descriptions
440468fn handleStderrErr ( io_err : std:: io:: Error ) -> RocStr {
441469 match io_err. kind ( ) {
442- ErrorKind :: BrokenPipe => RocStr :: from ( "ErrorKind::BrokenPipe" ) ,
443- ErrorKind :: WouldBlock => RocStr :: from ( "ErrorKind::WouldBlock" ) ,
444- ErrorKind :: WriteZero => RocStr :: from ( "ErrorKind::WriteZero" ) ,
445- ErrorKind :: Unsupported => RocStr :: from ( "ErrorKind::Unsupported" ) ,
446- ErrorKind :: Interrupted => RocStr :: from ( "ErrorKind::Interrupted" ) ,
447- ErrorKind :: OutOfMemory => RocStr :: from ( "ErrorKind::OutOfMemory" ) ,
448- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
470+ ErrorKind :: BrokenPipe => "ErrorKind::BrokenPipe" . into ( ) ,
471+ ErrorKind :: WouldBlock => "ErrorKind::WouldBlock" . into ( ) ,
472+ ErrorKind :: WriteZero => "ErrorKind::WriteZero" . into ( ) ,
473+ ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
474+ ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
475+ ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
476+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
449477 }
450478}
451479
@@ -667,20 +695,15 @@ pub extern "C" fn roc_fx_dirList(
667695
668696 let mut entries = Vec :: new ( ) ;
669697
670- for entry in dir {
671- match entry {
672- Ok ( entry) => {
673- let path = entry. path ( ) ;
674- let str = path. as_os_str ( ) ;
675- entries. push ( os_str_to_roc_path ( str) ) ;
676- }
677- Err ( _) => { } // TODO should we ignore errors reading directory??
678- }
698+ for entry in dir. flatten ( ) {
699+ let path = entry. path ( ) ;
700+ let str = path. as_os_str ( ) ;
701+ entries. push ( os_str_to_roc_path ( str) ) ;
679702 }
680703
681- return roc_std:: RocResult :: ok ( RocList :: from_iter ( entries) ) ;
704+ roc_std:: RocResult :: ok ( RocList :: from_iter ( entries) )
682705 } else {
683- return roc_std:: RocResult :: err ( "ErrorKind::NotADirectory" . into ( ) ) ;
706+ roc_std:: RocResult :: err ( "ErrorKind::NotADirectory" . into ( ) )
684707 }
685708}
686709
@@ -741,7 +764,7 @@ pub extern "C" fn roc_fx_sendRequest(roc_request: &roc_app::Request) -> roc_app:
741764 let bytes = String :: from_utf8 ( roc_request. body . as_slice ( ) . to_vec ( ) ) . unwrap ( ) ;
742765 let mime_type_str = roc_request. mimeType . as_str ( ) ;
743766
744- if !has_content_type_header && mime_type_str. len ( ) > 0 {
767+ if !has_content_type_header && ! mime_type_str. is_empty ( ) {
745768 req_builder = req_builder. header ( "Content-Type" , mime_type_str) ;
746769 }
747770
@@ -895,15 +918,21 @@ pub extern "C" fn roc_fx_tcpConnect(host: &RocStr, port: u16) -> roc_app::Connec
895918 }
896919}
897920
921+ /// # Safety
922+ ///
923+ /// This function should provided a valid pointer to a `BufReader<TcpStream>`.
898924#[ no_mangle]
899- pub extern "C" fn roc_fx_tcpClose ( stream_ptr : * mut BufReader < TcpStream > ) {
925+ pub unsafe extern "C" fn roc_fx_tcpClose ( stream_ptr : * mut BufReader < TcpStream > ) {
900926 unsafe {
901927 drop ( Box :: from_raw ( stream_ptr) ) ;
902928 }
903929}
904930
931+ /// # Safety
932+ ///
933+ /// This function should provided a valid pointer to a `BufReader<TcpStream>`.
905934#[ no_mangle]
906- pub extern "C" fn roc_fx_tcpReadUpTo (
935+ pub unsafe extern "C" fn roc_fx_tcpReadUpTo (
907936 bytes_to_read : u64 ,
908937 stream_ptr : * mut BufReader < TcpStream > ,
909938) -> roc_app:: ReadResult {
@@ -924,14 +953,17 @@ pub extern "C" fn roc_fx_tcpReadUpTo(
924953 }
925954}
926955
956+ /// # Safety
957+ ///
958+ /// This function should provided a valid pointer to a `BufReader<TcpStream>`.
927959#[ no_mangle]
928- pub extern "C" fn roc_fx_tcpReadExactly (
960+ pub unsafe extern "C" fn roc_fx_tcpReadExactly (
929961 bytes_to_read : u64 ,
930962 stream_ptr : * mut BufReader < TcpStream > ,
931963) -> roc_app:: ReadExactlyResult {
932964 let reader = unsafe { & mut * stream_ptr } ;
933965 let mut buffer = Vec :: with_capacity ( bytes_to_read as usize ) ;
934- let mut chunk = reader. take ( bytes_to_read as u64 ) ;
966+ let mut chunk = reader. take ( bytes_to_read) ;
935967
936968 match chunk. read_to_end ( & mut buffer) {
937969 Ok ( read) => {
@@ -947,8 +979,11 @@ pub extern "C" fn roc_fx_tcpReadExactly(
947979 }
948980}
949981
982+ /// # Safety
983+ ///
984+ /// This function should provided a valid pointer to a `BufReader<TcpStream>`.
950985#[ no_mangle]
951- pub extern "C" fn roc_fx_tcpReadUntil (
986+ pub unsafe extern "C" fn roc_fx_tcpReadUntil (
952987 byte : u8 ,
953988 stream_ptr : * mut BufReader < TcpStream > ,
954989) -> roc_app:: ReadResult {
@@ -966,8 +1001,11 @@ pub extern "C" fn roc_fx_tcpReadUntil(
9661001 }
9671002}
9681003
1004+ /// # Safety
1005+ ///
1006+ /// This function should provided a valid pointer to a `BufReader<TcpStream>`.
9691007#[ no_mangle]
970- pub extern "C" fn roc_fx_tcpWrite (
1008+ pub unsafe extern "C" fn roc_fx_tcpWrite (
9711009 msg : & RocList < u8 > ,
9721010 stream_ptr : * mut BufReader < TcpStream > ,
9731011) -> roc_app:: WriteResult {
@@ -1123,7 +1161,7 @@ pub extern "C" fn roc_fx_commandOutput(roc_cmd: &roc_app::Command) -> roc_app::O
11231161 } ;
11241162
11251163 roc_app:: Output {
1126- status : status ,
1164+ status,
11271165 stdout : RocList :: from ( & output. stdout [ ..] ) ,
11281166 stderr : RocList :: from ( & output. stderr [ ..] ) ,
11291167 }
@@ -1186,7 +1224,7 @@ fn handleDirError(io_err: std::io::Error) -> RocStr {
11861224 // ErrorKind::FilesystemQuotaExceeded => RocStr::from("ErrorKind::FilesystemQuotaExceeded"),
11871225 // ErrorKind::StorageFull => RocStr::from("ErrorKind::StorageFull"),
11881226 // ErrorKind::InvalidFilename => RocStr::from("ErrorKind::InvalidFilename"),
1189- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
1227+ _ => RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ,
11901228 }
11911229}
11921230
0 commit comments