Add function pointer short-hand.

This commit is contained in:
Stephen Chung
2022-08-05 23:30:44 +08:00
parent ca65e17610
commit d8532b48b6
8 changed files with 68 additions and 13 deletions

View File

@@ -149,6 +149,18 @@ impl Engine {
}
_ if global.always_search_scope => (0, expr.start_position()),
Expr::Variable(.., Some(i), pos) => (i.get() as usize, *pos),
// Scripted function with the same name
#[cfg(not(feature = "no_function"))]
Expr::Variable(v, None, pos)
if lib
.iter()
.flat_map(|&m| m.iter_script_fn())
.any(|(_, _, f, ..)| f == v.3) =>
{
let val: Dynamic =
crate::FnPtr::new_unchecked(v.3.as_str(), Default::default()).into();
return Ok((val.into(), *pos));
}
Expr::Variable(v, None, pos) => (v.0.map_or(0, NonZeroUsize::get), *pos),
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
};

View File

@@ -244,12 +244,12 @@ impl Engine {
// We shouldn't do this for too many variants because, soon or later, the added comparisons
// will cost more than the mis-predicted `match` branch.
if let Stmt::Assignment(x, ..) = stmt {
let (op_info, BinaryExpr { lhs, rhs }) = &**x;
#[cfg(not(feature = "unchecked"))]
self.inc_operations(&mut global.num_operations, stmt.position())?;
let result = if x.1.lhs.is_variable_access(false) {
let (op_info, BinaryExpr { lhs, rhs }) = &**x;
let result = if let Expr::Variable(x, ..) = lhs {
let rhs_result = self
.eval_expr(scope, global, caches, lib, this_ptr, rhs, level)
.map(Dynamic::flatten);
@@ -261,7 +261,7 @@ impl Engine {
if let Ok(search_val) = search_result {
let (mut lhs_ptr, pos) = search_val;
let var_name = lhs.get_variable_name(false).expect("`Expr::Variable`");
let var_name = x.3.as_str();
#[cfg(not(feature = "no_closure"))]
// Also handle case where target is a `Dynamic` shared value