Skip to content

Commit

Permalink
feat(wasi): add support for wasi:[email protected]
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Jan 22, 2025
1 parent a1e2bfc commit 630e330
Show file tree
Hide file tree
Showing 19 changed files with 865 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ci/vendor-wit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ make_vendor "wasi/src/p2" "
[email protected]
"

make_vendor "wasi/src/p3" "
random@[email protected]
"

make_vendor "wasi-http/src/p2" "
[email protected]
[email protected]
Expand Down
1 change: 1 addition & 0 deletions crates/test-programs/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn build_and_generate_tests() {
s if s.starts_with("http_") => "http",
s if s.starts_with("preview1_") => "preview1",
s if s.starts_with("preview2_") => "preview2",
s if s.starts_with("preview3_") => "preview3",
s if s.starts_with("cli_") => "cli",
s if s.starts_with("api_") => "api",
s if s.starts_with("nn_") => "nn",
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/src/bin/preview2_random.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use test_programs::wasi::random;
use test_programs::wasi::random0_2_3 as random;

fn main() {
let mut bytes = [0_u8; 256];
Expand Down
40 changes: 40 additions & 0 deletions crates/test-programs/src/bin/preview3_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use test_programs::wasi::random0_3_0 as random;

fn main() {
let mut bytes = [0_u8; 256];
getrandom::getrandom(&mut bytes).unwrap();

assert!(bytes.iter().any(|x| *x != 0));

// Acquired random bytes should be of the expected length.
let array = random::random::get_random_bytes(100);
assert_eq!(array.len(), 100);

// It shouldn't take 100+ tries to get a nonzero random integer.
for i in 0.. {
if random::random::get_random_u64() == 0 {
continue;
}
assert!(i < 100);
break;
}

// The `insecure_seed` API should return the same result each time.
let (a1, b1) = random::insecure_seed::insecure_seed();
let (a2, b2) = random::insecure_seed::insecure_seed();
assert_eq!(a1, a2);
assert_eq!(b1, b2);

// Acquired random bytes should be of the expected length.
let array = random::insecure::get_insecure_random_bytes(100);
assert_eq!(array.len(), 100);

// It shouldn't take 100+ tries to get a nonzero random integer.
for i in 0.. {
if random::insecure::get_insecure_random_u64() == 0 {
continue;
}
assert!(i < 100);
break;
}
}
5 changes: 4 additions & 1 deletion crates/test-programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ wit_bindgen::generate!({
include wasi:http/[email protected];
include wasi:config/[email protected];
include wasi:keyvalue/[email protected];
include wasi:random/[email protected];
}
",
path: [
"../wasi/src/p3/wit",
"../wasi-http/src/p2/wit",
"../wasi-config/src/p2/wit",
"../wasi-keyvalue/src/p2/wit",
Expand All @@ -33,7 +36,7 @@ pub mod proxy {
with: {
"wasi:http/[email protected]": crate::wasi::http::types,
"wasi:http/[email protected]": crate::wasi::http::outgoing_handler,
"wasi:random/[email protected]": crate::wasi::random::random,
"wasi:random/[email protected]": crate::wasi::random0_2_3::random,
"wasi:io/[email protected]": crate::wasi::io::error,
"wasi:io/[email protected]": crate::wasi::io::poll,
"wasi:io/[email protected]": crate::wasi::io::streams,
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/src/sockets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::wasi::clocks::monotonic_clock;
use crate::wasi::io::poll::{self, Pollable};
use crate::wasi::io::streams::{InputStream, OutputStream, StreamError};
use crate::wasi::random;
use crate::wasi::random0_2_3 as random;
use crate::wasi::sockets::instance_network;
use crate::wasi::sockets::ip_name_lookup;
use crate::wasi::sockets::network::{
Expand Down
1 change: 1 addition & 0 deletions crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ default = [ "preview1"]
preview1 = [
"dep:wiggle",
]
p3 = []

[[test]]
name = "process_stdin"
Expand Down
2 changes: 2 additions & 0 deletions crates/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ mod filesystem;
mod ip_name_lookup;
mod network;
pub mod p2;
#[cfg(feature = "p3")]
pub mod p3;
pub mod pipe;
mod poll;
#[cfg(feature = "preview1")]
Expand Down
Loading

0 comments on commit 630e330

Please sign in to comment.