spin_factor_wasi/
wasi_2023_11_10.rs

1use super::wasi_2023_10_18::{convert, convert_result};
2use spin_factors::anyhow::{self, Result};
3use wasmtime::component::{Linker, Resource};
4use wasmtime_wasi::WasiCtxView;
5
6mod latest {
7    pub use wasmtime_wasi::p2::bindings::*;
8}
9
10mod bindings {
11    use super::latest;
12
13    wasmtime::component::bindgen!({
14        path: "../../wit",
15        world: "wasi:cli/reactor@0.2.0-rc-2023-11-10",
16        imports: {
17            "wasi:io/streams/[drop]input-stream": async | trappable,
18            "wasi:io/streams/[drop]output-stream": async | trappable,
19            "wasi:filesystem/types/[method]descriptor.advise": async | trappable,
20            "wasi:filesystem/types/[method]descriptor.create-directory-at": async | trappable,
21            "wasi:filesystem/types/[method]descriptor.get-flags": async | trappable,
22            "wasi:filesystem/types/[method]descriptor.get-type": async | trappable,
23            "wasi:filesystem/types/[method]descriptor.is-same-object": async | trappable,
24            "wasi:filesystem/types/[method]descriptor.link-at": async | trappable,
25            "wasi:filesystem/types/[method]descriptor.metadata-hash": async | trappable,
26            "wasi:filesystem/types/[method]descriptor.metadata-hash-at": async | trappable,
27            "wasi:filesystem/types/[method]descriptor.open-at": async | trappable,
28            "wasi:filesystem/types/[method]descriptor.read": async | trappable,
29            "wasi:filesystem/types/[method]descriptor.read-directory": async | trappable,
30            "wasi:filesystem/types/[method]descriptor.readlink-at": async | trappable,
31            "wasi:filesystem/types/[method]descriptor.remove-directory-at": async | trappable,
32            "wasi:filesystem/types/[method]descriptor.rename-at": async | trappable,
33            "wasi:filesystem/types/[method]descriptor.set-size": async | trappable,
34            "wasi:filesystem/types/[method]descriptor.set-times": async | trappable,
35            "wasi:filesystem/types/[method]descriptor.set-times-at": async | trappable,
36            "wasi:filesystem/types/[method]descriptor.stat": async | trappable,
37            "wasi:filesystem/types/[method]descriptor.stat-at": async | trappable,
38            "wasi:filesystem/types/[method]descriptor.symlink-at": async | trappable,
39            "wasi:filesystem/types/[method]descriptor.sync": async | trappable,
40            "wasi:filesystem/types/[method]descriptor.sync-data": async | trappable,
41            "wasi:filesystem/types/[method]descriptor.unlink-file-at": async | trappable,
42            "wasi:filesystem/types/[method]descriptor.write": async | trappable,
43            "wasi:io/streams/[method]input-stream.blocking-read": async | trappable,
44            "wasi:io/streams/[method]input-stream.blocking-skip": async | trappable,
45            "wasi:io/streams/[method]output-stream.blocking-splice": async | trappable,
46            "wasi:io/streams/[method]output-stream.blocking-flush": async | trappable,
47            "wasi:io/streams/[method]output-stream.blocking-write-and-flush": async | trappable,
48            "wasi:io/streams/[method]output-stream.blocking-write-zeroes-and-flush": async | trappable,
49            "wasi:filesystem/types/[method]directory-entry-stream.read-directory-entry": async | trappable,
50            "wasi:io/poll/[method]pollable.block": async | trappable,
51            "wasi:io/poll/[method]pollable.ready": async | trappable,
52            "wasi:io/poll/poll": async | trappable,
53
54            "wasi:sockets/tcp/[method]tcp-socket.start-bind": async | trappable,
55            "wasi:sockets/tcp/[method]tcp-socket.start-connect": async | trappable,
56            "wasi:sockets/udp/[method]udp-socket.start-bind": async | trappable,
57            "wasi:sockets/udp/[method]udp-socket.stream": async | trappable,
58            "wasi:sockets/udp/[method]outgoing-datagram-stream.send": async | trappable,
59            default: trappable,
60        },
61        with: {
62            "wasi:io/poll/pollable": latest::io::poll::Pollable,
63            "wasi:io/streams/input-stream": latest::io::streams::InputStream,
64            "wasi:io/streams/output-stream": latest::io::streams::OutputStream,
65            "wasi:io/error/error": latest::io::error::Error,
66            "wasi:filesystem/types/directory-entry-stream": latest::filesystem::types::DirectoryEntryStream,
67            "wasi:filesystem/types/descriptor": latest::filesystem::types::Descriptor,
68            "wasi:cli/terminal-input/terminal-input": latest::cli::terminal_input::TerminalInput,
69            "wasi:cli/terminal-output/terminal-output": latest::cli::terminal_output::TerminalOutput,
70            "wasi:sockets/tcp/tcp-socket": latest::sockets::tcp::TcpSocket,
71            "wasi:sockets/udp/udp-socket": latest::sockets::udp::UdpSocket,
72            "wasi:sockets/udp/outgoing-datagram-stream": latest::sockets::udp::OutgoingDatagramStream,
73            "wasi:sockets/udp/incoming-datagram-stream": latest::sockets::udp::IncomingDatagramStream,
74            "wasi:sockets/network/network": latest::sockets::network::Network,
75            "wasi:sockets/ip-name-lookup/resolve-address-stream": latest::sockets::ip_name_lookup::ResolveAddressStream,
76        },
77    });
78}
79
80mod wasi {
81    pub use super::bindings::wasi::{
82        cli0_2_0_rc_2023_11_10 as cli, clocks0_2_0_rc_2023_11_10 as clocks,
83        filesystem0_2_0_rc_2023_11_10 as filesystem, io0_2_0_rc_2023_11_10 as io,
84        random0_2_0_rc_2023_11_10 as random, sockets0_2_0_rc_2023_11_10 as sockets,
85    };
86}
87
88use wasi::cli::terminal_input::TerminalInput;
89use wasi::cli::terminal_output::TerminalOutput;
90use wasi::clocks::monotonic_clock::{Duration, Instant};
91use wasi::clocks::wall_clock::Datetime;
92use wasi::filesystem::types::{
93    Advice, Descriptor, DescriptorFlags, DescriptorStat, DescriptorType, DirectoryEntry,
94    DirectoryEntryStream, ErrorCode as FsErrorCode, Filesize, MetadataHashValue, NewTimestamp,
95    OpenFlags, PathFlags,
96};
97use wasi::io::poll::Pollable;
98use wasi::io::streams::{Error as IoError, InputStream, OutputStream, StreamError};
99use wasi::sockets::ip_name_lookup::{IpAddress, ResolveAddressStream};
100use wasi::sockets::network::{Ipv4SocketAddress, Ipv6SocketAddress};
101use wasi::sockets::tcp::{
102    ErrorCode as SocketErrorCode, IpAddressFamily, IpSocketAddress, Network, ShutdownType,
103    TcpSocket,
104};
105use wasi::sockets::udp::{
106    IncomingDatagram, IncomingDatagramStream, OutgoingDatagram, OutgoingDatagramStream, UdpSocket,
107};
108
109use crate::HasWasi;
110
111pub fn add_to_linker<T>(
112    linker: &mut Linker<T>,
113    closure: fn(&mut T) -> WasiCtxView<'_>,
114) -> Result<()>
115where
116    T: Send + 'static,
117{
118    wasi::clocks::monotonic_clock::add_to_linker::<_, HasWasi>(linker, closure)?;
119    wasi::clocks::wall_clock::add_to_linker::<_, HasWasi>(linker, closure)?;
120    wasi::filesystem::types::add_to_linker::<_, HasWasi>(linker, closure)?;
121    wasi::filesystem::preopens::add_to_linker::<_, HasWasi>(linker, closure)?;
122    wasi::io::error::add_to_linker::<_, HasWasi>(linker, closure)?;
123    wasi::io::poll::add_to_linker::<_, HasWasi>(linker, closure)?;
124    wasi::io::streams::add_to_linker::<_, HasWasi>(linker, closure)?;
125    wasi::random::random::add_to_linker::<_, HasWasi>(linker, closure)?;
126    wasi::random::insecure::add_to_linker::<_, HasWasi>(linker, closure)?;
127    wasi::random::insecure_seed::add_to_linker::<_, HasWasi>(linker, closure)?;
128    wasi::cli::exit::add_to_linker::<_, HasWasi>(linker, closure)?;
129    wasi::cli::environment::add_to_linker::<_, HasWasi>(linker, closure)?;
130    wasi::cli::stdin::add_to_linker::<_, HasWasi>(linker, closure)?;
131    wasi::cli::stdout::add_to_linker::<_, HasWasi>(linker, closure)?;
132    wasi::cli::stderr::add_to_linker::<_, HasWasi>(linker, closure)?;
133    wasi::cli::terminal_input::add_to_linker::<_, HasWasi>(linker, closure)?;
134    wasi::cli::terminal_output::add_to_linker::<_, HasWasi>(linker, closure)?;
135    wasi::cli::terminal_stdin::add_to_linker::<_, HasWasi>(linker, closure)?;
136    wasi::cli::terminal_stdout::add_to_linker::<_, HasWasi>(linker, closure)?;
137    wasi::cli::terminal_stderr::add_to_linker::<_, HasWasi>(linker, closure)?;
138    wasi::sockets::tcp::add_to_linker::<_, HasWasi>(linker, closure)?;
139    wasi::sockets::tcp_create_socket::add_to_linker::<_, HasWasi>(linker, closure)?;
140    wasi::sockets::udp::add_to_linker::<_, HasWasi>(linker, closure)?;
141    wasi::sockets::udp_create_socket::add_to_linker::<_, HasWasi>(linker, closure)?;
142    wasi::sockets::instance_network::add_to_linker::<_, HasWasi>(linker, closure)?;
143    wasi::sockets::network::add_to_linker::<_, HasWasi>(linker, closure)?;
144    wasi::sockets::ip_name_lookup::add_to_linker::<_, HasWasi>(linker, closure)?;
145    Ok(())
146}
147
148impl wasi::clocks::monotonic_clock::Host for WasiCtxView<'_> {
149    fn now(&mut self) -> wasmtime::Result<Instant> {
150        latest::clocks::monotonic_clock::Host::now(self)
151    }
152
153    fn resolution(&mut self) -> wasmtime::Result<Instant> {
154        latest::clocks::monotonic_clock::Host::resolution(self)
155    }
156
157    fn subscribe_instant(&mut self, when: Instant) -> wasmtime::Result<Resource<Pollable>> {
158        latest::clocks::monotonic_clock::Host::subscribe_instant(self, when)
159    }
160
161    fn subscribe_duration(&mut self, when: Duration) -> wasmtime::Result<Resource<Pollable>> {
162        latest::clocks::monotonic_clock::Host::subscribe_duration(self, when)
163    }
164}
165
166impl wasi::clocks::wall_clock::Host for WasiCtxView<'_> {
167    fn now(&mut self) -> wasmtime::Result<Datetime> {
168        Ok(latest::clocks::wall_clock::Host::now(self)?.into())
169    }
170
171    fn resolution(&mut self) -> wasmtime::Result<Datetime> {
172        Ok(latest::clocks::wall_clock::Host::resolution(self)?.into())
173    }
174}
175
176impl wasi::filesystem::types::Host for WasiCtxView<'_> {
177    fn filesystem_error_code(
178        &mut self,
179        err: Resource<wasi::filesystem::types::Error>,
180    ) -> wasmtime::Result<Option<FsErrorCode>> {
181        Ok(latest::filesystem::types::Host::filesystem_error_code(self, err)?.map(|e| e.into()))
182    }
183}
184
185impl wasi::filesystem::types::HostDescriptor for WasiCtxView<'_> {
186    fn read_via_stream(
187        &mut self,
188        self_: Resource<Descriptor>,
189        offset: Filesize,
190    ) -> wasmtime::Result<Result<Resource<InputStream>, FsErrorCode>> {
191        convert_result(latest::filesystem::types::HostDescriptor::read_via_stream(
192            self, self_, offset,
193        ))
194    }
195
196    fn write_via_stream(
197        &mut self,
198        self_: Resource<Descriptor>,
199        offset: Filesize,
200    ) -> wasmtime::Result<Result<Resource<OutputStream>, FsErrorCode>> {
201        convert_result(latest::filesystem::types::HostDescriptor::write_via_stream(
202            self, self_, offset,
203        ))
204    }
205
206    fn append_via_stream(
207        &mut self,
208        self_: Resource<Descriptor>,
209    ) -> wasmtime::Result<Result<Resource<OutputStream>, FsErrorCode>> {
210        convert_result(latest::filesystem::types::HostDescriptor::append_via_stream(self, self_))
211    }
212
213    async fn advise(
214        &mut self,
215        self_: Resource<Descriptor>,
216        offset: Filesize,
217        length: Filesize,
218        advice: Advice,
219    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
220        convert_result(
221            latest::filesystem::types::HostDescriptor::advise(
222                self,
223                self_,
224                offset,
225                length,
226                advice.into(),
227            )
228            .await,
229        )
230    }
231
232    async fn sync_data(
233        &mut self,
234        self_: Resource<Descriptor>,
235    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
236        convert_result(latest::filesystem::types::HostDescriptor::sync_data(self, self_).await)
237    }
238
239    async fn get_flags(
240        &mut self,
241        self_: Resource<Descriptor>,
242    ) -> wasmtime::Result<Result<DescriptorFlags, FsErrorCode>> {
243        convert_result(latest::filesystem::types::HostDescriptor::get_flags(self, self_).await)
244    }
245
246    async fn get_type(
247        &mut self,
248        self_: Resource<Descriptor>,
249    ) -> wasmtime::Result<Result<DescriptorType, FsErrorCode>> {
250        convert_result(latest::filesystem::types::HostDescriptor::get_type(self, self_).await)
251    }
252
253    async fn set_size(
254        &mut self,
255        self_: Resource<Descriptor>,
256        size: Filesize,
257    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
258        convert_result(latest::filesystem::types::HostDescriptor::set_size(self, self_, size).await)
259    }
260
261    async fn set_times(
262        &mut self,
263        self_: Resource<Descriptor>,
264        data_access_timestamp: NewTimestamp,
265        data_modification_timestamp: NewTimestamp,
266    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
267        convert_result(
268            latest::filesystem::types::HostDescriptor::set_times(
269                self,
270                self_,
271                data_access_timestamp.into(),
272                data_modification_timestamp.into(),
273            )
274            .await,
275        )
276    }
277
278    async fn read(
279        &mut self,
280        self_: Resource<Descriptor>,
281        length: Filesize,
282        offset: Filesize,
283    ) -> wasmtime::Result<Result<(Vec<u8>, bool), FsErrorCode>> {
284        convert_result(
285            latest::filesystem::types::HostDescriptor::read(self, self_, length, offset).await,
286        )
287    }
288
289    async fn write(
290        &mut self,
291        self_: Resource<Descriptor>,
292        buffer: Vec<u8>,
293        offset: Filesize,
294    ) -> wasmtime::Result<Result<Filesize, FsErrorCode>> {
295        convert_result(
296            latest::filesystem::types::HostDescriptor::write(self, self_, buffer, offset).await,
297        )
298    }
299
300    async fn read_directory(
301        &mut self,
302        self_: Resource<Descriptor>,
303    ) -> wasmtime::Result<Result<Resource<DirectoryEntryStream>, FsErrorCode>> {
304        convert_result(latest::filesystem::types::HostDescriptor::read_directory(self, self_).await)
305    }
306
307    async fn sync(
308        &mut self,
309        self_: Resource<Descriptor>,
310    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
311        convert_result(latest::filesystem::types::HostDescriptor::sync(self, self_).await)
312    }
313
314    async fn create_directory_at(
315        &mut self,
316        self_: Resource<Descriptor>,
317        path: String,
318    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
319        convert_result(
320            latest::filesystem::types::HostDescriptor::create_directory_at(self, self_, path).await,
321        )
322    }
323
324    async fn stat(
325        &mut self,
326        self_: Resource<Descriptor>,
327    ) -> wasmtime::Result<Result<DescriptorStat, FsErrorCode>> {
328        convert_result(latest::filesystem::types::HostDescriptor::stat(self, self_).await)
329    }
330
331    async fn stat_at(
332        &mut self,
333        self_: Resource<Descriptor>,
334        path_flags: PathFlags,
335        path: String,
336    ) -> wasmtime::Result<Result<DescriptorStat, FsErrorCode>> {
337        convert_result(
338            latest::filesystem::types::HostDescriptor::stat_at(
339                self,
340                self_,
341                path_flags.into(),
342                path,
343            )
344            .await,
345        )
346    }
347
348    async fn set_times_at(
349        &mut self,
350        self_: Resource<Descriptor>,
351        path_flags: PathFlags,
352        path: String,
353        data_access_timestamp: NewTimestamp,
354        data_modification_timestamp: NewTimestamp,
355    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
356        convert_result(
357            latest::filesystem::types::HostDescriptor::set_times_at(
358                self,
359                self_,
360                path_flags.into(),
361                path,
362                data_access_timestamp.into(),
363                data_modification_timestamp.into(),
364            )
365            .await,
366        )
367    }
368
369    async fn link_at(
370        &mut self,
371        self_: Resource<Descriptor>,
372        old_path_flags: PathFlags,
373        old_path: String,
374        new_descriptor: Resource<Descriptor>,
375        new_path: String,
376    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
377        convert_result(
378            latest::filesystem::types::HostDescriptor::link_at(
379                self,
380                self_,
381                old_path_flags.into(),
382                old_path,
383                new_descriptor,
384                new_path,
385            )
386            .await,
387        )
388    }
389
390    async fn open_at(
391        &mut self,
392        self_: Resource<Descriptor>,
393        path_flags: PathFlags,
394        path: String,
395        open_flags: OpenFlags,
396        flags: DescriptorFlags,
397    ) -> wasmtime::Result<Result<Resource<Descriptor>, FsErrorCode>> {
398        convert_result(
399            latest::filesystem::types::HostDescriptor::open_at(
400                self,
401                self_,
402                path_flags.into(),
403                path,
404                open_flags.into(),
405                flags.into(),
406            )
407            .await,
408        )
409    }
410
411    async fn readlink_at(
412        &mut self,
413        self_: Resource<Descriptor>,
414        path: String,
415    ) -> wasmtime::Result<Result<String, FsErrorCode>> {
416        convert_result(
417            latest::filesystem::types::HostDescriptor::readlink_at(self, self_, path).await,
418        )
419    }
420
421    async fn remove_directory_at(
422        &mut self,
423        self_: Resource<Descriptor>,
424        path: String,
425    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
426        convert_result(
427            latest::filesystem::types::HostDescriptor::remove_directory_at(self, self_, path).await,
428        )
429    }
430
431    async fn rename_at(
432        &mut self,
433        self_: Resource<Descriptor>,
434        old_path: String,
435        new_descriptor: Resource<Descriptor>,
436        new_path: String,
437    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
438        convert_result(
439            latest::filesystem::types::HostDescriptor::rename_at(
440                self,
441                self_,
442                old_path,
443                new_descriptor,
444                new_path,
445            )
446            .await,
447        )
448    }
449
450    async fn symlink_at(
451        &mut self,
452        self_: Resource<Descriptor>,
453        old_path: String,
454        new_path: String,
455    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
456        convert_result(
457            latest::filesystem::types::HostDescriptor::symlink_at(self, self_, old_path, new_path)
458                .await,
459        )
460    }
461
462    async fn unlink_file_at(
463        &mut self,
464        self_: Resource<Descriptor>,
465        path: String,
466    ) -> wasmtime::Result<Result<(), FsErrorCode>> {
467        convert_result(
468            latest::filesystem::types::HostDescriptor::unlink_file_at(self, self_, path).await,
469        )
470    }
471
472    async fn is_same_object(
473        &mut self,
474        self_: Resource<Descriptor>,
475        other: Resource<Descriptor>,
476    ) -> wasmtime::Result<bool> {
477        latest::filesystem::types::HostDescriptor::is_same_object(self, self_, other).await
478    }
479
480    async fn metadata_hash(
481        &mut self,
482        self_: Resource<Descriptor>,
483    ) -> wasmtime::Result<Result<MetadataHashValue, FsErrorCode>> {
484        convert_result(latest::filesystem::types::HostDescriptor::metadata_hash(self, self_).await)
485    }
486
487    async fn metadata_hash_at(
488        &mut self,
489        self_: Resource<Descriptor>,
490        path_flags: PathFlags,
491        path: String,
492    ) -> wasmtime::Result<Result<MetadataHashValue, FsErrorCode>> {
493        convert_result(
494            latest::filesystem::types::HostDescriptor::metadata_hash_at(
495                self,
496                self_,
497                path_flags.into(),
498                path,
499            )
500            .await,
501        )
502    }
503
504    fn drop(&mut self, rep: Resource<Descriptor>) -> wasmtime::Result<()> {
505        latest::filesystem::types::HostDescriptor::drop(self, rep)
506    }
507}
508
509impl wasi::filesystem::types::HostDirectoryEntryStream for WasiCtxView<'_> {
510    async fn read_directory_entry(
511        &mut self,
512        self_: Resource<DirectoryEntryStream>,
513    ) -> wasmtime::Result<Result<Option<DirectoryEntry>, FsErrorCode>> {
514        convert_result(
515            latest::filesystem::types::HostDirectoryEntryStream::read_directory_entry(self, self_)
516                .await
517                .map(|e| e.map(DirectoryEntry::from)),
518        )
519    }
520
521    fn drop(&mut self, rep: Resource<DirectoryEntryStream>) -> wasmtime::Result<()> {
522        latest::filesystem::types::HostDirectoryEntryStream::drop(self, rep)
523    }
524}
525
526impl wasi::filesystem::preopens::Host for WasiCtxView<'_> {
527    fn get_directories(&mut self) -> wasmtime::Result<Vec<(Resource<Descriptor>, String)>> {
528        latest::filesystem::preopens::Host::get_directories(self)
529    }
530}
531
532impl wasi::io::poll::Host for WasiCtxView<'_> {
533    async fn poll(&mut self, list: Vec<Resource<Pollable>>) -> wasmtime::Result<Vec<u32>> {
534        latest::io::poll::Host::poll(self.table, list).await
535    }
536}
537
538impl wasi::io::poll::HostPollable for WasiCtxView<'_> {
539    async fn block(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<()> {
540        latest::io::poll::HostPollable::block(self.table, rep).await
541    }
542
543    async fn ready(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<bool> {
544        latest::io::poll::HostPollable::ready(self.table, rep).await
545    }
546
547    fn drop(&mut self, rep: Resource<Pollable>) -> wasmtime::Result<()> {
548        latest::io::poll::HostPollable::drop(self.table, rep)
549    }
550}
551
552impl wasi::io::error::Host for WasiCtxView<'_> {}
553
554impl wasi::io::error::HostError for WasiCtxView<'_> {
555    fn to_debug_string(&mut self, self_: Resource<IoError>) -> wasmtime::Result<String> {
556        latest::io::error::HostError::to_debug_string(self.table, self_)
557    }
558
559    fn drop(&mut self, rep: Resource<IoError>) -> wasmtime::Result<()> {
560        latest::io::error::HostError::drop(self.table, rep)
561    }
562}
563
564fn convert_stream_result<T, T2>(
565    view: &mut WasiCtxView<'_>,
566    result: Result<T, wasmtime_wasi::p2::StreamError>,
567) -> wasmtime::Result<Result<T2, StreamError>>
568where
569    T2: From<T>,
570{
571    match result {
572        Ok(e) => Ok(Ok(e.into())),
573        Err(wasmtime_wasi::p2::StreamError::Closed) => Ok(Err(StreamError::Closed)),
574        Err(wasmtime_wasi::p2::StreamError::LastOperationFailed(e)) => {
575            let e = view.table.push(e)?;
576            Ok(Err(StreamError::LastOperationFailed(e)))
577        }
578        Err(wasmtime_wasi::p2::StreamError::Trap(e)) => Err(e),
579    }
580}
581
582impl wasi::io::streams::Host for WasiCtxView<'_> {}
583
584impl wasi::io::streams::HostInputStream for WasiCtxView<'_> {
585    fn read(
586        &mut self,
587        self_: Resource<InputStream>,
588        len: u64,
589    ) -> wasmtime::Result<Result<Vec<u8>, StreamError>> {
590        let result = latest::io::streams::HostInputStream::read(self.table, self_, len);
591        convert_stream_result(self, result)
592    }
593
594    async fn blocking_read(
595        &mut self,
596        self_: Resource<InputStream>,
597        len: u64,
598    ) -> wasmtime::Result<Result<Vec<u8>, StreamError>> {
599        let result =
600            latest::io::streams::HostInputStream::blocking_read(self.table, self_, len).await;
601        convert_stream_result(self, result)
602    }
603
604    fn skip(
605        &mut self,
606        self_: Resource<InputStream>,
607        len: u64,
608    ) -> wasmtime::Result<Result<u64, StreamError>> {
609        let result = latest::io::streams::HostInputStream::skip(self.table, self_, len);
610        convert_stream_result(self, result)
611    }
612
613    async fn blocking_skip(
614        &mut self,
615        self_: Resource<InputStream>,
616        len: u64,
617    ) -> wasmtime::Result<Result<u64, StreamError>> {
618        let result =
619            latest::io::streams::HostInputStream::blocking_skip(self.table, self_, len).await;
620        convert_stream_result(self, result)
621    }
622
623    fn subscribe(&mut self, self_: Resource<InputStream>) -> wasmtime::Result<Resource<Pollable>> {
624        latest::io::streams::HostInputStream::subscribe(self.table, self_)
625    }
626
627    async fn drop(&mut self, rep: Resource<InputStream>) -> wasmtime::Result<()> {
628        latest::io::streams::HostInputStream::drop(self.table, rep).await
629    }
630}
631
632impl wasi::io::streams::HostOutputStream for WasiCtxView<'_> {
633    fn check_write(
634        &mut self,
635        self_: Resource<OutputStream>,
636    ) -> wasmtime::Result<Result<u64, StreamError>> {
637        let result = latest::io::streams::HostOutputStream::check_write(self.table, self_);
638        convert_stream_result(self, result)
639    }
640
641    fn write(
642        &mut self,
643        self_: Resource<OutputStream>,
644        contents: Vec<u8>,
645    ) -> wasmtime::Result<Result<(), StreamError>> {
646        let result = latest::io::streams::HostOutputStream::write(self.table, self_, contents);
647        convert_stream_result(self, result)
648    }
649
650    async fn blocking_write_and_flush(
651        &mut self,
652        self_: Resource<OutputStream>,
653        contents: Vec<u8>,
654    ) -> wasmtime::Result<Result<(), StreamError>> {
655        let result = latest::io::streams::HostOutputStream::blocking_write_and_flush(
656            self.table, self_, contents,
657        )
658        .await;
659        convert_stream_result(self, result)
660    }
661
662    fn flush(
663        &mut self,
664        self_: Resource<OutputStream>,
665    ) -> wasmtime::Result<Result<(), StreamError>> {
666        let result = latest::io::streams::HostOutputStream::flush(self.table, self_);
667        convert_stream_result(self, result)
668    }
669
670    async fn blocking_flush(
671        &mut self,
672        self_: Resource<OutputStream>,
673    ) -> wasmtime::Result<Result<(), StreamError>> {
674        let result = latest::io::streams::HostOutputStream::blocking_flush(self.table, self_).await;
675        convert_stream_result(self, result)
676    }
677
678    fn subscribe(&mut self, self_: Resource<OutputStream>) -> wasmtime::Result<Resource<Pollable>> {
679        latest::io::streams::HostOutputStream::subscribe(self.table, self_)
680    }
681
682    fn write_zeroes(
683        &mut self,
684        self_: Resource<OutputStream>,
685        len: u64,
686    ) -> wasmtime::Result<Result<(), StreamError>> {
687        let result = latest::io::streams::HostOutputStream::write_zeroes(self.table, self_, len);
688        convert_stream_result(self, result)
689    }
690
691    async fn blocking_write_zeroes_and_flush(
692        &mut self,
693        self_: Resource<OutputStream>,
694        len: u64,
695    ) -> wasmtime::Result<Result<(), StreamError>> {
696        let result = latest::io::streams::HostOutputStream::blocking_write_zeroes_and_flush(
697            self.table, self_, len,
698        )
699        .await;
700        convert_stream_result(self, result)
701    }
702
703    fn splice(
704        &mut self,
705        self_: Resource<OutputStream>,
706        src: Resource<InputStream>,
707        len: u64,
708    ) -> wasmtime::Result<Result<u64, StreamError>> {
709        let result = latest::io::streams::HostOutputStream::splice(self.table, self_, src, len);
710        convert_stream_result(self, result)
711    }
712
713    async fn blocking_splice(
714        &mut self,
715        self_: Resource<OutputStream>,
716        src: Resource<InputStream>,
717        len: u64,
718    ) -> wasmtime::Result<Result<u64, StreamError>> {
719        let result =
720            latest::io::streams::HostOutputStream::blocking_splice(self.table, self_, src, len)
721                .await;
722        convert_stream_result(self, result)
723    }
724
725    async fn drop(&mut self, rep: Resource<OutputStream>) -> wasmtime::Result<()> {
726        latest::io::streams::HostOutputStream::drop(self.table, rep).await
727    }
728}
729
730impl wasi::random::random::Host for WasiCtxView<'_> {
731    fn get_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
732        latest::random::random::Host::get_random_bytes(self.ctx.random(), len)
733    }
734
735    fn get_random_u64(&mut self) -> wasmtime::Result<u64> {
736        latest::random::random::Host::get_random_u64(self.ctx.random())
737    }
738}
739
740impl wasi::random::insecure::Host for WasiCtxView<'_> {
741    fn get_insecure_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
742        latest::random::insecure::Host::get_insecure_random_bytes(self.ctx.random(), len)
743    }
744
745    fn get_insecure_random_u64(&mut self) -> wasmtime::Result<u64> {
746        latest::random::insecure::Host::get_insecure_random_u64(self.ctx.random())
747    }
748}
749
750impl wasi::random::insecure_seed::Host for WasiCtxView<'_> {
751    fn insecure_seed(&mut self) -> wasmtime::Result<(u64, u64)> {
752        latest::random::insecure_seed::Host::insecure_seed(self.ctx.random())
753    }
754}
755
756impl wasi::cli::exit::Host for WasiCtxView<'_> {
757    fn exit(&mut self, status: Result<(), ()>) -> wasmtime::Result<()> {
758        latest::cli::exit::Host::exit(self, status)
759    }
760}
761
762impl wasi::cli::environment::Host for WasiCtxView<'_> {
763    fn get_environment(&mut self) -> wasmtime::Result<Vec<(String, String)>> {
764        latest::cli::environment::Host::get_environment(self)
765    }
766
767    fn get_arguments(&mut self) -> wasmtime::Result<Vec<String>> {
768        latest::cli::environment::Host::get_arguments(self)
769    }
770
771    fn initial_cwd(&mut self) -> wasmtime::Result<Option<String>> {
772        latest::cli::environment::Host::initial_cwd(self)
773    }
774}
775
776impl wasi::cli::stdin::Host for WasiCtxView<'_> {
777    fn get_stdin(&mut self) -> wasmtime::Result<Resource<InputStream>> {
778        latest::cli::stdin::Host::get_stdin(self)
779    }
780}
781
782impl wasi::cli::stdout::Host for WasiCtxView<'_> {
783    fn get_stdout(&mut self) -> wasmtime::Result<Resource<OutputStream>> {
784        latest::cli::stdout::Host::get_stdout(self)
785    }
786}
787
788impl wasi::cli::stderr::Host for WasiCtxView<'_> {
789    fn get_stderr(&mut self) -> wasmtime::Result<Resource<OutputStream>> {
790        latest::cli::stderr::Host::get_stderr(self)
791    }
792}
793
794impl wasi::cli::terminal_stdin::Host for WasiCtxView<'_> {
795    fn get_terminal_stdin(&mut self) -> wasmtime::Result<Option<Resource<TerminalInput>>> {
796        latest::cli::terminal_stdin::Host::get_terminal_stdin(self)
797    }
798}
799
800impl wasi::cli::terminal_stdout::Host for WasiCtxView<'_> {
801    fn get_terminal_stdout(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
802        latest::cli::terminal_stdout::Host::get_terminal_stdout(self)
803    }
804}
805
806impl wasi::cli::terminal_stderr::Host for WasiCtxView<'_> {
807    fn get_terminal_stderr(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
808        latest::cli::terminal_stderr::Host::get_terminal_stderr(self)
809    }
810}
811
812impl wasi::cli::terminal_input::Host for WasiCtxView<'_> {}
813
814impl wasi::cli::terminal_input::HostTerminalInput for WasiCtxView<'_> {
815    fn drop(&mut self, rep: Resource<TerminalInput>) -> wasmtime::Result<()> {
816        latest::cli::terminal_input::HostTerminalInput::drop(self, rep)
817    }
818}
819
820impl wasi::cli::terminal_output::Host for WasiCtxView<'_> {}
821
822impl wasi::cli::terminal_output::HostTerminalOutput for WasiCtxView<'_> {
823    fn drop(&mut self, rep: Resource<TerminalOutput>) -> wasmtime::Result<()> {
824        latest::cli::terminal_output::HostTerminalOutput::drop(self, rep)
825    }
826}
827
828impl wasi::sockets::tcp::Host for WasiCtxView<'_> {}
829
830impl wasi::sockets::tcp::HostTcpSocket for WasiCtxView<'_> {
831    async fn start_bind(
832        &mut self,
833        self_: Resource<TcpSocket>,
834        network: Resource<Network>,
835        local_address: IpSocketAddress,
836    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
837        convert_result(
838            latest::sockets::tcp::HostTcpSocket::start_bind(
839                self,
840                self_,
841                network,
842                local_address.into(),
843            )
844            .await,
845        )
846    }
847
848    fn finish_bind(
849        &mut self,
850        self_: Resource<TcpSocket>,
851    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
852        convert_result(latest::sockets::tcp::HostTcpSocket::finish_bind(
853            self, self_,
854        ))
855    }
856
857    async fn start_connect(
858        &mut self,
859        self_: Resource<TcpSocket>,
860        network: Resource<Network>,
861        remote_address: IpSocketAddress,
862    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
863        convert_result(
864            latest::sockets::tcp::HostTcpSocket::start_connect(
865                self,
866                self_,
867                network,
868                remote_address.into(),
869            )
870            .await,
871        )
872    }
873
874    fn finish_connect(
875        &mut self,
876        self_: Resource<TcpSocket>,
877    ) -> wasmtime::Result<Result<(Resource<InputStream>, Resource<OutputStream>), SocketErrorCode>>
878    {
879        convert_result(latest::sockets::tcp::HostTcpSocket::finish_connect(
880            self, self_,
881        ))
882    }
883
884    fn start_listen(
885        &mut self,
886        self_: Resource<TcpSocket>,
887    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
888        convert_result(latest::sockets::tcp::HostTcpSocket::start_listen(
889            self, self_,
890        ))
891    }
892
893    fn finish_listen(
894        &mut self,
895        self_: Resource<TcpSocket>,
896    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
897        convert_result(latest::sockets::tcp::HostTcpSocket::finish_listen(
898            self, self_,
899        ))
900    }
901
902    fn accept(
903        &mut self,
904        self_: Resource<TcpSocket>,
905    ) -> wasmtime::Result<
906        Result<
907            (
908                Resource<TcpSocket>,
909                Resource<InputStream>,
910                Resource<OutputStream>,
911            ),
912            SocketErrorCode,
913        >,
914    > {
915        convert_result(latest::sockets::tcp::HostTcpSocket::accept(self, self_))
916    }
917
918    fn local_address(
919        &mut self,
920        self_: Resource<TcpSocket>,
921    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
922        convert_result(latest::sockets::tcp::HostTcpSocket::local_address(
923            self, self_,
924        ))
925    }
926
927    fn remote_address(
928        &mut self,
929        self_: Resource<TcpSocket>,
930    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
931        convert_result(latest::sockets::tcp::HostTcpSocket::remote_address(
932            self, self_,
933        ))
934    }
935
936    fn address_family(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<IpAddressFamily> {
937        latest::sockets::tcp::HostTcpSocket::address_family(self, self_).map(|e| e.into())
938    }
939
940    fn ipv6_only(
941        &mut self,
942        _self_: Resource<TcpSocket>,
943    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
944        anyhow::bail!("ipv6-only API no longer supported")
945    }
946
947    fn set_ipv6_only(
948        &mut self,
949        _self_: Resource<TcpSocket>,
950        _value: bool,
951    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
952        anyhow::bail!("ipv6-only API no longer supported")
953    }
954
955    fn set_listen_backlog_size(
956        &mut self,
957        self_: Resource<TcpSocket>,
958        value: u64,
959    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
960        convert_result(
961            latest::sockets::tcp::HostTcpSocket::set_listen_backlog_size(self, self_, value),
962        )
963    }
964
965    fn is_listening(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<bool> {
966        latest::sockets::tcp::HostTcpSocket::is_listening(self, self_)
967    }
968
969    fn keep_alive_enabled(
970        &mut self,
971        self_: Resource<TcpSocket>,
972    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
973        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_enabled(
974            self, self_,
975        ))
976    }
977
978    fn set_keep_alive_enabled(
979        &mut self,
980        self_: Resource<TcpSocket>,
981        value: bool,
982    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
983        convert_result(latest::sockets::tcp::HostTcpSocket::set_keep_alive_enabled(
984            self, self_, value,
985        ))
986    }
987
988    fn keep_alive_idle_time(
989        &mut self,
990        self_: Resource<TcpSocket>,
991    ) -> wasmtime::Result<Result<Duration, SocketErrorCode>> {
992        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_idle_time(
993            self, self_,
994        ))
995    }
996
997    fn set_keep_alive_idle_time(
998        &mut self,
999        self_: Resource<TcpSocket>,
1000        value: Duration,
1001    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1002        convert_result(
1003            latest::sockets::tcp::HostTcpSocket::set_keep_alive_idle_time(self, self_, value),
1004        )
1005    }
1006
1007    fn keep_alive_interval(
1008        &mut self,
1009        self_: Resource<TcpSocket>,
1010    ) -> wasmtime::Result<Result<Duration, SocketErrorCode>> {
1011        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_interval(
1012            self, self_,
1013        ))
1014    }
1015
1016    fn set_keep_alive_interval(
1017        &mut self,
1018        self_: Resource<TcpSocket>,
1019        value: Duration,
1020    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1021        convert_result(
1022            latest::sockets::tcp::HostTcpSocket::set_keep_alive_interval(self, self_, value),
1023        )
1024    }
1025
1026    fn keep_alive_count(
1027        &mut self,
1028        self_: Resource<TcpSocket>,
1029    ) -> wasmtime::Result<Result<u32, SocketErrorCode>> {
1030        convert_result(latest::sockets::tcp::HostTcpSocket::keep_alive_count(
1031            self, self_,
1032        ))
1033    }
1034
1035    fn set_keep_alive_count(
1036        &mut self,
1037        self_: Resource<TcpSocket>,
1038        value: u32,
1039    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1040        convert_result(latest::sockets::tcp::HostTcpSocket::set_keep_alive_count(
1041            self, self_, value,
1042        ))
1043    }
1044
1045    fn hop_limit(
1046        &mut self,
1047        self_: Resource<TcpSocket>,
1048    ) -> wasmtime::Result<Result<u8, SocketErrorCode>> {
1049        convert_result(latest::sockets::tcp::HostTcpSocket::hop_limit(self, self_))
1050    }
1051
1052    fn set_hop_limit(
1053        &mut self,
1054        self_: Resource<TcpSocket>,
1055        value: u8,
1056    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1057        convert_result(latest::sockets::tcp::HostTcpSocket::set_hop_limit(
1058            self, self_, value,
1059        ))
1060    }
1061
1062    fn receive_buffer_size(
1063        &mut self,
1064        self_: Resource<TcpSocket>,
1065    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1066        convert_result(latest::sockets::tcp::HostTcpSocket::receive_buffer_size(
1067            self, self_,
1068        ))
1069    }
1070
1071    fn set_receive_buffer_size(
1072        &mut self,
1073        self_: Resource<TcpSocket>,
1074        value: u64,
1075    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1076        convert_result(
1077            latest::sockets::tcp::HostTcpSocket::set_receive_buffer_size(self, self_, value),
1078        )
1079    }
1080
1081    fn send_buffer_size(
1082        &mut self,
1083        self_: Resource<TcpSocket>,
1084    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1085        convert_result(latest::sockets::tcp::HostTcpSocket::send_buffer_size(
1086            self, self_,
1087        ))
1088    }
1089
1090    fn set_send_buffer_size(
1091        &mut self,
1092        self_: Resource<TcpSocket>,
1093        value: u64,
1094    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1095        convert_result(latest::sockets::tcp::HostTcpSocket::set_send_buffer_size(
1096            self, self_, value,
1097        ))
1098    }
1099
1100    fn subscribe(&mut self, self_: Resource<TcpSocket>) -> wasmtime::Result<Resource<Pollable>> {
1101        latest::sockets::tcp::HostTcpSocket::subscribe(self, self_)
1102    }
1103
1104    fn shutdown(
1105        &mut self,
1106        self_: Resource<TcpSocket>,
1107        shutdown_type: ShutdownType,
1108    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1109        convert_result(latest::sockets::tcp::HostTcpSocket::shutdown(
1110            self,
1111            self_,
1112            shutdown_type.into(),
1113        ))
1114    }
1115
1116    fn drop(&mut self, rep: Resource<TcpSocket>) -> wasmtime::Result<()> {
1117        latest::sockets::tcp::HostTcpSocket::drop(self, rep)
1118    }
1119}
1120
1121impl wasi::sockets::tcp_create_socket::Host for WasiCtxView<'_> {
1122    fn create_tcp_socket(
1123        &mut self,
1124        address_family: IpAddressFamily,
1125    ) -> wasmtime::Result<Result<Resource<TcpSocket>, SocketErrorCode>> {
1126        convert_result(latest::sockets::tcp_create_socket::Host::create_tcp_socket(
1127            self,
1128            address_family.into(),
1129        ))
1130    }
1131}
1132
1133impl wasi::sockets::udp::Host for WasiCtxView<'_> {}
1134
1135impl wasi::sockets::udp::HostUdpSocket for WasiCtxView<'_> {
1136    async fn start_bind(
1137        &mut self,
1138        self_: Resource<UdpSocket>,
1139        network: Resource<Network>,
1140        local_address: IpSocketAddress,
1141    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1142        convert_result(
1143            latest::sockets::udp::HostUdpSocket::start_bind(
1144                self,
1145                self_,
1146                network,
1147                local_address.into(),
1148            )
1149            .await,
1150        )
1151    }
1152
1153    fn finish_bind(
1154        &mut self,
1155        self_: Resource<UdpSocket>,
1156    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1157        convert_result(latest::sockets::udp::HostUdpSocket::finish_bind(
1158            self, self_,
1159        ))
1160    }
1161
1162    fn local_address(
1163        &mut self,
1164        self_: Resource<UdpSocket>,
1165    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
1166        convert_result(latest::sockets::udp::HostUdpSocket::local_address(
1167            self, self_,
1168        ))
1169    }
1170
1171    fn remote_address(
1172        &mut self,
1173        self_: Resource<UdpSocket>,
1174    ) -> wasmtime::Result<Result<IpSocketAddress, SocketErrorCode>> {
1175        convert_result(latest::sockets::udp::HostUdpSocket::remote_address(
1176            self, self_,
1177        ))
1178    }
1179
1180    fn address_family(&mut self, self_: Resource<UdpSocket>) -> wasmtime::Result<IpAddressFamily> {
1181        latest::sockets::udp::HostUdpSocket::address_family(self, self_).map(|e| e.into())
1182    }
1183
1184    fn ipv6_only(
1185        &mut self,
1186        _self_: Resource<UdpSocket>,
1187    ) -> wasmtime::Result<Result<bool, SocketErrorCode>> {
1188        anyhow::bail!("ipv6-only API no longer supported")
1189    }
1190
1191    fn set_ipv6_only(
1192        &mut self,
1193        _self_: Resource<UdpSocket>,
1194        _value: bool,
1195    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1196        anyhow::bail!("ipv6-only API no longer supported")
1197    }
1198
1199    fn unicast_hop_limit(
1200        &mut self,
1201        self_: Resource<UdpSocket>,
1202    ) -> wasmtime::Result<Result<u8, SocketErrorCode>> {
1203        convert_result(latest::sockets::udp::HostUdpSocket::unicast_hop_limit(
1204            self, self_,
1205        ))
1206    }
1207
1208    fn set_unicast_hop_limit(
1209        &mut self,
1210        self_: Resource<UdpSocket>,
1211        value: u8,
1212    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1213        convert_result(latest::sockets::udp::HostUdpSocket::set_unicast_hop_limit(
1214            self, self_, value,
1215        ))
1216    }
1217
1218    fn receive_buffer_size(
1219        &mut self,
1220        self_: Resource<UdpSocket>,
1221    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1222        convert_result(latest::sockets::udp::HostUdpSocket::receive_buffer_size(
1223            self, self_,
1224        ))
1225    }
1226
1227    fn set_receive_buffer_size(
1228        &mut self,
1229        self_: Resource<UdpSocket>,
1230        value: u64,
1231    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1232        convert_result(
1233            latest::sockets::udp::HostUdpSocket::set_receive_buffer_size(self, self_, value),
1234        )
1235    }
1236
1237    fn send_buffer_size(
1238        &mut self,
1239        self_: Resource<UdpSocket>,
1240    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1241        convert_result(latest::sockets::udp::HostUdpSocket::send_buffer_size(
1242            self, self_,
1243        ))
1244    }
1245
1246    fn set_send_buffer_size(
1247        &mut self,
1248        self_: Resource<UdpSocket>,
1249        value: u64,
1250    ) -> wasmtime::Result<Result<(), SocketErrorCode>> {
1251        convert_result(latest::sockets::udp::HostUdpSocket::set_send_buffer_size(
1252            self, self_, value,
1253        ))
1254    }
1255
1256    async fn stream(
1257        &mut self,
1258        self_: Resource<UdpSocket>,
1259        remote_address: Option<IpSocketAddress>,
1260    ) -> wasmtime::Result<
1261        Result<
1262            (
1263                Resource<IncomingDatagramStream>,
1264                Resource<OutgoingDatagramStream>,
1265            ),
1266            SocketErrorCode,
1267        >,
1268    > {
1269        convert_result(
1270            latest::sockets::udp::HostUdpSocket::stream(
1271                self,
1272                self_,
1273                remote_address.map(|a| a.into()),
1274            )
1275            .await,
1276        )
1277    }
1278
1279    fn subscribe(&mut self, self_: Resource<UdpSocket>) -> wasmtime::Result<Resource<Pollable>> {
1280        latest::sockets::udp::HostUdpSocket::subscribe(self, self_)
1281    }
1282
1283    fn drop(&mut self, rep: Resource<UdpSocket>) -> wasmtime::Result<()> {
1284        latest::sockets::udp::HostUdpSocket::drop(self, rep)
1285    }
1286}
1287
1288impl wasi::sockets::udp::HostOutgoingDatagramStream for WasiCtxView<'_> {
1289    fn check_send(
1290        &mut self,
1291        self_: Resource<OutgoingDatagramStream>,
1292    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1293        convert_result(latest::sockets::udp::HostOutgoingDatagramStream::check_send(self, self_))
1294    }
1295
1296    async fn send(
1297        &mut self,
1298        self_: Resource<OutgoingDatagramStream>,
1299        datagrams: Vec<OutgoingDatagram>,
1300    ) -> wasmtime::Result<Result<u64, SocketErrorCode>> {
1301        convert_result(
1302            latest::sockets::udp::HostOutgoingDatagramStream::send(
1303                self,
1304                self_,
1305                datagrams.into_iter().map(|d| d.into()).collect(),
1306            )
1307            .await,
1308        )
1309    }
1310
1311    fn subscribe(
1312        &mut self,
1313        self_: Resource<OutgoingDatagramStream>,
1314    ) -> wasmtime::Result<Resource<Pollable>> {
1315        latest::sockets::udp::HostOutgoingDatagramStream::subscribe(self, self_)
1316    }
1317
1318    fn drop(&mut self, rep: Resource<OutgoingDatagramStream>) -> wasmtime::Result<()> {
1319        latest::sockets::udp::HostOutgoingDatagramStream::drop(self, rep)
1320    }
1321}
1322
1323impl wasi::sockets::udp::HostIncomingDatagramStream for WasiCtxView<'_> {
1324    fn receive(
1325        &mut self,
1326        self_: Resource<IncomingDatagramStream>,
1327        max_results: u64,
1328    ) -> wasmtime::Result<Result<Vec<IncomingDatagram>, SocketErrorCode>> {
1329        convert_result(latest::sockets::udp::HostIncomingDatagramStream::receive(
1330            self,
1331            self_,
1332            max_results,
1333        ))
1334        .map(|r| r.map(|r: Vec<_>| r.into_iter().map(|d| d.into()).collect()))
1335    }
1336
1337    fn subscribe(
1338        &mut self,
1339        self_: Resource<IncomingDatagramStream>,
1340    ) -> wasmtime::Result<Resource<Pollable>> {
1341        latest::sockets::udp::HostIncomingDatagramStream::subscribe(self, self_)
1342    }
1343
1344    fn drop(&mut self, rep: Resource<IncomingDatagramStream>) -> wasmtime::Result<()> {
1345        latest::sockets::udp::HostIncomingDatagramStream::drop(self, rep)
1346    }
1347}
1348
1349impl wasi::sockets::udp_create_socket::Host for WasiCtxView<'_> {
1350    fn create_udp_socket(
1351        &mut self,
1352        address_family: IpAddressFamily,
1353    ) -> wasmtime::Result<Result<Resource<UdpSocket>, SocketErrorCode>> {
1354        convert_result(latest::sockets::udp_create_socket::Host::create_udp_socket(
1355            self,
1356            address_family.into(),
1357        ))
1358    }
1359}
1360
1361impl wasi::sockets::instance_network::Host for WasiCtxView<'_> {
1362    fn instance_network(&mut self) -> wasmtime::Result<Resource<Network>> {
1363        latest::sockets::instance_network::Host::instance_network(self)
1364    }
1365}
1366
1367impl wasi::sockets::network::Host for WasiCtxView<'_> {}
1368
1369impl wasi::sockets::network::HostNetwork for WasiCtxView<'_> {
1370    fn drop(&mut self, rep: Resource<Network>) -> wasmtime::Result<()> {
1371        latest::sockets::network::HostNetwork::drop(self, rep)
1372    }
1373}
1374
1375impl wasi::sockets::ip_name_lookup::Host for WasiCtxView<'_> {
1376    fn resolve_addresses(
1377        &mut self,
1378        network: Resource<Network>,
1379        name: String,
1380    ) -> wasmtime::Result<Result<Resource<ResolveAddressStream>, SocketErrorCode>> {
1381        convert_result(latest::sockets::ip_name_lookup::Host::resolve_addresses(
1382            self, network, name,
1383        ))
1384    }
1385}
1386
1387impl wasi::sockets::ip_name_lookup::HostResolveAddressStream for WasiCtxView<'_> {
1388    fn resolve_next_address(
1389        &mut self,
1390        self_: Resource<ResolveAddressStream>,
1391    ) -> wasmtime::Result<Result<Option<IpAddress>, SocketErrorCode>> {
1392        convert_result(
1393            latest::sockets::ip_name_lookup::HostResolveAddressStream::resolve_next_address(
1394                self, self_,
1395            )
1396            .map(|e| e.map(|e| e.into())),
1397        )
1398    }
1399
1400    fn subscribe(
1401        &mut self,
1402        self_: Resource<ResolveAddressStream>,
1403    ) -> wasmtime::Result<Resource<Pollable>> {
1404        latest::sockets::ip_name_lookup::HostResolveAddressStream::subscribe(self, self_)
1405    }
1406
1407    fn drop(&mut self, rep: Resource<ResolveAddressStream>) -> wasmtime::Result<()> {
1408        latest::sockets::ip_name_lookup::HostResolveAddressStream::drop(self, rep)
1409    }
1410}
1411
1412convert! {
1413    struct latest::clocks::wall_clock::Datetime [<=>] Datetime {
1414        seconds,
1415        nanoseconds,
1416    }
1417
1418    enum latest::filesystem::types::ErrorCode => FsErrorCode {
1419        Access,
1420        WouldBlock,
1421        Already,
1422        BadDescriptor,
1423        Busy,
1424        Deadlock,
1425        Quota,
1426        Exist,
1427        FileTooLarge,
1428        IllegalByteSequence,
1429        InProgress,
1430        Interrupted,
1431        Invalid,
1432        Io,
1433        IsDirectory,
1434        Loop,
1435        TooManyLinks,
1436        MessageSize,
1437        NameTooLong,
1438        NoDevice,
1439        NoEntry,
1440        NoLock,
1441        InsufficientMemory,
1442        InsufficientSpace,
1443        NotDirectory,
1444        NotEmpty,
1445        NotRecoverable,
1446        Unsupported,
1447        NoTty,
1448        NoSuchDevice,
1449        Overflow,
1450        NotPermitted,
1451        Pipe,
1452        ReadOnly,
1453        InvalidSeek,
1454        TextFileBusy,
1455        CrossDevice,
1456    }
1457
1458    enum Advice => latest::filesystem::types::Advice {
1459        Normal,
1460        Sequential,
1461        Random,
1462        WillNeed,
1463        DontNeed,
1464        NoReuse,
1465    }
1466
1467    flags DescriptorFlags [<=>] latest::filesystem::types::DescriptorFlags {
1468        READ,
1469        WRITE,
1470        FILE_INTEGRITY_SYNC,
1471        DATA_INTEGRITY_SYNC,
1472        REQUESTED_WRITE_SYNC,
1473        MUTATE_DIRECTORY,
1474    }
1475
1476    enum DescriptorType [<=>] latest::filesystem::types::DescriptorType {
1477        Unknown,
1478        BlockDevice,
1479        CharacterDevice,
1480        Directory,
1481        Fifo,
1482        SymbolicLink,
1483        RegularFile,
1484        Socket,
1485    }
1486
1487    enum NewTimestamp => latest::filesystem::types::NewTimestamp {
1488        NoChange,
1489        Now,
1490        Timestamp(e),
1491    }
1492
1493    flags PathFlags => latest::filesystem::types::PathFlags {
1494        SYMLINK_FOLLOW,
1495    }
1496
1497    flags OpenFlags => latest::filesystem::types::OpenFlags {
1498        CREATE,
1499        DIRECTORY,
1500        EXCLUSIVE,
1501        TRUNCATE,
1502    }
1503
1504    struct latest::filesystem::types::MetadataHashValue => MetadataHashValue {
1505        lower,
1506        upper,
1507    }
1508
1509    struct latest::filesystem::types::DirectoryEntry => DirectoryEntry {
1510        type_,
1511        name,
1512    }
1513
1514
1515    enum latest::sockets::network::ErrorCode => SocketErrorCode {
1516        Unknown,
1517        AccessDenied,
1518        NotSupported,
1519        InvalidArgument,
1520        OutOfMemory,
1521        Timeout,
1522        ConcurrencyConflict,
1523        NotInProgress,
1524        WouldBlock,
1525        InvalidState,
1526        NewSocketLimit,
1527        AddressNotBindable,
1528        AddressInUse,
1529        RemoteUnreachable,
1530        ConnectionRefused,
1531        ConnectionReset,
1532        ConnectionAborted,
1533        DatagramTooLarge,
1534        NameUnresolvable,
1535        TemporaryResolverFailure,
1536        PermanentResolverFailure,
1537    }
1538
1539    enum latest::sockets::network::IpAddress [<=>] IpAddress {
1540        Ipv4(e),
1541        Ipv6(e),
1542    }
1543
1544    enum latest::sockets::network::IpSocketAddress [<=>] IpSocketAddress {
1545        Ipv4(e),
1546        Ipv6(e),
1547    }
1548
1549    struct latest::sockets::network::Ipv4SocketAddress [<=>] Ipv4SocketAddress {
1550        port,
1551        address,
1552    }
1553
1554    struct latest::sockets::network::Ipv6SocketAddress [<=>] Ipv6SocketAddress {
1555        port,
1556        flow_info,
1557        scope_id,
1558        address,
1559    }
1560
1561    enum latest::sockets::network::IpAddressFamily [<=>] IpAddressFamily {
1562        Ipv4,
1563        Ipv6,
1564    }
1565
1566    enum ShutdownType => latest::sockets::tcp::ShutdownType {
1567        Receive,
1568        Send,
1569        Both,
1570    }
1571
1572    struct latest::sockets::udp::IncomingDatagram => IncomingDatagram {
1573        data,
1574        remote_address,
1575    }
1576}
1577
1578impl From<latest::filesystem::types::DescriptorStat> for DescriptorStat {
1579    fn from(e: latest::filesystem::types::DescriptorStat) -> DescriptorStat {
1580        DescriptorStat {
1581            type_: e.type_.into(),
1582            link_count: e.link_count,
1583            size: e.size,
1584            data_access_timestamp: e.data_access_timestamp.map(|e| e.into()),
1585            data_modification_timestamp: e.data_modification_timestamp.map(|e| e.into()),
1586            status_change_timestamp: e.status_change_timestamp.map(|e| e.into()),
1587        }
1588    }
1589}
1590
1591impl From<OutgoingDatagram> for latest::sockets::udp::OutgoingDatagram {
1592    fn from(d: OutgoingDatagram) -> Self {
1593        Self {
1594            data: d.data,
1595            remote_address: d.remote_address.map(|a| a.into()),
1596        }
1597    }
1598}