From 810514dd31df69031fd8a8ef18697b1542913cd7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 17 Aug 2020 00:13:52 +0800 Subject: [PATCH] Add get/set/index_get/index_set parameters to rhai_fn. --- codegen/src/function.rs | 30 +++++++++++++++++++++++------- codegen/src/rhai_module.rs | 32 +++++++++++++++++++++++++++----- src/packages/array_basic.rs | 2 +- src/packages/map_basic.rs | 2 +- src/packages/string_more.rs | 2 +- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 4285ece7..36f914d0 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -13,9 +13,15 @@ use std::collections::HashMap; use quote::{quote, quote_spanned}; use syn::{parse::Parse, parse::ParseStream, parse::Parser, spanned::Spanned}; +use super::rhai_module::get_register_name; + #[derive(Debug, Default)] pub(crate) struct ExportedFnParams { pub name: Option, + pub get: Option, + pub set: Option, + pub index_get: bool, + pub index_set: bool, pub return_raw: bool, pub skip: bool, } @@ -76,14 +82,24 @@ impl Parse for ExportedFnParams { } let mut name = None; + let mut get = None; + let mut set = None; + let mut index_get = false; + let mut index_set = false; let mut return_raw = false; let mut skip = false; for (ident, value) in attrs.drain() { match (ident.to_string().as_ref(), value) { ("name", Some(s)) => name = Some(s.value()), - ("name", None) => return Err(syn::Error::new(ident.span(), "requires value")), + ("get", Some(s)) => get = Some(s.value()), + ("set", Some(s)) => set = Some(s.value()), + ("get", None) | ("set", None) | ("name", None) => { + return Err(syn::Error::new(ident.span(), "requires value")) + } + ("index_get", None) => index_get = true, + ("index_set", None) => index_get = true, ("return_raw", None) => return_raw = true, - ("return_raw", Some(s)) => { + ("index_get", Some(s)) | ("index_set", Some(s)) | ("return_raw", Some(s)) => { return Err(syn::Error::new(s.span(), "extraneous value")) } ("skip", None) => skip = true, @@ -99,6 +115,10 @@ impl Parse for ExportedFnParams { Ok(ExportedFnParams { name, + get, + set, + index_get, + index_set, return_raw, skip, ..Default::default() @@ -353,11 +373,7 @@ impl ExportedFn { pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream { let sig_name = self.name().clone(); - let name = if let Some(ref name) = self.params.name { - name.clone() - } else { - self.name().to_string() - }; + let name = get_register_name(self); let arg_count = self.arg_count(); let is_method_call = self.mutable_receiver(); diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 3d238b19..22c22056 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -4,6 +4,32 @@ use crate::function::ExportedFn; pub(crate) type ExportedConst = (String, syn::Expr); +pub(crate) fn get_register_name(function: &ExportedFn) -> String { + pub const FN_IDX_GET: &str = "index$get$"; + pub const FN_IDX_SET: &str = "index$set$"; + + pub fn make_getter(id: &str) -> String { + format!("get${}", id) + } + pub fn make_setter(id: &str) -> String { + format!("set${}", id) + } + + if let Some(ref name) = function.params.name { + name.clone() + } else if let Some(ref name) = function.params.get { + make_getter(name).clone() + } else if let Some(ref name) = function.params.set { + make_setter(name).clone() + } else if function.params.index_get { + FN_IDX_GET.to_string() + } else if function.params.index_set { + FN_IDX_SET.to_string() + } else { + function.name().to_string() + } +} + pub(crate) fn generate_body( fns: &Vec, consts: &Vec, @@ -29,11 +55,7 @@ pub(crate) fn generate_body( &format!("{}_token", function.name().to_string()), function.name().span(), ); - let reg_name = if let Some(ref name) = function.params.name { - name.clone() - } else { - function.name().to_string() - }; + let reg_name = get_register_name(function); let fn_literal = syn::LitStr::new(®_name, proc_macro2::Span::call_site()); let fn_input_types: Vec = function .arg_list() diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index fee7ae4d..c4590b08 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -140,7 +140,7 @@ mod array_functions { #[cfg(not(feature = "no_object"))] #[export_module] mod object_functions { - #[rhai_fn(name = "get$len")] + #[rhai_fn(get = "len")] pub fn len(list: &mut Array) -> INT { array_functions::len(list) } diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index 9c9f080d..12ad00e8 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -24,7 +24,7 @@ mod map_functions { pub fn len(map: &mut Map) -> INT { map.len() as INT } - #[rhai_fn(name = "get$len")] + #[rhai_fn(get = "len")] pub fn len_prop(map: &mut Map) -> INT { len(map) } diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index c9efcffe..e21d039e 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -342,7 +342,7 @@ mod index_functions { #[cfg(not(feature = "no_object"))] #[export_module] mod object_functions { - #[rhai_fn(name = "get$len")] + #[rhai_fn(get = "len")] pub fn len(s: &mut ImmutableString) -> INT { string_functions::len(s) }