Menu

[r6]: / trunk / JavaScript / JsEngine.cs  Maximize  Restore  History

Download this file

261 lines (229 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/** JavaScript -- DOT NET Javascript Library
* Copyright (C) 2005 John Garrison
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using JavaScript.Debugger;
namespace JavaScript
{
/// <summary>
///
/// </summary>
public class JsEngine
{
public JsEngine( JsObject global )
{
global.AddGlobalFunctions();
m_globals.Add( global );
AddGlobalVar( "Object", new JsObject() );
AddGlobalVar( "String", new TypeString("") );
AddGlobalVar( "Array", new TypeArray() );
AddGlobalVar( "Date", new TypeDate() );
AddGlobalVar( "Math", new TypeMath() );
AddGlobalVar( "Number", new TypeNumber() );
AddGlobalVar( "RegExp", new TypeRegExp() );
AddGlobalVar( "Function", new TypeFunctionObject() );
}
public JsEngine( ) : this( new JsObject() )
{
}
public JsObject Eval( string scr )
{
return Eval( scr, 1 );
}
public JsObject Eval( string script, int startline )
{
return Eval(script, null, startline);
//Debug.Assert(0 != startline, "startline is one based");
//Parser parser = new Parser(m_strictParsing);
//ExecList code = parser.Parse(script, startline);
//JsInterp interp = new JsInterp( (JsObject)m_globals[0], code );
//for ( int x = 1; x < m_globals.Count; x++ )
//{
// interp.Context.EnterGlobalContext( (JsObject)m_globals[x] );
//}
//return interp.Run( );
}
public JsObject Eval( string src, JsObject context, int startline )
{
Debug.Assert(0 != startline, "startline is one based");
Parser parser = new Parser(m_strictParsing);
ExecList code = parser.Parse(src, startline);
JsInterp interp = new JsInterp( (JsObject)m_globals[0], code, m_strictTyping );
for ( int x = 1; x < m_globals.Count; x++ )
{
interp.Context.EnterGlobalContext( (JsObject)m_globals[x] );
}
if (context != null)
{
interp.Context.EnterContext(context);
}
if (m_debugger != null)
{
if (m_debugger.Visible)
{
return m_debugger.PushEval(interp);
}
else
{
m_debugger.Dispose();
m_debugger = null;
}
}
try
{
return interp.Run();
}
catch (DebuggerException de)
{
m_debugger = new DebugWindow(this, de);
m_debugger.ShowDialog();
JsObject ret = m_debugger.ReturnValue;
m_debugger.Dispose();
m_debugger = null;
return ret;
}
}
public void Start( string script, int startline )
{
Debug.Assert(0 != startline, "startline is one based");
m_complete = false;
Parser parser = new Parser(m_strictParsing);
ExecList code = parser.Parse( script, startline );
m_interp = new JsInterp( (JsObject)m_globals[0], code, m_strictTyping );
for ( int x = 1; x < m_globals.Count; x++ )
{
m_interp.Context.EnterGlobalContext( (JsObject)m_globals[x] );
}
m_interp.PrepareToRun();
}
public void Step()
{
m_complete = m_interp.Step();
}
public bool Completed()
{
return m_complete;
}
public JsObject ReturnValue
{
get
{
return m_interp.ReturnValue();
}
}
public bool StrictParsing
{
get { return m_strictParsing; }
set { m_strictParsing = value; }
}
public bool StrictTyping
{
get { return m_strictTyping; }
set { m_strictTyping = value; }
}
public bool IsInDebugger
{
get { return m_debugger != null; }
}
public void EndDebugging()
{
if (m_debugger == null)
{
return;
}
m_debugger.EndSession();
}
public JsInterp PrepareInterp(string src, int startline, JsObject context)
{
Debug.Assert(0 != startline, "startline is one based");
Parser parser = new Parser(m_strictParsing);
ExecList code = parser.Parse(src, startline);
JsInterp interp = new JsInterp((JsObject)m_globals[0], code, m_strictTyping);
for (int x = 1; x < m_globals.Count; x++)
{
interp.Context.EnterGlobalContext((JsObject)m_globals[x]);
}
if (context != null)
{
interp.Context.EnterContext(context);
}
return interp;
}
public Command CurrentCommand
{
get
{
return m_interp.DebugGetCurrentCommand();
}
}
public int Position
{
get
{
return m_interp.Position;
}
}
/// <summary>
/// Add the object to the global namespace. Same effect as "var doc = new Whatever();"
/// This allows external programs to define global objects, such as "document" or "window".
/// </summary>
/// <param name="obj"></param>
public void AddGlobalVar( string name, JsObject obj )
{
((JsObject)m_globals[0]).SetProperty( name, obj );
}
public void AddGlobalContext( JsObject ctx )
{
m_globals.Add( ctx );
}
public Stack<object> DebugGetStack()
{
return m_interp.DebugGetStack();
}
public StackFrame DebugGetContext()
{
return m_interp.Context;
}
protected List<JsObject> m_globals = new List<JsObject>();
protected JsInterp m_interp;
protected bool m_complete;
protected bool m_strictParsing = true;
protected bool m_strictTyping = false;
protected DebugWindow m_debugger;
}
public class DebuggerException : Exception
{
//private JsEngine m_engine;
private JsInterp m_interp;
public DebuggerException(/*JsEngine engine, */JsInterp interp)
: base()
{
//m_engine = engine;
m_interp = interp;
}
//public JsEngine JsEngine
//{
// get { return m_engine; }
//}
public JsInterp JsInterp
{
get { return m_interp; }
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.