This project is read-only.

Debugging

Defining debug mode

By default, scripts are executed in Release mode and are not impacted by any debugging overhead. To enable debugging in Jint, the DebugMode property must be set to true.

JintEngine engine = new JintEngine();
engine.SetDebugMode(true);

Debug information

Every debugging feature gives access to some information represented by the class Jint.Debugger.DebugInformation. This class gives access to:
  • The executed statement
  • The call stack
  • The local variables

Executing step by step

The debugger enables to execute the scripts step by step. It exposes an event named Step which is called when any statement is executed. It returns the engine as the sender argument, and a DebugInformation instance. The example below demonstrates how to trace all the executed statements to the console.

JintEngine jint = new JintEngine();
jint.SetDebugMode(true);

jint.Step += (sender, info) =>
{
   Console.WriteLine("{0}: Line {1}, Char {2}", info.CurrentStatement.ToString(), info.Location.Start.Line, info.Location.Start.Char);
};

Evaluating local variables

In the debug information, the local variables are accessible through a dictionary indexed by their names.

Setting breakpoints

Breakpoints are added to the BreakPoints collection of the engine. The class Jint.Debugger.BreakPoint is used to represent a located point in a script string, and optionally define a condition to break the execution.

The example below demonstrates how to stop on a statement inside a function, only if a parameter as a specific value.

JintEngine jint = new JintEngine();
jint.SetDebugMode(true);
jint.BreakPoints.Add(new BreakPoint(4, 22, "x == 2;")); // Line 4, Char 22 => return x*x; only if x == 2

jint.Break += (sender, info) =>
{
    Console.WriteLine(info.Locals["x"].Value); // Displays 2
};

jint.Run(@"
    var i = 3; 
    function square(x) { 
        return x*x; // breakpoint here
    }
    
    square(1);
    square(2); // will break only on this line
    square(3);
");

Extensibility

This allows any standard application to handle simple debugging, but also to create a very simple graphical editor which would display a script, break points, and steps while executing. This application would just need to register to the Step event to get every step executed and display it to the editor, as the locals, watch list and call stack.

Last edited Jul 19, 2011 at 1:50 AM by sebastienros, version 4

Comments

helionprime Dec 19, 2013 at 7:30 AM 
Could you please update this page, because it is not up to date with current version. (eg. Location object od DebugInformation does not exist anymore. and if you can show some examples of evaluating local variables in step event.

Thank you.