/[jscoverage]/trunk/js/jsatom.h
ViewVC logotype

Diff of /trunk/js/jsatom.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 332 by siliconforks, Thu Oct 23 19:03:33 2008 UTC revision 460 by siliconforks, Sat Sep 26 23:15:22 2009 UTC
# Line 65  Line 65 
65  #define ATOM_IS_STRING(atom)      JSVAL_IS_STRING(ATOM_KEY(atom))  #define ATOM_IS_STRING(atom)      JSVAL_IS_STRING(ATOM_KEY(atom))
66  #define ATOM_TO_STRING(atom)      JSVAL_TO_STRING(ATOM_KEY(atom))  #define ATOM_TO_STRING(atom)      JSVAL_TO_STRING(ATOM_KEY(atom))
67    
 JS_STATIC_ASSERT(sizeof(JSHashNumber) == 4);  
 JS_STATIC_ASSERT(sizeof(JSAtom *) == JS_BYTES_PER_WORD);  
   
68  #if JS_BYTES_PER_WORD == 4  #if JS_BYTES_PER_WORD == 4
69  # define ATOM_HASH(atom)          ((JSHashNumber)(atom) >> 2)  # define ATOM_HASH(atom)          ((JSHashNumber)(atom) >> 2)
70  #elif JS_BYTES_PER_WORD == 8  #elif JS_BYTES_PER_WORD == 8
# Line 91  Line 88 
88    
89  #define ALE_ATOM(ale)   ((JSAtom *) (ale)->entry.key)  #define ALE_ATOM(ale)   ((JSAtom *) (ale)->entry.key)
90  #define ALE_INDEX(ale)  ((jsatomid) JS_PTR_TO_UINT32((ale)->entry.value))  #define ALE_INDEX(ale)  ((jsatomid) JS_PTR_TO_UINT32((ale)->entry.value))
 #define ALE_JSOP(ale)   ((JSOp) JS_PTR_TO_UINT32((ale)->entry.value))  
91  #define ALE_VALUE(ale)  ((jsval) (ale)->entry.value)  #define ALE_VALUE(ale)  ((jsval) (ale)->entry.value)
92  #define ALE_NEXT(ale)   ((JSAtomListElement *) (ale)->entry.next)  #define ALE_NEXT(ale)   ((JSAtomListElement *) (ale)->entry.next)
93    
94    /*
95     * In an upvars list, ALE_DEFN(ale)->resolve() is the outermost definition the
96     * name may reference. If a with block or a function that calls eval encloses
97     * the use, the name may end up referring to something else at runtime.
98     */
99    #define ALE_DEFN(ale)   ((JSDefinition *) (ale)->entry.value)
100    
101  #define ALE_SET_ATOM(ale,atom)  ((ale)->entry.key = (const void *)(atom))  #define ALE_SET_ATOM(ale,atom)  ((ale)->entry.key = (const void *)(atom))
102  #define ALE_SET_INDEX(ale,index)((ale)->entry.value = JS_UINT32_TO_PTR(index))  #define ALE_SET_INDEX(ale,index)((ale)->entry.value = JS_UINT32_TO_PTR(index))
103  #define ALE_SET_JSOP(ale,op)    ((ale)->entry.value = JS_UINT32_TO_PTR(op))  #define ALE_SET_DEFN(ale, dn)   ((ale)->entry.value = (void *)(dn))
104  #define ALE_SET_VALUE(ale, v)   ((ale)->entry.value = (void *)(v))  #define ALE_SET_VALUE(ale, v)   ((ale)->entry.value = (void *)(v))
105    #define ALE_SET_NEXT(ale,nxt)   ((ale)->entry.next = (JSHashEntry *)(nxt))
106    
107  struct JSAtomList {  /*
108     * NB: JSAtomSet must be plain-old-data as it is embedded in the pn_u union in
109     * JSParseNode. JSAtomList encapsulates all operational uses of a JSAtomSet.
110     *
111     * The JSAtomList name is traditional, even though the implementation is a map
112     * (not to be confused with JSAtomMap). In particular the "ALE" and "ale" short
113     * names for JSAtomListElement variables roll off the fingers, compared to ASE
114     * or AME alternatives.
115     */
116    struct JSAtomSet {
117      JSHashEntry         *list;          /* literals indexed for mapping */      JSHashEntry         *list;          /* literals indexed for mapping */
118      JSHashTable         *table;         /* hash table if list gets too long */      JSHashTable         *table;         /* hash table if list gets too long */
119      jsuint              count;          /* count of indexed literals */      jsuint              count;          /* count of indexed literals */
120  };  };
121    
122  #define ATOM_LIST_INIT(al)  ((al)->list = NULL, (al)->table = NULL,           \  #ifdef __cplusplus
123                               (al)->count = 0)  
124    struct JSAtomList : public JSAtomSet
125    {
126    #ifdef DEBUG
127        const JSAtomSet* set;               /* asserted null in mutating methods */
128    #endif
129    
130        JSAtomList() {
131            list = NULL; table = NULL; count = 0;
132    #ifdef DEBUG
133            set = NULL;
134    #endif
135        }
136    
137        JSAtomList(const JSAtomSet& as) {
138            list = as.list; table = as.table; count = as.count;
139    #ifdef DEBUG
140            set = &as;
141    #endif
142        }
143    
144        void clear() { JS_ASSERT(!set); list = NULL; table = NULL; count = 0; }
145    
146        JSAtomListElement *lookup(JSAtom *atom) {
147            JSHashEntry **hep;
148            return rawLookup(atom, hep);
149        }
150    
151        JSAtomListElement *rawLookup(JSAtom *atom, JSHashEntry **&hep);
152    
153        enum AddHow { UNIQUE, SHADOW, HOIST };
154    
155        JSAtomListElement *add(JSCompiler *jsc, JSAtom *atom, AddHow how = UNIQUE);
156    
157        void remove(JSCompiler *jsc, JSAtom *atom) {
158            JSHashEntry **hep;
159            JSAtomListElement *ale = rawLookup(atom, hep);
160            if (ale)
161                rawRemove(jsc, ale, hep);
162        }
163    
164        void rawRemove(JSCompiler *jsc, JSAtomListElement *ale, JSHashEntry **hep);
165    };
166    
167    /*
168     * Iterate over an atom list. We define a call operator to minimize the syntax
169     * tax for users. We do not use a more standard pattern using ++ and * because
170     * (a) it's the wrong pattern for a non-scalar; (b) it's overkill -- one method
171     * is enough. (This comment is overkill!)
172     */
173    class JSAtomListIterator {
174        JSAtomList*         list;
175        JSAtomListElement*  next;
176        uint32              index;
177    
178      public:
179        JSAtomListIterator(JSAtomList* al) : list(al) { reset(); }
180    
181        void reset() {
182            next = (JSAtomListElement *) list->list;
183            index = 0;
184        }
185    
186        JSAtomListElement* operator ()();
187    };
188    
189  #define ATOM_LIST_SEARCH(_ale,_al,_atom)                                      \  #endif /* __cplusplus */
     JS_BEGIN_MACRO                                                            \  
         JSHashEntry **_hep;                                                   \  
         ATOM_LIST_LOOKUP(_ale, _hep, _al, _atom);                             \  
     JS_END_MACRO  
   
 #define ATOM_LIST_LOOKUP(_ale,_hep,_al,_atom)                                 \  
     JS_BEGIN_MACRO                                                            \  
         if ((_al)->table) {                                                   \  
             _hep = JS_HashTableRawLookup((_al)->table, ATOM_HASH(_atom),      \  
                                          _atom);                              \  
             _ale = *_hep ? (JSAtomListElement *) *_hep : NULL;                \  
         } else {                                                              \  
             JSHashEntry **_alep = &(_al)->list;                               \  
             _hep = NULL;                                                      \  
             while ((_ale = (JSAtomListElement *)*_alep) != NULL) {            \  
                 if (ALE_ATOM(_ale) == (_atom)) {                              \  
                     /* Hit, move atom's element to the front of the list. */  \  
                     *_alep = (_ale)->entry.next;                              \  
                     (_ale)->entry.next = (_al)->list;                         \  
                     (_al)->list = &_ale->entry;                               \  
                     break;                                                    \  
                 }                                                             \  
                 _alep = &_ale->entry.next;                                    \  
             }                                                                 \  
         }                                                                     \  
     JS_END_MACRO  
190    
191  struct JSAtomMap {  struct JSAtomMap {
192      JSAtom              **vector;       /* array of ptrs to indexed atoms */      JSAtom              **vector;       /* array of ptrs to indexed atoms */
# Line 162  Line 213 
213      /* The rt->emptyString atom, see jsstr.c's js_InitRuntimeStringState. */      /* The rt->emptyString atom, see jsstr.c's js_InitRuntimeStringState. */
214      JSAtom              *emptyAtom;      JSAtom              *emptyAtom;
215    
216      /* Type names and value literals. */      /*
217      JSAtom              *typeAtoms[JSTYPE_LIMIT];       * Literal value and type names.
218         * NB: booleanAtoms must come right before typeAtoms!
219         */
220      JSAtom              *booleanAtoms[2];      JSAtom              *booleanAtoms[2];
221        JSAtom              *typeAtoms[JSTYPE_LIMIT];
222      JSAtom              *nullAtom;      JSAtom              *nullAtom;
223    
224      /* Standard class constructor or prototype names. */      /* Standard class constructor or prototype names. */
# Line 172  Line 226 
226    
227      /* Various built-in or commonly-used atoms, pinned on first context. */      /* Various built-in or commonly-used atoms, pinned on first context. */
228      JSAtom              *anonymousAtom;      JSAtom              *anonymousAtom;
229        JSAtom              *applyAtom;
230      JSAtom              *argumentsAtom;      JSAtom              *argumentsAtom;
231      JSAtom              *arityAtom;      JSAtom              *arityAtom;
232        JSAtom              *callAtom;
233      JSAtom              *calleeAtom;      JSAtom              *calleeAtom;
234      JSAtom              *callerAtom;      JSAtom              *callerAtom;
235      JSAtom              *classPrototypeAtom;      JSAtom              *classPrototypeAtom;
# Line 219  Line 275 
275  #endif  #endif
276    
277  #ifdef NARCISSUS  #ifdef NARCISSUS
278      JSAtom              *callAtom;      JSAtom              *__call__Atom;
279      JSAtom              *constructAtom;      JSAtom              *__construct__Atom;
280      JSAtom              *hasInstanceAtom;      JSAtom              *__hasInstance__Atom;
281      JSAtom              *ExecutionContextAtom;      JSAtom              *ExecutionContextAtom;
282      JSAtom              *currentAtom;      JSAtom              *currentAtom;
283  #endif  #endif
# Line 261  Line 317 
317  #define ATOM_OFFSET_LIMIT       (sizeof(JSAtomState))  #define ATOM_OFFSET_LIMIT       (sizeof(JSAtomState))
318    
319  #define COMMON_ATOMS_START(state)                                             \  #define COMMON_ATOMS_START(state)                                             \
320      (JSAtom **)((uint8 *)(state) + ATOM_OFFSET_START)      ((JSAtom **)((uint8 *)(state) + ATOM_OFFSET_START))
321    #define COMMON_ATOM_INDEX(name)                                               \
322  /* Start and limit offsets should correspond to atoms. */      ((offsetof(JSAtomState, name##Atom) - ATOM_OFFSET_START)                  \
323  JS_STATIC_ASSERT(ATOM_OFFSET_START % sizeof(JSAtom *) == 0);       / sizeof(JSAtom*))
324  JS_STATIC_ASSERT(ATOM_OFFSET_LIMIT % sizeof(JSAtom *) == 0);  #define COMMON_TYPE_ATOM_INDEX(type)                                          \
325        ((offsetof(JSAtomState, typeAtoms[type]) - ATOM_OFFSET_START)             \
326         / sizeof(JSAtom*))
327    
328  #define ATOM_OFFSET(name)       offsetof(JSAtomState, name##Atom)  #define ATOM_OFFSET(name)       offsetof(JSAtomState, name##Atom)
329  #define OFFSET_TO_ATOM(rt,off)  (*(JSAtom **)((char*)&(rt)->atomState + (off)))  #define OFFSET_TO_ATOM(rt,off)  (*(JSAtom **)((char*)&(rt)->atomState + (off)))
# Line 275  Line 333 
333      ((cx)->runtime->atomState.classAtoms[JSProto_##name])      ((cx)->runtime->atomState.classAtoms[JSProto_##name])
334    
335  extern const char *const js_common_atom_names[];  extern const char *const js_common_atom_names[];
336    extern const size_t      js_common_atom_count;
337    
338  /*  /*
339   * Macros to access C strings for JSType and boolean literals together with   * Macros to access C strings for JSType and boolean literals.
340   * checks that type names and booleans starts from index 1 and 1+JSTYPE_LIMIT   */
341   * correspondingly.  #define JS_BOOLEAN_STR(type) (js_common_atom_names[1 + (type)])
342   */  #define JS_TYPE_STR(type)    (js_common_atom_names[1 + 2 + (type)])
 #define JS_TYPE_STR(type)    (js_common_atom_names[1 + (type)])  
 #define JS_BOOLEAN_STR(type) (js_common_atom_names[1 + JSTYPE_LIMIT + (type)])  
   
 JS_STATIC_ASSERT(1 * sizeof(JSAtom *) ==  
                  offsetof(JSAtomState, typeAtoms) - ATOM_OFFSET_START);  
 JS_STATIC_ASSERT((1 + JSTYPE_LIMIT) * sizeof(JSAtom *) ==  
                  offsetof(JSAtomState, booleanAtoms) - ATOM_OFFSET_START);  
343    
344  /* Well-known predefined C strings. */  /* Well-known predefined C strings. */
345  #define JS_PROTO(name,code,init) extern const char js_##name##_str[];  #define JS_PROTO(name,code,init) extern const char js_##name##_str[];
# Line 295  Line 347 
347  #undef JS_PROTO  #undef JS_PROTO
348    
349  extern const char   js_anonymous_str[];  extern const char   js_anonymous_str[];
350    extern const char   js_apply_str[];
351  extern const char   js_arguments_str[];  extern const char   js_arguments_str[];
352  extern const char   js_arity_str[];  extern const char   js_arity_str[];
353    extern const char   js_call_str[];
354  extern const char   js_callee_str[];  extern const char   js_callee_str[];
355  extern const char   js_caller_str[];  extern const char   js_caller_str[];
356  extern const char   js_class_prototype_str[];  extern const char   js_class_prototype_str[];
# Line 342  Line 396 
396  extern const char   js_xml_str[];  extern const char   js_xml_str[];
397    
398  #ifdef NARCISSUS  #ifdef NARCISSUS
399  extern const char   js_call_str[];  extern const char   js___call___str[];
400  extern const char   js_construct_str[];  extern const char   js___construct___str[];
401  extern const char   js_hasInstance_str[];  extern const char   js___hasInstance___str[];
402  extern const char   js_ExecutionContext_str[];  extern const char   js_ExecutionContext_str[];
403  extern const char   js_current_str[];  extern const char   js_current_str[];
404  #endif  #endif
# Line 427  Line 481 
481  #endif  #endif
482    
483  /*  /*
  * Assign atom an index and insert it on al.  
  */  
 extern JSAtomListElement *  
 js_IndexAtom(JSContext *cx, JSAtom *atom, JSAtomList *al);  
   
 /*  
484   * For all unmapped atoms recorded in al, add a mapping from the atom's index   * For all unmapped atoms recorded in al, add a mapping from the atom's index
485   * to its address. map->length must already be set to the number of atoms in   * to its address. map->length must already be set to the number of atoms in
486   * the list and map->vector must point to pre-allocated memory.   * the list and map->vector must point to pre-allocated memory.

Legend:
Removed from v.332  
changed lines
  Added in v.460

  ViewVC Help
Powered by ViewVC 1.1.24