Use fluent style.

This commit is contained in:
Stephen Chung 2022-12-09 10:04:44 +08:00
parent a391f26920
commit 3d5908480a
6 changed files with 92 additions and 101 deletions

View File

@ -171,7 +171,7 @@ impl Engine {
let mut arg_values = StaticVec::new_const(); let mut arg_values = StaticVec::new_const();
args.parse(&mut arg_values); args.parse(&mut arg_values);
let result = self._call_fn( self._call_fn(
options, options,
scope, scope,
&mut GlobalRuntimeState::new(self), &mut GlobalRuntimeState::new(self),
@ -179,8 +179,8 @@ impl Engine {
ast, ast,
name.as_ref(), name.as_ref(),
arg_values.as_mut(), arg_values.as_mut(),
)?; )
.and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T)); return Ok(reify!(result => T));
@ -193,6 +193,7 @@ impl Engine {
let t = self.map_type_name(type_name::<T>()).into(); let t = self.map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
}) })
})
} }
/// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments. /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments.
/// ///

View File

@ -233,34 +233,29 @@ impl Engine {
ast.resolver().cloned(), ast.resolver().cloned(),
); );
let statements = ast.statements(); auto_restore!(global => move |g| {
#[cfg(not(feature = "no_module"))]
if statements.is_empty() { {
return Ok(Dynamic::UNIT); g.embedded_module_resolver = orig_embedded_module_resolver;
} }
let result = self.eval_global_statements(global, caches, scope, statements); #[cfg(not(feature = "no_function"))]
g.lib.truncate(orig_lib_len);
g.source = orig_source;
});
self.eval_global_statements(global, caches, scope, ast.statements())
.and_then(|r| {
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.is_debugger_registered() { if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate; global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let mut this = Dynamic::NULL; let mut this = Dynamic::NULL;
let node = &crate::ast::Stmt::Noop(Position::NONE); let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, scope, &mut this, node)?; self.run_debugger(global, caches, scope, &mut this, node)?;
} }
Ok(r)
#[cfg(not(feature = "no_module"))] })
{
global.embedded_module_resolver = orig_embedded_module_resolver;
}
#[cfg(not(feature = "no_function"))]
global.lib.truncate(orig_lib_len);
global.source = orig_source;
result
} }
} }

View File

@ -126,15 +126,8 @@ impl Engine {
global.embedded_module_resolver = ast.resolver().cloned(); global.embedded_module_resolver = ast.resolver().cloned();
} }
let statements = ast.statements(); self.eval_global_statements(global, caches, scope, ast.statements())
.and_then(|_| {
let result = if !statements.is_empty() {
self.eval_global_statements(global, caches, scope, statements)
.map(|_| ())
} else {
Ok(())
};
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.is_debugger_registered() { if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate; global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
@ -142,8 +135,8 @@ impl Engine {
let node = &crate::ast::Stmt::Noop(crate::Position::NONE); let node = &crate::ast::Stmt::Noop(crate::Position::NONE);
self.run_debugger(global, caches, scope, &mut this, node)?; self.run_debugger(global, caches, scope, &mut this, node)?;
} }
Ok(())
result })
} }
} }

View File

@ -303,8 +303,8 @@ impl<'a> NativeCallContext<'a> {
let mut args: StaticVec<_> = arg_values.iter_mut().collect(); let mut args: StaticVec<_> = arg_values.iter_mut().collect();
let result = self._call_fn_raw(fn_name, &mut args, false, false, false)?; self._call_fn_raw(fn_name, &mut args, false, false, false)
.and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T)); return Ok(reify!(result => T));
@ -316,6 +316,7 @@ impl<'a> NativeCallContext<'a> {
let t = self.engine().map_type_name(type_name::<T>()).into(); let t = self.engine().map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
}) })
})
} }
/// Call a registered native Rust function inside the call context with the provided arguments. /// Call a registered native Rust function inside the call context with the provided arguments.
/// ///
@ -333,8 +334,8 @@ impl<'a> NativeCallContext<'a> {
let mut args: StaticVec<_> = arg_values.iter_mut().collect(); let mut args: StaticVec<_> = arg_values.iter_mut().collect();
let result = self._call_fn_raw(fn_name, &mut args, true, false, false)?; self._call_fn_raw(fn_name, &mut args, true, false, false)
.and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T)); return Ok(reify!(result => T));
@ -346,6 +347,7 @@ impl<'a> NativeCallContext<'a> {
let t = self.engine().map_type_name(type_name::<T>()).into(); let t = self.engine().map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
}) })
})
} }
/// Call a function (native Rust or scripted) inside the call context. /// Call a function (native Rust or scripted) inside the call context.
/// ///

View File

@ -205,12 +205,12 @@ impl Engine {
} }
// First check script-defined functions // First check script-defined functions
let result = global.lib.iter().any(|m| m.contains_fn(hash_script)) let r = global.lib.iter().any(|m| m.contains_fn(hash_script))
// Then check the global namespace and packages // Then check the global namespace and packages
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script)); || self.global_modules.iter().any(|m| m.contains_fn(hash_script));
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let result = result || let r = r ||
// Then check imported modules // Then check imported modules
global.contains_qualified_fn(hash_script) global.contains_qualified_fn(hash_script)
// Then check sub-modules // Then check sub-modules
@ -218,11 +218,11 @@ impl Engine {
m.values().any(|m| m.contains_qualified_fn(hash_script)) m.values().any(|m| m.contains_qualified_fn(hash_script))
}); });
if !result && !cache.filter.is_absent_and_set(hash_script) { if !r && !cache.filter.is_absent_and_set(hash_script) {
// Do not cache "one-hit wonders" // Do not cache "one-hit wonders"
cache.map.insert(hash_script, None); cache.map.insert(hash_script, None);
} }
result r
} }
} }

View File

@ -158,8 +158,7 @@ impl FnPtr {
let ctx = (engine, self.fn_name(), None, &*global, Position::NONE).into(); let ctx = (engine, self.fn_name(), None, &*global, Position::NONE).into();
let result = self.call_raw(&ctx, None, arg_values)?; self.call_raw(&ctx, None, arg_values).and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T)); return Ok(reify!(result => T));
@ -171,6 +170,7 @@ impl FnPtr {
let t = engine.map_type_name(type_name::<T>()).into(); let t = engine.map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
}) })
})
} }
/// Call the function pointer with curried arguments (if any). /// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust. /// The function may be script-defined (not available under `no_function`) or native Rust.
@ -187,8 +187,7 @@ impl FnPtr {
let mut arg_values = crate::StaticVec::new_const(); let mut arg_values = crate::StaticVec::new_const();
args.parse(&mut arg_values); args.parse(&mut arg_values);
let result = self.call_raw(context, None, arg_values)?; self.call_raw(context, None, arg_values).and_then(|result| {
// Bail out early if the return type needs no cast // Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() { if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T)); return Ok(reify!(result => T));
@ -200,6 +199,7 @@ impl FnPtr {
let t = context.engine().map_type_name(type_name::<T>()).into(); let t = context.engine().map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
}) })
})
} }
/// Call the function pointer with curried arguments (if any). /// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust. /// The function may be script-defined (not available under `no_function`) or native Rust.