Enforce signatures on user get/set/index functions

This commit is contained in:
J Henry Waugh
2020-09-12 22:28:41 -05:00
parent 0a2f93439e
commit b1f23ac3ed
26 changed files with 553 additions and 21 deletions

View File

@@ -377,9 +377,11 @@ impl ExportedFn {
}
pub fn set_params(&mut self, mut params: ExportedFnParams) -> syn::Result<()> {
// Do not allow non-returning raw functions.
// Several issues are checked here to avoid issues with diagnostics caused by raising them
// later.
//
// 1. Do not allow non-returning raw functions.
//
// This is caught now to avoid issues with diagnostics later.
if params.return_raw
&& mem::discriminant(&self.signature.output)
== mem::discriminant(&syn::ReturnType::Default)
@@ -390,6 +392,58 @@ impl ExportedFn {
));
}
match params.special {
// 2a. Property getters must take only the subject as an argument.
FnSpecialAccess::Property(Property::Get(_)) if self.arg_count() != 1 =>
return Err(syn::Error::new(
self.signature.span(),
"property getter requires exactly 1 argument",
)),
// 2b. Property getters must return a value.
FnSpecialAccess::Property(Property::Get(_)) if self.return_type().is_none() =>
return Err(syn::Error::new(
self.signature.span(),
"property getter must return a value"
)),
// 3a. Property setters must take the subject and a new value as arguments.
FnSpecialAccess::Property(Property::Set(_)) if self.arg_count() != 2 =>
return Err(syn::Error::new(
self.signature.span(),
"property setter requires exactly 2 arguments",
)),
// 3b. Property setters must return nothing.
FnSpecialAccess::Property(Property::Set(_)) if self.return_type().is_some() =>
return Err(syn::Error::new(
self.signature.span(),
"property setter must return no value"
)),
// 4a. Index getters must take the subject and the accessed "index" as arguments.
FnSpecialAccess::Index(Index::Get) if self.arg_count() != 2 =>
return Err(syn::Error::new(
self.signature.span(),
"index getter requires exactly 2 arguments",
)),
// 4b. Index getters must return a value.
FnSpecialAccess::Index(Index::Get) if self.return_type().is_none() =>
return Err(syn::Error::new(
self.signature.span(),
"index getter must return a value"
)),
// 5a. Index setters must take the subject, "index", and new value as arguments.
FnSpecialAccess::Index(Index::Set) if self.arg_count() != 3 =>
return Err(syn::Error::new(
self.signature.span(),
"index setter requires exactly 3 arguments",
)),
// 5b. Index setters must return nothing.
FnSpecialAccess::Index(Index::Set) if self.return_type().is_some() =>
return Err(syn::Error::new(
self.signature.span(),
"index setter must return no value"
)),
_ => {}
}
self.params = params;
Ok(())
}

View File

@@ -1197,23 +1197,25 @@ mod generate_tests {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
#[rhai_fn(set = "squared")]
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
}
};
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m
}
@@ -1223,10 +1225,11 @@ mod generate_tests {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg1 = mem::take(args[1usize]).clone().cast::<u64>();
let arg0: &mut _ = &mut args[0usize].write_lock::<u64>().unwrap();
Ok(Dynamic::from(int_foo(arg0)))
Ok(Dynamic::from(int_foo(arg0, arg1)))
}
fn is_method_call(&self) -> bool { true }
@@ -1235,7 +1238,7 @@ mod generate_tests {
Box::new(int_foo_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<u64>()].into_boxed_slice()
new_vec![TypeId::of::<u64>(), TypeId::of::<u64>()].into_boxed_slice()
}
}
pub fn int_foo_token_callable() -> CallableFunction {
@@ -1256,25 +1259,29 @@ mod generate_tests {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
#[rhai_fn(name = "set_sq", set = "squared")]
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
}
};
let expected_tokens = quote! {
pub mod one_fn {
pub fn int_foo(x: &mut u64) {
*x = (*x) * (*x)
pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("set_sq", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set_sq", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
m
}
@@ -1284,10 +1291,11 @@ mod generate_tests {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg1 = mem::take(args[1usize]).clone().cast::<u64>();
let arg0: &mut _ = &mut args[0usize].write_lock::<u64>().unwrap();
Ok(Dynamic::from(int_foo(arg0)))
Ok(Dynamic::from(int_foo(arg0, arg1)))
}
fn is_method_call(&self) -> bool { true }
@@ -1296,7 +1304,7 @@ mod generate_tests {
Box::new(int_foo_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<u64>()].into_boxed_slice()
new_vec![TypeId::of::<u64>(), TypeId::of::<u64>()].into_boxed_slice()
}
}
pub fn int_foo_token_callable() -> CallableFunction {