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 13
Expand file tree
/
Copy pathScriptableObject.cs
More file actions
1410 lines (1303 loc) · 52.9 KB
/
Copy pathScriptableObject.cs
File metadata and controls
1410 lines (1303 loc) · 52.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="ScriptableObject.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>
//------------------------------------------------------------------------------
// API class
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
using EcmaScript.NET.Debugging;
using EcmaScript.NET.Attributes;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
/// <summary> This is the default implementation of the Scriptable interface. This
/// class provides convenient default behavior that makes it easier to
/// define host objects.
/// <p>
/// Various properties and methods of JavaScript objects can be conveniently
/// defined using methods of ScriptableObject.
/// <p>
/// Classes extending ScriptableObject must define the getClassName method.
///
/// </summary>
public abstract class ScriptableObject : IScriptable, DebuggableObject
{
/// <summary> Return the name of the class.
///
/// This is typically the same name as the constructor.
/// Classes extending ScriptableObject must implement this abstract
/// method.
/// </summary>
public abstract string ClassName { get;}
/// <summary> Returns the parent (enclosing) scope of the object.</summary>
/// <summary> Sets the parent (enclosing) scope of the object.</summary>
public IScriptable ParentScope
{
get
{
return parentScopeObject;
}
set
{
parentScopeObject = value;
}
}
/// <summary> Returns an array of ids for the properties of the object.
///
/// <p>All properties, even those with attribute DONTENUM, are listed. <p>
///
/// </summary>
/// <returns> an array of java.lang.Objects with an entry for every
/// listed property. Properties accessed via an integer index will
/// have a corresponding
/// Integer entry in the returned array. Properties accessed by
/// a String will have a String entry in the returned array.
/// </returns>
virtual public object [] AllIds
{
get
{
return GetIds (true);
}
}
/// <summary> Return true if this object is sealed.
///
/// It is an error to attempt to add or remove properties to
/// a sealed object.
///
/// </summary>
/// <returns> true if sealed, false otherwise.
/// </returns>
public bool Sealed
{
get
{
return count < 0;
}
}
/// <summary> The empty property attribute.
///
/// Used by getAttributes() and setAttributes().
///
/// </summary>
public const int EMPTY = 0x00;
/// <summary> Property attribute indicating assignment to this property is ignored.
///
/// </summary>
public const int READONLY = 0x01;
/// <summary> Property attribute indicating property is not enumerated.
///
/// Only enumerated properties will be returned by getIds().
///
/// </summary>
public const int DONTENUM = 0x02;
/// <summary> Property attribute indicating property cannot be deleted.
///
/// </summary>
public const int PERMANENT = 0x04;
internal static void CheckValidAttributes (int attributes)
{
const int mask = READONLY | DONTENUM | PERMANENT;
if ((attributes & ~mask) != 0) {
throw new ArgumentException (Convert.ToString (attributes));
}
}
public ScriptableObject ()
{
}
public ScriptableObject (IScriptable scope, IScriptable prototype)
{
if (scope == null)
throw new ArgumentException ();
parentScopeObject = scope;
prototypeObject = prototype;
}
/// <summary> Returns true if the named property is defined.
///
/// </summary>
/// <param name="name">the name of the property
/// </param>
/// <param name="start">the object in which the lookup began
/// </param>
/// <returns> true if and only if the property was found in the object
/// </returns>
public virtual bool Has (string name, IScriptable start)
{
return null != GetNamedSlot (name);
}
/// <summary> Returns true if the property index is defined.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
/// <param name="start">the object in which the lookup began
/// </param>
/// <returns> true if and only if the property was found in the object
/// </returns>
public virtual bool Has (int index, IScriptable start)
{
return null != GetSlot (null, index);
}
/// <summary> Returns the value of the named property or NOT_FOUND.
///
/// If the property was created using defineProperty, the
/// appropriate getter method is called.
///
/// </summary>
/// <param name="name">the name of the property
/// </param>
/// <param name="start">the object in which the lookup began
/// </param>
/// <returns> the value of the property (may be null), or NOT_FOUND
/// </returns>
public virtual object Get (string name, IScriptable start)
{
Slot slot = GetNamedSlot (name);
if (slot == null) {
return UniqueTag.NotFound;
}
return slot.GetValue (null, start, start);
}
/// <summary> Returns the value of the indexed property or NOT_FOUND.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
/// <param name="start">the object in which the lookup began
/// </param>
/// <returns> the value of the property (may be null), or NOT_FOUND
/// </returns>
public virtual object Get (int index, IScriptable start)
{
Slot slot = GetSlot (null, index);
if (slot == null) {
return UniqueTag.NotFound;
}
return slot.GetValue (null, start, start);
}
/// <summary> Sets the value of the named property, creating it if need be.
///
/// If the property was created using defineProperty, the
/// appropriate setter method is called. <p>
///
/// If the property's attributes include READONLY, no action is
/// taken.
/// This method will actually set the property in the start
/// object.
///
/// </summary>
/// <param name="name">the name of the property
/// </param>
/// <param name="start">the object whose property is being set
/// </param>
/// <param name="value">value to set the property to
/// </param>
public virtual object Put (string name, IScriptable start, object value)
{
Slot slot = lastAccess; // Get local copy
if ((object)name != (object)slot.stringKey || slot.wasDeleted != 0) {
int hash = name.GetHashCode ();
slot = GetSlot (name, hash);
if (slot == null) {
if (start != this) {
start.Put (name, start, value);
return value;
}
slot = AddSlot (name, hash, null);
}
// Note: cache is not updated in put
}
if (start == this && Sealed) {
throw Context.ReportRuntimeErrorById ("msg.modify.sealed", name);
}
if ((slot.attributes & ScriptableObject.READONLY) != 0) {
// FINDME
Context cx = Context.CurrentContext;
if (cx.Version == Context.Versions.JS1_2) {
throw Context.ReportRuntimeErrorById ("msg.read-only", name);
} else {
if (cx.HasFeature (Context.Features.Strict)) {
Context.ReportWarningById ("msg.read-only", name);
}
}
return value;
}
if (this == start) {
return slot.SetValue (null, start, start, value);
}
else {
if (slot.setter != null) {
Slot newSlot = (Slot)slot.Clone ();
((ScriptableObject)start).AddSlotImpl (newSlot.stringKey, newSlot.intKey, newSlot);
return newSlot.SetValue (null, start, start, value);
}
else {
return start.Put (name, start, value);
}
}
return value;
}
/// <summary> Sets the value of the indexed property, creating it if need be.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
/// <param name="start">the object whose property is being set
/// </param>
/// <param name="value">value to set the property to
/// </param>
public virtual object Put (int index, IScriptable start, object value)
{
Slot slot = GetSlot (null, index);
if (slot == null) {
if (start != this) {
return start.Put (index, start, value);
}
slot = AddSlot (null, index, null);
}
if (start == this && Sealed) {
throw Context.ReportRuntimeErrorById ("msg.modify.sealed", Convert.ToString (index));
}
if ((slot.attributes & ScriptableObject.READONLY) != 0) {
return slot.GetValue (null, start, start); // TODO: ???
}
if (this == start) {
return slot.SetValue (null, start, start, value);
}
else {
return start.Put (index, start, value);
}
}
/// <summary> Removes a named property from the object.
///
/// If the property is not found, or it has the PERMANENT attribute,
/// no action is taken.
///
/// </summary>
/// <param name="name">the name of the property
/// </param>
public virtual void Delete (string name)
{
RemoveSlot (name, name.GetHashCode ());
}
/// <summary> Removes the indexed property from the object.
///
/// If the property is not found, or it has the PERMANENT attribute,
/// no action is taken.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
public virtual void Delete (int index)
{
RemoveSlot (null, index);
}
/// <summary> Get the attributes of a named property.
///
/// The property is specified by <code>name</code>
/// as defined for <code>has</code>.<p>
///
/// </summary>
/// <param name="name">the identifier for the property
/// </param>
/// <returns> the bitset of attributes
/// </returns>
/// <exception cref=""> EvaluatorException if the named property is not found
/// </exception>
public virtual int GetAttributes (string name)
{
Slot slot = GetNamedSlot (name);
if (slot == null) {
throw Context.ReportRuntimeErrorById ("msg.prop.not.found", name);
}
return slot.attributes;
}
/// <summary> Get the attributes of an indexed property.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
/// <exception cref=""> EvaluatorException if the named property is not found
/// is not found
/// </exception>
/// <returns> the bitset of attributes
/// </returns>
public virtual int GetAttributes (int index)
{
Slot slot = GetSlot (null, index);
if (slot == null) {
throw Context.ReportRuntimeErrorById ("msg.prop.not.found", Convert.ToString (index));
}
return slot.attributes;
}
/// <summary> Set the attributes of a named property.
///
/// The property is specified by <code>name</code>
/// as defined for <code>has</code>.<p>
///
/// The possible attributes are READONLY, DONTENUM,
/// and PERMANENT. Combinations of attributes
/// are expressed by the bitwise OR of attributes.
/// EMPTY is the state of no attributes set. Any unused
/// bits are reserved for future use.
///
/// </summary>
/// <param name="name">the name of the property
/// </param>
/// <param name="attributes">the bitset of attributes
/// </param>
/// <exception cref=""> EvaluatorException if the named property is not found
/// </exception>
public virtual void SetAttributes (string name, int attributes)
{
CheckValidAttributes (attributes);
Slot slot = GetNamedSlot (name);
if (slot == null) {
throw Context.ReportRuntimeErrorById ("msg.prop.not.found", name);
}
slot.attributes = (short)attributes;
}
/// <summary> Set the attributes of an indexed property.
///
/// </summary>
/// <param name="index">the numeric index for the property
/// </param>
/// <param name="attributes">the bitset of attributes
/// </param>
/// <exception cref=""> EvaluatorException if the named property is not found
/// </exception>
public virtual void SetAttributes (int index, int attributes)
{
CheckValidAttributes (attributes);
Slot slot = GetSlot (null, index);
if (slot == null) {
throw Context.ReportRuntimeErrorById ("msg.prop.not.found", Convert.ToString (index));
}
slot.attributes = (short)attributes;
}
/// <summary> Returns the prototype of the object.</summary>
public virtual IScriptable GetPrototype ()
{
return prototypeObject;
}
/// <summary> Sets the prototype of the object.</summary>
public virtual void SetPrototype (IScriptable m)
{
prototypeObject = m;
}
/// <summary> Returns an array of ids for the properties of the object.
///
/// <p>Any properties with the attribute DONTENUM are not listed. <p>
///
/// </summary>
/// <returns> an array of java.lang.Objects with an entry for every
/// listed property. Properties accessed via an integer index will
/// have a corresponding
/// Integer entry in the returned array. Properties accessed by
/// a String will have a String entry in the returned array.
/// </returns>
public virtual object [] GetIds ()
{
return GetIds (false);
}
/// <summary> Implements the [[DefaultValue]] internal method.
///
/// <p>Note that the toPrimitive conversion is a no-op for
/// every type other than Object, for which [[DefaultValue]]
/// is called. See ECMA 9.1.<p>
///
/// A <code>hint</code> of null means "no hint".
///
/// </summary>
/// <param name="typeHint">the type hint
/// </param>
/// <returns> the default value for the object
///
/// See ECMA 8.6.2.6.
/// </returns>
public virtual object GetDefaultValue (Type typeHint)
{
Context cx = null;
for (int i = 0; i < 2; i++) {
bool tryToString;
if (typeHint == typeof (string)) {
tryToString = (i == 0);
}
else {
tryToString = (i == 1);
}
string methodName;
object [] args;
if (tryToString) {
methodName = "toString";
args = ScriptRuntime.EmptyArgs;
}
else {
methodName = "valueOf";
args = new object [1];
string hint;
if (typeHint == null) {
hint = "undefined";
}
else if (typeHint == typeof (string)) {
hint = "string";
}
else if (typeHint == typeof (IScriptable)) {
hint = "object";
}
else if (typeHint == typeof (IFunction)) {
hint = "function";
}
else if (typeHint == typeof (bool) || typeHint == typeof (bool)) {
hint = "boolean";
}
else if (CliHelper.IsNumberType (typeHint) || typeHint == typeof (byte) || typeHint == typeof (sbyte)) {
hint = "number";
}
else {
throw Context.ReportRuntimeErrorById ("msg.invalid.type", typeHint.ToString ());
}
args [0] = hint;
}
object v = GetProperty (this, methodName);
if (!(v is IFunction))
continue;
IFunction fun = (IFunction)v;
if (cx == null)
cx = Context.CurrentContext;
v = fun.Call (cx, fun.ParentScope, this, args);
if (v != null) {
if (!(v is IScriptable)) {
return v;
}
if (typeHint == typeof (IScriptable) || typeHint == typeof (IFunction)) {
return v;
}
if (tryToString && v is Wrapper) {
// Let a wrapped java.lang.String pass for a primitive
// string.
object u = ((Wrapper)v).Unwrap ();
if (u is string)
return u;
}
}
}
// fall through to error
string arg = (typeHint == null) ? "undefined" : typeHint.FullName;
throw ScriptRuntime.TypeErrorById ("msg.default.value", arg);
}
/// <summary> Implements the instanceof operator.
///
/// <p>This operator has been proposed to ECMA.
///
/// </summary>
/// <param name="instance">The value that appeared on the LHS of the instanceof
/// operator
/// </param>
/// <returns> true if "this" appears in value's prototype chain
///
/// </returns>
public virtual bool HasInstance (IScriptable instance)
{
// According to specs -- section 11.8.6 of ECMA-262 -- instanceof operator
// on objects NOT implementing [[HasInstance]] internal method should
// throw a TypeError exception. Hence, in the following script the
// catch block must get executed, (since Math object does not implement
// [[HasInstance]] method).
throw ScriptRuntime.TypeError ("msg.bad.instanceof.rhs");
// Default for JS objects (other than Function) is to do prototype
// chasing. This will be overridden in NativeFunction and non-JS
// objects.
//return ScriptRuntime.jsDelegatesTo(instance, this);
}
/// <summary> Custom <tt>==</tt> operator.
/// Must return {@link Scriptable#NOT_FOUND} if this object does not
/// have custom equality operator for the given value,
/// <tt>Boolean.TRUE</tt> if this object is equivalent to <tt>value</tt>,
/// <tt>Boolean.FALSE</tt> if this object is not equivalent to
/// <tt>value</tt>.
/// <p>
/// The default implementation returns Boolean.TRUE
/// if <tt>this == value</tt> or {@link Scriptable#NOT_FOUND} otherwise.
/// It indicates that by default custom equality is available only if
/// <tt>value</tt> is <tt>this</tt> in which case true is returned.
/// </summary>
protected internal virtual object EquivalentValues (object value)
{
return (this == value) ? (object)true : UniqueTag.NotFound;
}
/// <summary> Define a JavaScript property.
///
/// Creates the property with an initial value and sets its attributes.
///
/// </summary>
/// <param name="propertyName">the name of the property to define.
/// </param>
/// <param name="value">the initial value of the property
/// </param>
/// <param name="attributes">the attributes of the JavaScript property
/// </param>
public virtual void DefineProperty (string propertyName, object value, int attributes)
{
Put (propertyName, this, value);
SetAttributes (propertyName, attributes);
}
/// <summary> Utility method to add properties to arbitrary Scriptable object.
/// If destination is instance of ScriptableObject, calls
/// defineProperty there, otherwise calls put in destination
/// ignoring attributes
/// </summary>
public static void DefineProperty (IScriptable destination, string propertyName, object value, int attributes)
{
if (!(destination is ScriptableObject)) {
destination.Put (propertyName, destination, value);
return;
}
ScriptableObject so = (ScriptableObject)destination;
so.DefineProperty (propertyName, value, attributes);
}
/// <summary> Get the Object.prototype property.
/// See ECMA 15.2.4.
/// </summary>
public static IScriptable GetObjectPrototype (IScriptable scope)
{
return getClassPrototype (scope, "Object");
}
/// <summary> Get the Function.prototype property.
/// See ECMA 15.3.4.
/// </summary>
public static IScriptable GetFunctionPrototype (IScriptable scope)
{
return getClassPrototype (scope, "Function");
}
/// <summary> Get the prototype for the named class.
///
/// For example, <code>getClassPrototype(s, "Date")</code> will first
/// walk up the parent chain to find the outermost scope, then will
/// search that scope for the Date constructor, and then will
/// return Date.prototype. If any of the lookups fail, or
/// the prototype is not a JavaScript object, then null will
/// be returned.
///
/// </summary>
/// <param name="scope">an object in the scope chain
/// </param>
/// <param name="className">the name of the constructor
/// </param>
/// <returns> the prototype for the named class, or null if it
/// cannot be found.
/// </returns>
public static IScriptable getClassPrototype (IScriptable scope, string className)
{
scope = GetTopLevelScope (scope);
object ctor = GetProperty (scope, className);
object proto;
if (ctor is BaseFunction) {
proto = ((BaseFunction)ctor).PrototypeProperty;
}
else if (ctor is IScriptable) {
IScriptable ctorObj = (IScriptable)ctor;
proto = ctorObj.Get ("prototype", ctorObj);
}
else {
return null;
}
if (proto is IScriptable) {
return (IScriptable)proto;
}
return null;
}
/// <summary> Get the global scope.
///
/// <p>Walks the parent scope chain to find an object with a null
/// parent scope (the global object).
///
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <returns> the corresponding global scope
/// </returns>
public static IScriptable GetTopLevelScope (IScriptable obj)
{
for (; ; ) {
IScriptable parent = obj.ParentScope;
if (parent == null) {
return obj;
}
obj = parent;
}
}
/// <summary> Seal this object.
///
/// A sealed object may not have properties added or removed. Once
/// an object is sealed it may not be unsealed.
///
/// </summary>
public virtual void SealObject ()
{
lock (this) {
if (count >= 0) {
count = -1 - count;
}
}
}
/// <summary> Gets a named property from an object or any object in its prototype chain.
/// <p>
/// Searches the prototype chain for a property named <code>name</code>.
/// <p>
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="name">a property name
/// </param>
/// <returns> the value of a property with name <code>name</code> found in
/// <code>obj</code> or any object in its prototype chain, or
/// <code>Scriptable.NOT_FOUND</code> if not found
/// </returns>
public static object GetProperty (IScriptable obj, string name)
{
IScriptable start = obj;
object result;
do {
result = obj.Get (name, start);
if (result != UniqueTag.NotFound)
break;
obj = obj.GetPrototype ();
}
while (obj != null);
return result;
}
/// <summary> Gets an indexed property from an object or any object in its prototype chain.
/// <p>
/// Searches the prototype chain for a property with integral index
/// <code>index</code>. Note that if you wish to look for properties with numerical
/// but non-integral indicies, you should use getProperty(Scriptable,String) with
/// the string value of the index.
/// <p>
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="index">an integral index
/// </param>
/// <returns> the value of a property with index <code>index</code> found in
/// <code>obj</code> or any object in its prototype chain, or
/// <code>Scriptable.NOT_FOUND</code> if not found
/// </returns>
public static object GetProperty (IScriptable obj, int index)
{
IScriptable start = obj;
object result;
do {
result = obj.Get (index, start);
if (result != UniqueTag.NotFound)
break;
obj = obj.GetPrototype ();
}
while (obj != null);
return result;
}
/// <summary> Returns whether a named property is defined in an object or any object
/// in its prototype chain.
/// <p>
/// Searches the prototype chain for a property named <code>name</code>.
/// <p>
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="name">a property name
/// </param>
/// <returns> the true if property was found
/// </returns>
public static bool HasProperty (IScriptable obj, string name)
{
return null != GetBase (obj, name);
}
/// <summary> Returns whether an indexed property is defined in an object or any object
/// in its prototype chain.
/// <p>
/// Searches the prototype chain for a property with index <code>index</code>.
/// <p>
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="index">a property index
/// </param>
/// <returns> the true if property was found
/// </returns>
public static bool HasProperty (IScriptable obj, int index)
{
return null != GetBase (obj, index);
}
/// <summary> Puts a named property in an object or in an object in its prototype chain.
/// <p>
/// Seaches for the named property in the prototype chain. If it is found,
/// the value of the property is changed. If it is not found, a new
/// property is added in <code>obj</code>.
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="name">a property name
/// </param>
/// <param name="value">any JavaScript value accepted by Scriptable.put
/// </param>
public static object PutProperty (IScriptable obj, string name, object value)
{
IScriptable toBase = GetBase (obj, name);
if (toBase == null)
toBase = obj;
return toBase.Put (name, obj, value);
}
/// <summary> Puts an indexed property in an object or in an object in its prototype chain.
/// <p>
/// Seaches for the indexed property in the prototype chain. If it is found,
/// the value of the property is changed. If it is not found, a new
/// property is added in <code>obj</code>.
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="index">a property index
/// </param>
/// <param name="value">any JavaScript value accepted by Scriptable.put
/// </param>
public static object PutProperty (IScriptable obj, int index, object value)
{
IScriptable toBase = GetBase (obj, index);
if (toBase == null)
toBase = obj;
return toBase.Put (index, obj, value);
}
/// <summary> Removes the property from an object or its prototype chain.
/// <p>
/// Searches for a property with <code>name</code> in obj or
/// its prototype chain. If it is found, the object's delete
/// method is called.
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="name">a property name
/// </param>
/// <returns> true if the property doesn't exist or was successfully removed
/// </returns>
public static bool DeleteProperty (IScriptable obj, string name)
{
IScriptable toBase = GetBase (obj, name);
if (toBase == null)
return true;
toBase.Delete (name);
return !toBase.Has (name, obj);
}
/// <summary> Removes the property from an object or its prototype chain.
/// <p>
/// Searches for a property with <code>index</code> in obj or
/// its prototype chain. If it is found, the object's delete
/// method is called.
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <param name="index">a property index
/// </param>
/// <returns> true if the property doesn't exist or was successfully removed
/// </returns>
public static bool DeleteProperty (IScriptable obj, int index)
{
IScriptable toBase = GetBase (obj, index);
if (toBase == null)
return true;
toBase.Delete (index);
return !toBase.Has (index, obj);
}
/// <summary> Returns an array of all ids from an object and its prototypes.
/// <p>
/// </summary>
/// <param name="obj">a JavaScript object
/// </param>
/// <returns> an array of all ids from all object in the prototype chain.
/// If a given id occurs multiple times in the prototype chain,
/// it will occur only once in this list.
/// </returns>
public static object [] GetPropertyIds (IScriptable obj)
{
if (obj == null) {
return ScriptRuntime.EmptyArgs;
}
object [] result = obj.GetIds ();
ObjToIntMap map = null;
for (; ; ) {
obj = obj.GetPrototype ();
if (obj == null) {
break;
}
object [] ids = obj.GetIds ();
if (ids.Length == 0) {
continue;
}
if (map == null) {
if (result.Length == 0) {
result = ids;
continue;
}
map = new ObjToIntMap (result.Length + ids.Length);
for (int i = 0; i != result.Length; ++i) {
map.intern (result [i]);
}
result = null; // Allow to GC the result
}
for (int i = 0; i != ids.Length; ++i) {
map.intern (ids [i]);
}
}
if (map != null) {
result = map.getKeys ();
}
return result;
}
/// <summary> Call a method of an object.</summary>
/// <param name="obj">the JavaScript object
/// </param>
/// <param name="methodName">the name of the function property
/// </param>
/// <param name="args">the arguments for the call
///
/// </param>
public static object CallMethod (IScriptable obj, string methodName, object [] args)
{
return CallMethod (null, obj, methodName, args);
}
/// <summary> Call a method of an object.</summary>
/// <param name="cx">the Context object associated with the current thread.
/// </param>
/// <param name="obj">the JavaScript object
/// </param>
/// <param name="methodName">the name of the function property
/// </param>
/// <param name="args">the arguments for the call
/// </param>
public static object CallMethod (Context cx, IScriptable obj, string methodName, object [] args)
{
object funObj = GetProperty (obj, methodName);
if (!(funObj is IFunction)) {
throw ScriptRuntime.NotFunctionError (obj, methodName);
}
IFunction fun = (IFunction)funObj;
// TODO: What should be the scope when calling funObj?
// The following favor scope stored in the object on the assumption
// that is more useful especially under dynamic scope setup.
// An alternative is to check for dynamic scope flag
// and use ScriptableObject.getTopLevelScope(fun) if the flag is not
// set. But that require access to Context and messy code
// so for now it is not checked.
IScriptable scope = ScriptableObject.GetTopLevelScope (obj);
if (cx != null) {
return fun.Call (cx, scope, obj, args);
}
else {
return Context.Call (null, fun, scope, obj, args);
}
}
private static IScriptable GetBase (IScriptable obj, string name)
{
do {
if (obj.Has (name, obj))
break;
obj = obj.GetPrototype ();
}
while (obj != null);
return obj;
}
private static IScriptable GetBase (IScriptable obj, int index)
{
do {
if (obj.Has (index, obj))
break;
obj = obj.GetPrototype ();
}
while (obj != null);
return obj;
}
/// <summary> Get arbitrary application-specific value associated with this object.</summary>
/// <param name="key">key object to select particular value.
/// </param>
public object GetAssociatedValue (object key)
{
Hashtable h = associatedValues;
if (h == null)