Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-07-08 13:50:15 +08:00
24 changed files with 639 additions and 371 deletions

View File

@@ -6,7 +6,7 @@ use crate::error::ParseError;
use crate::fn_call::FuncArgs;
use crate::fn_native::{IteratorFn, SendSync};
use crate::fn_register::RegisterFn;
use crate::module::Module;
use crate::module::{FuncReturn, Module};
use crate::optimize::{optimize_into_ast, OptimizationLevel};
use crate::parser::AST;
use crate::result::EvalAltResult;
@@ -39,30 +39,23 @@ impl Engine {
///
/// This function is very low level. It takes a list of `TypeId`'s indicating the actual types of the parameters.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`.
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// The arguments are guaranteed to be of the correct types matching the `TypeId`'s.
///
/// To get access to a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To get access to a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To get access to the first mutable parameter, use `args.get_mut(0).unwrap()`
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn(
pub fn register_raw_fn<T: Variant + Clone>(
&mut self,
name: &str,
arg_types: &[TypeId],
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module.set_fn_var_args(name, arg_types, func);
self.global_module.set_raw_fn(name, arg_types, func);
}
/// Register a function of no parameters with the `Engine`.
@@ -71,19 +64,12 @@ impl Engine {
///
/// This function is very low level.
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn_0(
pub fn register_raw_fn_0<T: Variant + Clone>(
&mut self,
name: &str,
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module.set_fn_var_args(name, &[], func);
self.global_module.set_raw_fn(name, &[], func);
}
/// Register a function of one parameter with the `Engine`.
@@ -92,30 +78,23 @@ impl Engine {
///
/// This function is very low level.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`.
/// The argument is guaranteed to be of the correct type.
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// which is guaranteed to contain enough arguments of the correct types.
///
/// To get access to a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To get access to a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To get access to the first mutable parameter, use `args.get_mut(0).unwrap()`
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn_1<A: Variant + Clone>(
pub fn register_raw_fn_1<A: Variant + Clone, T: Variant + Clone>(
&mut self,
name: &str,
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module
.set_fn_var_args(name, &[TypeId::of::<A>()], func);
.set_raw_fn(name, &[TypeId::of::<A>()], func);
}
/// Register a function of two parameters with the `Engine`.
@@ -124,30 +103,23 @@ impl Engine {
///
/// This function is very low level.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`.
/// The arguments are guaranteed to be of the correct types.
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// which is guaranteed to contain enough arguments of the correct types.
///
/// To get access to a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To get access to a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To get access to the first mutable parameter, use `args.get_mut(0).unwrap()`
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn_2<A: Variant + Clone, B: Variant + Clone>(
pub fn register_raw_fn_2<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self,
name: &str,
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module
.set_fn_var_args(name, &[TypeId::of::<A>(), TypeId::of::<B>()], func);
.set_raw_fn(name, &[TypeId::of::<A>(), TypeId::of::<B>()], func);
}
/// Register a function of three parameters with the `Engine`.
@@ -156,29 +128,27 @@ impl Engine {
///
/// This function is very low level.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`.
/// The arguments are guaranteed to be of the correct types.
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// which is guaranteed to contain enough arguments of the correct types.
///
/// To get access to a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To get access to a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To get access to the first mutable parameter, use `args.get_mut(0).unwrap()`
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn_3<A: Variant + Clone, B: Variant + Clone, C: Variant + Clone>(
pub fn register_raw_fn_3<
A: Variant + Clone,
B: Variant + Clone,
C: Variant + Clone,
T: Variant + Clone,
>(
&mut self,
name: &str,
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module.set_fn_var_args(
self.global_module.set_raw_fn(
name,
&[TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()],
func,
@@ -191,34 +161,28 @@ impl Engine {
///
/// This function is very low level.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`.
/// The arguments are guaranteed to be of the correct types.
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// which is guaranteed to contain enough arguments of the correct types.
///
/// To get access to a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To get access to a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To get access to the first mutable parameter, use `args.get_mut(0).unwrap()`
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[deprecated(note = "this function is volatile and may change")]
pub fn register_raw_fn_4<
A: Variant + Clone,
B: Variant + Clone,
C: Variant + Clone,
D: Variant + Clone,
T: Variant + Clone,
>(
&mut self,
name: &str,
#[cfg(not(feature = "sync"))] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ 'static,
#[cfg(feature = "sync")] func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send
+ Sync
+ 'static,
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) {
self.global_module.set_fn_var_args(
self.global_module.set_raw_fn(
name,
&[
TypeId::of::<A>(),
@@ -1395,7 +1359,7 @@ impl Engine {
name: &str,
mut this_ptr: Option<&mut Dynamic>,
mut arg_values: impl AsMut<[Dynamic]>,
) -> Result<Dynamic, Box<EvalAltResult>> {
) -> FuncReturn<Dynamic> {
self.call_fn_dynamic_raw(scope, lib, name, &mut this_ptr, arg_values.as_mut())
}
@@ -1415,7 +1379,7 @@ impl Engine {
name: &str,
this_ptr: &mut Option<&mut Dynamic>,
arg_values: &mut [Dynamic],
) -> Result<Dynamic, Box<EvalAltResult>> {
) -> FuncReturn<Dynamic> {
let lib = lib.as_ref();
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
let fn_def =

View File

@@ -399,6 +399,39 @@ fn default_print(s: &str) {
/// Search for a module within an imports stack.
/// Position in `EvalAltResult` is None and must be set afterwards.
fn search_imports<'s>(
mods: &'s Imports,
state: &mut State,
modules: &Box<ModuleRef>,
) -> Result<&'s Module, Box<EvalAltResult>> {
let (root, root_pos) = modules.get(0);
// Qualified - check if the root module is directly indexed
let index = if state.always_search {
None
} else {
modules.index()
};
Ok(if let Some(index) = index {
let offset = mods.len() - index.get();
&mods.get(offset).unwrap().1
} else {
mods.iter()
.rev()
.find(|(n, _)| n == root)
.map(|(_, m)| m)
.ok_or_else(|| {
Box::new(EvalAltResult::ErrorModuleNotFound(
root.to_string(),
*root_pos,
))
})?
})
}
/// Search for a module within an imports stack.
/// Position in `EvalAltResult` is None and must be set afterwards.
fn search_imports_mut<'s>(
mods: &'s mut Imports,
state: &mut State,
modules: &Box<ModuleRef>,
@@ -429,15 +462,49 @@ fn search_imports<'s>(
})
}
/// Search for a variable within the scope
fn search_scope<'s, 'a>(
/// Search for a variable within the scope and imports
fn search_namespace<'s, 'a>(
scope: &'s mut Scope,
mods: &'s mut Imports,
state: &mut State,
this_ptr: &'s mut Option<&mut Dynamic>,
expr: &'a Expr,
) -> Result<(&'s mut Dynamic, &'a str, ScopeEntryType, Position), Box<EvalAltResult>> {
let ((name, pos), modules, hash_var, index) = match expr {
match expr {
Expr::Variable(v) => match v.as_ref() {
// Qualified variable
((name, pos), Some(modules), hash_var, _) => {
let module = search_imports_mut(mods, state, modules)?;
let target = module
.get_qualified_var_mut(*hash_var)
.map_err(|err| match *err {
EvalAltResult::ErrorVariableNotFound(_, _) => {
Box::new(EvalAltResult::ErrorVariableNotFound(
format!("{}{}", modules, name),
*pos,
))
}
_ => err.new_position(*pos),
})?;
// Module variables are constant
Ok((target, name, ScopeEntryType::Constant, *pos))
}
// Normal variable access
_ => search_scope_only(scope, state, this_ptr, expr),
},
_ => unreachable!(),
}
}
/// Search for a variable within the scope
fn search_scope_only<'s, 'a>(
scope: &'s mut Scope,
state: &mut State,
this_ptr: &'s mut Option<&mut Dynamic>,
expr: &'a Expr,
) -> Result<(&'s mut Dynamic, &'a str, ScopeEntryType, Position), Box<EvalAltResult>> {
let ((name, pos), _, _, index) = match expr {
Expr::Variable(v) => v.as_ref(),
_ => unreachable!(),
};
@@ -451,37 +518,21 @@ fn search_scope<'s, 'a>(
}
}
// Check if it is qualified
if let Some(modules) = modules {
let module = search_imports(mods, state, modules)?;
let target = module
.get_qualified_var_mut(*hash_var)
.map_err(|err| match *err {
EvalAltResult::ErrorVariableNotFound(_, _) => Box::new(
EvalAltResult::ErrorVariableNotFound(format!("{}{}", modules, name), *pos),
),
_ => err.new_position(*pos),
})?;
// Check if it is directly indexed
let index = if state.always_search { None } else { *index };
// Module variables are constant
Ok((target, name, ScopeEntryType::Constant, *pos))
let index = if let Some(index) = index {
scope.len() - index.get()
} else {
// Unqualified - check if it is directly indexed
let index = if state.always_search { None } else { *index };
// Find the variable in the scope
scope
.get_index(name)
.ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), *pos)))?
.0
};
let index = if let Some(index) = index {
scope.len() - index.get()
} else {
// Find the variable in the scope
scope
.get_index(name)
.ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), *pos)))?
.0
};
let (val, typ) = scope.get_mut(index);
Ok((val, name, typ, *pos))
}
let (val, typ) = scope.get_mut(index);
Ok((val, name, typ, *pos))
}
impl Engine {
@@ -1276,7 +1327,8 @@ impl Engine {
self.inc_operations(state)
.map_err(|err| err.new_position(*var_pos))?;
let (target, _, typ, pos) = search_scope(scope, mods, state, this_ptr, dot_lhs)?;
let (target, _, typ, pos) =
search_namespace(scope, mods, state, this_ptr, dot_lhs)?;
// Constants cannot be modified
match typ {
@@ -1573,7 +1625,7 @@ impl Engine {
}
}
Expr::Variable(_) => {
let (val, _, _, _) = search_scope(scope, mods, state, this_ptr, expr)?;
let (val, _, _, _) = search_namespace(scope, mods, state, this_ptr, expr)?;
Ok(val.clone())
}
Expr::Property(_) => unreachable!(),
@@ -1587,7 +1639,7 @@ impl Engine {
let mut rhs_val =
self.eval_expr(scope, mods, state, lib, this_ptr, rhs_expr, level)?;
let (lhs_ptr, name, typ, pos) =
search_scope(scope, mods, state, this_ptr, lhs_expr)?;
search_namespace(scope, mods, state, this_ptr, lhs_expr)?;
self.inc_operations(state)
.map_err(|err| err.new_position(pos))?;
@@ -1800,7 +1852,7 @@ impl Engine {
.collect::<Result<_, _>>()?;
let (target, _, _, pos) =
search_scope(scope, mods, state, this_ptr, lhs)?;
search_namespace(scope, mods, state, this_ptr, lhs)?;
self.inc_operations(state)
.map_err(|err| err.new_position(pos))?;
@@ -1836,12 +1888,48 @@ impl Engine {
let ((name, _, pos), modules, hash_script, args_expr, def_val) = x.as_ref();
let modules = modules.as_ref().unwrap();
let mut arg_values = args_expr
.iter()
.map(|expr| self.eval_expr(scope, mods, state, lib, this_ptr, expr, level))
.collect::<Result<StaticVec<_>, _>>()?;
let mut arg_values: StaticVec<Dynamic>;
let mut args: StaticVec<_>;
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
if args_expr.is_empty() {
// No arguments
args = Default::default();
} else {
// See if the first argument is a variable (not module-qualified).
// If so, convert to method-call style in order to leverage potential
// &mut first argument and avoid cloning the value
match args_expr.get(0) {
// func(x, ...) -> x.func(...)
Expr::Variable(x) if x.1.is_none() => {
arg_values = args_expr
.iter()
.skip(1)
.map(|expr| {
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)
})
.collect::<Result<_, _>>()?;
let (target, _, _, pos) =
search_scope_only(scope, state, this_ptr, args_expr.get(0))?;
self.inc_operations(state)
.map_err(|err| err.new_position(pos))?;
args = once(target).chain(arg_values.iter_mut()).collect();
}
// func(..., ...) or func(mod::x, ...)
_ => {
arg_values = args_expr
.iter()
.map(|expr| {
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)
})
.collect::<Result<_, _>>()?;
args = arg_values.iter_mut().collect();
}
}
}
let module = search_imports(mods, state, modules)?;

View File

@@ -3,7 +3,7 @@
use crate::any::{Dynamic, Variant};
use crate::calc_fn_hash;
use crate::engine::{make_getter, make_setter, Engine, Imports, FN_IDX_GET, FN_IDX_SET};
use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, SendSync, Shared};
use crate::fn_native::{CallableFunction as Func, FnCallArgs, IteratorFn, SendSync, Shared};
use crate::parser::{
FnAccess,
FnAccess::{Private, Public},
@@ -49,18 +49,14 @@ pub struct Module {
all_variables: HashMap<u64, Dynamic, StraightHasherBuilder>,
/// External Rust functions.
functions: HashMap<
u64,
(String, FnAccess, StaticVec<TypeId>, CallableFunction),
StraightHasherBuilder,
>,
functions: HashMap<u64, (String, FnAccess, StaticVec<TypeId>, Func), StraightHasherBuilder>,
/// Iterator functions, keyed by the type producing the iterator.
type_iterators: HashMap<TypeId, IteratorFn>,
/// Flattened collection of all external Rust functions, native or scripted,
/// including those in sub-modules.
all_functions: HashMap<u64, CallableFunction, StraightHasherBuilder>,
all_functions: HashMap<u64, Func, StraightHasherBuilder>,
/// Is the module indexed?
indexed: bool,
@@ -346,18 +342,18 @@ impl Module {
/// Set a Rust function into the module, returning a hash key.
///
/// If there is an existing Rust function of the same hash, it is replaced.
pub fn set_fn(
pub(crate) fn set_fn(
&mut self,
name: impl Into<String>,
access: FnAccess,
params: &[TypeId],
func: CallableFunction,
arg_types: &[TypeId],
func: Func,
) -> u64 {
let name = name.into();
let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned());
let hash_fn = calc_fn_hash(empty(), &name, arg_types.len(), arg_types.iter().cloned());
let params = params.into_iter().cloned().collect();
let params = arg_types.into_iter().cloned().collect();
self.functions
.insert(hash_fn, (name, access, params, func.into()));
@@ -367,29 +363,72 @@ impl Module {
hash_fn
}
/// Set a Rust function taking a reference to the scripting `Engine`, plus a list of
/// mutable `Dynamic` references into the module, returning a hash key.
/// A list of `TypeId`'s is taken as the argument types.
/// Set a Rust function taking a reference to the scripting `Engine`, the current set of functions,
/// plus a list of mutable `Dynamic` references into the module, returning a hash key.
///
/// Use this to register a built-in function which must reference settings on the scripting
/// `Engine` (e.g. to prevent growing an array beyond the allowed maximum size).
/// `Engine` (e.g. to prevent growing an array beyond the allowed maximum size), or to call a
/// script-defined function in the current evaluation context.
///
/// If there is a similar existing Rust function, it is replaced.
pub(crate) fn set_fn_var_args<T: Variant + Clone>(
///
/// ## WARNING - Low Level API
///
/// This function is very low level.
///
/// A list of `TypeId`'s is taken as the argument types.
///
/// Arguments are simply passed in as a mutable array of `&mut Dynamic`,
/// which is guaranteed to contain enough arguments of the correct types.
///
/// To access a primary parameter value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()`
///
/// To access a parameter value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`.
///
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
///
/// # Examples
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_raw_fn("double_or_not",
/// // Pass parameter types via a slice with TypeId's
/// &[std::any::TypeId::of::<i64>(), std::any::TypeId::of::<bool>() ],
/// // Fixed closure signature
/// |engine, lib, args| {
/// // 'args' is guaranteed to be the right length and of the correct types
///
/// // Get the second parameter by 'consuming' it
/// let double = std::mem::take(args[1]).cast::<bool>();
/// // Since it is a primary type, it can also be cheaply copied
/// let double = args[1].clone().cast::<bool>();
/// // Get a mutable reference to the first argument.
/// let x = args[0].downcast_mut::<i64>().unwrap();
///
/// let orig = *x;
///
/// if double {
/// *x *= 2; // the first argument can be mutated
/// }
///
/// Ok(orig) // return Result<T, Box<EvalAltResult>>
/// });
///
/// assert!(module.contains_fn(hash));
/// ```
pub fn set_raw_fn<T: Variant + Clone>(
&mut self,
name: impl Into<String>,
args: &[TypeId],
arg_types: &[TypeId],
func: impl Fn(&Engine, &Module, &mut [&mut Dynamic]) -> FuncReturn<T> + SendSync + 'static,
) -> u64 {
let f = move |engine: &Engine, lib: &Module, args: &mut FnCallArgs| {
func(engine, lib, args).map(Dynamic::from)
};
self.set_fn(
name,
Public,
args,
CallableFunction::from_method(Box::new(f)),
)
self.set_fn(name, Public, arg_types, Func::from_method(Box::new(f)))
}
/// Set a Rust function taking no parameters into the module, returning a hash key.
@@ -411,13 +450,8 @@ impl Module {
func: impl Fn() -> FuncReturn<T> + SendSync + 'static,
) -> u64 {
let f = move |_: &Engine, _: &Module, _: &mut FnCallArgs| func().map(Dynamic::from);
let args = [];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_pure(Box::new(f)),
)
let arg_types = [];
self.set_fn(name, Public, &arg_types, Func::from_pure(Box::new(f)))
}
/// Set a Rust function taking one parameter into the module, returning a hash key.
@@ -441,13 +475,8 @@ impl Module {
let f = move |_: &Engine, _: &Module, args: &mut FnCallArgs| {
func(mem::take(args[0]).cast::<A>()).map(Dynamic::from)
};
let args = [TypeId::of::<A>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_pure(Box::new(f)),
)
let arg_types = [TypeId::of::<A>()];
self.set_fn(name, Public, &arg_types, Func::from_pure(Box::new(f)))
}
/// Set a Rust function taking one mutable parameter into the module, returning a hash key.
@@ -471,13 +500,8 @@ impl Module {
let f = move |_: &Engine, _: &Module, args: &mut FnCallArgs| {
func(args[0].downcast_mut::<A>().unwrap()).map(Dynamic::from)
};
let args = [TypeId::of::<A>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_method(Box::new(f)),
)
let arg_types = [TypeId::of::<A>()];
self.set_fn(name, Public, &arg_types, Func::from_method(Box::new(f)))
}
/// Set a Rust getter function taking one mutable parameter, returning a hash key.
@@ -528,13 +552,8 @@ impl Module {
func(a, b).map(Dynamic::from)
};
let args = [TypeId::of::<A>(), TypeId::of::<B>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_pure(Box::new(f)),
)
let arg_types = [TypeId::of::<A>(), TypeId::of::<B>()];
self.set_fn(name, Public, &arg_types, Func::from_pure(Box::new(f)))
}
/// Set a Rust function taking two parameters (the first one mutable) into the module,
@@ -564,13 +583,8 @@ impl Module {
func(a, b).map(Dynamic::from)
};
let args = [TypeId::of::<A>(), TypeId::of::<B>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_method(Box::new(f)),
)
let arg_types = [TypeId::of::<A>(), TypeId::of::<B>()];
self.set_fn(name, Public, &arg_types, Func::from_method(Box::new(f)))
}
/// Set a Rust setter function taking two parameters (the first one mutable) into the module,
@@ -656,13 +670,8 @@ impl Module {
func(a, b, c).map(Dynamic::from)
};
let args = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_pure(Box::new(f)),
)
let arg_types = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
self.set_fn(name, Public, &arg_types, Func::from_pure(Box::new(f)))
}
/// Set a Rust function taking three parameters (the first one mutable) into the module,
@@ -698,13 +707,8 @@ impl Module {
func(a, b, c).map(Dynamic::from)
};
let args = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_method(Box::new(f)),
)
let arg_types = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
self.set_fn(name, Public, &arg_types, Func::from_method(Box::new(f)))
}
/// Set a Rust index setter taking three parameters (the first one mutable) into the module,
@@ -735,12 +739,12 @@ impl Module {
func(a, b, c).map(Dynamic::from)
};
let args = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<A>()];
let arg_types = [TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<A>()];
self.set_fn(
FN_IDX_SET,
Public,
&args,
CallableFunction::from_method(Box::new(f)),
&arg_types,
Func::from_method(Box::new(f)),
)
}
@@ -778,18 +782,13 @@ impl Module {
func(a, b, c, d).map(Dynamic::from)
};
let args = [
let arg_types = [
TypeId::of::<A>(),
TypeId::of::<B>(),
TypeId::of::<C>(),
TypeId::of::<D>(),
];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_pure(Box::new(f)),
)
self.set_fn(name, Public, &arg_types, Func::from_pure(Box::new(f)))
}
/// Set a Rust function taking four parameters (the first one mutable) into the module,
@@ -827,25 +826,20 @@ impl Module {
func(a, b, c, d).map(Dynamic::from)
};
let args = [
let arg_types = [
TypeId::of::<A>(),
TypeId::of::<B>(),
TypeId::of::<C>(),
TypeId::of::<C>(),
];
self.set_fn(
name,
Public,
&args,
CallableFunction::from_method(Box::new(f)),
)
self.set_fn(name, Public, &arg_types, Func::from_method(Box::new(f)))
}
/// Get a Rust function.
///
/// The `u64` hash is calculated by the function `crate::calc_fn_hash`.
/// It is also returned by the `set_fn_XXX` calls.
pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> {
pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&Func> {
self.functions.get(&hash_fn).map(|(_, _, _, v)| v)
}
@@ -855,9 +849,9 @@ impl Module {
/// The `u64` hash is calculated by the function `crate::calc_fn_hash` and must match
/// the hash calculated by `index_all_sub_modules`.
pub(crate) fn get_qualified_fn(
&mut self,
&self,
hash_qualified_fn: u64,
) -> Result<&CallableFunction, Box<EvalAltResult>> {
) -> Result<&Func, Box<EvalAltResult>> {
self.all_functions.get(&hash_qualified_fn).ok_or_else(|| {
Box::new(EvalAltResult::ErrorFunctionNotFound(
String::new(),
@@ -886,9 +880,7 @@ impl Module {
.iter()
.filter(|(_, (_, _, _, v))| match v {
#[cfg(not(feature = "no_function"))]
CallableFunction::Script(ref f) => {
filter(f.access, f.name.as_str(), f.params.len())
}
Func::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()),
_ => true,
})
.map(|(&k, v)| (k, v.clone())),
@@ -906,7 +898,7 @@ impl Module {
#[cfg(not(feature = "no_function"))]
pub(crate) fn retain_functions(&mut self, filter: impl Fn(FnAccess, &str, usize) -> bool) {
self.functions.retain(|_, (_, _, _, v)| match v {
CallableFunction::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()),
Func::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()),
_ => true,
});
@@ -936,7 +928,7 @@ impl Module {
/// Get an iterator to the functions in the module.
pub(crate) fn iter_fn(
&self,
) -> impl Iterator<Item = &(String, FnAccess, StaticVec<TypeId>, CallableFunction)> {
) -> impl Iterator<Item = &(String, FnAccess, StaticVec<TypeId>, Func)> {
self.functions.values()
}
@@ -1003,7 +995,7 @@ impl Module {
module: &'a Module,
qualifiers: &mut Vec<&'a str>,
variables: &mut Vec<(u64, Dynamic)>,
functions: &mut Vec<(u64, CallableFunction)>,
functions: &mut Vec<(u64, Func)>,
) {
for (name, m) in &module.modules {
// Index all the sub-modules first.
@@ -1153,6 +1145,7 @@ pub trait ModuleResolver: SendSync {
/// Re-export module resolvers.
#[cfg(not(feature = "no_module"))]
pub mod resolvers {
pub use super::collection::ModuleResolversCollection;
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
pub use super::file::FileModuleResolver;
@@ -1348,9 +1341,6 @@ mod stat {
/// Module resolution service that serves modules added into it.
///
/// `StaticModuleResolver` is a smart pointer to a `HashMap<String, Module>`.
/// It can simply be treated as `&HashMap<String, Module>`.
///
/// # Examples
///
/// ```
@@ -1444,3 +1434,86 @@ mod stat {
}
}
}
/// Module resolver collection.
#[cfg(not(feature = "no_module"))]
mod collection {
use super::*;
/// Module resolution service that holds a collection of module resolves,
/// to be searched in sequential order.
///
/// # Examples
///
/// ```
/// use rhai::{Engine, Module};
/// use rhai::module_resolvers::{StaticModuleResolver, ModuleResolversCollection};
///
/// let mut collection = ModuleResolversCollection::new();
///
/// let resolver = StaticModuleResolver::new();
/// collection.push(resolver);
///
/// let mut engine = Engine::new();
/// engine.set_module_resolver(Some(collection));
/// ```
#[derive(Default)]
pub struct ModuleResolversCollection(Vec<Box<dyn ModuleResolver>>);
impl ModuleResolversCollection {
/// Create a new `ModuleResolversCollection`.
///
/// # Examples
///
/// ```
/// use rhai::{Engine, Module};
/// use rhai::module_resolvers::{StaticModuleResolver, ModuleResolversCollection};
///
/// let mut collection = ModuleResolversCollection::new();
///
/// let resolver = StaticModuleResolver::new();
/// collection.push(resolver);
///
/// let mut engine = Engine::new();
/// engine.set_module_resolver(Some(collection));
/// ```
pub fn new() -> Self {
Default::default()
}
}
impl ModuleResolversCollection {
/// Add a module keyed by its path.
pub fn push(&mut self, resolver: impl ModuleResolver + 'static) {
self.0.push(Box::new(resolver));
}
/// Get an iterator of all the module resolvers.
pub fn iter(&self) -> impl Iterator<Item = &dyn ModuleResolver> {
self.0.iter().map(|v| v.as_ref())
}
/// Remove all module resolvers.
pub fn clear(&mut self) {
self.0.clear();
}
}
impl ModuleResolver for ModuleResolversCollection {
fn resolve(
&self,
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>> {
for resolver in self.0.iter() {
if let Ok(module) = resolver.resolve(engine, path, pos) {
return Ok(module);
}
}
Err(Box::new(EvalAltResult::ErrorModuleNotFound(
path.into(),
pos,
)))
}
}
}

View File

@@ -67,7 +67,7 @@ macro_rules! reg_tri {
macro_rules! reg_pad {
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
$({
$lib.set_fn_var_args($op,
$lib.set_raw_fn($op,
&[TypeId::of::<Array>(), TypeId::of::<INT>(), TypeId::of::<$par>()],
$func::<$par>
);

View File

@@ -223,7 +223,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
Ok(())
},
);
lib.set_fn_var_args(
lib.set_raw_fn(
"pad",
&[TypeId::of::<ImmutableString>(), TypeId::of::<INT>(), TypeId::of::<char>()],
|engine: &Engine, _: &Module, args: &mut [&mut Dynamic]| {

View File

@@ -213,6 +213,7 @@ pub enum Token {
As,
LexError(Box<LexError>),
Comment(String),
Reserved(String),
Custom(String),
EOF,
}
@@ -229,6 +230,7 @@ impl Token {
StringConstant(_) => "string".into(),
CharConstant(c) => c.to_string().into(),
Identifier(s) => s.clone().into(),
Reserved(s) => s.clone().into(),
Custom(s) => s.clone().into(),
LexError(err) => err.to_string().into(),
@@ -339,7 +341,6 @@ impl Token {
UnaryMinus |
Multiply |
Divide |
Colon |
Comma |
Period |
Equals |
@@ -750,7 +751,9 @@ fn get_next_token_inner(
}
}
// 0x????, 0o????, 0b????
ch @ 'x' | ch @ 'X' | ch @ 'o' | ch @ 'O' | ch @ 'b' | ch @ 'B' if c == '0' => {
ch @ 'x' | ch @ 'X' | ch @ 'o' | ch @ 'O' | ch @ 'b' | ch @ 'B'
if c == '0' =>
{
result.push(next_char);
eat_next(stream, pos);
@@ -889,42 +892,48 @@ fn get_next_token_inner(
}
// " - string literal
('"', _) => return parse_string_literal(stream, state, pos, '"')
.map_or_else(
|err| Some((Token::LexError(Box::new(err.0)), err.1)),
|out| Some((Token::StringConstant(out), start_pos)),
),
('"', _) => {
return parse_string_literal(stream, state, pos, '"').map_or_else(
|err| Some((Token::LexError(Box::new(err.0)), err.1)),
|out| Some((Token::StringConstant(out), start_pos)),
)
}
// ' - character literal
('\'', '\'') => return Some((
Token::LexError(Box::new(LERR::MalformedChar("".to_string()))),
start_pos,
)),
('\'', _) => return Some(
parse_string_literal(stream, state, pos, '\'')
.map_or_else(
|err| (Token::LexError(Box::new(err.0)), err.1),
|result| {
let mut chars = result.chars();
let first = chars.next();
('\'', '\'') => {
return Some((
Token::LexError(Box::new(LERR::MalformedChar("".to_string()))),
start_pos,
))
}
('\'', _) => {
return Some(parse_string_literal(stream, state, pos, '\'').map_or_else(
|err| (Token::LexError(Box::new(err.0)), err.1),
|result| {
let mut chars = result.chars();
let first = chars.next();
if chars.next().is_some() {
(
Token::LexError(Box::new(LERR::MalformedChar(result))),
start_pos,
)
} else {
(Token::CharConstant(first.expect("should be Some")), start_pos)
}
},
),
),
if chars.next().is_some() {
(
Token::LexError(Box::new(LERR::MalformedChar(result))),
start_pos,
)
} else {
(
Token::CharConstant(first.expect("should be Some")),
start_pos,
)
}
},
))
}
// Braces
('{', _) => return Some((Token::LeftBrace, start_pos)),
('}', _) => return Some((Token::RightBrace, start_pos)),
// Parentheses
('(', '*') => return Some((Token::Reserved("(*".into()), start_pos)),
('(', _) => return Some((Token::LeftParen, start_pos)),
(')', _) => return Some((Token::RightParen, start_pos)),
@@ -953,15 +962,11 @@ fn get_next_token_inner(
eat_next(stream, pos);
return Some((Token::MinusAssign, start_pos));
}
('-', '>') => return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"'->' is not a valid symbol. This is not C or C++!".to_string(),
))),
start_pos,
)),
('-', '>') => return Some((Token::Reserved("->".into()), start_pos)),
('-', _) if !state.non_unary => return Some((Token::UnaryMinus, start_pos)),
('-', _) => return Some((Token::Minus, start_pos)),
('*', ')') => return Some((Token::Reserved("*)".into()), start_pos)),
('*', '=') => {
eat_next(stream, pos);
return Some((Token::MultiplyAssign, start_pos));
@@ -1026,49 +1031,31 @@ fn get_next_token_inner(
// Warn against `===`
if stream.peek_next() == Some('=') {
return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"'===' is not a valid operator. This is not JavaScript! Should it be '=='?"
.to_string(),
))),
start_pos,
));
return Some((Token::Reserved("===".into()), start_pos));
}
return Some((Token::EqualsTo, start_pos));
}
('=', '>') => return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"'=>' is not a valid symbol. This is not Rust! Should it be '>='?"
.to_string(),
))),
start_pos,
)),
('=', '>') => return Some((Token::Reserved("=>".into()), start_pos)),
('=', _) => return Some((Token::Equals, start_pos)),
(':', ':') => {
eat_next(stream, pos);
if stream.peek_next() == Some('<') {
return Some((Token::Reserved("::<".into()), start_pos));
}
return Some((Token::DoubleColon, start_pos));
}
(':', '=') => return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"':=' is not a valid assignment operator. This is not Pascal! Should it be simply '='?"
.to_string(),
))),
start_pos,
)),
(':', '=') => return Some((Token::Reserved(":=".into()), start_pos)),
(':', _) => return Some((Token::Colon, start_pos)),
('<', '=') => {
eat_next(stream, pos);
return Some((Token::LessThanEqualsTo, start_pos));
}
('<', '-') => return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"'<-' is not a valid symbol. Should it be '<='?".to_string(),
))),
start_pos,
)),
('<', '-') => return Some((Token::Reserved("<-".into()), start_pos)),
('<', '<') => {
eat_next(stream, pos);
@@ -1106,15 +1093,8 @@ fn get_next_token_inner(
('!', '=') => {
eat_next(stream, pos);
// Warn against `!==`
if stream.peek_next() == Some('=') {
return Some((
Token::LexError(Box::new(LERR::ImproperSymbol(
"'!==' is not a valid operator. This is not JavaScript! Should it be '!='?"
.to_string(),
))),
start_pos,
));
return Some((Token::Reserved("!==".into()), start_pos));
}
return Some((Token::NotEqualsTo, start_pos));
@@ -1159,10 +1139,17 @@ fn get_next_token_inner(
}
('~', _) => return Some((Token::PowerOf, start_pos)),
('@', _) => return Some((Token::Reserved("@".into()), start_pos)),
('\0', _) => unreachable!(),
(ch, _) if ch.is_whitespace() => (),
(ch, _) => return Some((Token::LexError(Box::new(LERR::UnexpectedInput(ch.to_string()))), start_pos)),
(ch, _) => {
return Some((
Token::LexError(Box::new(LERR::UnexpectedInput(ch.to_string()))),
start_pos,
))
}
}
}
@@ -1237,6 +1224,41 @@ impl<'a> Iterator for TokenIterator<'a, '_> {
self.engine.custom_keywords.as_ref(),
) {
(None, _, _) => None,
(Some((Token::Reserved(s), pos)), None, None) => return Some((match s.as_str() {
"===" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'===' is not a valid operator. This is not JavaScript! Should it be '=='?"
.to_string(),
))),
"!==" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'!==' is not a valid operator. This is not JavaScript! Should it be '!='?"
.to_string(),
))),
"->" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'->' is not a valid symbol. This is not C or C++!".to_string(),
))),
"<-" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'<-' is not a valid symbol. This is not Go! Should it be '<='?".to_string(),
))),
"=>" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'=>' is not a valid symbol. This is not Rust! Should it be '>='?"
.to_string(),
))),
":=" => Token::LexError(Box::new(LERR::ImproperSymbol(
"':=' is not a valid assignment operator. This is not Go! Should it be simply '='?"
.to_string(),
))),
"::<" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'::<>' is not a valid symbol. This is not Rust! Should it be '::'?"
.to_string(),
))),
"(*" | "*)" => Token::LexError(Box::new(LERR::ImproperSymbol(
"'(* .. *)' is not a valid comment style. This is not Pascal! Should it be '/* .. */'?"
.to_string(),
))),
token => Token::LexError(Box::new(LERR::ImproperSymbol(
format!("'{}' is not a valid symbol.", token)
))),
}, pos)),
(r @ Some(_), None, None) => r,
(Some((token, pos)), Some(disabled), _)
if token.is_operator() && disabled.contains(token.syntax().as_ref()) =>