Skip to content

Commit 559ede0

Browse files
author
Peter Huene
authored
Update wasm-compose example based on latest changes. (#1154)
This commit updates the wasm-compose example based on the latest `cargo-component` changes.
1 parent 64fee0e commit 559ede0

File tree

9 files changed

+31
-36
lines changed

9 files changed

+31
-36
lines changed

crates/wasm-compose/example/middleware/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ publish = false
66

77
[dependencies]
88
flate2 = "1.0.24"
9-
wit-bindgen = { version = "0.6.0", default_features = false }
9+
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }
1010

1111
[lib]
1212
crate-type = ["cdylib"]

crates/wasm-compose/example/middleware/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use bindings::example::service::handler as downstream;
2-
use bindings::exports::example::service::handler::{Error, Handler, Request, Response};
1+
cargo_component_bindings::generate!();
2+
3+
use bindings::{
4+
example::service::handler as downstream,
5+
exports::example::service::handler::{Error, Handler, Request, Response},
6+
};
37
use flate2::{write::GzEncoder, Compression};
48
use std::io::Write;
59

@@ -44,5 +48,3 @@ impl Handler for Component {
4448
Ok(response)
4549
}
4650
}
47-
48-
bindings::export!(Component);

crates/wasm-compose/example/server/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ publish = false
66

77
[dependencies]
88
async-std = { version = "1.12.0", features = ["attributes"] }
9-
clap = { version = "3.2.23", features = ["derive"] }
10-
driftwood = "0.0.6"
9+
clap = { version = "4.3.19", features = ["derive"] }
10+
driftwood = "0.0.7"
1111
tide = "0.16.0"
12-
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364", features = ["component-model"] }
13-
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364" }
14-
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364" }
15-
anyhow = "1.0.71"
12+
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b", features = ["component-model"] }
13+
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b" }
14+
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b" }
15+
anyhow = "1.0.72"
1616

1717
[workspace]

crates/wasm-compose/example/server/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ search-paths:
44
instantiations:
55
$input:
66
arguments:
7-
example:service/handler: svc
7+
example:service/handler@0.1.0: svc

crates/wasm-compose/example/server/src/main.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tide::{
1010
};
1111

1212
use wasmtime::{component::*, Config, Engine, Store};
13-
use wasmtime_wasi::preview2::{wasi, Table, WasiCtx, WasiCtxBuilder, WasiView};
13+
use wasmtime_wasi::preview2::{command, Table, WasiCtx, WasiCtxBuilder, WasiView};
1414

1515
use exports::example::service::*;
1616

@@ -118,33 +118,26 @@ impl ServerApp {
118118
let body = req.body_bytes().await?;
119119
let headers = req
120120
.iter()
121-
.map(|(n, v)| (n.as_str().as_bytes(), v.as_str().as_bytes()))
121+
.map(|(n, v)| {
122+
(
123+
n.as_str().as_bytes().to_vec(),
124+
v.as_str().as_bytes().to_vec(),
125+
)
126+
})
122127
.collect::<Vec<_>>();
123128

124129
// Create a new store for the request
125130
let state = req.state();
126131
let mut linker = Linker::new(&state.engine);
127-
wasi::filesystem::filesystem::add_to_linker(&mut linker, |x| x)?;
128-
wasi::io::streams::add_to_linker(&mut linker, |x| x)?;
129-
wasi::cli_base::environment::add_to_linker(&mut linker, |x| x)?;
130-
wasi::cli_base::preopens::add_to_linker(&mut linker, |x| x)?;
131-
wasi::cli_base::exit::add_to_linker(&mut linker, |x| x)?;
132-
wasi::cli_base::stdin::add_to_linker(&mut linker, |x| x)?;
133-
wasi::cli_base::stdout::add_to_linker(&mut linker, |x| x)?;
134-
wasi::cli_base::stderr::add_to_linker(&mut linker, |x| x)?;
132+
command::add_to_linker(&mut linker)?;
135133

136134
let wasi_view = ServerWasiView::new()?;
137135
let mut store = Store::new(&state.engine, wasi_view);
138-
let (service, _) = Service::instantiate_async(&mut store, &state.component, &linker).await?;
136+
let (service, _) =
137+
Service::instantiate_async(&mut store, &state.component, &linker).await?;
139138
service
140139
.example_service_handler()
141-
.call_execute(
142-
&mut store,
143-
handler::Request {
144-
headers: &headers,
145-
body: &body,
146-
},
147-
)
140+
.call_execute(&mut store, &handler::Request { headers, body })
148141
.await?
149142
.map(TryInto::try_into)
150143
.map_err(handler::Error::into_tide)?

crates/wasm-compose/example/service.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package example:service
1+
package example:service@0.1.0
22

33
interface handler {
44
record request {

crates/wasm-compose/example/service/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
wit-bindgen = { version = "0.6.0", default_features = false }
8+
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }
99

1010
[lib]
1111
crate-type = ["cdylib"]

crates/wasm-compose/example/service/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cargo_component_bindings::generate!();
2+
13
use bindings::exports::example::service::handler::{Error, Handler, Request, Response};
24
use std::str;
35

@@ -27,5 +29,3 @@ impl Handler for Component {
2729
})
2830
}
2931
}
30-
31-
bindings::export!(Component);

crates/wasm-compose/src/encoding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'a> TypeState<'a> {
178178
type_exports_rev: HashMap::new(),
179179
instance_exports: HashMap::new(),
180180
type_defs: HashMap::new(),
181-
encodable: encodable,
181+
encodable,
182182
},
183183
);
184184
self.scopes.push(prev);
@@ -1324,7 +1324,7 @@ fn toposort<'a>(
13241324
}
13251325
if let Some(list) = deps.get(cur) {
13261326
for dep in list {
1327-
toposort(*dep, deps, order);
1327+
toposort(dep, deps, order);
13281328
}
13291329
}
13301330
let ok = order.insert(cur);

0 commit comments

Comments
 (0)