1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
38 * ***** END LICENSE BLOCK ***** */
46 #include "jsversion.h"
50 #include "jshashtable.h"
56 #define ATOM_PINNED 0x1 /* atom is pinned against GC */
57 #define ATOM_INTERNED 0x2 /* pinned variant for JS_Intern* API */
58 #define ATOM_NOCOPY 0x4 /* don't copy atom string bytes */
59 #define ATOM_TMPSTR 0x8 /* internal, to avoid extra string */
61 #define STRING_TO_ATOM(str) (JS_ASSERT(str->isAtomized()), \
63 #define ATOM_TO_STRING(atom) ((JSString *)atom)
64 #define ATOM_TO_JSVAL(atom) STRING_TO_JSVAL(ATOM_TO_STRING(atom))
66 /* Engine-internal extensions of jsid */
68 static JS_ALWAYS_INLINE jsid
69 JSID_FROM_BITS(size_t bits
)
76 static JS_ALWAYS_INLINE jsid
77 ATOM_TO_JSID(JSAtom
*atom
)
79 JS_ASSERT(((size_t)atom
& 0x7) == 0);
80 return JSID_FROM_BITS((size_t)atom
);
83 /* All strings stored in jsids are atomized. */
84 static JS_ALWAYS_INLINE JSBool
87 return JSID_IS_STRING(id
);
90 static JS_ALWAYS_INLINE JSBool
91 JSID_IS_ATOM(jsid id
, JSAtom
*atom
)
93 return JSID_BITS(id
) == JSID_BITS(ATOM_TO_JSID(atom
));
96 static JS_ALWAYS_INLINE JSAtom
*
99 return (JSAtom
*)JSID_TO_STRING(id
);
104 static JS_ALWAYS_INLINE Value
107 if (JSID_IS_STRING(id
))
108 return StringValue(JSID_TO_STRING(id
));
109 if (JS_LIKELY(JSID_IS_INT(id
)))
110 return Int32Value(JSID_TO_INT(id
));
111 if (JS_LIKELY(JSID_IS_OBJECT(id
)))
112 return ObjectValue(*JSID_TO_OBJECT(id
));
113 JS_ASSERT(JSID_IS_DEFAULT_XML_NAMESPACE(id
) || JSID_IS_VOID(id
));
114 return UndefinedValue();
117 static JS_ALWAYS_INLINE jsval
120 return Jsvalify(IdToValue(id
));
125 #if JS_BYTES_PER_WORD == 4
126 # define ATOM_HASH(atom) ((JSHashNumber)(atom) >> 2)
127 #elif JS_BYTES_PER_WORD == 8
128 # define ATOM_HASH(atom) (((JSHashNumber)(jsuword)(atom) >> 3) ^ \
129 (JSHashNumber)((jsuword)(atom) >> 32))
131 # error "Unsupported configuration"
135 * Return a printable, lossless char[] representation of a string-type atom.
136 * The lifetime of the result extends at least until the next GC activation,
137 * longer if cx's string newborn root is not overwritten.
140 js_AtomToPrintableString(JSContext
*cx
, JSAtom
*atom
);
142 struct JSAtomListElement
{
146 #define ALE_ATOM(ale) ((JSAtom *) (ale)->entry.key)
147 #define ALE_INDEX(ale) (jsatomid(uintptr_t((ale)->entry.value)))
148 #define ALE_VALUE(ale) ((jsboxedword) (ale)->entry.value)
149 #define ALE_NEXT(ale) ((JSAtomListElement *) (ale)->entry.next)
152 * In an upvars list, ALE_DEFN(ale)->resolve() is the outermost definition the
153 * name may reference. If a with block or a function that calls eval encloses
154 * the use, the name may end up referring to something else at runtime.
156 #define ALE_DEFN(ale) ((JSDefinition *) (ale)->entry.value)
158 #define ALE_SET_ATOM(ale,atom) ((ale)->entry.key = (const void *)(atom))
159 #define ALE_SET_INDEX(ale,index)((ale)->entry.value = (void *)(index))
160 #define ALE_SET_DEFN(ale, dn) ((ale)->entry.value = (void *)(dn))
161 #define ALE_SET_VALUE(ale, v) ((ale)->entry.value = (void *)(v))
162 #define ALE_SET_NEXT(ale,nxt) ((ale)->entry.next = (JSHashEntry *)(nxt))
165 * NB: JSAtomSet must be plain-old-data as it is embedded in the pn_u union in
166 * JSParseNode. JSAtomList encapsulates all operational uses of a JSAtomSet.
168 * The JSAtomList name is traditional, even though the implementation is a map
169 * (not to be confused with JSAtomMap). In particular the "ALE" and "ale" short
170 * names for JSAtomListElement variables roll off the fingers, compared to ASE
171 * or AME alternatives.
174 JSHashEntry
*list
; /* literals indexed for mapping */
175 JSHashTable
*table
; /* hash table if list gets too long */
176 jsuint count
; /* count of indexed literals */
179 struct JSAtomList
: public JSAtomSet
182 const JSAtomSet
* set
; /* asserted null in mutating methods */
186 list
= NULL
; table
= NULL
; count
= 0;
192 JSAtomList(const JSAtomSet
& as
) {
193 list
= as
.list
; table
= as
.table
; count
= as
.count
;
199 void clear() { JS_ASSERT(!set
); list
= NULL
; table
= NULL
; count
= 0; }
201 JSAtomListElement
*lookup(JSAtom
*atom
) {
203 return rawLookup(atom
, hep
);
206 JSAtomListElement
*rawLookup(JSAtom
*atom
, JSHashEntry
**&hep
);
208 enum AddHow
{ UNIQUE
, SHADOW
, HOIST
};
210 JSAtomListElement
*add(js::Parser
*parser
, JSAtom
*atom
, AddHow how
= UNIQUE
);
212 void remove(js::Parser
*parser
, JSAtom
*atom
) {
214 JSAtomListElement
*ale
= rawLookup(atom
, hep
);
216 rawRemove(parser
, ale
, hep
);
219 void rawRemove(js::Parser
*parser
, JSAtomListElement
*ale
, JSHashEntry
**hep
);
223 * A subclass of JSAtomList with a destructor. This atom list owns its
224 * hash table and its entries, but no keys or values.
226 struct JSAutoAtomList
: public JSAtomList
228 JSAutoAtomList(js::Parser
*p
): parser(p
) {}
231 js::Parser
*parser
; /* For freeing list entries. */
235 * Iterate over an atom list. We define a call operator to minimize the syntax
236 * tax for users. We do not use a more standard pattern using ++ and * because
237 * (a) it's the wrong pattern for a non-scalar; (b) it's overkill -- one method
238 * is enough. (This comment is overkill!)
240 class JSAtomListIterator
{
242 JSAtomListElement
* next
;
246 JSAtomListIterator(JSAtomList
* al
) : list(al
) { reset(); }
249 next
= (JSAtomListElement
*) list
->list
;
253 JSAtomListElement
* operator ()();
257 JSAtom
**vector
; /* array of ptrs to indexed atoms */
258 jsatomid length
; /* count of (to-be-)indexed atoms */
263 #define ATOM_ENTRY_FLAG_MASK ((size_t)(ATOM_PINNED | ATOM_INTERNED))
265 JS_STATIC_ASSERT(ATOM_ENTRY_FLAG_MASK
< JS_GCTHING_ALIGN
);
267 typedef uintptr_t AtomEntryType
;
269 static JS_ALWAYS_INLINE JSString
*
270 AtomEntryToKey(AtomEntryType entry
)
272 JS_ASSERT(entry
!= 0);
273 return (JSString
*)(entry
& ~ATOM_ENTRY_FLAG_MASK
);
278 typedef JSString
*Lookup
;
280 static HashNumber
hash(JSString
*str
) {
281 return js_HashString(str
);
284 static bool match(AtomEntryType entry
, JSString
*lookup
) {
285 return entry
? js_EqualStrings(AtomEntryToKey(entry
), lookup
) : false;
289 typedef HashSet
<AtomEntryType
, AtomHasher
, SystemAllocPolicy
> AtomSet
;
302 * From this point until the end of struct definition the struct must
303 * contain only JSAtom fields. We use this to access the storage occupied
304 * by the common atoms in js_FinishCommonAtoms.
306 * js_common_atom_names defined in jsatom.c contains C strings for atoms
307 * in the order of atom fields here. Therefore you must update that array
308 * if you change member order here.
311 /* The rt->emptyString atom, see jsstr.c's js_InitRuntimeStringState. */
315 * Literal value and type names.
316 * NB: booleanAtoms must come right before typeAtoms!
318 JSAtom
*booleanAtoms
[2];
319 JSAtom
*typeAtoms
[JSTYPE_LIMIT
];
322 /* Standard class constructor or prototype names. */
323 JSAtom
*classAtoms
[JSProto_LIMIT
];
325 /* Various built-in or commonly-used atoms, pinned on first context. */
326 JSAtom
*anonymousAtom
;
328 JSAtom
*argumentsAtom
;
333 JSAtom
*classPrototypeAtom
;
334 JSAtom
*constructorAtom
;
337 JSAtom
*fileNameAtom
;
340 JSAtom
*ignoreCaseAtom
;
343 JSAtom
*iteratorAtom
;
345 JSAtom
*lastIndexAtom
;
347 JSAtom
*lineNumberAtom
;
349 JSAtom
*multilineAtom
;
352 JSAtom
*noSuchMethodAtom
;
358 JSAtom
*toGMTStringAtom
;
359 JSAtom
*toLocaleStringAtom
;
360 JSAtom
*toSourceAtom
;
361 JSAtom
*toStringAtom
;
362 JSAtom
*toUTCStringAtom
;
366 JSAtom
*enumerableAtom
;
367 JSAtom
*configurableAtom
;
368 JSAtom
*writableAtom
;
370 JSAtom
*useStrictAtom
;
372 #if JS_HAS_XML_SUPPORT
374 JSAtom
*namespaceAtom
;
376 JSAtom
*qualifierAtom
;
380 JSAtom
*starQualifierAtom
;
387 JSAtom
*getOwnPropertyDescriptorAtom
;
388 JSAtom
*getPropertyDescriptorAtom
;
389 JSAtom
*definePropertyAtom
;
391 JSAtom
*getOwnPropertyNamesAtom
;
392 JSAtom
*enumerateAtom
;
397 JSAtom
*enumerateOwnAtom
;
400 /* Less frequently used atoms, pinned lazily by JS_ResolveStandardClass. */
402 JSAtom
*InfinityAtom
;
405 JSAtom
*decodeURIAtom
;
406 JSAtom
*decodeURIComponentAtom
;
407 JSAtom
*defineGetterAtom
;
408 JSAtom
*defineSetterAtom
;
409 JSAtom
*encodeURIAtom
;
410 JSAtom
*encodeURIComponentAtom
;
412 JSAtom
*functionNamespaceURIAtom
;
413 JSAtom
*hasOwnPropertyAtom
;
414 JSAtom
*isFiniteAtom
;
416 JSAtom
*isPrototypeOfAtom
;
417 JSAtom
*isXMLNameAtom
;
418 JSAtom
*lookupGetterAtom
;
419 JSAtom
*lookupSetterAtom
;
420 JSAtom
*parseFloatAtom
;
421 JSAtom
*parseIntAtom
;
422 JSAtom
*propertyIsEnumerableAtom
;
423 JSAtom
*unescapeAtom
;
430 #define ATOM(name) cx->runtime->atomState.name##Atom
432 #define ATOM_OFFSET_START offsetof(JSAtomState, emptyAtom)
433 #define LAZY_ATOM_OFFSET_START offsetof(JSAtomState, lazy)
434 #define ATOM_OFFSET_LIMIT (sizeof(JSAtomState))
436 #define COMMON_ATOMS_START(state) \
437 ((JSAtom **)((uint8 *)(state) + ATOM_OFFSET_START))
438 #define COMMON_ATOM_INDEX(name) \
439 ((offsetof(JSAtomState, name##Atom) - ATOM_OFFSET_START) \
441 #define COMMON_TYPE_ATOM_INDEX(type) \
442 ((offsetof(JSAtomState, typeAtoms[type]) - ATOM_OFFSET_START) \
445 #define ATOM_OFFSET(name) offsetof(JSAtomState, name##Atom)
446 #define OFFSET_TO_ATOM(rt,off) (*(JSAtom **)((char*)&(rt)->atomState + (off)))
447 #define CLASS_ATOM_OFFSET(name) offsetof(JSAtomState,classAtoms[JSProto_##name])
449 #define CLASS_ATOM(cx,name) \
450 ((cx)->runtime->atomState.classAtoms[JSProto_##name])
452 extern const char *const js_common_atom_names
[];
453 extern const size_t js_common_atom_count
;
456 * Macros to access C strings for JSType and boolean literals.
458 #define JS_BOOLEAN_STR(type) (js_common_atom_names[1 + (type)])
459 #define JS_TYPE_STR(type) (js_common_atom_names[1 + 2 + (type)])
461 /* Well-known predefined C strings. */
462 #define JS_PROTO(name,code,init) extern const char js_##name##_str[];
463 #include "jsproto.tbl"
466 extern const char js_anonymous_str
[];
467 extern const char js_apply_str
[];
468 extern const char js_arguments_str
[];
469 extern const char js_arity_str
[];
470 extern const char js_call_str
[];
471 extern const char js_callee_str
[];
472 extern const char js_caller_str
[];
473 extern const char js_class_prototype_str
[];
474 extern const char js_close_str
[];
475 extern const char js_constructor_str
[];
476 extern const char js_count_str
[];
477 extern const char js_etago_str
[];
478 extern const char js_each_str
[];
479 extern const char js_eval_str
[];
480 extern const char js_fileName_str
[];
481 extern const char js_get_str
[];
482 extern const char js_getter_str
[];
483 extern const char js_global_str
[];
484 extern const char js_ignoreCase_str
[];
485 extern const char js_index_str
[];
486 extern const char js_input_str
[];
487 extern const char js_iterator_str
[];
488 extern const char js_join_str
[];
489 extern const char js_lastIndex_str
[];
490 extern const char js_length_str
[];
491 extern const char js_lineNumber_str
[];
492 extern const char js_message_str
[];
493 extern const char js_multiline_str
[];
494 extern const char js_name_str
[];
495 extern const char js_namespace_str
[];
496 extern const char js_next_str
[];
497 extern const char js_noSuchMethod_str
[];
498 extern const char js_object_str
[];
499 extern const char js_proto_str
[];
500 extern const char js_ptagc_str
[];
501 extern const char js_qualifier_str
[];
502 extern const char js_send_str
[];
503 extern const char js_setter_str
[];
504 extern const char js_set_str
[];
505 extern const char js_source_str
[];
506 extern const char js_space_str
[];
507 extern const char js_stack_str
[];
508 extern const char js_sticky_str
[];
509 extern const char js_stago_str
[];
510 extern const char js_star_str
[];
511 extern const char js_starQualifier_str
[];
512 extern const char js_tagc_str
[];
513 extern const char js_toGMTString_str
[];
514 extern const char js_toLocaleString_str
[];
515 extern const char js_toSource_str
[];
516 extern const char js_toString_str
[];
517 extern const char js_toUTCString_str
[];
518 extern const char js_undefined_str
[];
519 extern const char js_valueOf_str
[];
520 extern const char js_toJSON_str
[];
521 extern const char js_xml_str
[];
522 extern const char js_enumerable_str
[];
523 extern const char js_configurable_str
[];
524 extern const char js_writable_str
[];
525 extern const char js_value_str
[];
528 * Initialize atom state. Return true on success, false on failure to allocate
529 * memory. The caller must zero rt->atomState before calling this function and
530 * only call it after js_InitGC successfully returns.
533 js_InitAtomState(JSRuntime
*rt
);
536 * Free and clear atom state including any interned string atoms. This
537 * function must be called before js_FinishGC.
540 js_FinishAtomState(JSRuntime
*rt
);
543 * Atom tracing and garbage collection hooks.
547 js_TraceAtomState(JSTracer
*trc
);
550 js_SweepAtomState(JSContext
*cx
);
553 js_InitCommonAtoms(JSContext
*cx
);
556 js_FinishCommonAtoms(JSContext
*cx
);
559 * Find or create the atom for a string. Return null on failure to allocate
563 js_AtomizeString(JSContext
*cx
, JSString
*str
, uintN flags
);
566 js_Atomize(JSContext
*cx
, const char *bytes
, size_t length
, uintN flags
);
569 js_AtomizeChars(JSContext
*cx
, const jschar
*chars
, size_t length
, uintN flags
);
572 * Return an existing atom for the given char array or null if the char
573 * sequence is currently not atomized.
576 js_GetExistingStringAtom(JSContext
*cx
, const jschar
*chars
, size_t length
);
580 extern JS_FRIEND_API(void)
581 js_DumpAtoms(JSContext
*cx
, FILE *fp
);
586 js_ValueToAtom(JSContext
*cx
, const js::Value
&v
, JSAtom
**atomp
);
589 js_ValueToStringId(JSContext
*cx
, const js::Value
&v
, jsid
*idp
);
592 js_InternNonIntElementId(JSContext
*cx
, JSObject
*obj
, const js::Value
&idval
,
595 js_InternNonIntElementId(JSContext
*cx
, JSObject
*obj
, const js::Value
&idval
,
596 jsid
*idp
, js::Value
*vp
);
598 * For all unmapped atoms recorded in al, add a mapping from the atom's index
599 * to its address. map->length must already be set to the number of atoms in
600 * the list and map->vector must point to pre-allocated memory.
603 js_InitAtomMap(JSContext
*cx
, JSAtomMap
*map
, JSAtomList
*al
);
605 #endif /* jsatom_h___ */