1个不稳定版本
0.1.0 | 2021年5月18日 |
---|
#717 in 命令行界面
6KB
66 行
gut-plugin
辅助库,提供宏以创建gut插件。
gut插件系统加载位于gut目录(wasm
)的插件,目录为 $HOME/.gut
。
如何创建gut插件
- 使用cargo创建一个新的库
$ cargo new my_gut_plugin --lib
- 在
Cargo.toml
中添加以下内容
[lib]
name = "gut_myplugin"
crate-type = ["cdylib"]
[dependencies]
gut-plugin = "0.1.0"
- 使用提供的宏导出函数
use std::slice;
use std::str;
// gut_export!([names], [descriptions])
// names: [&str] - the names of the functions to export.
// descriptions: [&str] - the descriptions of the functions to export.
gut_plugin::gut_export!(
["my_plugin"],
["Prints the provided string"]
);
// all exported functions must have this signature:
//
// #[no_mangle]
// fn_name(ptr: i32, len: i32)
//
// this is becuase gut will invoke the function and pass a string if one is provided.
// example:
// $ gut my_plugin "world"
#[no_mangle]
fn my_plugin(ptr: i32, len: i32) {
let slice = unsafe { slice::from_raw_parts(ptr as _, len as _) };
let string_from_host = str::from_utf8(&slice).unwrap();
println!("Hello {}!", string_from_host)
}
- 构建
# you may need to add wasm32-wasi target to build wasm
$ rustup target add wasm32-wasi
# build
$ cargo build --target wasm32-wasi --release
# move to gut directory
$ cp target/wasm32-wasi/release/gut_myplugin.wasm $HOME/.gut/
依赖
~1.7–2.4MB
~53K SLoC