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 jsatom_h___
41 #define jsatom_h___
42 /*
43 * JS atom table.
44 */
45 #include <stddef.h>
46 #include "jstypes.h"
47 #include "jshash.h" /* Added by JSIFY */
48 #include "jsapi.h"
49 #include "jsprvtd.h"
50 #include "jspubtd.h"
52 #ifdef JS_THREADSAFE
53 #include "jslock.h"
54 #endif
56 JS_BEGIN_EXTERN_C
58 #define ATOM_PINNED 0x01 /* atom is pinned against GC */
59 #define ATOM_INTERNED 0x02 /* pinned variant for JS_Intern* API */
60 #define ATOM_MARK 0x04 /* atom is reachable via GC */
61 #define ATOM_NOCOPY 0x40 /* don't copy atom string bytes */
62 #define ATOM_TMPSTR 0x80 /* internal, to avoid extra string */
64 struct JSAtom {
65 JSHashEntry entry; /* key is jsval, value keyword info */
66 uint32 flags; /* pinned, interned, and mark flags */
67 jsatomid number; /* atom serial number and hash code */
68 };
70 #define ATOM_KEY(atom) ((jsval)(atom)->entry.key)
71 #define ATOM_IS_OBJECT(atom) JSVAL_IS_OBJECT(ATOM_KEY(atom))
72 #define ATOM_TO_OBJECT(atom) JSVAL_TO_OBJECT(ATOM_KEY(atom))
73 #define ATOM_IS_INT(atom) JSVAL_IS_INT(ATOM_KEY(atom))
74 #define ATOM_TO_INT(atom) JSVAL_TO_INT(ATOM_KEY(atom))
75 #define ATOM_IS_DOUBLE(atom) JSVAL_IS_DOUBLE(ATOM_KEY(atom))
76 #define ATOM_TO_DOUBLE(atom) JSVAL_TO_DOUBLE(ATOM_KEY(atom))
77 #define ATOM_IS_STRING(atom) JSVAL_IS_STRING(ATOM_KEY(atom))
78 #define ATOM_TO_STRING(atom) JSVAL_TO_STRING(ATOM_KEY(atom))
79 #define ATOM_IS_BOOLEAN(atom) JSVAL_IS_BOOLEAN(ATOM_KEY(atom))
80 #define ATOM_TO_BOOLEAN(atom) JSVAL_TO_BOOLEAN(ATOM_KEY(atom))
82 /*
83 * Return a printable, lossless char[] representation of a string-type atom.
84 * The lifetime of the result extends at least until the next GC activation,
85 * longer if cx's string newborn root is not overwritten.
86 */
87 extern JS_FRIEND_API(const char *)
88 js_AtomToPrintableString(JSContext *cx, JSAtom *atom);
90 #define ATOM_KEYWORD(atom) ((struct keyword *)(atom)->entry.value)
91 #define ATOM_SET_KEYWORD(atom,kw) ((atom)->entry.value = (kw))
93 struct JSAtomListElement {
94 JSHashEntry entry;
95 };
97 #define ALE_ATOM(ale) ((JSAtom *) (ale)->entry.key)
98 #define ALE_INDEX(ale) ((jsatomid) (ale)->entry.value)
99 #define ALE_JSOP(ale) ((JSOp) (ale)->entry.value)
100 #define ALE_NEXT(ale) ((JSAtomListElement *) (ale)->entry.next)
102 #define ALE_SET_ATOM(ale,atom) ((ale)->entry.key = (const void *)(atom))
103 #define ALE_SET_INDEX(ale,index)((ale)->entry.value = (void *)(index))
104 #define ALE_SET_JSOP(ale,op) ((ale)->entry.value = (void *)(op))
105 #define ALE_SET_NEXT(ale,link) ((ale)->entry.next = (JSHashEntry *)(link))
107 struct JSAtomList {
108 JSAtomListElement *list; /* literals indexed for mapping */
109 JSHashTable *table; /* hash table if list gets too long */
110 jsuint count; /* count of indexed literals */
111 };
113 #define ATOM_LIST_INIT(al) ((al)->list = NULL, (al)->table = NULL, \
114 (al)->count = 0)
116 #define ATOM_LIST_SEARCH(_ale,_al,_atom) \
117 JS_BEGIN_MACRO \
118 JSHashEntry **_hep; \
119 ATOM_LIST_LOOKUP(_ale, _hep, _al, _atom); \
120 JS_END_MACRO
122 #define ATOM_LIST_LOOKUP(_ale,_hep,_al,_atom) \
123 JS_BEGIN_MACRO \
124 if ((_al)->table) { \
125 _hep = JS_HashTableRawLookup((_al)->table, _atom->number, _atom); \
126 _ale = *_hep ? (JSAtomListElement *) *_hep : NULL; \
127 } else { \
128 JSAtomListElement **_alep = &(_al)->list; \
129 _hep = NULL; \
130 while ((_ale = *_alep) != NULL) { \
131 if (ALE_ATOM(_ale) == (_atom)) { \
132 /* Hit, move atom's element to the front of the list. */ \
133 *_alep = ALE_NEXT(_ale); \
134 ALE_SET_NEXT(_ale, (_al)->list); \
135 (_al)->list = _ale; \
136 break; \
137 } \
138 _alep = (JSAtomListElement **)&_ale->entry.next; \
139 } \
140 } \
141 JS_END_MACRO
143 struct JSAtomMap {
144 JSAtom **vector; /* array of ptrs to indexed atoms */
145 jsatomid length; /* count of (to-be-)indexed atoms */
146 };
148 struct JSAtomState {
149 JSRuntime *runtime; /* runtime that owns us */
150 JSHashTable *table; /* hash table containing all atoms */
151 jsatomid number; /* one beyond greatest atom number */
152 jsatomid liveAtoms; /* number of live atoms after last GC */
154 /* Type names and value literals. */
155 JSAtom *typeAtoms[JSTYPE_LIMIT];
156 JSAtom *booleanAtoms[2];
157 JSAtom *nullAtom;
159 /* Various built-in or commonly-used atoms, pinned on first context. */
160 JSAtom *ArgumentsAtom;
161 JSAtom *ArrayAtom;
162 JSAtom *BooleanAtom;
163 JSAtom *CallAtom;
164 JSAtom *DateAtom;
165 JSAtom *ErrorAtom;
166 JSAtom *FunctionAtom;
167 JSAtom *MathAtom;
168 JSAtom *NumberAtom;
169 JSAtom *ObjectAtom;
170 JSAtom *RegExpAtom;
171 JSAtom *ScriptAtom;
172 JSAtom *StringAtom;
173 JSAtom *anonymousAtom;
174 JSAtom *argumentsAtom;
175 JSAtom *arityAtom;
176 JSAtom *calleeAtom;
177 JSAtom *callerAtom;
178 JSAtom *classPrototypeAtom;
179 JSAtom *constructorAtom;
180 JSAtom *countAtom;
181 JSAtom *evalAtom;
182 JSAtom *getAtom;
183 JSAtom *getterAtom;
184 JSAtom *indexAtom;
185 JSAtom *inputAtom;
186 JSAtom *lengthAtom;
187 JSAtom *nameAtom;
188 JSAtom *noSuchMethodAtom;
189 JSAtom *parentAtom;
190 JSAtom *protoAtom;
191 JSAtom *setAtom;
192 JSAtom *setterAtom;
193 JSAtom *toLocaleStringAtom;
194 JSAtom *toSourceAtom;
195 JSAtom *toStringAtom;
196 JSAtom *valueOfAtom;
198 /* Less frequently used atoms, pinned lazily by JS_ResolveStandardClass. */
199 struct {
200 JSAtom *EvalErrorAtom;
201 JSAtom *InfinityAtom;
202 JSAtom *InternalErrorAtom;
203 JSAtom *NaNAtom;
204 JSAtom *RangeErrorAtom;
205 JSAtom *ReferenceErrorAtom;
206 JSAtom *SyntaxErrorAtom;
207 JSAtom *TypeErrorAtom;
208 JSAtom *URIErrorAtom;
209 JSAtom *decodeURIAtom;
210 JSAtom *decodeURIComponentAtom;
211 JSAtom *defineGetterAtom;
212 JSAtom *defineSetterAtom;
213 JSAtom *encodeURIAtom;
214 JSAtom *encodeURIComponentAtom;
215 JSAtom *escapeAtom;
216 JSAtom *hasOwnPropertyAtom;
217 JSAtom *isFiniteAtom;
218 JSAtom *isNaNAtom;
219 JSAtom *isPrototypeOfAtom;
220 JSAtom *lookupGetterAtom;
221 JSAtom *lookupSetterAtom;
222 JSAtom *parseFloatAtom;
223 JSAtom *parseIntAtom;
224 JSAtom *propertyIsEnumerableAtom;
225 JSAtom *unescapeAtom;
226 JSAtom *unevalAtom;
227 JSAtom *unwatchAtom;
228 JSAtom *watchAtom;
229 } lazy;
231 #ifdef JS_THREADSAFE
232 JSThinLock lock;
233 volatile uint32 tablegen;
234 #endif
235 };
237 /* Well-known predefined strings and their atoms. */
238 extern const char *js_type_str[];
239 extern const char *js_boolean_str[];
241 extern const char js_Arguments_str[];
242 extern const char js_Array_str[];
243 extern const char js_Boolean_str[];
244 extern const char js_Call_str[];
245 extern const char js_Date_str[];
246 extern const char js_Function_str[];
247 extern const char js_Math_str[];
248 extern const char js_Number_str[];
249 extern const char js_Object_str[];
250 extern const char js_RegExp_str[];
251 extern const char js_Script_str[];
252 extern const char js_String_str[];
253 extern const char js_anonymous_str[];
254 extern const char js_arguments_str[];
255 extern const char js_arity_str[];
256 extern const char js_callee_str[];
257 extern const char js_caller_str[];
258 extern const char js_class_prototype_str[];
259 extern const char js_constructor_str[];
260 extern const char js_count_str[];
261 extern const char js_eval_str[];
262 extern const char js_getter_str[];
263 extern const char js_get_str[];
264 extern const char js_index_str[];
265 extern const char js_input_str[];
266 extern const char js_length_str[];
267 extern const char js_name_str[];
268 extern const char js_noSuchMethod_str[];
269 extern const char js_parent_str[];
270 extern const char js_proto_str[];
271 extern const char js_setter_str[];
272 extern const char js_set_str[];
273 extern const char js_toSource_str[];
274 extern const char js_toString_str[];
275 extern const char js_toLocaleString_str[];
276 extern const char js_valueOf_str[];
278 /*
279 * Initialize atom state. Return true on success, false with an out of
280 * memory error report on failure.
281 */
282 extern JSBool
283 js_InitAtomState(JSContext *cx, JSAtomState *state);
285 /*
286 * Free and clear atom state (except for any interned string atoms).
287 */
288 extern void
289 js_FreeAtomState(JSContext *cx, JSAtomState *state);
291 /*
292 * Interned strings are atoms that live until state's runtime is destroyed.
293 * This function frees all interned string atoms, and then frees and clears
294 * state's members (just as js_FreeAtomState does), unless there aren't any
295 * interned strings in state -- in which case state must be "free" already.
296 *
297 * NB: js_FreeAtomState is called for each "last" context being destroyed in
298 * a runtime, where there may yet be another context created in the runtime;
299 * whereas js_FinishAtomState is called from JS_DestroyRuntime, when we know
300 * that no more contexts will be created. Thus we minimize garbage during
301 * context-free episodes on a runtime, while preserving atoms created by the
302 * JS_Intern*String APIs for the life of the runtime.
303 */
304 extern void
305 js_FinishAtomState(JSAtomState *state);
307 /*
308 * Atom garbage collection hooks.
309 */
310 typedef void
311 (*JSGCThingMarker)(void *thing, void *data);
313 extern void
314 js_MarkAtomState(JSAtomState *state, uintN gcflags, JSGCThingMarker mark,
315 void *data);
317 extern void
318 js_SweepAtomState(JSAtomState *state);
320 extern JSBool
321 js_InitPinnedAtoms(JSContext *cx, JSAtomState *state);
323 extern void
324 js_UnpinPinnedAtoms(JSAtomState *state);
326 /*
327 * Find or create the atom for an object. If we create a new atom, give it the
328 * type indicated in flags. Return 0 on failure to allocate memory.
329 */
330 extern JSAtom *
331 js_AtomizeObject(JSContext *cx, JSObject *obj, uintN flags);
333 /*
334 * Find or create the atom for a Boolean value. If we create a new atom, give
335 * it the type indicated in flags. Return 0 on failure to allocate memory.
336 */
337 extern JSAtom *
338 js_AtomizeBoolean(JSContext *cx, JSBool b, uintN flags);
340 /*
341 * Find or create the atom for an integer value. If we create a new atom, give
342 * it the type indicated in flags. Return 0 on failure to allocate memory.
343 */
344 extern JSAtom *
345 js_AtomizeInt(JSContext *cx, jsint i, uintN flags);
347 /*
348 * Find or create the atom for a double value. If we create a new atom, give
349 * it the type indicated in flags. Return 0 on failure to allocate memory.
350 */
351 extern JSAtom *
352 js_AtomizeDouble(JSContext *cx, jsdouble d, uintN flags);
354 /*
355 * Find or create the atom for a string. If we create a new atom, give it the
356 * type indicated in flags. Return 0 on failure to allocate memory.
357 */
358 extern JSAtom *
359 js_AtomizeString(JSContext *cx, JSString *str, uintN flags);
361 extern JS_FRIEND_API(JSAtom *)
362 js_Atomize(JSContext *cx, const char *bytes, size_t length, uintN flags);
364 extern JS_FRIEND_API(JSAtom *)
365 js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, uintN flags);
367 /*
368 * This variant handles all value tag types.
369 */
370 extern JSAtom *
371 js_AtomizeValue(JSContext *cx, jsval value, uintN flags);
373 /*
374 * Convert v to an atomized string.
375 */
376 extern JSAtom *
377 js_ValueToStringAtom(JSContext *cx, jsval v);
379 /*
380 * Assign atom an index and insert it on al.
381 */
382 extern JSAtomListElement *
383 js_IndexAtom(JSContext *cx, JSAtom *atom, JSAtomList *al);
385 /*
386 * Get the atom with index i from map.
387 */
388 extern JS_FRIEND_API(JSAtom *)
389 js_GetAtom(JSContext *cx, JSAtomMap *map, jsatomid i);
391 /*
392 * For all unmapped atoms recorded in al, add a mapping from the atom's index
393 * to its address. The GC must not run until all indexed atoms in atomLists
394 * have been mapped by scripts connected to live objects (Function and Script
395 * class objects have scripts as/in their private data -- the GC knows about
396 * these two classes).
397 */
398 extern JS_FRIEND_API(JSBool)
399 js_InitAtomMap(JSContext *cx, JSAtomMap *map, JSAtomList *al);
401 /*
402 * Free map->vector and clear map.
403 */
404 extern JS_FRIEND_API(void)
405 js_FreeAtomMap(JSContext *cx, JSAtomMap *map);
407 JS_END_EXTERN_C
409 #endif /* jsatom_h___ */