forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsobj.h
1872 lines (1509 loc) · 63.2 KB
/
jsobj.h
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: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef jsobj_h___
#define jsobj_h___
/*
* JS object definitions.
*
* A JS object consists of a possibly-shared object descriptor containing
* ordered property names, called the map; and a dense vector of property
* values, called slots. The map/slot pointer pair is GC'ed, while the map
* is reference counted and the slot vector is malloc'ed.
*/
#include "jsapi.h"
#include "jshash.h"
#include "jspubtd.h"
#include "jsprvtd.h"
#include "jslock.h"
#include "jsvalue.h"
#include "jsvector.h"
#include "jscell.h"
namespace js {
class JSProxyHandler;
class AutoPropDescArrayRooter;
namespace mjit {
class Compiler;
}
static inline PropertyOp
CastAsPropertyOp(JSObject *object)
{
return JS_DATA_TO_FUNC_PTR(PropertyOp, object);
}
static inline StrictPropertyOp
CastAsStrictPropertyOp(JSObject *object)
{
return JS_DATA_TO_FUNC_PTR(StrictPropertyOp, object);
}
static inline JSPropertyOp
CastAsJSPropertyOp(JSObject *object)
{
return JS_DATA_TO_FUNC_PTR(JSPropertyOp, object);
}
static inline JSStrictPropertyOp
CastAsJSStrictPropertyOp(JSObject *object)
{
return JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, object);
}
inline JSObject *
CastAsObject(PropertyOp op)
{
return JS_FUNC_TO_DATA_PTR(JSObject *, op);
}
inline JSObject *
CastAsObject(StrictPropertyOp op)
{
return JS_FUNC_TO_DATA_PTR(JSObject *, op);
}
inline Value
CastAsObjectJsval(PropertyOp op)
{
return ObjectOrNullValue(CastAsObject(op));
}
inline Value
CastAsObjectJsval(StrictPropertyOp op)
{
return ObjectOrNullValue(CastAsObject(op));
}
/*
* A representation of ECMA-262 ed. 5's internal Property Descriptor data
* structure.
*/
struct PropDesc {
/*
* Original object from which this descriptor derives, passed through for
* the benefit of proxies.
*/
js::Value pd;
js::Value value, get, set;
/* Property descriptor boolean fields. */
uint8 attrs;
/* Bits indicating which values are set. */
bool hasGet : 1;
bool hasSet : 1;
bool hasValue : 1;
bool hasWritable : 1;
bool hasEnumerable : 1;
bool hasConfigurable : 1;
friend class js::AutoPropDescArrayRooter;
PropDesc();
public:
/* 8.10.5 ToPropertyDescriptor(Obj) */
bool initialize(JSContext* cx, const js::Value &v);
/* 8.10.1 IsAccessorDescriptor(desc) */
bool isAccessorDescriptor() const {
return hasGet || hasSet;
}
/* 8.10.2 IsDataDescriptor(desc) */
bool isDataDescriptor() const {
return hasValue || hasWritable;
}
/* 8.10.3 IsGenericDescriptor(desc) */
bool isGenericDescriptor() const {
return !isAccessorDescriptor() && !isDataDescriptor();
}
bool configurable() const {
return (attrs & JSPROP_PERMANENT) == 0;
}
bool enumerable() const {
return (attrs & JSPROP_ENUMERATE) != 0;
}
bool writable() const {
return (attrs & JSPROP_READONLY) == 0;
}
JSObject* getterObject() const {
return get.isUndefined() ? NULL : &get.toObject();
}
JSObject* setterObject() const {
return set.isUndefined() ? NULL : &set.toObject();
}
const js::Value &getterValue() const {
return get;
}
const js::Value &setterValue() const {
return set;
}
js::PropertyOp getter() const {
return js::CastAsPropertyOp(getterObject());
}
js::StrictPropertyOp setter() const {
return js::CastAsStrictPropertyOp(setterObject());
}
};
typedef Vector<PropDesc, 1> PropDescArray;
void
MeterEntryCount(uintN count);
} /* namespace js */
enum {
INVALID_SHAPE = 0x8fffffff,
SHAPELESS = 0xffffffff
};
/*
* On success, and if id was found, return true with *objp non-null and with a
* property of *objp stored in *propp. If successful but id was not found,
* return true with both *objp and *propp null.
*/
extern JS_FRIEND_API(JSBool)
js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
JSProperty **propp);
extern JSBool
js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, const js::Value *value,
js::PropertyOp getter, js::StrictPropertyOp setter, uintN attrs);
extern JSBool
js_GetProperty(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id, js::Value *vp);
inline JSBool
js_GetProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *vp)
{
return js_GetProperty(cx, obj, obj, id, vp);
}
namespace js {
extern JSBool
GetPropertyDefault(JSContext *cx, JSObject *obj, jsid id, const Value &def, Value *vp);
} /* namespace js */
extern JSBool
js_SetProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *vp, JSBool strict);
extern JSBool
js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
extern JSBool
js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
extern JSBool
js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *rval, JSBool strict);
extern JS_FRIEND_API(JSBool)
js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
js::Value *statep, jsid *idp);
extern JSType
js_TypeOf(JSContext *cx, JSObject *obj);
namespace js {
struct NativeIterator;
class RegExp;
class GlobalObject;
class ArgumentsObject;
class NormalArgumentsObject;
class StrictArgumentsObject;
class StringObject;
}
struct JSFunction;
namespace nanojit {
class ValidateWriter;
}
/*
* JSObject struct, with members sized to fit in 32 bytes on 32-bit targets,
* 64 bytes on 64-bit systems. The JSFunction struct is an extension of this
* struct allocated from a larger GC size-class.
*
* The clasp member stores the js::Class pointer for this object. We do *not*
* synchronize updates of clasp or flags -- API clients must take care.
*
* An object is a delegate if it is on another object's prototype (the proto
* field) or scope chain (the parent field), and therefore the delegate might
* be asked implicitly to get or set a property on behalf of another object.
* Delegates may be accessed directly too, as may any object, but only those
* objects linked after the head of any prototype or scope chain are flagged
* as delegates. This definition helps to optimize shape-based property cache
* invalidation (see Purge{Scope,Proto}Chain in jsobj.cpp).
*
* The meaning of the system object bit is defined by the API client. It is
* set in JS_NewSystemObject and is queried by JS_IsSystemObject (jsdbgapi.h),
* but it has no intrinsic meaning to SpiderMonkey. Further, JSFILENAME_SYSTEM
* and JS_FlagScriptFilenamePrefix (also exported via jsdbgapi.h) are intended
* to be complementary to this bit, but it is up to the API client to implement
* any such association.
*
* Both these flag bits are initially zero; they may be set or queried using
* the (is|set)(Delegate|System) inline methods.
*
* The slots member is a pointer to the slot vector for the object.
* This can be either a fixed array allocated immediately after the object,
* or a dynamically allocated array. A dynamic array can be tested for with
* hasSlotsArray(). In all cases, capacity gives the number of usable slots.
* Two objects with the same shape have the same number of fixed slots,
* and either both have or neither have dynamically allocated slot arrays.
*
* If you change this struct, you'll probably need to change the AccSet values
* in jsbuiltins.h.
*/
struct JSObject : js::gc::Cell {
/*
* TraceRecorder must be a friend because it generates code that
* manipulates JSObjects, which requires peeking under any encapsulation.
* ValidateWriter must be a friend because it works in tandem with
* TraceRecorder.
*/
friend class js::TraceRecorder;
friend class nanojit::ValidateWriter;
friend class GetPropCompiler;
/*
* Private pointer to the last added property and methods to manipulate the
* list it links among properties in this scope.
*/
js::Shape *lastProp;
js::Class *clasp;
private:
inline void setLastProperty(const js::Shape *shape);
inline void removeLastProperty();
/* For setLastProperty() only. */
friend class js::StringObject;
#ifdef DEBUG
void checkShapeConsistency();
#endif
public:
inline const js::Shape *lastProperty() const;
inline js::Shape **nativeSearch(jsid id, bool adding = false);
inline const js::Shape *nativeLookup(jsid id);
inline bool nativeContains(jsid id);
inline bool nativeContains(const js::Shape &shape);
enum {
DELEGATE = 0x01,
SYSTEM = 0x02,
NOT_EXTENSIBLE = 0x04,
BRANDED = 0x08,
GENERIC = 0x10,
METHOD_BARRIER = 0x20,
INDEXED = 0x40,
OWN_SHAPE = 0x80,
BOUND_FUNCTION = 0x100,
HAS_EQUALITY = 0x200,
METHOD_THRASH_COUNT_MASK = 0xc00,
METHOD_THRASH_COUNT_SHIFT = 10,
METHOD_THRASH_COUNT_MAX = METHOD_THRASH_COUNT_MASK >> METHOD_THRASH_COUNT_SHIFT
};
/*
* Impose a sane upper bound, originally checked only for dense arrays, on
* number of slots in an object.
*/
enum {
NSLOTS_BITS = 29,
NSLOTS_LIMIT = JS_BIT(NSLOTS_BITS)
};
uint32 flags; /* flags */
uint32 objShape; /* copy of lastProp->shape, or override if different */
/* If prototype, lazily filled array of empty shapes for each object size. */
js::EmptyShape **emptyShapes;
JSObject *proto; /* object's prototype */
JSObject *parent; /* object's parent */
void *privateData; /* private data */
jsuword capacity; /* capacity of slots */
js::Value *slots; /* dynamically allocated slots,
or pointer to fixedSlots() */
/*
* Return an immutable, shareable, empty shape with the same clasp as this
* and the same slotSpan as this had when empty.
*
* If |this| is the scope of an object |proto|, the resulting scope can be
* used as the scope of a new object whose prototype is |proto|.
*/
inline bool canProvideEmptyShape(js::Class *clasp);
inline js::EmptyShape *getEmptyShape(JSContext *cx, js::Class *aclasp,
/* gc::FinalizeKind */ unsigned kind);
inline bool isNative() const;
inline bool isNewborn() const;
js::Class *getClass() const { return clasp; }
JSClass *getJSClass() const { return Jsvalify(clasp); }
bool hasClass(const js::Class *c) const {
return c == clasp;
}
const js::ObjectOps *getOps() const {
return &getClass()->ops;
}
uint32 shape() const {
JS_ASSERT(objShape != INVALID_SHAPE);
return objShape;
}
bool isDelegate() const { return !!(flags & DELEGATE); }
void setDelegate() { flags |= DELEGATE; }
void clearDelegate() { flags &= ~DELEGATE; }
bool isBoundFunction() const { return !!(flags & BOUND_FUNCTION); }
static void setDelegateNullSafe(JSObject *obj) {
if (obj)
obj->setDelegate();
}
bool isSystem() const { return !!(flags & SYSTEM); }
void setSystem() { flags |= SYSTEM; }
/*
* A branded object contains plain old methods (function-valued properties
* without magic getters and setters), and its shape evolves whenever a
* function value changes.
*/
bool branded() { return !!(flags & BRANDED); }
/*
* NB: these return false on shape overflow but do not report any error.
* Callers who depend on shape guarantees should therefore bail off trace,
* e.g., on false returns.
*/
bool brand(JSContext *cx);
bool unbrand(JSContext *cx);
bool generic() { return !!(flags & GENERIC); }
void setGeneric() { flags |= GENERIC; }
uintN getMethodThrashCount() const {
return (flags & METHOD_THRASH_COUNT_MASK) >> METHOD_THRASH_COUNT_SHIFT;
}
void setMethodThrashCount(uintN count) {
JS_ASSERT(count <= METHOD_THRASH_COUNT_MAX);
flags = (flags & ~METHOD_THRASH_COUNT_MASK) | (count << METHOD_THRASH_COUNT_SHIFT);
}
bool hasSpecialEquality() const { return !!(flags & HAS_EQUALITY); }
void assertSpecialEqualitySynced() const {
JS_ASSERT(!!clasp->ext.equality == hasSpecialEquality());
}
/* Sets an object's HAS_EQUALITY flag based on its clasp. */
inline void syncSpecialEquality();
private:
void generateOwnShape(JSContext *cx);
inline void setOwnShape(uint32 s);
inline void clearOwnShape();
public:
inline bool nativeEmpty() const;
bool hasOwnShape() const { return !!(flags & OWN_SHAPE); }
inline void setMap(js::Shape *amap);
inline void setSharedNonNativeMap();
/* Functions for setting up scope chain object maps and shapes. */
void initCall(JSContext *cx, const js::Bindings &bindings, JSObject *parent);
void initClonedBlock(JSContext *cx, JSObject *proto, js::StackFrame *priv);
void setBlockOwnShape(JSContext *cx);
void deletingShapeChange(JSContext *cx, const js::Shape &shape);
const js::Shape *methodShapeChange(JSContext *cx, const js::Shape &shape);
bool methodShapeChange(JSContext *cx, uint32 slot);
void protoShapeChange(JSContext *cx);
void shadowingShapeChange(JSContext *cx, const js::Shape &shape);
bool globalObjectOwnShapeChange(JSContext *cx);
void watchpointOwnShapeChange(JSContext *cx) { generateOwnShape(cx); }
void extensibleShapeChange(JSContext *cx) {
/* This will do for now. */
generateOwnShape(cx);
}
/*
* A scope has a method barrier when some compiler-created "null closure"
* function objects (functions that do not use lexical bindings above their
* scope, only free variable names) that have a correct JSSLOT_PARENT value
* thanks to the COMPILE_N_GO optimization are stored as newly added direct
* property values of the scope's object.
*
* The de-facto standard JS language requires each evaluation of such a
* closure to result in a unique (according to === and observable effects)
* function object. ES3 tried to allow implementations to "join" such
* objects to a single compiler-created object, but this makes an overt
* mutation hazard, also an "identity hazard" against interoperation among
* implementations that join and do not join.
*
* To stay compatible with the de-facto standard, we store the compiler-
* created function object as the method value and set the METHOD_BARRIER
* flag.
*
* The method value is part of the method property tree node's identity, so
* it effectively brands the scope with a predictable shape corresponding
* to the method value, but without the overhead of setting the BRANDED
* flag, which requires assigning a new shape peculiar to each branded
* scope. Instead the shape is shared via the property tree among all the
* scopes referencing the method property tree node.
*
* Then when reading from a scope for which scope->hasMethodBarrier() is
* true, we count on the scope's qualified/guarded shape being unique and
* add a read barrier that clones the compiler-created function object on
* demand, reshaping the scope.
*
* This read barrier is bypassed when evaluating the callee sub-expression
* of a call expression (see the JOF_CALLOP opcodes in jsopcode.tbl), since
* such ops do not present an identity or mutation hazard. The compiler
* performs this optimization only for null closures that do not use their
* own name or equivalent built-in references (arguments.callee).
*
* The BRANDED write barrier, JSObject::methodWriteBarrer, must check for
* METHOD_BARRIER too, and regenerate this scope's shape if the method's
* value is in fact changing.
*/
bool hasMethodBarrier() { return !!(flags & METHOD_BARRIER); }
void setMethodBarrier() { flags |= METHOD_BARRIER; }
/*
* Test whether this object may be branded due to method calls, which means
* any assignment to a function-valued property must regenerate shape; else
* test whether this object has method properties, which require a method
* write barrier.
*/
bool brandedOrHasMethodBarrier() { return !!(flags & (BRANDED | METHOD_BARRIER)); }
/*
* Read barrier to clone a joined function object stored as a method.
* Defined in jsobjinlines.h, but not declared inline per standard style in
* order to avoid gcc warnings.
*/
const js::Shape *methodReadBarrier(JSContext *cx, const js::Shape &shape, js::Value *vp);
/*
* Write barrier to check for a change of method value. Defined inline in
* jsobjinlines.h after methodReadBarrier. The slot flavor is required by
* JSOP_*GVAR, which deals in slots not shapes, while not deoptimizing to
* map slot to shape unless JSObject::flags show that this is necessary.
* The methodShapeChange overload (above) parallels this.
*/
const js::Shape *methodWriteBarrier(JSContext *cx, const js::Shape &shape, const js::Value &v);
bool methodWriteBarrier(JSContext *cx, uint32 slot, const js::Value &v);
bool isIndexed() const { return !!(flags & INDEXED); }
void setIndexed() { flags |= INDEXED; }
/*
* Return true if this object is a native one that has been converted from
* shared-immutable prototype-rooted shape storage to dictionary-shapes in
* a doubly-linked list.
*/
inline bool inDictionaryMode() const;
inline uint32 propertyCount() const;
inline bool hasPropertyTable() const;
/* gc::FinalizeKind */ unsigned finalizeKind() const;
uint32 numSlots() const { return capacity; }
size_t slotsAndStructSize(uint32 nslots) const;
size_t slotsAndStructSize() const { return slotsAndStructSize(numSlots()); }
inline js::Value* fixedSlots() const;
inline size_t numFixedSlots() const;
static inline size_t getFixedSlotOffset(size_t slot);
public:
/* Minimum size for dynamically allocated slots. */
static const uint32 SLOT_CAPACITY_MIN = 8;
bool allocSlots(JSContext *cx, size_t nslots);
bool growSlots(JSContext *cx, size_t nslots);
void shrinkSlots(JSContext *cx, size_t nslots);
bool ensureSlots(JSContext *cx, size_t nslots) {
if (numSlots() < nslots)
return growSlots(cx, nslots);
return true;
}
/*
* Ensure that the object has at least JSCLASS_RESERVED_SLOTS(clasp) +
* nreserved slots.
*
* This method may be called only for native objects freshly created using
* NewObject or one of its variant where the new object will both (a) never
* escape to script and (b) never be extended with ad-hoc properties that
* would try to allocate higher slots without the fresh object first having
* its map set to a shape path that maps those slots.
*
* Block objects satisfy (a) and (b), as there is no evil eval-based way to
* add ad-hoc properties to a Block instance. Call objects satisfy (a) and
* (b) as well, because the compiler-created Shape path that covers args,
* vars, and upvars, stored in their callee function in u.i.names, becomes
* their initial map.
*/
bool ensureInstanceReservedSlots(JSContext *cx, size_t nreserved);
/*
* Get a direct pointer to the object's slots.
* This can be reallocated if the object is modified, watch out!
*/
js::Value *getSlots() const {
return slots;
}
/*
* NB: ensureClassReservedSlotsForEmptyObject asserts that nativeEmpty()
* Use ensureClassReservedSlots for any object, either empty or already
* extended with properties.
*/
bool ensureClassReservedSlotsForEmptyObject(JSContext *cx);
inline bool ensureClassReservedSlots(JSContext *cx);
inline uint32 slotSpan() const;
inline bool containsSlot(uint32 slot) const;
js::Value& getSlotRef(uintN slot) {
JS_ASSERT(slot < capacity);
return slots[slot];
}
inline js::Value &nativeGetSlotRef(uintN slot);
const js::Value &getSlot(uintN slot) const {
JS_ASSERT(slot < capacity);
return slots[slot];
}
inline const js::Value &nativeGetSlot(uintN slot) const;
void setSlot(uintN slot, const js::Value &value) {
JS_ASSERT(slot < capacity);
slots[slot] = value;
}
inline void nativeSetSlot(uintN slot, const js::Value &value);
inline js::Value getReservedSlot(uintN index) const;
/* Defined in jsscopeinlines.h to avoid including implementation dependencies here. */
inline void updateShape(JSContext *cx);
inline void updateFlags(const js::Shape *shape, bool isDefinitelyAtom = false);
/* Extend this object to have shape as its last-added property. */
inline void extend(JSContext *cx, const js::Shape *shape, bool isDefinitelyAtom = false);
JSObject *getProto() const { return proto; }
void clearProto() { proto = NULL; }
void setProto(JSObject *newProto) {
#ifdef DEBUG
for (JSObject *obj = newProto; obj; obj = obj->getProto())
JS_ASSERT(obj != this);
#endif
setDelegateNullSafe(newProto);
proto = newProto;
}
JSObject *getParent() const {
return parent;
}
void clearParent() {
parent = NULL;
}
void setParent(JSObject *newParent) {
#ifdef DEBUG
for (JSObject *obj = newParent; obj; obj = obj->getParent())
JS_ASSERT(obj != this);
#endif
setDelegateNullSafe(newParent);
parent = newParent;
}
JS_FRIEND_API(js::GlobalObject *) getGlobal() const;
bool isGlobal() const {
return !!(getClass()->flags & JSCLASS_IS_GLOBAL);
}
inline js::GlobalObject *asGlobal();
void *getPrivate() const {
JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
return privateData;
}
void setPrivate(void *data) {
JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
privateData = data;
}
/* N.B. Infallible: NULL means 'no principal', not an error. */
inline JSPrincipals *principals(JSContext *cx);
/*
* ES5 meta-object properties and operations.
*/
private:
enum ImmutabilityType { SEAL, FREEZE };
/*
* The guts of Object.seal (ES5 15.2.3.8) and Object.freeze (ES5 15.2.3.9): mark the
* object as non-extensible, and adjust each property's attributes appropriately: each
* property becomes non-configurable, and if |freeze|, data properties become
* read-only as well.
*/
bool sealOrFreeze(JSContext *cx, ImmutabilityType it);
public:
bool isExtensible() const { return !(flags & NOT_EXTENSIBLE); }
bool preventExtensions(JSContext *cx, js::AutoIdVector *props);
/* ES5 15.2.3.8: non-extensible, all props non-configurable */
inline bool seal(JSContext *cx) { return sealOrFreeze(cx, SEAL); }
/* ES5 15.2.3.9: non-extensible, all properties non-configurable, all data props read-only */
bool freeze(JSContext *cx) { return sealOrFreeze(cx, FREEZE); }
/*
* Primitive-specific getters and setters.
*/
private:
static const uint32 JSSLOT_PRIMITIVE_THIS = 0;
public:
inline const js::Value &getPrimitiveThis() const;
inline void setPrimitiveThis(const js::Value &pthis);
public:
inline js::StringObject *asString();
/*
* Array-specific getters and setters (for both dense and slow arrays).
*/
inline uint32 getArrayLength() const;
inline void setArrayLength(uint32 length);
inline uint32 getDenseArrayCapacity();
inline js::Value* getDenseArrayElements();
inline const js::Value &getDenseArrayElement(uintN idx);
inline js::Value* addressOfDenseArrayElement(uintN idx);
inline void setDenseArrayElement(uintN idx, const js::Value &val);
inline void shrinkDenseArrayElements(JSContext *cx, uintN cap);
/*
* ensureDenseArrayElements ensures that the dense array can hold at least
* index + extra elements. It returns ED_OK on success, ED_FAILED on
* failure to grow the array, ED_SPARSE when the array is too sparse to
* grow (this includes the case of index + extra overflow). In the last
* two cases the array is kept intact.
*/
enum EnsureDenseResult { ED_OK, ED_FAILED, ED_SPARSE };
inline EnsureDenseResult ensureDenseArrayElements(JSContext *cx, uintN index, uintN extra);
/*
* Check if after growing the dense array will be too sparse.
* newElementsHint is an estimated number of elements to be added.
*/
bool willBeSparseDenseArray(uintN requiredCapacity, uintN newElementsHint);
JSBool makeDenseArraySlow(JSContext *cx);
public:
inline js::ArgumentsObject *asArguments();
inline js::NormalArgumentsObject *asNormalArguments();
inline js::StrictArgumentsObject *asStrictArguments();
private:
/*
* Reserved slot structure for Call objects:
*
* private - the stack frame corresponding to the Call object
* until js_PutCallObject or its on-trace analog
* is called, null thereafter
* JSSLOT_CALL_CALLEE - callee function for the stack frame, or null if
* the stack frame is for strict mode eval code
* JSSLOT_CALL_ARGUMENTS - arguments object for non-strict mode eval stack
* frames (not valid for strict mode eval frames)
*/
static const uint32 JSSLOT_CALL_CALLEE = 0;
static const uint32 JSSLOT_CALL_ARGUMENTS = 1;
public:
/* Number of reserved slots. */
static const uint32 CALL_RESERVED_SLOTS = 2;
/* True if this is for a strict mode eval frame or for a function call. */
inline bool callIsForEval() const;
/* The stack frame for this Call object, if the frame is still active. */
inline js::StackFrame *maybeCallObjStackFrame() const;
/*
* The callee function if this Call object was created for a function
* invocation, or null if it was created for a strict mode eval frame.
*/
inline JSObject *getCallObjCallee() const;
inline JSFunction *getCallObjCalleeFunction() const;
inline void setCallObjCallee(JSObject *callee);
inline const js::Value &getCallObjArguments() const;
inline void setCallObjArguments(const js::Value &v);
/* Returns the formal argument at the given index. */
inline const js::Value &callObjArg(uintN i) const;
inline js::Value &callObjArg(uintN i);
/* Returns the variable at the given index. */
inline const js::Value &callObjVar(uintN i) const;
inline js::Value &callObjVar(uintN i);
/*
* Date-specific getters and setters.
*/
static const uint32 JSSLOT_DATE_UTC_TIME = 0;
/*
* Cached slots holding local properties of the date.
* These are undefined until the first actual lookup occurs
* and are reset to undefined whenever the date's time is modified.
*/
static const uint32 JSSLOT_DATE_COMPONENTS_START = 1;
static const uint32 JSSLOT_DATE_LOCAL_TIME = 1;
static const uint32 JSSLOT_DATE_LOCAL_YEAR = 2;
static const uint32 JSSLOT_DATE_LOCAL_MONTH = 3;
static const uint32 JSSLOT_DATE_LOCAL_DATE = 4;
static const uint32 JSSLOT_DATE_LOCAL_DAY = 5;
static const uint32 JSSLOT_DATE_LOCAL_HOURS = 6;
static const uint32 JSSLOT_DATE_LOCAL_MINUTES = 7;
static const uint32 JSSLOT_DATE_LOCAL_SECONDS = 8;
static const uint32 DATE_CLASS_RESERVED_SLOTS = 9;
inline const js::Value &getDateUTCTime() const;
inline void setDateUTCTime(const js::Value &pthis);
/*
* Function-specific getters and setters.
*/
private:
friend struct JSFunction;
friend class js::mjit::Compiler;
/*
* Flat closures with one or more upvars snapshot the upvars' values into a
* vector of js::Values referenced from this slot.
*/
static const uint32 JSSLOT_FLAT_CLOSURE_UPVARS = 0;
/*
* Null closures set or initialized as methods have these slots. See the
* "method barrier" comments and methods.
*/
static const uint32 JSSLOT_FUN_METHOD_ATOM = 0;
static const uint32 JSSLOT_FUN_METHOD_OBJ = 1;
static const uint32 JSSLOT_BOUND_FUNCTION_THIS = 0;
static const uint32 JSSLOT_BOUND_FUNCTION_ARGS_COUNT = 1;
public:
static const uint32 FUN_CLASS_RESERVED_SLOTS = 2;
inline JSFunction *getFunctionPrivate() const;
inline js::Value *getFlatClosureUpvars() const;
inline js::Value getFlatClosureUpvar(uint32 i) const;
inline js::Value &getFlatClosureUpvar(uint32 i);
inline void setFlatClosureUpvars(js::Value *upvars);
inline bool hasMethodObj(const JSObject& obj) const;
inline void setMethodObj(JSObject& obj);
inline bool initBoundFunction(JSContext *cx, const js::Value &thisArg,
const js::Value *args, uintN argslen);
inline JSObject *getBoundFunctionTarget() const;
inline const js::Value &getBoundFunctionThis() const;
inline const js::Value *getBoundFunctionArguments(uintN &argslen) const;
/*
* RegExp-specific getters and setters.
*/
private:
static const uint32 JSSLOT_REGEXP_LAST_INDEX = 0;
static const uint32 JSSLOT_REGEXP_SOURCE = 1;
static const uint32 JSSLOT_REGEXP_GLOBAL = 2;
static const uint32 JSSLOT_REGEXP_IGNORE_CASE = 3;
static const uint32 JSSLOT_REGEXP_MULTILINE = 4;
static const uint32 JSSLOT_REGEXP_STICKY = 5;
/*
* Compute the initial shape to associate with fresh regular expression
* objects, encoding their initial properties. Return the shape after
* changing this regular expression object's last property to it.
*/
const js::Shape *assignInitialRegExpShape(JSContext *cx);
public:
static const uint32 REGEXP_CLASS_RESERVED_SLOTS = 6;
inline const js::Value &getRegExpLastIndex() const;
inline void setRegExpLastIndex(const js::Value &v);
inline void setRegExpLastIndex(jsdouble d);
inline void zeroRegExpLastIndex();
inline void setRegExpSource(JSString *source);
inline void setRegExpGlobal(bool global);
inline void setRegExpIgnoreCase(bool ignoreCase);
inline void setRegExpMultiline(bool multiline);
inline void setRegExpSticky(bool sticky);
inline bool initRegExp(JSContext *cx, js::RegExp *re);
/*
* Iterator-specific getters and setters.
*/
inline js::NativeIterator *getNativeIterator() const;
inline void setNativeIterator(js::NativeIterator *);
/*
* Script-related getters.
*/
inline JSScript *getScript() const;
/*
* XML-related getters and setters.
*/
/*
* Slots for XML-related classes are as follows:
* - js_NamespaceClass.base reserves the *_NAME_* and *_NAMESPACE_* slots.
* - js_QNameClass.base, js_AttributeNameClass, js_AnyNameClass reserve
* the *_NAME_* and *_QNAME_* slots.
* - Others (js_XMLClass, js_XMLFilterClass) don't reserve any slots.
*/
private:
static const uint32 JSSLOT_NAME_PREFIX = 0; // shared
static const uint32 JSSLOT_NAME_URI = 1; // shared
static const uint32 JSSLOT_NAMESPACE_DECLARED = 2;
static const uint32 JSSLOT_QNAME_LOCAL_NAME = 2;
public:
static const uint32 NAMESPACE_CLASS_RESERVED_SLOTS = 3;
static const uint32 QNAME_CLASS_RESERVED_SLOTS = 3;
inline JSLinearString *getNamePrefix() const;
inline jsval getNamePrefixVal() const;
inline void setNamePrefix(JSLinearString *prefix);
inline void clearNamePrefix();
inline JSLinearString *getNameURI() const;
inline jsval getNameURIVal() const;
inline void setNameURI(JSLinearString *uri);