-
Notifications
You must be signed in to change notification settings - Fork 54
/
JSObject.cpp
4292 lines (3683 loc) · 129 KB
/
JSObject.cpp
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: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* 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/. */
/*
* JS object implementation.
*/
#include "vm/JSObject-inl.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TemplateLib.h"
#include <string.h>
#include "jsapi.h"
#include "jsexn.h"
#include "jsfriendapi.h"
#include "jsnum.h"
#include "jstypes.h"
#include "jsutil.h"
#include "builtin/Array.h"
#include "builtin/BigInt.h"
#include "builtin/Eval.h"
#include "builtin/Object.h"
#include "builtin/String.h"
#include "builtin/Symbol.h"
#include "builtin/WeakSetObject.h"
#include "frontend/BytecodeCompiler.h"
#include "gc/Policy.h"
#include "jit/BaselineJIT.h"
#include "js/CharacterEncoding.h"
#include "js/MemoryMetrics.h"
#include "js/PropertyDescriptor.h" // JS::FromPropertyDescriptor
#include "js/PropertySpec.h" // JSPropertySpec
#include "js/Proxy.h"
#include "js/UbiNode.h"
#include "js/UniquePtr.h"
#include "js/Wrapper.h"
#include "util/Text.h"
#include "util/Windows.h"
#include "vm/ArgumentsObject.h"
#include "vm/BytecodeUtil.h"
#include "vm/DateObject.h"
#include "vm/Interpreter.h"
#include "vm/Iteration.h"
#include "vm/JSAtom.h"
#include "vm/JSContext.h"
#include "vm/JSFunction.h"
#include "vm/JSScript.h"
#include "vm/ProxyObject.h"
#include "vm/RegExpStaticsObject.h"
#include "vm/Shape.h"
#include "vm/TypedArrayObject.h"
#include "builtin/Boolean-inl.h"
#include "builtin/TypedObject-inl.h"
#include "gc/Marking-inl.h"
#include "vm/ArrayObject-inl.h"
#include "vm/BooleanObject-inl.h"
#include "vm/Caches-inl.h"
#include "vm/Compartment-inl.h"
#include "vm/Interpreter-inl.h"
#include "vm/JSAtom-inl.h"
#include "vm/JSContext-inl.h"
#include "vm/JSFunction-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/NumberObject-inl.h"
#include "vm/ObjectGroup-inl.h"
#include "vm/Realm-inl.h"
#include "vm/Shape-inl.h"
#include "vm/StringObject-inl.h"
#include "vm/TypedArrayObject-inl.h"
#include "vm/TypeInference-inl.h"
using namespace js;
void js::ReportNotObject(JSContext* cx, JSErrNum err, int spindex,
HandleValue v) {
MOZ_ASSERT(!v.isObject());
ReportValueError(cx, err, spindex, v, nullptr);
}
void js::ReportNotObject(JSContext* cx, JSErrNum err, HandleValue v) {
ReportNotObject(cx, err, JSDVG_SEARCH_STACK, v);
}
void js::ReportNotObject(JSContext* cx, const Value& v) {
RootedValue value(cx, v);
ReportNotObject(cx, JSMSG_OBJECT_REQUIRED, value);
}
void js::ReportNotObjectArg(JSContext* cx, const char* nth, const char* fun,
HandleValue v) {
MOZ_ASSERT(!v.isObject());
UniqueChars bytes;
if (const char* chars = ValueToSourceForError(cx, v, bytes)) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr,
JSMSG_OBJECT_REQUIRED_ARG, nth, fun, chars);
}
}
JS_PUBLIC_API const char* JS::InformalValueTypeName(const Value& v) {
switch (v.type()) {
case ValueType::Double:
case ValueType::Int32:
return "number";
case ValueType::Boolean:
return "boolean";
case ValueType::Undefined:
return "undefined";
case ValueType::Null:
return "null";
case ValueType::String:
return "string";
case ValueType::Symbol:
return "symbol";
case ValueType::BigInt:
return "bigint";
case ValueType::Object:
return v.toObject().getClass()->name;
case ValueType::Magic:
return "magic";
case ValueType::PrivateGCThing:
break;
}
MOZ_CRASH("unexpected type");
}
// ES6 draft rev37 6.2.4.4 FromPropertyDescriptor
JS_PUBLIC_API bool JS::FromPropertyDescriptor(JSContext* cx,
Handle<PropertyDescriptor> desc,
MutableHandleValue vp) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(desc);
// Step 1.
if (!desc.object()) {
vp.setUndefined();
return true;
}
return FromPropertyDescriptorToObject(cx, desc, vp);
}
bool js::FromPropertyDescriptorToObject(JSContext* cx,
Handle<PropertyDescriptor> desc,
MutableHandleValue vp) {
// Step 2-3.
RootedObject obj(cx, NewBuiltinClassInstance<PlainObject>(cx));
if (!obj) {
return false;
}
const JSAtomState& names = cx->names();
// Step 4.
if (desc.hasValue()) {
if (!DefineDataProperty(cx, obj, names.value, desc.value())) {
return false;
}
}
// Step 5.
RootedValue v(cx);
if (desc.hasWritable()) {
v.setBoolean(desc.writable());
if (!DefineDataProperty(cx, obj, names.writable, v)) {
return false;
}
}
// Step 6.
if (desc.hasGetterObject()) {
if (JSObject* get = desc.getterObject()) {
v.setObject(*get);
} else {
v.setUndefined();
}
if (!DefineDataProperty(cx, obj, names.get, v)) {
return false;
}
}
// Step 7.
if (desc.hasSetterObject()) {
if (JSObject* set = desc.setterObject()) {
v.setObject(*set);
} else {
v.setUndefined();
}
if (!DefineDataProperty(cx, obj, names.set, v)) {
return false;
}
}
// Step 8.
if (desc.hasEnumerable()) {
v.setBoolean(desc.enumerable());
if (!DefineDataProperty(cx, obj, names.enumerable, v)) {
return false;
}
}
// Step 9.
if (desc.hasConfigurable()) {
v.setBoolean(desc.configurable());
if (!DefineDataProperty(cx, obj, names.configurable, v)) {
return false;
}
}
vp.setObject(*obj);
return true;
}
bool js::GetFirstArgumentAsObject(JSContext* cx, const CallArgs& args,
const char* method,
MutableHandleObject objp) {
if (!args.requireAtLeast(cx, method, 1)) {
return false;
}
HandleValue v = args[0];
if (!v.isObject()) {
UniqueChars bytes =
DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, nullptr);
if (!bytes) {
return false;
}
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_UNEXPECTED_TYPE, bytes.get(),
"not an object");
return false;
}
objp.set(&v.toObject());
return true;
}
static bool GetPropertyIfPresent(JSContext* cx, HandleObject obj, HandleId id,
MutableHandleValue vp, bool* foundp) {
if (!HasProperty(cx, obj, id, foundp)) {
return false;
}
if (!*foundp) {
vp.setUndefined();
return true;
}
return GetProperty(cx, obj, obj, id, vp);
}
bool js::Throw(JSContext* cx, HandleId id, unsigned errorNumber,
const char* details) {
MOZ_ASSERT(js_ErrorFormatString[errorNumber].argCount == (details ? 2 : 1));
MOZ_ASSERT_IF(details, JS::StringIsASCII(details));
UniqueChars bytes =
IdToPrintableUTF8(cx, id, IdToPrintableBehavior::IdIsPropertyKey);
if (!bytes) {
return false;
}
if (details) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber,
bytes.get(), details);
} else {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber,
bytes.get());
}
return false;
}
/*** PropertyDescriptor operations and DefineProperties *********************/
static const char js_getter_str[] = "getter";
static const char js_setter_str[] = "setter";
static Result<> CheckCallable(JSContext* cx, JSObject* obj,
const char* fieldName) {
if (obj && !obj->isCallable()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, fieldName);
return cx->alreadyReportedError();
}
return Ok();
}
bool js::ToPropertyDescriptor(JSContext* cx, HandleValue descval,
bool checkAccessors,
MutableHandle<PropertyDescriptor> desc) {
// step 2
RootedObject obj(cx,
RequireObject(cx, JSMSG_OBJECT_REQUIRED_PROP_DESC, descval));
if (!obj) {
return false;
}
// step 3
desc.clear();
bool found = false;
RootedId id(cx);
RootedValue v(cx);
unsigned attrs = 0;
// step 4
id = NameToId(cx->names().enumerable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (ToBoolean(v)) {
attrs |= JSPROP_ENUMERATE;
}
} else {
attrs |= JSPROP_IGNORE_ENUMERATE;
}
// step 5
id = NameToId(cx->names().configurable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (!ToBoolean(v)) {
attrs |= JSPROP_PERMANENT;
}
} else {
attrs |= JSPROP_IGNORE_PERMANENT;
}
// step 6
id = NameToId(cx->names().value);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
desc.value().set(v);
} else {
attrs |= JSPROP_IGNORE_VALUE;
}
// step 7
id = NameToId(cx->names().writable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (!ToBoolean(v)) {
attrs |= JSPROP_READONLY;
}
} else {
attrs |= JSPROP_IGNORE_READONLY;
}
// step 8
bool hasGetOrSet;
id = NameToId(cx->names().get);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
hasGetOrSet = found;
if (found) {
if (v.isObject()) {
if (checkAccessors) {
JS_TRY_OR_RETURN_FALSE(cx,
CheckCallable(cx, &v.toObject(), js_getter_str));
}
desc.setGetterObject(&v.toObject());
} else if (!v.isUndefined()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, js_getter_str);
return false;
}
attrs |= JSPROP_GETTER;
}
// step 9
id = NameToId(cx->names().set);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
hasGetOrSet |= found;
if (found) {
if (v.isObject()) {
if (checkAccessors) {
JS_TRY_OR_RETURN_FALSE(cx,
CheckCallable(cx, &v.toObject(), js_setter_str));
}
desc.setSetterObject(&v.toObject());
} else if (!v.isUndefined()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, js_setter_str);
return false;
}
attrs |= JSPROP_SETTER;
}
// step 10
if (hasGetOrSet) {
if (!(attrs & JSPROP_IGNORE_READONLY) || !(attrs & JSPROP_IGNORE_VALUE)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_INVALID_DESCRIPTOR);
return false;
}
// By convention, these bits are not used on accessor descriptors.
attrs &= ~(JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE);
}
desc.setAttributes(attrs);
MOZ_ASSERT_IF(attrs & JSPROP_READONLY,
!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
return true;
}
Result<> js::CheckPropertyDescriptorAccessors(JSContext* cx,
Handle<PropertyDescriptor> desc) {
if (desc.hasGetterObject()) {
MOZ_TRY(CheckCallable(cx, desc.getterObject(), js_getter_str));
}
if (desc.hasSetterObject()) {
MOZ_TRY(CheckCallable(cx, desc.setterObject(), js_setter_str));
}
return Ok();
}
void js::CompletePropertyDescriptor(MutableHandle<PropertyDescriptor> desc) {
desc.assertValid();
if (desc.isGenericDescriptor() || desc.isDataDescriptor()) {
if (!desc.hasWritable()) {
desc.attributesRef() |= JSPROP_READONLY;
}
desc.attributesRef() &= ~(JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE);
} else {
if (!desc.hasGetterObject()) {
desc.setGetterObject(nullptr);
}
if (!desc.hasSetterObject()) {
desc.setSetterObject(nullptr);
}
desc.attributesRef() |= JSPROP_GETTER | JSPROP_SETTER;
}
if (!desc.hasConfigurable()) {
desc.attributesRef() |= JSPROP_PERMANENT;
}
desc.attributesRef() &= ~(JSPROP_IGNORE_PERMANENT | JSPROP_IGNORE_ENUMERATE);
desc.assertComplete();
}
bool js::ReadPropertyDescriptors(
JSContext* cx, HandleObject props, bool checkAccessors,
MutableHandleIdVector ids, MutableHandle<PropertyDescriptorVector> descs) {
if (!GetPropertyKeys(cx, props, JSITER_OWNONLY | JSITER_SYMBOLS, ids)) {
return false;
}
RootedId id(cx);
for (size_t i = 0, len = ids.length(); i < len; i++) {
id = ids[i];
Rooted<PropertyDescriptor> desc(cx);
RootedValue v(cx);
if (!GetProperty(cx, props, props, id, &v) ||
!ToPropertyDescriptor(cx, v, checkAccessors, &desc) ||
!descs.append(desc)) {
return false;
}
}
return true;
}
/*** Seal and freeze ********************************************************/
static unsigned GetSealedOrFrozenAttributes(unsigned attrs,
IntegrityLevel level) {
// Make all attributes permanent; if freezing, make data attributes
// read-only.
if (level == IntegrityLevel::Frozen &&
!(attrs & (JSPROP_GETTER | JSPROP_SETTER))) {
return JSPROP_PERMANENT | JSPROP_READONLY;
}
return JSPROP_PERMANENT;
}
/* ES6 draft rev 29 (6 Dec 2014) 7.3.13. */
bool js::SetIntegrityLevel(JSContext* cx, HandleObject obj,
IntegrityLevel level) {
cx->check(obj);
// Steps 3-5. (Steps 1-2 are redundant assertions.)
if (!PreventExtensions(cx, obj)) {
return false;
}
// Steps 6-9, loosely interpreted.
if (obj->isNative() && !obj->as<NativeObject>().inDictionaryMode() &&
!obj->is<TypedArrayObject>() && !obj->is<MappedArgumentsObject>()) {
HandleNativeObject nobj = obj.as<NativeObject>();
// Seal/freeze non-dictionary objects by constructing a new shape
// hierarchy mirroring the original one, which can be shared if many
// objects with the same structure are sealed/frozen. If we use the
// generic path below then any non-empty object will be converted to
// dictionary mode.
RootedShape last(
cx, EmptyShape::getInitialShape(
cx, nobj->getClass(), nobj->taggedProto(),
nobj->numFixedSlots(), nobj->lastProperty()->getObjectFlags()));
if (!last) {
return false;
}
// Get an in-order list of the shapes in this object.
using ShapeVec = GCVector<Shape*, 8>;
Rooted<ShapeVec> shapes(cx, ShapeVec(cx));
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
if (!shapes.append(&r.front())) {
return false;
}
}
Reverse(shapes.begin(), shapes.end());
for (Shape* shape : shapes) {
Rooted<StackShape> child(cx, StackShape(shape));
child.setAttrs(child.attrs() |
GetSealedOrFrozenAttributes(child.attrs(), level));
if (!JSID_IS_EMPTY(child.get().propid) &&
level == IntegrityLevel::Frozen) {
MarkTypePropertyNonWritable(cx, nobj, child.get().propid);
}
last = cx->zone()->propertyTree().getChild(cx, last, child);
if (!last) {
return false;
}
}
MOZ_ASSERT(nobj->lastProperty()->slotSpan() == last->slotSpan());
MOZ_ALWAYS_TRUE(nobj->setLastProperty(cx, last));
// Ordinarily ArraySetLength handles this, but we're going behind its back
// right now, so we must do this manually.
if (level == IntegrityLevel::Frozen && obj->is<ArrayObject>()) {
MOZ_ASSERT(!nobj->denseElementsAreCopyOnWrite());
obj->as<ArrayObject>().setNonWritableLength(cx);
}
} else {
// Steps 6-7.
RootedIdVector keys(cx);
if (!GetPropertyKeys(
cx, obj, JSITER_HIDDEN | JSITER_OWNONLY | JSITER_SYMBOLS, &keys)) {
return false;
}
RootedId id(cx);
Rooted<PropertyDescriptor> desc(cx);
const unsigned AllowConfigure =
JSPROP_IGNORE_ENUMERATE | JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE;
const unsigned AllowConfigureAndWritable =
AllowConfigure & ~JSPROP_IGNORE_READONLY;
// 8.a/9.a. The two different loops are merged here.
for (size_t i = 0; i < keys.length(); i++) {
id = keys[i];
if (level == IntegrityLevel::Sealed) {
// 8.a.i.
desc.setAttributes(AllowConfigure | JSPROP_PERMANENT);
} else {
// 9.a.i-ii.
Rooted<PropertyDescriptor> currentDesc(cx);
if (!GetOwnPropertyDescriptor(cx, obj, id, ¤tDesc)) {
return false;
}
// 9.a.iii.
if (!currentDesc.object()) {
continue;
}
// 9.a.iii.1-2
if (currentDesc.isAccessorDescriptor()) {
desc.setAttributes(AllowConfigure | JSPROP_PERMANENT);
} else {
desc.setAttributes(AllowConfigureAndWritable | JSPROP_PERMANENT |
JSPROP_READONLY);
}
}
// 8.a.i-ii. / 9.a.iii.3-4
if (!DefineProperty(cx, obj, id, desc)) {
return false;
}
}
}
// Finally, freeze or seal the dense elements.
if (obj->isNative()) {
ObjectElements::FreezeOrSeal(cx, &obj->as<NativeObject>(), level);
}
return true;
}
static bool ResolveLazyProperties(JSContext* cx, HandleNativeObject obj) {
const JSClass* clasp = obj->getClass();
if (JSEnumerateOp enumerate = clasp->getEnumerate()) {
if (!enumerate(cx, obj)) {
return false;
}
}
if (clasp->getNewEnumerate() && clasp->getResolve()) {
RootedIdVector properties(cx);
if (!clasp->getNewEnumerate()(cx, obj, &properties,
/* enumerableOnly = */ false)) {
return false;
}
RootedId id(cx);
for (size_t i = 0; i < properties.length(); i++) {
id = properties[i];
bool found;
if (!HasOwnProperty(cx, obj, id, &found)) {
return false;
}
}
}
return true;
}
// ES6 draft rev33 (12 Feb 2015) 7.3.15
bool js::TestIntegrityLevel(JSContext* cx, HandleObject obj,
IntegrityLevel level, bool* result) {
// Steps 3-6. (Steps 1-2 are redundant assertions.)
bool status;
if (!IsExtensible(cx, obj, &status)) {
return false;
}
if (status) {
*result = false;
return true;
}
// Fast path for native objects.
if (obj->isNative()) {
HandleNativeObject nobj = obj.as<NativeObject>();
// Force lazy properties to be resolved.
if (!ResolveLazyProperties(cx, nobj)) {
return false;
}
// Typed array elements are non-configurable, writable properties, so
// if any elements are present, the typed array cannot be frozen.
if (nobj->is<TypedArrayObject>() &&
nobj->as<TypedArrayObject>().length() > 0 &&
level == IntegrityLevel::Frozen) {
*result = false;
return true;
}
bool hasDenseElements = false;
for (size_t i = 0; i < nobj->getDenseInitializedLength(); i++) {
if (nobj->containsDenseElement(i)) {
hasDenseElements = true;
break;
}
}
if (hasDenseElements) {
// Unless the sealed flag is set, dense elements are configurable.
if (!nobj->denseElementsAreSealed()) {
*result = false;
return true;
}
// Unless the frozen flag is set, dense elements are writable.
if (level == IntegrityLevel::Frozen && !nobj->denseElementsAreFrozen()) {
*result = false;
return true;
}
}
// Steps 7-9.
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
Shape* shape = &r.front();
// Steps 9.c.i-ii.
if (shape->configurable() ||
(level == IntegrityLevel::Frozen && shape->isDataDescriptor() &&
shape->writable())) {
*result = false;
return true;
}
}
} else {
// Steps 7-8.
RootedIdVector props(cx);
if (!GetPropertyKeys(
cx, obj, JSITER_HIDDEN | JSITER_OWNONLY | JSITER_SYMBOLS, &props)) {
return false;
}
// Step 9.
RootedId id(cx);
Rooted<PropertyDescriptor> desc(cx);
for (size_t i = 0, len = props.length(); i < len; i++) {
id = props[i];
// Steps 9.a-b.
if (!GetOwnPropertyDescriptor(cx, obj, id, &desc)) {
return false;
}
// Step 9.c.
if (!desc.object()) {
continue;
}
// Steps 9.c.i-ii.
if (desc.configurable() || (level == IntegrityLevel::Frozen &&
desc.isDataDescriptor() && desc.writable())) {
*result = false;
return true;
}
}
}
// Step 10.
*result = true;
return true;
}
/* * */
/*
* Get the GC kind to use for scripted 'new' on the given class.
* FIXME bug 547327: estimate the size from the allocation site.
*/
static inline gc::AllocKind NewObjectGCKind(const JSClass* clasp) {
if (clasp == &ArrayObject::class_) {
return gc::AllocKind::OBJECT8;
}
if (clasp == &JSFunction::class_) {
return gc::AllocKind::OBJECT2;
}
return gc::AllocKind::OBJECT4;
}
static inline JSObject* NewObject(JSContext* cx, HandleObjectGroup group,
gc::AllocKind kind, NewObjectKind newKind,
uint32_t initialShapeFlags = 0) {
const JSClass* clasp = group->clasp();
MOZ_ASSERT(clasp != &ArrayObject::class_);
MOZ_ASSERT_IF(clasp == &JSFunction::class_,
kind == gc::AllocKind::FUNCTION ||
kind == gc::AllocKind::FUNCTION_EXTENDED);
// For objects which can have fixed data following the object, only use
// enough fixed slots to cover the number of reserved slots in the object,
// regardless of the allocation kind specified.
size_t nfixed = ClassCanHaveFixedData(clasp)
? GetGCKindSlots(gc::GetGCObjectKind(clasp), clasp)
: GetGCKindSlots(kind, clasp);
RootedShape shape(cx, EmptyShape::getInitialShape(cx, clasp, group->proto(),
nfixed, initialShapeFlags));
if (!shape) {
return nullptr;
}
gc::InitialHeap heap = GetInitialHeap(newKind, group);
JSObject* obj;
if (clasp->isJSFunction()) {
JS_TRY_VAR_OR_RETURN_NULL(cx, obj,
JSFunction::create(cx, kind, heap, shape, group));
} else if (MOZ_LIKELY(clasp->isNative())) {
JS_TRY_VAR_OR_RETURN_NULL(
cx, obj, NativeObject::create(cx, kind, heap, shape, group));
} else {
MOZ_ASSERT(IsTypedObjectClass(clasp));
JS_TRY_VAR_OR_RETURN_NULL(
cx, obj, TypedObject::create(cx, kind, heap, shape, group));
}
if (newKind == SingletonObject) {
RootedObject nobj(cx, obj);
if (!JSObject::setSingleton(cx, nobj)) {
return nullptr;
}
obj = nobj;
}
probes::CreateObject(cx, obj);
return obj;
}
void NewObjectCache::fillProto(EntryIndex entry, const JSClass* clasp,
js::TaggedProto proto, gc::AllocKind kind,
NativeObject* obj) {
MOZ_ASSERT_IF(proto.isObject(), !proto.toObject()->is<GlobalObject>());
MOZ_ASSERT(obj->taggedProto() == proto);
return fill(entry, clasp, proto.raw(), kind, obj);
}
bool js::NewObjectWithTaggedProtoIsCachable(JSContext* cx,
Handle<TaggedProto> proto,
NewObjectKind newKind,
const JSClass* clasp) {
return !cx->isHelperThreadContext() && proto.isObject() &&
newKind == GenericObject && clasp->isNative() &&
!proto.toObject()->is<GlobalObject>();
}
JSObject* js::NewObjectWithGivenTaggedProto(JSContext* cx, const JSClass* clasp,
Handle<TaggedProto> proto,
gc::AllocKind allocKind,
NewObjectKind newKind,
uint32_t initialShapeFlags) {
if (CanChangeToBackgroundAllocKind(allocKind, clasp)) {
allocKind = ForegroundToBackgroundAllocKind(allocKind);
}
bool isCachable =
NewObjectWithTaggedProtoIsCachable(cx, proto, newKind, clasp);
if (isCachable) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
if (cache.lookupProto(clasp, proto.toObject(), allocKind, &entry)) {
JSObject* obj =
cache.newObjectFromHit(cx, entry, GetInitialHeap(newKind, clasp));
if (obj) {
return obj;
}
}
}
RootedObjectGroup group(
cx, ObjectGroup::defaultNewGroup(cx, clasp, proto, nullptr));
if (!group) {
return nullptr;
}
RootedObject obj(cx,
NewObject(cx, group, allocKind, newKind, initialShapeFlags));
if (!obj) {
return nullptr;
}
if (isCachable && !obj->as<NativeObject>().hasDynamicSlots()) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
cache.lookupProto(clasp, proto.toObject(), allocKind, &entry);
cache.fillProto(entry, clasp, proto, allocKind, &obj->as<NativeObject>());
}
return obj;
}
static bool NewObjectIsCachable(JSContext* cx, NewObjectKind newKind,
const JSClass* clasp) {
return !cx->isHelperThreadContext() && newKind == GenericObject &&
clasp->isNative();
}
JSObject* js::NewObjectWithClassProtoCommon(JSContext* cx, const JSClass* clasp,
HandleObject protoArg,
gc::AllocKind allocKind,
NewObjectKind newKind) {
if (protoArg) {
return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(protoArg),
allocKind, newKind);
}
if (CanChangeToBackgroundAllocKind(allocKind, clasp)) {
allocKind = ForegroundToBackgroundAllocKind(allocKind);
}
Handle<GlobalObject*> global = cx->global();
bool isCachable = NewObjectIsCachable(cx, newKind, clasp);
if (isCachable) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
if (cache.lookupGlobal(clasp, global, allocKind, &entry)) {
gc::InitialHeap heap = GetInitialHeap(newKind, clasp);
JSObject* obj = cache.newObjectFromHit(cx, entry, heap);
if (obj) {
return obj;
}
}
}
// Find the appropriate proto for clasp. Built-in classes have a cached
// proto on cx->global(); all others get %ObjectPrototype%.
JSProtoKey protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
if (protoKey == JSProto_Null) {
protoKey = JSProto_Object;
}
JSObject* proto = GlobalObject::getOrCreatePrototype(cx, protoKey);
if (!proto) {
return nullptr;
}
RootedObjectGroup group(
cx, ObjectGroup::defaultNewGroup(cx, clasp, TaggedProto(proto)));
if (!group) {
return nullptr;
}
JSObject* obj = NewObject(cx, group, allocKind, newKind);
if (!obj) {
return nullptr;
}
if (isCachable && !obj->as<NativeObject>().hasDynamicSlots()) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
cache.lookupGlobal(clasp, global, allocKind, &entry);
cache.fillGlobal(entry, clasp, global, allocKind, &obj->as<NativeObject>());
}
return obj;
}
static bool NewObjectWithGroupIsCachable(JSContext* cx, HandleObjectGroup group,
NewObjectKind newKind) {
if (!group->proto().isObject() || newKind != GenericObject ||
!group->clasp()->isNative() || cx->isHelperThreadContext()) {
return false;
}
AutoSweepObjectGroup sweep(group);
return !group->newScript(sweep) || group->newScript(sweep)->analyzed();
}
/*
* Create a plain object with the specified group. This bypasses getNewGroup to
* avoid losing creation site information for objects made by scripted 'new'.
*/
JSObject* js::NewObjectWithGroupCommon(JSContext* cx, HandleObjectGroup group,
gc::AllocKind allocKind,
NewObjectKind newKind) {
MOZ_ASSERT(gc::IsObjectAllocKind(allocKind));
if (CanChangeToBackgroundAllocKind(allocKind, group->clasp())) {
allocKind = ForegroundToBackgroundAllocKind(allocKind);
}
bool isCachable = NewObjectWithGroupIsCachable(cx, group, newKind);
if (isCachable) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
if (cache.lookupGroup(group, allocKind, &entry)) {
JSObject* obj =
cache.newObjectFromHit(cx, entry, GetInitialHeap(newKind, group));
if (obj) {
return obj;
}
}
}
JSObject* obj = NewObject(cx, group, allocKind, newKind);
if (!obj) {
return nullptr;
}
if (isCachable && !obj->as<NativeObject>().hasDynamicSlots()) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
cache.lookupGroup(group, allocKind, &entry);
cache.fillGroup(entry, group, allocKind, &obj->as<NativeObject>());
}
return obj;
}
bool js::NewObjectScriptedCall(JSContext* cx, MutableHandleObject pobj) {
jsbytecode* pc;
RootedScript script(cx, cx->currentScript(&pc));