using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using JavaScript;
namespace JsTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.TextBox txtOut;
private System.Windows.Forms.Button btnEval;
private System.Windows.Forms.ListBox lstStack;
private System.Windows.Forms.ListBox lstContext;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtNextOp;
private System.Windows.Forms.TextBox txtNextArg;
private System.Windows.Forms.Button btnStep;
private System.Windows.Forms.TextBox txtLine;
private System.Windows.Forms.Button btnDis;
private System.Windows.Forms.TextBox txtHint;
private TextBox txtPos;
private Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtSource = new System.Windows.Forms.TextBox();
this.txtOut = new System.Windows.Forms.TextBox();
this.btnEval = new System.Windows.Forms.Button();
this.lstStack = new System.Windows.Forms.ListBox();
this.lstContext = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btnStep = new System.Windows.Forms.Button();
this.txtLine = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.txtNextOp = new System.Windows.Forms.TextBox();
this.txtNextArg = new System.Windows.Forms.TextBox();
this.btnDis = new System.Windows.Forms.Button();
this.txtHint = new System.Windows.Forms.TextBox();
this.txtPos = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtSource
//
this.txtSource.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtSource.Location = new System.Drawing.Point(12, 11);
this.txtSource.Multiline = true;
this.txtSource.Name = "txtSource";
this.txtSource.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtSource.Size = new System.Drawing.Size(549, 200);
this.txtSource.TabIndex = 0;
this.txtSource.Text = "function def(a)\r\n{\r\n\tthis.val = a;\r\n}\r\nvar x = new def(2);\r\nx;";
//
// txtOut
//
this.txtOut.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtOut.Location = new System.Drawing.Point(96, 220);
this.txtOut.Name = "txtOut";
this.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtOut.Size = new System.Drawing.Size(462, 28);
this.txtOut.TabIndex = 1;
//
// btnEval
//
this.btnEval.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnEval.Location = new System.Drawing.Point(12, 217);
this.btnEval.Name = "btnEval";
this.btnEval.Size = new System.Drawing.Size(78, 32);
this.btnEval.TabIndex = 2;
this.btnEval.Text = "Run";
this.btnEval.Click += new System.EventHandler(this.btnEval_Click);
//
// lstStack
//
this.lstStack.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstStack.ItemHeight = 21;
this.lstStack.Location = new System.Drawing.Point(290, 357);
this.lstStack.Name = "lstStack";
this.lstStack.Size = new System.Drawing.Size(268, 67);
this.lstStack.TabIndex = 4;
//
// lstContext
//
this.lstContext.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstContext.ItemHeight = 21;
this.lstContext.Location = new System.Drawing.Point(12, 357);
this.lstContext.Name = "lstContext";
this.lstContext.Size = new System.Drawing.Size(268, 67);
this.lstContext.TabIndex = 5;
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label2.Location = new System.Drawing.Point(62, 324);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(137, 30);
this.label2.TabIndex = 6;
this.label2.Text = "Context";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.label3.Location = new System.Drawing.Point(355, 324);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(137, 30);
this.label3.TabIndex = 7;
this.label3.Text = "Stack";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnStep
//
this.btnStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnStep.Location = new System.Drawing.Point(12, 289);
this.btnStep.Name = "btnStep";
this.btnStep.Size = new System.Drawing.Size(78, 32);
this.btnStep.TabIndex = 8;
this.btnStep.Text = "Step";
this.btnStep.Click += new System.EventHandler(this.button1_Click);
//
// txtLine
//
this.txtLine.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtLine.Location = new System.Drawing.Point(167, 255);
this.txtLine.Name = "txtLine";
this.txtLine.Size = new System.Drawing.Size(88, 28);
this.txtLine.TabIndex = 9;
this.txtLine.Text = "0";
//
// label4
//
this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.label4.Location = new System.Drawing.Point(62, 253);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(99, 30);
this.label4.TabIndex = 10;
this.label4.Text = "Line:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtNextOp
//
this.txtNextOp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtNextOp.Location = new System.Drawing.Point(118, 289);
this.txtNextOp.Name = "txtNextOp";
this.txtNextOp.Size = new System.Drawing.Size(137, 28);
this.txtNextOp.TabIndex = 15;
//
// txtNextArg
//
this.txtNextArg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtNextArg.Location = new System.Drawing.Point(261, 255);
this.txtNextArg.Name = "txtNextArg";
this.txtNextArg.Size = new System.Drawing.Size(137, 28);
this.txtNextArg.TabIndex = 16;
//
// btnDis
//
this.btnDis.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnDis.Location = new System.Drawing.Point(483, 289);
this.btnDis.Name = "btnDis";
this.btnDis.Size = new System.Drawing.Size(78, 32);
this.btnDis.TabIndex = 19;
this.btnDis.Text = "Dis";
this.btnDis.Click += new System.EventHandler(this.btnDis_Click);
//
// txtHint
//
this.txtHint.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtHint.Location = new System.Drawing.Point(404, 255);
this.txtHint.Name = "txtHint";
this.txtHint.Size = new System.Drawing.Size(99, 28);
this.txtHint.TabIndex = 20;
//
// txtPos
//
this.txtPos.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.txtPos.Location = new System.Drawing.Point(318, 289);
this.txtPos.Name = "txtPos";
this.txtPos.Size = new System.Drawing.Size(88, 28);
this.txtPos.TabIndex = 21;
this.txtPos.Text = "0";
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.label1.Location = new System.Drawing.Point(261, 287);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(51, 30);
this.label1.TabIndex = 22;
this.label1.Text = "Pos:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(9, 21);
this.ClientSize = new System.Drawing.Size(570, 436);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtPos);
this.Controls.Add(this.txtHint);
this.Controls.Add(this.txtNextArg);
this.Controls.Add(this.txtNextOp);
this.Controls.Add(this.txtLine);
this.Controls.Add(this.txtOut);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.btnDis);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnStep);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.lstContext);
this.Controls.Add(this.lstStack);
this.Controls.Add(this.btnEval);
this.Font = new System.Drawing.Font("Century Gothic", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "frmMain";
this.Text = "JavaScript Console";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnEval_Click(object sender, System.EventArgs e)
{
try
{
if ( m_stepping )
{
while ( ! m_javascript.Completed() )
{
button1_Click(null,null);
}
m_stepping = false;
return;
}
JsObject obj = m_javascript.Eval( txtSource.Text );
txtOut.Text = obj.ToString();
}
catch ( Exception ex )
{
txtOut.Text = ex.ToString();
}
}
protected JsEngine m_javascript = new JsEngine();
protected bool m_stepping = false;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if ( ! m_stepping )
{
m_stepping = true;
m_javascript.Start( txtSource.Text, 1 );
txtOut.Text = "";
}
Command cmd = m_javascript.CurrentCommand;
int pos = m_javascript.Position;
m_javascript.Step();
SetCtxStkLists();
if ( m_javascript.Completed() )
{
txtOut.Text = m_javascript.ReturnValue.ToString();
m_stepping = false;
}
txtHint.Text = cmd.DebugHint;
txtLine.Text = cmd.LineNumber.ToString();
txtNextOp.Text = cmd.OpCode.ToString();
txtNextArg.Text = cmd.Arg == null ? "null" : cmd.Arg.ToString();
txtPos.Text = pos.ToString();
}
catch ( Exception ex )
{
MessageBox.Show( this, ex.ToString() );
}
}
private void SetCtxStkLists()
{
lstStack.Items.Clear();
Stack<object> stk = m_javascript.DebugGetStack();
object[] stkitms = stk.ToArray();
for ( int x = 0; x < stk.Count; x++ )
{
lstStack.Items.Add( stkitms[x].ToString() );
}
lstContext.Items.Clear();
StackFrame ctx = m_javascript.DebugGetContext();
object[] ctxitms = ctx.ToArray();
for ( int x = 0; x < ctx.Count; x++ )
{
lstContext.Items.Add( ctxitms[x].ToString() );
}
}
private void btnDis_Click(object sender, System.EventArgs e)
{
try
{
Parser parser = new Parser(true);
ExecList code = parser.Parse(txtSource.Text, 1);
frmDIS frm = new frmDIS();
frm.SetCode(code);
frm.Show();
}
catch (Exception exc)
{
txtOut.Text = exc.ToString();
}
}
private void frmMain_Load(object sender, System.EventArgs e)
{
m_javascript.AddGlobalVar( "document", new JavaScript.UnitTest.TestDocument() );
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}