11#![ allow( non_snake_case) ]
2-
2+ #! [ allow ( improper_ctypes ) ]
33use core:: alloc:: Layout ;
44use core:: ffi:: c_void;
55use core:: mem:: MaybeUninit ;
@@ -54,16 +54,19 @@ extern "C" {
5454 #[ allow( dead_code) ]
5555 #[ link_name = "roc__mainForHost_0_size" ]
5656 fn size_Fx ( ) -> i64 ;
57-
58- #[ link_name = "roc__mainForHost_0_result_size" ]
59- fn size_Fx_result ( ) -> i64 ;
6057}
6158
59+ /// # Safety
60+ ///
61+ /// This function is unsafe.
6262#[ no_mangle]
6363pub unsafe extern "C" fn roc_alloc ( size : usize , _alignment : u32 ) -> * mut c_void {
6464 libc:: malloc ( size)
6565}
6666
67+ /// # Safety
68+ ///
69+ /// This function is unsafe.
6770#[ no_mangle]
6871pub unsafe extern "C" fn roc_realloc (
6972 c_ptr : * mut c_void ,
@@ -74,11 +77,17 @@ pub unsafe extern "C" fn roc_realloc(
7477 libc:: realloc ( c_ptr, new_size)
7578}
7679
80+ /// # Safety
81+ ///
82+ /// This function is unsafe.
7783#[ no_mangle]
7884pub unsafe extern "C" fn roc_dealloc ( c_ptr : * mut c_void , _alignment : u32 ) {
7985 libc:: free ( c_ptr)
8086}
8187
88+ /// # Safety
89+ ///
90+ /// This function is unsafe.
8291#[ no_mangle]
8392pub unsafe extern "C" fn roc_panic ( msg : & RocStr , tag_id : u32 ) {
8493 _ = crossterm:: terminal:: disable_raw_mode ( ) ;
@@ -99,17 +108,26 @@ pub unsafe extern "C" fn roc_panic(msg: &RocStr, tag_id: u32) {
99108 }
100109}
101110
111+ /// # Safety
112+ ///
113+ /// This function is unsafe.
102114#[ no_mangle]
103115pub unsafe extern "C" fn roc_dbg ( loc : & RocStr , msg : & RocStr , src : & RocStr ) {
104116 eprintln ! ( "[{}] {} = {}" , loc, src, msg) ;
105117}
106118
119+ /// # Safety
120+ ///
121+ /// This function is unsafe.
107122#[ cfg( unix) ]
108123#[ no_mangle]
109124pub unsafe extern "C" fn roc_getppid ( ) -> libc:: pid_t {
110125 libc:: getppid ( )
111126}
112127
128+ /// # Safety
129+ ///
130+ /// This function should be called with a valid addr pointer.
113131#[ cfg( unix) ]
114132#[ no_mangle]
115133pub unsafe extern "C" fn roc_mmap (
@@ -123,6 +141,9 @@ pub unsafe extern "C" fn roc_mmap(
123141 libc:: mmap ( addr, len, prot, flags, fd, offset)
124142}
125143
144+ /// # Safety
145+ ///
146+ /// This function should be called with a valid name pointer.
126147#[ cfg( unix) ]
127148#[ no_mangle]
128149pub unsafe extern "C" fn roc_shm_open (
@@ -152,9 +173,10 @@ fn print_backtrace() {
152173 let fn_name = fn_name. to_string ( ) ;
153174
154175 if should_show_in_backtrace ( & fn_name) {
155- let mut entry: Entry = Default :: default ( ) ;
156-
157- entry. fn_name = format_fn_name ( & fn_name) ;
176+ let mut entry = Entry {
177+ fn_name : format_fn_name ( & fn_name) ,
178+ ..Default :: default ( )
179+ } ;
158180
159181 if let Some ( path) = symbol. filename ( ) {
160182 entry. filename = Some ( path. to_string_lossy ( ) . into_owned ( ) ) ;
@@ -201,7 +223,7 @@ fn should_show_in_backtrace(fn_name: &str) -> bool {
201223fn format_fn_name ( fn_name : & str ) -> String {
202224 // e.g. convert "_Num_sub_a0c29024d3ec6e3a16e414af99885fbb44fa6182331a70ab4ca0886f93bad5"
203225 // to ["Num", "sub", "a0c29024d3ec6e3a16e414af99885fbb44fa6182331a70ab4ca0886f93bad5"]
204- let mut pieces_iter = fn_name. split ( "_" ) ;
226+ let mut pieces_iter = fn_name. split ( '_' ) ;
205227
206228 if let ( _, Some ( module_name) , Some ( name) ) =
207229 ( pieces_iter. next ( ) , pieces_iter. next ( ) , pieces_iter. next ( ) )
@@ -228,6 +250,9 @@ fn display_roc_fn(module_name: &str, fn_name: &str) -> String {
228250 format ! ( "\u{001B} [36m{module_name}\u{001B} [39m.{fn_name}" )
229251}
230252
253+ /// # Safety
254+ ///
255+ /// This function should be provided a valid dst pointer.
231256#[ no_mangle]
232257pub unsafe extern "C" fn roc_memset ( dst : * mut c_void , c : i32 , n : usize ) -> * mut c_void {
233258 libc:: memset ( dst, c, n)
@@ -259,7 +284,11 @@ pub fn init() {
259284 roc_fx_ttyModeRaw as _ ,
260285 roc_fx_fileWriteUtf8 as _ ,
261286 roc_fx_fileWriteBytes as _ ,
287+ roc_fx_pathType as _ ,
262288 roc_fx_fileReadBytes as _ ,
289+ roc_fx_fileReader as _ ,
290+ roc_fx_fileReadLine as _ ,
291+ roc_fx_closeFile as _ ,
263292 roc_fx_fileDelete as _ ,
264293 roc_fx_cwd as _ ,
265294 roc_fx_posixTime as _ ,
@@ -278,7 +307,7 @@ pub fn init() {
278307 roc_fx_dirCreateAll as _ ,
279308 roc_fx_dirDeleteEmpty as _ ,
280309 roc_fx_dirDeleteAll as _ ,
281- roc_fx_pathType as _ ,
310+ roc_fx_currentArchOS as _ ,
282311 ] ;
283312 #[ allow( forgetting_references) ]
284313 std:: mem:: forget ( std:: hint:: black_box ( funcs) ) ;
@@ -305,10 +334,13 @@ pub extern "C" fn rust_main() -> i32 {
305334
306335 std:: alloc:: dealloc ( buffer, layout) ;
307336
308- return out;
337+ out
309338 }
310339}
311340
341+ /// # Safety
342+ ///
343+ /// This function should be passed a pointer to a closure data buffer.
312344pub unsafe fn call_the_closure ( closure_data_ptr : * const u8 ) -> i32 {
313345 // Main always returns an i32. just allocate for that.
314346 let mut out: RocResult < ( ) , i32 > = RocResult :: ok ( ( ) ) ;
@@ -381,7 +413,7 @@ fn handleStdinErr(io_err: std::io::Error) -> RocStr {
381413 ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
382414 ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
383415 ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
384- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
416+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
385417 }
386418}
387419
@@ -403,20 +435,20 @@ pub extern "C" fn roc_fx_stdinBytes() -> RocList<u8> {
403435
404436 match stdin. lock ( ) . read ( & mut buffer) {
405437 Ok ( bytes_read) => RocList :: from ( & buffer[ 0 ..bytes_read] ) ,
406- Err ( _) => RocList :: from ( ( & [ ] ) . as_slice ( ) ) ,
438+ Err ( _) => RocList :: from ( ( [ ] ) . as_slice ( ) ) ,
407439 }
408440}
409441
410442/// See docs in `platform/Stdout.roc` for descriptions
411443fn handleStdoutErr ( io_err : std:: io:: Error ) -> RocStr {
412444 match io_err. kind ( ) {
413- ErrorKind :: BrokenPipe => RocStr :: from ( "ErrorKind::BrokenPipe" ) ,
414- ErrorKind :: WouldBlock => RocStr :: from ( "ErrorKind::WouldBlock" ) ,
415- ErrorKind :: WriteZero => RocStr :: from ( "ErrorKind::WriteZero" ) ,
416- ErrorKind :: Unsupported => RocStr :: from ( "ErrorKind::Unsupported" ) ,
417- ErrorKind :: Interrupted => RocStr :: from ( "ErrorKind::Interrupted" ) ,
418- ErrorKind :: OutOfMemory => RocStr :: from ( "ErrorKind::OutOfMemory" ) ,
419- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
445+ ErrorKind :: BrokenPipe => "ErrorKind::BrokenPipe" . into ( ) ,
446+ ErrorKind :: WouldBlock => "ErrorKind::WouldBlock" . into ( ) ,
447+ ErrorKind :: WriteZero => "ErrorKind::WriteZero" . into ( ) ,
448+ ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
449+ ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
450+ ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
451+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
420452 }
421453}
422454
@@ -450,13 +482,13 @@ pub extern "C" fn roc_fx_stdoutWrite(text: &RocStr) -> RocResult<(), RocStr> {
450482/// See docs in `platform/Stdout.roc` for descriptions
451483fn handleStderrErr ( io_err : std:: io:: Error ) -> RocStr {
452484 match io_err. kind ( ) {
453- ErrorKind :: BrokenPipe => RocStr :: from ( "ErrorKind::BrokenPipe" ) ,
454- ErrorKind :: WouldBlock => RocStr :: from ( "ErrorKind::WouldBlock" ) ,
455- ErrorKind :: WriteZero => RocStr :: from ( "ErrorKind::WriteZero" ) ,
456- ErrorKind :: Unsupported => RocStr :: from ( "ErrorKind::Unsupported" ) ,
457- ErrorKind :: Interrupted => RocStr :: from ( "ErrorKind::Interrupted" ) ,
458- ErrorKind :: OutOfMemory => RocStr :: from ( "ErrorKind::OutOfMemory" ) ,
459- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
485+ ErrorKind :: BrokenPipe => "ErrorKind::BrokenPipe" . into ( ) ,
486+ ErrorKind :: WouldBlock => "ErrorKind::WouldBlock" . into ( ) ,
487+ ErrorKind :: WriteZero => "ErrorKind::WriteZero" . into ( ) ,
488+ ErrorKind :: Unsupported => "ErrorKind::Unsupported" . into ( ) ,
489+ ErrorKind :: Interrupted => "ErrorKind::Interrupted" . into ( ) ,
490+ ErrorKind :: OutOfMemory => "ErrorKind::OutOfMemory" . into ( ) ,
491+ _ => format ! ( "{:?}" , io_err) . as_str ( ) . into ( ) ,
460492 }
461493}
462494
@@ -683,20 +715,15 @@ pub extern "C" fn roc_fx_dirList(
683715
684716 let mut entries = Vec :: new ( ) ;
685717
686- for entry in dir {
687- match entry {
688- Ok ( entry) => {
689- let path = entry. path ( ) ;
690- let str = path. as_os_str ( ) ;
691- entries. push ( os_str_to_roc_path ( str) ) ;
692- }
693- Err ( _) => { } // TODO should we ignore errors reading directory??
694- }
718+ for entry in dir. flatten ( ) {
719+ let path = entry. path ( ) ;
720+ let str = path. as_os_str ( ) ;
721+ entries. push ( os_str_to_roc_path ( str) ) ;
695722 }
696723
697- return RocResult :: ok ( RocList :: from_iter ( entries) ) ;
724+ RocResult :: ok ( RocList :: from_iter ( entries) )
698725 } else {
699- return RocResult :: err ( "ErrorKind::NotADirectory" . into ( ) ) ;
726+ RocResult :: err ( "ErrorKind::NotADirectory" . into ( ) )
700727 }
701728}
702729
@@ -717,7 +744,6 @@ fn os_str_to_roc_path(os_str: &OsStr) -> RocList<u8> {
717744}
718745
719746#[ repr( C ) ]
720- #[ derive( Debug ) ]
721747pub struct Request {
722748 body : RocList < u8 > ,
723749 headers : RocList < Header > ,
@@ -728,14 +754,12 @@ pub struct Request {
728754}
729755
730756#[ repr( C ) ]
731- #[ derive( Debug ) ]
732757pub struct Header {
733758 key : RocStr ,
734759 value : RocStr ,
735760}
736761
737762#[ repr( C ) ]
738- #[ derive( Debug ) ]
739763pub struct Metadata {
740764 headers : RocList < Header > ,
741765 url : RocStr ,
@@ -755,7 +779,6 @@ impl Metadata {
755779}
756780
757781#[ repr( C ) ]
758- #[ derive( Debug ) ]
759782pub struct InternalResponse {
760783 body : RocList < u8 > ,
761784 metadata : Metadata ,
@@ -822,7 +845,7 @@ pub extern "C" fn roc_fx_sendRequest(roc_request: &Request) -> InternalResponse
822845 let bytes = String :: from_utf8 ( roc_request. body . as_slice ( ) . to_vec ( ) ) . unwrap ( ) ;
823846 let mime_type_str = roc_request. mimeType . as_str ( ) ;
824847
825- if !has_content_type_header && mime_type_str. len ( ) > 0 {
848+ if !has_content_type_header && ! mime_type_str. is_empty ( ) {
826849 req_builder = req_builder. header ( "Content-Type" , mime_type_str) ;
827850 }
828851
@@ -1024,7 +1047,6 @@ pub extern "C" fn roc_fx_tcpReadExactly(
10241047 Err ( err) => RocResult :: err ( to_tcp_stream_err ( err) ) ,
10251048 }
10261049 } )
1027-
10281050}
10291051
10301052#[ no_mangle]
@@ -1090,7 +1112,6 @@ fn to_tcp_stream_err(err: std::io::Error) -> RocStr {
10901112}
10911113
10921114#[ repr( C ) ]
1093- #[ derive( Debug ) ]
10941115pub struct Command {
10951116 pub args : RocList < RocStr > ,
10961117 pub envs : RocList < RocStr > ,
@@ -1282,7 +1303,7 @@ fn handleDirError(io_err: std::io::Error) -> RocStr {
12821303 // ErrorKind::FilesystemQuotaExceeded => RocStr::from("ErrorKind::FilesystemQuotaExceeded"),
12831304 // ErrorKind::StorageFull => RocStr::from("ErrorKind::StorageFull"),
12841305 // ErrorKind::InvalidFilename => RocStr::from("ErrorKind::InvalidFilename"),
1285- _ => RocStr :: from ( RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ) ,
1306+ _ => RocStr :: from ( format ! ( "{:?}" , io_err) . as_str ( ) ) ,
12861307 }
12871308}
12881309
0 commit comments