forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathScriptableObject.java
More file actions
3261 lines (3012 loc) · 121 KB
/
Copy pathScriptableObject.java
File metadata and controls
3261 lines (3012 loc) · 121 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
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// API class
package org.mozilla.javascript;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.mozilla.javascript.debug.DebuggableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;
import org.mozilla.javascript.annotations.JSSetter;
import org.mozilla.javascript.annotations.JSStaticFunction;
/**
* 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.
*
* @see org.mozilla.javascript.Scriptable
* @author Norris Boyd
*/
public abstract class ScriptableObject implements Scriptable, Serializable,
DebuggableObject,
ConstProperties
{
static final long serialVersionUID = 2829861078851942586L;
/**
* The empty property attribute.
*
* Used by getAttributes() and setAttributes().
*
* @see org.mozilla.javascript.ScriptableObject#getAttributes(String)
* @see org.mozilla.javascript.ScriptableObject#setAttributes(String, int)
*/
public static final int EMPTY = 0x00;
/**
* Property attribute indicating assignment to this property is ignored.
*
* @see org.mozilla.javascript.ScriptableObject
* #put(String, Scriptable, Object)
* @see org.mozilla.javascript.ScriptableObject#getAttributes(String)
* @see org.mozilla.javascript.ScriptableObject#setAttributes(String, int)
*/
public static final int READONLY = 0x01;
/**
* Property attribute indicating property is not enumerated.
*
* Only enumerated properties will be returned by getIds().
*
* @see org.mozilla.javascript.ScriptableObject#getIds()
* @see org.mozilla.javascript.ScriptableObject#getAttributes(String)
* @see org.mozilla.javascript.ScriptableObject#setAttributes(String, int)
*/
public static final int DONTENUM = 0x02;
/**
* Property attribute indicating property cannot be deleted.
*
* @see org.mozilla.javascript.ScriptableObject#delete(String)
* @see org.mozilla.javascript.ScriptableObject#getAttributes(String)
* @see org.mozilla.javascript.ScriptableObject#setAttributes(String, int)
*/
public static final int PERMANENT = 0x04;
/**
* Property attribute indicating that this is a const property that has not
* been assigned yet. The first 'const' assignment to the property will
* clear this bit.
*/
public static final int UNINITIALIZED_CONST = 0x08;
public static final int CONST = PERMANENT|READONLY|UNINITIALIZED_CONST;
/**
* The prototype of this object.
*/
private Scriptable prototypeObject;
/**
* The parent scope of this object.
*/
private Scriptable parentScopeObject;
private transient Slot[] slots;
// If count >= 0, it gives number of keys or if count < 0,
// it indicates sealed object where ~count gives number of keys
private int count;
// Where external array data is stored.
private transient ExternalArrayData externalData;
// gateways into the definition-order linked list of slots
private transient Slot firstAdded;
private transient Slot lastAdded;
private volatile Map<Object,Object> associatedValues;
private static final int SLOT_QUERY = 1;
private static final int SLOT_MODIFY = 2;
private static final int SLOT_MODIFY_CONST = 3;
private static final int SLOT_MODIFY_GETTER_SETTER = 4;
private static final int SLOT_CONVERT_ACCESSOR_TO_DATA = 5;
// initial slot array size, must be a power of 2
private static final int INITIAL_SLOT_SIZE = 4;
private boolean isExtensible = true;
private static final Method GET_ARRAY_LENGTH;
static {
try {
GET_ARRAY_LENGTH = ScriptableObject.class.getMethod("getExternalArrayLength");
} catch (NoSuchMethodException nsm) {
throw new RuntimeException(nsm);
}
}
private static class Slot implements Serializable
{
private static final long serialVersionUID = -6090581677123995491L;
String name; // This can change due to caching
int indexOrHash;
private volatile short attributes;
transient volatile boolean wasDeleted;
volatile Object value;
transient Slot next; // next in hash table bucket
transient volatile Slot orderedNext; // next in linked list
Slot(String name, int indexOrHash, int attributes)
{
this.name = name;
this.indexOrHash = indexOrHash;
this.attributes = (short)attributes;
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
if (name != null) {
indexOrHash = name.hashCode();
}
}
boolean setValue(Object value, Scriptable owner, Scriptable start) {
if ((attributes & READONLY) != 0) {
return true;
}
if (owner == start) {
this.value = value;
return true;
} else {
return false;
}
}
Object getValue(Scriptable start) {
return value;
}
int getAttributes()
{
return attributes;
}
synchronized void setAttributes(int value)
{
checkValidAttributes(value);
attributes = (short)value;
}
void markDeleted() {
wasDeleted = true;
value = null;
name = null;
}
ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) {
return buildDataDescriptor(scope, value, attributes);
}
}
protected static ScriptableObject buildDataDescriptor(Scriptable scope,
Object value,
int attributes) {
ScriptableObject desc = new NativeObject();
ScriptRuntime.setBuiltinProtoAndParent(desc, scope, TopLevel.Builtins.Object);
desc.defineProperty("value", value, EMPTY);
desc.defineProperty("writable", (attributes & READONLY) == 0, EMPTY);
desc.defineProperty("enumerable", (attributes & DONTENUM) == 0, EMPTY);
desc.defineProperty("configurable", (attributes & PERMANENT) == 0, EMPTY);
return desc;
}
private static final class GetterSlot extends Slot
{
static final long serialVersionUID = -4900574849788797588L;
Object getter;
Object setter;
GetterSlot(String name, int indexOrHash, int attributes)
{
super(name, indexOrHash, attributes);
}
@Override
ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) {
int attr = getAttributes();
ScriptableObject desc = new NativeObject();
ScriptRuntime.setBuiltinProtoAndParent(desc, scope, TopLevel.Builtins.Object);
desc.defineProperty("enumerable", (attr & DONTENUM) == 0, EMPTY);
desc.defineProperty("configurable", (attr & PERMANENT) == 0, EMPTY);
if (getter != null) desc.defineProperty("get", getter, EMPTY);
if (setter != null) desc.defineProperty("set", setter, EMPTY);
return desc;
}
@Override
boolean setValue(Object value, Scriptable owner, Scriptable start) {
if (setter == null) {
if (getter != null) {
if (Context.getContext().hasFeature(Context.FEATURE_STRICT_MODE)) {
// Based on TC39 ES3.1 Draft of 9-Feb-2009, 8.12.4, step 2,
// we should throw a TypeError in this case.
throw ScriptRuntime.typeError3("msg.set.prop.no.setter", name, start.getClassName(), Context.toString(value));
}
if (Context.getContext().hasFeature(Context.FEATURE_HTMLUNIT_ASK_OBJECT_TO_WRITE_READONLY)) {
Scriptable scriptable = start;
if (scriptable instanceof Delegator) {
scriptable = ((Delegator) scriptable).getDelegee();
}
if (scriptable instanceof ScriptableObject) {
boolean allowSetting = ((ScriptableObject) scriptable).isReadOnlySettable(name, value);
if (!allowSetting) {
return true;
}
}
if (owner == start) {
getter = null;
}
}
else {
// Based on TC39 ES3.1 Draft of 9-Feb-2009, 8.12.4, step 2,
// we should throw a TypeError in this case.
throw ScriptRuntime.typeError3("msg.set.prop.no.setter", name, start.getClassName(), Context.toString(value));
}
}
} else {
Context cx = Context.getContext();
if (setter instanceof MemberBox) {
MemberBox nativeSetter = (MemberBox)setter;
Class<?> pTypes[] = nativeSetter.argTypes;
// XXX: cache tag since it is already calculated in
// defineProperty ?
Class<?> valueType = pTypes[pTypes.length - 1];
int tag = FunctionObject.getTypeTag(valueType);
Object actualArg = FunctionObject.convertArg(cx, start,
value, tag);
Object setterThis;
Object[] args;
if (nativeSetter.delegateTo == null) {
setterThis = start;
args = new Object[] { actualArg };
} else {
setterThis = nativeSetter.delegateTo;
args = new Object[] { start, actualArg };
}
nativeSetter.invoke(setterThis, args);
} else if (setter instanceof Function) {
Function f = (Function)setter;
f.call(cx, f.getParentScope(), start,
new Object[] { value });
}
return true;
}
return super.setValue(value, owner, start);
}
@Override
Object getValue(Scriptable start) {
if (getter != null) {
if (getter instanceof MemberBox) {
MemberBox nativeGetter = (MemberBox)getter;
Object getterThis;
Object[] args;
if (nativeGetter.delegateTo == null) {
getterThis = start;
args = ScriptRuntime.emptyArgs;
} else {
getterThis = nativeGetter.delegateTo;
args = new Object[] { start };
}
return nativeGetter.invoke(getterThis, args);
} else if (getter instanceof Function) {
Function f = (Function)getter;
Context cx = Context.getContext();
return f.call(cx, f.getParentScope(), start,
ScriptRuntime.emptyArgs);
}
}
Object val = this.value;
if (val instanceof LazilyLoadedCtor) {
LazilyLoadedCtor initializer = (LazilyLoadedCtor)val;
try {
initializer.init();
} finally {
this.value = val = initializer.getValue();
}
}
return val;
}
@Override
void markDeleted() {
super.markDeleted();
getter = null;
setter = null;
}
}
/**
* A wrapper around a slot that allows the slot to be used in a new slot
* table while keeping it functioning in its old slot table/linked list
* context. This is used when linked slots are copied to a new slot table.
* In a multi-threaded environment, these slots may still be accessed
* through their old slot table. See bug 688458.
*/
private static class RelinkedSlot extends Slot {
final Slot slot;
RelinkedSlot(Slot slot) {
super(slot.name, slot.indexOrHash, slot.attributes);
// Make sure we always wrap the actual slot, not another relinked one
this.slot = unwrapSlot(slot);
}
@Override
boolean setValue(Object value, Scriptable owner, Scriptable start) {
return slot.setValue(value, owner, start);
}
@Override
Object getValue(Scriptable start) {
return slot.getValue(start);
}
@Override
ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) {
return slot.getPropertyDescriptor(cx, scope);
}
@Override
int getAttributes() {
return slot.getAttributes();
}
@Override
void setAttributes(int value) {
slot.setAttributes(value);
}
@Override
void markDeleted() {
super.markDeleted();
slot.markDeleted();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(slot); // just serialize the wrapped slot
}
}
static void checkValidAttributes(int attributes)
{
final int mask = READONLY | DONTENUM | PERMANENT | UNINITIALIZED_CONST;
if ((attributes & ~mask) != 0) {
throw new IllegalArgumentException(String.valueOf(attributes));
}
}
public ScriptableObject()
{
}
public ScriptableObject(Scriptable scope, Scriptable prototype)
{
if (scope == null)
throw new IllegalArgumentException();
parentScopeObject = scope;
prototypeObject = prototype;
}
/**
* Gets the value that will be returned by calling the typeof operator on this object.
* @return default is "object" unless {@link #avoidObjectDetection()} is <code>true</code> in which
* case it returns "undefined"
*/
public String getTypeOf() {
return avoidObjectDetection() ? "undefined" : "object";
}
/**
* Return the name of the class.
*
* This is typically the same name as the constructor.
* Classes extending ScriptableObject must implement this abstract
* method.
*/
public abstract String getClassName();
/**
* Returns true if the named property is defined.
*
* @param name the name of the property
* @param start the object in which the lookup began
* @return true if and only if the property was found in the object
*/
public boolean has(String name, Scriptable start)
{
return null != getSlot(name, 0, SLOT_QUERY);
}
/**
* Returns true if the property index is defined.
*
* @param index the numeric index for the property
* @param start the object in which the lookup began
* @return true if and only if the property was found in the object
*/
public boolean has(int index, Scriptable start)
{
if (externalData != null) {
return (index < externalData.getArrayLength());
}
return null != getSlot(null, index, SLOT_QUERY);
}
/**
* Returns the value of the named property or NOT_FOUND.
*
* If the property was created using defineProperty, the
* appropriate getter method is called.
*
* @param name the name of the property
* @param start the object in which the lookup began
* @return the value of the property (may be null), or NOT_FOUND
*/
public Object get(String name, Scriptable start)
{
Slot slot = getSlot(name, 0, SLOT_QUERY);
if (slot == null) {
return Scriptable.NOT_FOUND;
}
return slot.getValue(start);
}
/**
* Returns the value of the indexed property or NOT_FOUND.
*
* @param index the numeric index for the property
* @param start the object in which the lookup began
* @return the value of the property (may be null), or NOT_FOUND
*/
public Object get(int index, Scriptable start)
{
if (externalData != null) {
if (index < externalData.getArrayLength()) {
return externalData.getArrayElement(index);
}
return Scriptable.NOT_FOUND;
}
Slot slot = getSlot(null, index, SLOT_QUERY);
if (slot == null) {
return Scriptable.NOT_FOUND;
}
return slot.getValue(start);
}
/**
* 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.
*
* @param name the name of the property
* @param start the object whose property is being set
* @param value value to set the property to
*/
public void put(String name, Scriptable start, Object value)
{
if (putImpl(name, 0, start, value))
return;
if (start == this) throw Kit.codeBug();
start.put(name, start, value);
}
/**
* Sets the value of the indexed property, creating it if need be.
*
* @param index the numeric index for the property
* @param start the object whose property is being set
* @param value value to set the property to
*/
public void put(int index, Scriptable start, Object value)
{
if (externalData != null) {
if (index < externalData.getArrayLength()) {
externalData.setArrayElement(index, value);
} else {
throw new JavaScriptException(
ScriptRuntime.newNativeError(Context.getCurrentContext(), this,
TopLevel.NativeErrors.RangeError,
new Object[] { "External array index out of bounds " }),
null, 0);
}
return;
}
if (putImpl(null, index, start, value))
return;
if (start == this) throw Kit.codeBug();
start.put(index, start, value);
}
/**
* Removes a named property from the object.
*
* If the property is not found, or it has the PERMANENT attribute,
* no action is taken.
*
* @param name the name of the property
*/
public void delete(String name)
{
checkNotSealed(name, 0);
removeSlot(name, 0);
}
/**
* Removes the indexed property from the object.
*
* If the property is not found, or it has the PERMANENT attribute,
* no action is taken.
*
* @param index the numeric index for the property
*/
public void delete(int index)
{
checkNotSealed(null, index);
removeSlot(null, index);
}
/**
* Sets the value of the named const 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.
*
* @param name the name of the property
* @param start the object whose property is being set
* @param value value to set the property to
*/
public void putConst(String name, Scriptable start, Object value)
{
if (putConstImpl(name, 0, start, value, READONLY))
return;
if (start == this) throw Kit.codeBug();
if (start instanceof ConstProperties)
((ConstProperties)start).putConst(name, start, value);
else
start.put(name, start, value);
}
public void defineConst(String name, Scriptable start)
{
if (putConstImpl(name, 0, start, Undefined.instance, UNINITIALIZED_CONST))
return;
if (start == this) throw Kit.codeBug();
if (start instanceof ConstProperties)
((ConstProperties)start).defineConst(name, start);
}
/**
* Returns true if the named property is defined as a const on this object.
* @param name
* @return true if the named property is defined as a const, false
* otherwise.
*/
public boolean isConst(String name)
{
Slot slot = getSlot(name, 0, SLOT_QUERY);
if (slot == null) {
return false;
}
return (slot.getAttributes() & (PERMANENT|READONLY)) ==
(PERMANENT|READONLY);
}
/**
* @deprecated Use {@link #getAttributes(String name)}. The engine always
* ignored the start argument.
*/
@Deprecated
public final int getAttributes(String name, Scriptable start)
{
return getAttributes(name);
}
/**
* @deprecated Use {@link #getAttributes(int index)}. The engine always
* ignored the start argument.
*/
@Deprecated
public final int getAttributes(int index, Scriptable start)
{
return getAttributes(index);
}
/**
* @deprecated Use {@link #setAttributes(String name, int attributes)}.
* The engine always ignored the start argument.
*/
@Deprecated
public final void setAttributes(String name, Scriptable start,
int attributes)
{
setAttributes(name, attributes);
}
/**
* @deprecated Use {@link #setAttributes(int index, int attributes)}.
* The engine always ignored the start argument.
*/
@Deprecated
public void setAttributes(int index, Scriptable start,
int attributes)
{
setAttributes(index, attributes);
}
/**
* Get the attributes of a named property.
*
* The property is specified by <code>name</code>
* as defined for <code>has</code>.<p>
*
* @param name the identifier for the property
* @return the bitset of attributes
* @exception EvaluatorException if the named property is not found
* @see org.mozilla.javascript.ScriptableObject#has(String, Scriptable)
* @see org.mozilla.javascript.ScriptableObject#READONLY
* @see org.mozilla.javascript.ScriptableObject#DONTENUM
* @see org.mozilla.javascript.ScriptableObject#PERMANENT
* @see org.mozilla.javascript.ScriptableObject#EMPTY
*/
public int getAttributes(String name)
{
return findAttributeSlot(name, 0, SLOT_QUERY).getAttributes();
}
/**
* Get the attributes of an indexed property.
*
* @param index the numeric index for the property
* @exception EvaluatorException if the named property is not found
* is not found
* @return the bitset of attributes
* @see org.mozilla.javascript.ScriptableObject#has(String, Scriptable)
* @see org.mozilla.javascript.ScriptableObject#READONLY
* @see org.mozilla.javascript.ScriptableObject#DONTENUM
* @see org.mozilla.javascript.ScriptableObject#PERMANENT
* @see org.mozilla.javascript.ScriptableObject#EMPTY
*/
public int getAttributes(int index)
{
return findAttributeSlot(null, index, SLOT_QUERY).getAttributes();
}
/**
* 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.
*
* @param name the name of the property
* @param attributes the bitset of attributes
* @exception EvaluatorException if the named property is not found
* @see org.mozilla.javascript.Scriptable#has(String, Scriptable)
* @see org.mozilla.javascript.ScriptableObject#READONLY
* @see org.mozilla.javascript.ScriptableObject#DONTENUM
* @see org.mozilla.javascript.ScriptableObject#PERMANENT
* @see org.mozilla.javascript.ScriptableObject#EMPTY
*/
public void setAttributes(String name, int attributes)
{
checkNotSealed(name, 0);
findAttributeSlot(name, 0, SLOT_MODIFY).setAttributes(attributes);
}
/**
* Set the attributes of an indexed property.
*
* @param index the numeric index for the property
* @param attributes the bitset of attributes
* @exception EvaluatorException if the named property is not found
* @see org.mozilla.javascript.Scriptable#has(String, Scriptable)
* @see org.mozilla.javascript.ScriptableObject#READONLY
* @see org.mozilla.javascript.ScriptableObject#DONTENUM
* @see org.mozilla.javascript.ScriptableObject#PERMANENT
* @see org.mozilla.javascript.ScriptableObject#EMPTY
*/
public void setAttributes(int index, int attributes)
{
checkNotSealed(null, index);
findAttributeSlot(null, index, SLOT_MODIFY).setAttributes(attributes);
}
/**
* XXX: write docs.
*/
public void setGetterOrSetter(String name, int index,
Callable getterOrSetter, boolean isSetter)
{
setGetterOrSetter(name, index, getterOrSetter, isSetter, false);
}
private void setGetterOrSetter(String name, int index, Callable getterOrSetter,
boolean isSetter, boolean force)
{
if (name != null && index != 0)
throw new IllegalArgumentException(name);
if (!force) {
checkNotSealed(name, index);
}
final GetterSlot gslot;
if (isExtensible()) {
gslot = (GetterSlot)getSlot(name, index, SLOT_MODIFY_GETTER_SETTER);
} else {
Slot slot = unwrapSlot(getSlot(name, index, SLOT_QUERY));
if (!(slot instanceof GetterSlot))
return;
gslot = (GetterSlot) slot;
}
if (!force) {
int attributes = gslot.getAttributes();
if ((attributes & READONLY) != 0) {
throw Context.reportRuntimeError1("msg.modify.readonly", name);
}
}
if (isSetter) {
gslot.setter = getterOrSetter;
} else {
gslot.getter = getterOrSetter;
}
gslot.value = Undefined.instance;
}
/**
* Get the getter or setter for a given property. Used by __lookupGetter__
* and __lookupSetter__.
*
* @param name Name of the object. If nonnull, index must be 0.
* @param index Index of the object. If nonzero, name must be null.
* @param isSetter If true, return the setter, otherwise return the getter.
* @exception IllegalArgumentException if both name and index are nonnull
* and nonzero respectively.
* @return Null if the property does not exist. Otherwise returns either
* the getter or the setter for the property, depending on
* the value of isSetter (may be undefined if unset).
*/
public Object getGetterOrSetter(String name, int index, boolean isSetter)
{
if (name != null && index != 0)
throw new IllegalArgumentException(name);
Slot slot = unwrapSlot(getSlot(name, index, SLOT_QUERY));
if (slot == null)
return null;
if (slot instanceof GetterSlot) {
GetterSlot gslot = (GetterSlot)slot;
Object result = isSetter ? gslot.setter : gslot.getter;
return result != null ? result : Undefined.instance;
} else
return Undefined.instance;
}
/**
* Returns whether a property is a getter or a setter
* @param name property name
* @param index property index
* @param setter true to check for a setter, false for a getter
* @return whether the property is a getter or a setter
*/
protected boolean isGetterOrSetter(String name, int index, boolean setter) {
Slot slot = unwrapSlot(getSlot(name, index, SLOT_QUERY));
if (slot instanceof GetterSlot) {
if (setter && ((GetterSlot)slot).setter != null) return true;
if (!setter && ((GetterSlot)slot).getter != null) return true;
}
return false;
}
void addLazilyInitializedValue(String name, int index,
LazilyLoadedCtor init, int attributes)
{
if (name != null && index != 0)
throw new IllegalArgumentException(name);
checkNotSealed(name, index);
GetterSlot gslot = (GetterSlot)getSlot(name, index,
SLOT_MODIFY_GETTER_SETTER);
gslot.setAttributes(attributes);
gslot.getter = null;
gslot.setter = null;
gslot.value = init;
}
/**
* Attach the specified object to this object, and delegate all indexed property lookups to it. In other words,
* if the object has 3 elements, then an attempt to look up or modify "[0]", "[1]", or "[2]" will be delegated
* to this object. Additional indexed properties outside the range specified, and additional non-indexed
* properties, may still be added. The object specified must implement the ExternalArrayData interface.
*
* @param array the List to use for delegated property access. Set this to null to revert back to regular
* property access.
* @since 1.7.6
*/
public void setExternalArrayData(ExternalArrayData array)
{
externalData = array;
if (array == null) {
delete("length");
} else {
// Define "length" to return whatever length the List gives us.
defineProperty("length", null,
GET_ARRAY_LENGTH, null, READONLY | DONTENUM);
}
}
/**
* Return the array that was previously set by the call to "setExternalArrayData".
*
* @return the array, or null if it was never set
* @since 1.7.6
*/
public ExternalArrayData getExternalArrayData()
{
return externalData;
}
/**
* This is a function used by setExternalArrayData to dynamically get the "length" property value.
*/
public Object getExternalArrayLength()
{
return (externalData == null ? 0 : externalData.getArrayLength());
}
/**
* Returns the prototype of the object.
*/
public Scriptable getPrototype()
{
return prototypeObject;
}
/**
* Sets the prototype of the object.
*/
public void setPrototype(Scriptable m)
{
prototypeObject = m;
}
/**
* Returns the parent (enclosing) scope of the object.
*/
public Scriptable getParentScope()
{
return parentScopeObject;
}
/**
* Sets the parent (enclosing) scope of the object.
*/
public void setParentScope(Scriptable m)
{
parentScopeObject = m;
}
/**
* Returns an array of ids for the properties of the object.
*
* <p>Any properties with the attribute DONTENUM are not listed. <p>
*
* @return 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.
*/
public Object[] getIds() {
return getIds(false);
}
/**
* Returns an array of ids for the properties of the object.
*
* <p>All properties, even those with attribute DONTENUM, are listed. <p>
*
* @return 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.
*/
public Object[] getAllIds() {
return getIds(true);
}
/**
* 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".
*
* @param typeHint the type hint
* @return the default value for the object
*
* See ECMA 8.6.2.6.
*/
public Object getDefaultValue(Class<?> typeHint)
{
return getDefaultValue(this, typeHint);
}