]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/mod.rs
Reuse `unix::cmath`
[rust.git] / library / std / src / sys / hermit / mod.rs
1 //! System bindings for HermitCore
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for HermitCore.
5 //!
6 //! This is all super highly experimental and not actually intended for
7 //! wide/production use yet, it's still all in the experimental category. This
8 //! will likely change over time.
9 //!
10 //! Currently all functions here are basically stubs that immediately return
11 //! errors. The hope is that with a portability lint we can turn actually just
12 //! remove all this and just omit parts of the standard library if we're
13 //! compiling for wasm. That way it's a compile time error for something that's
14 //! guaranteed to be a runtime error!
15
16 #![allow(unsafe_op_in_unsafe_fn)]
17
18 use crate::intrinsics;
19 use crate::os::raw::c_char;
20
21 pub mod alloc;
22 pub mod args;
23 #[path = "../unix/cmath.rs"]
24 pub mod cmath;
25 pub mod condvar;
26 pub mod env;
27 pub mod ext;
28 pub mod fd;
29 pub mod fs;
30 pub mod io;
31 pub mod memchr;
32 pub mod mutex;
33 pub mod net;
34 pub mod os;
35 pub mod path;
36 #[path = "../unsupported/pipe.rs"]
37 pub mod pipe;
38 #[path = "../unsupported/process.rs"]
39 pub mod process;
40 pub mod rwlock;
41 pub mod stack_overflow;
42 pub mod stdio;
43 pub mod thread;
44 pub mod thread_local_dtor;
45 pub mod thread_local_key;
46 pub mod time;
47
48 use crate::io::ErrorKind;
49 pub use crate::sys_common::os_str_bytes as os_str;
50
51 #[allow(unused_extern_crates)]
52 pub extern crate hermit_abi as abi;
53
54 pub fn unsupported<T>() -> crate::io::Result<T> {
55     Err(unsupported_err())
56 }
57
58 pub fn unsupported_err() -> crate::io::Error {
59     crate::io::Error::new_const(
60         crate::io::ErrorKind::Unsupported,
61         &"operation not supported on HermitCore yet",
62     )
63 }
64
65 pub unsafe fn strlen(start: *const c_char) -> usize {
66     let mut str = start;
67
68     while *str != 0 {
69         str = str.offset(1);
70     }
71
72     (str as usize) - (start as usize)
73 }
74
75 #[no_mangle]
76 pub extern "C" fn floor(x: f64) -> f64 {
77     unsafe { intrinsics::floorf64(x) }
78 }
79
80 pub fn abort_internal() -> ! {
81     unsafe {
82         abi::abort();
83     }
84 }
85
86 // FIXME: just a workaround to test the system
87 pub fn hashmap_random_keys() -> (u64, u64) {
88     (1, 2)
89 }
90
91 // This function is needed by the panic runtime. The symbol is named in
92 // pre-link args for the target specification, so keep that in sync.
93 #[cfg(not(test))]
94 #[no_mangle]
95 // NB. used by both libunwind and libpanic_abort
96 pub extern "C" fn __rust_abort() {
97     abort_internal();
98 }
99
100 #[cfg(not(test))]
101 pub fn init() {
102     let _ = net::init();
103 }
104
105 #[cfg(not(test))]
106 #[no_mangle]
107 pub unsafe extern "C" fn runtime_entry(
108     argc: i32,
109     argv: *const *const c_char,
110     env: *const *const c_char,
111 ) -> ! {
112     use crate::sys::hermit::thread_local_dtor::run_dtors;
113     extern "C" {
114         fn main(argc: isize, argv: *const *const c_char) -> i32;
115     }
116
117     // initialize environment
118     os::init_environment(env as *const *const i8);
119
120     let result = main(argc as isize, argv);
121
122     run_dtors();
123     abi::exit(result);
124 }
125
126 pub fn decode_error_kind(errno: i32) -> ErrorKind {
127     match errno {
128         x if x == 13 as i32 => ErrorKind::PermissionDenied,
129         x if x == 98 as i32 => ErrorKind::AddrInUse,
130         x if x == 99 as i32 => ErrorKind::AddrNotAvailable,
131         x if x == 11 as i32 => ErrorKind::WouldBlock,
132         x if x == 103 as i32 => ErrorKind::ConnectionAborted,
133         x if x == 111 as i32 => ErrorKind::ConnectionRefused,
134         x if x == 104 as i32 => ErrorKind::ConnectionReset,
135         x if x == 17 as i32 => ErrorKind::AlreadyExists,
136         x if x == 4 as i32 => ErrorKind::Interrupted,
137         x if x == 22 as i32 => ErrorKind::InvalidInput,
138         x if x == 2 as i32 => ErrorKind::NotFound,
139         x if x == 107 as i32 => ErrorKind::NotConnected,
140         x if x == 1 as i32 => ErrorKind::PermissionDenied,
141         x if x == 32 as i32 => ErrorKind::BrokenPipe,
142         x if x == 110 as i32 => ErrorKind::TimedOut,
143         _ => ErrorKind::Other,
144     }
145 }
146
147 pub fn cvt(result: i32) -> crate::io::Result<usize> {
148     if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) }
149 }