Merge branch 'plugins' of https://github.com/schungx/rhai into plugins

This commit is contained in:
Stephen Chung 2020-08-14 13:40:33 +08:00
commit 2495b367e5
4 changed files with 72 additions and 57 deletions

View File

@ -1,5 +1,8 @@
#![allow(unused)] #![allow(unused)]
#[cfg(no_std)]
use core::mem;
use std::collections::HashMap; use std::collections::HashMap;
use quote::{quote, quote_spanned}; use quote::{quote, quote_spanned};
@ -329,7 +332,7 @@ impl ExportedFn {
self.name().span(), self.name().span(),
); );
quote! { quote! {
pub fn #input_types_fn_name() -> Box<[std::any::TypeId]> { pub fn #input_types_fn_name() -> Box<[TypeId]> {
#token_name().input_types() #token_name().input_types()
} }
} }
@ -375,7 +378,7 @@ impl ExportedFn {
); );
input_type_exprs.push( input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!( syn::parse2::<syn::Expr>(quote_spanned!(
arg_type.span()=> std::any::TypeId::of::<#arg_type>() arg_type.span()=> TypeId::of::<#arg_type>()
)) ))
.unwrap(), .unwrap(),
); );
@ -407,7 +410,7 @@ impl ExportedFn {
&syn::Type::Path(ref p) if p.path == str_type_path => { &syn::Type::Path(ref p) if p.path == str_type_path => {
is_str_ref = true; is_str_ref = true;
quote_spanned!(arg_type.span()=> quote_spanned!(arg_type.span()=>
std::mem::take(args[#i]) mem::take(args[#i])
.clone().cast::<ImmutableString>()) .clone().cast::<ImmutableString>())
} }
_ => panic!("internal error: why wasn't this found earlier!?"), _ => panic!("internal error: why wasn't this found earlier!?"),
@ -415,7 +418,7 @@ impl ExportedFn {
_ => { _ => {
is_str_ref = false; is_str_ref = false;
quote_spanned!(arg_type.span()=> quote_spanned!(arg_type.span()=>
std::mem::take(args[#i]).clone().cast::<#arg_type>()) mem::take(args[#i]).clone().cast::<#arg_type>())
} }
}; };
@ -428,14 +431,14 @@ impl ExportedFn {
if !is_str_ref { if !is_str_ref {
input_type_exprs.push( input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!( syn::parse2::<syn::Expr>(quote_spanned!(
arg_type.span()=> std::any::TypeId::of::<#arg_type>() arg_type.span()=> TypeId::of::<#arg_type>()
)) ))
.unwrap(), .unwrap(),
); );
} else { } else {
input_type_exprs.push( input_type_exprs.push(
syn::parse2::<syn::Expr>(quote_spanned!( syn::parse2::<syn::Expr>(quote_spanned!(
arg_type.span()=> std::any::TypeId::of::<ImmutableString>() arg_type.span()=> TypeId::of::<ImmutableString>()
)) ))
.unwrap(), .unwrap(),
); );
@ -489,8 +492,8 @@ impl ExportedFn {
fn is_method_call(&self) -> bool { #is_method_call } fn is_method_call(&self) -> bool { #is_method_call }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(#type_name()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(#type_name()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![#(#input_type_exprs),*].into_boxed_slice() new_vec![#(#input_type_exprs),*].into_boxed_slice()
} }
} }
} }
@ -781,14 +784,14 @@ mod generate_tests {
fn is_method_call(&self) -> bool { false } fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![].into_boxed_slice() new_vec![].into_boxed_slice()
} }
} }
pub fn token_callable() -> CallableFunction { pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token()) CallableFunction::from_plugin(Token())
} }
pub fn token_input_types() -> Box<[std::any::TypeId]> { pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types() Token().input_types()
} }
type EvalBox = Box<EvalAltResult>; type EvalBox = Box<EvalAltResult>;
@ -822,21 +825,21 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>(); let arg0 = mem::take(args[0usize]).clone().cast::<usize>();
Ok(Dynamic::from(do_something(arg0))) Ok(Dynamic::from(do_something(arg0)))
} }
fn is_method_call(&self) -> bool { false } fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<usize>()].into_boxed_slice() new_vec![TypeId::of::<usize>()].into_boxed_slice()
} }
} }
pub fn token_callable() -> CallableFunction { pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token()) CallableFunction::from_plugin(Token())
} }
pub fn token_input_types() -> Box<[std::any::TypeId]> { pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types() Token().input_types()
} }
type EvalBox = Box<EvalAltResult>; type EvalBox = Box<EvalAltResult>;
@ -866,15 +869,15 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>(); let arg0 = mem::take(args[0usize]).clone().cast::<usize>();
Ok(Dynamic::from(do_something(arg0))) Ok(Dynamic::from(do_something(arg0)))
} }
fn is_method_call(&self) -> bool { false } fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(MyType()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(MyType()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<usize>()].into_boxed_slice() new_vec![TypeId::of::<usize>()].into_boxed_slice()
} }
} }
}; };
@ -903,23 +906,23 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>(); let arg0 = mem::take(args[0usize]).clone().cast::<usize>();
let arg1 = std::mem::take(args[1usize]).clone().cast::<usize>(); let arg1 = mem::take(args[1usize]).clone().cast::<usize>();
Ok(Dynamic::from(add_together(arg0, arg1))) Ok(Dynamic::from(add_together(arg0, arg1)))
} }
fn is_method_call(&self) -> bool { false } fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<usize>(), new_vec![TypeId::of::<usize>(),
std::any::TypeId::of::<usize>()].into_boxed_slice() TypeId::of::<usize>()].into_boxed_slice()
} }
} }
pub fn token_callable() -> CallableFunction { pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token()) CallableFunction::from_plugin(Token())
} }
pub fn token_input_types() -> Box<[std::any::TypeId]> { pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types() Token().input_types()
} }
type EvalBox = Box<EvalAltResult>; type EvalBox = Box<EvalAltResult>;
@ -953,7 +956,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg1 = std::mem::take(args[1usize]).clone().cast::<usize>(); let arg1 = mem::take(args[1usize]).clone().cast::<usize>();
let arg0: &mut _ = &mut args[0usize].write_lock::<usize>().unwrap(); let arg0: &mut _ = &mut args[0usize].write_lock::<usize>().unwrap();
Ok(Dynamic::from(increment(arg0, arg1))) Ok(Dynamic::from(increment(arg0, arg1)))
} }
@ -961,15 +964,15 @@ mod generate_tests {
fn is_method_call(&self) -> bool { true } fn is_method_call(&self) -> bool { true }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<usize>(), new_vec![TypeId::of::<usize>(),
std::any::TypeId::of::<usize>()].into_boxed_slice() TypeId::of::<usize>()].into_boxed_slice()
} }
} }
pub fn token_callable() -> CallableFunction { pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token()) CallableFunction::from_plugin(Token())
} }
pub fn token_input_types() -> Box<[std::any::TypeId]> { pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types() Token().input_types()
} }
type EvalBox = Box<EvalAltResult>; type EvalBox = Box<EvalAltResult>;
@ -1004,21 +1007,21 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<ImmutableString>(); let arg0 = mem::take(args[0usize]).clone().cast::<ImmutableString>();
Ok(Dynamic::from(special_print(&arg0))) Ok(Dynamic::from(special_print(&arg0)))
} }
fn is_method_call(&self) -> bool { false } fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false } fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) } fn clone_boxed(&self) -> Box<dyn PluginFunction> { Box::new(Token()) }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<ImmutableString>()].into_boxed_slice() new_vec![TypeId::of::<ImmutableString>()].into_boxed_slice()
} }
} }
pub fn token_callable() -> CallableFunction { pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token()) CallableFunction::from_plugin(Token())
} }
pub fn token_input_types() -> Box<[std::any::TypeId]> { pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types() Token().input_types()
} }
type EvalBox = Box<EvalAltResult>; type EvalBox = Box<EvalAltResult>;

View File

@ -4,6 +4,14 @@ use syn::{parse::Parse, parse::ParseStream};
use crate::function::ExportedFn; use crate::function::ExportedFn;
use crate::rhai_module::ExportedConst; use crate::rhai_module::ExportedConst;
#[cfg(no_std)]
use alloc::vec as new_vec;
#[cfg(not(no_std))]
use std::vec as new_vec;
#[cfg(no_std)]
use core::mem;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Module { pub(crate) struct Module {
mod_all: Option<syn::ItemMod>, mod_all: Option<syn::ItemMod>,
@ -53,8 +61,8 @@ impl Parse for Module {
}) })
.collect(); .collect();
} else { } else {
consts = vec![]; consts = new_vec![];
fns = vec![]; fns = new_vec![];
} }
Ok(Module { Ok(Module {
mod_all: Some(mod_all), mod_all: Some(mod_all),
@ -319,14 +327,14 @@ mod generate_tests {
fn clone_boxed(&self) -> Box<dyn PluginFunction> { fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(get_mystic_number_token()) Box::new(get_mystic_number_token())
} }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![].into_boxed_slice() new_vec![].into_boxed_slice()
} }
} }
pub fn get_mystic_number_token_callable() -> CallableFunction { pub fn get_mystic_number_token_callable() -> CallableFunction {
CallableFunction::from_plugin(get_mystic_number_token()) CallableFunction::from_plugin(get_mystic_number_token())
} }
pub fn get_mystic_number_token_input_types() -> Box<[std::any::TypeId]> { pub fn get_mystic_number_token_input_types() -> Box<[TypeId]> {
get_mystic_number_token().input_types() get_mystic_number_token().input_types()
} }
} }
@ -371,7 +379,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<INT>(); let arg0 = mem::take(args[0usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_one_to(arg0))) Ok(Dynamic::from(add_one_to(arg0)))
} }
@ -380,14 +388,14 @@ mod generate_tests {
fn clone_boxed(&self) -> Box<dyn PluginFunction> { fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(add_one_to_token()) Box::new(add_one_to_token())
} }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<INT>()].into_boxed_slice() new_vec![TypeId::of::<INT>()].into_boxed_slice()
} }
} }
pub fn add_one_to_token_callable() -> CallableFunction { pub fn add_one_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_one_to_token()) CallableFunction::from_plugin(add_one_to_token())
} }
pub fn add_one_to_token_input_types() -> Box<[std::any::TypeId]> { pub fn add_one_to_token_input_types() -> Box<[TypeId]> {
add_one_to_token().input_types() add_one_to_token().input_types()
} }
} }
@ -433,8 +441,8 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<INT>(); let arg0 = mem::take(args[0usize]).clone().cast::<INT>();
let arg1 = std::mem::take(args[1usize]).clone().cast::<INT>(); let arg1 = mem::take(args[1usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_together(arg0, arg1))) Ok(Dynamic::from(add_together(arg0, arg1)))
} }
@ -443,15 +451,15 @@ mod generate_tests {
fn clone_boxed(&self) -> Box<dyn PluginFunction> { fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(add_together_token()) Box::new(add_together_token())
} }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<INT>(), new_vec![TypeId::of::<INT>(),
std::any::TypeId::of::<INT>()].into_boxed_slice() TypeId::of::<INT>()].into_boxed_slice()
} }
} }
pub fn add_together_token_callable() -> CallableFunction { pub fn add_together_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_together_token()) CallableFunction::from_plugin(add_together_token())
} }
pub fn add_together_token_input_types() -> Box<[std::any::TypeId]> { pub fn add_together_token_input_types() -> Box<[TypeId]> {
add_together_token().input_types() add_together_token().input_types()
} }
} }
@ -605,7 +613,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = std::mem::take(args[0usize]).clone().cast::<ImmutableString>(); let arg0 = mem::take(args[0usize]).clone().cast::<ImmutableString>();
Ok(Dynamic::from(print_out_to(&arg0))) Ok(Dynamic::from(print_out_to(&arg0)))
} }
@ -614,14 +622,14 @@ mod generate_tests {
fn clone_boxed(&self) -> Box<dyn PluginFunction> { fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(print_out_to_token()) Box::new(print_out_to_token())
} }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<ImmutableString>()].into_boxed_slice() new_vec![TypeId::of::<ImmutableString>()].into_boxed_slice()
} }
} }
pub fn print_out_to_token_callable() -> CallableFunction { pub fn print_out_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(print_out_to_token()) CallableFunction::from_plugin(print_out_to_token())
} }
pub fn print_out_to_token_input_types() -> Box<[std::any::TypeId]> { pub fn print_out_to_token_input_types() -> Box<[TypeId]> {
print_out_to_token().input_types() print_out_to_token().input_types()
} }
} }
@ -676,14 +684,14 @@ mod generate_tests {
fn clone_boxed(&self) -> Box<dyn PluginFunction> { fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(increment_token()) Box::new(increment_token())
} }
fn input_types(&self) -> Box<[std::any::TypeId]> { fn input_types(&self) -> Box<[TypeId]> {
vec![std::any::TypeId::of::<FLOAT>()].into_boxed_slice() new_vec![TypeId::of::<FLOAT>()].into_boxed_slice()
} }
} }
pub fn increment_token_callable() -> CallableFunction { pub fn increment_token_callable() -> CallableFunction {
CallableFunction::from_plugin(increment_token()) CallableFunction::from_plugin(increment_token())
} }
pub fn increment_token_input_types() -> Box<[std::any::TypeId]> { pub fn increment_token_input_types() -> Box<[TypeId]> {
increment_token().input_types() increment_token().input_types()
} }
} }

View File

@ -1,4 +1,5 @@
use rhai::module_resolvers::*; use rhai::module_resolvers::*;
use rhai::plugin::*;
use rhai::{Array, Engine, EvalAltResult, Module, RegisterFn, FLOAT}; use rhai::{Array, Engine, EvalAltResult, Module, RegisterFn, FLOAT};
pub mod raw_fn { pub mod raw_fn {

View File

@ -1,8 +1,11 @@
//! Module defining plugins in Rhai. Is exported for use by plugin authors. //! Module defining plugins in Rhai. Is exported for use by plugin authors.
use crate::stdlib::{any::TypeId, boxed::Box};
pub use crate::{ pub use crate::{
stdlib::any::TypeId,
stdlib::boxed::Box,
stdlib::vec::Vec,
stdlib::vec as new_vec,
stdlib::mem,
fn_native::CallableFunction, fn_native::CallableFunction,
Dynamic, Dynamic,
Engine, Engine,