]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/wasi/mod.rs
New upstream version 1.41.1+dfsg1
[rustc.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 crate::io as std_io;
18 use crate::mem;
19 use crate::os::raw::c_char;
20
21 pub mod alloc;
22 pub mod args;
23 #[path = "../wasm/cmath.rs"]
24 pub mod cmath;
25 #[path = "../wasm/condvar.rs"]
26 pub mod condvar;
27 pub mod env;
28 pub mod fd;
29 pub mod fs;
30 #[path = "../wasm/memchr.rs"]
31 pub mod memchr;
32 #[path = "../wasm/mutex.rs"]
33 pub mod mutex;
34 pub mod net;
35 pub mod io;
36 pub mod os;
37 pub use crate::sys_common::os_str_bytes as os_str;
38 pub mod path;
39 pub mod pipe;
40 pub mod process;
41 #[path = "../wasm/rwlock.rs"]
42 pub mod rwlock;
43 #[path = "../wasm/stack_overflow.rs"]
44 pub mod stack_overflow;
45 pub mod stdio;
46 pub mod thread;
47 #[path = "../wasm/thread_local.rs"]
48 pub mod thread_local;
49 #[path = "../wasm/fast_thread_local.rs"]
50 pub mod fast_thread_local;
51 pub mod time;
52 pub mod ext;
53
54 #[cfg(not(test))]
55 pub fn init() {
56 }
57
58 pub fn unsupported<T>() -> std_io::Result<T> {
59 Err(unsupported_err())
60 }
61
62 pub fn unsupported_err() -> std_io::Error {
63 std_io::Error::new(
64 std_io::ErrorKind::Other,
65 "operation not supported on wasm yet",
66 )
67 }
68
69 pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind {
70 use std_io::ErrorKind::*;
71 if errno > u16::max_value() as i32 || errno < 0 {
72 return Other;
73 }
74 match errno as u16 {
75 wasi::ERRNO_CONNREFUSED => ConnectionRefused,
76 wasi::ERRNO_CONNRESET => ConnectionReset,
77 wasi::ERRNO_PERM | wasi::ERRNO_ACCES => PermissionDenied,
78 wasi::ERRNO_PIPE => BrokenPipe,
79 wasi::ERRNO_NOTCONN => NotConnected,
80 wasi::ERRNO_CONNABORTED => ConnectionAborted,
81 wasi::ERRNO_ADDRNOTAVAIL => AddrNotAvailable,
82 wasi::ERRNO_ADDRINUSE => AddrInUse,
83 wasi::ERRNO_NOENT => NotFound,
84 wasi::ERRNO_INTR => Interrupted,
85 wasi::ERRNO_INVAL => InvalidInput,
86 wasi::ERRNO_TIMEDOUT => TimedOut,
87 wasi::ERRNO_EXIST => AlreadyExists,
88 wasi::ERRNO_AGAIN => WouldBlock,
89 _ => Other,
90 }
91 }
92
93 // This enum is used as the storage for a bunch of types which can't actually
94 // exist.
95 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
96 pub enum Void {}
97
98 pub unsafe fn strlen(mut s: *const c_char) -> usize {
99 let mut n = 0;
100 while *s != 0 {
101 n += 1;
102 s = s.offset(1);
103 }
104 return n
105 }
106
107 pub unsafe fn abort_internal() -> ! {
108 libc::abort()
109 }
110
111 pub fn hashmap_random_keys() -> (u64, u64) {
112 let mut ret = (0u64, 0u64);
113 unsafe {
114 let base = &mut ret as *mut (u64, u64) as *mut u8;
115 let len = mem::size_of_val(&ret);
116 wasi::random_get(base, len).expect("random_get failure");
117 }
118 return ret
119 }
120
121 fn err2io(err: wasi::Error) -> std_io::Error {
122 std_io::Error::from_raw_os_error(err.raw_error().into())
123 }