|
| 1 | +// Args: |
| 2 | +// mapdir: hamlet:wasitests/test_fs/hamlet |
| 3 | + |
| 4 | +use std::fs; |
| 5 | +use std::io::{Read, Seek, SeekFrom}; |
| 6 | +#[cfg(target_os = "wasi")] |
| 7 | +use std::os::wasi::prelude::AsRawFd; |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +#[cfg(target_os = "wasi")] |
| 11 | +#[link(wasm_import_module = "wasi_unstable")] |
| 12 | +extern "C" { |
| 13 | + fn poll_oneoff(subscriptons: u32, events: u32, nsubscriptons: u32, nevents: u32) -> u16; |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Copy, Clone)] |
| 17 | +#[repr(C)] |
| 18 | +pub struct __wasi_event_t { |
| 19 | + pub userdata: u64, |
| 20 | + pub error: u16, |
| 21 | + pub type_: __wasi_eventtype_t, |
| 22 | + pub u: __wasi_event_u, |
| 23 | +} |
| 24 | + |
| 25 | +impl Default for __wasi_event_t { |
| 26 | + fn default() -> Self { |
| 27 | + __wasi_event_t { |
| 28 | + userdata: 0, |
| 29 | + error: 0, |
| 30 | + type_: 0, |
| 31 | + u: __wasi_event_u { |
| 32 | + fd_readwrite: __wasi_event_fd_readwrite_t { |
| 33 | + nbytes: 0, |
| 34 | + flags: 0, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 42 | +#[repr(C)] |
| 43 | +pub struct __wasi_event_fd_readwrite_t { |
| 44 | + pub nbytes: u64, |
| 45 | + pub flags: u16, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Copy, Clone)] |
| 49 | +#[repr(C)] |
| 50 | +pub union __wasi_event_u { |
| 51 | + pub fd_readwrite: __wasi_event_fd_readwrite_t, |
| 52 | +} |
| 53 | + |
| 54 | +impl std::fmt::Debug for __wasi_event_u { |
| 55 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 56 | + write!(f, "__wasi_event_u {{ ")?; |
| 57 | + write!(f, "{:?} }}", unsafe { self.fd_readwrite }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +pub type __wasi_eventtype_t = u8; |
| 62 | +pub const __WASI_EVENTTYPE_CLOCK: u8 = 0; |
| 63 | +pub const __WASI_EVENTTYPE_FD_READ: u8 = 1; |
| 64 | +pub const __WASI_EVENTTYPE_FD_WRITE: u8 = 2; |
| 65 | + |
| 66 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 67 | +#[repr(C)] |
| 68 | +pub struct __wasi_subscription_clock_t { |
| 69 | + pub userdata: u64, |
| 70 | + pub clock_id: u32, |
| 71 | + pub timeout: u64, |
| 72 | + pub precision: u64, |
| 73 | + pub flags: u16, |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 77 | +#[repr(C)] |
| 78 | +pub struct __wasi_subscription_fs_readwrite_t { |
| 79 | + pub fd: u32, |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Copy, Clone)] |
| 83 | +#[repr(C)] |
| 84 | +pub union __wasi_subscription_u { |
| 85 | + clock: __wasi_subscription_clock_t, |
| 86 | + fd_readwrite: __wasi_subscription_fs_readwrite_t, |
| 87 | +} |
| 88 | + |
| 89 | +#[derive(Copy, Clone)] |
| 90 | +#[repr(C)] |
| 91 | +pub struct __wasi_subscription_t { |
| 92 | + pub userdata: u64, |
| 93 | + pub type_: __wasi_eventtype_t, |
| 94 | + pub u: __wasi_subscription_u, |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(target_os = "wasi")] |
| 98 | +fn poll_read(fds: &[u32]) -> Result<Vec<__wasi_event_t>, u16> { |
| 99 | + let mut in_ = fds |
| 100 | + .iter() |
| 101 | + .map(|n| __wasi_subscription_t { |
| 102 | + userdata: 0x123456, |
| 103 | + type_: __WASI_EVENTTYPE_FD_READ, |
| 104 | + u: __wasi_subscription_u { |
| 105 | + fd_readwrite: __wasi_subscription_fs_readwrite_t { fd: *n as u32 }, |
| 106 | + }, |
| 107 | + }) |
| 108 | + .collect::<Vec<_>>(); |
| 109 | + let mut out_ = vec![Default::default(); in_.len()]; |
| 110 | + let mut nsubscriptions: u32 = 0; |
| 111 | + let result = unsafe { |
| 112 | + poll_oneoff( |
| 113 | + in_.as_ptr() as usize as u32, |
| 114 | + out_.as_mut_ptr() as usize as u32, |
| 115 | + in_.len() as u32, |
| 116 | + (&mut nsubscriptions as *mut u32) as usize as u32, |
| 117 | + ) |
| 118 | + }; |
| 119 | + |
| 120 | + if result == 0 { |
| 121 | + Ok(out_) |
| 122 | + } else { |
| 123 | + Err(result) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +fn main() { |
| 128 | + #[cfg(not(target_os = "wasi"))] |
| 129 | + let mut base = PathBuf::from("wasitests/test_fs"); |
| 130 | + #[cfg(target_os = "wasi")] |
| 131 | + let mut base = PathBuf::from("/"); |
| 132 | + |
| 133 | + let path = base.join("hamlet/act4/scene1.txt"); |
| 134 | + let mut file = fs::File::open(&path).expect("Could not open file"); |
| 135 | + let mut buffer = [0u8; 64]; |
| 136 | + |
| 137 | + #[cfg(target_os = "wasi")] |
| 138 | + { |
| 139 | + let fds = vec![file.as_raw_fd()]; |
| 140 | + let mut result = poll_read(fds.as_slice()).expect("First poll"); |
| 141 | + while result[0].error != 0 { |
| 142 | + result = poll_read(fds.as_slice()).expect("subsequent polls"); |
| 143 | + } |
| 144 | + println!("{:?}", result[0]); |
| 145 | + } |
| 146 | + #[cfg(not(target_os = "wasi"))] |
| 147 | + { |
| 148 | + println!("{}", "__wasi_event_t { userdata: 1193046, error: 0, type_: 1, u: __wasi_event_u { __wasi_event_fd_readwrite_t { nbytes: 2259, flags: 0 } } }"); |
| 149 | + } |
| 150 | +} |
0 commit comments