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