|
1 | 1 | use anyhow::{Context, Result}; |
2 | 2 | use wasi_cap_std_sync::WasiCtxBuilder; |
3 | | -use wasi_common::{wasi, WasiCtx}; |
| 3 | +use wasi_common::{wasi, Table, WasiCtx, WasiView}; |
4 | 4 | use wasmtime::{ |
5 | 5 | component::{Component, Linker}, |
6 | 6 | Config, Engine, Store, |
7 | 7 | }; |
| 8 | +use wasmtime_wasi_sockets::{WasiSocketsCtx, WasiSocketsView}; |
| 9 | +use wasmtime_wasi_sockets_sync::WasiSocketsCtxBuilder; |
8 | 10 |
|
9 | 11 | use clap::Parser; |
10 | 12 |
|
@@ -60,74 +62,119 @@ async fn main() -> Result<()> { |
60 | 62 |
|
61 | 63 | let engine = Engine::new(&config)?; |
62 | 64 | let component = Component::from_file(&engine, &input)?; |
63 | | - let mut linker = Linker::new(&engine); |
64 | 65 |
|
65 | 66 | let mut argv: Vec<&str> = vec!["wasm"]; |
66 | 67 | argv.extend(args.args.iter().map(String::as_str)); |
67 | 68 |
|
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); |
72 | 70 |
|
73 | 71 | for (guest, host) in args.map_dirs { |
74 | 72 | let dir = cap_std::fs::Dir::open_ambient_dir(&host, cap_std::ambient_authority()) |
75 | 73 | .context(format!("opening directory {host:?}"))?; |
76 | 74 | builder = builder.preopened_dir(dir, &guest); |
77 | 75 | } |
78 | 76 |
|
79 | | - let wasi_ctx = builder.build()?; |
| 77 | + let mut table = Table::new(); |
| 78 | + let wasi = builder.build(&mut table)?; |
80 | 79 |
|
81 | 80 | 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(()) |
83 | 140 | } 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) |
85 | 179 | } |
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(()) |
133 | 180 | } |
0 commit comments