Menu

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

Download this file

587 lines (531 with data), 14.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/** 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 System.Text;
namespace JavaScript
{
public class TypeMismatchException : Exception
{
public TypeMismatchException( string msg ) : base( msg )
{
}
public TypeMismatchException( ) : base ( "Type Mismatch Exception" )
{
}
}
/// <summary>
///
/// </summary>
public class JsObject
{
public JsObject()
{
//NativeLValue lvalue = new NativeLValue( "length", new NativeValueGet( _LengthGetter ), new NativeValueSet( _LengthSetter ) );
//m_properties.Add( lvalue.Name, lvalue );
}
public virtual List<LValue> Properties
{
get
{
List<LValue> values = new List<LValue>();
foreach (LValue lv in m_properties.Values)
{
values.Add(lv);
}
return values;
}
}
public virtual string Name
{
get
{
return "object";
}
}
public virtual string TypeName
{
get
{
return Name;
}
}
public virtual void Construct(Stack<object> stk, int argCount)
{
if (argCount != 0)
{
throw new ParseError("Object() takes zero parameters");
}
}
public virtual LValue GetPropertyLValueNoCreate( string name )
{
if (m_properties.ContainsKey(name))
{
return m_properties[name];
}
if (name == "toString")
{
TypeNativeFunction nfn = new TypeNativeFunction("toString", new string[] { "val", "rad" }, new JsNativeCall(ToStrFunction));
AddLValue(new LValue(name, nfn));
return m_properties[name];
}
return null;
}
public virtual LValue GetPropertyLValue( string name )
{
if (!m_properties.ContainsKey(name))
{
m_properties.Add( name, new LValue( name, TypeUndefined.Instance ) );
}
if (name == "toString")
{
TypeNativeFunction nfn = new TypeNativeFunction("toString", new string[] { "val", "rad" }, new JsNativeCall(ToStrFunction));
AddLValue(new LValue(name, nfn));
return m_properties[name];
}
Debug.Assert(null != m_properties[name]);
return m_properties[name];
}
public void CreateProperty(string name, JsObject val)
{
LValue lvalue = GetPropertyLValueNoCreate(name);
if (lvalue == null)
{
m_properties[name] = new LValue(name, val);
}
else
{
lvalue.Set(val);
}
}
/// <summary>
/// All objects can have dynamic properties. If the property
/// isn't defined, return TypeUndefined.
/// </summary>
public virtual JsObject GetProperty( string name )
{
LValue lvalue = (LValue)m_properties[name];
if ( lvalue == null )
{
if (name == "toString")
{
TypeNativeFunction nfn = new TypeNativeFunction("toString", new string[] { "val", "rad" }, new JsNativeCall(ToStrFunction));
AddLValue(new LValue(name, nfn));
return nfn;
}
return TypeUndefined.Instance;
}
return lvalue.Get();
}
/// <summary>
/// Set a dynamic property. If the property doesn't exist,
/// create it.
/// </summary>
/// <param name="name"></param>
/// <param name="val"></param>
public virtual void SetProperty( string name, JsObject val )
{
Debug.Assert(null != val);
Debug.Assert(val != this || name == "window", "Property assignment should not resolve to self");
if ( ! m_properties.ContainsKey( name ) )
{
m_properties[name] = new LValue( name );
}
((LValue)m_properties[name]).Set( val );
}
public JsObject Dup()
{
JsObject obj = SubClassDup();
foreach (string key in m_properties.Keys)
{
LValue lvalue = (LValue)m_properties[key];
if (!obj.m_properties.ContainsKey(lvalue.Name))
{
if (lvalue is NativeLValue)
{
obj.m_properties.Add(lvalue.Name, lvalue);
}
else
{
//obj.SetProperty(key, lvalue.Get().Dup());
obj.SetProperty(key, lvalue.Get());
}
}
}
return obj;
}
public virtual JsObject SubClassDup()
{
return new JsObject();
}
/// <summary>
/// All objects except for TypeFunction should throw a type exception here.
/// </summary>
public virtual void PrepareCall( Stack<object> stk, int argCount )
{
throw new TypeMismatchException( "Not a function type" );
}
/// <summary>
/// Attempt to convert to an integer -- throws TypeMismatch if can't
/// </summary>
/// <exception cref="TypeMismatch"></exception>
public virtual JsObject ToInt()
{
return TypeNumber.NAN;
}
/// <summary>
/// Attempt to convert to a double -- throws TypeMismatch if can't
/// </summary>
/// <exception cref="TypeMismatch"></exception>
public virtual JsObject ToDouble()
{
return TypeNumber.NAN;
}
/// <summary>
/// Attempt to converto to a bool -- throws TypeMismatch if can't
/// </summary>
/// <exception cref="TypeMismatch"></exception>
public virtual JsObject ToBool()
{
//throw new TypeMismatchException( );
return TypeBool.True;
}
public virtual JsObject ToJsString()
{
throw new TypeMismatchException( );
}
public override string ToString()
{
StringBuilder props = new StringBuilder();
props.Append(Name);
props.Append('[');
foreach ( string key in m_properties.Keys )
{
LValue lval = (LValue)m_properties[key];
if (lval.Get() == this)
{
continue;
}
if (props.Length > 7)
{
props.Append(",");
}
props.Append(key + "=" + lval.ToString());
}
props.Append(']');
return props.ToString();
}
public virtual JsObject PlusOperator (JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject MinusOperator (JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject TimesOperator (JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject DivOperator (JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject ModOperator (JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject AndOperator( JsObject o)
{
//throw new TypeMismatchException( );
if (o is TypeNull || this is TypeNull || o is TypeUndefined || this is TypeUndefined)
{
return TypeBool.False;
}
return TypeBool.True;
}
public virtual JsObject BitAndOperator( JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject OrOperator( JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject BitOrOperator( JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject XorOperator( JsObject o)
{
throw new TypeMismatchException( );
}
public virtual JsObject OnesComplOperator( )
{
throw new TypeMismatchException( );
}
public virtual TypeBool NotOperator()
{
return TypeBool.False;
}
public virtual TypeBool LessThanOperator(JsObject o)
{
throw new TypeMismatchException( );
}
public virtual TypeBool LessThanEqOperator(JsObject o)
{
throw new TypeMismatchException( );
}
public virtual TypeBool GreaterThanOperator(JsObject o)
{
throw new TypeMismatchException( );
}
public virtual TypeBool GreaterThanEqOperator(JsObject o)
{
throw new TypeMismatchException( );
}
public virtual TypeBool EqualOperator(JsObject o)
{
if (o == TypeNull.Instance)
{
return TypeBool.False;
}
throw new TypeMismatchException( );
}
protected JsObject _LengthGetter(string name)
{
return new TypeInt( m_properties.Count );
}
protected void _LengthSetter(string name, JsObject val)
{
}
protected JsObject ToStrFunction(StackFrame ctx, int argCount)
{
if (argCount == 1)
{
return ctx.RValue("val").ToStr();
}
else if (argCount == 2)
{
int val = ((TypeInt)ctx.RValue("val").ToInt()).IntValue();
int radix = ((TypeInt)ctx.RValue("radix").ToInt()).IntValue();
if (radix == 10)
{
return new TypeString(val.ToString());
}
if (radix == 16)
{
return new TypeString(val.ToString("x"));
}
throw new ParseError("Unsupported radix of " + radix.ToString());
}
else
{
return ToStr();
}
}
protected virtual JsObject ToStr()
{
return new TypeString("[object]");
}
public void AddGlobalFunctions()
{
TypeNativeFunction nfn = new TypeNativeFunction( "parseInt", new string[] {"val", "radix"}, new JsNativeCall( ParseInt ) );
SetProperty( nfn.Name, nfn );
nfn = new TypeNativeFunction( "parseFloat", new string[] {"val"}, new JsNativeCall( ParseFloat ) );
SetProperty( nfn.Name, nfn );
nfn = new TypeNativeFunction( "typeof", new string[] {"val"}, new JsNativeCall( TypeOfFunction ) );
SetProperty( nfn.Name, nfn );
nfn = new TypeNativeFunction("eval", new string[] { "sourcecode" }, new JsNativeCall(EvalFunction));
SetProperty(nfn.Name, nfn);
nfn = new TypeNativeFunction("isNaN", new string[] { "val" }, new JsNativeCall(IsNanFunction));
SetProperty(nfn.Name, nfn);
nfn = new TypeNativeFunction("toString", new string[] { "val", "rad" }, new JsNativeCall(ToStrFunction));
SetProperty(nfn.Name, nfn);
nfn = new TypeNativeFunction("escape", new string[] { "str" }, new JsNativeCall(EscapeFunction));
SetProperty(nfn.Name, nfn);
nfn = new TypeNativeFunction("unescape", new string[] { "str" }, new JsNativeCall(UnescapeFunction));
SetProperty(nfn.Name, nfn);
}
protected JsObject ParseInt( StackFrame ctx, int argCount )
{
int radix = 10;
if ( argCount == 2 )
{
radix = ((TypeInt)ctx.RValue("radix").ToInt()).IntValue();
}
else if( argCount != 1 )
{
throw new ParseError( "parseInt doesn't take " + argCount + " parameters" );
}
JsObject val = ctx.RValue("val");
if ( val is TypeInt || val is TypeDouble )
{
return val.ToInt();
}
string str = val.ToString();
if (str.Length == 0)
{
return TypeNumber.NAN;
}
if (str.Length > 2 && str[0] == '0' && str[1] == 'x')
{
// hex
return new TypeInt(str, 16);
}
if (str.Length > 0 && str[0] == '0')
{
// octal
return new TypeInt(str, 8);
}
if ( Char.IsDigit(str[0]) )
{
int pos = 1;
while (pos < str.Length)
{
if (! Char.IsDigit(str[pos]) )
{
break;
}
pos++;
}
string digits = str.Substring(0, pos);
return new TypeInt(digits, radix);
}
return TypeNumber.NAN;
}
protected JsObject ParseFloat( StackFrame ctx, int argCount )
{
if ( argCount != 1 )
{
throw new ParseError( "parseFloat doesn't take " + argCount + " parameters" );
}
JsObject val = ctx.RValue("val");
if (val is TypeDouble || val is TypeInt)
{
return val.ToDouble();
}
double dval;
string str = val.ToString();
if (Double.TryParse(str, out dval))
{
return new TypeDouble(dval);
}
if (Char.IsDigit(str[0]))
{
int pos = 1;
while (pos < str.Length && (Char.IsDigit(str[pos]) || str[pos] == '.' || str[pos] == 'e'))
{
pos++;
}
str = str.Substring(0, pos);
return new TypeDouble(Double.Parse(str));
}
return TypeNumber.NAN;
}
protected virtual JsObject TypeOf()
{
return new TypeString("object");
}
protected JsObject TypeOfFunction( StackFrame ctx, int argCount )
{
Debug.Assert(argCount == 1);
return ctx.RValue("val").TypeOf();
}
protected JsObject EvalFunction(StackFrame ctx, int argCount)
{
if (argCount == 0)
{
return TypeUndefined.Instance;
}
if (argCount > 1)
{
throw new ParseError("eval only takes one argument");
}
TypeString sourcecode = (TypeString)ctx.LValue("sourcecode").Get();
Parser parser = new Parser(false);
ExecList prog = parser.Parse(sourcecode.ToString());
StackFrame[] frames = ctx.OuterContext.ToArray();
JsInterp interp = new JsInterp(this, prog, false);
JsObject ret = interp.Run(frames[frames.Length-2]);
//return ret;
return TypeUndefined.Instance;
}
protected JsObject IsNanFunction(StackFrame ctx, int argCount)
{
if (argCount != 1)
{
throw new ParseError("isNaN takes one argument");
}
JsObject val = ctx.LValue("val").Get();
if (val is TypeDouble)
{
return new TypeBool( Double.NaN == ((TypeDouble)val).DoubleVal() );
}
if (val is TypeInt)
{
return TypeBool.False;
}
return TypeBool.True;
}
protected JsObject EscapeFunction(StackFrame ctx, int argCount)
{
if (argCount != 1)
{
throw new ParseError("escape takes one argument");
}
JsObject val = ctx.LValue("str").Get();
return new TypeString(Uri.EscapeDataString(val.ToString()).Replace("'", "%27"));
}
protected JsObject UnescapeFunction(StackFrame ctx, int argCount)
{
if (argCount != 1)
{
throw new ParseError("escape takes one argument");
}
JsObject val = ctx.LValue("str").Get();
return new TypeString(Uri.UnescapeDataString(val.ToString()).Replace("%27", "'"));
}
/// <summary>
/// Used by sub classes to add properties
/// </summary>
/// <param name="lvalue">the property to add</param>
protected void AddLValue(LValue lvalue)
{
m_properties[lvalue.Name] = lvalue;
}
public override bool Equals(object obj)
{
if (!(obj is JsObject))
{
return false;
}
return ((TypeBool)this.EqualOperator((JsObject)obj)).Value();
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
internal Dictionary<string, LValue> _Properties
{
get { return m_properties; }
}
protected Dictionary<string, LValue> m_properties = new Dictionary<string,LValue>();
}
}
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.