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