home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsatom.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.7 KB  |  241 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 jsatom_h___
  20. #define jsatom_h___
  21. /*
  22.  * JS atom table.
  23.  */
  24. #include <stddef.h>
  25. #include "prtypes.h"
  26. #ifndef NSPR20
  27. #include "prhash.h"
  28. #else
  29. #include "plhash.h"
  30. #endif
  31. #include "jsapi.h"
  32. #include "jsprvtd.h"
  33. #include "jspubtd.h"
  34.  
  35. PR_BEGIN_EXTERN_C
  36.  
  37. #define ATOM_INDEXED    0x01            /* indexed for literal mapping */
  38. #define ATOM_NOCOPY     0x02            /* don't copy atom string bytes */
  39. #define ATOM_NOHOLD     0x04            /* atomize a weak link for parser */
  40. #define ATOM_TMPSTR     0x08            /* internal, to avoid extra string */
  41.  
  42. struct JSAtom {
  43.     PRHashEntry         entry;          /* key is jsval, value keyword info */
  44.     jsrefcount          nrefs;          /* reference count (not at front!) */
  45.     uint8               flags;          /* flags, ATOM_INDEXED or 0 for now */
  46.     int8                kwindex;        /* keyword index, -1 if not keyword */
  47.     jsatomid            index;          /* index in script-specific atom map */
  48.     jsatomid            number;         /* atom serial number and hash code */
  49. };
  50.  
  51. #define ATOM_KEY(atom)           ((jsval)(atom)->entry.key)
  52. #define ATOM_IS_OBJECT(atom)     JSVAL_IS_OBJECT(ATOM_KEY(atom))
  53. #define ATOM_TO_OBJECT(atom)     JSVAL_TO_OBJECT(ATOM_KEY(atom))
  54. #define ATOM_IS_INT(atom)        JSVAL_IS_INT(ATOM_KEY(atom))
  55. #define ATOM_TO_INT(atom)        JSVAL_TO_INT(ATOM_KEY(atom))
  56. #define ATOM_IS_DOUBLE(atom)     JSVAL_IS_DOUBLE(ATOM_KEY(atom))
  57. #define ATOM_TO_DOUBLE(atom)     JSVAL_TO_DOUBLE(ATOM_KEY(atom))
  58. #define ATOM_IS_STRING(atom)     JSVAL_IS_STRING(ATOM_KEY(atom))
  59. #define ATOM_TO_STRING(atom)     JSVAL_TO_STRING(ATOM_KEY(atom))
  60. #define ATOM_IS_BOOLEAN(atom)    JSVAL_IS_BOOLEAN(ATOM_KEY(atom))
  61. #define ATOM_TO_BOOLEAN(atom)    JSVAL_TO_BOOLEAN(ATOM_KEY(atom))
  62. #define ATOM_BYTES(atom)         JS_GetStringBytes(ATOM_TO_STRING(atom))
  63. #define ATOM_NEXT(atom)          ((JSAtom *)(atom)->entry.value)
  64. #define ATOM_SET_NEXT(atom,next) ((atom)->entry.value = (next))
  65.  
  66. struct JSAtomList {
  67.     JSAtom              *list;          /* literals indexed for mapping */
  68.     jsuint              count;          /* count of indexed literals */
  69. };
  70.  
  71. #define INIT_ATOM_LIST(al)  ((al)->list = NULL, (al)->count = 0)
  72.  
  73. struct JSAtomMap {
  74.     JSAtom              **vector;       /* array of ptrs to indexed atoms */
  75.     jsatomid            length;         /* count of (to-be-)indexed atoms */
  76. };
  77.  
  78. struct JSAtomState {
  79.     PRHashTable         *table;         /* hash table containing all atoms */
  80.     jsatomid            number;         /* one beyond greatest atom number */
  81.  
  82.     /* Type names and value literals. */
  83.     JSAtom              *typeAtoms[JSTYPE_LIMIT];
  84.     JSAtom              *booleanAtoms[2];
  85.     JSAtom              *nullAtom;
  86.  
  87.     /* Various built-in or commonly-used atoms. */
  88.     JSAtom              *ArrayAtom;
  89.     JSAtom              *ObjectAtom;
  90.     JSAtom              *anonymousAtom;
  91.     JSAtom              *assignAtom;
  92.     JSAtom              *classPrototypeAtom;
  93.     JSAtom              *constructorAtom;
  94.     JSAtom              *countAtom;
  95.     JSAtom              *indexAtom;
  96.     JSAtom              *inputAtom;
  97.     JSAtom              *lengthAtom;
  98.     JSAtom              *parentAtom;
  99.     JSAtom              *protoAtom;
  100.     JSAtom              *toStringAtom;
  101.     JSAtom              *valueOfAtom;
  102. };
  103.  
  104. /* Well-known predefined strings and their atoms. */
  105. extern char   *js_type_str[];
  106.  
  107. extern char   js_Array_str[];
  108. extern char   js_Object_str[];
  109. extern char   js_anonymous_str[];
  110. extern char   js_assign_str[];
  111. extern char   js_class_prototype_str[];
  112. extern char   js_constructor_str[];
  113. extern char   js_count_str[];
  114. extern char   js_eval_str[];
  115. extern char   js_index_str[];
  116. extern char   js_input_str[];
  117. extern char   js_length_str[];
  118. extern char   js_parent_str[];
  119. extern char   js_proto_str[];
  120. extern char   js_toString_str[];
  121. extern char   js_valueOf_str[];
  122.  
  123. /*
  124.  * Initialize atom bookkeeping.  Return true on success, false with an out of
  125.  * memory error report on failure.
  126.  */
  127. extern JSBool
  128. js_InitAtomState(JSContext *cx, JSAtomState *state);
  129.  
  130. /*
  131.  * Free and clear atom bookkeeping.
  132.  */
  133. extern void
  134. js_FreeAtomState(JSContext *cx, JSAtomState *state);
  135.  
  136. /*
  137.  * Free and clear atom bookkeeping.
  138.  */
  139. typedef void
  140. (*JSAtomMarker)(JSRuntime *rt, void *thing);
  141.  
  142. extern void
  143. js_MarkAtomState(JSRuntime *rt, JSAtomMarker mark);
  144.  
  145. /*
  146.  * Find or create the atom for an object.  If we create a new atom, give it the
  147.  * type indicated in flags.  Return 0 on failure to allocate memory.
  148.  */
  149. extern JSAtom *
  150. js_AtomizeObject(JSContext *cx, JSObject *obj, uintN flags);
  151.  
  152. /*
  153.  * Find or create the atom for a Boolean value.  If we create a new atom, give
  154.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  155.  */
  156. extern JSAtom *
  157. js_AtomizeBoolean(JSContext *cx, JSBool b, uintN flags);
  158.  
  159. /*
  160.  * Find or create the atom for an integer value.  If we create a new atom, give
  161.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  162.  */
  163. extern JSAtom *
  164. js_AtomizeInt(JSContext *cx, jsint i, uintN flags);
  165.  
  166. /*
  167.  * Find or create the atom for a double value.  If we create a new atom, give
  168.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  169.  */
  170. extern JSAtom *
  171. js_AtomizeDouble(JSContext *cx, jsdouble d, uintN flags);
  172.  
  173. /*
  174.  * Find or create the atom for a string.  If we create a new atom, give it the
  175.  * type indicated in flags.  Return 0 on failure to allocate memory.
  176.  */
  177. extern JSAtom *
  178. js_AtomizeString(JSContext *cx, JSString *str, uintN flags);
  179.  
  180. extern JS_FRIEND_API(JSAtom *)
  181. js_Atomize(JSContext *cx, const char *bytes, size_t length, uintN flags);
  182.  
  183. extern JS_FRIEND_API(JSAtom *)
  184. js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, uintN flags);
  185.  
  186. /*
  187.  * This variant handles all value tag types.
  188.  */
  189. extern JSAtom *
  190. js_AtomizeValue(JSContext *cx, jsval value, uintN flags);
  191.  
  192. /*
  193.  * Convert v to an atomized string.
  194.  */
  195. extern JSAtom *
  196. js_ValueToStringAtom(JSContext *cx, jsval v);
  197.  
  198. /*
  199.  * Assign atom an index and insert it on al.
  200.  */
  201. extern jsatomid
  202. js_IndexAtom(JSContext *cx, JSAtom *atom, JSAtomList *al);
  203.  
  204. /*
  205.  * Atom reference counting operators.
  206.  */
  207. extern JSAtom *
  208. js_HoldAtom(JSContext *cx, JSAtom *atom);
  209.  
  210. extern JS_FRIEND_API(JSAtom *)
  211. js_DropAtom(JSContext *cx, JSAtom *atom);
  212.  
  213. /*
  214.  * Get the atom with index i from map.
  215.  */
  216. extern JSAtom *
  217. js_GetAtom(JSContext *cx, JSAtomMap *map, jsatomid i);
  218.  
  219. /*
  220.  * For all unmapped atoms recorded in al, add a mapping from the atom's index
  221.  * to its address, which holds a reference.
  222.  */
  223. extern JS_FRIEND_API(JSBool)
  224. js_InitAtomMap(JSContext *cx, JSAtomMap *map, JSAtomList *al);
  225.  
  226. /*
  227.  * Drop all atom references from map and free its vector.
  228.  */
  229. extern JS_FRIEND_API(void)
  230. js_FreeAtomMap(JSContext *cx, JSAtomMap *map);
  231.  
  232. /*
  233.  * Drop any unmapped atoms on al.
  234.  */
  235. extern void
  236. js_DropUnmappedAtoms(JSContext *cx, JSAtomList *al);
  237.  
  238. PR_END_EXTERN_C
  239.  
  240. #endif /* jsatom_h___ */
  241.