-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add export macros This adds two macros that wrap wit_bindgen::generate! with appropriate path, world, exports, and with options: - `wasi::cli::run::export!` for `wasi:cli/run` - `wasi::http::incoming_handler::export!` for `wasi:http/incoming-handler` * Regenerate bindings with wit-bindgen 0.19.2 * Rename and test examples * Update docs: wasm32-wasi-preview{1,2} to wasm32-wasip{1,2} * Add Export Macros section to root docs * Add note about wasm32-wasi command compat * ci: Add wasm32-unknown-unknown target
- Loading branch information
Showing
7 changed files
with
556 additions
and
735 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
fn main() { | ||
#[cfg(feature = "macros")] | ||
generate_macros(); | ||
} | ||
|
||
#[cfg(feature = "macros")] | ||
fn generate_macros() { | ||
use std::path::Path; | ||
|
||
let crate_root = std::env::var_os("CARGO_MANIFEST_DIR").unwrap(); | ||
let wit_path = Path::new(&crate_root) | ||
.join("wit") | ||
.to_str() | ||
.expect("project path must be valid UTF-8") | ||
.to_string(); | ||
|
||
fn write_macro(filename: &str, contents: impl ToString) { | ||
let out_dir = std::env::var_os("OUT_DIR").unwrap(); | ||
let dest_path = Path::new(&out_dir).join(filename); | ||
std::fs::write(dest_path, contents.to_string()).unwrap(); | ||
} | ||
|
||
write_macro( | ||
"cli_run_export.rs", | ||
quote::quote! { | ||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! cli_run_export { | ||
($export_impl:path) => { | ||
::wasi::macros::wit_bindgen::generate!({ | ||
path: #wit_path, | ||
world: "wasi:cli/command", | ||
exports: { | ||
"wasi:cli/run": $export_impl, | ||
}, | ||
with: { | ||
"wasi:cli/environment@0.2.0": ::wasi::cli::environment, | ||
"wasi:cli/exit@0.2.0": ::wasi::cli::exit, | ||
"wasi:cli/stderr@0.2.0": ::wasi::cli::stderr, | ||
"wasi:cli/stdin@0.2.0": ::wasi::cli::stdin, | ||
"wasi:cli/stdout@0.2.0": ::wasi::cli::stdout, | ||
"wasi:cli/terminal-input@0.2.0": ::wasi::cli::terminal_input, | ||
"wasi:cli/terminal-output@0.2.0": ::wasi::cli::terminal_output, | ||
"wasi:cli/terminal-stderr@0.2.0": ::wasi::cli::terminal_stderr, | ||
"wasi:cli/terminal-stdin@0.2.0": ::wasi::cli::terminal_stdin, | ||
"wasi:cli/terminal-stdout@0.2.0": ::wasi::cli::terminal_stdout, | ||
"wasi:clocks/monotonic-clock@0.2.0": ::wasi::clocks::monotonic_clock, | ||
"wasi:clocks/wall-clock@0.2.0": ::wasi::clocks::wall_clock, | ||
"wasi:filesystem/preopens@0.2.0": ::wasi::filesystem::preopens, | ||
"wasi:filesystem/types@0.2.0": ::wasi::filesystem::types, | ||
"wasi:io/error@0.2.0": ::wasi::io::error, | ||
"wasi:io/poll@0.2.0": ::wasi::io::poll, | ||
"wasi:io/streams@0.2.0": ::wasi::io::streams, | ||
"wasi:random/insecure-seed@0.2.0": ::wasi::random::insecure_seed, | ||
"wasi:random/insecure@0.2.0": ::wasi::random::insecure, | ||
"wasi:random/random@0.2.0": ::wasi::random::random, | ||
"wasi:sockets/instance-network@0.2.0": ::wasi::sockets::instance_network, | ||
"wasi:sockets/ip-name-lookup@0.2.0": ::wasi::sockets::ip_name_lookup, | ||
"wasi:sockets/network@0.2.0": ::wasi::sockets::network, | ||
"wasi:sockets/tcp-create-socket@0.2.0": ::wasi::sockets::tcp_create_socket, | ||
"wasi:sockets/tcp@0.2.0": ::wasi::sockets::tcp, | ||
"wasi:sockets/udp-create-socket@0.2.0": ::wasi::sockets::udp_create_socket, | ||
"wasi:sockets/udp@0.2.0": ::wasi::sockets::udp, | ||
}, | ||
runtime_path: "::wasi::macros::wit_bindgen::rt", | ||
}); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
write_macro( | ||
"http_incoming_handler_export.rs", | ||
quote::quote! { | ||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! http_incoming_handler_export { | ||
($export_impl:path) => { | ||
::wasi::macros::wit_bindgen::generate!({ | ||
path: #wit_path, | ||
world: "wasi:http/proxy", | ||
exports: { | ||
"wasi:http/incoming-handler": $export_impl, | ||
}, | ||
with: { | ||
"wasi:cli/stderr@0.2.0": ::wasi::cli::stderr, | ||
"wasi:cli/stdin@0.2.0": ::wasi::cli::stdin, | ||
"wasi:cli/stdout@0.2.0": ::wasi::cli::stdout, | ||
"wasi:clocks/monotonic-clock@0.2.0": ::wasi::clocks::monotonic_clock, | ||
"wasi:clocks/wall-clock@0.2.0": ::wasi::clocks::wall_clock, | ||
"wasi:http/outgoing-handler@0.2.0": ::wasi::http::outgoing_handler, | ||
"wasi:http/types@0.2.0": ::wasi::http::types, | ||
"wasi:io/error@0.2.0": ::wasi::io::error, | ||
"wasi:io/poll@0.2.0": ::wasi::io::poll, | ||
"wasi:io/streams@0.2.0": ::wasi::io::streams, | ||
"wasi:random/random@0.2.0": ::wasi::random::random, | ||
}, | ||
runtime_path: "::wasi::macros::wit_bindgen::rt", | ||
}); | ||
} | ||
} | ||
}, | ||
); | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
wasi::cli::run::export!(Example); | ||
|
||
struct Example; | ||
|
||
impl exports::wasi::cli::run::Guest for Example { | ||
fn run() -> Result<(), ()> { | ||
let stdout = wasi::cli::stdout::get_stdout(); | ||
stdout.blocking_write_and_flush(b"Hello, WASI!").unwrap(); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use wasi::http::types::{ | ||
Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam, | ||
}; | ||
|
||
wasi::http::incoming_handler::export!(Example); | ||
|
||
struct Example; | ||
|
||
impl exports::wasi::http::incoming_handler::Guest for Example { | ||
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) { | ||
let resp = OutgoingResponse::new(Fields::new()); | ||
let body = resp.body().unwrap(); | ||
|
||
ResponseOutparam::set(response_out, Ok(resp)); | ||
|
||
let out = body.write().unwrap(); | ||
out.blocking_write_and_flush(b"Hello, WASI!").unwrap(); | ||
drop(out); | ||
|
||
OutgoingBody::finish(body, None).unwrap(); | ||
} | ||
} |
Oops, something went wrong.