Skip to content

Commit dead475

Browse files
committed
Adapting the code to the new bindings.
1 parent 5fd0eaa commit dead475

File tree

19 files changed

+1487
-927
lines changed

19 files changed

+1487
-927
lines changed

crates/wasi-preview1-component-adapter/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ pub unsafe extern "C" fn poll_oneoff(
17481748
});
17491749
}
17501750

1751-
#[link(wasm_import_module = "wasi:poll/poll")]
1751+
#[link(wasm_import_module = "wasi:io/poll")]
17521752
extern "C" {
17531753
#[link_name = "poll-oneoff"]
17541754
fn poll_oneoff_import(pollables: *const Pollable, len: usize, rval: *mut BoolList);

crates/wasi/src/preview2/command.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ wasmtime::component::bindgen!({
1313
"wasi:filesystem/preopens": crate::preview2::bindings::filesystem::preopens,
1414
"wasi:sockets/tcp": crate::preview2::bindings::sockets::tcp,
1515
"wasi:clocks/monotonic_clock": crate::preview2::bindings::clocks::monotonic_clock,
16-
"wasi:poll/poll": crate::preview2::bindings::poll::poll,
16+
"wasi:io/poll": crate::preview2::bindings::io::poll,
1717
"wasi:io/streams": crate::preview2::bindings::io::streams,
1818
"wasi:clocks/timezone": crate::preview2::bindings::clocks::timezone,
1919
"wasi:clocks/wall_clock": crate::preview2::bindings::clocks::wall_clock,
@@ -31,13 +31,15 @@ wasmtime::component::bindgen!({
3131
},
3232
});
3333

34-
pub fn add_to_linker<T: WasiView>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()> {
34+
pub fn add_to_linker<T: WasiView + Sync>(
35+
l: &mut wasmtime::component::Linker<T>,
36+
) -> anyhow::Result<()> {
3537
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
3638
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
3739
crate::preview2::bindings::clocks::timezone::add_to_linker(l, |t| t)?;
3840
crate::preview2::bindings::filesystem::types::add_to_linker(l, |t| t)?;
3941
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
40-
crate::preview2::bindings::poll::poll::add_to_linker(l, |t| t)?;
42+
crate::preview2::bindings::io::poll::add_to_linker(l, |t| t)?;
4143
crate::preview2::bindings::io::streams::add_to_linker(l, |t| t)?;
4244
crate::preview2::bindings::random::random::add_to_linker(l, |t| t)?;
4345
crate::preview2::bindings::cli::exit::add_to_linker(l, |t| t)?;
@@ -73,7 +75,7 @@ pub mod sync {
7375
"wasi:filesystem/preopens": crate::preview2::bindings::filesystem::preopens,
7476
"wasi:sockets/tcp": crate::preview2::bindings::sockets::tcp,
7577
"wasi:clocks/monotonic_clock": crate::preview2::bindings::clocks::monotonic_clock,
76-
"wasi:poll/poll": crate::preview2::bindings::sync_io::poll::poll,
78+
"wasi:io/poll": crate::preview2::bindings::sync_io::io::poll,
7779
"wasi:io/streams": crate::preview2::bindings::sync_io::io::streams,
7880
"wasi:clocks/timezone": crate::preview2::bindings::clocks::timezone,
7981
"wasi:clocks/wall_clock": crate::preview2::bindings::clocks::wall_clock,
@@ -91,15 +93,15 @@ pub mod sync {
9193
},
9294
});
9395

94-
pub fn add_to_linker<T: WasiView>(
96+
pub fn add_to_linker<T: WasiView + Sync>(
9597
l: &mut wasmtime::component::Linker<T>,
9698
) -> anyhow::Result<()> {
9799
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
98100
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
99101
crate::preview2::bindings::clocks::timezone::add_to_linker(l, |t| t)?;
100102
crate::preview2::bindings::sync_io::filesystem::types::add_to_linker(l, |t| t)?;
101103
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
102-
crate::preview2::bindings::sync_io::poll::poll::add_to_linker(l, |t| t)?;
104+
crate::preview2::bindings::sync_io::io::poll::add_to_linker(l, |t| t)?;
103105
crate::preview2::bindings::sync_io::io::streams::add_to_linker(l, |t| t)?;
104106
crate::preview2::bindings::random::random::add_to_linker(l, |t| t)?;
105107
crate::preview2::bindings::cli::exit::add_to_linker(l, |t| t)?;

crates/wasi/src/preview2/ctx.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::clocks::host::{monotonic_clock, wall_clock};
2+
use crate::preview2::bindings::filesystem::types::Descriptor;
23
use crate::preview2::{
34
clocks::{self, HostMonotonicClock, HostWallClock},
45
filesystem::{Dir, TableFsExt},
@@ -13,6 +14,7 @@ use cap_std::net::Pool;
1314
use cap_std::{ambient_authority, AmbientAuthority};
1415
use std::mem;
1516
use std::net::{Ipv4Addr, Ipv6Addr};
17+
use wasmtime::component::Resource;
1618

1719
pub struct WasiCtxBuilder {
1820
stdin: (Box<dyn HostInputStream>, IsATTY),
@@ -343,7 +345,7 @@ pub struct WasiCtx {
343345
pub(crate) monotonic_clock: Box<dyn HostMonotonicClock + Send + Sync>,
344346
pub(crate) env: Vec<(String, String)>,
345347
pub(crate) args: Vec<String>,
346-
pub(crate) preopens: Vec<(u32, String)>,
348+
pub(crate) preopens: Vec<(Resource<Descriptor>, String)>,
347349
pub(crate) stdin: StdioInput,
348350
pub(crate) stdout: StdioOutput,
349351
pub(crate) stderr: StdioOutput,

crates/wasi/src/preview2/filesystem.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::preview2::bindings::filesystem::types::Descriptor;
12
use crate::preview2::{
23
AbortOnDropJoinHandle, HostOutputStream, OutputStreamError, StreamRuntimeError, StreamState,
34
Table, TableError,
@@ -6,6 +7,7 @@ use anyhow::anyhow;
67
use bytes::{Bytes, BytesMut};
78
use futures::future::{maybe_done, MaybeDone};
89
use std::sync::Arc;
10+
use wasmtime::component::Resource;
911

1012
bitflags::bitflags! {
1113
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -44,42 +46,42 @@ impl File {
4446
}
4547
}
4648
pub(crate) trait TableFsExt {
47-
fn push_file(&mut self, file: File) -> Result<u32, TableError>;
48-
fn delete_file(&mut self, fd: u32) -> Result<File, TableError>;
49-
fn is_file(&self, fd: u32) -> bool;
50-
fn get_file(&self, fd: u32) -> Result<&File, TableError>;
49+
fn push_file(&mut self, file: File) -> Result<Resource<Descriptor>, TableError>;
50+
fn delete_file(&mut self, fd: Resource<Descriptor>) -> Result<File, TableError>;
51+
fn is_file(&self, fd: &Resource<Descriptor>) -> bool;
52+
fn get_file(&self, fd: &Resource<Descriptor>) -> Result<&File, TableError>;
5153

52-
fn push_dir(&mut self, dir: Dir) -> Result<u32, TableError>;
53-
fn delete_dir(&mut self, fd: u32) -> Result<Dir, TableError>;
54-
fn is_dir(&self, fd: u32) -> bool;
55-
fn get_dir(&self, fd: u32) -> Result<&Dir, TableError>;
54+
fn push_dir(&mut self, dir: Dir) -> Result<Resource<Descriptor>, TableError>;
55+
fn delete_dir(&mut self, fd: Resource<Descriptor>) -> Result<Dir, TableError>;
56+
fn is_dir(&self, fd: &Resource<Descriptor>) -> bool;
57+
fn get_dir(&self, fd: &Resource<Descriptor>) -> Result<&Dir, TableError>;
5658
}
5759

5860
impl TableFsExt for Table {
59-
fn push_file(&mut self, file: File) -> Result<u32, TableError> {
60-
self.push(Box::new(file))
61+
fn push_file(&mut self, file: File) -> Result<Resource<Descriptor>, TableError> {
62+
Ok(Resource::new_own(self.push(Box::new(file))?))
6163
}
62-
fn delete_file(&mut self, fd: u32) -> Result<File, TableError> {
63-
self.delete(fd)
64+
fn delete_file(&mut self, fd: Resource<Descriptor>) -> Result<File, TableError> {
65+
self.delete(fd.rep())
6466
}
65-
fn is_file(&self, fd: u32) -> bool {
66-
self.is::<File>(fd)
67+
fn is_file(&self, fd: &Resource<Descriptor>) -> bool {
68+
self.is::<File>(fd.rep())
6769
}
68-
fn get_file(&self, fd: u32) -> Result<&File, TableError> {
69-
self.get(fd)
70+
fn get_file(&self, fd: &Resource<Descriptor>) -> Result<&File, TableError> {
71+
self.get(fd.rep())
7072
}
7173

72-
fn push_dir(&mut self, dir: Dir) -> Result<u32, TableError> {
73-
self.push(Box::new(dir))
74+
fn push_dir(&mut self, dir: Dir) -> Result<Resource<Descriptor>, TableError> {
75+
Ok(Resource::new_own(self.push(Box::new(dir))?))
7476
}
75-
fn delete_dir(&mut self, fd: u32) -> Result<Dir, TableError> {
76-
self.delete(fd)
77+
fn delete_dir(&mut self, fd: Resource<Descriptor>) -> Result<Dir, TableError> {
78+
self.delete(fd.rep())
7779
}
78-
fn is_dir(&self, fd: u32) -> bool {
79-
self.is::<Dir>(fd)
80+
fn is_dir(&self, fd: &Resource<Descriptor>) -> bool {
81+
self.is::<Dir>(fd.rep())
8082
}
81-
fn get_dir(&self, fd: u32) -> Result<&Dir, TableError> {
82-
self.get(fd)
83+
fn get_dir(&self, fd: &Resource<Descriptor>) -> Result<&Dir, TableError> {
84+
self.get(fd.rep())
8385
}
8486
}
8587

crates/wasi/src/preview2/host/clocks.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
use crate::preview2::bindings::{
44
clocks::monotonic_clock::{self, Instant},
5-
clocks::timezone::{self, Timezone, TimezoneDisplay},
5+
clocks::timezone::{self, TimezoneDisplay},
66
clocks::wall_clock::{self, Datetime},
7-
poll::poll::Pollable,
7+
io::poll::Pollable,
88
};
99
use crate::preview2::{HostPollable, TablePollableExt, WasiView};
1010
use cap_std::time::SystemTime;
11+
use wasmtime::component::Resource;
1112

1213
impl TryFrom<SystemTime> for Datetime {
1314
type Error = anyhow::Error;
@@ -50,7 +51,7 @@ impl<T: WasiView> monotonic_clock::Host for T {
5051
Ok(self.ctx().monotonic_clock.resolution())
5152
}
5253

53-
fn subscribe(&mut self, when: Instant, absolute: bool) -> anyhow::Result<Pollable> {
54+
fn subscribe(&mut self, when: Instant, absolute: bool) -> anyhow::Result<Resource<Pollable>> {
5455
use std::time::Duration;
5556
// Calculate time relative to clock object, which may not have the same zero
5657
// point as tokio Inst::now()
@@ -93,15 +94,11 @@ impl<T: WasiView> monotonic_clock::Host for T {
9394
}
9495

9596
impl<T: WasiView> timezone::Host for T {
96-
fn display(&mut self, timezone: Timezone, when: Datetime) -> anyhow::Result<TimezoneDisplay> {
97+
fn display(&mut self, when: Datetime) -> anyhow::Result<TimezoneDisplay> {
9798
todo!("timezone display is not implemented")
9899
}
99100

100-
fn utc_offset(&mut self, timezone: Timezone, when: Datetime) -> anyhow::Result<i32> {
101+
fn utc_offset(&mut self, when: Datetime) -> anyhow::Result<i32> {
101102
todo!("timezone utc_offset is not implemented")
102103
}
103-
104-
fn drop_timezone(&mut self, timezone: Timezone) -> anyhow::Result<()> {
105-
todo!("timezone drop is not implemented")
106-
}
107104
}

0 commit comments

Comments
 (0)