Remove call stack under no_function.

This commit is contained in:
Stephen Chung 2022-01-25 14:32:07 +08:00
parent 40aaab60c3
commit 3cec9751bf
6 changed files with 32 additions and 16 deletions

View File

@ -221,6 +221,7 @@ fn main() {
[""] | ["step", ..] => break DebuggerCommand::StepInto,
["next", ..] => break DebuggerCommand::StepOver,
["scope", ..] => print_scope(context.scope()),
#[cfg(not(feature = "no_function"))]
["backtrace", ..] => {
context
.global_runtime_state()

View File

@ -3,7 +3,7 @@
use super::{EvalContext, EvalState, GlobalRuntimeState};
use crate::ast::{ASTNode, Expr, Stmt};
use crate::{Dynamic, Engine, Identifier, Module, Position, Scope, StaticVec};
use crate::{Dynamic, Engine, Identifier, Module, Position, Scope};
use std::fmt;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -75,14 +75,16 @@ impl fmt::Display for BreakPoint {
}
/// A function call.
#[cfg(not(feature = "no_function"))]
#[derive(Debug, Clone, Hash)]
pub struct CallStackFrame {
pub fn_name: Identifier,
pub args: StaticVec<Dynamic>,
pub args: crate::StaticVec<Dynamic>,
pub source: Identifier,
pub pos: Position,
}
#[cfg(not(feature = "no_function"))]
impl fmt::Display for CallStackFrame {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut fp = f.debug_tuple(&self.fn_name);
@ -110,6 +112,7 @@ impl fmt::Display for CallStackFrame {
pub struct Debugger {
status: DebuggerCommand,
break_points: Vec<BreakPoint>,
#[cfg(not(feature = "no_function"))]
call_stack: Vec<CallStackFrame>,
}
@ -119,30 +122,35 @@ impl Debugger {
Self {
status: DebuggerCommand::Continue,
break_points: Vec::new(),
#[cfg(not(feature = "no_function"))]
call_stack: Vec::new(),
}
}
/// Get the function call stack depth.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn call_stack_len(&self) -> usize {
self.call_stack.len()
}
/// Get the current call stack.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn call_stack(&self) -> &[CallStackFrame] {
&self.call_stack
}
/// Rewind the function call stack to a particular depth.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub(crate) fn rewind_call_stack(&mut self, len: usize) {
self.call_stack.truncate(len);
}
/// Add a new frame to the function call stack.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub(crate) fn push_call_stack_frame(
&mut self,
fn_name: impl Into<Identifier>,
args: StaticVec<Dynamic>,
args: crate::StaticVec<Dynamic>,
source: impl Into<Identifier>,
pos: Position,
) {

View File

@ -11,7 +11,10 @@ mod target;
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
pub use chaining::{ChainArgument, ChainType};
#[cfg(feature = "debugging")]
pub use debugger::{BreakPoint, CallStackFrame, Debugger, DebuggerCommand, OnDebuggerCallback};
#[cfg(not(feature = "no_function"))]
pub use debugger::CallStackFrame;
#[cfg(feature = "debugging")]
pub use debugger::{BreakPoint, Debugger, DebuggerCommand, OnDebuggerCallback};
pub use eval_context::EvalContext;
pub use eval_state::EvalState;
pub use global_state::GlobalRuntimeState;

View File

@ -712,17 +712,15 @@ impl Engine {
err_map.insert("source".into(), global.source.clone().into());
}
if err_pos.is_none() {
// No position info
} else {
let line = err_pos.line().unwrap() as INT;
let position = if err_pos.is_beginning_of_line() {
0
} else {
err_pos.position().unwrap()
} as INT;
err_map.insert("line".into(), line.into());
err_map.insert("position".into(), position.into());
if !err_pos.is_none() {
err_map.insert(
"line".into(),
(err_pos.line().unwrap() as INT).into(),
);
err_map.insert(
"position".into(),
(err_pos.position().unwrap_or(0) as INT).into(),
);
}
err.dump_fields(&mut err_map);

View File

@ -71,7 +71,9 @@ impl Engine {
let orig_scope_len = scope.len();
let orig_imports_len = global.num_imports();
#[cfg(feature = "debugging")]
#[cfg(not(feature = "no_function"))]
let orig_call_stack_len = global.debugger.call_stack_len();
// Put arguments into scope as variables
@ -82,6 +84,7 @@ impl Engine {
// Push a new call stack frame
#[cfg(feature = "debugging")]
#[cfg(not(feature = "no_function"))]
global.debugger.push_call_stack_frame(
fn_def.name.clone(),
scope
@ -166,6 +169,7 @@ impl Engine {
// Pop the call stack
#[cfg(feature = "debugging")]
#[cfg(not(feature = "no_function"))]
global.debugger.rewind_call_stack(orig_call_stack_len);
result

View File

@ -160,7 +160,9 @@ pub use types::{
/// Exported under the `debugging` feature only.
#[cfg(feature = "debugging")]
pub mod debugger {
pub use super::eval::{BreakPoint, CallStackFrame, Debugger, DebuggerCommand};
#[cfg(not(feature = "no_function"))]
pub use super::eval::CallStackFrame;
pub use super::eval::{BreakPoint, Debugger, DebuggerCommand};
}
/// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most