1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsobj_h___
41 #define jsobj_h___
42 /*
43 * JS object definitions.
44 *
45 * A JS object consists of a possibly-shared object descriptor containing
46 * ordered property names, called the map; and a dense vector of property
47 * values, called slots. The map/slot pointer pair is GC'ed, while the map
48 * is reference counted and the slot vector is malloc'ed.
49 */
50 #include "jshash.h" /* Added by JSIFY */
51 #include "jsprvtd.h"
52 #include "jspubtd.h"
54 JS_BEGIN_EXTERN_C
56 struct JSObjectMap {
57 jsrefcount nrefs; /* count of all referencing objects */
58 JSObjectOps *ops; /* high level object operation vtable */
59 uint32 nslots; /* length of obj->slots vector */
60 uint32 freeslot; /* index of next free obj->slots element */
61 };
63 /* Shorthand macros for frequently-made calls. */
64 #if defined JS_THREADSAFE && defined DEBUG
65 #define OBJ_LOOKUP_PROPERTY(cx,obj,id,objp,propp) \
66 (obj)->map->ops->lookupProperty(cx,obj,id,objp,propp,__FILE__,__LINE__)
67 #else
68 #define OBJ_LOOKUP_PROPERTY(cx,obj,id,objp,propp) \
69 (obj)->map->ops->lookupProperty(cx,obj,id,objp,propp)
70 #endif
71 #define OBJ_DEFINE_PROPERTY(cx,obj,id,value,getter,setter,attrs,propp) \
72 (obj)->map->ops->defineProperty(cx,obj,id,value,getter,setter,attrs,propp)
73 #define OBJ_GET_PROPERTY(cx,obj,id,vp) \
74 (obj)->map->ops->getProperty(cx,obj,id,vp)
75 #define OBJ_SET_PROPERTY(cx,obj,id,vp) \
76 (obj)->map->ops->setProperty(cx,obj,id,vp)
77 #define OBJ_GET_ATTRIBUTES(cx,obj,id,prop,attrsp) \
78 (obj)->map->ops->getAttributes(cx,obj,id,prop,attrsp)
79 #define OBJ_SET_ATTRIBUTES(cx,obj,id,prop,attrsp) \
80 (obj)->map->ops->setAttributes(cx,obj,id,prop,attrsp)
81 #define OBJ_DELETE_PROPERTY(cx,obj,id,rval) \
82 (obj)->map->ops->deleteProperty(cx,obj,id,rval)
83 #define OBJ_DEFAULT_VALUE(cx,obj,hint,vp) \
84 (obj)->map->ops->defaultValue(cx,obj,hint,vp)
85 #define OBJ_ENUMERATE(cx,obj,enum_op,statep,idp) \
86 (obj)->map->ops->enumerate(cx,obj,enum_op,statep,idp)
87 #define OBJ_CHECK_ACCESS(cx,obj,id,mode,vp,attrsp) \
88 (obj)->map->ops->checkAccess(cx,obj,id,mode,vp,attrsp)
90 /* These four are time-optimized to avoid stub calls. */
91 #define OBJ_THIS_OBJECT(cx,obj) \
92 ((obj)->map->ops->thisObject \
93 ? (obj)->map->ops->thisObject(cx,obj) \
94 : (obj))
95 #define OBJ_DROP_PROPERTY(cx,obj,prop) \
96 ((obj)->map->ops->dropProperty \
97 ? (obj)->map->ops->dropProperty(cx,obj,prop) \
98 : (void)0)
99 #define OBJ_GET_REQUIRED_SLOT(cx,obj,slot) \
100 ((obj)->map->ops->getRequiredSlot \
101 ? (obj)->map->ops->getRequiredSlot(cx, obj, slot) \
102 : JSVAL_VOID)
103 #define OBJ_SET_REQUIRED_SLOT(cx,obj,slot,v) \
104 ((obj)->map->ops->setRequiredSlot \
105 ? (obj)->map->ops->setRequiredSlot(cx, obj, slot, v) \
106 : JS_TRUE)
108 /*
109 * In the original JS engine design, obj->slots pointed to a vector of length
110 * JS_INITIAL_NSLOTS words if obj->map was shared with a prototype object,
111 * else of length obj->map->nslots. With the advent of JS_GetReservedSlot,
112 * JS_SetReservedSlot, and JSCLASS_HAS_RESERVED_SLOTS (see jsapi.h), the size
113 * of the minimum length slots vector in the case where map is shared cannot
114 * be constant. This length starts at JS_INITIAL_NSLOTS, but may advance to
115 * include all the reserved slots.
116 *
117 * Therefore slots must be self-describing. Rather than tag its low order bit
118 * (a bit is all we need) to distinguish initial length from reserved length,
119 * we do "the BSTR thing": over-allocate slots by one jsval, and store the
120 * *net* length (counting usable slots, which have non-negative obj->slots[]
121 * indices) in obj->slots[-1]. All code that sets obj->slots must be aware of
122 * this hack -- you have been warned, and jsobj.c has been updated!
123 */
124 struct JSObject {
125 JSObjectMap *map;
126 jsval *slots;
127 };
129 #define JSSLOT_PROTO 0
130 #define JSSLOT_PARENT 1
131 #define JSSLOT_CLASS 2
132 #define JSSLOT_PRIVATE 3
133 #define JSSLOT_START(clasp) (((clasp)->flags & JSCLASS_HAS_PRIVATE) \
134 ? JSSLOT_PRIVATE + 1 \
135 : JSSLOT_CLASS + 1)
137 #define JSSLOT_FREE(clasp) (JSSLOT_START(clasp) \
138 + JSCLASS_RESERVED_SLOTS(clasp))
140 #define JS_INITIAL_NSLOTS 5
142 #ifdef DEBUG
143 #define MAP_CHECK_SLOT(map,slot) \
144 JS_ASSERT((uint32)slot < JS_MIN((map)->freeslot, (map)->nslots))
145 #define OBJ_CHECK_SLOT(obj,slot) \
146 MAP_CHECK_SLOT((obj)->map, slot)
147 #else
148 #define OBJ_CHECK_SLOT(obj,slot) ((void)0)
149 #endif
151 /* Fast macros for accessing obj->slots while obj is locked (if thread-safe). */
152 #define LOCKED_OBJ_GET_SLOT(obj,slot) \
153 (OBJ_CHECK_SLOT(obj, slot), (obj)->slots[slot])
154 #define LOCKED_OBJ_SET_SLOT(obj,slot,value) \
155 (OBJ_CHECK_SLOT(obj, slot), (obj)->slots[slot] = (value))
156 #define LOCKED_OBJ_GET_PROTO(obj) \
157 JSVAL_TO_OBJECT(LOCKED_OBJ_GET_SLOT(obj, JSSLOT_PROTO))
158 #define LOCKED_OBJ_GET_CLASS(obj) \
159 ((JSClass *)JSVAL_TO_PRIVATE(LOCKED_OBJ_GET_SLOT(obj, JSSLOT_CLASS)))
161 #ifdef JS_THREADSAFE
163 /* Thread-safe functions and wrapper macros for accessing obj->slots. */
164 #define OBJ_GET_SLOT(cx,obj,slot) \
165 (OBJ_CHECK_SLOT(obj, slot), \
166 (OBJ_IS_NATIVE(obj) && OBJ_SCOPE(obj)->ownercx == cx) \
167 ? LOCKED_OBJ_GET_SLOT(obj, slot) \
168 : js_GetSlotThreadSafe(cx, obj, slot))
170 #define OBJ_SET_SLOT(cx,obj,slot,value) \
171 (OBJ_CHECK_SLOT(obj, slot), \
172 (OBJ_IS_NATIVE(obj) && OBJ_SCOPE(obj)->ownercx == cx) \
173 ? (void) LOCKED_OBJ_SET_SLOT(obj, slot, value) \
174 : js_SetSlotThreadSafe(cx, obj, slot, value))
176 /*
177 * If thread-safe, define an OBJ_GET_SLOT wrapper that bypasses, for a native
178 * object, the lock-free "fast path" test of (OBJ_SCOPE(obj)->ownercx == cx),
179 * to avoid needlessly switching from lock-free to lock-full scope when doing
180 * GC on a different context from the last one to own the scope. The caller
181 * in this case is probably a JSClass.mark function, e.g., fun_mark, or maybe
182 * a finalizer.
183 *
184 * The GC runs only when all threads except the one on which the GC is active
185 * are suspended at GC-safe points, so there is no hazard in directly accessing
186 * obj->slots[slot] from the GC's thread, once rt->gcRunning has been set. See
187 * jsgc.c for details.
188 */
189 #define THREAD_IS_RUNNING_GC(rt, thread) \
190 ((rt)->gcRunning && (rt)->gcThread == (thread))
192 #define CX_THREAD_IS_RUNNING_GC(cx) \
193 THREAD_IS_RUNNING_GC((cx)->runtime, (cx)->thread)
195 #define GC_AWARE_GET_SLOT(cx, obj, slot) \
196 ((OBJ_IS_NATIVE(obj) && CX_THREAD_IS_RUNNING_GC(cx)) \
197 ? (obj)->slots[slot] \
198 : OBJ_GET_SLOT(cx, obj, slot))
200 #else /* !JS_THREADSAFE */
202 #define OBJ_GET_SLOT(cx,obj,slot) LOCKED_OBJ_GET_SLOT(obj,slot)
203 #define OBJ_SET_SLOT(cx,obj,slot,value) LOCKED_OBJ_SET_SLOT(obj,slot,value)
204 #define GC_AWARE_GET_SLOT(cx,obj,slot) LOCKED_OBJ_GET_SLOT(obj,slot)
206 #endif /* !JS_THREADSAFE */
208 /* Thread-safe proto, parent, and class access macros. */
209 #define OBJ_GET_PROTO(cx,obj) \
210 JSVAL_TO_OBJECT(OBJ_GET_SLOT(cx, obj, JSSLOT_PROTO))
211 #define OBJ_SET_PROTO(cx,obj,proto) \
212 OBJ_SET_SLOT(cx, obj, JSSLOT_PROTO, OBJECT_TO_JSVAL(proto))
214 #define OBJ_GET_PARENT(cx,obj) \
215 JSVAL_TO_OBJECT(OBJ_GET_SLOT(cx, obj, JSSLOT_PARENT))
216 #define OBJ_SET_PARENT(cx,obj,parent) \
217 OBJ_SET_SLOT(cx, obj, JSSLOT_PARENT, OBJECT_TO_JSVAL(parent))
219 #define OBJ_GET_CLASS(cx,obj) \
220 ((JSClass *)JSVAL_TO_PRIVATE(OBJ_GET_SLOT(cx, obj, JSSLOT_CLASS)))
222 /* Test whether a map or object is native. */
223 #define MAP_IS_NATIVE(map) \
224 ((map)->ops == &js_ObjectOps || \
225 ((map)->ops && (map)->ops->newObjectMap == js_ObjectOps.newObjectMap))
227 #define OBJ_IS_NATIVE(obj) MAP_IS_NATIVE((obj)->map)
229 extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps;
230 extern JS_FRIEND_DATA(JSObjectOps) js_WithObjectOps;
231 extern JSClass js_ObjectClass;
232 extern JSClass js_WithClass;
234 struct JSSharpObjectMap {
235 jsrefcount depth;
236 jsatomid sharpgen;
237 JSHashTable *table;
238 };
240 #define SHARP_BIT ((jsatomid) 1)
241 #define BUSY_BIT ((jsatomid) 2)
242 #define SHARP_ID_SHIFT 2
243 #define IS_SHARP(he) ((jsatomid)(he)->value & SHARP_BIT)
244 #define MAKE_SHARP(he) ((he)->value = (void*)((jsatomid)(he)->value|SHARP_BIT))
245 #define IS_BUSY(he) ((jsatomid)(he)->value & BUSY_BIT)
246 #define MAKE_BUSY(he) ((he)->value = (void*)((jsatomid)(he)->value|BUSY_BIT))
247 #define CLEAR_BUSY(he) ((he)->value = (void*)((jsatomid)(he)->value&~BUSY_BIT))
249 extern JSHashEntry *
250 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
251 jschar **sp);
253 extern void
254 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap);
256 extern JSBool
257 js_obj_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
258 jsval *rval);
260 extern JSBool
261 js_obj_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
262 jsval *rval);
264 extern JSObject *
265 js_InitObjectClass(JSContext *cx, JSObject *obj);
267 /* Select Object.prototype method names shared between jsapi.c and jsobj.c. */
268 extern const char js_watch_str[];
269 extern const char js_unwatch_str[];
270 extern const char js_hasOwnProperty_str[];
271 extern const char js_isPrototypeOf_str[];
272 extern const char js_propertyIsEnumerable_str[];
273 extern const char js_defineGetter_str[];
274 extern const char js_defineSetter_str[];
275 extern const char js_lookupGetter_str[];
276 extern const char js_lookupSetter_str[];
278 extern void
279 js_InitObjectMap(JSObjectMap *map, jsrefcount nrefs, JSObjectOps *ops,
280 JSClass *clasp);
282 extern JSObjectMap *
283 js_NewObjectMap(JSContext *cx, jsrefcount nrefs, JSObjectOps *ops,
284 JSClass *clasp, JSObject *obj);
286 extern void
287 js_DestroyObjectMap(JSContext *cx, JSObjectMap *map);
289 extern JSObjectMap *
290 js_HoldObjectMap(JSContext *cx, JSObjectMap *map);
292 extern JSObjectMap *
293 js_DropObjectMap(JSContext *cx, JSObjectMap *map, JSObject *obj);
295 extern JSObject *
296 js_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
298 extern JSObject *
299 js_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
300 JSObject *parent, uintN argc, jsval *argv);
302 extern void
303 js_FinalizeObject(JSContext *cx, JSObject *obj);
305 extern JSBool
306 js_AllocSlot(JSContext *cx, JSObject *obj, uint32 *slotp);
308 extern void
309 js_FreeSlot(JSContext *cx, JSObject *obj, uint32 slot);
311 /*
312 * Find or create a property named by id in obj's scope, with the given getter
313 * and setter, slot, attributes, and other members.
314 */
315 extern JSScopeProperty *
316 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
317 JSPropertyOp getter, JSPropertyOp setter, uint32 slot,
318 uintN attrs, uintN flags, intN shortid);
320 /*
321 * Change sprop to have the given attrs, getter, and setter in scope, morphing
322 * it into a potentially new JSScopeProperty. Return a pointer to the changed
323 * or identical property.
324 */
325 extern JSScopeProperty *
326 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
327 JSScopeProperty *sprop, uintN attrs, uintN mask,
328 JSPropertyOp getter, JSPropertyOp setter);
330 /*
331 * On error, return false. On success, if propp is non-null, return true with
332 * obj locked and with a held property in *propp; if propp is null, return true
333 * but release obj's lock first. Therefore all callers who pass non-null propp
334 * result parameters must later call OBJ_DROP_PROPERTY(cx, obj, *propp) both to
335 * drop the held property, and to release the lock on obj.
336 */
337 extern JSBool
338 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
339 JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
340 JSProperty **propp);
342 extern JSBool
343 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
344 JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
345 uintN flags, intN shortid, JSProperty **propp);
347 /*
348 * Unlike js_DefineProperty, propp must be non-null. On success, and if id was
349 * found, return true with *objp non-null and locked, and with a held property
350 * stored in *propp. If successful but id was not found, return true with both
351 * *objp and *propp null. Therefore all callers who receive a non-null *propp
352 * must later call OBJ_DROP_PROPERTY(cx, *objp, *propp).
353 */
354 #if defined JS_THREADSAFE && defined DEBUG
355 extern JS_FRIEND_API(JSBool)
356 _js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
357 JSProperty **propp, const char *file, uintN line);
359 #define js_LookupProperty(cx,obj,id,objp,propp) \
360 _js_LookupProperty(cx,obj,id,objp,propp,__FILE__,__LINE__)
361 #else
362 extern JS_FRIEND_API(JSBool)
363 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
364 JSProperty **propp);
365 #endif
367 /*
368 * Specialized subroutine that allows caller to preset JSRESOLVE_* flags.
369 */
370 extern JSBool
371 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
372 JSObject **objp, JSProperty **propp
373 #if defined JS_THREADSAFE && defined DEBUG
374 , const char *file, uintN line
375 #endif
376 );
378 extern JS_FRIEND_API(JSBool)
379 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
380 JSProperty **propp);
382 extern JSObject *
383 js_FindIdentifierBase(JSContext *cx, jsid id);
385 extern JSObject *
386 js_FindVariableScope(JSContext *cx, JSFunction **funp);
388 extern JSBool
389 js_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
391 extern JSBool
392 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
394 extern JSBool
395 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
396 uintN *attrsp);
398 extern JSBool
399 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
400 uintN *attrsp);
402 extern JSBool
403 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *rval);
405 extern JSBool
406 js_DefaultValue(JSContext *cx, JSObject *obj, JSType hint, jsval *vp);
408 extern JSIdArray *
409 js_NewIdArray(JSContext *cx, jsint length);
411 extern JSIdArray *
412 js_GrowIdArray(JSContext *cx, JSIdArray *ida, jsint length);
414 extern JSBool
415 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
416 jsval *statep, jsid *idp);
418 extern JSBool
419 js_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
420 jsval *vp, uintN *attrsp);
422 extern JSBool
423 js_Call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
425 extern JSBool
426 js_Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
427 jsval *rval);
429 extern JSBool
430 js_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
432 extern JSBool
433 js_SetProtoOrParent(JSContext *cx, JSObject *obj, uint32 slot, JSObject *pobj);
435 extern JSBool
436 js_IsDelegate(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
438 extern JSBool
439 js_GetClassPrototype(JSContext *cx, const char *name, JSObject **protop);
441 extern JSBool
442 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto,
443 uintN attrs);
445 extern JSBool
446 js_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
448 extern JSObject *
449 js_ValueToNonNullObject(JSContext *cx, jsval v);
451 extern JSBool
452 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, jsval *rval);
454 extern JSBool
455 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
456 uintN argc, jsval *argv, jsval *rval);
458 extern JSBool
459 js_XDRObject(JSXDRState *xdr, JSObject **objp);
461 extern uint32
462 js_Mark(JSContext *cx, JSObject *obj, void *arg);
464 extern void
465 js_Clear(JSContext *cx, JSObject *obj);
467 extern jsval
468 js_GetRequiredSlot(JSContext *cx, JSObject *obj, uint32 slot);
470 extern JSBool
471 js_SetRequiredSlot(JSContext *cx, JSObject *obj, uint32 slot, jsval v);
473 JS_END_EXTERN_C
475 #endif /* jsobj_h___ */