Skip to content

Commit

Permalink
Support the wasm32-wasip2 target
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Oct 21, 2024
1 parent d8485ef commit 0b4fa41
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 89 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ on:
- 'release-**'
pull_request:
jobs:
ci:
name: Lint and test
lint:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@1.79.0
uses: dtolnay/rust-toolchain@1.82.0
with:
targets: wasm32-wasip1
targets: wasm32-wasip2
components: clippy, rustfmt
- name: Re-vendor WIT
run: |
Expand All @@ -25,5 +24,24 @@ jobs:
run: cargo fmt --all -- --check
- name: cargo clippy
run: cargo clippy --all-targets --all-features -- -D warnings
test:
needs: lint
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix:
include:
- rust: "1.81"
targets: "wasm32-wasip1"
- rust: "1.82"
targets: "wasm32-wasip2"
name: Test on target ${{ matrix.targets }}
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.targets }}
- name: cargo test
run: cargo test
5 changes: 3 additions & 2 deletions test-programs/artifacts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false
[build-dependencies]
anyhow.workspace = true
cargo_metadata = "0.18.1"
wit-component = "0.208.1"
wit-component = "0.219.1"
heck = "0.5.0"
wasi-preview1-component-adapter-provider = "23.0.1"
wasi-preview1-component-adapter-provider = "25.0.1"
version_check = "0.9.5"
37 changes: 25 additions & 12 deletions test-programs/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ fn main() -> Result<()> {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

println!("cargo::rerun-if-changed=../src");
println!("cargo::rerun-if-changed=../../waki");

let wasip2 = version_check::is_min_version("1.82.0").unwrap_or(false);
let wasi_target = if wasip2 {
"wasm32-wasip2"
} else {
"wasm32-wasip1"
};

let status = Command::new("cargo")
.arg("build")
.arg("--package=test-programs")
.arg("--target=wasm32-wasip1")
.arg(format!("--target={wasi_target}"))
.env("CARGO_TARGET_DIR", &out_dir)
.env("CARGO_PROFILE_DEV_DEBUG", "1")
.status()?;
Expand All @@ -37,21 +45,26 @@ fn main() -> Result<()> {
for target in targets {
let camel = target.to_shouty_snake_case();
let wasm = out_dir
.join("wasm32-wasip1")
.join(wasi_target)
.join("debug")
.join(format!("{target}.wasm"));

let adapter = match target.as_str() {
s if s.starts_with("client_") => {
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER
}
s if s.starts_with("server_") => {
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER
}
other => panic!("unknown type {other}"),
let path = if wasip2 {
wasm
} else {
compile_component(
&wasm,
match target.as_str() {
s if s.starts_with("client_") => {
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER
}
s if s.starts_with("server_") => {
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER
}
other => panic!("unknown type {other}"),
},
)?
};

let path = compile_component(&wasm, adapter)?;
generated_code += &format!("pub const {camel}_COMPONENT: &str = {path:?};\n");
}

Expand Down
3 changes: 1 addition & 2 deletions waki/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ waki-macros.workspace = true
anyhow.workspace = true
serde.workspace = true
wit-bindgen = "0.34.0"
url = "2.5.2"
form_urlencoded = "1.2.1"
http = "1.1.0"
serde_urlencoded = "0.7.1"
serde_json = { version = "1.0.128", optional = true }
mime = { version = "0.3.17", optional = true }
mime_guess = { version = "2.0.5", optional = true }
Expand Down
24 changes: 13 additions & 11 deletions waki/src/common/request_and_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use anyhow::{anyhow, Error, Result};
use serde::Serialize;
use std::borrow::Borrow;
use std::collections::HashMap;

macro_rules! impl_common_get_methods {
Expand Down Expand Up @@ -68,7 +69,7 @@ macro_rules! impl_common_get_methods {

/// Parse the body as form data.
pub fn form(self) -> Result<HashMap<String, String>> {
Ok(serde_urlencoded::from_bytes(self.body()?.as_ref())?)
Ok(form_urlencoded::parse(self.body()?.as_ref()).into_owned().collect())
}

/// Parse the body as multipart/form-data.
Expand Down Expand Up @@ -223,23 +224,24 @@ macro_rules! impl_common_set_methods {
/// # use waki::ResponseBuilder;
/// # fn run() {
/// # let r = ResponseBuilder::new();
/// r.form(&[("a", "b"), ("c", "d")]);
/// r.form([("a", "b"), ("c", "d")]);
/// # }
/// ```
pub fn form<T: Serialize + ?Sized>(mut self, form: &T) -> Self {
let mut err = None;
pub fn form<K, V, I>(mut self, form: I) -> Self
where
K: AsRef<str>,
V: AsRef<str>,
I: IntoIterator,
I::Item: Borrow<(K, V)>,
{
if let Ok(ref mut inner) = self.inner {
inner.headers.insert(
CONTENT_TYPE,
"application/x-www-form-urlencoded".parse().unwrap(),
);
match serde_urlencoded::to_string(form) {
Ok(data) => inner.body = Body::Bytes(data.into()),
Err(e) => err = Some(e.into()),
}
}
if let Some(e) = err {
self.inner = Err(e);
let mut serializer = form_urlencoded::Serializer::new(String::new());
serializer.extend_pairs(form);
inner.body = Body::Bytes(serializer.finish().into())
}
self
}
Expand Down
17 changes: 9 additions & 8 deletions waki/src/common/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bindings::wasi::http::types::Scheme;
use std::fmt::{Display, Formatter, Result};

impl From<&str> for Scheme {
fn from(s: &str) -> Self {
Expand All @@ -11,12 +10,14 @@ impl From<&str> for Scheme {
}
}

impl Display for Scheme {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(match self {
Scheme::Http => "http",
Scheme::Https => "https",
Scheme::Other(s) => s,
})
impl TryInto<http::uri::Scheme> for Scheme {
type Error = http::uri::InvalidUri;

fn try_into(self) -> Result<http::uri::Scheme, Self::Error> {
match self {
Scheme::Http => Ok(http::uri::Scheme::HTTP),
Scheme::Https => Ok(http::uri::Scheme::HTTPS),
Scheme::Other(s) => s.as_str().try_into(),
}
}
}
Loading

0 comments on commit 0b4fa41

Please sign in to comment.