Streamline as_mut to &mut.

This commit is contained in:
Stephen Chung 2021-04-17 13:54:24 +08:00
parent 199df9aa4a
commit 2f2b7403cb
4 changed files with 16 additions and 26 deletions

View File

@ -913,15 +913,14 @@ impl Engine {
let new_hash = FnCallHash::from_script(calc_fn_hash(empty(), fn_name, args_len)); let new_hash = FnCallHash::from_script(calc_fn_hash(empty(), fn_name, args_len));
// Arguments are passed as-is, adding the curried arguments // Arguments are passed as-is, adding the curried arguments
let mut curry = fn_ptr.curry().iter().cloned().collect::<StaticVec<_>>(); let mut curry = fn_ptr.curry().iter().cloned().collect::<StaticVec<_>>();
let mut arg_values = curry let mut args = curry
.iter_mut() .iter_mut()
.chain(call_args.iter_mut()) .chain(call_args.iter_mut())
.collect::<StaticVec<_>>(); .collect::<StaticVec<_>>();
let args = arg_values.as_mut();
// Map it to name(args) in function-call style // Map it to name(args) in function-call style
self.exec_fn_call( self.exec_fn_call(
mods, state, lib, fn_name, new_hash, args, false, false, pos, None, level, mods, state, lib, fn_name, new_hash, &mut args, false, false, pos, None, level,
) )
} }
KEYWORD_FN_PTR_CALL => { KEYWORD_FN_PTR_CALL => {
@ -952,15 +951,14 @@ impl Engine {
); );
// Replace the first argument with the object pointer, adding the curried arguments // Replace the first argument with the object pointer, adding the curried arguments
let mut curry = fn_ptr.curry().iter().cloned().collect::<StaticVec<_>>(); let mut curry = fn_ptr.curry().iter().cloned().collect::<StaticVec<_>>();
let mut arg_values = once(obj) let mut args = once(obj)
.chain(curry.iter_mut()) .chain(curry.iter_mut())
.chain(call_args.iter_mut()) .chain(call_args.iter_mut())
.collect::<StaticVec<_>>(); .collect::<StaticVec<_>>();
let args = arg_values.as_mut();
// Map it to name(args) in function-call style // Map it to name(args) in function-call style
self.exec_fn_call( self.exec_fn_call(
mods, state, lib, fn_name, new_hash, args, is_ref, true, pos, None, level, mods, state, lib, fn_name, new_hash, &mut args, is_ref, true, pos, None, level,
) )
} }
KEYWORD_FN_PTR_CURRY => { KEYWORD_FN_PTR_CURRY => {
@ -1030,13 +1028,12 @@ impl Engine {
}; };
// Attached object pointer in front of the arguments // Attached object pointer in front of the arguments
let mut arg_values = once(obj) let mut args = once(obj)
.chain(call_args.iter_mut()) .chain(call_args.iter_mut())
.collect::<StaticVec<_>>(); .collect::<StaticVec<_>>();
let args = arg_values.as_mut();
self.exec_fn_call( self.exec_fn_call(
mods, state, lib, fn_name, hash, args, is_ref, true, pos, None, level, mods, state, lib, fn_name, hash, &mut args, is_ref, true, pos, None, level,
) )
} }
}?; }?;
@ -1344,10 +1341,8 @@ impl Engine {
} }
} }
let args = args.as_mut();
self.exec_fn_call( self.exec_fn_call(
mods, state, lib, name, hash, args, is_ref, false, pos, capture, level, mods, state, lib, name, hash, &mut args, is_ref, false, pos, capture, level,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
} }
@ -1468,7 +1463,6 @@ impl Engine {
if fn_def.body.is_empty() { if fn_def.body.is_empty() {
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
} else { } else {
let args = args.as_mut();
let new_scope = &mut Default::default(); let new_scope = &mut Default::default();
let mut source = module.id_raw().cloned(); let mut source = module.id_raw().cloned();
@ -1477,7 +1471,7 @@ impl Engine {
let level = level + 1; let level = level + 1;
let result = self.call_script_fn( let result = self.call_script_fn(
new_scope, mods, state, lib, &mut None, fn_def, args, pos, level, new_scope, mods, state, lib, &mut None, fn_def, &mut args, pos, level,
); );
state.source = source; state.source = source;
@ -1489,17 +1483,13 @@ impl Engine {
Some(f) if f.is_plugin_fn() => f Some(f) if f.is_plugin_fn() => f
.get_plugin_fn() .get_plugin_fn()
.clone() .clone()
.call( .call((self, fn_name, module.id(), &*mods, lib).into(), &mut args)
(self, fn_name, module.id(), &*mods, lib).into(),
args.as_mut(),
)
.map_err(|err| err.fill_position(pos)), .map_err(|err| err.fill_position(pos)),
Some(f) if f.is_native() => f.get_native_fn()( Some(f) if f.is_native() => {
(self, fn_name, module.id(), &*mods, lib).into(), f.get_native_fn()((self, fn_name, module.id(), &*mods, lib).into(), &mut args)
args.as_mut(), .map_err(|err| err.fill_position(pos))
) }
.map_err(|err| err.fill_position(pos)),
Some(f) => unreachable!("unknown function type: {:?}", f), Some(f) => unreachable!("unknown function type: {:?}", f),

View File

@ -348,7 +348,7 @@ impl FnPtr {
arg_values.iter_mut().collect() arg_values.iter_mut().collect()
}; };
ctx.call_fn_dynamic_raw(self.fn_name(), is_method, args.as_mut()) ctx.call_fn_dynamic_raw(self.fn_name(), is_method, &mut args)
} }
} }

View File

@ -947,7 +947,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) {
"" ""
}; };
if let Some(result) = call_fn_with_constant_arguments(&state, x.name.as_ref(), arg_values.as_mut()) if let Some(result) = call_fn_with_constant_arguments(&state, x.name.as_ref(), &mut arg_values)
.or_else(|| { .or_else(|| {
if !arg_for_type_of.is_empty() { if !arg_for_type_of.is_empty() {
// Handle `type_of()` // Handle `type_of()`

View File

@ -381,7 +381,7 @@ fn parse_fn_call(
(Token::RightParen, _) => { (Token::RightParen, _) => {
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
let hash = if let Some(modules) = namespace.as_mut() { let hash = if let Some(ref mut modules) = namespace {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
modules.set_index(state.find_module(&modules[0].name)); modules.set_index(state.find_module(&modules[0].name));