PageRenderTime 46ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsatom.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 458 lines | 295 code | 56 blank | 107 comment | 12 complexity | 1bbf3a65cf3c38107bba3647710eaf70 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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 ***** */
  39. #ifndef jsatom_h___
  40. #define jsatom_h___
  41. /*
  42. * JS atom table.
  43. */
  44. #include <stddef.h>
  45. #include "jsversion.h"
  46. #include "jstypes.h"
  47. #include "jshash.h" /* Added by JSIFY */
  48. #include "jsdhash.h"
  49. #include "jsapi.h"
  50. #include "jsprvtd.h"
  51. #include "jspubtd.h"
  52. #include "jslock.h"
  53. JS_BEGIN_EXTERN_C
  54. #define ATOM_PINNED 0x1 /* atom is pinned against GC */
  55. #define ATOM_INTERNED 0x2 /* pinned variant for JS_Intern* API */
  56. #define ATOM_NOCOPY 0x4 /* don't copy atom string bytes */
  57. #define ATOM_TMPSTR 0x8 /* internal, to avoid extra string */
  58. #define ATOM_KEY(atom) ((jsval)(atom))
  59. #define ATOM_IS_DOUBLE(atom) JSVAL_IS_DOUBLE(ATOM_KEY(atom))
  60. #define ATOM_TO_DOUBLE(atom) JSVAL_TO_DOUBLE(ATOM_KEY(atom))
  61. #define ATOM_IS_STRING(atom) JSVAL_IS_STRING(ATOM_KEY(atom))
  62. #define ATOM_TO_STRING(atom) JSVAL_TO_STRING(ATOM_KEY(atom))
  63. JS_STATIC_ASSERT(sizeof(JSHashNumber) == 4);
  64. JS_STATIC_ASSERT(sizeof(JSAtom *) == JS_BYTES_PER_WORD);
  65. #if JS_BYTES_PER_WORD == 4
  66. # define ATOM_HASH(atom) ((JSHashNumber)(atom) >> 2)
  67. #elif JS_BYTES_PER_WORD == 8
  68. # define ATOM_HASH(atom) (((JSHashNumber)(jsuword)(atom) >> 3) ^ \
  69. (JSHashNumber)((jsuword)(atom) >> 32))
  70. #else
  71. # error "Unsupported configuration"
  72. #endif
  73. /*
  74. * Return a printable, lossless char[] representation of a string-type atom.
  75. * The lifetime of the result extends at least until the next GC activation,
  76. * longer if cx's string newborn root is not overwritten.
  77. */
  78. extern const char *
  79. js_AtomToPrintableString(JSContext *cx, JSAtom *atom);
  80. struct JSAtomListElement {
  81. JSHashEntry entry;
  82. };
  83. #define ALE_ATOM(ale) ((JSAtom *) (ale)->entry.key)
  84. #define ALE_INDEX(ale) ((jsatomid) JS_PTR_TO_UINT32((ale)->entry.value))
  85. #define ALE_JSOP(ale) ((JSOp) JS_PTR_TO_UINT32((ale)->entry.value))
  86. #define ALE_VALUE(ale) ((jsval) (ale)->entry.value)
  87. #define ALE_NEXT(ale) ((JSAtomListElement *) (ale)->entry.next)
  88. #define ALE_SET_ATOM(ale,atom) ((ale)->entry.key = (const void *)(atom))
  89. #define ALE_SET_INDEX(ale,index)((ale)->entry.value = JS_UINT32_TO_PTR(index))
  90. #define ALE_SET_JSOP(ale,op) ((ale)->entry.value = JS_UINT32_TO_PTR(op))
  91. #define ALE_SET_VALUE(ale, v) ((ale)->entry.value = (void *)(v))
  92. struct JSAtomList {
  93. JSHashEntry *list; /* literals indexed for mapping */
  94. JSHashTable *table; /* hash table if list gets too long */
  95. jsuint count; /* count of indexed literals */
  96. };
  97. #define ATOM_LIST_INIT(al) ((al)->list = NULL, (al)->table = NULL, \
  98. (al)->count = 0)
  99. #define ATOM_LIST_SEARCH(_ale,_al,_atom) \
  100. JS_BEGIN_MACRO \
  101. JSHashEntry **_hep; \
  102. ATOM_LIST_LOOKUP(_ale, _hep, _al, _atom); \
  103. JS_END_MACRO
  104. #define ATOM_LIST_LOOKUP(_ale,_hep,_al,_atom) \
  105. JS_BEGIN_MACRO \
  106. if ((_al)->table) { \
  107. _hep = JS_HashTableRawLookup((_al)->table, ATOM_HASH(_atom), \
  108. _atom); \
  109. _ale = *_hep ? (JSAtomListElement *) *_hep : NULL; \
  110. } else { \
  111. JSHashEntry **_alep = &(_al)->list; \
  112. _hep = NULL; \
  113. while ((_ale = (JSAtomListElement *)*_alep) != NULL) { \
  114. if (ALE_ATOM(_ale) == (_atom)) { \
  115. /* Hit, move atom's element to the front of the list. */ \
  116. *_alep = (_ale)->entry.next; \
  117. (_ale)->entry.next = (_al)->list; \
  118. (_al)->list = &_ale->entry; \
  119. break; \
  120. } \
  121. _alep = &_ale->entry.next; \
  122. } \
  123. } \
  124. JS_END_MACRO
  125. struct JSAtomMap {
  126. JSAtom **vector; /* array of ptrs to indexed atoms */
  127. jsatomid length; /* count of (to-be-)indexed atoms */
  128. };
  129. struct JSAtomState {
  130. JSDHashTable stringAtoms; /* hash table with shared strings */
  131. JSDHashTable doubleAtoms; /* hash table with shared doubles */
  132. #ifdef JS_THREADSAFE
  133. JSThinLock lock;
  134. #endif
  135. /*
  136. * From this point until the end of struct definition the struct must
  137. * contain only JSAtom fields. We use this to access the storage occupied
  138. * by the common atoms in js_FinishCommonAtoms.
  139. *
  140. * js_common_atom_names defined in jsatom.c contains C strings for atoms
  141. * in the order of atom fields here. Therefore you must update that array
  142. * if you change member order here.
  143. */
  144. /* The rt->emptyString atom, see jsstr.c's js_InitRuntimeStringState. */
  145. JSAtom *emptyAtom;
  146. /*
  147. * Literal value and type names.
  148. * NB: booleanAtoms must come right before typeAtoms!
  149. */
  150. JSAtom *booleanAtoms[2];
  151. JSAtom *typeAtoms[JSTYPE_LIMIT];
  152. JSAtom *nullAtom;
  153. /* Standard class constructor or prototype names. */
  154. JSAtom *classAtoms[JSProto_LIMIT];
  155. /* Various built-in or commonly-used atoms, pinned on first context. */
  156. JSAtom *anonymousAtom;
  157. JSAtom *applyAtom;
  158. JSAtom *argumentsAtom;
  159. JSAtom *arityAtom;
  160. JSAtom *callAtom;
  161. JSAtom *calleeAtom;
  162. JSAtom *callerAtom;
  163. JSAtom *classPrototypeAtom;
  164. JSAtom *constructorAtom;
  165. JSAtom *countAtom;
  166. JSAtom *eachAtom;
  167. JSAtom *evalAtom;
  168. JSAtom *fileNameAtom;
  169. JSAtom *getAtom;
  170. JSAtom *getterAtom;
  171. JSAtom *indexAtom;
  172. JSAtom *inputAtom;
  173. JSAtom *iteratorAtom;
  174. JSAtom *lengthAtom;
  175. JSAtom *lineNumberAtom;
  176. JSAtom *messageAtom;
  177. JSAtom *nameAtom;
  178. JSAtom *nextAtom;
  179. JSAtom *noSuchMethodAtom;
  180. JSAtom *parentAtom;
  181. JSAtom *protoAtom;
  182. JSAtom *setAtom;
  183. JSAtom *setterAtom;
  184. JSAtom *stackAtom;
  185. JSAtom *toLocaleStringAtom;
  186. JSAtom *toSourceAtom;
  187. JSAtom *toStringAtom;
  188. JSAtom *valueOfAtom;
  189. JSAtom *toJSONAtom;
  190. JSAtom *void0Atom;
  191. #if JS_HAS_XML_SUPPORT
  192. JSAtom *etagoAtom;
  193. JSAtom *namespaceAtom;
  194. JSAtom *ptagcAtom;
  195. JSAtom *qualifierAtom;
  196. JSAtom *spaceAtom;
  197. JSAtom *stagoAtom;
  198. JSAtom *starAtom;
  199. JSAtom *starQualifierAtom;
  200. JSAtom *tagcAtom;
  201. JSAtom *xmlAtom;
  202. #endif
  203. #ifdef NARCISSUS
  204. JSAtom *__call__Atom;
  205. JSAtom *__construct__Atom;
  206. JSAtom *__hasInstance__Atom;
  207. JSAtom *ExecutionContextAtom;
  208. JSAtom *currentAtom;
  209. #endif
  210. /* Less frequently used atoms, pinned lazily by JS_ResolveStandardClass. */
  211. struct {
  212. JSAtom *InfinityAtom;
  213. JSAtom *NaNAtom;
  214. JSAtom *XMLListAtom;
  215. JSAtom *decodeURIAtom;
  216. JSAtom *decodeURIComponentAtom;
  217. JSAtom *defineGetterAtom;
  218. JSAtom *defineSetterAtom;
  219. JSAtom *encodeURIAtom;
  220. JSAtom *encodeURIComponentAtom;
  221. JSAtom *escapeAtom;
  222. JSAtom *functionNamespaceURIAtom;
  223. JSAtom *hasOwnPropertyAtom;
  224. JSAtom *isFiniteAtom;
  225. JSAtom *isNaNAtom;
  226. JSAtom *isPrototypeOfAtom;
  227. JSAtom *isXMLNameAtom;
  228. JSAtom *lookupGetterAtom;
  229. JSAtom *lookupSetterAtom;
  230. JSAtom *parseFloatAtom;
  231. JSAtom *parseIntAtom;
  232. JSAtom *propertyIsEnumerableAtom;
  233. JSAtom *unescapeAtom;
  234. JSAtom *unevalAtom;
  235. JSAtom *unwatchAtom;
  236. JSAtom *watchAtom;
  237. } lazy;
  238. };
  239. #define ATOM_OFFSET_START offsetof(JSAtomState, emptyAtom)
  240. #define LAZY_ATOM_OFFSET_START offsetof(JSAtomState, lazy)
  241. #define ATOM_OFFSET_LIMIT (sizeof(JSAtomState))
  242. #define COMMON_ATOMS_START(state) \
  243. ((JSAtom **)((uint8 *)(state) + ATOM_OFFSET_START))
  244. #define COMMON_ATOM_INDEX(name) \
  245. ((offsetof(JSAtomState, name##Atom) - ATOM_OFFSET_START) \
  246. / sizeof(JSAtom*))
  247. #define COMMON_TYPE_ATOM_INDEX(type) \
  248. ((offsetof(JSAtomState, typeAtoms[type]) - ATOM_OFFSET_START) \
  249. / sizeof(JSAtom*))
  250. /* Start and limit offsets should correspond to atoms. */
  251. JS_STATIC_ASSERT(ATOM_OFFSET_START % sizeof(JSAtom *) == 0);
  252. JS_STATIC_ASSERT(ATOM_OFFSET_LIMIT % sizeof(JSAtom *) == 0);
  253. #define ATOM_OFFSET(name) offsetof(JSAtomState, name##Atom)
  254. #define OFFSET_TO_ATOM(rt,off) (*(JSAtom **)((char*)&(rt)->atomState + (off)))
  255. #define CLASS_ATOM_OFFSET(name) offsetof(JSAtomState,classAtoms[JSProto_##name])
  256. #define CLASS_ATOM(cx,name) \
  257. ((cx)->runtime->atomState.classAtoms[JSProto_##name])
  258. extern const char *const js_common_atom_names[];
  259. extern const size_t js_common_atom_count;
  260. /*
  261. * Macros to access C strings for JSType and boolean literals together with
  262. * checks that boolean names start from index 1 and type names from 1+2.
  263. */
  264. #define JS_BOOLEAN_STR(type) (js_common_atom_names[1 + (type)])
  265. #define JS_TYPE_STR(type) (js_common_atom_names[1 + 2 + (type)])
  266. JS_STATIC_ASSERT(1 * sizeof(JSAtom *) ==
  267. offsetof(JSAtomState, booleanAtoms) - ATOM_OFFSET_START);
  268. JS_STATIC_ASSERT((1 + 2) * sizeof(JSAtom *) ==
  269. offsetof(JSAtomState, typeAtoms) - ATOM_OFFSET_START);
  270. /* Well-known predefined C strings. */
  271. #define JS_PROTO(name,code,init) extern const char js_##name##_str[];
  272. #include "jsproto.tbl"
  273. #undef JS_PROTO
  274. extern const char js_anonymous_str[];
  275. extern const char js_apply_str[];
  276. extern const char js_arguments_str[];
  277. extern const char js_arity_str[];
  278. extern const char js_call_str[];
  279. extern const char js_callee_str[];
  280. extern const char js_caller_str[];
  281. extern const char js_class_prototype_str[];
  282. extern const char js_close_str[];
  283. extern const char js_constructor_str[];
  284. extern const char js_count_str[];
  285. extern const char js_etago_str[];
  286. extern const char js_each_str[];
  287. extern const char js_eval_str[];
  288. extern const char js_fileName_str[];
  289. extern const char js_get_str[];
  290. extern const char js_getter_str[];
  291. extern const char js_index_str[];
  292. extern const char js_input_str[];
  293. extern const char js_iterator_str[];
  294. extern const char js_length_str[];
  295. extern const char js_lineNumber_str[];
  296. extern const char js_message_str[];
  297. extern const char js_name_str[];
  298. extern const char js_namespace_str[];
  299. extern const char js_next_str[];
  300. extern const char js_noSuchMethod_str[];
  301. extern const char js_object_str[];
  302. extern const char js_parent_str[];
  303. extern const char js_proto_str[];
  304. extern const char js_ptagc_str[];
  305. extern const char js_qualifier_str[];
  306. extern const char js_send_str[];
  307. extern const char js_setter_str[];
  308. extern const char js_set_str[];
  309. extern const char js_space_str[];
  310. extern const char js_stack_str[];
  311. extern const char js_stago_str[];
  312. extern const char js_star_str[];
  313. extern const char js_starQualifier_str[];
  314. extern const char js_tagc_str[];
  315. extern const char js_toSource_str[];
  316. extern const char js_toString_str[];
  317. extern const char js_toLocaleString_str[];
  318. extern const char js_undefined_str[];
  319. extern const char js_valueOf_str[];
  320. extern const char js_toJSON_str[];
  321. extern const char js_xml_str[];
  322. #ifdef NARCISSUS
  323. extern const char js___call___str[];
  324. extern const char js___construct___str[];
  325. extern const char js___hasInstance___str[];
  326. extern const char js_ExecutionContext_str[];
  327. extern const char js_current_str[];
  328. #endif
  329. /*
  330. * Initialize atom state. Return true on success, false on failure to allocate
  331. * memory. The caller must zero rt->atomState before calling this function and
  332. * only call it after js_InitGC successfully returns.
  333. */
  334. extern JSBool
  335. js_InitAtomState(JSRuntime *rt);
  336. /*
  337. * Free and clear atom state including any interned string atoms. This
  338. * function must be called before js_FinishGC.
  339. */
  340. extern void
  341. js_FinishAtomState(JSRuntime *rt);
  342. /*
  343. * Atom tracing and garbage collection hooks.
  344. */
  345. extern void
  346. js_TraceAtomState(JSTracer *trc, JSBool allAtoms);
  347. extern void
  348. js_SweepAtomState(JSContext *cx);
  349. extern JSBool
  350. js_InitCommonAtoms(JSContext *cx);
  351. extern void
  352. js_FinishCommonAtoms(JSContext *cx);
  353. /*
  354. * Find or create the atom for a double value. Return null on failure to
  355. * allocate memory.
  356. */
  357. extern JSAtom *
  358. js_AtomizeDouble(JSContext *cx, jsdouble d);
  359. /*
  360. * Find or create the atom for a string. Return null on failure to allocate
  361. * memory.
  362. */
  363. extern JSAtom *
  364. js_AtomizeString(JSContext *cx, JSString *str, uintN flags);
  365. extern JSAtom *
  366. js_Atomize(JSContext *cx, const char *bytes, size_t length, uintN flags);
  367. extern JSAtom *
  368. js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, uintN flags);
  369. /*
  370. * Return an existing atom for the given char array or null if the char
  371. * sequence is currently not atomized.
  372. */
  373. extern JSAtom *
  374. js_GetExistingStringAtom(JSContext *cx, const jschar *chars, size_t length);
  375. /*
  376. * This variant handles all primitive values.
  377. */
  378. JSBool
  379. js_AtomizePrimitiveValue(JSContext *cx, jsval v, JSAtom **atomp);
  380. /*
  381. * Convert v to an atomized string and wrap it as an id.
  382. */
  383. extern JSBool
  384. js_ValueToStringId(JSContext *cx, jsval v, jsid *idp);
  385. #ifdef DEBUG
  386. extern JS_FRIEND_API(void)
  387. js_DumpAtoms(JSContext *cx, FILE *fp);
  388. #endif
  389. /*
  390. * Assign atom an index and insert it on al.
  391. */
  392. extern JSAtomListElement *
  393. js_IndexAtom(JSContext *cx, JSAtom *atom, JSAtomList *al);
  394. /*
  395. * For all unmapped atoms recorded in al, add a mapping from the atom's index
  396. * to its address. map->length must already be set to the number of atoms in
  397. * the list and map->vector must point to pre-allocated memory.
  398. */
  399. extern void
  400. js_InitAtomMap(JSContext *cx, JSAtomMap *map, JSAtomList *al);
  401. JS_END_EXTERN_C
  402. #endif /* jsatom_h___ */