25 次重大发布
新 0.26.0 | 2024 年 8 月 22 日 |
---|---|
0.25.0 | 2024 年 7 月 16 日 |
0.24.0 | 2024 年 7 月 8 日 |
0.18.1 | 2024 年 3 月 4 日 |
0.1.0 | 2023 年 3 月 23 日 |
#620 in WebAssembly
82,137 个月下载量
用于 18 个 crates (10 直接)
5MB
89K SLoC
wasmer-wasi

此 crate 提供了从 Wasmer 运行时轻松使用 WASI 所需的导入。WebAssembly 系统接口 (WASI,简称 WASI) 是 WebAssembly 的一个模块化系统接口。WASI 正在 WebAssembly 小组中标准化。
非常简洁地,从用户的角度来看,WASI 是一组在特定 命名空间(根据 WASI 版本而变化)下的 WebAssembly 模块 导入。针对 wasm32-wasi
目标编译的程序将能够支持标准 I/O、文件 I/O、文件系统操作、内存管理、时间、字符串、环境变量、程序启动等。
Wasmer WASI 是为了实现完全沙箱化而创建的。我们能够实现这一点,归功于我们的虚拟文件系统实现 (wasmer-vfs
),并且只允许安全的系统调用返回到主机。
注意:如果您遇到任何沙箱问题,请在该仓库中提出问题 https://github.com/wasmerio/wasmer。
此 crate 提供了通过我们的 ImportObject
API 创建导入以轻松从 Wasmer 运行时使用 WASI 所需的 API。
支持的 WASI 版本
WASI 版本 | 支持 |
---|---|
wasi_unstable |
✅ |
wasi_snapshot_preview1 |
✅ |
特殊的 Latest
版本指向 wasi_snapshot_preview1
。
了解更多关于 WASI 版本过程(临时、快照、旧)的信息。
用法
让我们考虑以下 hello.rs
Rust 程序
fn main() {
println!("Hello, {:?}", std::env::args().nth(1));
}
然后,让我们将其编译为支持 WASI 的 WebAssembly 模块
$ rustc --target wasm32-wasi hello.rs
最后,让我们使用 wasmer
CLI 来执行它。
$ wasmer run hello.wasm Gordon
Hello, Some("Gordon")
…还可以使用 wasmer
和 wasmer-wasi
库以编程方式执行。
use wasmer::{Store, Module, Instance};
use wasmer_wasix::WasiState;
let mut store = Store::default();
let module = Module::from_file(&store, "hello.wasm")?;
// Create the `WasiEnv`.
let wasi_env = WasiState::builder("command-name")
.args(&["Gordon"])
.finalize()?;
// Generate an `ImportObject`.
let mut wasi_thread = wasi_env.new_thread();
let import_object = wasi_thread.import_object(&module)?;
// Let's instantiate the module with the imports.
let instance = Instance::new(&module, &import_object)?;
// Let's call the `_start` function, which is our `main` function in Rust.
let start = instance.exports.get_function("_start")?;
start.call(&[])?;
查看使用 WASI 的完整工作示例:完整示例。
更多资源
这个库是关于 Wasmer 运行时内部 WASI 实现的。它包含有关实现本身的文档。但是,如果您想了解更多关于 WASI 的信息,以下是一些可能有帮助的链接列表
wasi-libc
,基于 WASI 系统调用的 WebAssembly 程序的 WASI libc 实现。它提供了大量的 POSIX 兼容的 C API,包括对标准 I/O、文件 I/O、文件系统操作、内存管理、时间、字符串、环境变量、程序启动以及许多其他 API 的支持。wasi-sdk
,基于wasi-libc
的具有 WASI 功能的 WebAssembly C/C++ 工具链。- WASI API 文档,
- WASI C API 头文件,
- WASI 应用程序二进制接口 (ABI),例如,我们可以了解
_start
和_initialize
(对于 reactors)。
依赖项
~28–47MB
~826K SLoC