Skip to content

Commit

Permalink
Redesign for Vec<Box<dyn Tracer>>
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Jan 20, 2024
1 parent b8af0df commit 80d8ac0
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 135 deletions.
39 changes: 36 additions & 3 deletions cli/src/debug/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use boa_engine::{
builtins::function::OrdinaryFunction,
js_string,
object::ObjectInitializer,
vm::flowgraph::{Direction, Graph},
vm::{flowgraph::{Direction, Graph}, trace::{Tracer, TraceAction}},
Context, JsArgs, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
};

Expand Down Expand Up @@ -121,6 +121,39 @@ fn bytecode(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue>
Ok(js_string!(code.to_string()).into())
}

// ==== Trace functionality ====

#[derive(Debug)]
struct FunctionTracer;

impl Tracer for FunctionTracer {
fn should_trace(&self, frame: &boa_engine::vm::CallFrame) -> TraceAction {
if frame.code_block().traceable() {
if frame.code_block().was_traced() {
return TraceAction::Block
}
return TraceAction::BlockWithBytecode
}
TraceAction::None
}

fn emit_bytecode_trace(&self, msg: &str) {
println!("{msg}");
}

fn emit_call_frame_entrance_trace(&self, msg: &str) {
println!("{msg}");
}

fn emit_call_frame_exit_trace(&self, msg: &str) {
println!("{msg}");
}

fn emit_instruction_trace(&self, msg: &str) {
println!("{msg}");
}
}

fn set_trace_flag_in_function_object(object: &JsObject, value: bool) -> JsResult<()> {
let Some(function) = object.downcast_ref::<OrdinaryFunction>() else {
return Err(JsNativeError::typ()
Expand All @@ -145,7 +178,7 @@ fn trace(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsVal

let arguments = args.get(2..).unwrap_or(&[]);

context.init_partial_trace();
context.set_tracer_implementation(Box::new(FunctionTracer));
set_trace_flag_in_function_object(callable, true)?;
let result = callable.call(this, arguments, context);
set_trace_flag_in_function_object(callable, false)?;
Expand All @@ -163,7 +196,7 @@ fn traceable(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<J
.into());
};

context.init_partial_trace();
context.set_tracer_implementation(Box::new(FunctionTracer));
set_trace_flag_in_function_object(callable, traceable)?;

Ok(value.clone())
Expand Down
9 changes: 1 addition & 8 deletions core/engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,17 +458,10 @@ impl Context {
self.vm.trace.activate_trace();
}

#[cfg(feature = "trace")]
#[inline]
/// Initializes a partial `Vm` trace from the context.
pub fn init_partial_trace(&mut self) {
self.vm.trace.activate_partial_trace();
}

#[cfg(feature = "trace")]
/// Sets custom handling of trace messages.
pub fn set_tracer_implementation(&mut self, tracer: Box<dyn trace::Tracer>) {
self.init_trace();
// self.init_trace();
self.vm.trace.set_tracer(tracer);
}

Expand Down
6 changes: 3 additions & 3 deletions core/engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl CodeBlock {

/// Check if the function is traced.
#[cfg(feature = "trace")]
pub(crate) fn traceable(&self) -> bool {
pub fn traceable(&self) -> bool {
self.flags.get().contains(CodeBlockFlags::TRACEABLE)
}
/// Enable or disable instruction tracing to `stdout`.
Expand All @@ -252,13 +252,13 @@ impl CodeBlock {

/// Returns whether the frame has been previously traced.
#[cfg(feature = "trace")]
pub(crate) fn was_traced(&self) -> bool {
pub fn was_traced(&self) -> bool {
self.flags.get().contains(CodeBlockFlags::WAS_TRACED)
}

/// Set the current frame as traced.
#[cfg(feature = "trace")]
pub(crate) fn set_frame_traced(&self, value: bool) {
pub fn set_frame_traced(&self, value: bool) {
let mut flags = self.flags.get();
flags.set(CodeBlockFlags::WAS_TRACED, value);
self.flags.set(flags);
Expand Down
8 changes: 4 additions & 4 deletions core/engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod runtime_limits;
pub mod flowgraph;

#[cfg(feature = "trace")]
use trace::{TraceAction, VmTrace};
use trace::VmTrace;

// TODO: see if this can be exposed on all features.
#[allow(unused_imports)]
Expand Down Expand Up @@ -389,10 +389,10 @@ impl Context {
}

#[cfg(feature = "trace")]
let result = if self.vm.trace.should_trace(&self.vm) == TraceAction::None {
self.execute_instruction(f)
} else {
let result = if self.vm.trace.should_trace(&self.vm.frame()) {
self.trace_execute_instruction(f)
} else {
self.execute_instruction(f)
};

#[cfg(not(feature = "trace"))]
Expand Down
Loading

0 comments on commit 80d8ac0

Please sign in to comment.