home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsobj.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.6 KB  |  185 lines

  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 License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsobj_h___
  20. #define jsobj_h___
  21. /*
  22.  * JS object definitions.
  23.  *
  24.  * A JS object consists of a possibly-shared object descriptor containing
  25.  * ordered property names, called the map; and a dense vector of property
  26.  * values, called slots.  The map/slot pointer pair is GC'ed, while the map
  27.  * is reference counted and the slot vector is malloc'ed.
  28.  */
  29. #ifdef NETSCAPE_INTERNAL
  30. #ifndef NSPR20
  31. #include "prhash.h"
  32. #else
  33. #include "plhash.h"
  34. #endif
  35. #endif
  36. #include "jsprvtd.h"
  37. #include "jspubtd.h"
  38.  
  39. PR_BEGIN_EXTERN_C
  40.  
  41. struct JSObjectMap {
  42.     jsrefcount  nrefs;          /* count of all referencing objects */
  43.     uint32      nslots;         /* length of obj->slots vector */
  44.     uint32      freeslot;       /* index of next free obj->slots element */
  45.     JSClass     *clasp;         /* class name and operations */
  46.     JSProperty  *props;         /* property list in definition order */
  47. };
  48.  
  49. struct JSObject {
  50.     JSObjectMap *map;
  51.     jsval       *slots;
  52. };
  53.  
  54. #define JSSLOT_PROTO        0
  55. #define JSSLOT_PARENT       1
  56. #define JSSLOT_PRIVATE      2
  57. #define JSSLOT_START        2
  58.  
  59. #define JS_INITIAL_NSLOTS   5
  60.  
  61. /*
  62.  * Fast get and set macros for well-known slots.  These must be called within
  63.  * JS_LOCK(cx) and JS_UNLOCK(cx).
  64.  */
  65. #ifdef DEBUG
  66. #define MAP_CHECK_SLOT(m,s) PR_ASSERT(s < PR_MAX((m)->nslots, (m)->freeslot))
  67. #define OBJ_CHECK_SLOT(o,s) MAP_CHECK_SLOT((o)->map,s)
  68. #else
  69. #define OBJ_CHECK_SLOT(o,s) ((void)0)
  70. #endif
  71. #define OBJ_GET_SLOT(o,s)   (OBJ_CHECK_SLOT(o,s), (o)->slots[s])
  72. #define OBJ_SET_SLOT(o,s,v) (OBJ_CHECK_SLOT(o,s), (o)->slots[s] = (v))
  73. #define OBJ_GET_PROTO(o)    JSVAL_TO_OBJECT(OBJ_GET_SLOT(o,JSSLOT_PROTO))
  74. #define OBJ_SET_PROTO(o,p)  OBJ_SET_SLOT(o,JSSLOT_PROTO,OBJECT_TO_JSVAL(p))
  75. #define OBJ_GET_PARENT(o)   JSVAL_TO_OBJECT(OBJ_GET_SLOT(o,JSSLOT_PARENT))
  76. #define OBJ_SET_PARENT(o,p) OBJ_SET_SLOT(o,JSSLOT_PARENT,OBJECT_TO_JSVAL(p))
  77.  
  78. extern JSClass js_ObjectClass;
  79. extern JSClass js_WithClass;
  80.  
  81. struct JSSharpObjectMap {
  82.     jsrefcount  depth;
  83.     jsatomid    sharpgen;
  84.     PRHashTable *table;
  85. };
  86.  
  87. #define SHARP_BIT       1
  88. #define IS_SHARP(he)    ((jsatomid)(he)->value & SHARP_BIT)
  89. #define MAKE_SHARP(he)  ((he)->value = (void*)((jsatomid)(he)->value|SHARP_BIT))
  90.  
  91. extern PRHashEntry *
  92. js_EnterSharpObject(JSContext *cx, JSObject *obj, jschar **sp);
  93.  
  94. extern void
  95. js_LeaveSharpObject(JSContext *cx);
  96.  
  97. extern JSBool
  98. js_obj_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
  99.             jsval *rval);
  100.  
  101. extern JSObject *
  102. js_InitObjectClass(JSContext *cx, JSObject *obj);
  103.  
  104. extern JSObject *
  105. js_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
  106.  
  107. extern JSObject *
  108. js_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
  109.            JSObject *parent);
  110.  
  111. extern void
  112. js_FinalizeObject(JSContext *cx, JSObject *obj);
  113.  
  114. extern jsval
  115. js_GetSlot(JSContext *cx, JSObject *obj, uint32 slot);
  116.  
  117. extern JSBool
  118. js_SetSlot(JSContext *cx, JSObject *obj, uint32 slot, jsval value);
  119.  
  120. extern JSBool
  121. js_AllocSlot(JSContext *cx, JSObject *obj, uint32 *slotp);
  122.  
  123. extern void
  124. js_FreeSlot(JSContext *cx, JSObject *obj, uint32 slot);
  125.  
  126. extern JSProperty *
  127. js_DefineProperty(JSContext *cx, JSObject *obj, jsval id, jsval value,
  128.           JSPropertyOp getter, JSPropertyOp setter, uintN flags);
  129.  
  130. /*
  131.  * Pass &obj for objp if you want to resolve lazily bound properties.  In that
  132.  * case, *objp may differ from obj on return; caveat callers.
  133.  */
  134. extern JS_FRIEND_API(JSBool)
  135. js_LookupProperty(JSContext *cx, JSObject *obj, jsval id, JSObject **objp,
  136.           JSProperty **propp);
  137.  
  138. extern JSBool
  139. js_FindProperty(JSContext *cx, jsval id, JSObject **objp, JSProperty **propp);
  140.  
  141. extern JSBool
  142. js_FindVariable(JSContext *cx, jsval id, JSObject **objp, JSProperty **propp);
  143.  
  144. extern JSObject *
  145. js_FindVariableScope(JSContext *cx, JSFunction **funp);
  146.  
  147. extern JSProperty *
  148. js_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
  149.  
  150. extern JSProperty *
  151. js_SetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
  152.  
  153. extern JSBool
  154. js_DeleteProperty(JSContext *cx, JSObject *obj, jsval id, jsval *rval);
  155.  
  156. extern JSBool
  157. js_DeleteProperty2(JSContext *cx, JSObject *obj, JSProperty *prop, jsval id,
  158.            jsval *rval);
  159.  
  160. extern JSBool
  161. js_GetClassPrototype(JSContext *cx, JSClass *clasp, JSObject **protop);
  162.  
  163. extern JSBool
  164. js_SetClassPrototype(JSContext *cx, JSFunction *fun, JSObject *obj);
  165.  
  166. extern JSString *
  167. js_ObjectToString(JSContext *cx, JSObject *obj);
  168.  
  169. extern JSBool
  170. js_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
  171.  
  172. extern JSObject *
  173. js_ValueToNonNullObject(JSContext *cx, jsval v);
  174.  
  175. extern void
  176. js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, jsval *rval);
  177.  
  178. extern void
  179. js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
  180.          uintN argc, jsval *argv, jsval *rval);
  181.  
  182. PR_END_EXTERN_C
  183.  
  184. #endif /* jsobj_h___ */
  185.