forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNativeObject.java
More file actions
1051 lines (932 loc) · 41.1 KB
/
NativeObject.java
File metadata and controls
1051 lines (932 loc) · 41.1 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/. */
package org.mozilla.javascript;
import static org.mozilla.javascript.ClassDescriptor.Destination.CTOR;
import static org.mozilla.javascript.ClassDescriptor.Destination.PROTO;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import org.mozilla.javascript.ScriptRuntime.StringIdOrIndex;
/**
* This class implements the Object native object. See ECMA 15.2.
*
* @author Norris Boyd
*/
public class NativeObject extends ScriptableObject implements Map {
private static final long serialVersionUID = -6345305608474346996L;
private static final Object OBJECT_TAG = "Object";
private static final String CLASS_NAME = "Object";
public static final String PROTO_PROPERTY = "__proto__";
public static final String PARENT_PROPERTY = "__parent__";
private static ClassDescriptor LEGACY_DESCRIPTOR;
private static ClassDescriptor ES6_DESCRIPTOR;
static {
var builder =
new ClassDescriptor.Builder(
CLASS_NAME,
1,
NativeObject::js_constructorCall,
NativeObject::js_constructor);
builder.withMethod(CTOR, "getPrototypeOf", 1, NativeObject::js_getPrototypeOf);
builder.withMethod(CTOR, "keys", 1, NativeObject::js_keys);
builder.withMethod(CTOR, "getOwnPropertyNames", 1, NativeObject::js_getOwnPropertyNames);
builder.withMethod(
CTOR, "getOwnPropertySymbols", 1, NativeObject::js_getOwnPropertySymbols);
builder.withMethod(CTOR, "getOwnPropertyDescriptor", 2, NativeObject::js_getOwnPropDesc);
builder.withMethod(CTOR, "getOwnPropertyDescriptors", 1, NativeObject::js_getOwnPropDescs);
builder.withMethod(CTOR, "defineProperty", 3, NativeObject::js_defineProperty);
builder.withMethod(CTOR, "isExtensible", 1, NativeObject::js_isExtensible);
builder.withMethod(CTOR, "preventExtensions", 1, NativeObject::js_preventExtensions);
builder.withMethod(CTOR, "defineProperties", 2, NativeObject::js_defineProperties);
builder.withMethod(CTOR, "create", 2, NativeObject::js_create);
builder.withMethod(CTOR, "isSealed", 1, NativeObject::js_isSealed);
builder.withMethod(CTOR, "isFrozen", 1, NativeObject::js_isFrozen);
builder.withMethod(CTOR, "seal", 1, NativeObject::js_seal);
builder.withMethod(CTOR, "freeze", 1, NativeObject::js_freeze);
builder.withMethod(CTOR, "assign", 2, NativeObject::js_assign);
builder.withMethod(CTOR, "is", 2, NativeObject::js_is);
builder.withMethod(CTOR, "groupBy", 2, NativeObject::js_groupBy);
builder.withMethod(PROTO, "toString", 0, NativeObject::js_toString);
builder.withMethod(PROTO, "toLocaleString", 0, NativeObject::js_toLocaleString);
builder.withMethod(PROTO, "__lookupGetter__", 1, NativeObject::js_lookupGetter);
builder.withMethod(PROTO, "__lookupSetter__", 1, NativeObject::js_lookupSetter);
builder.withMethod(PROTO, "__defineGetter__", 2, NativeObject::js_defineGetter);
builder.withMethod(PROTO, "__defineSetter__", 2, NativeObject::js_defineSetter);
builder.withMethod(PROTO, "hasOwnProperty", 1, NativeObject::js_hasOwnProperty);
builder.withMethod(PROTO, "propertyIsEnumerable", 1, NativeObject::js_propertyIsEnumerable);
builder.withMethod(PROTO, "valueOf", 0, NativeObject::js_valueOf);
builder.withMethod(PROTO, "isPrototypeOf", 1, NativeObject::js_isPrototypeOf);
builder.withMethod(PROTO, "toSource", 0, ScriptRuntime::defaultObjectToSource);
LEGACY_DESCRIPTOR = builder.build();
ES6_DESCRIPTOR =
builder.withMethod(CTOR, "setPrototypeOf", 2, NativeObject::js_setPrototypeOf)
.withMethod(CTOR, "entries", 1, NativeObject::js_entries)
.withMethod(CTOR, "fromEntries", 1, NativeObject::js_fromEntries)
.withMethod(CTOR, "values", 1, NativeObject::js_values)
.withMethod(CTOR, "hasOwn", 1, NativeObject::js_hasOwn)
.withProp(
PROTO,
PROTO_PROPERTY,
NativeObject::js_protoGetter,
NativeObject::js_protoSetter,
DONTENUM | READONLY)
.build();
}
static JSFunction init(Context cx, Scriptable s, boolean sealed) {
var desc = cx.version >= Context.VERSION_ES6 ? ES6_DESCRIPTOR : LEGACY_DESCRIPTOR;
return desc.buildConstructor(cx, s, new NativeObject(), sealed);
}
@Override
public String getClassName() {
return "Object";
}
@Override
public String toString() {
return ScriptRuntime.defaultObjectToString(this);
}
private static Scriptable js_constructorCall(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (args.length == 0 || args[0] == null || Undefined.isUndefined(args[0])) {
return cx.newObject(f.getDeclarationScope());
}
return ScriptRuntime.toObject(cx, f.getDeclarationScope(), args[0]);
}
private static Scriptable js_constructor(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (args.length == 0 || args[0] == null || Undefined.isUndefined(args[0])) {
return cx.newObject(f.getDeclarationScope());
}
return ScriptRuntime.toObject(cx, f.getDeclarationScope(), args[0]);
}
private static Object js_toLocaleString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (thisObj == null) {
throw ScriptRuntime.notFunctionError(null);
}
var so = ScriptRuntime.toObject(s, thisObj);
Object toString = ScriptableObject.getProperty(so, "toString");
if (!(toString instanceof Callable)) {
throw ScriptRuntime.notFunctionError(toString);
}
Callable fun = (Callable) toString;
return fun.call(cx, f.getDeclarationScope(), so, ScriptRuntime.emptyArgs);
}
private static Object js_toString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return ScriptRuntime.defaultObjectToString((Scriptable) thisObj);
}
private static Object js_valueOf(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (cx.getLanguageVersion() >= Context.VERSION_1_8
&& (thisObj == null || Undefined.isUndefined(thisObj))) {
throw ScriptRuntime.typeErrorById(
"msg." + (thisObj == null ? "null" : "undef") + ".to.object");
}
return thisObj;
}
private static Object js_hasOwnProperty(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (cx.getLanguageVersion() >= Context.VERSION_1_8
&& (thisObj == null || Undefined.isUndefined(thisObj))) {
throw ScriptRuntime.typeErrorById(
"msg." + (thisObj == null ? "null" : "undef") + ".to.object");
}
Object arg = args.length < 1 ? Undefined.instance : args[0];
return AbstractEcmaObjectOperations.hasOwnProperty(cx, thisObj, arg);
}
private static Object js_propertyIsEnumerable(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (cx.getLanguageVersion() >= Context.VERSION_1_8
&& (thisObj == null || Undefined.isUndefined(thisObj))) {
throw ScriptRuntime.typeErrorById(
"msg." + (thisObj == null ? "null" : "undef") + ".to.object");
}
boolean result;
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable so = ScriptRuntime.toObject(s, thisObj);
if (arg instanceof Symbol) {
result = ((SymbolScriptable) so).has((Symbol) arg, so);
result = result && isEnumerable((Symbol) arg, thisObj);
} else {
StringIdOrIndex soi = ScriptRuntime.toStringIdOrIndex(arg);
// When checking if a property is enumerable, a missing property should
// return "false" instead of
// throwing an exception. See: https://github.com/mozilla/rhino/issues/415
try {
if (soi.stringId == null) {
result = so.has(soi.index, so);
result = result && isEnumerable(soi.index, so);
} else {
result = so.has(soi.stringId, so);
result = result && isEnumerable(soi.stringId, so);
}
} catch (EvaluatorException ee) {
if (ee.getMessage()
.startsWith(
ScriptRuntime.getMessageById(
"msg.prop.not.found",
soi.stringId == null
? Integer.toString(soi.index)
: soi.stringId))) {
result = false;
} else {
throw ee;
}
}
}
return ScriptRuntime.wrapBoolean(result);
}
private static Object js_isPrototypeOf(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (cx.getLanguageVersion() >= Context.VERSION_1_8
&& (thisObj == null || Undefined.isUndefined(thisObj))) {
throw ScriptRuntime.typeErrorById(
"msg." + (thisObj == null ? "null" : "undef") + ".to.object");
}
boolean result = false;
if (args.length != 0 && args[0] instanceof Scriptable) {
Scriptable v = (Scriptable) args[0];
do {
v = v.getPrototype();
if (v == thisObj) {
result = true;
break;
}
} while (v != null);
}
return ScriptRuntime.wrapBoolean(result);
}
private static Object js_protoGetter(Scriptable thisObj) {
/*
Let O be ? ToObject(this value).
2. Return ? O.[[GetPrototypeOf]]().
*/
ScriptableObject o = (ScriptableObject) ScriptRuntime.toObject(thisObj, thisObj);
return o.getPrototype();
}
public static void js_protoSetter(Scriptable thisObj, Object proto) {
/*
Let O be ? RequireObjectCoercible(this value).
2. If proto is not an Object and proto is not null, return undefined.
3. If O is not an Object, return undefined.
4. Let status be ? O.[[SetPrototypeOf]](proto).
5. If status is false, throw a TypeError exception.
6. Return undefined.
*/
Object o =
(ScriptableObject)
ScriptRuntimeES6.requireObjectCoercible(
null, thisObj, CLASS_NAME, PROTO_PROPERTY);
if (!(proto instanceof Scriptable) && proto != null) {
return /* undefined */;
}
if (ScriptRuntime.isSymbol(proto)) {
return;
}
if (!(o instanceof Scriptable) || ScriptRuntime.isSymbol(o)) {
return;
}
setPrototypeOf(o, (Scriptable) proto);
}
private static Object js_defineGetter(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return js_defineGetterOrSetter(cx, s, false, thisObj, args);
}
private static Object js_defineSetter(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return js_defineGetterOrSetter(cx, s, true, thisObj, args);
}
private static Object js_defineGetterOrSetter(
Context cx, Scriptable scope, boolean isSetter, Object thisObj, Object[] args) {
if (args.length < 2 || !(args[1] instanceof Callable)) {
Object badArg = (args.length >= 2 ? args[1] : Undefined.instance);
throw ScriptRuntime.notFunctionError(badArg);
}
if (!(thisObj instanceof ScriptableObject)) {
throw Context.reportRuntimeErrorById(
"msg.extend.scriptable",
thisObj == null ? "null" : thisObj.getClass().getName(),
String.valueOf(args[0]));
}
ScriptableObject so = (ScriptableObject) thisObj;
StringIdOrIndex s = ScriptRuntime.toStringIdOrIndex(args[0]);
int index = s.stringId != null ? 0 : s.index;
Callable getterOrSetter = (Callable) args[1];
so.setGetterOrSetter(s.stringId, index, getterOrSetter, isSetter);
if (so instanceof NativeArray) ((NativeArray) so).setDenseOnly(false);
return Undefined.instance;
}
private static Object js_lookupGetter(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return js_lookupGetterOrSetter(cx, s, false, thisObj, args);
}
private static Object js_lookupSetter(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return js_lookupGetterOrSetter(cx, s, true, thisObj, args);
}
private static Object js_lookupGetterOrSetter(
Context cx, Scriptable scope, boolean isSetter, Object thisObj, Object[] args) {
if (args.length < 1 || !(thisObj instanceof ScriptableObject)) return Undefined.instance;
ScriptableObject so = (ScriptableObject) thisObj;
StringIdOrIndex s = ScriptRuntime.toStringIdOrIndex(args[0]);
int index = s.stringId != null ? 0 : s.index;
Object gs;
for (; ; ) {
gs = so.getGetterOrSetter(s.stringId, index, scope, isSetter);
if (gs != null) {
break;
}
// If there is no getter or setter for the object itself,
// how about the prototype?
Scriptable v = so.getPrototype();
if (v == null) {
break;
}
if (v instanceof ScriptableObject) {
so = (ScriptableObject) v;
} else {
break;
}
}
if (gs != null) {
return gs;
}
return Undefined.instance;
}
private static Object js_getPrototypeOf(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable obj = getCompatibleObject(cx, s, arg);
return obj.getPrototype();
}
private static Object js_setPrototypeOf(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
if (args.length < 2) {
throw ScriptRuntime.typeErrorById(
"msg.method.missing.parameter",
"Object.setPrototypeOf",
"2",
Integer.toString(args.length));
}
Scriptable proto = (args[1] == null) ? null : ensureScriptable(args[1]);
if (ScriptRuntime.isSymbol(proto)) {
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(proto));
}
final Object arg0 = args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
ScriptRuntimeES6.requireObjectCoercible(cx, arg0, OBJECT_TAG, "setPrototypeOf");
}
return setPrototypeOf(arg0, proto);
}
private static Object setPrototypeOf(Object thisObj, Scriptable proto) {
if (!(thisObj instanceof ScriptableObject)) {
return thisObj;
}
ScriptableObject thisScriptable = (ScriptableObject) thisObj;
if (thisScriptable.getPrototype() == proto) {
return thisObj;
}
if (!thisScriptable.isExtensible()) {
throw ScriptRuntime.typeErrorById("msg.not.extensible");
}
// cycle detection
Scriptable prototypeProto = proto;
while (prototypeProto != null) {
if (prototypeProto == thisScriptable) {
throw ScriptRuntime.typeErrorById(
"msg.object.cyclic.prototype", thisScriptable.getClass().getSimpleName());
}
prototypeProto = prototypeProto.getPrototype();
}
thisScriptable.setPrototype(proto);
return thisScriptable;
}
private static Object js_keys(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable obj = getCompatibleObject(cx, f.getDeclarationScope(), arg);
Object[] ids = obj.getIds();
for (int i = 0; i < ids.length; i++) {
ids[i] = ScriptRuntime.toString(ids[i]);
}
return cx.newArray(f.getDeclarationScope(), ids);
}
private static Object js_entries(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable obj = getCompatibleObject(cx, s, arg);
Object[] ids = obj.getIds();
int j = 0;
for (int i = 0; i < ids.length; i++) {
if (ids[i] instanceof Integer) {
int intId = (Integer) ids[i];
if (obj.has(intId, obj) && isEnumerable(intId, obj)) {
String stringId = ScriptRuntime.toString(ids[i]);
Object[] entry = new Object[] {stringId, obj.get(intId, obj)};
ids[j++] = cx.newArray(s, entry);
}
} else {
String stringId = ScriptRuntime.toString(ids[i]);
if (obj.has(stringId, obj) && isEnumerable(stringId, obj)) {
Object[] entry = new Object[] {stringId, obj.get(stringId, obj)};
ids[j++] = cx.newArray(s, entry);
}
}
}
if (j != ids.length) {
ids = Arrays.copyOf(ids, j);
}
return cx.newArray(s, ids);
}
private static Object js_fromEntries(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
arg = getCompatibleObject(cx, f.getDeclarationScope(), arg);
Scriptable obj = cx.newObject(f.getDeclarationScope());
ScriptRuntime.loadFromIterable(
cx,
s,
arg,
(key, value) -> {
if (key instanceof Integer) {
obj.put((Integer) key, obj, value);
} else if (key instanceof Symbol && obj instanceof SymbolScriptable) {
// using instanceof is correct here
// (see org.mozilla.javascript.tests.es6.Symbol3Test.fromEntries)
((SymbolScriptable) obj).put((Symbol) key, obj, value);
} else {
obj.put(ScriptRuntime.toString(key), obj, value);
}
});
return obj;
}
private static Object js_values(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable obj = getCompatibleObject(cx, s, arg);
Object[] ids = obj.getIds();
int j = 0;
for (int i = 0; i < ids.length; i++) {
if (ids[i] instanceof Integer) {
int intId = (Integer) ids[i];
if (obj.has(intId, obj) && isEnumerable(intId, obj)) {
ids[j++] = obj.get(intId, obj);
}
} else {
String stringId = ScriptRuntime.toString(ids[i]);
// getter may remove keys
if (obj.has(stringId, obj) && isEnumerable(stringId, obj)) {
ids[j++] = obj.get(stringId, obj);
}
}
}
if (j != ids.length) {
ids = Arrays.copyOf(ids, j);
}
return cx.newArray(s, ids);
}
private static Object js_hasOwn(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Object propertyName = args.length < 2 ? Undefined.instance : args[1];
return AbstractEcmaObjectOperations.hasOwnProperty(cx, arg, propertyName);
}
private static Object js_getOwnPropertyNames(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable s2 = getCompatibleObject(cx, s, arg);
ScriptableObject obj = ensureScriptableObject(s2);
Object[] ids;
try (var map = obj.startCompoundOp(false)) {
ids = obj.getIds(map, true, false);
}
for (int i = 0; i < ids.length; i++) {
ids[i] = ScriptRuntime.toString(ids[i]);
}
return cx.newArray(s, ids);
}
private static Object js_getOwnPropertySymbols(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable s2 = getCompatibleObject(cx, s, arg);
ScriptableObject obj = ensureScriptableObject(s2);
Object[] ids;
try (var map = obj.startCompoundOp(false)) {
ids = obj.getIds(map, true, true);
}
ArrayList<Object> syms = new ArrayList<>();
for (Object o : ids) {
if (o instanceof Symbol) {
syms.add(o);
}
}
return cx.newArray(s, syms.toArray());
}
private static Object js_getOwnPropDesc(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
// TODO(norris): There's a deeper issue here if
// arg instanceof Scriptable. Should we create a new
// interface to admit the new ECMAScript 5 operations?
Scriptable s2 = getCompatibleObject(cx, s, arg);
ScriptableObject obj = ensureScriptableObject(s2);
Object nameArg = args.length < 2 ? Undefined.instance : args[1];
var desc = obj.getOwnPropertyDescriptor(cx, nameArg);
return desc == null ? Undefined.instance : desc.toObject(f.getDeclarationScope());
}
private static Object js_getOwnPropDescs(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable s2 = getCompatibleObject(cx, s, arg);
ScriptableObject obj = ensureScriptableObject(s2);
ScriptableObject descs = (ScriptableObject) cx.newObject(f.getDeclarationScope());
Object[] ids;
try (var map = obj.startCompoundOp(false)) {
ids = obj.getIds(map, true, true);
}
for (Object key : ids) {
var desc = obj.getOwnPropertyDescriptor(cx, key);
if (desc == null) {
continue;
} else if (key instanceof Symbol) {
descs.put((Symbol) key, descs, desc.toObject(s));
} else if (key instanceof Integer) {
descs.put((Integer) key, descs, desc.toObject(s));
} else {
descs.put(ScriptRuntime.toString(key), descs, desc.toObject(s));
}
}
return descs;
}
private static Object js_defineProperty(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
ScriptableObject obj = ensureScriptableObject(arg);
Object name = args.length < 2 ? Undefined.instance : args[1];
Object descArg = args.length < 3 ? Undefined.instance : args[2];
var desc = new DescriptorInfo(ensureScriptableObject(descArg));
ScriptableObject.checkPropertyDefinition(desc);
obj.defineOwnProperty(cx, name, desc);
return obj;
}
private static Object js_isExtensible(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return Boolean.FALSE;
}
ScriptableObject obj = ensureScriptableObject(arg);
return Boolean.valueOf(obj.isExtensible());
}
private static Object js_preventExtensions(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return arg;
}
ScriptableObject obj = ensureScriptableObject(arg);
if (!obj.preventExtensions()) {
throw ScriptRuntime.typeError("Object.preventExtensions is not allowed");
}
return obj;
}
private static Object js_defineProperties(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
ScriptableObject obj = ensureScriptableObject(arg);
Object propsObj = args.length < 2 ? Undefined.instance : args[1];
Scriptable props = Context.toObject(propsObj, s);
obj.defineOwnProperties(cx, ensureScriptableObject(props));
return obj;
}
private static Object js_create(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
Scriptable obj = (arg == null) ? null : ensureScriptable(arg);
ScriptableObject newObject = new NativeObject();
newObject.setParentScope(s);
newObject.setPrototype(obj);
if (args.length > 1 && !Undefined.isUndefined(args[1])) {
Scriptable props = Context.toObject(args[1], s);
newObject.defineOwnProperties(cx, ensureScriptableObject(props));
}
return newObject;
}
private static Object js_isSealed(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return Boolean.TRUE;
}
return AbstractEcmaObjectOperations.testIntegrityLevel(
cx, arg, AbstractEcmaObjectOperations.INTEGRITY_LEVEL.SEALED);
}
private static Object js_isFrozen(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return Boolean.TRUE;
}
return AbstractEcmaObjectOperations.testIntegrityLevel(
cx, arg, AbstractEcmaObjectOperations.INTEGRITY_LEVEL.FROZEN);
}
private static Object js_seal(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return arg;
}
boolean status =
AbstractEcmaObjectOperations.setIntegrityLevel(
cx, arg, AbstractEcmaObjectOperations.INTEGRITY_LEVEL.SEALED);
if (!status) {
throw ScriptRuntime.typeError("Object is not sealable");
}
return arg;
}
private static Object js_freeze(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object arg = args.length < 1 ? Undefined.instance : args[0];
if (cx.getLanguageVersion() >= Context.VERSION_ES6 && !(arg instanceof ScriptableObject)) {
return arg;
}
boolean status =
AbstractEcmaObjectOperations.setIntegrityLevel(
cx, arg, AbstractEcmaObjectOperations.INTEGRITY_LEVEL.FROZEN);
if (!status) {
throw ScriptRuntime.typeError("Object is not freezable");
}
return arg;
}
private static Object js_assign(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Scriptable targetObj;
if (args.length > 0) {
targetObj = ScriptRuntime.toObject(cx, s, args[0]);
} else {
targetObj = ScriptRuntime.toObject(cx, s, Undefined.instance);
}
for (int i = 1; i < args.length; i++) {
if ((args[i] == null) || Undefined.isUndefined(args[i])) {
continue;
}
Scriptable sourceObj = ScriptRuntime.toObject(cx, s, args[i]);
Object[] ids;
if (sourceObj instanceof ScriptableObject) {
var scriptable = (ScriptableObject) sourceObj;
try (var map = scriptable.startCompoundOp(false)) {
ids = scriptable.getIds(map, false, true);
}
} else {
ids = sourceObj.getIds();
}
for (Object key : ids) {
if (key instanceof Integer) {
int intId = (Integer) key;
if (sourceObj.has(intId, sourceObj) && isEnumerable(intId, sourceObj)) {
Object val = sourceObj.get(intId, sourceObj);
AbstractEcmaObjectOperations.put(cx, targetObj, intId, val, true);
}
} else if (key instanceof String) {
String stringId = ScriptRuntime.toString(key);
if (sourceObj.has(stringId, sourceObj) && isEnumerable(stringId, sourceObj)) {
Object val = sourceObj.get(stringId, sourceObj);
AbstractEcmaObjectOperations.put(cx, targetObj, stringId, val, true);
}
}
}
// This is a separate loop for Symbols, as they must be
// copied over after string properties
if (sourceObj instanceof ScriptableObject) {
for (Object key : ids) {
if (key instanceof Symbol) {
Symbol sym = (Symbol) key;
if (((ScriptableObject) sourceObj).has(sym, sourceObj)
&& isEnumerable(sym, sourceObj)) {
Object val = ((ScriptableObject) sourceObj).get(sym, sourceObj);
AbstractEcmaObjectOperations.put(cx, targetObj, sym, val, true);
}
}
}
}
}
return targetObj;
}
private static Object js_is(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object a1 = args.length < 1 ? Undefined.instance : args[0];
Object a2 = args.length < 2 ? Undefined.instance : args[1];
return ScriptRuntime.wrapBoolean(ScriptRuntime.same(a1, a2));
}
private static Object js_groupBy(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Object items = args.length < 1 ? Undefined.instance : args[0];
Object callback = args.length < 2 ? Undefined.instance : args[1];
Map<Object, List<Object>> groups =
AbstractEcmaObjectOperations.groupBy(
cx,
s,
OBJECT_TAG,
"groupBy",
items,
callback,
AbstractEcmaObjectOperations.KEY_COERCION.PROPERTY);
NativeObject obj = (NativeObject) cx.newObject(s);
obj.setPrototype(null);
for (Map.Entry<Object, List<Object>> entry : groups.entrySet()) {
Scriptable elements = cx.newArray(s, entry.getValue().toArray());
ScriptableObject desc = (ScriptableObject) cx.newObject(f.getDeclarationScope());
desc.put("enumerable", desc, Boolean.TRUE);
desc.put("configurable", desc, Boolean.TRUE);
desc.put("value", desc, elements);
obj.defineOwnProperty(cx, entry.getKey(), desc);
}
return obj;
}
private static boolean isEnumerable(int index, Object obj) {
if (obj instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) obj;
try {
int attrs = so.getAttributes(index);
return (attrs & ScriptableObject.DONTENUM) == 0;
} catch (RhinoException re) {
// Not all ScriptableObject implementations implement
// "getAttributes" for all properties
return true;
}
} else {
return true;
}
}
private static boolean isEnumerable(String key, Object obj) {
if (obj instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) obj;
try {
int attrs = so.getAttributes(key);
return (attrs & ScriptableObject.DONTENUM) == 0;
} catch (RhinoException re) {
return true;
}
} else {
return true;
}
}
private static boolean isEnumerable(Symbol sym, Object obj) {
if (obj instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) obj;
try {
int attrs = so.getAttributes(sym);
return (attrs & ScriptableObject.DONTENUM) == 0;
} catch (RhinoException re) {
return true;
}
} else {
return true;
}
}
private static Scriptable getCompatibleObject(Context cx, Scriptable scope, Object arg) {
if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
Scriptable s = ScriptRuntime.toObject(cx, scope, arg);
return ensureScriptable(s);
}
return ensureScriptable(arg);
}
// methods implementing java.util.Map
@Override
public boolean containsKey(Object key) {
if (key instanceof String) {
return has((String) key, this);
} else if (key instanceof Number) {
return has(((Number) key).intValue(), this);
}
return false;
}
@Override
public boolean containsValue(Object value) {
for (Object obj : values()) {
if (Objects.equals(value, obj)) {
return true;
}
}
return false;
}
@Override
public Object remove(Object key) {
Object value = get(key);
if (key instanceof String) {
delete((String) key);
} else if (key instanceof Number) {
delete(((Number) key).intValue());
}
return value;
}
@Override
public Set<Object> keySet() {
return new KeySet();
}
@Override
public Collection<Object> values() {
return new ValueCollection();
}
@Override
public Set<Map.Entry<Object, Object>> entrySet() {
return new EntrySet();
}
@Override
public Object put(Object key, Object value) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
class EntrySet extends AbstractSet<Entry<Object, Object>> {
@Override
public Iterator<Entry<Object, Object>> iterator() {
return new Iterator<Map.Entry<Object, Object>>() {
Object[] ids = getIds();
Object key = null;
int index = 0;
@Override
public boolean hasNext() {
return index < ids.length;
}
@Override
public Map.Entry<Object, Object> next() {
final Object ekey = key = ids[index++];
final Object value = get(key);
return new Map.Entry<Object, Object>() {
@Override
public Object getKey() {
return ekey;
}
@Override
public Object getValue() {
return value;
}
@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Map.Entry)) {
return false;
}
Map.Entry<?, ?> e = (Map.Entry<?, ?>) other;
return (ekey == null ? e.getKey() == null : ekey.equals(e.getKey()))
&& (value == null
? e.getValue() == null
: value.equals(e.getValue()));
}
@Override
public int hashCode() {
return (ekey == null ? 0 : ekey.hashCode())
^ (value == null ? 0 : value.hashCode());
}
@Override
public String toString() {
return ekey + "=" + value;
}
};
}
@Override
public void remove() {
if (key == null) {
throw new IllegalStateException();
}
NativeObject.this.remove(key);
key = null;
}
};
}
@Override
public int size() {
return NativeObject.this.size();
}
}
class KeySet extends AbstractSet<Object> {
@Override
public boolean contains(Object key) {
return containsKey(key);
}
@Override
public Iterator<Object> iterator() {
return new Iterator<Object>() {
Object[] ids = getIds();
Object key;
int index = 0;
@Override
public boolean hasNext() {
return index < ids.length;
}
@Override
public Object next() {
try {
return (key = ids[index++]);
} catch (ArrayIndexOutOfBoundsException e) {
key = null;
throw new NoSuchElementException();
}
}
@Override
public void remove() {