wasmedge_process_interface

Crates.iowasmedge_process_interface
lib.rswasmedge_process_interface
version0.2.1
sourcesrc
created_at2021-05-19 07:43:08.410724
updated_at2021-10-19 11:18:49.425145
descriptionA Rust library that provides Rust to WebAssembly developers with syntax for running commands functionality
homepage
repositoryhttps://github.com/second-state/wasmedge_process_interface
max_upload_size
id399431
size27,145
Yi-Ying He (q82419)

documentation

README

WasmEdge Process Interface

A Rust library that provides Rust to WebAssembly developers with syntax for running commands functionality when their Wasm is being executed on WasmEdge (formerly SSVM).

From a high-level overview here, we are essentially building a process interface that will allow the native operating system (which WasmEdge is running on) to play a part in the runtime execution. Specifically, play a part in executing commands with arguments and environment values as part of Wasm execution.

How to use this library

Rust dependency

Developers will add the wasmedge_process_interface crate as a dependency to their Rust -> Wasm applications. For example, add the following line to the application's Cargo.toml file.

[dependencies]
wasmedge_process_interface = "^0.2.1"

Developers will bring the Command modules of wasmedge_process_interface into scope within their Rust -> Wasm application's code. For example, adding the following code to the top of their main.rs file.

use wasmedge_process_interface::Command;

Execute commands with program name

Developers can then use syntax, such as the following, to execute commands such as using std::process::Command. After compilation, the output target Wasm file will contain imports of host functions about running external commands.

Create a Command object

let mut cmd = Command::new("ls");

Append arguments

let mut cmd = Command::new("ls");
cmd.arg("-al");

Or the following:

let cmd = Command::new("ls").arg("-al");

Or the following:

let cmd = Command::new("ls").arg("-alF").arg("..");

Or the following:

let cmd = Command::new("ls").args(&["-alF", ".."]);

Append environment variables

let mut cmd = Command::new("printenv").arg("ONE").env("ONE", "1");

Or the following:

use std::collections::HashMap;
let mut cmd = Command::new("rusttest");
let mut hash: HashMap<String, String> = HashMap::new();
hash.insert(String::from("ENV1"), String::from("VALUE1"));
hash.insert(String::from("ENV2"), String::from("VALUE2"));
let mut cmd = Command::new("printenv").arg("ENV1").envs(hash);

Append stdin

let mut cmd = Command::new("python3").stdin("print(\"HELLO PYTHON\")");

Or the following:

// Consider about the `\n` charactor in stdin strings.
let mut cmd = Command::new("python3").stdin("import time\n").stdin("print(time.time())");

Specify execution timeout

// Timeout values are in milliseconds.
let mut cmd = Command::new("python3")
              .stdin("from time import sleep\n")
              .stdin("print('PYTHON start sleep 2s', flush=True)\n")
              .stdin("sleep(2)\n")
              .stdin("print('PYTHON end sleep 2s', flush=True)\n")
              .timeout(1000);

Execution and get outputs

Please remember to check for the return status of the child process.

let out = Command::new("python3")
          .stdin("from time import sleep\n")
          .stdin("import sys\n")
          .stdin("print('stdout: PYTHON start sleep 2s', flush=True)\n")
          .stdin("print('stderr: PYTHON start sleep 2s', file=sys.stderr, flush=True)\n")
          .stdin("sleep(2)\n")
          .stdin("print('stdout: PYTHON end sleep 2s', flush=True)\n")
          .stdin("print('stderr: PYTHON end sleep 2s', file=sys.stderr, flush=True)\n")
          .timeout(1000)
          .output();

println!(" return code : {}", out.status);
println!(" stdout :");
print!("{}", str::from_utf8(&out.stdout).expect("GET STDOUT ERR"));
println!(" stderr :");
print!("{}", str::from_utf8(&out.stderr).expect("GET STDERR ERR"));

Crates.io

The official crate is available at crates.io.

Commit count: 16

cargo fmt