6 个版本
0.1.5 | 2024年2月11日 |
---|---|
0.1.4 | 2023年10月2日 |
0.1.3 | 2023年9月3日 |
0.1.2 | 2023年8月30日 |
#837 in WebAssembly
每月 30 次下载
52KB
902 行
modularity
modularity
是一个用于加载和链接 WebAssembly 组件 的基础库。它通过以下功能作为基于 WASM 的插件和修改系统的基石:
- 从任意来源解析 WASM 包的依赖图
- 使用来自其他组件和宿主的导入实例化 WASM 包
- 允许宿主检查和调用包导出
使用方法
下面的示例说明了如何使用此 crate。完整的版本可以在 示例文件夹 中找到。它首先创建一个 PackageResolver
,指定应用程序希望加载的包列表。然后,它反复调用 PackageResolver::resolve
,在解析器报告需要新组件时提供新组件。一旦解析器完成了依赖图的构建,它将产生一个 PackageContextImage
。随后,将图像应用于 PackageContext
,其中所有组件都将被链接和实例化。之后,可以通过上下文访问包导出。
// Create the WASM engine and store
let engine = Engine::new(wasmi::Engine::default());
let mut store = Store::new(&engine, ());
// Create a context to hold packages
let mut ctx = PackageContext::default();
// Create a resolver with the list of top-level dependencies
let mut resolver = Some(PackageResolver::new(package_ids), Linker::default());
while let Some(r) = take(&mut resolver) {
match r.resolve() {
Ok(x) => {
// Create a transition to move the context to the new set of packages
// The linking process can be customized here
let transition = PackageContextTransitionBuilder::new(&x, &ctx)
.build(&mut store, &ctx)
.unwrap();
// Apply the transition to the package context
transition.apply(&mut store);
println!("Loaded packages are {:?}", ctx.packages().collect::<Vec<_>>());
}
Err(PackageResolverError::MissingPackages(mut r)) => {
for u in r.unresolved() {
// Gets the component with the specified ID from a source
u.resolve(u.id(), get_package(&u));
}
resolver = Some(r);
}
x => panic!("Error occurred: {x:?}"),
}
}
modularity
依赖于 wasm_component_layer
crate 来创建加载的 WASM 模块。消费者负责从源提供解析的 wasm_component_layer::Component
实例。
依赖项
~14MB
~267K SLoC