Bug 537204 - No more BOGUS_CX bogosities (r=jwalden).
[mozilla-central.git] / js / src / jsobj.h
blob2b124e737fba7463d239048437058eab6cb03f6a
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 "jspubtd.h"
53 #include "jsprvtd.h"
56 * A representation of ECMA-262 ed. 5's internal property descriptor data
57 * structure.
59 struct PropertyDescriptor {
60 PropertyDescriptor();
62 /* 8.10.5 ToPropertyDescriptor(Obj) */
63 bool initialize(JSContext* cx, jsid id, jsval v);
65 /* 8.10.1 IsAccessorDescriptor(desc) */
66 bool isAccessorDescriptor() const {
67 return hasGet || hasSet;
70 /* 8.10.2 IsDataDescriptor(desc) */
71 bool isDataDescriptor() const {
72 return hasValue || hasWritable;
75 /* 8.10.3 IsGenericDescriptor(desc) */
76 bool isGenericDescriptor() const {
77 return !isAccessorDescriptor() && !isDataDescriptor();
80 bool configurable() const {
81 return (attrs & JSPROP_PERMANENT) == 0;
84 bool enumerable() const {
85 return (attrs & JSPROP_ENUMERATE) != 0;
88 bool writable() const {
89 return (attrs & JSPROP_READONLY) == 0;
92 JSObject* getterObject() const {
93 return get != JSVAL_VOID ? JSVAL_TO_OBJECT(get) : NULL;
95 JSObject* setterObject() const {
96 return set != JSVAL_VOID ? JSVAL_TO_OBJECT(set) : NULL;
99 jsval getterValue() const {
100 return get;
102 jsval setterValue() const {
103 return set;
106 JSPropertyOp getter() const {
107 return js_CastAsPropertyOp(getterObject());
109 JSPropertyOp setter() const {
110 return js_CastAsPropertyOp(setterObject());
113 static void traceDescriptorArray(JSTracer* trc, JSObject* obj);
114 static void finalizeDescriptorArray(JSContext* cx, JSObject* obj);
116 jsid id;
117 jsval value, get, set;
119 /* Property descriptor boolean fields. */
120 uint8 attrs;
122 /* Bits indicating which values are set. */
123 bool hasGet : 1;
124 bool hasSet : 1;
125 bool hasValue : 1;
126 bool hasWritable : 1;
127 bool hasEnumerable : 1;
128 bool hasConfigurable : 1;
131 JS_BEGIN_EXTERN_C
133 /* For detailed comments on these function pointer types, see jsprvtd.h. */
134 struct JSObjectOps {
136 * Custom shared object map for non-native objects. For native objects
137 * this should be null indicating, that JSObject.map is an instance of
138 * JSScope.
140 const JSObjectMap *objectMap;
142 /* Mandatory non-null function pointer members. */
143 JSLookupPropOp lookupProperty;
144 JSDefinePropOp defineProperty;
145 JSPropertyIdOp getProperty;
146 JSPropertyIdOp setProperty;
147 JSAttributesOp getAttributes;
148 JSAttributesOp setAttributes;
149 JSPropertyIdOp deleteProperty;
150 JSConvertOp defaultValue;
151 JSNewEnumerateOp enumerate;
152 JSCheckAccessIdOp checkAccess;
154 /* Optionally non-null members start here. */
155 JSObjectOp thisObject;
156 JSPropertyRefOp dropProperty;
157 JSNative call;
158 JSNative construct;
159 JSHasInstanceOp hasInstance;
160 JSTraceOp trace;
161 JSFinalizeOp clear;
164 struct JSObjectMap {
165 const JSObjectOps * const ops; /* high level object operation vtable */
166 uint32 shape; /* shape identifier */
168 explicit JSObjectMap(const JSObjectOps *ops, uint32 shape) : ops(ops), shape(shape) {}
170 enum { SHAPELESS = 0xffffffff };
173 const uint32 JS_INITIAL_NSLOTS = 5;
175 const uint32 JSSLOT_PROTO = 0;
176 const uint32 JSSLOT_PARENT = 1;
179 * The first available slot to store generic value. For JSCLASS_HAS_PRIVATE
180 * classes the slot stores a pointer to private data reinterpreted as jsval.
181 * Such pointer is stored as is without an overhead of PRIVATE_TO_JSVAL
182 * tagging and should be accessed using the (get|set)Private methods of
183 * JSObject.
185 const uint32 JSSLOT_PRIVATE = 2;
187 const uint32 JSSLOT_PRIMITIVE_THIS = JSSLOT_PRIVATE;
189 const uintptr_t JSSLOT_CLASS_MASK_BITS = 3;
192 * JSObject struct, with members sized to fit in 32 bytes on 32-bit targets,
193 * 64 bytes on 64-bit systems. The JSFunction struct is an extension of this
194 * struct allocated from a larger GC size-class.
196 * The classword member stores the JSClass pointer for this object, with the
197 * least two bits encoding whether this object is a "delegate" or a "system"
198 * object. We do *not* synchronize updates of classword -- API clients must
199 * take care.
201 * An object is a delegate if it is on another object's prototype (linked by
202 * JSSLOT_PROTO) or scope (JSSLOT_PARENT) chain, and therefore the delegate
203 * might be asked implicitly to get or set a property on behalf of another
204 * object. Delegates may be accessed directly too, as may any object, but only
205 * those objects linked after the head of any prototype or scope chain are
206 * flagged as delegates. This definition helps to optimize shape-based property
207 * cache invalidation (see Purge{Scope,Proto}Chain in jsobj.cpp).
209 * The meaning of the system object bit is defined by the API client. It is
210 * set in JS_NewSystemObject and is queried by JS_IsSystemObject (jsdbgapi.h),
211 * but it has no intrinsic meaning to SpiderMonkey. Further, JSFILENAME_SYSTEM
212 * and JS_FlagScriptFilenamePrefix (also exported via jsdbgapi.h) are intended
213 * to be complementary to this bit, but it is up to the API client to implement
214 * any such association.
216 * Both these classword tag bits are initially zero; they may be set or queried
217 * using the (is|set)(Delegate|System) inline methods.
219 * The dslots member is null or a pointer into a dynamically allocated vector
220 * of jsvals for reserved and dynamic slots. If dslots is not null, dslots[-1]
221 * records the number of available slots.
223 struct JSObject {
224 JSObjectMap *map; /* property map, see jsscope.h */
225 jsuword classword; /* JSClass ptr | bits, see above */
226 jsval fslots[JS_INITIAL_NSLOTS]; /* small number of fixed slots */
227 jsval *dslots; /* dynamically allocated slots */
229 JSClass *getClass() const {
230 return (JSClass *) (classword & ~JSSLOT_CLASS_MASK_BITS);
233 bool isDelegate() const {
234 return (classword & jsuword(1)) != jsuword(0);
237 void setDelegate() {
238 classword |= jsuword(1);
241 static void setDelegateNullSafe(JSObject *obj) {
242 if (obj)
243 obj->setDelegate();
246 bool isSystem() const {
247 return (classword & jsuword(2)) != jsuword(0);
250 void setSystem() {
251 classword |= jsuword(2);
254 JSObject *getProto() const {
255 return JSVAL_TO_OBJECT(fslots[JSSLOT_PROTO]);
258 void clearProto() {
259 fslots[JSSLOT_PROTO] = JSVAL_NULL;
262 void setProto(JSObject *newProto) {
263 setDelegateNullSafe(newProto);
264 fslots[JSSLOT_PROTO] = OBJECT_TO_JSVAL(newProto);
267 JSObject *getParent() const {
268 return JSVAL_TO_OBJECT(fslots[JSSLOT_PARENT]);
271 void clearParent() {
272 fslots[JSSLOT_PARENT] = JSVAL_NULL;
275 void setParent(JSObject *newParent) {
276 setDelegateNullSafe(newParent);
277 fslots[JSSLOT_PARENT] = OBJECT_TO_JSVAL(newParent);
280 void traceProtoAndParent(JSTracer *trc) const {
281 JSObject *proto = getProto();
282 if (proto)
283 JS_CALL_OBJECT_TRACER(trc, proto, "__proto__");
285 JSObject *parent = getParent();
286 if (parent)
287 JS_CALL_OBJECT_TRACER(trc, parent, "__parent__");
290 void *getPrivate() const {
291 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
292 jsval v = fslots[JSSLOT_PRIVATE];
293 JS_ASSERT((v & jsval(1)) == jsval(0));
294 return reinterpret_cast<void *>(v);
297 void setPrivate(void *data) {
298 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
299 jsval v = reinterpret_cast<jsval>(data);
300 JS_ASSERT((v & jsval(1)) == jsval(0));
301 fslots[JSSLOT_PRIVATE] = v;
304 static jsval defaultPrivate(JSClass *clasp) {
305 return (clasp->flags & JSCLASS_HAS_PRIVATE)
306 ? JSVAL_NULL
307 : JSVAL_VOID;
310 /* The map field is not initialized here and should be set separately. */
311 void init(JSClass *clasp, JSObject *proto, JSObject *parent,
312 jsval privateSlotValue) {
313 JS_ASSERT(((jsuword) clasp & 3) == 0);
314 JS_STATIC_ASSERT(JSSLOT_PRIVATE + 3 == JS_INITIAL_NSLOTS);
315 JS_ASSERT_IF(clasp->flags & JSCLASS_HAS_PRIVATE,
316 (privateSlotValue & jsval(1)) == jsval(0));
318 classword = jsuword(clasp);
319 JS_ASSERT(!isDelegate());
320 JS_ASSERT(!isSystem());
322 setProto(proto);
323 setParent(parent);
324 fslots[JSSLOT_PRIVATE] = privateSlotValue;
325 fslots[JSSLOT_PRIVATE + 1] = JSVAL_VOID;
326 fslots[JSSLOT_PRIVATE + 2] = JSVAL_VOID;
327 dslots = NULL;
331 * Like init, but also initializes map. The catch: proto must be the result
332 * of a call to js_InitClass(...clasp, ...).
334 inline void initSharingEmptyScope(JSClass *clasp, JSObject *proto, JSObject *parent,
335 jsval privateSlotValue);
337 JSBool lookupProperty(JSContext *cx, jsid id,
338 JSObject **objp, JSProperty **propp) {
339 return map->ops->lookupProperty(cx, this, id, objp, propp);
342 JSBool defineProperty(JSContext *cx, jsid id, jsval value,
343 JSPropertyOp getter = JS_PropertyStub,
344 JSPropertyOp setter = JS_PropertyStub,
345 uintN attrs = JSPROP_ENUMERATE) {
346 return map->ops->defineProperty(cx, this, id, value, getter, setter, attrs);
349 JSBool getProperty(JSContext *cx, jsid id, jsval *vp) {
350 return map->ops->getProperty(cx, this, id, vp);
353 JSBool setProperty(JSContext *cx, jsid id, jsval *vp) {
354 return map->ops->setProperty(cx, this, id, vp);
357 JSBool getAttributes(JSContext *cx, jsid id, JSProperty *prop,
358 uintN *attrsp) {
359 return map->ops->getAttributes(cx, this, id, prop, attrsp);
362 JSBool setAttributes(JSContext *cx, jsid id, JSProperty *prop,
363 uintN *attrsp) {
364 return map->ops->setAttributes(cx, this, id, prop, attrsp);
367 JSBool deleteProperty(JSContext *cx, jsid id, jsval *rval) {
368 return map->ops->deleteProperty(cx, this, id, rval);
371 JSBool defaultValue(JSContext *cx, JSType hint, jsval *vp) {
372 return map->ops->defaultValue(cx, this, hint, vp);
375 JSBool enumerate(JSContext *cx, JSIterateOp op, jsval *statep,
376 jsid *idp) {
377 return map->ops->enumerate(cx, this, op, statep, idp);
380 JSBool checkAccess(JSContext *cx, jsid id, JSAccessMode mode, jsval *vp,
381 uintN *attrsp) {
382 return map->ops->checkAccess(cx, this, id, mode, vp, attrsp);
385 /* These four are time-optimized to avoid stub calls. */
386 JSObject *thisObject(JSContext *cx) {
387 return map->ops->thisObject ? map->ops->thisObject(cx, this) : this;
390 void dropProperty(JSContext *cx, JSProperty *prop) {
391 if (map->ops->dropProperty)
392 map->ops->dropProperty(cx, this, prop);
395 inline bool isArray() const;
396 inline bool isDenseArray() const;
397 inline bool isFunction() const;
398 inline bool isRegExp() const;
399 inline bool isXML() const;
402 /* Compatibility macros. */
403 #define STOBJ_GET_PROTO(obj) ((obj)->getProto())
404 #define STOBJ_SET_PROTO(obj,proto) ((obj)->setProto(proto))
405 #define STOBJ_CLEAR_PROTO(obj) ((obj)->clearProto())
407 #define STOBJ_GET_PARENT(obj) ((obj)->getParent())
408 #define STOBJ_SET_PARENT(obj,parent) ((obj)->setParent(parent))
409 #define STOBJ_CLEAR_PARENT(obj) ((obj)->clearParent())
411 #define OBJ_GET_PROTO(cx,obj) STOBJ_GET_PROTO(obj)
412 #define OBJ_SET_PROTO(cx,obj,proto) STOBJ_SET_PROTO(obj, proto)
413 #define OBJ_CLEAR_PROTO(cx,obj) STOBJ_CLEAR_PROTO(obj)
415 #define OBJ_GET_PARENT(cx,obj) STOBJ_GET_PARENT(obj)
416 #define OBJ_SET_PARENT(cx,obj,parent) STOBJ_SET_PARENT(obj, parent)
417 #define OBJ_CLEAR_PARENT(cx,obj) STOBJ_CLEAR_PARENT(obj)
419 #define JSSLOT_START(clasp) (((clasp)->flags & JSCLASS_HAS_PRIVATE) \
420 ? JSSLOT_PRIVATE + 1 \
421 : JSSLOT_PRIVATE)
423 #define JSSLOT_FREE(clasp) (JSSLOT_START(clasp) \
424 + JSCLASS_RESERVED_SLOTS(clasp))
427 * Maximum capacity of the obj->dslots vector, net of the hidden slot at
428 * obj->dslots[-1] that is used to store the length of the vector biased by
429 * JS_INITIAL_NSLOTS (and again net of the slot at index -1).
431 #define MAX_DSLOTS_LENGTH (JS_MAX(~uint32(0), ~size_t(0)) / sizeof(jsval) - 1)
432 #define MAX_DSLOTS_LENGTH32 (~uint32(0) / sizeof(jsval) - 1)
435 * STOBJ prefix means Single Threaded Object. Use the following fast macros to
436 * directly manipulate slots in obj when only one thread can access obj, or
437 * when accessing read-only slots within JS_INITIAL_NSLOTS.
440 #define STOBJ_NSLOTS(obj) \
441 ((obj)->dslots ? (uint32)(obj)->dslots[-1] : (uint32)JS_INITIAL_NSLOTS)
443 inline jsval&
444 STOBJ_GET_SLOT(JSObject *obj, uintN slot)
446 return (slot < JS_INITIAL_NSLOTS)
447 ? obj->fslots[slot]
448 : (JS_ASSERT(slot < (uint32)obj->dslots[-1]),
449 obj->dslots[slot - JS_INITIAL_NSLOTS]);
452 inline void
453 STOBJ_SET_SLOT(JSObject *obj, uintN slot, jsval value)
455 if (slot < JS_INITIAL_NSLOTS) {
456 obj->fslots[slot] = value;
457 } else {
458 JS_ASSERT(slot < (uint32)obj->dslots[-1]);
459 obj->dslots[slot - JS_INITIAL_NSLOTS] = value;
463 inline JSClass*
464 STOBJ_GET_CLASS(const JSObject* obj)
466 return obj->getClass();
469 #define OBJ_CHECK_SLOT(obj,slot) \
470 (JS_ASSERT(OBJ_IS_NATIVE(obj)), JS_ASSERT(slot < OBJ_SCOPE(obj)->freeslot))
472 #define LOCKED_OBJ_GET_SLOT(obj,slot) \
473 (OBJ_CHECK_SLOT(obj, slot), STOBJ_GET_SLOT(obj, slot))
474 #define LOCKED_OBJ_SET_SLOT(obj,slot,value) \
475 (OBJ_CHECK_SLOT(obj, slot), STOBJ_SET_SLOT(obj, slot, value))
477 #ifdef JS_THREADSAFE
479 /* Thread-safe functions and wrapper macros for accessing slots in obj. */
480 #define OBJ_GET_SLOT(cx,obj,slot) \
481 (OBJ_CHECK_SLOT(obj, slot), \
482 (OBJ_SCOPE(obj)->title.ownercx == cx) \
483 ? LOCKED_OBJ_GET_SLOT(obj, slot) \
484 : js_GetSlotThreadSafe(cx, obj, slot))
486 #define OBJ_SET_SLOT(cx,obj,slot,value) \
487 JS_BEGIN_MACRO \
488 OBJ_CHECK_SLOT(obj, slot); \
489 if (OBJ_SCOPE(obj)->title.ownercx == cx) \
490 LOCKED_OBJ_SET_SLOT(obj, slot, value); \
491 else \
492 js_SetSlotThreadSafe(cx, obj, slot, value); \
493 JS_END_MACRO
496 * If thread-safe, define an OBJ_GET_SLOT wrapper that bypasses, for a native
497 * object, the lock-free "fast path" test of (OBJ_SCOPE(obj)->ownercx == cx),
498 * to avoid needlessly switching from lock-free to lock-full scope when doing
499 * GC on a different context from the last one to own the scope. The caller
500 * in this case is probably a JSClass.mark function, e.g., fun_mark, or maybe
501 * a finalizer.
503 * The GC runs only when all threads except the one on which the GC is active
504 * are suspended at GC-safe points, so calling STOBJ_GET_SLOT from the GC's
505 * thread is safe when rt->gcRunning is set. See jsgc.c for details.
507 #define THREAD_IS_RUNNING_GC(rt, thread) \
508 ((rt)->gcRunning && (rt)->gcThread == (thread))
510 #define CX_THREAD_IS_RUNNING_GC(cx) \
511 THREAD_IS_RUNNING_GC((cx)->runtime, (cx)->thread)
513 #else /* !JS_THREADSAFE */
515 #define OBJ_GET_SLOT(cx,obj,slot) LOCKED_OBJ_GET_SLOT(obj,slot)
516 #define OBJ_SET_SLOT(cx,obj,slot,value) LOCKED_OBJ_SET_SLOT(obj,slot,value)
518 #endif /* !JS_THREADSAFE */
521 * Class is invariant and comes from the fixed clasp member. Thus no locking
522 * is necessary to read it. Same for the private slot.
524 #define OBJ_GET_CLASS(cx,obj) STOBJ_GET_CLASS(obj)
527 * Test whether the object is native. FIXME bug 492938: consider how it would
528 * affect the performance to do just the !ops->objectMap check.
530 #define OPS_IS_NATIVE(ops) \
531 JS_LIKELY((ops) == &js_ObjectOps || !(ops)->objectMap)
533 #define OBJ_IS_NATIVE(obj) OPS_IS_NATIVE((obj)->map->ops)
535 #ifdef __cplusplus
536 inline void
537 OBJ_TO_INNER_OBJECT(JSContext *cx, JSObject *&obj)
539 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
540 if (clasp->flags & JSCLASS_IS_EXTENDED) {
541 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
542 if (xclasp->innerObject)
543 obj = xclasp->innerObject(cx, obj);
548 * The following function has been copied to jsd/jsd_val.c. If making changes to
549 * OBJ_TO_OUTER_OBJECT, please update jsd/jsd_val.c as well.
551 inline void
552 OBJ_TO_OUTER_OBJECT(JSContext *cx, JSObject *&obj)
554 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
555 if (clasp->flags & JSCLASS_IS_EXTENDED) {
556 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
557 if (xclasp->outerObject)
558 obj = xclasp->outerObject(cx, obj);
561 #endif
563 extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps;
564 extern JS_FRIEND_DATA(JSObjectOps) js_WithObjectOps;
565 extern JSClass js_ObjectClass;
566 extern JSClass js_WithClass;
567 extern JSClass js_BlockClass;
570 * Block scope object macros. The slots reserved by js_BlockClass are:
572 * JSSLOT_PRIVATE JSStackFrame * active frame pointer or null
573 * JSSLOT_BLOCK_DEPTH int depth of block slots in frame
575 * After JSSLOT_BLOCK_DEPTH come one or more slots for the block locals.
577 * A With object is like a Block object, in that both have one reserved slot
578 * telling the stack depth of the relevant slots (the slot whose value is the
579 * object named in the with statement, the slots containing the block's local
580 * variables); and both have a private slot referring to the JSStackFrame in
581 * whose activation they were created (or null if the with or block object
582 * outlives the frame).
584 #define JSSLOT_BLOCK_DEPTH (JSSLOT_PRIVATE + 1)
586 static inline bool
587 OBJ_IS_CLONED_BLOCK(JSObject *obj)
589 return obj->getProto() != NULL;
592 extern JSBool
593 js_DefineBlockVariable(JSContext *cx, JSObject *obj, jsid id, intN index);
595 #define OBJ_BLOCK_COUNT(cx,obj) \
596 (OBJ_SCOPE(obj)->entryCount)
597 #define OBJ_BLOCK_DEPTH(cx,obj) \
598 JSVAL_TO_INT(STOBJ_GET_SLOT(obj, JSSLOT_BLOCK_DEPTH))
599 #define OBJ_SET_BLOCK_DEPTH(cx,obj,depth) \
600 STOBJ_SET_SLOT(obj, JSSLOT_BLOCK_DEPTH, INT_TO_JSVAL(depth))
603 * To make sure this slot is well-defined, always call js_NewWithObject to
604 * create a With object, don't call js_NewObject directly. When creating a
605 * With object that does not correspond to a stack slot, pass -1 for depth.
607 * When popping the stack across this object's "with" statement, client code
608 * must call withobj->setPrivate(NULL).
610 extern JS_REQUIRES_STACK JSObject *
611 js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth);
614 * Create a new block scope object not linked to any proto or parent object.
615 * Blocks are created by the compiler to reify let blocks and comprehensions.
616 * Only when dynamic scope is captured do they need to be cloned and spliced
617 * into an active scope chain.
619 extern JSObject *
620 js_NewBlockObject(JSContext *cx);
622 extern JSObject *
623 js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp);
625 extern JS_REQUIRES_STACK JSBool
626 js_PutBlockObject(JSContext *cx, JSBool normalUnwind);
628 JSBool
629 js_XDRBlockObject(JSXDRState *xdr, JSObject **objp);
631 struct JSSharpObjectMap {
632 jsrefcount depth;
633 jsatomid sharpgen;
634 JSHashTable *table;
637 #define SHARP_BIT ((jsatomid) 1)
638 #define BUSY_BIT ((jsatomid) 2)
639 #define SHARP_ID_SHIFT 2
640 #define IS_SHARP(he) (JS_PTR_TO_UINT32((he)->value) & SHARP_BIT)
641 #define MAKE_SHARP(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|SHARP_BIT))
642 #define IS_BUSY(he) (JS_PTR_TO_UINT32((he)->value) & BUSY_BIT)
643 #define MAKE_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|BUSY_BIT))
644 #define CLEAR_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)&~BUSY_BIT))
646 extern JSHashEntry *
647 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
648 jschar **sp);
650 extern void
651 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap);
654 * Mark objects stored in map if GC happens between js_EnterSharpObject
655 * and js_LeaveSharpObject. GC calls this when map->depth > 0.
657 extern void
658 js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map);
660 extern JSBool
661 js_HasOwnPropertyHelper(JSContext *cx, JSLookupPropOp lookup, uintN argc,
662 jsval *vp);
664 extern JSBool
665 js_HasOwnProperty(JSContext *cx, JSLookupPropOp lookup, JSObject *obj, jsid id,
666 JSObject **objp, JSProperty **propp);
668 extern JSBool
669 js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
671 extern JSObject *
672 js_InitEval(JSContext *cx, JSObject *obj);
674 extern JSObject *
675 js_InitObjectClass(JSContext *cx, JSObject *obj);
677 extern JSObject *
678 js_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
679 JSClass *clasp, JSNative constructor, uintN nargs,
680 JSPropertySpec *ps, JSFunctionSpec *fs,
681 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
684 * Select Object.prototype method names shared between jsapi.cpp and jsobj.cpp.
686 extern const char js_watch_str[];
687 extern const char js_unwatch_str[];
688 extern const char js_hasOwnProperty_str[];
689 extern const char js_isPrototypeOf_str[];
690 extern const char js_propertyIsEnumerable_str[];
691 extern const char js_defineGetter_str[];
692 extern const char js_defineSetter_str[];
693 extern const char js_lookupGetter_str[];
694 extern const char js_lookupSetter_str[];
696 extern JSBool
697 js_GetClassId(JSContext *cx, JSClass *clasp, jsid *idp);
699 extern JSObject *
700 js_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto,
701 JSObject *parent, size_t objectSize = 0);
704 * See jsapi.h, JS_NewObjectWithGivenProto.
706 extern JSObject *
707 js_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
708 JSObject *parent, size_t objectSize = 0);
711 * Allocate a new native object with the given value of the proto and private
712 * slots. The parent slot is set to the value of proto's parent slot.
714 * clasp must be a native class. proto must be the result of a call to
715 * js_InitClass(...clasp, ...).
717 * Note that this is the correct global object for native class instances, but
718 * not for user-defined functions called as constructors. Functions used as
719 * constructors must create instances parented by the parent of the function
720 * object, not by the parent of its .prototype object value.
722 extern JSObject*
723 js_NewObjectWithClassProto(JSContext *cx, JSClass *clasp, JSObject *proto,
724 jsval privateSlotValue);
727 * Fast access to immutable standard objects (constructors and prototypes).
729 extern JSBool
730 js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
731 JSObject **objp);
733 extern JSBool
734 js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key, JSObject *cobj);
736 extern JSBool
737 js_FindClassObject(JSContext *cx, JSObject *start, jsid id, jsval *vp);
739 extern JSObject *
740 js_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
741 JSObject *parent, uintN argc, jsval *argv);
743 extern JSBool
744 js_AllocSlot(JSContext *cx, JSObject *obj, uint32 *slotp);
746 extern void
747 js_FreeSlot(JSContext *cx, JSObject *obj, uint32 slot);
749 extern bool
750 js_GrowSlots(JSContext *cx, JSObject *obj, size_t nslots);
752 extern void
753 js_ShrinkSlots(JSContext *cx, JSObject *obj, size_t nslots);
755 static inline void
756 js_FreeSlots(JSContext *cx, JSObject *obj)
758 if (obj->dslots)
759 js_ShrinkSlots(cx, obj, 0);
763 * Ensure that the object has at least JSCLASS_RESERVED_SLOTS(clasp)+nreserved
764 * slots. The function can be called only for native objects just created with
765 * js_NewObject or its forms. In particular, the object should not be shared
766 * between threads and its dslots array must be null. nreserved must match the
767 * value that JSClass.reserveSlots (if any) would return after the object is
768 * fully initialized.
770 bool
771 js_EnsureReservedSlots(JSContext *cx, JSObject *obj, size_t nreserved);
773 extern jsid
774 js_CheckForStringIndex(jsid id);
777 * js_PurgeScopeChain does nothing if obj is not itself a prototype or parent
778 * scope, else it reshapes the scope and prototype chains it links. It calls
779 * js_PurgeScopeChainHelper, which asserts that obj is flagged as a delegate
780 * (i.e., obj has ever been on a prototype or parent chain).
782 extern void
783 js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id);
785 #ifdef __cplusplus /* Aargh, libgjs, bug 492720. */
786 static JS_INLINE void
787 js_PurgeScopeChain(JSContext *cx, JSObject *obj, jsid id)
789 if (obj->isDelegate())
790 js_PurgeScopeChainHelper(cx, obj, id);
792 #endif
795 * Find or create a property named by id in obj's scope, with the given getter
796 * and setter, slot, attributes, and other members.
798 extern JSScopeProperty *
799 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
800 JSPropertyOp getter, JSPropertyOp setter, uint32 slot,
801 uintN attrs, uintN flags, intN shortid);
804 * Change sprop to have the given attrs, getter, and setter in scope, morphing
805 * it into a potentially new JSScopeProperty. Return a pointer to the changed
806 * or identical property.
808 extern JSScopeProperty *
809 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
810 JSScopeProperty *sprop, uintN attrs, uintN mask,
811 JSPropertyOp getter, JSPropertyOp setter);
813 extern JSBool
814 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
815 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
818 * Flags for the defineHow parameter of js_DefineNativeProperty.
820 const uintN JSDNP_CACHE_RESULT = 1; /* an interpreter call from JSOP_INITPROP */
821 const uintN JSDNP_DONT_PURGE = 2; /* suppress js_PurgeScopeChain */
822 const uintN JSDNP_SET_METHOD = 4; /* js_{DefineNativeProperty,SetPropertyHelper}
823 must pass the SPROP_IS_METHOD flag on to
824 js_AddScopeProperty */
827 * On error, return false. On success, if propp is non-null, return true with
828 * obj locked and with a held property in *propp; if propp is null, return true
829 * but release obj's lock first. Therefore all callers who pass non-null propp
830 * result parameters must later call obj->dropProperty(cx, *propp) both to drop
831 * the held property, and to release the lock on obj.
833 extern JSBool
834 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
835 JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
836 uintN flags, intN shortid, JSProperty **propp,
837 uintN defineHow = 0);
840 * Unlike js_DefineNativeProperty, propp must be non-null. On success, and if
841 * id was found, return true with *objp non-null and locked, and with a held
842 * property stored in *propp. If successful but id was not found, return true
843 * with both *objp and *propp null. Therefore all callers who receive a
844 * non-null *propp must later call (*objp)->dropProperty(cx, *propp).
846 extern JS_FRIEND_API(JSBool)
847 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
848 JSProperty **propp);
851 * Specialized subroutine that allows caller to preset JSRESOLVE_* flags and
852 * returns the index along the prototype chain in which *propp was found, or
853 * the last index if not found, or -1 on error.
855 extern int
856 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
857 JSObject **objp, JSProperty **propp);
861 * We cache name lookup results only for the global object or for native
862 * non-global objects without prototype or with prototype that never mutates,
863 * see bug 462734 and bug 487039.
865 static inline bool
866 js_IsCacheableNonGlobalScope(JSObject *obj)
868 extern JS_FRIEND_DATA(JSClass) js_CallClass;
869 extern JS_FRIEND_DATA(JSClass) js_DeclEnvClass;
870 JS_ASSERT(STOBJ_GET_PARENT(obj));
872 JSClass *clasp = STOBJ_GET_CLASS(obj);
873 bool cacheable = (clasp == &js_CallClass ||
874 clasp == &js_BlockClass ||
875 clasp == &js_DeclEnvClass);
877 JS_ASSERT_IF(cacheable, obj->map->ops->lookupProperty == js_LookupProperty);
878 return cacheable;
882 * If cacheResult is false, return JS_NO_PROP_CACHE_FILL on success.
884 extern JSPropCacheEntry *
885 js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
886 JSObject **objp, JSObject **pobjp, JSProperty **propp);
889 * Return the index along the scope chain in which id was found, or the last
890 * index if not found, or -1 on error.
892 extern JS_FRIEND_API(JSBool)
893 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
894 JSProperty **propp);
896 extern JS_REQUIRES_STACK JSObject *
897 js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id);
899 extern JSObject *
900 js_FindVariableScope(JSContext *cx, JSFunction **funp);
903 * JSGET_CACHE_RESULT is the analogue of JSDNP_CACHE_RESULT for js_GetMethod.
905 * JSGET_METHOD_BARRIER (the default, hence 0 but provided for documentation)
906 * enables a read barrier that preserves standard function object semantics (by
907 * default we assume our caller won't leak a joined callee to script, where it
908 * would create hazardous mutable object sharing as well as observable identity
909 * according to == and ===.
911 * JSGET_NO_METHOD_BARRIER avoids the performance overhead of the method read
912 * barrier, which is not needed when invoking a lambda that otherwise does not
913 * leak its callee reference (via arguments.callee or its name).
915 const uintN JSGET_CACHE_RESULT = 1; // from a caching interpreter opcode
916 const uintN JSGET_METHOD_BARRIER = 0; // get can leak joined function object
917 const uintN JSGET_NO_METHOD_BARRIER = 2; // call to joined function can't leak
920 * NB: js_NativeGet and js_NativeSet are called with the scope containing sprop
921 * (pobj's scope for Get, obj's for Set) locked, and on successful return, that
922 * scope is again locked. But on failure, both functions return false with the
923 * scope containing sprop unlocked.
925 extern JSBool
926 js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj,
927 JSScopeProperty *sprop, uintN getHow, jsval *vp);
929 extern JSBool
930 js_NativeSet(JSContext *cx, JSObject *obj, JSScopeProperty *sprop, bool added,
931 jsval *vp);
933 extern JSBool
934 js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN getHow,
935 jsval *vp);
937 extern JSBool
938 js_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
940 extern JSBool
941 js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, jsval *vp);
944 * Check whether it is OK to assign an undeclared property of the global
945 * object at the current script PC.
947 extern JS_FRIEND_API(bool)
948 js_CheckUndeclaredVarAssignment(JSContext *cx);
950 extern JSBool
951 js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
952 jsval *vp);
954 extern JSBool
955 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
957 extern JSBool
958 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
959 uintN *attrsp);
961 extern JSBool
962 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
963 uintN *attrsp);
965 extern JSBool
966 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *rval);
968 extern JSBool
969 js_DefaultValue(JSContext *cx, JSObject *obj, JSType hint, jsval *vp);
971 extern JSBool
972 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
973 jsval *statep, jsid *idp);
975 extern void
976 js_MarkEnumeratorState(JSTracer *trc, JSObject *obj, jsval state);
978 extern void
979 js_PurgeCachedNativeEnumerators(JSContext *cx, JSThreadData *data);
981 extern JSBool
982 js_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
983 jsval *vp, uintN *attrsp);
985 extern JSBool
986 js_Call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
988 extern JSBool
989 js_Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
990 jsval *rval);
992 extern JSBool
993 js_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
995 extern JSBool
996 js_SetProtoOrParent(JSContext *cx, JSObject *obj, uint32 slot, JSObject *pobj,
997 JSBool checkForCycles);
999 extern JSBool
1000 js_IsDelegate(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
1002 extern JSBool
1003 js_GetClassPrototype(JSContext *cx, JSObject *scope, jsid id,
1004 JSObject **protop);
1006 extern JSBool
1007 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto,
1008 uintN attrs);
1011 * Wrap boolean, number or string as Boolean, Number or String object.
1012 * *vp must not be an object, null or undefined.
1014 extern JSBool
1015 js_PrimitiveToObject(JSContext *cx, jsval *vp);
1017 extern JSBool
1018 js_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
1020 extern JSObject *
1021 js_ValueToNonNullObject(JSContext *cx, jsval v);
1023 extern JSBool
1024 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, jsval *rval);
1026 extern JSBool
1027 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
1028 uintN argc, jsval *argv, jsval *rval);
1030 extern JSBool
1031 js_XDRObject(JSXDRState *xdr, JSObject **objp);
1033 extern void
1034 js_TraceObject(JSTracer *trc, JSObject *obj);
1036 extern void
1037 js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize);
1039 extern void
1040 js_Clear(JSContext *cx, JSObject *obj);
1042 extern bool
1043 js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1045 bool
1046 js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1049 * Precondition: obj must be locked.
1051 extern JSBool
1052 js_ReallocSlots(JSContext *cx, JSObject *obj, uint32 nslots,
1053 JSBool exactAllocation);
1055 extern JSObject *
1056 js_CheckScopeChainValidity(JSContext *cx, JSObject *scopeobj, const char *caller);
1058 extern JSBool
1059 js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
1060 JSPrincipals *principals, JSAtom *caller);
1062 /* Infallible -- returns its argument if there is no wrapped object. */
1063 extern JSObject *
1064 js_GetWrappedObject(JSContext *cx, JSObject *obj);
1066 /* NB: Infallible. */
1067 extern const char *
1068 js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
1069 JSPrincipals *principals, uintN *linenop);
1071 /* Infallible, therefore cx is last parameter instead of first. */
1072 extern JSBool
1073 js_IsCallable(JSObject *obj, JSContext *cx);
1075 extern JSBool
1076 js_ReportGetterOnlyAssignment(JSContext *cx);
1078 extern JS_FRIEND_API(JSBool)
1079 js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
1081 #ifdef DEBUG
1082 JS_FRIEND_API(void) js_DumpChars(const jschar *s, size_t n);
1083 JS_FRIEND_API(void) js_DumpString(JSString *str);
1084 JS_FRIEND_API(void) js_DumpAtom(JSAtom *atom);
1085 JS_FRIEND_API(void) js_DumpValue(jsval val);
1086 JS_FRIEND_API(void) js_DumpId(jsid id);
1087 JS_FRIEND_API(void) js_DumpObject(JSObject *obj);
1088 JS_FRIEND_API(void) js_DumpStackFrame(JSStackFrame *fp);
1089 #endif
1091 extern uintN
1092 js_InferFlags(JSContext *cx, uintN defaultFlags);
1094 /* Object constructor native. Exposed only so the JIT can know its address. */
1095 JSBool
1096 js_Object(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
1098 JS_END_EXTERN_C
1100 #endif /* jsobj_h___ */