Skip to content

Commit

Permalink
appends docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Jul 15, 2024
1 parent 5873f34 commit 7cf278a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
54 changes: 54 additions & 0 deletions crates/byondapi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ pub fn bind(attr: TokenStream, item: TokenStream) -> TokenStream {

let args = &input.sig.inputs;

let all_docs = input
.attrs
.iter()
.filter(|attr| matches!(attr.style, syn::AttrStyle::Outer))
.filter_map(|attr| match &attr.meta {
syn::Meta::NameValue(nameval) => {
let ident = nameval.path.get_ident()?;
if ident.to_string() == "doc".to_string() {
match &nameval.value {
syn::Expr::Lit(literal) => match &literal.lit {
syn::Lit::Str(docstring) => {
Some(format!("///{}\n", docstring.value(),))
}
_ => None,
},
_ => None,
}
} else {
None
}
}
_ => None,
})
.collect::<String>();

//Check for returns
let func_return = match &input.sig.output {
syn::ReturnType::Default => {
Expand Down Expand Up @@ -103,6 +128,7 @@ pub fn bind(attr: TokenStream, item: TokenStream) -> TokenStream {
proc_path: #p,
func_name: #func_name_ffi_disp,
func_arguments: #arg_names_disp,
docs: #all_docs,
is_variadic: false,
}
});
Expand All @@ -125,6 +151,7 @@ pub fn bind(attr: TokenStream, item: TokenStream) -> TokenStream {
proc_path: #func_name_disp,
func_name: #func_name_ffi_disp,
func_arguments: #arg_names_disp,
docs: #all_docs,
is_variadic: false,
}
});
Expand Down Expand Up @@ -172,6 +199,31 @@ pub fn bind_raw_args(attr: TokenStream, item: TokenStream) -> TokenStream {
let func_name_ffi = Ident::new(&func_name_ffi, func_name.span());
let func_name_ffi_disp = quote!(#func_name_ffi).to_string();

let all_docs = input
.attrs
.iter()
.filter(|attr| matches!(attr.style, syn::AttrStyle::Outer))
.filter_map(|attr| match &attr.meta {
syn::Meta::NameValue(nameval) => {
let ident = nameval.path.get_ident()?;
if ident.to_string() == "doc".to_string() {
match &nameval.value {
syn::Expr::Lit(literal) => match &literal.lit {
syn::Lit::Str(docstring) => {
Some(format!("///{}\n", docstring.value(),))
}
_ => None,
},
_ => None,
}
} else {
None
}
}
_ => None,
})
.collect::<String>();

//Check for returns
let func_return = match &input.sig.output {
syn::ReturnType::Default => {
Expand Down Expand Up @@ -221,6 +273,7 @@ pub fn bind_raw_args(attr: TokenStream, item: TokenStream) -> TokenStream {
proc_path: #p,
func_name: #func_name_ffi_disp,
func_arguments: "",
docs: #all_docs,
is_variadic: true,
}
});
Expand All @@ -243,6 +296,7 @@ pub fn bind_raw_args(attr: TokenStream, item: TokenStream) -> TokenStream {
proc_path: #func_name_disp,
func_name: #func_name_ffi_disp,
func_arguments: "",
docs: #all_docs,
is_variadic: true,
}
});
Expand Down
17 changes: 17 additions & 0 deletions crates/byondapi-rs-test/dm_project/bindings.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,73 @@

#define BYONDAPI_TEST (__byondapi_test || __detect_byondapi_test())

///Tests new
/proc/test_new_obj()
return call_ext(BYONDAPI_TEST, "byond:test_new_obj_ffi")()

///Tests lists read
/proc/test_list_read(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_read_ffi")(list)

///Tests non-assoc lists
/proc/test_non_assoc_list(list)
return call_ext(BYONDAPI_TEST, "byond:test_non_assoc_list_ffi")(list)

///Tests ref
/proc/test_ref(turf)
return call_ext(BYONDAPI_TEST, "byond:test_ref_ffi")(turf)

///Tests lists lookup
/proc/test_list_key_lookup(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_key_lookup_ffi")(list)

///Tests length with strings
/proc/test_length_with_str(object)
return call_ext(BYONDAPI_TEST, "byond:test_length_with_str_ffi")(object)

///Tests block
/proc/test_block()
return call_ext(BYONDAPI_TEST, "byond:test_block_ffi")()

///Tests lists length
/proc/test_length_with_list(list)
return call_ext(BYONDAPI_TEST, "byond:test_length_with_list_ffi")(list)

///Tests lists popping
/proc/test_list_pop(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_pop_ffi")(list)

///Tests lists indexing
/proc/test_list_index(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_index_ffi")(list)

///Tests lists
/proc/test_list_double(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_double_ffi")(list)

///Tests list pushes
/proc/test_list_push(list)
return call_ext(BYONDAPI_TEST, "byond:test_list_push_ffi")(list)

///Tests readwrite vars
/proc/test_readwrite_var(object)
return call_ext(BYONDAPI_TEST, "byond:test_readwrite_var_ffi")(object)

///Tests proccalls
/proc/test_proc_call(object)
return call_ext(BYONDAPI_TEST, "byond:test_proc_call_ffi")(object)

///Tests pointers
/proc/test_ptr(ptr)
return call_ext(BYONDAPI_TEST, "byond:test_ptr_ffi")(ptr)

///Tests raw args binds
/proc/test_args(...)
var/list/args_copy = args.Copy()
args_copy.Insert(1, src)
return call_ext(BYONDAPI_TEST, "byond:test_args_ffi")(arglist(args_copy))

///Tests main lib connection
/proc/test_connection()
return call_ext(BYONDAPI_TEST, "byond:test_connection_ffi")()

19 changes: 19 additions & 0 deletions crates/byondapi-rs-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ fn setup_panic_handler() {
}))
}

///Tests main lib connection
#[byondapi::bind]
fn test_connection() -> Result<ByondValue> {
setup_panic_handler();
Ok(ByondValue::new_num(69.0))
}

///Tests raw args binds
#[byondapi::bind_raw_args]
fn test_args() -> Result<ByondValue> {
setup_panic_handler();
assert_eq!(args.len(), 2);
Ok(args[1])
}

///Tests pointers
#[byondapi::bind]
fn test_ptr(ptr: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -44,11 +47,13 @@ fn test_ptr(ptr: ByondValue) -> Result<ByondValue> {
Ok(ByondValue::null())
}

///Tests proccalls
#[byondapi::bind]
fn test_proc_call(object: ByondValue) -> Result<ByondValue> {
Ok(object.call("get_name", &[])?)
}

///Tests readwrite vars
#[byondapi::bind]
fn test_readwrite_var(object: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -57,6 +62,8 @@ fn test_readwrite_var(object: ByondValue) -> Result<ByondValue> {

Ok(object.read_string("name")?.try_into()?)
}

///Tests list pushes
#[byondapi::bind]
fn test_list_push(mut list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -66,6 +73,7 @@ fn test_list_push(mut list: ByondValue) -> Result<ByondValue> {
Ok(list)
}

///Tests lists
#[byondapi::bind]
fn test_list_double(list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -78,13 +86,15 @@ fn test_list_double(list: ByondValue) -> Result<ByondValue> {
Ok(collection.as_slice().try_into()?)
}

///Tests lists indexing
#[byondapi::bind]
fn test_list_index(list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();

Ok(list.read_list_index(3.0)?)
}

///Tests lists popping
#[byondapi::bind]
fn test_list_pop(mut list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -98,12 +108,14 @@ fn test_list_pop(mut list: ByondValue) -> Result<ByondValue> {
Ok(element.unwrap())
}

///Tests lists length
#[byondapi::bind]
fn test_length_with_list(list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Ok(list.builtin_length()?)
}

///Tests block
#[byondapi::bind]
fn test_block() -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -123,12 +135,15 @@ fn test_block() -> Result<ByondValue> {
Ok((block.len() as f32).into())
}

///Tests length with strings
#[byondapi::bind]
fn test_length_with_str(object: ByondValue) -> Result<ByondValue> {
setup_panic_handler();

Ok(object.builtin_length()?)
}

///Tests lists lookup
#[byondapi::bind]
fn test_list_key_lookup(mut list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand Down Expand Up @@ -165,6 +180,7 @@ fn test_list_key_lookup(mut list: ByondValue) -> Result<ByondValue> {
Ok(Default::default())
}

///Tests ref
#[byondapi::bind]
fn test_ref(turf: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -177,6 +193,7 @@ fn test_ref(turf: ByondValue) -> Result<ByondValue> {
))?)
}

///Tests non-assoc lists
#[byondapi::bind]
fn test_non_assoc_list(list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -199,6 +216,7 @@ fn test_non_assoc_list(list: ByondValue) -> Result<ByondValue> {
Ok(Default::default())
}

///Tests lists read
#[byondapi::bind]
fn test_list_read(list: ByondValue) -> Result<ByondValue> {
setup_panic_handler();
Expand All @@ -217,6 +235,7 @@ fn test_list_read(list: ByondValue) -> Result<ByondValue> {
Ok(Default::default())
}

///Tests new
#[byondapi::bind]
fn test_new_obj() -> Result<ByondValue> {
Ok(ByondValue::builtin_new(
Expand Down
6 changes: 4 additions & 2 deletions crates/byondapi-rs/src/binds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub struct Bind {
pub proc_path: &'static str,
pub func_name: &'static str,
pub func_arguments: &'static str,
pub docs: &'static str,
pub is_variadic: bool,
}

Expand Down Expand Up @@ -32,6 +33,7 @@ pub fn generate_bindings(libname: &str) {
.unwrap();
for thing in inventory::iter::<Bind> {
let path = thing.proc_path;
let docs = thing.docs;
let func_name = thing.func_name;
let func_arguments = thing.func_arguments;
let func_arguments_srcless = func_arguments
Expand All @@ -41,7 +43,7 @@ pub fn generate_bindings(libname: &str) {
if thing.is_variadic {
//can't directly modify args, fuck you byond
file.write_fmt(format_args!(
r#"{path}(...)
r#"{docs}{path}(...)
var/list/args_copy = args.Copy()
args_copy.Insert(1, src)
return call_ext({libname_upper}, "byond:{func_name}")(arglist(args_copy))
Expand All @@ -51,7 +53,7 @@ pub fn generate_bindings(libname: &str) {
.unwrap()
} else {
file.write_fmt(format_args!(
r#"{path}({func_arguments_srcless})
r#"{docs}{path}({func_arguments_srcless})
return call_ext({libname_upper}, "byond:{func_name}")({func_arguments})
"#
Expand Down

0 comments on commit 7cf278a

Please sign in to comment.