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, [""] | ["step", ..] => break DebuggerCommand::StepInto,
["next", ..] => break DebuggerCommand::StepOver, ["next", ..] => break DebuggerCommand::StepOver,
["scope", ..] => print_scope(context.scope()), ["scope", ..] => print_scope(context.scope()),
#[cfg(not(feature = "no_function"))]
["backtrace", ..] => { ["backtrace", ..] => {
context context
.global_runtime_state() .global_runtime_state()

View File

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

View File

@ -11,7 +11,10 @@ mod target;
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
pub use chaining::{ChainArgument, ChainType}; pub use chaining::{ChainArgument, ChainType};
#[cfg(feature = "debugging")] #[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_context::EvalContext;
pub use eval_state::EvalState; pub use eval_state::EvalState;
pub use global_state::GlobalRuntimeState; pub use global_state::GlobalRuntimeState;

View File

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

View File

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

View File

@ -160,7 +160,9 @@ pub use types::{
/// Exported under the `debugging` feature only. /// Exported under the `debugging` feature only.
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
pub mod debugger { 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 /// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most