mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-07 20:39:04 +03:00
Work on macro binding generation, some cleanup needed, rename runner to plugin
This commit is contained in:
parent
dda6dcb3b8
commit
f6a9558c5c
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
/target
|
||||
**/target
|
||||
/zed.xcworkspace
|
||||
.DS_Store
|
||||
/script/node_modules
|
||||
|
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -3609,6 +3609,15 @@ dependencies = [
|
||||
"xml-rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plugin"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"rust_plugin_macros",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.16.8"
|
||||
@ -4311,6 +4320,17 @@ dependencies = [
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_plugin_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.21"
|
||||
|
9
crates/plugin/Cargo.toml
Normal file
9
crates/plugin/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "plugin"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
bincode = "1.3"
|
||||
rust_plugin_macros = { path = "../plugin_macros" }
|
@ -1,5 +1,3 @@
|
||||
use core::slice;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Buffer {
|
||||
ptr: *const u8,
|
||||
@ -15,19 +13,21 @@ pub extern "C" fn __alloc_buffer(len: usize) -> *const u8 {
|
||||
return buffer.ptr;
|
||||
}
|
||||
|
||||
/// Frees a given buffer, requires the size.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn __free_buffer(ptr: *const u8, len: usize) {
|
||||
let buffer = Buffer { ptr, len };
|
||||
let vec = unsafe { buffer.to_vec() };
|
||||
std::mem::drop(vec);
|
||||
}
|
||||
// /// Frees a given buffer, requires the size.
|
||||
// #[no_mangle]
|
||||
// pub extern "C" fn __free_buffer(ptr: *const u8, len: usize) {
|
||||
// let buffer = Buffer { ptr, len };
|
||||
// let vec = unsafe { buffer.to_vec() };
|
||||
// std::mem::drop(vec);
|
||||
// }
|
||||
|
||||
impl Buffer {
|
||||
#[inline(always)]
|
||||
pub unsafe fn to_vec(&self) -> Vec<u8> {
|
||||
slice::from_raw_parts(self.ptr, self.len).to_vec()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_vec(mut vec: Vec<u8>) -> Buffer {
|
||||
vec.shrink_to(0);
|
||||
let ptr = vec.as_ptr();
|
||||
@ -36,6 +36,7 @@ impl Buffer {
|
||||
Buffer { ptr, len }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn leak_to_heap(self) -> *const Buffer {
|
||||
let boxed = Box::new(self);
|
||||
let ptr = Box::<Buffer>::into_raw(boxed) as *const Buffer;
|
||||
@ -43,21 +44,8 @@ impl Buffer {
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn banana(ptr: *const u8, len: usize) -> *const Buffer {
|
||||
// setup
|
||||
let buffer = Buffer { ptr, len };
|
||||
let data = unsafe { buffer.to_vec() };
|
||||
// operation
|
||||
// let reversed: Vec<u8> = data.into_iter().rev().collect();
|
||||
let number: f64 = bincode::deserialize(&data).unwrap();
|
||||
let new_number = number * 2.0;
|
||||
let new_data = bincode::serialize(&new_number).unwrap();
|
||||
// teardown
|
||||
let new_buffer = unsafe { Buffer::from_vec(new_data) };
|
||||
return new_buffer.leak_to_heap();
|
||||
}
|
||||
|
||||
pub fn main() -> () {
|
||||
()
|
||||
pub mod prelude {
|
||||
pub use super::{Buffer, __alloc_buffer};
|
||||
#[macro_use]
|
||||
pub use plugin_macros::bind;
|
||||
}
|
14
crates/plugin_macros/Cargo.toml
Normal file
14
crates/plugin_macros/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "plugin_macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
serde = "1.0"
|
||||
bincode = "1.3"
|
45
crates/plugin_macros/src/lib.rs
Normal file
45
crates/plugin_macros/src/lib.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use core::panic;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn, VisPublic, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
if !args.is_empty() {
|
||||
panic!("The bind attribute does not take any arguments");
|
||||
}
|
||||
|
||||
let inner_fn = parse_macro_input!(function as ItemFn);
|
||||
if let Visibility::Public(_) = inner_fn.vis {
|
||||
} else {
|
||||
panic!("The bind attribute only works for public functions");
|
||||
}
|
||||
|
||||
let inner_fn_name = format_ident!("{}", inner_fn.sig.ident);
|
||||
let outer_fn_name = format_ident!("__{}", inner_fn_name);
|
||||
|
||||
TokenStream::from(quote! {
|
||||
use serde;
|
||||
|
||||
#[no_mangle]
|
||||
#inner_fn
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const Buffer {
|
||||
// setup
|
||||
let buffer = Buffer { ptr, len };
|
||||
let data = unsafe { buffer.to_vec() };
|
||||
|
||||
// operation
|
||||
let argument = bincode::deserialize(&data).unwrap();
|
||||
let result = #inner_fn_name(argument);
|
||||
let new_data: Result<Vec<u8>, _> = bincode::serialize(&result);
|
||||
let new_data = new_data.unwrap();
|
||||
|
||||
// teardown
|
||||
let new_buffer = unsafe { Buffer::from_vec(new_data) };
|
||||
return new_buffer.leak_to_heap();
|
||||
}
|
||||
})
|
||||
}
|
0
crates/plugin_runtime/heck.txt
Normal file
0
crates/plugin_runtime/heck.txt
Normal file
180
crates/plugin_runtime/plugin/Cargo.lock
generated
Normal file
180
crates/plugin_runtime/plugin/Cargo.lock
generated
Normal file
@ -0,0 +1,180 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3"
|
||||
|
||||
[[package]]
|
||||
name = "cargo_test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"rust_plugin_macros",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_plugin_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.137"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.96"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744"
|
@ -4,4 +4,6 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.3"
|
||||
serde = "1.0"
|
||||
bincode = "1.3"
|
||||
plugin = { path = "../../../plugin" }
|
34
crates/plugin_runtime/plugin/cargo_test/src/main.rs
Normal file
34
crates/plugin_runtime/plugin/cargo_test/src/main.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use core::slice;
|
||||
use plugin::prelude::*;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn banana(ptr: *const u8, len: usize) -> *const Buffer {
|
||||
// setup
|
||||
let buffer = Buffer { ptr, len };
|
||||
let data = unsafe { buffer.to_vec() };
|
||||
// operation
|
||||
// let reversed: Vec<u8> = data.into_iter().rev().collect();
|
||||
let number: f64 = bincode::deserialize(&data).unwrap();
|
||||
let new_number = number * 2.0;
|
||||
let new_data = bincode::serialize(&new_number).unwrap();
|
||||
// teardown
|
||||
let new_buffer = unsafe { Buffer::from_vec(new_data) };
|
||||
return new_buffer.leak_to_heap();
|
||||
}
|
||||
|
||||
pub fn banana2(number: f64) -> f64 {
|
||||
number * 2.0
|
||||
}
|
||||
|
||||
#[bind]
|
||||
pub fn sum_lengths(strings: Vec<String>) -> usize {
|
||||
let mut total = 0;
|
||||
for string in strings {
|
||||
total += string.len();
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
pub fn main() -> () {
|
||||
()
|
||||
}
|
@ -12,7 +12,7 @@ pub struct Wasm<T> {
|
||||
store: Store<T>,
|
||||
instance: Instance,
|
||||
alloc_buffer: TypedFunc<i32, i32>,
|
||||
free_buffer: TypedFunc<(i32, i32), ()>,
|
||||
// free_buffer: TypedFunc<(i32, i32), ()>,
|
||||
}
|
||||
|
||||
pub struct WasmPlugin<T> {
|
||||
@ -50,7 +50,7 @@ impl<S> Runtime for Wasm<S> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
|
||||
let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?;
|
||||
let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
|
||||
// let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
|
||||
|
||||
Ok(Wasm {
|
||||
engine,
|
||||
@ -58,7 +58,7 @@ impl<S> Runtime for Wasm<S> {
|
||||
store,
|
||||
instance,
|
||||
alloc_buffer,
|
||||
free_buffer,
|
||||
// free_buffer,
|
||||
})
|
||||
}
|
||||
|
||||
@ -141,7 +141,8 @@ impl<S> Runtime for Wasm<S> {
|
||||
|
||||
// call the function, passing in the buffer and its length
|
||||
// this should return a pointer to a (ptr, lentgh) pair
|
||||
let result_buffer = fun.call(&mut self.store, (arg_buffer_ptr, arg_buffer_len as i32))?;
|
||||
let arg_buffer = (arg_buffer_ptr, arg_buffer_len as i32);
|
||||
let result_buffer = fun.call(&mut self.store, arg_buffer)?;
|
||||
dbg!(result_buffer);
|
||||
|
||||
// panic!();
|
||||
@ -163,10 +164,13 @@ impl<S> Runtime for Wasm<S> {
|
||||
dbg!(result_buffer_len);
|
||||
|
||||
// read the buffer at this point into a byte array
|
||||
let result = &plugin_memory.data(&mut self.store)[result_buffer_ptr..result_buffer_end];
|
||||
|
||||
// deserialize the byte array into the provided serde type
|
||||
let result = &plugin_memory.data(&mut self.store)[result_buffer_ptr..result_buffer_end];
|
||||
let result = bincode::deserialize(result)?;
|
||||
|
||||
// // deallocate the argument buffer
|
||||
// self.free_buffer.call(&mut self.store, arg_buffer);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
25
crates/runner/plugin/Cargo.lock
generated
25
crates/runner/plugin/Cargo.lock
generated
@ -1,25 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cargo_test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.137"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
|
Loading…
Reference in New Issue
Block a user