ICs for scripted new (bug 589398, r=luke,dmandelin).
[mozilla-central.git] / js / src / jsobj.h
blobc76af07c3ed33f3a507d01539c8c949210b8a59d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
25 * Contributor(s):
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef jsobj_h___
42 #define jsobj_h___
44 * JS object definitions.
46 * A JS object consists of a possibly-shared object descriptor containing
47 * ordered property names, called the map; and a dense vector of property
48 * values, called slots. The map/slot pointer pair is GC'ed, while the map
49 * is reference counted and the slot vector is malloc'ed.
51 #include "jsapi.h"
52 #include "jshash.h"
53 #include "jspubtd.h"
54 #include "jsprvtd.h"
55 #include "jslock.h"
56 #include "jsvalue.h"
57 #include "jsvector.h"
58 #include "jscell.h"
60 namespace js {
62 class JSProxyHandler;
63 class AutoPropDescArrayRooter;
65 namespace mjit {
66 class Compiler;
69 static inline PropertyOp
70 CastAsPropertyOp(JSObject *object)
72 return JS_DATA_TO_FUNC_PTR(PropertyOp, object);
75 static inline JSPropertyOp
76 CastAsJSPropertyOp(JSObject *object)
78 return JS_DATA_TO_FUNC_PTR(JSPropertyOp, object);
81 inline JSObject *
82 CastAsObject(PropertyOp op)
84 return JS_FUNC_TO_DATA_PTR(JSObject *, op);
87 inline Value
88 CastAsObjectJsval(PropertyOp op)
90 return ObjectOrNullValue(CastAsObject(op));
93 } /* namespace js */
96 * A representation of ECMA-262 ed. 5's internal property descriptor data
97 * structure.
99 struct PropDesc {
100 friend class js::AutoPropDescArrayRooter;
102 PropDesc();
104 public:
105 /* 8.10.5 ToPropertyDescriptor(Obj) */
106 bool initialize(JSContext* cx, jsid id, const js::Value &v);
108 /* 8.10.1 IsAccessorDescriptor(desc) */
109 bool isAccessorDescriptor() const {
110 return hasGet || hasSet;
113 /* 8.10.2 IsDataDescriptor(desc) */
114 bool isDataDescriptor() const {
115 return hasValue || hasWritable;
118 /* 8.10.3 IsGenericDescriptor(desc) */
119 bool isGenericDescriptor() const {
120 return !isAccessorDescriptor() && !isDataDescriptor();
123 bool configurable() const {
124 return (attrs & JSPROP_PERMANENT) == 0;
127 bool enumerable() const {
128 return (attrs & JSPROP_ENUMERATE) != 0;
131 bool writable() const {
132 return (attrs & JSPROP_READONLY) == 0;
135 JSObject* getterObject() const {
136 return get.isUndefined() ? NULL : &get.toObject();
138 JSObject* setterObject() const {
139 return set.isUndefined() ? NULL : &set.toObject();
142 const js::Value &getterValue() const {
143 return get;
145 const js::Value &setterValue() const {
146 return set;
149 js::PropertyOp getter() const {
150 return js::CastAsPropertyOp(getterObject());
152 js::PropertyOp setter() const {
153 return js::CastAsPropertyOp(setterObject());
156 js::Value pd;
157 jsid id;
158 js::Value value, get, set;
160 /* Property descriptor boolean fields. */
161 uint8 attrs;
163 /* Bits indicating which values are set. */
164 bool hasGet : 1;
165 bool hasSet : 1;
166 bool hasValue : 1;
167 bool hasWritable : 1;
168 bool hasEnumerable : 1;
169 bool hasConfigurable : 1;
172 namespace js {
174 typedef Vector<PropDesc, 1> PropDescArray;
176 } /* namespace js */
178 struct JSObjectMap {
179 static JS_FRIEND_DATA(const JSObjectMap) sharedNonNative;
181 uint32 shape; /* shape identifier */
182 uint32 slotSpan; /* one more than maximum live slot number */
184 explicit JSObjectMap(uint32 shape) : shape(shape), slotSpan(0) {}
185 JSObjectMap(uint32 shape, uint32 slotSpan) : shape(shape), slotSpan(slotSpan) {}
187 enum { INVALID_SHAPE = 0x8fffffff, SHAPELESS = 0xffffffff };
189 bool isNative() const { return this != &sharedNonNative; }
191 private:
192 /* No copy or assignment semantics. */
193 JSObjectMap(JSObjectMap &);
194 void operator=(JSObjectMap &);
198 * Unlike js_DefineNativeProperty, propp must be non-null. On success, and if
199 * id was found, return true with *objp non-null and locked, and with a held
200 * property stored in *propp. If successful but id was not found, return true
201 * with both *objp and *propp null. Therefore all callers who receive a
202 * non-null *propp must later call (*objp)->dropProperty(cx, *propp).
204 extern JS_FRIEND_API(JSBool)
205 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
206 JSProperty **propp);
208 extern JSBool
209 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, const js::Value *value,
210 js::PropertyOp getter, js::PropertyOp setter, uintN attrs);
212 extern JSBool
213 js_GetProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *vp);
215 extern JSBool
216 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *vp, JSBool strict);
218 extern JSBool
219 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
221 extern JSBool
222 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
224 extern JSBool
225 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, js::Value *rval, JSBool strict);
227 extern JS_FRIEND_API(JSBool)
228 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
229 js::Value *statep, jsid *idp);
231 extern JSType
232 js_TypeOf(JSContext *cx, JSObject *obj);
234 struct NativeIterator;
236 const uint32 JS_INITIAL_NSLOTS = 3;
239 * The first available slot to store generic value. For JSCLASS_HAS_PRIVATE
240 * classes the slot stores a pointer to private data stuffed in a Value.
241 * Such pointer is stored as is without an overhead of PRIVATE_TO_JSVAL
242 * tagging and should be accessed using the (get|set)Private methods of
243 * JSObject.
245 const uint32 JSSLOT_PRIVATE = 0;
247 struct JSFunction;
250 * JSObject struct, with members sized to fit in 32 bytes on 32-bit targets,
251 * 64 bytes on 64-bit systems. The JSFunction struct is an extension of this
252 * struct allocated from a larger GC size-class.
254 * The clasp member stores the js::Class pointer for this object. We do *not*
255 * synchronize updates of clasp or flags -- API clients must take care.
257 * An object is a delegate if it is on another object's prototype (the proto
258 * field) or scope chain (the parent field), and therefore the delegate might
259 * be asked implicitly to get or set a property on behalf of another object.
260 * Delegates may be accessed directly too, as may any object, but only those
261 * objects linked after the head of any prototype or scope chain are flagged
262 * as delegates. This definition helps to optimize shape-based property cache
263 * invalidation (see Purge{Scope,Proto}Chain in jsobj.cpp).
265 * The meaning of the system object bit is defined by the API client. It is
266 * set in JS_NewSystemObject and is queried by JS_IsSystemObject (jsdbgapi.h),
267 * but it has no intrinsic meaning to SpiderMonkey. Further, JSFILENAME_SYSTEM
268 * and JS_FlagScriptFilenamePrefix (also exported via jsdbgapi.h) are intended
269 * to be complementary to this bit, but it is up to the API client to implement
270 * any such association.
272 * Both these flag bits are initially zero; they may be set or queried using
273 * the (is|set)(Delegate|System) inline methods.
275 * The dslots member is null or a pointer into a dynamically allocated vector
276 * of Values for reserved and dynamic slots. If dslots is not null, dslots[-1]
277 * records the number of available slots.
279 struct JSObject : js::gc::Cell {
281 * TraceRecorder must be a friend because it generates code that
282 * manipulates JSObjects, which requires peeking under any encapsulation.
284 friend class js::TraceRecorder;
287 * Private pointer to the last added property and methods to manipulate the
288 * list it links among properties in this scope. The {remove,insert} pair
289 * for DictionaryProperties assert that the scope is in dictionary mode and
290 * any reachable properties are flagged as dictionary properties.
292 * NB: these private methods do *not* update this scope's shape to track
293 * lastProp->shape after they finish updating the linked list in the case
294 * where lastProp is updated. It is up to calling code in jsscope.cpp to
295 * call updateShape(cx) after updating lastProp.
297 union {
298 js::Shape *lastProp;
299 JSObjectMap *map;
302 js::Class *clasp;
304 private:
305 inline void setLastProperty(const js::Shape *shape);
306 inline void removeLastProperty();
308 #ifdef DEBUG
309 void checkShapeConsistency();
310 #endif
312 public:
313 inline const js::Shape *lastProperty() const;
315 inline js::Shape **nativeSearch(jsid id, bool adding = false);
316 inline const js::Shape *nativeLookup(jsid id);
318 inline bool nativeContains(jsid id);
319 inline bool nativeContains(const js::Shape &shape);
321 enum {
322 DELEGATE = 0x01,
323 SYSTEM = 0x02,
324 NOT_EXTENSIBLE = 0x04,
325 BRANDED = 0x08,
326 GENERIC = 0x10,
327 METHOD_BARRIER = 0x20,
328 INDEXED = 0x40,
329 OWN_SHAPE = 0x80,
330 BOUND_FUNCTION = 0x100
334 * Impose a sane upper bound, originally checked only for dense arrays, on
335 * number of slots in an object.
337 enum {
338 NSLOTS_BITS = 29,
339 NSLOTS_LIMIT = JS_BIT(NSLOTS_BITS)
342 uint32 flags; /* flags */
343 uint32 objShape; /* copy of lastProp->shape, or override if different */
345 JSObject *proto; /* object's prototype */
346 JSObject *parent; /* object's parent */
347 js::Value *dslots; /* dynamically allocated slots */
349 /* Empty shape of kids if prototype, located here to align fslots on 32 bit targets. */
350 js::EmptyShape *emptyShape;
352 js::Value fslots[JS_INITIAL_NSLOTS]; /* small number of fixed slots */
353 #ifdef JS_THREADSAFE
354 JSTitle title;
355 #endif
358 * Return an immutable, shareable, empty shape with the same clasp as this
359 * and the same slotSpan as this had when empty.
361 * If |this| is the scope of an object |proto|, the resulting scope can be
362 * used as the scope of a new object whose prototype is |proto|.
364 inline bool canProvideEmptyShape(js::Class *clasp);
365 inline js::EmptyShape *getEmptyShape(JSContext *cx, js::Class *aclasp);
367 bool isNative() const { return map->isNative(); }
369 js::Class *getClass() const { return clasp; }
370 JSClass *getJSClass() const { return Jsvalify(clasp); }
372 bool hasClass(const js::Class *c) const {
373 return c == clasp;
376 const js::ObjectOps *getOps() const {
377 return &getClass()->ops;
380 inline void trace(JSTracer *trc);
382 uint32 shape() const {
383 JS_ASSERT(objShape != JSObjectMap::INVALID_SHAPE);
384 return objShape;
387 bool isDelegate() const { return !!(flags & DELEGATE); }
388 void setDelegate() { flags |= DELEGATE; }
390 bool isBoundFunction() const { return !!(flags & BOUND_FUNCTION); }
392 static void setDelegateNullSafe(JSObject *obj) {
393 if (obj)
394 obj->setDelegate();
397 bool isSystem() const { return !!(flags & SYSTEM); }
398 void setSystem() { flags |= SYSTEM; }
401 * A branded object contains plain old methods (function-valued properties
402 * without magic getters and setters), and its shape evolves whenever a
403 * function value changes.
405 bool branded() { return !!(flags & BRANDED); }
407 bool brand(JSContext *cx, uint32 slot, js::Value v);
408 bool unbrand(JSContext *cx);
410 bool generic() { return !!(flags & GENERIC); }
411 void setGeneric() { flags |= GENERIC; }
413 private:
414 void generateOwnShape(JSContext *cx);
416 void setOwnShape(uint32 s) { flags |= OWN_SHAPE; objShape = s; }
417 void clearOwnShape() { flags &= ~OWN_SHAPE; objShape = map->shape; }
419 public:
420 inline bool nativeEmpty() const;
422 bool hasOwnShape() const { return !!(flags & OWN_SHAPE); }
424 void setMap(JSObjectMap *amap) {
425 JS_ASSERT(!hasOwnShape());
426 map = amap;
427 objShape = map->shape;
430 void setSharedNonNativeMap() {
431 setMap(const_cast<JSObjectMap *>(&JSObjectMap::sharedNonNative));
434 void deletingShapeChange(JSContext *cx, const js::Shape &shape);
435 bool methodShapeChange(JSContext *cx, const js::Shape &shape);
436 bool methodShapeChange(JSContext *cx, uint32 slot);
437 void protoShapeChange(JSContext *cx);
438 void shadowingShapeChange(JSContext *cx, const js::Shape &shape);
439 bool globalObjectOwnShapeChange(JSContext *cx);
441 void extensibleShapeChange(JSContext *cx) {
442 /* This will do for now. */
443 generateOwnShape(cx);
447 * A scope has a method barrier when some compiler-created "null closure"
448 * function objects (functions that do not use lexical bindings above their
449 * scope, only free variable names) that have a correct JSSLOT_PARENT value
450 * thanks to the COMPILE_N_GO optimization are stored as newly added direct
451 * property values of the scope's object.
453 * The de-facto standard JS language requires each evaluation of such a
454 * closure to result in a unique (according to === and observable effects)
455 * function object. ES3 tried to allow implementations to "join" such
456 * objects to a single compiler-created object, but this makes an overt
457 * mutation hazard, also an "identity hazard" against interoperation among
458 * implementations that join and do not join.
460 * To stay compatible with the de-facto standard, we store the compiler-
461 * created function object as the method value and set the METHOD_BARRIER
462 * flag.
464 * The method value is part of the method property tree node's identity, so
465 * it effectively brands the scope with a predictable shape corresponding
466 * to the method value, but without the overhead of setting the BRANDED
467 * flag, which requires assigning a new shape peculiar to each branded
468 * scope. Instead the shape is shared via the property tree among all the
469 * scopes referencing the method property tree node.
471 * Then when reading from a scope for which scope->hasMethodBarrier() is
472 * true, we count on the scope's qualified/guarded shape being unique and
473 * add a read barrier that clones the compiler-created function object on
474 * demand, reshaping the scope.
476 * This read barrier is bypassed when evaluating the callee sub-expression
477 * of a call expression (see the JOF_CALLOP opcodes in jsopcode.tbl), since
478 * such ops do not present an identity or mutation hazard. The compiler
479 * performs this optimization only for null closures that do not use their
480 * own name or equivalent built-in references (arguments.callee).
482 * The BRANDED write barrier, JSObject::methodWriteBarrer, must check for
483 * METHOD_BARRIER too, and regenerate this scope's shape if the method's
484 * value is in fact changing.
486 bool hasMethodBarrier() { return !!(flags & METHOD_BARRIER); }
487 void setMethodBarrier() { flags |= METHOD_BARRIER; }
490 * Test whether this object may be branded due to method calls, which means
491 * any assignment to a function-valued property must regenerate shape; else
492 * test whether this object has method properties, which require a method
493 * write barrier.
495 bool brandedOrHasMethodBarrier() { return !!(flags & (BRANDED | METHOD_BARRIER)); }
498 * Read barrier to clone a joined function object stored as a method.
499 * Defined in jsobjinlines.h, but not declared inline per standard style in
500 * order to avoid gcc warnings.
502 bool methodReadBarrier(JSContext *cx, const js::Shape &shape, js::Value *vp);
505 * Write barrier to check for a change of method value. Defined inline in
506 * jsobjinlines.h after methodReadBarrier. The slot flavor is required by
507 * JSOP_*GVAR, which deals in slots not shapes, while not deoptimizing to
508 * map slot to shape unless JSObject::flags show that this is necessary.
509 * The methodShapeChange overload (directly below) parallels this.
511 bool methodWriteBarrier(JSContext *cx, const js::Shape &shape, const js::Value &v);
512 bool methodWriteBarrier(JSContext *cx, uint32 slot, const js::Value &v);
514 bool isIndexed() const { return !!(flags & INDEXED); }
515 void setIndexed() { flags |= INDEXED; }
518 * Return true if this object is a native one that has been converted from
519 * shared-immutable prototype-rooted shape storage to dictionary-shapes in
520 * a doubly-linked list.
522 inline bool inDictionaryMode() const;
524 inline uint32 propertyCount() const;
526 inline bool hasPropertyTable() const;
528 uint32 numSlots(void) const {
529 return dslots ? dslots[-1].toPrivateUint32() : uint32(JS_INITIAL_NSLOTS);
532 size_t slotsAndStructSize(uint32 nslots) const;
533 size_t slotsAndStructSize() const { return slotsAndStructSize(numSlots()); }
535 private:
536 static size_t slotsToDynamicWords(size_t nslots) {
537 JS_ASSERT(nslots > JS_INITIAL_NSLOTS);
538 return nslots + 1 - JS_INITIAL_NSLOTS;
541 static size_t dynamicWordsToSlots(size_t nwords) {
542 JS_ASSERT(nwords > 1);
543 return nwords - 1 + JS_INITIAL_NSLOTS;
546 public:
547 bool allocSlots(JSContext *cx, size_t nslots);
548 bool growSlots(JSContext *cx, size_t nslots);
549 void shrinkSlots(JSContext *cx, size_t nslots);
552 * Ensure that the object has at least JSCLASS_RESERVED_SLOTS(clasp) +
553 * nreserved slots.
555 * This method may be called only for native objects freshly created using
556 * NewObject or one of its variant where the new object will both (a) never
557 * escape to script and (b) never be extended with ad-hoc properties that
558 * would try to allocate higher slots without the fresh object first having
559 * its map set to a shape path that maps those slots.
561 * Block objects satisfy (a) and (b), as there is no evil eval-based way to
562 * add ad-hoc properties to a Block instance. Call objects satisfy (a) and
563 * (b) as well, because the compiler-created Shape path that covers args,
564 * vars, and upvars, stored in their callee function in u.i.names, becomes
565 * their initial map.
567 bool ensureInstanceReservedSlots(JSContext *cx, size_t nreserved);
570 * NB: ensureClassReservedSlotsForEmptyObject asserts that nativeEmpty()
571 * Use ensureClassReservedSlots for any object, either empty or already
572 * extended with properties.
574 bool ensureClassReservedSlotsForEmptyObject(JSContext *cx);
576 inline bool ensureClassReservedSlots(JSContext *cx);
578 uint32 slotSpan() const { return map->slotSpan; }
580 bool containsSlot(uint32 slot) const { return slot < slotSpan(); }
582 js::Value& getSlotRef(uintN slot) {
583 return (slot < JS_INITIAL_NSLOTS)
584 ? fslots[slot]
585 : (JS_ASSERT(slot < dslots[-1].toPrivateUint32()),
586 dslots[slot - JS_INITIAL_NSLOTS]);
589 const js::Value &getSlot(uintN slot) const {
590 return (slot < JS_INITIAL_NSLOTS)
591 ? fslots[slot]
592 : (JS_ASSERT(slot < dslots[-1].toPrivateUint32()),
593 dslots[slot - JS_INITIAL_NSLOTS]);
596 void setSlot(uintN slot, const js::Value &value) {
597 if (slot < JS_INITIAL_NSLOTS) {
598 fslots[slot] = value;
599 } else {
600 JS_ASSERT(slot < dslots[-1].toPrivateUint32());
601 dslots[slot - JS_INITIAL_NSLOTS] = value;
605 inline const js::Value &lockedGetSlot(uintN slot) const;
606 inline void lockedSetSlot(uintN slot, const js::Value &value);
609 * These ones are for multi-threaded ("MT") objects. Use getSlot(),
610 * getSlotRef(), setSlot() to directly manipulate slots in obj when only
611 * one thread can access obj, or when accessing read-only slots within
612 * JS_INITIAL_NSLOTS.
614 inline js::Value getSlotMT(JSContext *cx, uintN slot);
615 inline void setSlotMT(JSContext *cx, uintN slot, const js::Value &value);
617 inline js::Value getReservedSlot(uintN index) const;
619 /* Defined in jsscopeinlines.h to avoid including implementation dependencies here. */
620 inline void updateShape(JSContext *cx);
621 inline void updateFlags(const js::Shape *shape, bool isDefinitelyAtom = false);
623 /* Extend this object to have shape as its last-added property. */
624 inline void extend(JSContext *cx, const js::Shape *shape, bool isDefinitelyAtom = false);
626 JSObject *getProto() const { return proto; }
627 void clearProto() { proto = NULL; }
629 void setProto(JSObject *newProto) {
630 #ifdef DEBUG
631 for (JSObject *obj = newProto; obj; obj = obj->getProto())
632 JS_ASSERT(obj != this);
633 #endif
634 setDelegateNullSafe(newProto);
635 proto = newProto;
638 JSObject *getParent() const {
639 return parent;
642 void clearParent() {
643 parent = NULL;
646 void setParent(JSObject *newParent) {
647 #ifdef DEBUG
648 for (JSObject *obj = newParent; obj; obj = obj->getParent())
649 JS_ASSERT(obj != this);
650 #endif
651 setDelegateNullSafe(newParent);
652 parent = newParent;
655 JSObject *getGlobal() const;
657 bool isGlobal() const {
658 return !!(getClass()->flags & JSCLASS_IS_GLOBAL);
661 void *getPrivate() const {
662 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
663 return *(void **)&fslots[JSSLOT_PRIVATE];
666 void setPrivate(void *data) {
667 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
668 *(void **)&fslots[JSSLOT_PRIVATE] = data;
673 * ES5 meta-object properties and operations.
676 private:
678 * The guts of Object.seal (ES5 15.2.3.8) and Object.freeze (ES5 15.2.3.9): mark the
679 * object as non-extensible, and adjust each property's attributes appropriately: each
680 * property becomes non-configurable, and if |freeze|, data properties become
681 * read-only as well.
683 bool sealOrFreeze(JSContext *cx, bool freeze = false);
685 public:
686 bool isExtensible() const { return !(flags & NOT_EXTENSIBLE); }
687 bool preventExtensions(JSContext *cx, js::AutoIdVector *props);
689 /* ES5 15.2.3.8: non-extensible, all props non-configurable */
690 inline bool seal(JSContext *cx) { return sealOrFreeze(cx); }
691 /* ES5 15.2.3.9: non-extensible, all properties non-configurable, all data props read-only */
692 bool freeze(JSContext *cx) { return sealOrFreeze(cx, true); }
695 * Primitive-specific getters and setters.
698 private:
699 static const uint32 JSSLOT_PRIMITIVE_THIS = JSSLOT_PRIVATE;
701 public:
702 inline const js::Value &getPrimitiveThis() const;
703 inline void setPrimitiveThis(const js::Value &pthis);
706 * Array-specific getters and setters (for both dense and slow arrays).
709 // Used by dense and slow arrays.
710 static const uint32 JSSLOT_ARRAY_LENGTH = JSSLOT_PRIVATE;
712 static const uint32 JSSLOT_DENSE_ARRAY_CAPACITY = JSSLOT_PRIVATE + 1;
714 // This assertion must remain true; see comment in js_MakeArraySlow().
715 // (Nb: This method is never called, it just contains a static assertion.
716 // The static assertion isn't inline because that doesn't work on Mac.)
717 inline void staticAssertArrayLengthIsInPrivateSlot();
719 public:
720 static const uint32 DENSE_ARRAY_CLASS_RESERVED_SLOTS = 3;
722 inline uint32 getArrayLength() const;
723 inline void setArrayLength(uint32 length);
725 inline uint32 getDenseArrayCapacity() const;
726 inline void setDenseArrayCapacity(uint32 capacity);
728 inline const js::Value &getDenseArrayElement(uint32 i) const;
729 inline js::Value *addressOfDenseArrayElement(uint32 i);
730 inline void setDenseArrayElement(uint32 i, const js::Value &v);
732 inline js::Value *getDenseArrayElements() const; // returns pointer to the Array's elements array
733 bool growDenseArrayElements(JSContext *cx, uint32 oldcap, uint32 newcap);
734 bool ensureDenseArrayElements(JSContext *cx, uint32 newcap);
735 bool shrinkDenseArrayElements(JSContext *cx, uint32 newcap);
736 inline void freeDenseArrayElements(JSContext *cx);
738 inline void voidDenseOnlyArraySlots(); // used when converting a dense array to a slow array
740 JSBool makeDenseArraySlow(JSContext *cx);
743 * Arguments-specific getters and setters.
746 private:
748 * Reserved slot structure for Arguments objects:
750 * JSSLOT_PRIVATE - the function's stack frame until the function
751 * returns; also, JS_ARGUMENTS_OBJECT_ON_TRACE if
752 * arguments was created on trace
753 * JSSLOT_ARGS_LENGTH - the number of actual arguments and a flag
754 * indicating whether arguments.length was
755 * overwritten. This slot is not used to represent
756 * arguments.length after that property has been
757 * assigned, even if the new value is integral: it's
758 * always the original length.
759 * JSSLOT_ARGS_DATA - pointer to an ArgumentsData structure containing
760 * the arguments.callee value or JSVAL_HOLE if that
761 * was overwritten, and the values of all arguments
762 * once the function has returned (or as soon as a
763 * strict arguments object has been created).
765 * Argument index i is stored in ArgumentsData.slots[i], accessible via
766 * {get,set}ArgsElement().
768 static const uint32 JSSLOT_ARGS_DATA = JSSLOT_PRIVATE + 2;
770 public:
771 /* Number of extra fixed arguments object slots besides JSSLOT_PRIVATE. */
772 static const uint32 JSSLOT_ARGS_LENGTH = JSSLOT_PRIVATE + 1;
773 static const uint32 ARGS_CLASS_RESERVED_SLOTS = 2;
774 static const uint32 ARGS_FIRST_FREE_SLOT = JSSLOT_PRIVATE + ARGS_CLASS_RESERVED_SLOTS + 1;
776 /* Lower-order bit stolen from the length slot. */
777 static const uint32 ARGS_LENGTH_OVERRIDDEN_BIT = 0x1;
778 static const uint32 ARGS_PACKED_BITS_COUNT = 1;
781 * Set the initial length of the arguments, and mark it as not overridden.
783 inline void setArgsLength(uint32 argc);
786 * Return the initial length of the arguments. This may differ from the
787 * current value of arguments.length!
789 inline uint32 getArgsInitialLength() const;
791 inline void setArgsLengthOverridden();
792 inline bool isArgsLengthOverridden() const;
794 inline js::ArgumentsData *getArgsData() const;
795 inline void setArgsData(js::ArgumentsData *data);
797 inline const js::Value &getArgsCallee() const;
798 inline void setArgsCallee(const js::Value &callee);
800 inline const js::Value &getArgsElement(uint32 i) const;
801 inline js::Value *addressOfArgsElement(uint32 i) const;
802 inline void setArgsElement(uint32 i, const js::Value &v);
804 private:
806 * Reserved slot structure for Arguments objects:
809 static const uint32 JSSLOT_CALL_CALLEE = JSSLOT_PRIVATE + 1;
810 static const uint32 JSSLOT_CALL_ARGUMENTS = JSSLOT_PRIVATE + 2;
812 public:
813 /* Number of extra fixed slots besides JSSLOT_PRIVATE. */
814 static const uint32 CALL_RESERVED_SLOTS = 2;
816 inline JSObject &getCallObjCallee() const;
817 inline JSFunction *getCallObjCalleeFunction() const;
818 inline void setCallObjCallee(JSObject &callee);
820 inline const js::Value &getCallObjArguments() const;
821 inline void setCallObjArguments(const js::Value &v);
824 * Date-specific getters and setters.
827 static const uint32 JSSLOT_DATE_UTC_TIME = JSSLOT_PRIVATE;
830 * Cached slots holding local properties of the date.
831 * These are undefined until the first actual lookup occurs
832 * and are reset to undefined whenever the date's time is modified.
834 static const uint32 JSSLOT_DATE_COMPONENTS_START = JSSLOT_PRIVATE + 1;
836 static const uint32 JSSLOT_DATE_LOCAL_TIME = JSSLOT_PRIVATE + 1;
837 static const uint32 JSSLOT_DATE_LOCAL_YEAR = JSSLOT_PRIVATE + 2;
838 static const uint32 JSSLOT_DATE_LOCAL_MONTH = JSSLOT_PRIVATE + 3;
839 static const uint32 JSSLOT_DATE_LOCAL_DATE = JSSLOT_PRIVATE + 4;
840 static const uint32 JSSLOT_DATE_LOCAL_DAY = JSSLOT_PRIVATE + 5;
841 static const uint32 JSSLOT_DATE_LOCAL_HOURS = JSSLOT_PRIVATE + 6;
842 static const uint32 JSSLOT_DATE_LOCAL_MINUTES = JSSLOT_PRIVATE + 7;
843 static const uint32 JSSLOT_DATE_LOCAL_SECONDS = JSSLOT_PRIVATE + 8;
845 static const uint32 DATE_CLASS_RESERVED_SLOTS = 9;
847 inline const js::Value &getDateUTCTime() const;
848 inline void setDateUTCTime(const js::Value &pthis);
851 * Function-specific getters and setters.
854 private:
855 friend struct JSFunction;
856 friend class js::mjit::Compiler;
859 * Flat closures with one or more upvars snapshot the upvars' values into a
860 * vector of js::Values referenced from this slot.
862 static const uint32 JSSLOT_FLAT_CLOSURE_UPVARS = JSSLOT_PRIVATE + 1;
865 * Null closures set or initialized as methods have these slots. See the
866 * "method barrier" comments and methods.
868 static const uint32 JSSLOT_FUN_METHOD_ATOM = JSSLOT_PRIVATE + 1;
869 static const uint32 JSSLOT_FUN_METHOD_OBJ = JSSLOT_PRIVATE + 2;
871 static const uint32 JSSLOT_BOUND_FUNCTION_THIS = JSSLOT_PRIVATE + 1;
872 static const uint32 JSSLOT_BOUND_FUNCTION_ARGS_COUNT = JSSLOT_PRIVATE + 2;
874 public:
875 static const uint32 FUN_CLASS_RESERVED_SLOTS = 2;
877 inline JSFunction *getFunctionPrivate() const;
879 inline js::Value *getFlatClosureUpvars() const;
880 inline js::Value getFlatClosureUpvar(uint32 i) const;
881 inline void setFlatClosureUpvars(js::Value *upvars);
883 inline bool hasMethodObj(const JSObject& obj) const;
884 inline void setMethodObj(JSObject& obj);
886 inline bool initBoundFunction(JSContext *cx, const js::Value &thisArg,
887 const js::Value *args, uintN argslen);
889 inline JSObject *getBoundFunctionTarget() const;
890 inline const js::Value &getBoundFunctionThis() const;
891 inline const js::Value *getBoundFunctionArguments(uintN &argslen) const;
894 * RegExp-specific getters and setters.
897 private:
898 static const uint32 JSSLOT_REGEXP_LAST_INDEX = JSSLOT_PRIVATE + 1;
900 public:
901 static const uint32 REGEXP_CLASS_RESERVED_SLOTS = 1;
903 inline const js::Value &getRegExpLastIndex() const;
904 inline void setRegExpLastIndex(const js::Value &v);
905 inline void setRegExpLastIndex(jsdouble d);
906 inline void zeroRegExpLastIndex();
909 * Iterator-specific getters and setters.
912 inline NativeIterator *getNativeIterator() const;
913 inline void setNativeIterator(NativeIterator *);
916 * XML-related getters and setters.
920 * Slots for XML-related classes are as follows:
921 * - js_NamespaceClass.base reserves the *_NAME_* and *_NAMESPACE_* slots.
922 * - js_QNameClass.base, js_AttributeNameClass, js_AnyNameClass reserve
923 * the *_NAME_* and *_QNAME_* slots.
924 * - Others (js_XMLClass, js_XMLFilterClass) don't reserve any slots.
926 private:
927 static const uint32 JSSLOT_NAME_PREFIX = JSSLOT_PRIVATE; // shared
928 static const uint32 JSSLOT_NAME_URI = JSSLOT_PRIVATE + 1; // shared
930 static const uint32 JSSLOT_NAMESPACE_DECLARED = JSSLOT_PRIVATE + 2;
932 static const uint32 JSSLOT_QNAME_LOCAL_NAME = JSSLOT_PRIVATE + 2;
934 public:
935 static const uint32 NAMESPACE_CLASS_RESERVED_SLOTS = 3;
936 static const uint32 QNAME_CLASS_RESERVED_SLOTS = 3;
938 inline jsval getNamePrefix() const;
939 inline void setNamePrefix(jsval prefix);
941 inline jsval getNameURI() const;
942 inline void setNameURI(jsval uri);
944 inline jsval getNamespaceDeclared() const;
945 inline void setNamespaceDeclared(jsval decl);
947 inline jsval getQNameLocalName() const;
948 inline void setQNameLocalName(jsval decl);
951 * Proxy-specific getters and setters.
954 inline js::JSProxyHandler *getProxyHandler() const;
955 inline const js::Value &getProxyPrivate() const;
956 inline void setProxyPrivate(const js::Value &priv);
959 * With object-specific getters and setters.
961 inline JSObject *getWithThis() const;
962 inline void setWithThis(JSObject *thisp);
965 * Back to generic stuff.
967 inline bool isCallable();
969 /* The map field is not initialized here and should be set separately. */
970 inline void initCommon(js::Class *aclasp, JSObject *proto, JSObject *parent,
971 JSContext *cx);
972 inline void init(js::Class *aclasp, JSObject *proto, JSObject *parent,
973 JSContext *cx);
974 inline void init(js::Class *aclasp, JSObject *proto, JSObject *parent,
975 void *priv, JSContext *cx);
976 inline void init(js::Class *aclasp, JSObject *proto, JSObject *parent,
977 const js::Value &privateSlotValue, JSContext *cx);
979 inline void finish(JSContext *cx);
980 JS_ALWAYS_INLINE void finalize(JSContext *cx, unsigned thindKind);
983 * Like init, but also initializes map. The catch: proto must be the result
984 * of a call to js_InitClass(...clasp, ...).
986 inline void initSharingEmptyShape(js::Class *clasp,
987 JSObject *proto,
988 JSObject *parent,
989 const js::Value &privateSlotValue,
990 JSContext *cx);
991 inline void initSharingEmptyShape(js::Class *clasp,
992 JSObject *proto,
993 JSObject *parent,
994 void *priv,
995 JSContext *cx);
997 inline bool hasSlotsArray() const { return !!dslots; }
999 /* This method can only be called when hasSlotsArray() returns true. */
1000 inline void freeSlotsArray(JSContext *cx);
1002 inline bool hasProperty(JSContext *cx, jsid id, bool *foundp, uintN flags = 0);
1004 bool allocSlot(JSContext *cx, uint32 *slotp);
1005 void freeSlot(JSContext *cx, uint32 slot);
1007 bool reportReadOnly(JSContext* cx, jsid id, uintN report = JSREPORT_ERROR);
1008 bool reportNotConfigurable(JSContext* cx, jsid id, uintN report = JSREPORT_ERROR);
1009 bool reportNotExtensible(JSContext *cx, uintN report = JSREPORT_ERROR);
1011 private:
1012 js::Shape *getChildProperty(JSContext *cx, js::Shape *parent, js::Shape &child);
1015 * Internal helper that adds a shape not yet mapped by this object.
1017 * Notes:
1018 * 1. getter and setter must be normalized based on flags (see jsscope.cpp).
1019 * 2. !isExtensible() checking must be done by callers.
1021 const js::Shape *addPropertyInternal(JSContext *cx, jsid id,
1022 js::PropertyOp getter, js::PropertyOp setter,
1023 uint32 slot, uintN attrs,
1024 uintN flags, intN shortid,
1025 js::Shape **spp);
1027 bool toDictionaryMode(JSContext *cx);
1029 public:
1030 /* Add a property whose id is not yet in this scope. */
1031 const js::Shape *addProperty(JSContext *cx, jsid id,
1032 js::PropertyOp getter, js::PropertyOp setter,
1033 uint32 slot, uintN attrs,
1034 uintN flags, intN shortid);
1036 /* Add a data property whose id is not yet in this scope. */
1037 const js::Shape *addDataProperty(JSContext *cx, jsid id, uint32 slot, uintN attrs) {
1038 JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
1039 return addProperty(cx, id, NULL, NULL, slot, attrs, 0, 0);
1042 /* Add or overwrite a property for id in this scope. */
1043 const js::Shape *putProperty(JSContext *cx, jsid id,
1044 js::PropertyOp getter, js::PropertyOp setter,
1045 uint32 slot, uintN attrs,
1046 uintN flags, intN shortid);
1048 /* Change the given property into a sibling with the same id in this scope. */
1049 const js::Shape *changeProperty(JSContext *cx, const js::Shape *shape, uintN attrs, uintN mask,
1050 js::PropertyOp getter, js::PropertyOp setter);
1052 /* Remove the property named by id from this object. */
1053 bool removeProperty(JSContext *cx, jsid id);
1055 /* Clear the scope, making it empty. */
1056 void clear(JSContext *cx);
1058 JSBool lookupProperty(JSContext *cx, jsid id, JSObject **objp, JSProperty **propp) {
1059 js::LookupPropOp op = getOps()->lookupProperty;
1060 return (op ? op : js_LookupProperty)(cx, this, id, objp, propp);
1063 JSBool defineProperty(JSContext *cx, jsid id, const js::Value &value,
1064 js::PropertyOp getter = js::PropertyStub,
1065 js::PropertyOp setter = js::PropertyStub,
1066 uintN attrs = JSPROP_ENUMERATE) {
1067 js::DefinePropOp op = getOps()->defineProperty;
1068 return (op ? op : js_DefineProperty)(cx, this, id, &value, getter, setter, attrs);
1071 JSBool getProperty(JSContext *cx, jsid id, js::Value *vp) {
1072 js::PropertyIdOp op = getOps()->getProperty;
1073 return (op ? op : js_GetProperty)(cx, this, id, vp);
1076 JSBool setProperty(JSContext *cx, jsid id, js::Value *vp, JSBool strict) {
1077 js::StrictPropertyIdOp op = getOps()->setProperty;
1078 return (op ? op : js_SetProperty)(cx, this, id, vp, strict);
1081 JSBool getAttributes(JSContext *cx, jsid id, uintN *attrsp) {
1082 js::AttributesOp op = getOps()->getAttributes;
1083 return (op ? op : js_GetAttributes)(cx, this, id, attrsp);
1086 JSBool setAttributes(JSContext *cx, jsid id, uintN *attrsp) {
1087 js::AttributesOp op = getOps()->setAttributes;
1088 return (op ? op : js_SetAttributes)(cx, this, id, attrsp);
1091 JSBool deleteProperty(JSContext *cx, jsid id, js::Value *rval, JSBool strict) {
1092 js::StrictPropertyIdOp op = getOps()->deleteProperty;
1093 return (op ? op : js_DeleteProperty)(cx, this, id, rval, strict);
1096 JSBool enumerate(JSContext *cx, JSIterateOp iterop, js::Value *statep, jsid *idp) {
1097 js::NewEnumerateOp op = getOps()->enumerate;
1098 return (op ? op : js_Enumerate)(cx, this, iterop, statep, idp);
1101 JSType typeOf(JSContext *cx) {
1102 js::TypeOfOp op = getOps()->typeOf;
1103 return (op ? op : js_TypeOf)(cx, this);
1106 JSObject *wrappedObject(JSContext *cx) const;
1108 /* These four are time-optimized to avoid stub calls. */
1109 JSObject *thisObject(JSContext *cx) {
1110 JSObjectOp op = getOps()->thisObject;
1111 return op ? op(cx, this) : this;
1114 static bool thisObject(JSContext *cx, const js::Value &v, js::Value *vp);
1116 inline void dropProperty(JSContext *cx, JSProperty *prop);
1118 JS_FRIEND_API(JSCompartment *) getCompartment(JSContext *cx);
1120 inline JSObject *getThrowTypeError() const;
1122 const js::Shape *defineBlockVariable(JSContext *cx, jsid id, intN index);
1124 void swap(JSObject *obj);
1126 inline bool canHaveMethodBarrier() const;
1128 inline bool isArguments() const;
1129 inline bool isNormalArguments() const;
1130 inline bool isStrictArguments() const;
1131 inline bool isArray() const;
1132 inline bool isDenseArray() const;
1133 inline bool isSlowArray() const;
1134 inline bool isNumber() const;
1135 inline bool isBoolean() const;
1136 inline bool isString() const;
1137 inline bool isPrimitive() const;
1138 inline bool isDate() const;
1139 inline bool isFunction() const;
1140 inline bool isObject() const;
1141 inline bool isWith() const;
1142 inline bool isBlock() const;
1143 inline bool isStaticBlock() const;
1144 inline bool isClonedBlock() const;
1145 inline bool isCall() const;
1146 inline bool isRegExp() const;
1147 inline bool isXML() const;
1148 inline bool isXMLId() const;
1149 inline bool isNamespace() const;
1150 inline bool isQName() const;
1152 inline bool isProxy() const;
1153 inline bool isObjectProxy() const;
1154 inline bool isFunctionProxy() const;
1156 JS_FRIEND_API(bool) isWrapper() const;
1157 JS_FRIEND_API(JSObject *) unwrap(uintN *flagsp = NULL);
1159 inline void initArrayClass();
1162 JS_STATIC_ASSERT(offsetof(JSObject, fslots) % sizeof(js::Value) == 0);
1164 #define JSSLOT_START(clasp) (((clasp)->flags & JSCLASS_HAS_PRIVATE) \
1165 ? JSSLOT_PRIVATE + 1 \
1166 : JSSLOT_PRIVATE)
1168 #define JSSLOT_FREE(clasp) (JSSLOT_START(clasp) \
1169 + JSCLASS_RESERVED_SLOTS(clasp))
1172 * Maximum capacity of the obj->dslots vector, net of the hidden slot at
1173 * obj->dslots[-1] that is used to store the length of the vector biased by
1174 * JS_INITIAL_NSLOTS (and again net of the slot at index -1).
1176 #define MAX_DSLOTS_LENGTH (~size_t(0) / sizeof(js::Value) - 1)
1177 #define MAX_DSLOTS_LENGTH32 (~uint32(0) / sizeof(js::Value) - 1)
1179 #define OBJ_CHECK_SLOT(obj,slot) JS_ASSERT((obj)->containsSlot(slot))
1181 #ifdef JS_THREADSAFE
1184 * The GC runs only when all threads except the one on which the GC is active
1185 * are suspended at GC-safe points, so calling obj->getSlot() from the GC's
1186 * thread is safe when rt->gcRunning is set. See jsgc.cpp for details.
1188 #define THREAD_IS_RUNNING_GC(rt, thread) \
1189 ((rt)->gcRunning && (rt)->gcThread == (thread))
1191 #define CX_THREAD_IS_RUNNING_GC(cx) \
1192 THREAD_IS_RUNNING_GC((cx)->runtime, (cx)->thread)
1194 #endif /* JS_THREADSAFE */
1196 inline void
1197 OBJ_TO_INNER_OBJECT(JSContext *cx, JSObject *&obj)
1199 if (JSObjectOp op = obj->getClass()->ext.innerObject)
1200 obj = op(cx, obj);
1203 inline void
1204 OBJ_TO_OUTER_OBJECT(JSContext *cx, JSObject *&obj)
1206 if (JSObjectOp op = obj->getClass()->ext.outerObject)
1207 obj = op(cx, obj);
1210 class JSValueArray {
1211 public:
1212 jsval *array;
1213 size_t length;
1215 JSValueArray(jsval *v, size_t c) : array(v), length(c) {}
1218 class ValueArray {
1219 public:
1220 js::Value *array;
1221 size_t length;
1223 ValueArray(js::Value *v, size_t c) : array(v), length(c) {}
1226 extern js::Class js_ObjectClass;
1227 extern js::Class js_WithClass;
1228 extern js::Class js_BlockClass;
1230 inline bool JSObject::isObject() const { return getClass() == &js_ObjectClass; }
1231 inline bool JSObject::isWith() const { return getClass() == &js_WithClass; }
1232 inline bool JSObject::isBlock() const { return getClass() == &js_BlockClass; }
1235 * Block scope object macros. The slots reserved by js_BlockClass are:
1237 * JSSLOT_PRIVATE JSStackFrame * active frame pointer or null
1238 * JSSLOT_BLOCK_DEPTH int depth of block slots in frame
1240 * After JSSLOT_BLOCK_DEPTH come one or more slots for the block locals.
1242 * A With object is like a Block object, in that both have one reserved slot
1243 * telling the stack depth of the relevant slots (the slot whose value is the
1244 * object named in the with statement, the slots containing the block's local
1245 * variables); and both have a private slot referring to the JSStackFrame in
1246 * whose activation they were created (or null if the with or block object
1247 * outlives the frame).
1249 static const uint32 JSSLOT_BLOCK_DEPTH = JSSLOT_PRIVATE + 1;
1250 static const uint32 JSSLOT_BLOCK_FIRST_FREE_SLOT = JSSLOT_BLOCK_DEPTH + 1;
1252 inline bool
1253 JSObject::isStaticBlock() const
1255 return isBlock() && !getProto();
1258 inline bool
1259 JSObject::isClonedBlock() const
1261 return isBlock() && !!getProto();
1264 static const uint32 JSSLOT_WITH_THIS = JSSLOT_PRIVATE + 2;
1266 #define OBJ_BLOCK_COUNT(cx,obj) \
1267 (obj)->propertyCount()
1268 #define OBJ_BLOCK_DEPTH(cx,obj) \
1269 (obj)->getSlot(JSSLOT_BLOCK_DEPTH).toInt32()
1270 #define OBJ_SET_BLOCK_DEPTH(cx,obj,depth) \
1271 (obj)->setSlot(JSSLOT_BLOCK_DEPTH, Value(Int32Value(depth)))
1274 * To make sure this slot is well-defined, always call js_NewWithObject to
1275 * create a With object, don't call js_NewObject directly. When creating a
1276 * With object that does not correspond to a stack slot, pass -1 for depth.
1278 * When popping the stack across this object's "with" statement, client code
1279 * must call withobj->setPrivate(NULL).
1281 extern JS_REQUIRES_STACK JSObject *
1282 js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth);
1284 inline JSObject *
1285 js_UnwrapWithObject(JSContext *cx, JSObject *withobj)
1287 JS_ASSERT(withobj->getClass() == &js_WithClass);
1288 return withobj->getProto();
1292 * Create a new block scope object not linked to any proto or parent object.
1293 * Blocks are created by the compiler to reify let blocks and comprehensions.
1294 * Only when dynamic scope is captured do they need to be cloned and spliced
1295 * into an active scope chain.
1297 extern JSObject *
1298 js_NewBlockObject(JSContext *cx);
1300 extern JSObject *
1301 js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp);
1303 extern JS_REQUIRES_STACK JSBool
1304 js_PutBlockObject(JSContext *cx, JSBool normalUnwind);
1306 JSBool
1307 js_XDRBlockObject(JSXDRState *xdr, JSObject **objp);
1309 struct JSSharpObjectMap {
1310 jsrefcount depth;
1311 jsatomid sharpgen;
1312 JSHashTable *table;
1315 #define SHARP_BIT ((jsatomid) 1)
1316 #define BUSY_BIT ((jsatomid) 2)
1317 #define SHARP_ID_SHIFT 2
1318 #define IS_SHARP(he) (uintptr_t((he)->value) & SHARP_BIT)
1319 #define MAKE_SHARP(he) ((he)->value = (void *) (uintptr_t((he)->value)|SHARP_BIT))
1320 #define IS_BUSY(he) (uintptr_t((he)->value) & BUSY_BIT)
1321 #define MAKE_BUSY(he) ((he)->value = (void *) (uintptr_t((he)->value)|BUSY_BIT))
1322 #define CLEAR_BUSY(he) ((he)->value = (void *) (uintptr_t((he)->value)&~BUSY_BIT))
1324 extern JSHashEntry *
1325 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
1326 jschar **sp);
1328 extern void
1329 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap);
1332 * Mark objects stored in map if GC happens between js_EnterSharpObject
1333 * and js_LeaveSharpObject. GC calls this when map->depth > 0.
1335 extern void
1336 js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map);
1338 extern JSBool
1339 js_HasOwnPropertyHelper(JSContext *cx, js::LookupPropOp lookup, uintN argc,
1340 js::Value *vp);
1342 extern JSBool
1343 js_HasOwnProperty(JSContext *cx, js::LookupPropOp lookup, JSObject *obj, jsid id,
1344 JSObject **objp, JSProperty **propp);
1346 extern JSBool
1347 js_NewPropertyDescriptorObject(JSContext *cx, jsid id, uintN attrs,
1348 const js::Value &getter, const js::Value &setter,
1349 const js::Value &value, js::Value *vp);
1351 extern JSBool
1352 js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, js::Value *vp);
1354 #ifdef OLD_GETTER_SETTER_METHODS
1355 JS_FRIEND_API(JSBool) js_obj_defineGetter(JSContext *cx, uintN argc, js::Value *vp);
1356 JS_FRIEND_API(JSBool) js_obj_defineSetter(JSContext *cx, uintN argc, js::Value *vp);
1357 #endif
1359 extern JSObject *
1360 js_InitObjectClass(JSContext *cx, JSObject *obj);
1362 extern JSObject *
1363 js_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
1364 js::Class *clasp, js::Native constructor, uintN nargs,
1365 JSPropertySpec *ps, JSFunctionSpec *fs,
1366 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
1369 * Select Object.prototype method names shared between jsapi.cpp and jsobj.cpp.
1371 extern const char js_watch_str[];
1372 extern const char js_unwatch_str[];
1373 extern const char js_hasOwnProperty_str[];
1374 extern const char js_isPrototypeOf_str[];
1375 extern const char js_propertyIsEnumerable_str[];
1377 #ifdef OLD_GETTER_SETTER_METHODS
1378 extern const char js_defineGetter_str[];
1379 extern const char js_defineSetter_str[];
1380 extern const char js_lookupGetter_str[];
1381 extern const char js_lookupSetter_str[];
1382 #endif
1384 extern JSBool
1385 js_PopulateObject(JSContext *cx, JSObject *newborn, JSObject *props);
1388 * Fast access to immutable standard objects (constructors and prototypes).
1390 extern JSBool
1391 js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
1392 JSObject **objp);
1394 extern JSBool
1395 js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
1396 JSObject *cobj, JSObject *prototype);
1399 * If protoKey is not JSProto_Null, then clasp is ignored. If protoKey is
1400 * JSProto_Null, clasp must non-null.
1402 extern JSBool
1403 js_FindClassObject(JSContext *cx, JSObject *start, JSProtoKey key,
1404 js::Value *vp, js::Class *clasp = NULL);
1406 extern JSObject *
1407 js_ConstructObject(JSContext *cx, js::Class *clasp, JSObject *proto,
1408 JSObject *parent, uintN argc, js::Value *argv);
1410 // Specialized call for constructing |this| with a known function callee,
1411 // and a known prototype.
1412 extern JSObject *
1413 js_CreateThisForFunctionWithProto(JSContext *cx, JSObject *callee, JSObject *proto);
1415 // Specialized call for constructing |this| with a known function callee.
1416 extern JSObject *
1417 js_CreateThisForFunction(JSContext *cx, JSObject *callee);
1419 // Generic call for constructing |this|.
1420 extern JSObject *
1421 js_CreateThis(JSContext *cx, JSObject *callee);
1423 extern jsid
1424 js_CheckForStringIndex(jsid id);
1427 * js_PurgeScopeChain does nothing if obj is not itself a prototype or parent
1428 * scope, else it reshapes the scope and prototype chains it links. It calls
1429 * js_PurgeScopeChainHelper, which asserts that obj is flagged as a delegate
1430 * (i.e., obj has ever been on a prototype or parent chain).
1432 extern void
1433 js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id);
1435 inline void
1436 js_PurgeScopeChain(JSContext *cx, JSObject *obj, jsid id)
1438 if (obj->isDelegate())
1439 js_PurgeScopeChainHelper(cx, obj, id);
1443 * Find or create a property named by id in obj's scope, with the given getter
1444 * and setter, slot, attributes, and other members.
1446 extern const js::Shape *
1447 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
1448 js::PropertyOp getter, js::PropertyOp setter, uint32 slot,
1449 uintN attrs, uintN flags, intN shortid);
1452 * Change shape to have the given attrs, getter, and setter in scope, morphing
1453 * it into a potentially new js::Shape. Return a pointer to the changed
1454 * or identical property.
1456 extern const js::Shape *
1457 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
1458 const js::Shape *shape, uintN attrs, uintN mask,
1459 js::PropertyOp getter, js::PropertyOp setter);
1461 extern JSBool
1462 js_DefineOwnProperty(JSContext *cx, JSObject *obj, jsid id,
1463 const js::Value &descriptor, JSBool *bp);
1466 * Flags for the defineHow parameter of js_DefineNativeProperty.
1468 const uintN JSDNP_CACHE_RESULT = 1; /* an interpreter call from JSOP_INITPROP */
1469 const uintN JSDNP_DONT_PURGE = 2; /* suppress js_PurgeScopeChain */
1470 const uintN JSDNP_SET_METHOD = 4; /* js_{DefineNativeProperty,SetPropertyHelper}
1471 must pass the js::Shape::METHOD
1472 flag on to JSObject::{add,put}Property */
1473 const uintN JSDNP_UNQUALIFIED = 8; /* Unqualified property set. Only used in
1474 the defineHow argument of
1475 js_SetPropertyHelper. */
1478 * On error, return false. On success, if propp is non-null, return true with
1479 * obj locked and with a held property in *propp; if propp is null, return true
1480 * but release obj's lock first. Therefore all callers who pass non-null propp
1481 * result parameters must later call obj->dropProperty(cx, *propp) both to drop
1482 * the held property, and to release the lock on obj.
1484 extern JSBool
1485 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, const js::Value &value,
1486 js::PropertyOp getter, js::PropertyOp setter, uintN attrs,
1487 uintN flags, intN shortid, JSProperty **propp,
1488 uintN defineHow = 0);
1491 * Specialized subroutine that allows caller to preset JSRESOLVE_* flags and
1492 * returns the index along the prototype chain in which *propp was found, or
1493 * the last index if not found, or -1 on error.
1495 extern int
1496 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
1497 JSObject **objp, JSProperty **propp);
1501 * We cache name lookup results only for the global object or for native
1502 * non-global objects without prototype or with prototype that never mutates,
1503 * see bug 462734 and bug 487039.
1505 inline bool
1506 js_IsCacheableNonGlobalScope(JSObject *obj)
1508 extern JS_FRIEND_DATA(js::Class) js_CallClass;
1509 extern JS_FRIEND_DATA(js::Class) js_DeclEnvClass;
1510 JS_ASSERT(obj->getParent());
1512 js::Class *clasp = obj->getClass();
1513 bool cacheable = (clasp == &js_CallClass ||
1514 clasp == &js_BlockClass ||
1515 clasp == &js_DeclEnvClass);
1517 JS_ASSERT_IF(cacheable, !obj->getOps()->lookupProperty);
1518 return cacheable;
1522 * If cacheResult is false, return JS_NO_PROP_CACHE_FILL on success.
1524 extern js::PropertyCacheEntry *
1525 js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
1526 JSObject **objp, JSObject **pobjp, JSProperty **propp);
1529 * Return the index along the scope chain in which id was found, or the last
1530 * index if not found, or -1 on error.
1532 extern JS_FRIEND_API(JSBool)
1533 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
1534 JSProperty **propp);
1536 extern JS_REQUIRES_STACK JSObject *
1537 js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id);
1539 extern JSObject *
1540 js_FindVariableScope(JSContext *cx, JSFunction **funp);
1543 * JSGET_CACHE_RESULT is the analogue of JSDNP_CACHE_RESULT for js_GetMethod.
1545 * JSGET_METHOD_BARRIER (the default, hence 0 but provided for documentation)
1546 * enables a read barrier that preserves standard function object semantics (by
1547 * default we assume our caller won't leak a joined callee to script, where it
1548 * would create hazardous mutable object sharing as well as observable identity
1549 * according to == and ===.
1551 * JSGET_NO_METHOD_BARRIER avoids the performance overhead of the method read
1552 * barrier, which is not needed when invoking a lambda that otherwise does not
1553 * leak its callee reference (via arguments.callee or its name).
1555 const uintN JSGET_CACHE_RESULT = 1; // from a caching interpreter opcode
1556 const uintN JSGET_METHOD_BARRIER = 0; // get can leak joined function object
1557 const uintN JSGET_NO_METHOD_BARRIER = 2; // call to joined function can't leak
1560 * NB: js_NativeGet and js_NativeSet are called with the scope containing shape
1561 * (pobj's scope for Get, obj's for Set) locked, and on successful return, that
1562 * scope is again locked. But on failure, both functions return false with the
1563 * scope containing shape unlocked.
1565 extern JSBool
1566 js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj, const js::Shape *shape, uintN getHow,
1567 js::Value *vp);
1569 extern JSBool
1570 js_NativeSet(JSContext *cx, JSObject *obj, const js::Shape *shape, bool added,
1571 js::Value *vp);
1573 extern JSBool
1574 js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uint32 getHow, js::Value *vp);
1576 extern bool
1577 js_GetPropertyHelperWithShape(JSContext *cx, JSObject *obj, jsid id, uint32 getHow,
1578 js::Value *vp, const js::Shape **shapeOut, JSObject **holderOut);
1580 extern JSBool
1581 js_GetOwnPropertyDescriptor(JSContext *cx, JSObject *obj, jsid id, js::Value *vp);
1583 extern JSBool
1584 js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, js::Value *vp);
1587 * Check whether it is OK to assign an undeclared property with name
1588 * propname of the global object in the current script on cx. Reports
1589 * an error if one needs to be reported (in particular in all cases
1590 * when it returns false).
1592 extern JS_FRIEND_API(bool)
1593 js_CheckUndeclaredVarAssignment(JSContext *cx, JSString *propname);
1595 extern JSBool
1596 js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
1597 js::Value *vp, JSBool strict);
1600 * Change attributes for the given native property. The caller must ensure
1601 * that obj is locked and this function always unlocks obj on return.
1603 extern JSBool
1604 js_SetNativeAttributes(JSContext *cx, JSObject *obj, js::Shape *shape,
1605 uintN attrs);
1607 namespace js {
1609 extern JSBool
1610 DefaultValue(JSContext *cx, JSObject *obj, JSType hint, Value *vp);
1612 extern JSBool
1613 CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1614 js::Value *vp, uintN *attrsp);
1616 } /* namespace js */
1618 extern bool
1619 js_IsDelegate(JSContext *cx, JSObject *obj, const js::Value &v);
1622 * If protoKey is not JSProto_Null, then clasp is ignored. If protoKey is
1623 * JSProto_Null, clasp must non-null.
1625 extern JS_FRIEND_API(JSBool)
1626 js_GetClassPrototype(JSContext *cx, JSObject *scope, JSProtoKey protoKey,
1627 JSObject **protop, js::Class *clasp = NULL);
1629 extern JSBool
1630 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto,
1631 uintN attrs);
1634 * Wrap boolean, number or string as Boolean, Number or String object.
1635 * *vp must not be an object, null or undefined.
1637 extern JSBool
1638 js_PrimitiveToObject(JSContext *cx, js::Value *vp);
1641 * v and vp may alias. On successful return, vp->isObjectOrNull(). If vp is not
1642 * rooted, the caller must root vp before the next possible GC.
1644 extern JSBool
1645 js_ValueToObjectOrNull(JSContext *cx, const js::Value &v, JSObject **objp);
1648 * v and vp may alias. On successful return, vp->isObject(). If vp is not
1649 * rooted, the caller must root vp before the next possible GC.
1651 extern JSObject *
1652 js_ValueToNonNullObject(JSContext *cx, const js::Value &v);
1654 extern JSBool
1655 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, js::Value *rval);
1657 extern JSBool
1658 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
1659 uintN argc, js::Value *argv, js::Value *rval);
1661 extern JSBool
1662 js_XDRObject(JSXDRState *xdr, JSObject **objp);
1664 extern void
1665 js_TraceObject(JSTracer *trc, JSObject *obj);
1667 extern void
1668 js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize);
1670 extern void
1671 js_ClearNative(JSContext *cx, JSObject *obj);
1673 extern bool
1674 js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, js::Value *vp);
1676 extern bool
1677 js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, const js::Value &v);
1679 extern JSBool
1680 js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
1681 JSPrincipals *principals, JSAtom *caller);
1683 /* For CSP -- checks if eval() and friends are allowed to run. */
1684 extern JSBool
1685 js_CheckContentSecurityPolicy(JSContext *cx);
1687 /* Infallible -- returns its argument if there is no wrapped object. */
1688 extern JSObject *
1689 js_GetWrappedObject(JSContext *cx, JSObject *obj);
1691 /* NB: Infallible. */
1692 extern const char *
1693 js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
1694 JSPrincipals *principals, uintN *linenop);
1696 extern JSBool
1697 js_ReportGetterOnlyAssignment(JSContext *cx);
1699 extern JS_FRIEND_API(JSBool)
1700 js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
1702 #ifdef DEBUG
1703 JS_FRIEND_API(void) js_DumpChars(const jschar *s, size_t n);
1704 JS_FRIEND_API(void) js_DumpString(JSString *str);
1705 JS_FRIEND_API(void) js_DumpAtom(JSAtom *atom);
1706 JS_FRIEND_API(void) js_DumpObject(JSObject *obj);
1707 JS_FRIEND_API(void) js_DumpValue(const js::Value &val);
1708 JS_FRIEND_API(void) js_DumpId(jsid id);
1709 JS_FRIEND_API(void) js_DumpStackFrame(JSContext *cx, JSStackFrame *start = NULL);
1710 bool IsSaneThisObject(JSObject &obj);
1711 #endif
1713 extern uintN
1714 js_InferFlags(JSContext *cx, uintN defaultFlags);
1716 /* Object constructor native. Exposed only so the JIT can know its address. */
1717 JSBool
1718 js_Object(JSContext *cx, uintN argc, js::Value *vp);
1721 namespace js {
1723 extern bool
1724 SetProto(JSContext *cx, JSObject *obj, JSObject *proto, bool checkForCycles);
1728 namespace js {
1730 extern JSString *
1731 obj_toStringHelper(JSContext *cx, JSObject *obj);
1734 #endif /* jsobj_h___ */