Skip to content

Commit

Permalink
Add limited console.trace implementation (#1623)
Browse files Browse the repository at this point in the history
This Pull Request partially fixes #307.

It changes the following:

- Implements limited `console.trace` functionality by dumping stack trace. Since `console.trace`'s output is supposed to be implementation-specific according to the technical specification, it should be technically correct 😀 Any hints about potential improvements are welcome!
  • Loading branch information
osman-turan committed Oct 26, 2021
1 parent e0ea210 commit a49bbe4
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,25 @@ impl Console {
Ok(JsValue::undefined())
}

#[cfg(feature = "vm")]
fn get_stack_trace(context: &mut Context) -> Vec<String> {
let mut stack_trace: Vec<String> = vec![];
let mut prev_frame = context.vm.frame.as_ref();

while let Some(frame) = prev_frame {
stack_trace.push(frame.code.name.to_string());
prev_frame = frame.prev.as_ref();
}

stack_trace
}

#[cfg(not(feature = "vm"))]
fn get_stack_trace(_: &mut Context) -> Vec<String> {
// TODO: Implement stack trace retrieval when "vm" feature is not available
vec![]
}

/// `console.trace(...data)`
///
/// Prints a stack trace with "trace" logLevel, optionally labelled by data.
Expand All @@ -316,11 +335,8 @@ impl Console {
context.console(),
);

/* TODO: get and print stack trace */
logger(
LogMessage::Log("Not implemented: <stack trace>".to_string()),
context.console(),
)
let stack_trace_dump = Self::get_stack_trace(context).join("\n");
logger(LogMessage::Log(stack_trace_dump), context.console())
}

Ok(JsValue::undefined())
Expand Down

0 comments on commit a49bbe4

Please sign in to comment.