Provide Position to debug.

This commit is contained in:
Stephen Chung
2020-12-12 11:47:18 +08:00
parent 5443368359
commit 40b6a014ae
7 changed files with 59 additions and 25 deletions

View File

@@ -4,7 +4,8 @@ use crate::ast::{Expr, FnCallExpr, Ident, IdentX, ReturnType, Stmt};
use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant};
use crate::fn_call::run_builtin_op_assignment;
use crate::fn_native::{
CallableFunction, IteratorFn, OnPrintCallback, OnProgressCallback, OnVarCallback,
CallableFunction, IteratorFn, OnDebugCallback, OnPrintCallback, OnProgressCallback,
OnVarCallback,
};
use crate::module::NamespaceRef;
use crate::optimize::OptimizationLevel;
@@ -626,7 +627,7 @@ pub struct Engine {
/// Callback closure for implementing the `print` command.
pub(crate) print: OnPrintCallback,
/// Callback closure for implementing the `debug` command.
pub(crate) debug: OnPrintCallback,
pub(crate) debug: OnDebugCallback,
/// Callback closure for progress reporting.
pub(crate) progress: Option<OnProgressCallback>,
@@ -677,7 +678,7 @@ pub fn is_anonymous_fn(fn_name: &str) -> bool {
fn_name.starts_with(FN_ANONYMOUS)
}
/// Print/debug to stdout
/// Print to stdout
#[inline(always)]
fn default_print(_s: &str) {
#[cfg(not(feature = "no_std"))]
@@ -685,6 +686,14 @@ fn default_print(_s: &str) {
println!("{}", _s);
}
/// Debug to stdout
#[inline(always)]
fn default_debug(_s: &str, _pos: Position) {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
println!("{:?} | {}", _pos, _s);
}
/// Search for a module within an imports stack.
/// [`Position`] in [`EvalAltResult`] is [`None`][Position::None] and must be set afterwards.
pub fn search_imports(
@@ -741,7 +750,7 @@ impl Engine {
// default print/debug implementations
print: Box::new(default_print),
debug: Box::new(default_print),
debug: Box::new(default_debug),
// progress callback
progress: None,
@@ -797,7 +806,7 @@ impl Engine {
resolve_var: None,
print: Box::new(|_| {}),
debug: Box::new(|_| {}),
debug: Box::new(|_, _| {}),
progress: None,
optimization_level: if cfg!(feature = "no_optimize") {

View File

@@ -1808,16 +1808,21 @@ impl Engine {
///
/// // Override action of 'print' function
/// let logger = result.clone();
/// engine.on_debug(move |s| logger.write().unwrap().push_str(s));
/// engine.on_debug(move |s, pos| logger.write().unwrap().push_str(
/// &format!("{:?} > {}", pos, s)
/// ));
///
/// engine.consume(r#"debug("hello");"#)?;
/// engine.consume(r#"let x = "hello"; debug(x);"#)?;
///
/// assert_eq!(*result.read().unwrap(), r#""hello""#);
/// assert_eq!(*result.read().unwrap(), r#"1:18 > "hello""#);
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub fn on_debug(&mut self, callback: impl Fn(&str) + SendSync + 'static) -> &mut Self {
pub fn on_debug(
&mut self,
callback: impl Fn(&str, Position) + SendSync + 'static,
) -> &mut Self {
self.debug = Box::new(callback);
self
}

View File

@@ -211,13 +211,16 @@ impl Engine {
false,
),
KEYWORD_DEBUG => (
(self.debug)(result.as_str().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
pos,
)
})?)
(self.debug)(
result.as_str().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
pos,
)
})?,
pos,
)
.into(),
false,
),

View File

@@ -356,7 +356,14 @@ pub type OnProgressCallback = Box<dyn Fn(u64) -> Option<Dynamic> + Send + Sync +
pub type OnPrintCallback = Box<dyn Fn(&str) + 'static>;
/// A standard callback function for printing.
#[cfg(feature = "sync")]
pub type OnPrintCallback<T, R> = Box<dyn Fn(&str) + Send + Sync + 'static>;
pub type OnPrintCallback = Box<dyn Fn(&str) + Send + Sync + 'static>;
/// A standard callback function for debugging.
#[cfg(not(feature = "sync"))]
pub type OnDebugCallback = Box<dyn Fn(&str, Position) + 'static>;
/// A standard callback function for debugging.
#[cfg(feature = "sync")]
pub type OnDebugCallback = Box<dyn Fn(&str, Position) + Send + Sync + 'static>;
/// A standard callback function for variable access.
#[cfg(not(feature = "sync"))]