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

View File

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

View File

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