Skip to main content

spin_factor_wasi/
wasi_2023_11_10.rs

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