Minor code refactoring.
This commit is contained in:
parent
95d0b2e620
commit
71ec23e621
@ -7,7 +7,7 @@ pub type Dynamic = Box<Variant>;
|
|||||||
pub trait Any: StdAny {
|
pub trait Any: StdAny {
|
||||||
fn type_id(&self) -> TypeId;
|
fn type_id(&self) -> TypeId;
|
||||||
|
|
||||||
fn type_name(&self) -> String;
|
fn type_name(&self) -> &'static str;
|
||||||
|
|
||||||
fn into_dynamic(&self) -> Dynamic;
|
fn into_dynamic(&self) -> Dynamic;
|
||||||
|
|
||||||
@ -25,8 +25,8 @@ where
|
|||||||
TypeId::of::<T>()
|
TypeId::of::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_name(&self) -> String {
|
fn type_name(&self) -> &'static str {
|
||||||
type_name::<T>().to_string()
|
type_name::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
20
src/api.rs
20
src/api.rs
@ -88,15 +88,19 @@ impl Engine {
|
|||||||
self.script_fns.clear(); // Clean up engine
|
self.script_fns.clear(); // Clean up engine
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Err(EvalAltResult::Return(out, pos)) => Ok(*out.downcast::<T>().map_err(|a| {
|
Err(EvalAltResult::Return(out, pos)) => out.downcast::<T>().map(|v| *v).map_err(|a| {
|
||||||
let name = self.map_type_name((*a).type_name());
|
EvalAltResult::ErrorMismatchOutputType(
|
||||||
EvalAltResult::ErrorMismatchOutputType(name, pos)
|
self.map_type_name((*a).type_name()).to_string(),
|
||||||
})?),
|
pos,
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
|
||||||
Ok(out) => Ok(*out.downcast::<T>().map_err(|a| {
|
Ok(out) => out.downcast::<T>().map(|v| *v).map_err(|a| {
|
||||||
let name = self.map_type_name((*a).type_name());
|
EvalAltResult::ErrorMismatchOutputType(
|
||||||
EvalAltResult::ErrorMismatchOutputType(name, Position::eof())
|
self.map_type_name((*a).type_name()).to_string(),
|
||||||
})?),
|
Position::eof(),
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
|
@ -175,8 +175,10 @@ impl Engine {
|
|||||||
self.call_fn_raw(ident.into(), args.into_vec(), None, pos)
|
self.call_fn_raw(ident.into(), args.into_vec(), None, pos)
|
||||||
.and_then(|b| {
|
.and_then(|b| {
|
||||||
b.downcast().map(|b| *b).map_err(|a| {
|
b.downcast().map(|b| *b).map_err(|a| {
|
||||||
let name = self.map_type_name((*a).type_name());
|
EvalAltResult::ErrorMismatchOutputType(
|
||||||
EvalAltResult::ErrorMismatchOutputType(name, pos)
|
self.map_type_name((*a).type_name()).into(),
|
||||||
|
pos,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -191,17 +193,16 @@ impl Engine {
|
|||||||
pos: Position,
|
pos: Position,
|
||||||
) -> Result<Dynamic, EvalAltResult> {
|
) -> Result<Dynamic, EvalAltResult> {
|
||||||
debug_println!(
|
debug_println!(
|
||||||
"Trying to call function {:?} with args {:?}",
|
"Calling {}({})",
|
||||||
ident,
|
ident,
|
||||||
args.iter()
|
args.iter()
|
||||||
.map(|x| { self.map_type_name((**x).type_name()) })
|
.map(|x| (*x).type_name())
|
||||||
|
.map(|name| self.map_type_name(name))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut spec = FnSpec {
|
let mut spec = FnSpec { ident, args: None };
|
||||||
ident: ident.clone(),
|
|
||||||
args: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
// First search in script-defined functions (can override built-in),
|
// First search in script-defined functions (can override built-in),
|
||||||
// then in built-in's
|
// then in built-in's
|
||||||
@ -219,7 +220,7 @@ impl Engine {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
let callback = match ident.as_str() {
|
let callback = match spec.ident.as_str() {
|
||||||
"print" => &self.on_print,
|
"print" => &self.on_print,
|
||||||
"debug" => &self.on_debug,
|
"debug" => &self.on_debug,
|
||||||
_ => return r,
|
_ => return r,
|
||||||
@ -240,7 +241,7 @@ impl Engine {
|
|||||||
f.params
|
f.params
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.zip(args.iter().map(|x| (&**x).into_dynamic())),
|
.zip(args.iter().map(|x| (*x).into_dynamic())),
|
||||||
);
|
);
|
||||||
|
|
||||||
match self.eval_stmt(&mut scope, &*f.body) {
|
match self.eval_stmt(&mut scope, &*f.body) {
|
||||||
@ -255,12 +256,12 @@ impl Engine {
|
|||||||
} else {
|
} else {
|
||||||
let types_list = args
|
let types_list = args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| (*(&**x).into_dynamic()).type_name())
|
.map(|x| (*x).type_name())
|
||||||
.map(|name| self.map_type_name(name))
|
.map(|name| self.map_type_name(name))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Err(EvalAltResult::ErrorFunctionNotFound(
|
Err(EvalAltResult::ErrorFunctionNotFound(
|
||||||
format!("{} ({})", ident, types_list.join(", ")),
|
format!("{} ({})", spec.ident, types_list.join(", ")),
|
||||||
pos,
|
pos,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -363,7 +364,7 @@ impl Engine {
|
|||||||
|
|
||||||
let mut val = self.call_fn_raw(get_fn_name, vec![this_ptr], None, *pos)?;
|
let mut val = self.call_fn_raw(get_fn_name, vec![this_ptr], None, *pos)?;
|
||||||
|
|
||||||
if let Some(arr) = (*val).downcast_mut() as Option<&mut Array> {
|
if let Some(arr) = val.downcast_mut() as Option<&mut Array> {
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
arr.get(idx as usize)
|
arr.get(idx as usize)
|
||||||
.cloned()
|
.cloned()
|
||||||
@ -371,7 +372,7 @@ impl Engine {
|
|||||||
} else {
|
} else {
|
||||||
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, *pos))
|
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, *pos))
|
||||||
}
|
}
|
||||||
} else if let Some(s) = (*val).downcast_mut() as Option<&mut String> {
|
} else if let Some(s) = val.downcast_mut() as Option<&mut String> {
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
s.chars()
|
s.chars()
|
||||||
.nth(idx as usize)
|
.nth(idx as usize)
|
||||||
@ -453,7 +454,7 @@ impl Engine {
|
|||||||
scope,
|
scope,
|
||||||
id,
|
id,
|
||||||
|val| {
|
|val| {
|
||||||
if let Some(arr) = (*val).downcast_ref() as Option<&Array> {
|
if let Some(arr) = val.downcast_ref() as Option<&Array> {
|
||||||
is_array = true;
|
is_array = true;
|
||||||
|
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
@ -463,7 +464,7 @@ impl Engine {
|
|||||||
} else {
|
} else {
|
||||||
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, begin))
|
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, begin))
|
||||||
}
|
}
|
||||||
} else if let Some(s) = (*val).downcast_ref() as Option<&String> {
|
} else if let Some(s) = val.downcast_ref() as Option<&String> {
|
||||||
is_array = false;
|
is_array = false;
|
||||||
|
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
@ -636,11 +637,8 @@ impl Engine {
|
|||||||
Expr::CharConstant(c, _) => Ok(Box::new(*c)),
|
Expr::CharConstant(c, _) => Ok(Box::new(*c)),
|
||||||
|
|
||||||
Expr::Identifier(id, pos) => scope
|
Expr::Identifier(id, pos) => scope
|
||||||
.iter()
|
.get(id)
|
||||||
.rev()
|
.map(|(_, _, val)| val)
|
||||||
.filter(|(name, _)| id == name)
|
|
||||||
.next()
|
|
||||||
.map(|(_, val)| val.clone())
|
|
||||||
.ok_or_else(|| EvalAltResult::ErrorVariableNotFound(id.clone(), *pos)),
|
.ok_or_else(|| EvalAltResult::ErrorVariableNotFound(id.clone(), *pos)),
|
||||||
|
|
||||||
Expr::Index(id, idx_raw, pos) => self
|
Expr::Index(id, idx_raw, pos) => self
|
||||||
@ -651,17 +649,14 @@ impl Engine {
|
|||||||
let rhs_val = self.eval_expr(scope, rhs)?;
|
let rhs_val = self.eval_expr(scope, rhs)?;
|
||||||
|
|
||||||
match **id {
|
match **id {
|
||||||
Expr::Identifier(ref n, pos) => scope
|
Expr::Identifier(ref name, pos) => {
|
||||||
.iter_mut()
|
if let Some((idx, _, _)) = scope.get(name) {
|
||||||
.rev()
|
*scope.get_mut(name, idx) = rhs_val;
|
||||||
.filter(|(name, _)| n == name)
|
Ok(Box::new(()) as Dynamic)
|
||||||
.next()
|
} else {
|
||||||
.map(|(_, val)| {
|
Err(EvalAltResult::ErrorVariableNotFound(name.clone(), pos))
|
||||||
*val = rhs_val;
|
}
|
||||||
Box::new(()) as Dynamic
|
}
|
||||||
})
|
|
||||||
.ok_or_else(|| EvalAltResult::ErrorVariableNotFound(n.clone(), pos)),
|
|
||||||
|
|
||||||
Expr::Index(ref id, ref idx_raw, pos) => {
|
Expr::Index(ref id, ref idx_raw, pos) => {
|
||||||
let idx_pos = idx_raw.position();
|
let idx_pos = idx_raw.position();
|
||||||
|
|
||||||
@ -893,11 +888,11 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn map_type_name(&self, name: String) -> String {
|
pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
|
||||||
self.type_names
|
self.type_names
|
||||||
.get(&name)
|
.get(name)
|
||||||
.map(|x| x.clone())
|
.map(|s| s.as_str())
|
||||||
.unwrap_or(name.to_string())
|
.unwrap_or(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make a new engine
|
/// Make a new engine
|
||||||
|
@ -39,20 +39,20 @@ macro_rules! def_register {
|
|||||||
const NUM_ARGS: usize = count_args!($($par)*);
|
const NUM_ARGS: usize = count_args!($($par)*);
|
||||||
|
|
||||||
if args.len() != NUM_ARGS {
|
if args.len() != NUM_ARGS {
|
||||||
return Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, pos));
|
Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, pos))
|
||||||
|
} else {
|
||||||
|
#[allow(unused_variables, unused_mut)]
|
||||||
|
let mut drain = args.drain(..);
|
||||||
|
$(
|
||||||
|
// Downcast every element, return in case of a type mismatch
|
||||||
|
let $par = (drain.next().unwrap().downcast_mut() as Option<&mut $par>).unwrap();
|
||||||
|
)*
|
||||||
|
|
||||||
|
// Call the user-supplied function using ($clone) to
|
||||||
|
// potentially clone the value, otherwise pass the reference.
|
||||||
|
let r = f($(($clone)($par)),*);
|
||||||
|
Ok(Box::new(r) as Dynamic)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables, unused_mut)]
|
|
||||||
let mut drain = args.drain(..);
|
|
||||||
$(
|
|
||||||
// Downcast every element, return in case of a type mismatch
|
|
||||||
let $par = ((*drain.next().unwrap()).downcast_mut() as Option<&mut $par>).unwrap();
|
|
||||||
)*
|
|
||||||
|
|
||||||
// Call the user-supplied function using ($clone) to
|
|
||||||
// potentially clone the value, otherwise pass the reference.
|
|
||||||
let r = f($(($clone)($par)),*);
|
|
||||||
Ok(Box::new(r) as Dynamic)
|
|
||||||
};
|
};
|
||||||
self.register_fn_raw(name.into(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
self.register_fn_raw(name.into(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
||||||
}
|
}
|
||||||
@ -72,19 +72,19 @@ macro_rules! def_register {
|
|||||||
const NUM_ARGS: usize = count_args!($($par)*);
|
const NUM_ARGS: usize = count_args!($($par)*);
|
||||||
|
|
||||||
if args.len() != NUM_ARGS {
|
if args.len() != NUM_ARGS {
|
||||||
return Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, pos));
|
Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, pos))
|
||||||
|
} else {
|
||||||
|
#[allow(unused_variables, unused_mut)]
|
||||||
|
let mut drain = args.drain(..);
|
||||||
|
$(
|
||||||
|
// Downcast every element, return in case of a type mismatch
|
||||||
|
let $par = (drain.next().unwrap().downcast_mut() as Option<&mut $par>).unwrap();
|
||||||
|
)*
|
||||||
|
|
||||||
|
// Call the user-supplied function using ($clone) to
|
||||||
|
// potentially clone the value, otherwise pass the reference.
|
||||||
|
Ok(f($(($clone)($par)),*))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables, unused_mut)]
|
|
||||||
let mut drain = args.drain(..);
|
|
||||||
$(
|
|
||||||
// Downcast every element, return in case of a type mismatch
|
|
||||||
let $par = ((*drain.next().unwrap()).downcast_mut() as Option<&mut $par>).unwrap();
|
|
||||||
)*
|
|
||||||
|
|
||||||
// Call the user-supplied function using ($clone) to
|
|
||||||
// potentially clone the value, otherwise pass the reference.
|
|
||||||
Ok(f($(($clone)($par)),*))
|
|
||||||
};
|
};
|
||||||
self.register_fn_raw(name.into(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
self.register_fn_raw(name.into(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user