PaxScripter demo application written in Delphi.NET which illustrates debug capabilities.

demo_debug.jpg
unit WinForm1;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data, PaxScript.Net;

type
  TWinForm2 = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    components: System.ComponentModel.IContainer;
    btnReset: System.Windows.Forms.Button;
    btnRun: System.Windows.Forms.Button;
    btnTraceInto: System.Windows.Forms.Button;
    btnStepOver: System.Windows.Forms.Button;
    btnAddBreakpoint: System.Windows.Forms.Button;
    btnRemoveBreakpoint: System.Windows.Forms.Button;
    PaxScripter1: PaxScript.Net.PaxScripter;
    rtbScript: System.Windows.Forms.RichTextBox;
    rtbState: System.Windows.Forms.RichTextBox;
    procedure InitializeComponent;
    procedure btnReset_Click(sender: System.Object; e: System.EventArgs);
    procedure btnRun_Click(sender: System.Object; e: System.EventArgs);
    procedure btnTraceInto_Click(sender: System.Object; e: System.EventArgs);
    procedure btnStepOver_Click(sender: System.Object; e: System.EventArgs);
    procedure btnAddBreakpoint_Click(sender: System.Object; e: System.EventArgs);
    procedure btnRemoveBreakpoint_Click(sender: System.Object; e: System.EventArgs);
    procedure PaxScripter1_OnChangeState(sender: PaxScript.Net.PaxScripter; e: PaxScript.Net.ChangeStateEventArgs);
  {$ENDREGION}
  strict protected
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
    procedure ShowInfo(sender: PaxScript.Net.PaxScripter; new_state: ScripterState);
  public
    constructor Create;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm2))]

implementation

{$REGION 'Windows Form Designer generated code'}
procedure TWinForm2.InitializeComponent;
begin
  Self.components := System.ComponentModel.Container.Create;
  Self.btnReset := System.Windows.Forms.Button.Create;
  Self.btnRun := System.Windows.Forms.Button.Create;
  Self.btnTraceInto := System.Windows.Forms.Button.Create;
  Self.btnStepOver := System.Windows.Forms.Button.Create;
  Self.btnAddBreakpoint := System.Windows.Forms.Button.Create;
  Self.btnRemoveBreakpoint := System.Windows.Forms.Button.Create;
  Self.PaxScripter1 := PaxScript.Net.PaxScripter.Create(Self.components);
  Self.rtbScript := System.Windows.Forms.RichTextBox.Create;
  Self.rtbState := System.Windows.Forms.RichTextBox.Create;
  Self.SuspendLayout;
  //
  // btnReset
  //
  Self.btnReset.Location := System.Drawing.Point.Create(24, 8);
  Self.btnReset.Name := 'btnReset';
  Self.btnReset.Size := System.Drawing.Size.Create(128, 23);
  Self.btnReset.TabIndex := 0;
  Self.btnReset.Text := 'Reset';
  Include(Self.btnReset.Click, Self.btnReset_Click);
  //
  // btnRun
  //
  Self.btnRun.Location := System.Drawing.Point.Create(24, 40);
  Self.btnRun.Name := 'btnRun';
  Self.btnRun.Size := System.Drawing.Size.Create(128, 23);
  Self.btnRun.TabIndex := 1;
  Self.btnRun.Text := 'Run';
  Include(Self.btnRun.Click, Self.btnRun_Click);
  //
  // btnTraceInto
  //
  Self.btnTraceInto.Location := System.Drawing.Point.Create(24, 72);
  Self.btnTraceInto.Name := 'btnTraceInto';
  Self.btnTraceInto.Size := System.Drawing.Size.Create(128, 23);
  Self.btnTraceInto.TabIndex := 2;
  Self.btnTraceInto.Text := 'Trace Into';
  Include(Self.btnTraceInto.Click, Self.btnTraceInto_Click);
  //
  // btnStepOver
  //
  Self.btnStepOver.Location := System.Drawing.Point.Create(24, 104);
  Self.btnStepOver.Name := 'btnStepOver';
  Self.btnStepOver.Size := System.Drawing.Size.Create(128, 23);
  Self.btnStepOver.TabIndex := 3;
  Self.btnStepOver.Text := 'Step Over';
  Include(Self.btnStepOver.Click, Self.btnStepOver_Click);
  //
  // btnAddBreakpoint
  //
  Self.btnAddBreakpoint.Location := System.Drawing.Point.Create(24, 136);
  Self.btnAddBreakpoint.Name := 'btnAddBreakpoint';
  Self.btnAddBreakpoint.Size := System.Drawing.Size.Create(128, 23);
  Self.btnAddBreakpoint.TabIndex := 4;
  Self.btnAddBreakpoint.Text := 'Add Breakpoint';
  Include(Self.btnAddBreakpoint.Click, Self.btnAddBreakpoint_Click);
  //
  // btnRemoveBreakpoint
  //
  Self.btnRemoveBreakpoint.Location := System.Drawing.Point.Create(24, 168);
  Self.btnRemoveBreakpoint.Name := 'btnRemoveBreakpoint';
  Self.btnRemoveBreakpoint.Size := System.Drawing.Size.Create(128, 23);
  Self.btnRemoveBreakpoint.TabIndex := 5;
  Self.btnRemoveBreakpoint.Text := 'Remove Breakpoint';
  Include(Self.btnRemoveBreakpoint.Click, Self.btnRemoveBreakpoint_Click);
  //
  // PaxScripter1
  //
  Include(Self.PaxScripter1.OnChangeState, Self.PaxScripter1_OnChangeState);
  //
  // rtbScript
  //
  Self.rtbScript.Location := System.Drawing.Point.Create(176, 8);
  Self.rtbScript.Name := 'rtbScript';
  Self.rtbScript.Size := System.Drawing.Size.Create(368, 216);
  Self.rtbScript.TabIndex := 6;
  Self.rtbScript.Text := 'using System;'#10'class TestFactorial'#10'{'#10'    public static long Factorial (long numb'+
  'er)'#10'    {'#10'        if (number == 0)'#10'            return 1;'#10'        else'#10'        '+
  '    return (number * Factorial (number - 1));'#10'    }'#10'    public static void Mai'+
  'n()'#10'    {'#10'        Console.WriteLine("3 factorial is {0}", Factorial(3)); '#10'    '+
  '}'#10'}';
  //
  // rtbState
  //
  Self.rtbState.Anchor := (System.Windows.Forms.AnchorStyles((((System.Windows.Forms.AnchorStyles.Top
    or System.Windows.Forms.AnchorStyles.Bottom) or System.Windows.Forms.AnchorStyles.Left)
    or System.Windows.Forms.AnchorStyles.Right)));
  Self.rtbState.Location := System.Drawing.Point.Create(8, 240);
  Self.rtbState.Name := 'rtbState';
  Self.rtbState.Size := System.Drawing.Size.Create(536, 160);
  Self.rtbState.TabIndex := 7;
  Self.rtbState.Text := '';
  //
  // TWinForm2
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(552, 414);
  Self.Controls.Add(Self.rtbState);
  Self.Controls.Add(Self.rtbScript);
  Self.Controls.Add(Self.btnRemoveBreakpoint);
  Self.Controls.Add(Self.btnAddBreakpoint);
  Self.Controls.Add(Self.btnStepOver);
  Self.Controls.Add(Self.btnTraceInto);
  Self.Controls.Add(Self.btnRun);
  Self.Controls.Add(Self.btnReset);
  Self.Name := 'TWinForm2';
  Self.Text := 'DebugApp';
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm2.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm2.Create;
begin
  inherited Create;
  InitializeComponent;
end;

procedure TWinForm2.ShowInfo(sender: PaxScript.Net.PaxScripter; new_state: ScripterState);
var
  v: System.Object;
  csr: CallStackRec;
  bp: Breakpoint;
  I: Integer;
  s: String;
begin
  s := '';
  case new_state of
    ScripterState.Init: s := 'Init';
    ScripterState.Paused: s := 'Paused';
    ScripterState.Terminated: s := 'Terminated';
  end;

  rtbState.Text := 'State: ' + s;
  rtbState.Text := rtbState.Text + #10 + 'Module: ' + sender.CurrentModule;
  rtbState.Text := rtbState.Text + #10 + 'Line ' + sender.CurrentLineNumber.ToString() +
      ' : ' + sender.CurrentLine;

  v := nil;
  if new_state = ScripterState.Paused then
  begin
    v := sender.Eval('number');
    sender.DiscardError();
  end;

  if v = nil then
    rtbState.Text := rtbState.Text + #10 + 'number = null'
  else
    rtbState.Text :=rtbState.Text + #10 + 'number = ' + v.ToString();

  rtbState.Text := rtbState.Text + #10 + 'Call stack:';

  for I:=0 to sender.Call_Stack.Count - 1 do
  begin
    csr := sender.Call_Stack[I];
    rtbState.Text := rtbState.Text + #10 + csr.CallView + ' at line ' + csr.LineNumber.ToString();
  end;

  rtbState.Text := rtbState.Text + #10 + 'Breakpoints:';
  for I:=0 to sender.Breakpoint_List.Count - 1 do
  begin
    bp := sender.Breakpoint_List[I];
    rtbState.Text := rtbState.Text +  #10 + 'Breakpoint at line ' + bp.LineNumber.ToString() +
      ', Activated = ' + bp.Activated.ToString();
  end;
end;

procedure TWinForm2.PaxScripter1_OnChangeState(sender: PaxScript.Net.PaxScripter;
  e: PaxScript.Net.ChangeStateEventArgs);
begin
	if not sender.HasErrors then
  	ShowInfo(sender, e.NewState);

  if e.NewState = ScripterState.Error then
  	MessageBox.Show('Error: ' + sender.Error_List[0].Message)
  else if e.OldState = ScripterState.Init then
  begin
  	sender.AddModule('1');
		sender.AddCode('1', rtbScript.Text);
  end;
end;

procedure TWinForm2.btnRemoveBreakpoint_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.RemoveAllBreakpoints();
	ShowInfo(paxScripter1, paxScripter1.State);
end;

procedure TWinForm2.btnAddBreakpoint_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.AddBreakpoint('1', 5);
	ShowInfo(paxScripter1, paxScripter1.State);
end;

procedure TWinForm2.btnStepOver_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.Run(RunMode.StepOver, []);
end;

procedure TWinForm2.btnTraceInto_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.Run(RunMode.TraceInto, []);
end;

procedure TWinForm2.btnRun_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.Run(RunMode.Run, []);
end;

procedure TWinForm2.btnReset_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.Reset();
end;

end.


Copyright © 2005-2024 Alexander Baranovsky. All rights reserved.