Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit bba1b8b

Browse files
authored
Merge pull request #160 from bytecodealliance/pch/separate-sockets
separate crates for wasi-sockets
2 parents 5258f49 + 732bfd9 commit bba1b8b

40 files changed

+746
-475
lines changed

Cargo.lock

Lines changed: 43 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@ version.workspace = true
44
edition.workspace = true
55

66
[workspace]
7-
members = ["verify", "host", "test-programs", "test-programs/wasi-tests", "test-programs/reactor-tests", "wasi-common", "byte-array", "wasi"]
7+
members = [
8+
"verify",
9+
"host",
10+
"test-programs",
11+
"test-programs/wasi-tests",
12+
"test-programs/reactor-tests",
13+
"wasi-common",
14+
"wasi-common/cap-std-sync",
15+
"wasi-sockets",
16+
"wasi-sockets/sync",
17+
"byte-array",
18+
"wasi"
19+
]
820

921
[workspace.package]
1022
version = "0.0.0"
@@ -26,6 +38,8 @@ async-trait = "0.1.59"
2638
io-lifetimes = { version = "1.0.0", default-features = false }
2739
wasi-common = { path = "wasi-common" }
2840
wasi-cap-std-sync = { path = "wasi-common/cap-std-sync" }
41+
wasmtime-wasi-sockets = { path = "wasi-sockets" }
42+
wasmtime-wasi-sockets-sync = { path = "wasi-sockets/sync" }
2943
once_cell = "1.12.0"
3044
system-interface = { version = "0.25.1", features = ["cap_std_impls"] }
3145
wit-bindgen = { version = "0.4.0", default-features = false }

host/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cap-std = { workspace = true }
99
wasmtime = { workspace = true }
1010
wasi-common = { workspace = true }
1111
wasi-cap-std-sync = { workspace = true }
12+
wasmtime-wasi-sockets = { workspace = true }
13+
wasmtime-wasi-sockets-sync = { workspace = true }
1214
clap = { version = "4.1.9", features = ["derive"] }
1315
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt" ]}
1416
tokio = { version = "1", features = ["full"] }

host/src/main.rs

Lines changed: 103 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use anyhow::{Context, Result};
22
use wasi_cap_std_sync::WasiCtxBuilder;
3-
use wasi_common::{wasi, WasiCtx};
3+
use wasi_common::{wasi, Table, WasiCtx, WasiView};
44
use wasmtime::{
55
component::{Component, Linker},
66
Config, Engine, Store,
77
};
8+
use wasmtime_wasi_sockets::{WasiSocketsCtx, WasiSocketsView};
9+
use wasmtime_wasi_sockets_sync::WasiSocketsCtxBuilder;
810

911
use clap::Parser;
1012

@@ -60,74 +62,119 @@ async fn main() -> Result<()> {
6062

6163
let engine = Engine::new(&config)?;
6264
let component = Component::from_file(&engine, &input)?;
63-
let mut linker = Linker::new(&engine);
6465

6566
let mut argv: Vec<&str> = vec!["wasm"];
6667
argv.extend(args.args.iter().map(String::as_str));
6768

68-
let mut builder = WasiCtxBuilder::new()
69-
.inherit_stdio()
70-
.inherit_network(cap_std::ambient_authority())
71-
.args(&argv);
69+
let mut builder = WasiCtxBuilder::new().inherit_stdio().args(&argv);
7270

7371
for (guest, host) in args.map_dirs {
7472
let dir = cap_std::fs::Dir::open_ambient_dir(&host, cap_std::ambient_authority())
7573
.context(format!("opening directory {host:?}"))?;
7674
builder = builder.preopened_dir(dir, &guest);
7775
}
7876

79-
let wasi_ctx = builder.build()?;
77+
let mut table = Table::new();
78+
let wasi = builder.build(&mut table)?;
8079

8180
if args.world == "command" {
82-
run_command(&mut linker, &engine, &component, wasi_ctx).await?;
81+
struct CommandCtx {
82+
table: Table,
83+
wasi: WasiCtx,
84+
sockets: WasiSocketsCtx,
85+
}
86+
impl WasiView for CommandCtx {
87+
fn table(&self) -> &Table {
88+
&self.table
89+
}
90+
fn table_mut(&mut self) -> &mut Table {
91+
&mut self.table
92+
}
93+
fn ctx(&self) -> &WasiCtx {
94+
&self.wasi
95+
}
96+
fn ctx_mut(&mut self) -> &mut WasiCtx {
97+
&mut self.wasi
98+
}
99+
}
100+
let sockets = WasiSocketsCtxBuilder::new()
101+
.inherit_network(cap_std::ambient_authority())
102+
.build();
103+
impl WasiSocketsView for CommandCtx {
104+
fn table(&self) -> &Table {
105+
&self.table
106+
}
107+
fn table_mut(&mut self) -> &mut Table {
108+
&mut self.table
109+
}
110+
fn ctx(&self) -> &WasiSocketsCtx {
111+
&self.sockets
112+
}
113+
fn ctx_mut(&mut self) -> &mut WasiSocketsCtx {
114+
&mut self.sockets
115+
}
116+
}
117+
118+
let mut linker = Linker::new(&engine);
119+
wasi::command::add_to_linker(&mut linker)?;
120+
wasmtime_wasi_sockets::add_to_linker(&mut linker)?;
121+
let mut store = Store::new(
122+
&engine,
123+
CommandCtx {
124+
table,
125+
wasi,
126+
sockets,
127+
},
128+
);
129+
130+
let (wasi, _instance) =
131+
wasi::command::Command::instantiate_async(&mut store, &component, &linker).await?;
132+
133+
let result: Result<(), ()> = wasi.call_main(&mut store).await?;
134+
135+
if result.is_err() {
136+
anyhow::bail!("command returned with failing exit status");
137+
}
138+
139+
Ok(())
83140
} else if args.world == "proxy" {
84-
run_proxy(&mut linker, &engine, &component, wasi_ctx).await?;
141+
struct ProxyCtx {
142+
table: Table,
143+
wasi: WasiCtx,
144+
}
145+
impl WasiView for ProxyCtx {
146+
fn table(&self) -> &Table {
147+
&self.table
148+
}
149+
fn table_mut(&mut self) -> &mut Table {
150+
&mut self.table
151+
}
152+
fn ctx(&self) -> &WasiCtx {
153+
&self.wasi
154+
}
155+
fn ctx_mut(&mut self) -> &mut WasiCtx {
156+
&mut self.wasi
157+
}
158+
}
159+
160+
let mut linker = Linker::new(&engine);
161+
wasi::proxy::add_to_linker(&mut linker)?;
162+
163+
let mut store = Store::new(&engine, ProxyCtx { table, wasi });
164+
165+
let (wasi, _instance) =
166+
wasi::proxy::Proxy::instantiate_async(&mut store, &component, &linker).await?;
167+
168+
// TODO: do something
169+
let _ = wasi;
170+
let result: Result<(), ()> = Ok(());
171+
172+
if result.is_err() {
173+
anyhow::bail!("proxy returned with failing exit status");
174+
}
175+
176+
Ok(())
177+
} else {
178+
anyhow::bail!("no such world {}", args.world)
85179
}
86-
87-
Ok(())
88-
}
89-
90-
async fn run_command(
91-
linker: &mut Linker<WasiCtx>,
92-
engine: &Engine,
93-
component: &Component,
94-
wasi_ctx: WasiCtx,
95-
) -> anyhow::Result<()> {
96-
wasi::command::add_to_linker(linker, |x| x)?;
97-
let mut store = Store::new(engine, wasi_ctx);
98-
99-
let (wasi, _instance) =
100-
wasi::command::Command::instantiate_async(&mut store, component, linker).await?;
101-
102-
let result: Result<(), ()> = wasi.call_main(&mut store).await?;
103-
104-
if result.is_err() {
105-
anyhow::bail!("command returned with failing exit status");
106-
}
107-
108-
Ok(())
109-
}
110-
111-
async fn run_proxy(
112-
linker: &mut Linker<WasiCtx>,
113-
engine: &Engine,
114-
component: &Component,
115-
wasi_ctx: WasiCtx,
116-
) -> anyhow::Result<()> {
117-
wasi::proxy::add_to_linker(linker, |x| x)?;
118-
119-
let mut store = Store::new(engine, wasi_ctx);
120-
121-
let (wasi, _instance) =
122-
wasi::proxy::Proxy::instantiate_async(&mut store, component, linker).await?;
123-
124-
// TODO: do something
125-
let _ = wasi;
126-
let result: Result<(), ()> = Ok(());
127-
128-
if result.is_err() {
129-
anyhow::bail!("proxy returned with failing exit status");
130-
}
131-
132-
Ok(())
133180
}

0 commit comments

Comments
 (0)