Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"crates/worldgen",
"crates/common",
"crates/protocol",
"crates/plugin-host/macros",
"crates/plugin-host",
"crates/server",

Expand Down
2 changes: 1 addition & 1 deletion crates/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ thiserror = "1"
num-traits = "0.2"
serde = { version = "1", features = ["derive"] }
bincode = "1"
vek = "0.12"
vek = "0.14"
2 changes: 1 addition & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ anyhow = "1"
ahash = "0.7"
parking_lot = "0.11"
flume = "0.10"
quill-common = { git = "https://github.com/feather-rs/quill" }
quill-common = { git = "https://github.com/feather-rs/quill", rev = "b6def83" }
2 changes: 1 addition & 1 deletion crates/generated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018"

[dependencies]
vek = "0.12"
vek = "0.14"
parking_lot = "0.11"
num-traits = "0.2"
num-derive = "0.3"
Expand Down
10 changes: 8 additions & 2 deletions crates/plugin-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ authors = ["caelunshun <caelunshun@gmail.com>"]
edition = "2018"

[dependencies]
feather-base = { path = "../base" }
feather-common = { path = "../common" }
feather-ecs = { path = "../ecs" }
feather-plugin-host-macros = { path = "macros" }

quill-common = { git = "https://github.com/feather-rs/quill" }
quill-plugin-format = { git = "https://github.com/feather-rs/quill" }
quill-common = { git = "https://github.com/feather-rs/quill", rev = "b6def83" }
quill-plugin-format = { git = "https://github.com/feather-rs/quill", rev = "b6def83" }
wasmer = { version = "1", default-features = false, features = ["jit"] }
wasmer-wasi = { version = "1", default-features = false }
libloading = "0.7"
tempfile = "3"
anyhow = "1"
bumpalo = "3"
bytemuck = "1"
ahash = "0.7"
log = "0.4"
vec-arena = "1"
bincode = "1"
serde = "1"
paste = "1"

[features]
llvm = ["wasmer/llvm"]
Expand Down
16 changes: 16 additions & 0 deletions crates/plugin-host/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "feather-plugin-host-macros"
version = "0.1.0"
authors = ["caelunshun <caelunshun@gmail.com>"]
edition = "2018"

[lib]
proc-macro = true

[dependencies]
syn = { version = "1", features = ["full"] }
quote = "1"
proc-macro2 = "1"

[dev-dependencies]
anyhow = "1"
101 changes: 101 additions & 0 deletions crates/plugin-host/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use quote::{format_ident, quote};
use syn::{FnArg, GenericArgument, PathSegment};

/// Annotates a function so that it implements
/// the NativeHostFunction trait.
#[proc_macro_attribute]
pub fn host_function(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input: syn::ItemFn = syn::parse_macro_input!(input);

let ident = input.sig.ident.clone();
let gateway_ident = format_ident!("{}_gateway", input.sig.ident);
let struct_ident = format_ident!("{}_struct", input.sig.ident);

let args = input
.sig
.inputs
.iter()
.map(|arg| match arg {
FnArg::Receiver(_) => panic!("self functions are not supported"),
FnArg::Typed(arg) => arg.clone(),
})
.collect::<Vec<_>>();
let args_idents: Vec<_> = args.iter().map(|arg| arg.pat.clone()).collect();
let args_idents_without_cx: Vec<_> = args_idents.iter().skip(1).cloned().collect();
let args_without_cx: Vec<_> = args.iter().skip(1).cloned().collect();

// Extract the inner return type from anyhow::Result<T>.
let ret = match input.sig.output.clone() {
syn::ReturnType::Default => return_type_panic(),
syn::ReturnType::Type(_, ty) => match *ty {
syn::Type::Path(path) => {
let segments: Vec<PathSegment> = path.path.segments.into_iter().collect();
if segments[0].ident != "anyhow" {
return_type_panic();
}
if segments[1].ident != "Result" {
return_type_panic();
}

let arg = segments[1].arguments.clone();
match arg {
syn::PathArguments::AngleBracketed(inner) => {
match inner.args.first().unwrap() {
GenericArgument::Type(typ) => typ.clone(),
_ => return_type_panic(),
}
}
_ => return_type_panic(),
}
}
_ => return_type_panic(),
},
};

let result = quote! {
#input

extern "C" fn #gateway_ident(#(#args),*) -> #ret {
#ident(#(#args_idents),*).expect("host function panicked")
}

#[allow(non_camel_case_types)]
pub struct #struct_ident;

impl crate::host_function::NativeHostFunction for #struct_ident {
fn to_function_pointer(self) -> usize {
// Safety: see the Nomicon: https://rust-lang.github.io/unsafe-code-guidelines/layout/function-pointers.html#representation
// For all targets that Feather compiles to, function pointers
// have the same layout as a usize.

unsafe {
std::mem::transmute(#gateway_ident as *const ())
}
}
}

impl crate::host_function::WasmHostFunction for #struct_ident {
fn to_wasm_function(self, store: &wasmer::Store, env: crate::env::PluginEnv) -> wasmer::Function {
wasmer::Function::new_native_with_env(store, env, |env: &crate::env::PluginEnv, #(#args_without_cx),*| {
let result: anyhow::Result<_> = #ident(&env.context, #(#args_idents_without_cx),*);
match result {
Ok(ret) => ret,
Err(e) => {
unsafe {
wasmer::raise_user_trap(e.into())
}
}
}
})
}
}
};
result.into()
}

fn return_type_panic() -> ! {
panic!("host functions must return an anyhow::Result<T>")
}
Loading