mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-07 20:39:04 +03:00
Add build.rs to rebuild plugins, and a test plugin
This commit is contained in:
parent
5b40734f80
commit
8aef8ab259
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3717,6 +3717,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bincode",
|
"bincode",
|
||||||
|
"pollster",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"wasi-common",
|
"wasi-common",
|
||||||
|
@ -11,3 +11,4 @@ anyhow = { version = "1.0", features = ["std"] }
|
|||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
bincode = "1.3"
|
bincode = "1.3"
|
||||||
|
pollster = "0.2.5"
|
47
crates/plugin_runtime/build.rs
Normal file
47
crates/plugin_runtime/build.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let base = Path::new("../../plugins");
|
||||||
|
|
||||||
|
// println!("cargo:rerun-if-changed=../../plugins/*");
|
||||||
|
println!("cargo:warning=Rebuilding plugins...");
|
||||||
|
|
||||||
|
let _ = std::fs::remove_dir_all(base.join("bin"));
|
||||||
|
let _ =
|
||||||
|
std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
|
||||||
|
|
||||||
|
std::process::Command::new("cargo")
|
||||||
|
.args([
|
||||||
|
"build",
|
||||||
|
"--release",
|
||||||
|
"--target",
|
||||||
|
"wasm32-wasi",
|
||||||
|
"--manifest-path",
|
||||||
|
base.join("Cargo.toml").to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.status()
|
||||||
|
.expect("Could not build plugins");
|
||||||
|
|
||||||
|
let binaries = std::fs::read_dir(base.join("target/wasm32-wasi/release"))
|
||||||
|
.expect("Could not find compiled plugins in target");
|
||||||
|
println!("cargo:warning={:?}", binaries);
|
||||||
|
|
||||||
|
for file in binaries {
|
||||||
|
let is_wasm = || {
|
||||||
|
let path = file.ok()?.path();
|
||||||
|
if path.extension()? == "wasm" {
|
||||||
|
Some(path)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(path) = is_wasm() {
|
||||||
|
std::fs::copy(&path, base.join("bin").join(path.file_name().unwrap()))
|
||||||
|
.expect("Could not copy compiled plugin to bin");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: create .wat versions
|
||||||
|
// TODO: optimize with wasm-opt
|
||||||
|
}
|
@ -1,14 +1,56 @@
|
|||||||
pub mod wasi;
|
pub mod wasi;
|
||||||
|
use pollster::FutureExt as _;
|
||||||
pub use wasi::*;
|
pub use wasi::*;
|
||||||
|
|
||||||
// #[cfg(test)]
|
#[cfg(test)]
|
||||||
// mod tests {
|
mod tests {
|
||||||
// use super::*;
|
use super::*;
|
||||||
|
|
||||||
// pub fn init_wasi() {
|
#[test]
|
||||||
// let plugin = WasiPluginBuilder::new().init(todo!()).unwrap();
|
pub fn test_plugin() {
|
||||||
// let handle: WasiFn<u32, String> = plugin.function("hello").unwrap();
|
pub struct TestPlugin {
|
||||||
// let result = plugin.call(handle, 27).unwrap();
|
noop: WasiFn<(), ()>,
|
||||||
// assert_eq!(result, "world 27");
|
constant: WasiFn<(), u32>,
|
||||||
// }
|
identity: WasiFn<u32, u32>,
|
||||||
// }
|
add: WasiFn<(u32, u32), u32>,
|
||||||
|
swap: WasiFn<(u32, u32), (u32, u32)>,
|
||||||
|
sort: WasiFn<Vec<u32>, Vec<u32>>,
|
||||||
|
print: WasiFn<String, ()>,
|
||||||
|
// and_back: WasiFn<u32, u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async {
|
||||||
|
let mut runtime = WasiPluginBuilder::new_with_default_ctx()
|
||||||
|
.unwrap()
|
||||||
|
.host_function("mystery_number", |input: u32| input + 7)
|
||||||
|
.unwrap()
|
||||||
|
.init(include_bytes!("../../../plugins/bin/test_plugin.wasm"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let plugin = TestPlugin {
|
||||||
|
noop: runtime.function("noop").unwrap(),
|
||||||
|
constant: runtime.function("constant").unwrap(),
|
||||||
|
identity: runtime.function("identity").unwrap(),
|
||||||
|
add: runtime.function("add").unwrap(),
|
||||||
|
swap: runtime.function("swap").unwrap(),
|
||||||
|
sort: runtime.function("sort").unwrap(),
|
||||||
|
print: runtime.function("print").unwrap(),
|
||||||
|
// and_back: runtime.function("and_back").unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let unsorted = vec![1, 3, 4, 2, 5];
|
||||||
|
let sorted = vec![1, 2, 3, 4, 5];
|
||||||
|
|
||||||
|
assert_eq!(runtime.call(&plugin.noop, ()).await.unwrap(), ());
|
||||||
|
assert_eq!(runtime.call(&plugin.constant, ()).await.unwrap(), 27);
|
||||||
|
assert_eq!(runtime.call(&plugin.identity, 58).await.unwrap(), 58);
|
||||||
|
assert_eq!(runtime.call(&plugin.add, (3, 4)).await.unwrap(), 7);
|
||||||
|
assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
|
||||||
|
assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
|
||||||
|
assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
|
||||||
|
// assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
|
||||||
|
}
|
||||||
|
.block_on()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
7
plugins/Cargo.lock
generated
7
plugins/Cargo.lock
generated
@ -112,6 +112,13 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "test_plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"plugin",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["./json_language"]
|
members = ["./json_language", "./test_plugin"]
|
||||||
|
10
plugins/test_plugin/Cargo.toml
Normal file
10
plugins/test_plugin/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "test_plugin"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
plugin = { path = "../../crates/plugin" }
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib"]
|
44
plugins/test_plugin/src/lib.rs
Normal file
44
plugins/test_plugin/src/lib.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use plugin::prelude::*;
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn noop() {}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn constant() -> u32 {
|
||||||
|
27
|
||||||
|
}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn identity(i: u32) -> u32 {
|
||||||
|
i
|
||||||
|
}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn add(a: u32, b: u32) -> u32 {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn swap(a: u32, b: u32) -> (u32, u32) {
|
||||||
|
(b, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn sort(mut list: Vec<u32>) -> Vec<u32> {
|
||||||
|
list.sort();
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
pub fn print(string: String) {
|
||||||
|
println!("to stdout: {}", string);
|
||||||
|
eprintln!("to stderr: {}", string);
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[import]
|
||||||
|
// fn mystery_number(input: u32) -> u32;
|
||||||
|
|
||||||
|
// #[export]
|
||||||
|
// pub fn and_back(secret: u32) -> u32 {
|
||||||
|
// mystery_number(secret)
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user