From fc405c4f0a4e2b6c3cd995f149cf52805b9149dd Mon Sep 17 00:00:00 2001 From: Osman Turan Date: Tue, 26 Oct 2021 18:10:27 +0000 Subject: [PATCH] Add limited console.trace implementation (#1623) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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! --- boa/src/builtins/console/mod.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/boa/src/builtins/console/mod.rs b/boa/src/builtins/console/mod.rs index 3f074091de..f1ee445226 100644 --- a/boa/src/builtins/console/mod.rs +++ b/boa/src/builtins/console/mod.rs @@ -299,6 +299,25 @@ impl Console { Ok(JsValue::undefined()) } + #[cfg(feature = "vm")] + fn get_stack_trace(context: &mut Context) -> Vec { + let mut stack_trace: Vec = 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 { + // 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. @@ -316,11 +335,8 @@ impl Console { context.console(), ); - /* TODO: get and print stack trace */ - logger( - LogMessage::Log("Not implemented: ".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())