This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathScriptRuntime.cs
More file actions
2610 lines (2333 loc) · 97.9 KB
/
ScriptRuntime.cs
File metadata and controls
2610 lines (2333 loc) · 97.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <license file="ScriptRuntime.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using System.Resources;
using System.Globalization;
using System.Text;
using System.Threading;
using EcmaScript.NET;
using EcmaScript.NET.Types;
using EcmaScript.NET.Types.RegExp;
using EcmaScript.NET.Types.E4X;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
/// <summary> This is the class that implements the runtime.
///
/// </summary>
public class ScriptRuntime
{
/// <summary> No instances should be created.</summary>
protected internal ScriptRuntime ()
{
}
public const int MAXSTACKSIZE = 1000;
private const string XML_INIT_CLASS = "EcmaScript.NET.Xml.Impl.XMLLib";
private static readonly object LIBRARY_SCOPE_KEY = new object ();
public static bool IsNativeRuntimeType (Type cl)
{
if (cl.IsPrimitive) {
return (cl != typeof (char));
}
else {
return (cl == typeof (string) || cl == typeof (bool)
|| CliHelper.IsNumberType (cl)
|| typeof (IScriptable).IsAssignableFrom (cl));
}
}
public static ScriptableObject InitStandardObjects (Context cx, ScriptableObject scope, bool zealed)
{
if (scope == null) {
scope = new BuiltinObject ();
}
scope.AssociateValue (LIBRARY_SCOPE_KEY, scope);
BaseFunction.Init (scope, zealed);
BuiltinObject.Init (scope, zealed);
IScriptable objectProto = ScriptableObject.GetObjectPrototype (scope);
// Function.prototype.__proto__ should be Object.prototype
IScriptable functionProto = ScriptableObject.GetFunctionPrototype (scope);
functionProto.SetPrototype (objectProto);
// Set the prototype of the object passed in if need be
if (scope.GetPrototype () == null)
scope.SetPrototype (objectProto);
// must precede NativeGlobal since it's needed therein
BuiltinError.Init (scope, zealed);
BuiltinGlobal.Init (cx, scope, zealed);
if (scope is BuiltinGlobalObject) {
((BuiltinGlobalObject)scope).Init (scope, zealed);
}
BuiltinArray.Init (scope, zealed);
BuiltinString.Init (scope, zealed);
BuiltinBoolean.Init (scope, zealed);
BuiltinNumber.Init (scope, zealed);
BuiltinDate.Init (scope, zealed);
BuiltinMath.Init (scope, zealed);
BuiltinWith.Init (scope, zealed);
BuiltinCall.Init (scope, zealed);
BuiltinScript.Init (scope, zealed);
BuiltinRegExp.Init (scope, zealed);
if (cx.HasFeature (Context.Features.E4x)) {
Types.E4X.XMLLib.Init (scope, zealed);
}
Continuation.Init (scope, zealed);
if (cx.HasFeature (Context.Features.NonEcmaItObject)) {
InitItObject (cx, scope);
}
return scope;
}
static void InitItObject (Context cx, ScriptableObject scope) {
BuiltinObject itObj = new BuiltinObject ();
itObj.SetPrototype (scope);
itObj.DefineProperty ("color", Undefined.Value, ScriptableObject.PERMANENT);
itObj.DefineProperty ("height", Undefined.Value, ScriptableObject.PERMANENT);
itObj.DefineProperty ("width", Undefined.Value, ScriptableObject.PERMANENT);
itObj.DefineProperty ("funny", Undefined.Value, ScriptableObject.PERMANENT);
itObj.DefineProperty ("array", Undefined.Value, ScriptableObject.PERMANENT);
itObj.DefineProperty ("rdonly", Undefined.Value, ScriptableObject.READONLY);
scope.DefineProperty ("it", itObj, ScriptableObject.PERMANENT);
}
public static ScriptableObject getLibraryScopeOrNull (IScriptable scope)
{
ScriptableObject libScope;
libScope = (ScriptableObject)ScriptableObject.GetTopScopeValue (scope, LIBRARY_SCOPE_KEY);
return libScope;
}
// It is public so NativeRegExp can access it .
public static bool isJSLineTerminator (int c)
{
// Optimization for faster check for eol character:
// they do not have 0xDFD0 bits set
if ((c & 0xDFD0) != 0) {
return false;
}
return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
}
/// <summary> Helper function for builtin objects that use the varargs form.
/// ECMA function formal arguments are undefined if not supplied;
/// this function pads the argument array out to the expected
/// length, if necessary.
/// </summary>
public static object [] padArguments (object [] args, int count)
{
if (count < args.Length)
return args;
int i;
object [] result = new object [count];
for (i = 0; i < args.Length; i++) {
result [i] = args [i];
}
for (; i < count; i++) {
result [i] = Undefined.Value;
}
return result;
}
public static string escapeString (string s)
{
return escapeString (s, '"');
}
/// <summary> For escaping strings printed by object and array literals; not quite
/// the same as 'escape.'
/// </summary>
public static string escapeString (string s, char escapeQuote)
{
if (!(escapeQuote == '"' || escapeQuote == '\''))
Context.CodeBug ();
System.Text.StringBuilder sb = null;
for (int i = 0, L = s.Length; i != L; ++i) {
int c = s [i];
if (' ' <= c && c <= '~' && c != escapeQuote && c != '\\') {
// an ordinary print character (like C isprint()) and not "
// or \ .
if (sb != null) {
sb.Append ((char)c);
}
continue;
}
if (sb == null) {
sb = new System.Text.StringBuilder (L + 3);
sb.Append (s);
sb.Length = i;
}
int escape = -1;
switch (c) {
case '\b':
escape = 'b';
break;
case '\f':
escape = 'f';
break;
case '\n':
escape = 'n';
break;
case '\r':
escape = 'r';
break;
case '\t':
escape = 't';
break;
case 0xb:
escape = 'v';
break; // Java lacks \v.
case ' ':
escape = ' ';
break;
case '\\':
escape = '\\';
break;
}
if (escape >= 0) {
// an \escaped sort of character
sb.Append ('\\');
sb.Append ((char)escape);
}
else if (c == escapeQuote) {
sb.Append ('\\');
sb.Append (escapeQuote);
}
else {
int hexSize;
if (c < 256) {
// 2-digit hex
sb.Append ("\\x");
hexSize = 2;
}
else {
// Unicode.
sb.Append ("\\u");
hexSize = 4;
}
// append hexadecimal form of c left-padded with 0
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) {
int digit = 0xf & (c >> shift);
int hc = (digit < 10) ? '0' + digit : 'a' - 10 + digit;
sb.Append ((char)hc);
}
}
}
return (sb == null) ? s : sb.ToString ();
}
internal static bool isValidIdentifierName (string s)
{
int L = s.Length;
if (L == 0)
return false;
if (!(char.IsLetter (s [0]) || s [0].CompareTo ('$') == 0 || s [0].CompareTo ('_') == 0))
return false;
for (int i = 1; i != L; ++i) {
if (!TokenStream.IsJavaIdentifierPart (s [i]))
return false;
}
return !TokenStream.isKeyword (s);
}
internal static string DefaultObjectToString (IScriptable obj)
{
return "[object " + obj.ClassName + ']';
}
internal static string uneval (Context cx, IScriptable scope, object value)
{
if (value == null) {
return "null";
}
if (value == Undefined.Value) {
return "undefined";
}
if (value is string) {
string escaped = escapeString ((string)value);
System.Text.StringBuilder sb = new System.Text.StringBuilder (escaped.Length + 2);
sb.Append ('\"');
sb.Append (escaped);
sb.Append ('\"');
return sb.ToString ();
}
if (CliHelper.IsNumber (value)) {
double d = Convert.ToDouble (value);
if (d == 0 && 1 / d < 0) {
return "-0";
}
return ScriptConvert.ToString (d);
}
if (value is bool) {
return ScriptConvert.ToString (value);
}
if (value is IScriptable) {
IScriptable obj = (IScriptable)value;
object v = ScriptableObject.GetProperty (obj, "toSource");
if (v is IFunction) {
IFunction f = (IFunction)v;
return ScriptConvert.ToString (f.Call (cx, scope, obj, EmptyArgs));
}
return ScriptConvert.ToString (value);
}
WarnAboutNonJSObject (value);
return value.ToString ();
}
internal static string defaultObjectToSource (Context cx, IScriptable scope, IScriptable thisObj, object [] args)
{
using (Helpers.StackOverflowVerifier sov = new Helpers.StackOverflowVerifier (1024)) {
bool toplevel, iterating;
if (cx.iterating == null) {
toplevel = true;
iterating = false;
cx.iterating = new ObjToIntMap (31);
}
else {
toplevel = false;
iterating = cx.iterating.has (thisObj);
}
System.Text.StringBuilder result = new System.Text.StringBuilder (128);
if (toplevel) {
result.Append ("(");
}
result.Append ('{');
// Make sure cx.iterating is set to null when done
// so we don't leak memory
try {
if (!iterating) {
cx.iterating.intern (thisObj); // stop recursion.
object [] ids = thisObj.GetIds ();
for (int i = 0; i < ids.Length; i++) {
if (i > 0)
result.Append (", ");
object id = ids [i];
object value;
if (id is int) {
int intId = ((int)id);
value = thisObj.Get (intId, thisObj);
result.Append (intId);
}
else {
string strId = (string)id;
value = thisObj.Get (strId, thisObj);
if (ScriptRuntime.isValidIdentifierName (strId)) {
result.Append (strId);
}
else {
result.Append ('\'');
result.Append (ScriptRuntime.escapeString (strId, '\''));
result.Append ('\'');
}
}
result.Append (':');
result.Append (ScriptRuntime.uneval (cx, scope, value));
}
}
}
finally {
if (toplevel) {
cx.iterating = null;
}
}
result.Append ('}');
if (toplevel) {
result.Append (')');
}
return result.ToString ();
}
}
public static IScriptable NewObject (Context cx, IScriptable scope, string constructorName, object [] args)
{
scope = ScriptableObject.GetTopLevelScope (scope);
IFunction ctor = getExistingCtor (cx, scope, constructorName);
if (args == null) {
args = ScriptRuntime.EmptyArgs;
}
return ctor.Construct (cx, scope, args);
}
// TODO: this is until setDefaultNamespace will learn how to store NS
// TODO: properly and separates namespace form Scriptable.get etc.
private const string DEFAULT_NS_TAG = "__default_namespace__";
public static object setDefaultNamespace (object ns, Context cx)
{
IScriptable scope = cx.currentActivationCall;
if (scope == null) {
scope = getTopCallScope (cx);
}
XMLLib xmlLib = CurrentXMLLib (cx);
object obj = xmlLib.ToDefaultXmlNamespace (cx, ns);
// TODO: this should be in separated namesapce from Scriptable.get/put
if (!scope.Has (DEFAULT_NS_TAG, scope)) {
// TODO: this is racy of cause
ScriptableObject.DefineProperty (scope, DEFAULT_NS_TAG, obj, ScriptableObject.PERMANENT | ScriptableObject.DONTENUM);
}
else {
scope.Put (DEFAULT_NS_TAG, scope, obj);
}
return Undefined.Value;
}
public static object searchDefaultNamespace (Context cx)
{
IScriptable scope = cx.currentActivationCall;
if (scope == null) {
scope = getTopCallScope (cx);
}
object nsObject;
for (; ; ) {
IScriptable parent = scope.ParentScope;
if (parent == null) {
nsObject = ScriptableObject.GetProperty (scope, DEFAULT_NS_TAG);
if (nsObject == UniqueTag.NotFound) {
return null;
}
break;
}
nsObject = scope.Get (DEFAULT_NS_TAG, scope);
if (nsObject != UniqueTag.NotFound) {
break;
}
scope = parent;
}
return nsObject;
}
public static object getTopLevelProp (IScriptable scope, string id)
{
scope = ScriptableObject.GetTopLevelScope (scope);
return ScriptableObject.GetProperty (scope, id);
}
internal static IFunction getExistingCtor (Context cx, IScriptable scope, string constructorName)
{
object ctorVal = ScriptableObject.GetProperty (scope, constructorName);
if (ctorVal is IFunction) {
return (IFunction)ctorVal;
}
if (ctorVal == UniqueTag.NotFound) {
throw Context.ReportRuntimeErrorById ("msg.ctor.not.found", constructorName);
}
else {
throw Context.ReportRuntimeErrorById ("msg.not.ctor", constructorName);
}
}
/// <summary> Return -1L if str is not an index or the index value as lower 32
/// bits of the result.
/// </summary>
private static long indexFromString (string str)
{
// The length of the decimal string representation of
// Integer.MAX_VALUE, 2147483647
const int MAX_VALUE_LENGTH = 10;
int len = str.Length;
if (len > 0) {
int i = 0;
bool negate = false;
int c = str [0];
if (c == '-') {
if (len > 1) {
c = str [1];
i = 1;
negate = true;
}
}
c -= '0';
if (0 <= c && c <= 9 && len <= (negate ? MAX_VALUE_LENGTH + 1 : MAX_VALUE_LENGTH)) {
// Use negative numbers to accumulate index to handle
// Integer.MIN_VALUE that is greater by 1 in absolute value
// then Integer.MAX_VALUE
int index = -c;
int oldIndex = 0;
i++;
if (index != 0) {
// Note that 00, 01, 000 etc. are not indexes
while (i != len && 0 <= (c = str [i] - '0') && c <= 9) {
oldIndex = index;
index = 10 * index - c;
i++;
}
}
// Make sure all characters were consumed and that it couldn't
// have overflowed.
if (i == len && (oldIndex > (int.MinValue / 10) || (oldIndex == (int.MinValue / 10) && c <= (negate ? -(int.MinValue % 10) : (int.MaxValue % 10))))) {
return unchecked ((int)0xFFFFFFFFL) & (negate ? index : -index);
}
}
}
return -1L;
}
/// <summary> If str is a decimal presentation of Uint32 value, return it as long.
/// Othewise return -1L;
/// </summary>
public static long testUint32String (string str)
{
// The length of the decimal string representation of
// UINT32_MAX_VALUE, 4294967296
const int MAX_VALUE_LENGTH = 10;
int len = str.Length;
if (1 <= len && len <= MAX_VALUE_LENGTH) {
int c = str [0];
c -= '0';
if (c == 0) {
// Note that 00,01 etc. are not valid Uint32 presentations
return (len == 1) ? 0L : -1L;
}
if (1 <= c && c <= 9) {
long v = c;
for (int i = 1; i != len; ++i) {
c = str [i] - '0';
if (!(0 <= c && c <= 9)) {
return -1;
}
v = 10 * v + c;
}
// Check for overflow
if ((ulong)v >> 32 == 0) {
return v;
}
}
}
return -1;
}
/// <summary> If s represents index, then return index value wrapped as Integer
/// and othewise return s.
/// </summary>
internal static object getIndexObject (string s)
{
long indexTest = indexFromString (s);
if (indexTest >= 0) {
return (int)indexTest;
}
return s;
}
/// <summary> If d is exact int value, return its value wrapped as Integer
/// and othewise return d converted to String.
/// </summary>
internal static object getIndexObject (double d)
{
int i = (int)d;
if ((double)i == d) {
return (int)i;
}
return ScriptConvert.ToString (d);
}
/// <summary> If ScriptConvert.ToString(id) is a decimal presentation of int32 value, then id
/// is index. In this case return null and make the index available
/// as ScriptRuntime.lastIndexResult(cx). Otherwise return ScriptConvert.ToString(id).
/// </summary>
internal static string ToStringIdOrIndex (Context cx, object id)
{
if (CliHelper.IsNumber (id)) {
double d = Convert.ToDouble (id);
int index = (int)d;
if (((double)index) == d) {
storeIndexResult (cx, index);
return null;
}
return ScriptConvert.ToString (id);
}
else {
string s;
if (id is string) {
s = ((string)id);
}
else {
s = ScriptConvert.ToString (id);
}
long indexTest = indexFromString (s);
if (indexTest >= 0) {
storeIndexResult (cx, (int)indexTest);
return null;
}
return s;
}
}
/// <summary> Call obj.[[Get]](id)</summary>
public static object getObjectElem (object obj, object elem, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefReadError (obj, elem);
}
return getObjectElem (sobj, elem, cx);
}
public static object getObjectElem (IScriptable obj, object elem, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
return xmlObject.EcmaGet (cx, elem);
}
object result;
string s = ScriptRuntime.ToStringIdOrIndex (cx, elem);
if (s == null) {
int index = lastIndexResult (cx);
result = ScriptableObject.GetProperty (obj, index);
}
else {
result = ScriptableObject.GetProperty (obj, s);
}
if (result == UniqueTag.NotFound) {
result = Undefined.Value;
}
return result;
}
/// <summary> Version of getObjectElem when elem is a valid JS identifier name.</summary>
public static object getObjectProp (object obj, string property, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefReadError (obj, property);
}
return getObjectProp (sobj, property, cx);
}
public static object getObjectProp (IScriptable obj, string property, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
return xmlObject.EcmaGet (cx, property);
}
object result = ScriptableObject.GetProperty (obj, property);
if (result == UniqueTag.NotFound) {
result = Undefined.Value;
}
return result;
}
/*
* A cheaper and less general version of the above for well-known argument
* types.
*/
public static object getObjectIndex (object obj, double dblIndex, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefReadError (obj, ScriptConvert.ToString (dblIndex));
}
int index = (int)dblIndex;
if ((double)index == dblIndex) {
return getObjectIndex (sobj, index, cx);
}
else {
string s = ScriptConvert.ToString (dblIndex);
return getObjectProp (sobj, s, cx);
}
}
public static object getObjectIndex (IScriptable obj, int index, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
return xmlObject.EcmaGet (cx, (object)index);
}
object result = ScriptableObject.GetProperty (obj, index);
if (result == UniqueTag.NotFound) {
result = Undefined.Value;
}
return result;
}
/*
* Call obj.[[Put]](id, value)
*/
public static object setObjectElem (object obj, object elem, object value, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefWriteError (obj, elem, value);
}
return setObjectElem (sobj, elem, value, cx);
}
public static object setObjectElem (IScriptable obj, object elem, object value, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
xmlObject.EcmaPut (cx, elem, value);
return value;
}
string s = ScriptRuntime.ToStringIdOrIndex (cx, elem);
if (s == null) {
int index = lastIndexResult (cx);
ScriptableObject.PutProperty (obj, index, value);
}
else {
ScriptableObject.PutProperty (obj, s, value);
}
return value;
}
/// <summary> Version of setObjectElem when elem is a valid JS identifier name.</summary>
public static object setObjectProp (object obj, string property, object value, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefWriteError (obj, property, value);
}
return setObjectProp (sobj, property, value, cx);
}
public static object setObjectProp (IScriptable obj, string property, object value, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
xmlObject.EcmaPut (cx, property, value);
}
else {
return ScriptableObject.PutProperty (obj, property, value);
}
return value;
}
/*
* A cheaper and less general version of the above for well-known argument
* types.
*/
public static object setObjectIndex (object obj, double dblIndex, object value, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
throw UndefWriteError (obj, Convert.ToString (dblIndex), value);
}
int index = (int)dblIndex;
if ((double)index == dblIndex) {
return setObjectIndex (sobj, index, value, cx);
}
else {
string s = ScriptConvert.ToString (dblIndex);
return setObjectProp (sobj, s, value, cx);
}
}
public static object setObjectIndex (IScriptable obj, int index, object value, Context cx)
{
if (obj is XMLObject) {
XMLObject xmlObject = (XMLObject)obj;
xmlObject.EcmaPut (cx, (object)index, value);
}
else {
return ScriptableObject.PutProperty (obj, index, value);
}
return value;
}
public static bool deleteObjectElem (IScriptable target, object elem, Context cx)
{
bool result;
if (target is XMLObject) {
XMLObject xmlObject = (XMLObject)target;
result = xmlObject.EcmaDelete (cx, elem);
}
else {
string s = ScriptRuntime.ToStringIdOrIndex (cx, elem);
if (s == null) {
int index = lastIndexResult (cx);
result = ScriptableObject.DeleteProperty (target, index);
}
else {
result = ScriptableObject.DeleteProperty (target, s);
}
}
return result;
}
public static bool hasObjectElem (IScriptable target, object elem, Context cx)
{
bool result;
if (target is XMLObject) {
XMLObject xmlObject = (XMLObject)target;
result = xmlObject.EcmaHas (cx, elem);
}
else {
string s = ScriptRuntime.ToStringIdOrIndex (cx, elem);
if (s == null) {
int index = lastIndexResult (cx);
result = ScriptableObject.HasProperty (target, index);
}
else {
result = ScriptableObject.HasProperty (target, s);
}
}
return result;
}
public static object refGet (IRef rf, Context cx)
{
return rf.Get (cx);
}
public static object refSet (IRef rf, object value, Context cx)
{
return rf.Set (cx, value);
}
public static object refDel (IRef rf, Context cx)
{
return rf.Delete (cx);
}
internal static bool isSpecialProperty (string s)
{
return s.Equals ("__proto__") || s.Equals ("__parent__");
}
public static IRef specialRef (object obj, string specialProperty, Context cx)
{
return SpecialRef.createSpecial (cx, obj, specialProperty);
}
/// <summary> The delete operator
///
/// See ECMA 11.4.1
///
/// In ECMA 0.19, the description of the delete operator (11.4.1)
/// assumes that the [[Delete]] method returns a value. However,
/// the definition of the [[Delete]] operator (8.6.2.5) does not
/// define a return value. Here we assume that the [[Delete]]
/// method doesn't return a value.
/// </summary>
public static object delete (object obj, object id, Context cx)
{
IScriptable sobj = ScriptConvert.ToObjectOrNull (cx, obj);
if (sobj == null) {
string idStr = (id == null) ? "null" : id.ToString ();
throw TypeErrorById ("msg.undef.prop.delete", ScriptConvert.ToString (obj), idStr);
}
bool result = deleteObjectElem (sobj, id, cx);
return result;
}
/// <summary> Looks up a name in the scope chain and returns its value.</summary>
public static object name (Context cx, IScriptable scope, string name)
{
IScriptable parent = scope.ParentScope;
if (parent == null) {
object result = topScopeName (cx, scope, name);
if (result == UniqueTag.NotFound) {
throw NotFoundError (scope, name);
}
return result;
}
return nameOrFunction (cx, scope, parent, name, false);
}
private static object nameOrFunction (Context cx, IScriptable scope, IScriptable parentScope, string name, bool asFunctionCall)
{
object result;
IScriptable thisObj = scope; // It is used only if asFunctionCall==true.
XMLObject firstXMLObject = null;
for (; ; ) {
if (scope is BuiltinWith) {
IScriptable withObj = scope.GetPrototype ();
if (withObj is XMLObject) {
XMLObject xmlObj = (XMLObject)withObj;
if (xmlObj.EcmaHas (cx, name)) {
// function this should be the target object of with
thisObj = xmlObj;
result = xmlObj.EcmaGet (cx, name);
break;
}
if (firstXMLObject == null) {
firstXMLObject = xmlObj;
}
}
else {
result = ScriptableObject.GetProperty (withObj, name);
if (result != UniqueTag.NotFound) {
// function this should be the target object of with
thisObj = withObj;
break;
}
}
}
else if (scope is BuiltinCall) {
// NativeCall does not prototype chain and Scriptable.get
// can be called directly.
result = scope.Get (name, scope);
if (result != UniqueTag.NotFound) {
if (asFunctionCall) {
// ECMA 262 requires that this for nested funtions
// should be top scope
thisObj = ScriptableObject.GetTopLevelScope (parentScope);
}
break;
}
}
else {
// Can happen if embedding decided that nested
// scopes are useful for what ever reasons.
result = ScriptableObject.GetProperty (scope, name);
if (result != UniqueTag.NotFound) {
thisObj = scope;
break;
}
}
scope = parentScope;
parentScope = parentScope.ParentScope;
if (parentScope == null) {
result = topScopeName (cx, scope, name);
if (result == UniqueTag.NotFound) {
if (firstXMLObject == null || asFunctionCall) {
throw NotFoundError (scope, name);
}
// The name was not found, but we did find an XML
// object in the scope chain and we are looking for name,
// not function. The result should be an empty XMLList
// in name context.
result = firstXMLObject.EcmaGet (cx, name);
}
// For top scope thisObj for functions is always scope itself.
thisObj = scope;
break;
}
}
if (asFunctionCall) {
if (!(result is ICallable)) {
throw NotFunctionError (result, name);
}
storeScriptable (cx, thisObj);
}
return result;
}
private static object topScopeName (Context cx, IScriptable scope, string name)
{
if (cx.useDynamicScope) {
scope = checkDynamicScope (cx.topCallScope, scope);
}
return ScriptableObject.GetProperty (scope, name);
}
/// <summary> Returns the object in the scope chain that has a given property.
///
/// The order of evaluation of an assignment expression involves
/// evaluating the lhs to a reference, evaluating the rhs, and then
/// modifying the reference with the rhs value. This method is used
/// to 'bind' the given name to an object containing that property
/// so that the side effects of evaluating the rhs do not affect
/// which property is modified.
/// Typically used in conjunction with setName.
///
/// See ECMA 10.1.4