]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasi/mod.rs
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
[rust.git] / src / libstd / sys / wasi / mod.rs
1 //! System bindings for the wasm/web platform
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for wasm. Note that this wasm is *not* the emscripten
5 //! wasm, so we have no runtime here.
6 //!
7 //! This is all super highly experimental and not actually intended for
8 //! wide/production use yet, it's still all in the experimental category. This
9 //! will likely change over time.
10 //!
11 //! Currently all functions here are basically stubs that immediately return
12 //! errors. The hope is that with a portability lint we can turn actually just
13 //! remove all this and just omit parts of the standard library if we're
14 //! compiling for wasm. That way it's a compile time error for something that's
15 //! guaranteed to be a runtime error!
16
17 use libc;
18 use crate::io::{Error, ErrorKind};
19 use crate::mem;
20 use crate::os::raw::c_char;
21
22 pub mod alloc;
23 pub mod args;
24 #[path = "../wasm/cmath.rs"]
25 pub mod cmath;
26 #[path = "../wasm/condvar.rs"]
27 pub mod condvar;
28 pub mod env;
29 pub mod fd;
30 pub mod fs;
31 #[path = "../wasm/memchr.rs"]
32 pub mod memchr;
33 #[path = "../wasm/mutex.rs"]
34 pub mod mutex;
35 pub mod net;
36 pub mod io;
37 pub mod os;
38 pub use crate::sys_common::os_str_bytes as os_str;
39 pub mod path;
40 pub mod pipe;
41 pub mod process;
42 #[path = "../wasm/rwlock.rs"]
43 pub mod rwlock;
44 #[path = "../wasm/stack_overflow.rs"]
45 pub mod stack_overflow;
46 pub mod stdio;
47 pub mod thread;
48 #[path = "../wasm/thread_local.rs"]
49 pub mod thread_local;
50 #[path = "../wasm/fast_thread_local.rs"]
51 pub mod fast_thread_local;
52 pub mod time;
53 pub mod ext;
54
55 #[cfg(not(test))]
56 pub fn init() {
57 }
58
59 pub fn unsupported<T>() -> crate::io::Result<T> {
60     Err(unsupported_err())
61 }
62
63 pub fn unsupported_err() -> Error {
64     Error::new(ErrorKind::Other, "operation not supported on wasm yet")
65 }
66
67 pub fn decode_error_kind(_code: i32) -> ErrorKind {
68     ErrorKind::Other
69 }
70
71 // This enum is used as the storage for a bunch of types which can't actually
72 // exist.
73 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
74 pub enum Void {}
75
76 pub unsafe fn strlen(mut s: *const c_char) -> usize {
77     let mut n = 0;
78     while *s != 0 {
79         n += 1;
80         s = s.offset(1);
81     }
82     return n
83 }
84
85 pub unsafe fn abort_internal() -> ! {
86     libc::abort()
87 }
88
89 pub fn hashmap_random_keys() -> (u64, u64) {
90     let mut ret = (0u64, 0u64);
91     unsafe {
92         let base = &mut ret as *mut (u64, u64) as *mut libc::c_void;
93         let len = mem::size_of_val(&ret);
94         cvt_wasi(libc::__wasi_random_get(base, len)).unwrap();
95     }
96     return ret
97 }
98
99 #[doc(hidden)]
100 pub trait IsMinusOne {
101     fn is_minus_one(&self) -> bool;
102 }
103
104 macro_rules! impl_is_minus_one {
105     ($($t:ident)*) => ($(impl IsMinusOne for $t {
106         fn is_minus_one(&self) -> bool {
107             *self == -1
108         }
109     })*)
110 }
111
112 impl_is_minus_one! { i8 i16 i32 i64 isize }
113
114 pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
115     if t.is_minus_one() {
116         Err(Error::last_os_error())
117     } else {
118         Ok(t)
119     }
120 }
121
122 pub fn cvt_wasi(r: u16) -> crate::io::Result<()> {
123     if r != libc::__WASI_ESUCCESS {
124         Err(Error::from_raw_os_error(r as i32))
125     } else {
126         Ok(())
127     }
128 }