Bug 505523 - Property cache can skip JSClass::resolve or JSClass::addProperty hooks...
[mozilla-central.git] / js / src / jsobj.h
blobb1ab97e2c67debdb9ed65e4c210b3f68fb05ec42
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 "jshash.h" /* Added by JSIFY */
52 #include "jsprvtd.h"
53 #include "jspubtd.h"
55 JS_BEGIN_EXTERN_C
57 /* For detailed comments on these function pointer types, see jsprvtd.h. */
58 struct JSObjectOps {
60 * Custom shared object map for non-native objects. For native objects
61 * this should be null indicating, that JSObject.map is an instance of
62 * JSScope.
64 const JSObjectMap *objectMap;
66 /* Mandatory non-null function pointer members. */
67 JSLookupPropOp lookupProperty;
68 JSDefinePropOp defineProperty;
69 JSPropertyIdOp getProperty;
70 JSPropertyIdOp setProperty;
71 JSAttributesOp getAttributes;
72 JSAttributesOp setAttributes;
73 JSPropertyIdOp deleteProperty;
74 JSConvertOp defaultValue;
75 JSNewEnumerateOp enumerate;
76 JSCheckAccessIdOp checkAccess;
78 /* Optionally non-null members start here. */
79 JSObjectOp thisObject;
80 JSPropertyRefOp dropProperty;
81 JSNative call;
82 JSNative construct;
83 JSHasInstanceOp hasInstance;
84 JSTraceOp trace;
85 JSFinalizeOp clear;
88 struct JSObjectMap {
89 const JSObjectOps * const ops; /* high level object operation vtable */
90 uint32 shape; /* shape identifier */
92 explicit JSObjectMap(const JSObjectOps *ops, uint32 shape) : ops(ops), shape(shape) {}
94 enum { SHAPELESS = 0xffffffff };
97 const uint32 JS_INITIAL_NSLOTS = 5;
99 const uint32 JSSLOT_PROTO = 0;
100 const uint32 JSSLOT_PARENT = 1;
103 * The first available slot to store generic value. For JSCLASS_HAS_PRIVATE
104 * classes the slot stores a pointer to private data reinterpreted as jsval.
105 * Such pointer is stored as is without an overhead of PRIVATE_TO_JSVAL
106 * tagging and should be accessed using the (get|set)Private methods of
107 * JSObject.
109 const uint32 JSSLOT_PRIVATE = 2;
111 const uint32 JSSLOT_PRIMITIVE_THIS = JSSLOT_PRIVATE;
113 const uintptr_t JSSLOT_CLASS_MASK_BITS = 3;
116 * JSObject struct, with members sized to fit in 32 bytes on 32-bit targets,
117 * 64 bytes on 64-bit systems. The JSFunction struct is an extension of this
118 * struct allocated from a larger GC size-class.
120 * The classword member stores the JSClass pointer for this object, with the
121 * least two bits encoding whether this object is a "delegate" or a "system"
122 * object. We do *not* synchronize updates of classword -- API clients must
123 * take care.
125 * An object is a delegate if it is on another object's prototype (linked by
126 * JSSLOT_PROTO) or scope (JSSLOT_PARENT) chain, and therefore the delegate
127 * might be asked implicitly to get or set a property on behalf of another
128 * object. Delegates may be accessed directly too, as may any object, but only
129 * those objects linked after the head of any prototype or scope chain are
130 * flagged as delegates. This definition helps to optimize shape-based property
131 * cache invalidation (see Purge{Scope,Proto}Chain in jsobj.cpp).
133 * The meaning of the system object bit is defined by the API client. It is
134 * set in JS_NewSystemObject and is queried by JS_IsSystemObject (jsdbgapi.h),
135 * but it has no intrinsic meaning to SpiderMonkey. Further, JSFILENAME_SYSTEM
136 * and JS_FlagScriptFilenamePrefix (also exported via jsdbgapi.h) are intended
137 * to be complementary to this bit, but it is up to the API client to implement
138 * any such association.
140 * Both these classword tag bits are initially zero; they may be set or queried
141 * using the (is|set)(Delegate|System) inline methods.
143 * The dslots member is null or a pointer into a dynamically allocated vector
144 * of jsvals for reserved and dynamic slots. If dslots is not null, dslots[-1]
145 * records the number of available slots.
147 struct JSObject {
148 JSObjectMap *map; /* property map, see jsscope.h */
149 jsuword classword; /* JSClass ptr | bits, see above */
150 jsval fslots[JS_INITIAL_NSLOTS]; /* small number of fixed slots */
151 jsval *dslots; /* dynamically allocated slots */
153 JSClass *getClass() const {
154 return (JSClass *) (classword & ~JSSLOT_CLASS_MASK_BITS);
157 bool isDelegate() const {
158 return (classword & jsuword(1)) != jsuword(0);
161 void setDelegate() {
162 classword |= jsuword(1);
165 static void setDelegateNullSafe(JSObject *obj) {
166 if (obj)
167 obj->setDelegate();
170 bool isSystem() const {
171 return (classword & jsuword(2)) != jsuword(0);
174 void setSystem() {
175 classword |= jsuword(2);
178 JSObject *getProto() const {
179 return JSVAL_TO_OBJECT(fslots[JSSLOT_PROTO]);
182 void clearProto() {
183 fslots[JSSLOT_PROTO] = JSVAL_NULL;
186 void setProto(JSObject *newProto) {
187 setDelegateNullSafe(newProto);
188 fslots[JSSLOT_PROTO] = OBJECT_TO_JSVAL(newProto);
191 JSObject *getParent() const {
192 return JSVAL_TO_OBJECT(fslots[JSSLOT_PARENT]);
195 void clearParent() {
196 fslots[JSSLOT_PARENT] = JSVAL_NULL;
199 void setParent(JSObject *newParent) {
200 setDelegateNullSafe(newParent);
201 fslots[JSSLOT_PARENT] = OBJECT_TO_JSVAL(newParent);
204 void traceProtoAndParent(JSTracer *trc) const {
205 JSObject *proto = getProto();
206 if (proto)
207 JS_CALL_OBJECT_TRACER(trc, proto, "__proto__");
209 JSObject *parent = getParent();
210 if (parent)
211 JS_CALL_OBJECT_TRACER(trc, parent, "__parent__");
214 void *getPrivate() const {
215 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
216 jsval v = fslots[JSSLOT_PRIVATE];
217 JS_ASSERT((v & jsval(1)) == jsval(0));
218 return reinterpret_cast<void *>(v);
221 void setPrivate(void *data) {
222 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
223 jsval v = reinterpret_cast<jsval>(data);
224 JS_ASSERT((v & jsval(1)) == jsval(0));
225 fslots[JSSLOT_PRIVATE] = v;
228 static jsval defaultPrivate(JSClass *clasp) {
229 return (clasp->flags & JSCLASS_HAS_PRIVATE)
230 ? JSVAL_NULL
231 : JSVAL_VOID;
234 /* The map field is not initialized here and should be set separately. */
235 void init(JSClass *clasp, JSObject *proto, JSObject *parent,
236 jsval privateSlotValue) {
237 JS_ASSERT(((jsuword) clasp & 3) == 0);
238 JS_STATIC_ASSERT(JSSLOT_PRIVATE + 3 == JS_INITIAL_NSLOTS);
239 JS_ASSERT_IF(clasp->flags & JSCLASS_HAS_PRIVATE,
240 (privateSlotValue & jsval(1)) == jsval(0));
242 classword = jsuword(clasp);
243 JS_ASSERT(!isDelegate());
244 JS_ASSERT(!isSystem());
246 setProto(proto);
247 setParent(parent);
248 fslots[JSSLOT_PRIVATE] = privateSlotValue;
249 fslots[JSSLOT_PRIVATE + 1] = JSVAL_VOID;
250 fslots[JSSLOT_PRIVATE + 2] = JSVAL_VOID;
251 dslots = NULL;
255 * Like init, but also initializes map. The catch: proto must be the result
256 * of a call to js_InitClass(...clasp, ...).
258 inline void initSharingEmptyScope(JSClass *clasp, JSObject *proto, JSObject *parent,
259 jsval privateSlotValue);
261 JSBool lookupProperty(JSContext *cx, jsid id,
262 JSObject **objp, JSProperty **propp) {
263 return map->ops->lookupProperty(cx, this, id, objp, propp);
266 JSBool defineProperty(JSContext *cx, jsid id, jsval value,
267 JSPropertyOp getter = JS_PropertyStub,
268 JSPropertyOp setter = JS_PropertyStub,
269 uintN attrs = JSPROP_ENUMERATE) {
270 return map->ops->defineProperty(cx, this, id, value, getter, setter, attrs);
273 JSBool getProperty(JSContext *cx, jsid id, jsval *vp) {
274 return map->ops->getProperty(cx, this, id, vp);
277 JSBool setProperty(JSContext *cx, jsid id, jsval *vp) {
278 return map->ops->setProperty(cx, this, id, vp);
281 JSBool getAttributes(JSContext *cx, jsid id, JSProperty *prop,
282 uintN *attrsp) {
283 return map->ops->getAttributes(cx, this, id, prop, attrsp);
286 JSBool setAttributes(JSContext *cx, jsid id, JSProperty *prop,
287 uintN *attrsp) {
288 return map->ops->setAttributes(cx, this, id, prop, attrsp);
291 JSBool deleteProperty(JSContext *cx, jsid id, jsval *rval) {
292 return map->ops->deleteProperty(cx, this, id, rval);
295 JSBool defaultValue(JSContext *cx, JSType hint, jsval *vp) {
296 return map->ops->defaultValue(cx, this, hint, vp);
299 JSBool enumerate(JSContext *cx, JSIterateOp op, jsval *statep,
300 jsid *idp) {
301 return map->ops->enumerate(cx, this, op, statep, idp);
304 JSBool checkAccess(JSContext *cx, jsid id, JSAccessMode mode, jsval *vp,
305 uintN *attrsp) {
306 return map->ops->checkAccess(cx, this, id, mode, vp, attrsp);
309 /* These four are time-optimized to avoid stub calls. */
310 JSObject *thisObject(JSContext *cx) {
311 return map->ops->thisObject ? map->ops->thisObject(cx, this) : this;
314 void dropProperty(JSContext *cx, JSProperty *prop) {
315 if (map->ops->dropProperty)
316 map->ops->dropProperty(cx, this, prop);
320 /* Compatibility macros. */
321 #define STOBJ_GET_PROTO(obj) ((obj)->getProto())
322 #define STOBJ_SET_PROTO(obj,proto) ((obj)->setProto(proto))
323 #define STOBJ_CLEAR_PROTO(obj) ((obj)->clearProto())
325 #define STOBJ_GET_PARENT(obj) ((obj)->getParent())
326 #define STOBJ_SET_PARENT(obj,parent) ((obj)->setParent(parent))
327 #define STOBJ_CLEAR_PARENT(obj) ((obj)->clearParent())
329 #define OBJ_GET_PROTO(cx,obj) STOBJ_GET_PROTO(obj)
330 #define OBJ_SET_PROTO(cx,obj,proto) STOBJ_SET_PROTO(obj, proto)
331 #define OBJ_CLEAR_PROTO(cx,obj) STOBJ_CLEAR_PROTO(obj)
333 #define OBJ_GET_PARENT(cx,obj) STOBJ_GET_PARENT(obj)
334 #define OBJ_SET_PARENT(cx,obj,parent) STOBJ_SET_PARENT(obj, parent)
335 #define OBJ_CLEAR_PARENT(cx,obj) STOBJ_CLEAR_PARENT(obj)
337 #define JSSLOT_START(clasp) (((clasp)->flags & JSCLASS_HAS_PRIVATE) \
338 ? JSSLOT_PRIVATE + 1 \
339 : JSSLOT_PRIVATE)
341 #define JSSLOT_FREE(clasp) (JSSLOT_START(clasp) \
342 + JSCLASS_RESERVED_SLOTS(clasp))
345 * Maximum capacity of the obj->dslots vector, net of the hidden slot at
346 * obj->dslots[-1] that is used to store the length of the vector biased by
347 * JS_INITIAL_NSLOTS (and again net of the slot at index -1).
349 #define MAX_DSLOTS_LENGTH (JS_MAX(~uint32(0), ~size_t(0)) / sizeof(jsval) - 1)
350 #define MAX_DSLOTS_LENGTH32 (~uint32(0) / sizeof(jsval) - 1)
353 * STOBJ prefix means Single Threaded Object. Use the following fast macros to
354 * directly manipulate slots in obj when only one thread can access obj, or
355 * when accessing read-only slots within JS_INITIAL_NSLOTS.
358 #define STOBJ_NSLOTS(obj) \
359 ((obj)->dslots ? (uint32)(obj)->dslots[-1] : (uint32)JS_INITIAL_NSLOTS)
361 inline jsval&
362 STOBJ_GET_SLOT(JSObject *obj, uintN slot)
364 return (slot < JS_INITIAL_NSLOTS)
365 ? obj->fslots[slot]
366 : (JS_ASSERT(slot < (uint32)obj->dslots[-1]),
367 obj->dslots[slot - JS_INITIAL_NSLOTS]);
370 inline void
371 STOBJ_SET_SLOT(JSObject *obj, uintN slot, jsval value)
373 if (slot < JS_INITIAL_NSLOTS) {
374 obj->fslots[slot] = value;
375 } else {
376 JS_ASSERT(slot < (uint32)obj->dslots[-1]);
377 obj->dslots[slot - JS_INITIAL_NSLOTS] = value;
381 inline JSClass*
382 STOBJ_GET_CLASS(const JSObject* obj)
384 return obj->getClass();
387 #define OBJ_CHECK_SLOT(obj,slot) \
388 (JS_ASSERT(OBJ_IS_NATIVE(obj)), JS_ASSERT(slot < OBJ_SCOPE(obj)->freeslot))
390 #define LOCKED_OBJ_GET_SLOT(obj,slot) \
391 (OBJ_CHECK_SLOT(obj, slot), STOBJ_GET_SLOT(obj, slot))
392 #define LOCKED_OBJ_SET_SLOT(obj,slot,value) \
393 (OBJ_CHECK_SLOT(obj, slot), STOBJ_SET_SLOT(obj, slot, value))
395 #ifdef JS_THREADSAFE
397 /* Thread-safe functions and wrapper macros for accessing slots in obj. */
398 #define OBJ_GET_SLOT(cx,obj,slot) \
399 (OBJ_CHECK_SLOT(obj, slot), \
400 (OBJ_SCOPE(obj)->title.ownercx == cx) \
401 ? LOCKED_OBJ_GET_SLOT(obj, slot) \
402 : js_GetSlotThreadSafe(cx, obj, slot))
404 #define OBJ_SET_SLOT(cx,obj,slot,value) \
405 JS_BEGIN_MACRO \
406 OBJ_CHECK_SLOT(obj, slot); \
407 if (OBJ_SCOPE(obj)->title.ownercx == cx) \
408 LOCKED_OBJ_SET_SLOT(obj, slot, value); \
409 else \
410 js_SetSlotThreadSafe(cx, obj, slot, value); \
411 JS_END_MACRO
414 * If thread-safe, define an OBJ_GET_SLOT wrapper that bypasses, for a native
415 * object, the lock-free "fast path" test of (OBJ_SCOPE(obj)->ownercx == cx),
416 * to avoid needlessly switching from lock-free to lock-full scope when doing
417 * GC on a different context from the last one to own the scope. The caller
418 * in this case is probably a JSClass.mark function, e.g., fun_mark, or maybe
419 * a finalizer.
421 * The GC runs only when all threads except the one on which the GC is active
422 * are suspended at GC-safe points, so calling STOBJ_GET_SLOT from the GC's
423 * thread is safe when rt->gcRunning is set. See jsgc.c for details.
425 #define THREAD_IS_RUNNING_GC(rt, thread) \
426 ((rt)->gcRunning && (rt)->gcThread == (thread))
428 #define CX_THREAD_IS_RUNNING_GC(cx) \
429 THREAD_IS_RUNNING_GC((cx)->runtime, (cx)->thread)
431 #else /* !JS_THREADSAFE */
433 #define OBJ_GET_SLOT(cx,obj,slot) LOCKED_OBJ_GET_SLOT(obj,slot)
434 #define OBJ_SET_SLOT(cx,obj,slot,value) LOCKED_OBJ_SET_SLOT(obj,slot,value)
436 #endif /* !JS_THREADSAFE */
439 * Class is invariant and comes from the fixed clasp member. Thus no locking
440 * is necessary to read it. Same for the private slot.
442 #define OBJ_GET_CLASS(cx,obj) STOBJ_GET_CLASS(obj)
445 * Test whether the object is native. FIXME bug 492938: consider how it would
446 * affect the performance to do just the !ops->objectMap check.
448 #define OPS_IS_NATIVE(ops) \
449 JS_LIKELY((ops) == &js_ObjectOps || !(ops)->objectMap)
451 #define OBJ_IS_NATIVE(obj) OPS_IS_NATIVE((obj)->map->ops)
453 #ifdef __cplusplus
454 inline void
455 OBJ_TO_INNER_OBJECT(JSContext *cx, JSObject *&obj)
457 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
458 if (clasp->flags & JSCLASS_IS_EXTENDED) {
459 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
460 if (xclasp->innerObject)
461 obj = xclasp->innerObject(cx, obj);
466 * The following function has been copied to jsd/jsd_val.c. If making changes to
467 * OBJ_TO_OUTER_OBJECT, please update jsd/jsd_val.c as well.
469 inline void
470 OBJ_TO_OUTER_OBJECT(JSContext *cx, JSObject *&obj)
472 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
473 if (clasp->flags & JSCLASS_IS_EXTENDED) {
474 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
475 if (xclasp->outerObject)
476 obj = xclasp->outerObject(cx, obj);
479 #endif
481 extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps;
482 extern JS_FRIEND_DATA(JSObjectOps) js_WithObjectOps;
483 extern JSClass js_ObjectClass;
484 extern JSClass js_WithClass;
485 extern JSClass js_BlockClass;
488 * Block scope object macros. The slots reserved by js_BlockClass are:
490 * JSSLOT_PRIVATE JSStackFrame * active frame pointer or null
491 * JSSLOT_BLOCK_DEPTH int depth of block slots in frame
493 * After JSSLOT_BLOCK_DEPTH come one or more slots for the block locals.
495 * A With object is like a Block object, in that both have one reserved slot
496 * telling the stack depth of the relevant slots (the slot whose value is the
497 * object named in the with statement, the slots containing the block's local
498 * variables); and both have a private slot referring to the JSStackFrame in
499 * whose activation they were created (or null if the with or block object
500 * outlives the frame).
502 #define JSSLOT_BLOCK_DEPTH (JSSLOT_PRIVATE + 1)
504 static inline bool
505 OBJ_IS_CLONED_BLOCK(JSObject *obj)
507 return obj->getProto() != NULL;
510 extern JSBool
511 js_DefineBlockVariable(JSContext *cx, JSObject *obj, jsid id, intN index);
513 #define OBJ_BLOCK_COUNT(cx,obj) \
514 (OBJ_SCOPE(obj)->entryCount)
515 #define OBJ_BLOCK_DEPTH(cx,obj) \
516 JSVAL_TO_INT(STOBJ_GET_SLOT(obj, JSSLOT_BLOCK_DEPTH))
517 #define OBJ_SET_BLOCK_DEPTH(cx,obj,depth) \
518 STOBJ_SET_SLOT(obj, JSSLOT_BLOCK_DEPTH, INT_TO_JSVAL(depth))
521 * To make sure this slot is well-defined, always call js_NewWithObject to
522 * create a With object, don't call js_NewObject directly. When creating a
523 * With object that does not correspond to a stack slot, pass -1 for depth.
525 * When popping the stack across this object's "with" statement, client code
526 * must call withobj->setPrivate(NULL).
528 extern JS_REQUIRES_STACK JSObject *
529 js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth);
532 * Create a new block scope object not linked to any proto or parent object.
533 * Blocks are created by the compiler to reify let blocks and comprehensions.
534 * Only when dynamic scope is captured do they need to be cloned and spliced
535 * into an active scope chain.
537 extern JSObject *
538 js_NewBlockObject(JSContext *cx);
540 extern JSObject *
541 js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp);
543 extern JS_REQUIRES_STACK JSBool
544 js_PutBlockObject(JSContext *cx, JSBool normalUnwind);
546 JSBool
547 js_XDRBlockObject(JSXDRState *xdr, JSObject **objp);
549 struct JSSharpObjectMap {
550 jsrefcount depth;
551 jsatomid sharpgen;
552 JSHashTable *table;
555 #define SHARP_BIT ((jsatomid) 1)
556 #define BUSY_BIT ((jsatomid) 2)
557 #define SHARP_ID_SHIFT 2
558 #define IS_SHARP(he) (JS_PTR_TO_UINT32((he)->value) & SHARP_BIT)
559 #define MAKE_SHARP(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|SHARP_BIT))
560 #define IS_BUSY(he) (JS_PTR_TO_UINT32((he)->value) & BUSY_BIT)
561 #define MAKE_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|BUSY_BIT))
562 #define CLEAR_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)&~BUSY_BIT))
564 extern JSHashEntry *
565 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
566 jschar **sp);
568 extern void
569 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap);
572 * Mark objects stored in map if GC happens between js_EnterSharpObject
573 * and js_LeaveSharpObject. GC calls this when map->depth > 0.
575 extern void
576 js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map);
578 extern JSBool
579 js_HasOwnPropertyHelper(JSContext *cx, JSLookupPropOp lookup, uintN argc,
580 jsval *vp);
582 extern JSBool
583 js_HasOwnProperty(JSContext *cx, JSLookupPropOp lookup, JSObject *obj, jsid id,
584 JSBool *foundp);
586 extern JSBool
587 js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
589 extern JSObject *
590 js_InitEval(JSContext *cx, JSObject *obj);
592 extern JSObject *
593 js_InitObjectClass(JSContext *cx, JSObject *obj);
595 extern JSObject *
596 js_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
597 JSClass *clasp, JSNative constructor, uintN nargs,
598 JSPropertySpec *ps, JSFunctionSpec *fs,
599 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
602 * Select Object.prototype method names shared between jsapi.cpp and jsobj.cpp.
604 extern const char js_watch_str[];
605 extern const char js_unwatch_str[];
606 extern const char js_hasOwnProperty_str[];
607 extern const char js_isPrototypeOf_str[];
608 extern const char js_propertyIsEnumerable_str[];
609 extern const char js_defineGetter_str[];
610 extern const char js_defineSetter_str[];
611 extern const char js_lookupGetter_str[];
612 extern const char js_lookupSetter_str[];
614 extern JSBool
615 js_GetClassId(JSContext *cx, JSClass *clasp, jsid *idp);
617 extern JSObject *
618 js_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto,
619 JSObject *parent, size_t objectSize = 0);
622 * See jsapi.h, JS_NewObjectWithGivenProto.
624 extern JSObject *
625 js_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
626 JSObject *parent, size_t objectSize = 0);
629 * Allocate a new native object with the given value of the proto and private
630 * slots. The parent slot is set to the value of proto's parent slot.
632 * clasp must be a native class. proto must be the result of a call to
633 * js_InitClass(...clasp, ...).
635 * Note that this is the correct global object for native class instances, but
636 * not for user-defined functions called as constructors. Functions used as
637 * constructors must create instances parented by the parent of the function
638 * object, not by the parent of its .prototype object value.
640 extern JSObject*
641 js_NewObjectWithClassProto(JSContext *cx, JSClass *clasp, JSObject *proto,
642 jsval privateSlotValue);
645 * Fast access to immutable standard objects (constructors and prototypes).
647 extern JSBool
648 js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
649 JSObject **objp);
651 extern JSBool
652 js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key, JSObject *cobj);
654 extern JSBool
655 js_FindClassObject(JSContext *cx, JSObject *start, jsid id, jsval *vp);
657 extern JSObject *
658 js_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
659 JSObject *parent, uintN argc, jsval *argv);
661 extern JSBool
662 js_AllocSlot(JSContext *cx, JSObject *obj, uint32 *slotp);
664 extern void
665 js_FreeSlot(JSContext *cx, JSObject *obj, uint32 slot);
667 extern bool
668 js_GrowSlots(JSContext *cx, JSObject *obj, size_t nslots);
670 extern void
671 js_ShrinkSlots(JSContext *cx, JSObject *obj, size_t nslots);
673 static inline void
674 js_FreeSlots(JSContext *cx, JSObject *obj)
676 if (obj->dslots)
677 js_ShrinkSlots(cx, obj, 0);
681 * Ensure that the object has at least JSCLASS_RESERVED_SLOTS(clasp)+nreserved
682 * slots. The function can be called only for native objects just created with
683 * js_NewObject or its forms. In particular, the object should not be shared
684 * between threads and its dslots array must be null. nreserved must match the
685 * value that JSClass.reserveSlots (if any) would return after the object is
686 * fully initialized.
688 bool
689 js_EnsureReservedSlots(JSContext *cx, JSObject *obj, size_t nreserved);
691 extern jsid
692 js_CheckForStringIndex(jsid id);
695 * js_PurgeScopeChain does nothing if obj is not itself a prototype or parent
696 * scope, else it reshapes the scope and prototype chains it links. It calls
697 * js_PurgeScopeChainHelper, which asserts that obj is flagged as a delegate
698 * (i.e., obj has ever been on a prototype or parent chain).
700 extern void
701 js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id);
703 #ifdef __cplusplus /* Aargh, libgjs, bug 492720. */
704 static JS_INLINE void
705 js_PurgeScopeChain(JSContext *cx, JSObject *obj, jsid id)
707 if (obj->isDelegate())
708 js_PurgeScopeChainHelper(cx, obj, id);
710 #endif
713 * Find or create a property named by id in obj's scope, with the given getter
714 * and setter, slot, attributes, and other members.
716 extern JSScopeProperty *
717 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
718 JSPropertyOp getter, JSPropertyOp setter, uint32 slot,
719 uintN attrs, uintN flags, intN shortid);
722 * Change sprop to have the given attrs, getter, and setter in scope, morphing
723 * it into a potentially new JSScopeProperty. Return a pointer to the changed
724 * or identical property.
726 extern JSScopeProperty *
727 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
728 JSScopeProperty *sprop, uintN attrs, uintN mask,
729 JSPropertyOp getter, JSPropertyOp setter);
731 extern JSBool
732 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
733 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
736 * Flags for the defineHow parameter of js_DefineNativeProperty.
738 const uintN JSDNP_CACHE_RESULT = 1; /* an interpreter call from JSOP_INITPROP */
739 const uintN JSDNP_DONT_PURGE = 2; /* suppress js_PurgeScopeChain */
740 const uintN JSDNP_SET_METHOD = 4; /* js_{DefineNativeProperty,SetPropertyHelper}
741 must pass the SPROP_IS_METHOD flag on to
742 js_AddScopeProperty */
745 * On error, return false. On success, if propp is non-null, return true with
746 * obj locked and with a held property in *propp; if propp is null, return true
747 * but release obj's lock first. Therefore all callers who pass non-null propp
748 * result parameters must later call obj->dropProperty(cx, *propp) both to drop
749 * the held property, and to release the lock on obj.
751 extern JSBool
752 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
753 JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
754 uintN flags, intN shortid, JSProperty **propp,
755 uintN defineHow = 0);
758 * Unlike js_DefineNativeProperty, propp must be non-null. On success, and if
759 * id was found, return true with *objp non-null and locked, and with a held
760 * property stored in *propp. If successful but id was not found, return true
761 * with both *objp and *propp null. Therefore all callers who receive a
762 * non-null *propp must later call (*objp)->dropProperty(cx, *propp).
764 extern JS_FRIEND_API(JSBool)
765 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
766 JSProperty **propp);
769 * Specialized subroutine that allows caller to preset JSRESOLVE_* flags and
770 * returns the index along the prototype chain in which *propp was found, or
771 * the last index if not found, or -1 on error.
773 extern int
774 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
775 JSObject **objp, JSProperty **propp);
779 * We cache name lookup results only for the global object or for native
780 * non-global objects without prototype or with prototype that never mutates,
781 * see bug 462734 and bug 487039.
783 static inline bool
784 js_IsCacheableNonGlobalScope(JSObject *obj)
786 extern JS_FRIEND_DATA(JSClass) js_CallClass;
787 extern JS_FRIEND_DATA(JSClass) js_DeclEnvClass;
788 JS_ASSERT(STOBJ_GET_PARENT(obj));
790 JSClass *clasp = STOBJ_GET_CLASS(obj);
791 bool cacheable = (clasp == &js_CallClass ||
792 clasp == &js_BlockClass ||
793 clasp == &js_DeclEnvClass);
795 JS_ASSERT_IF(cacheable, obj->map->ops->lookupProperty == js_LookupProperty);
796 return cacheable;
800 * If cacheResult is false, return JS_NO_PROP_CACHE_FILL on success.
802 extern JSPropCacheEntry *
803 js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
804 JSObject **objp, JSObject **pobjp, JSProperty **propp);
807 * Return the index along the scope chain in which id was found, or the last
808 * index if not found, or -1 on error.
810 extern JS_FRIEND_API(JSBool)
811 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
812 JSProperty **propp);
814 extern JS_REQUIRES_STACK JSObject *
815 js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id);
817 extern JSObject *
818 js_FindVariableScope(JSContext *cx, JSFunction **funp);
821 * JSGET_CACHE_RESULT is the analogue of JSDNP_CACHE_RESULT for js_GetMethod.
823 * JSGET_METHOD_BARRIER (the default, hence 0 but provided for documentation)
824 * enables a read barrier that preserves standard function object semantics (by
825 * default we assume our caller won't leak a joined callee to script, where it
826 * would create hazardous mutable object sharing as well as observable identity
827 * according to == and ===.
829 * JSGET_NO_METHOD_BARRIER avoids the performance overhead of the method read
830 * barrier, which is not needed when invoking a lambda that otherwise does not
831 * leak its callee reference (via arguments.callee or its name).
833 const uintN JSGET_CACHE_RESULT = 1; // from a caching interpreter opcode
834 const uintN JSGET_METHOD_BARRIER = 0; // get can leak joined function object
835 const uintN JSGET_NO_METHOD_BARRIER = 2; // call to joined function can't leak
838 * NB: js_NativeGet and js_NativeSet are called with the scope containing sprop
839 * (pobj's scope for Get, obj's for Set) locked, and on successful return, that
840 * scope is again locked. But on failure, both functions return false with the
841 * scope containing sprop unlocked.
843 extern JSBool
844 js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj,
845 JSScopeProperty *sprop, uintN getHow, jsval *vp);
847 extern JSBool
848 js_NativeSet(JSContext *cx, JSObject *obj, JSScopeProperty *sprop, bool added,
849 jsval *vp);
851 extern JSBool
852 js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN getHow,
853 jsval *vp);
855 extern JSBool
856 js_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
858 extern JSBool
859 js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, jsval *vp);
862 * Check whether it is OK to assign an undeclared property of the global
863 * object at the current script PC.
865 extern JS_FRIEND_API(JSBool)
866 js_CheckUndeclaredVarAssignment(JSContext *cx);
868 extern JSBool
869 js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
870 jsval *vp);
872 extern JSBool
873 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
875 extern JSBool
876 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
877 uintN *attrsp);
879 extern JSBool
880 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
881 uintN *attrsp);
883 extern JSBool
884 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *rval);
886 extern JSBool
887 js_DefaultValue(JSContext *cx, JSObject *obj, JSType hint, jsval *vp);
889 extern JSBool
890 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
891 jsval *statep, jsid *idp);
893 extern void
894 js_MarkEnumeratorState(JSTracer *trc, JSObject *obj, jsval state);
896 extern void
897 js_PurgeCachedNativeEnumerators(JSContext *cx, JSThreadData *data);
899 extern JSBool
900 js_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
901 jsval *vp, uintN *attrsp);
903 extern JSBool
904 js_Call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
906 extern JSBool
907 js_Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
908 jsval *rval);
910 extern JSBool
911 js_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
913 extern JSBool
914 js_SetProtoOrParent(JSContext *cx, JSObject *obj, uint32 slot, JSObject *pobj,
915 JSBool checkForCycles);
917 extern JSBool
918 js_IsDelegate(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
920 extern JSBool
921 js_GetClassPrototype(JSContext *cx, JSObject *scope, jsid id,
922 JSObject **protop);
924 extern JSBool
925 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto,
926 uintN attrs);
929 * Wrap boolean, number or string as Boolean, Number or String object.
930 * *vp must not be an object, null or undefined.
932 extern JSBool
933 js_PrimitiveToObject(JSContext *cx, jsval *vp);
935 extern JSBool
936 js_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
938 extern JSObject *
939 js_ValueToNonNullObject(JSContext *cx, jsval v);
941 extern JSBool
942 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, jsval *rval);
944 extern JSBool
945 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
946 uintN argc, jsval *argv, jsval *rval);
948 extern JSBool
949 js_XDRObject(JSXDRState *xdr, JSObject **objp);
951 extern void
952 js_TraceObject(JSTracer *trc, JSObject *obj);
954 extern void
955 js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize);
957 extern void
958 js_Clear(JSContext *cx, JSObject *obj);
960 extern bool
961 js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
963 bool
964 js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
967 * Precondition: obj must be locked.
969 extern JSBool
970 js_ReallocSlots(JSContext *cx, JSObject *obj, uint32 nslots,
971 JSBool exactAllocation);
973 extern JSObject *
974 js_CheckScopeChainValidity(JSContext *cx, JSObject *scopeobj, const char *caller);
976 extern JSBool
977 js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
978 JSPrincipals *principals, JSAtom *caller);
980 /* Infallible -- returns its argument if there is no wrapped object. */
981 extern JSObject *
982 js_GetWrappedObject(JSContext *cx, JSObject *obj);
984 /* NB: Infallible. */
985 extern const char *
986 js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
987 JSPrincipals *principals, uintN *linenop);
989 /* Infallible, therefore cx is last parameter instead of first. */
990 extern JSBool
991 js_IsCallable(JSObject *obj, JSContext *cx);
993 void
994 js_ReportGetterOnlyAssignment(JSContext *cx);
996 extern JS_FRIEND_API(JSBool)
997 js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
999 #ifdef DEBUG
1000 JS_FRIEND_API(void) js_DumpChars(const jschar *s, size_t n);
1001 JS_FRIEND_API(void) js_DumpString(JSString *str);
1002 JS_FRIEND_API(void) js_DumpAtom(JSAtom *atom);
1003 JS_FRIEND_API(void) js_DumpValue(jsval val);
1004 JS_FRIEND_API(void) js_DumpId(jsid id);
1005 JS_FRIEND_API(void) js_DumpObject(JSObject *obj);
1006 JS_FRIEND_API(void) js_DumpStackFrame(JSStackFrame *fp);
1007 #endif
1009 extern uintN
1010 js_InferFlags(JSContext *cx, uintN defaultFlags);
1012 /* Object constructor native. Exposed only so the JIT can know its address. */
1013 JSBool
1014 js_Object(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
1016 JS_END_EXTERN_C
1018 #endif /* jsobj_h___ */