Skip to content

Commit 0b05cf5

Browse files
stlankesvalopok
authored andcommitted
extend preview1 support
- support sleep support - support to determine the environment variables and the process arguments - remove the integration of a demo wasm module - avoid building unneeded wasm file
1 parent 1845b7b commit 0b05cf5

File tree

5 files changed

+193
-81
lines changed

5 files changed

+193
-81
lines changed

examples/hermit-wasm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ name = "hermit_wasm"
1515

1616
[features]
1717
default = []
18-
ci = []
1918

2019
[dependencies]
2120
anyhow = "1.0"

examples/hermit-wasm/build.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/hermit-wasm/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(thread_local)]
22
#![feature(maybe_uninit_slice)]
3-
#![feature(duration_millis_float)]
43

4+
use std::ffi::OsString;
55
use std::time::Instant;
66

77
use anyhow::{Context, Result};
@@ -17,7 +17,11 @@ mod capi;
1717
#[cfg(target_os = "hermit")]
1818
mod preview1;
1919

20-
pub fn run_preview1(module_bytes: &[u8], config: &wasmtime::Config) -> Result<()> {
20+
pub fn run_preview1(
21+
module_bytes: &[u8],
22+
config: &wasmtime::Config,
23+
#[allow(unused_variables)] module_and_args: &'static [OsString],
24+
) -> Result<()> {
2125
let engine = Engine::new(config)?;
2226
debug!("Wasmtime engine is configured as followed: {config:?}");
2327

@@ -38,7 +42,7 @@ pub fn run_preview1(module_bytes: &[u8], config: &wasmtime::Config) -> Result<()
3842
{
3943
let mut imports = module.imports();
4044
if imports.any(|i| i.module() == "wasi_snapshot_preview1") {
41-
preview1::init(&mut linker)?;
45+
preview1::init(&mut linker, module_and_args)?;
4246
}
4347
}
4448

examples/hermit-wasm/src/main.rs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![allow(dependency_on_unit_never_type_fallback)]
2-
1+
use std::ffi::OsString;
32
use std::fs::File;
43
use std::io::{Read, Write};
4+
use std::sync::LazyLock;
55

66
use anyhow::Result;
77
use chrono::Local;
@@ -14,17 +14,16 @@ use log::{LevelFilter, info};
1414

1515
#[derive(Parser, Debug)]
1616
#[command(author, version, about, long_about = None)]
17+
#[command(name = "hermit-wasm")]
1718
#[command(next_line_help = true)]
1819
pub struct Config {
19-
/// File name of the WASM module
20-
#[arg(short, long, value_name = "FILE")]
21-
fname: Option<String>,
22-
23-
/// Defines the usage of the WebAssembly threads proposal for compilation
24-
#[arg(short, long, default_value_t = false)]
25-
threads: bool,
20+
/// The WebAssembly module to run and arguments to pass to it.
21+
#[arg(value_name = "WASM")]
22+
pub module_and_args: Vec<OsString>,
2623
}
2724

25+
static CONFIG: LazyLock<Config> = LazyLock::new(Config::parse);
26+
2827
pub fn main() -> Result<()> {
2928
Builder::new()
3029
.filter_level(LevelFilter::Info)
@@ -41,34 +40,16 @@ pub fn main() -> Result<()> {
4140
})
4241
.init();
4342

44-
let args = Config::parse();
45-
4643
// First step is to create the Wasm execution engine with some config.
47-
// In this example we are using the default configuration.
48-
let mut config = wasmtime::Config::new();
49-
config.wasm_threads(args.threads);
50-
51-
for argument in std::env::args() {
52-
println!("{argument}");
53-
}
54-
55-
if let Some(fname) = args.fname {
56-
info!("Start Hermit-WASM!");
57-
58-
let mut buffer = Vec::new();
59-
let mut f = File::open(fname)?;
44+
// Currently, we are using the default configuration.
45+
let config = wasmtime::Config::new();
6046

61-
f.read_to_end(&mut buffer)?;
47+
info!("Start Hermit-WASM!");
6248

63-
run_preview1(buffer.as_slice(), &config)
64-
} else {
65-
info!("Start simple demo application in Hermit-WASM!");
49+
let mut buffer = Vec::new();
50+
let mut f = File::open(CONFIG.module_and_args[0].clone()).expect("Unable to open wasm module");
6651

67-
#[cfg(not(feature = "ci"))]
68-
let module_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/wasm-test.wasm"));
69-
#[cfg(feature = "ci")]
70-
let module_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/hello_world.wasm"));
52+
f.read_to_end(&mut buffer)?;
7153

72-
run_preview1(module_bytes, &config)
73-
}
54+
run_preview1(buffer.as_slice(), &config, &CONFIG.module_and_args)
7455
}

0 commit comments

Comments
 (0)