Id.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef js_Id_h
  7. #define js_Id_h
  8. // A jsid is an identifier for a property or method of an object which is
  9. // either a 31-bit unsigned integer, interned string or symbol.
  10. //
  11. // Also, there is an additional jsid value, JSID_VOID, which does not occur in
  12. // JS scripts but may be used to indicate the absence of a valid jsid. A void
  13. // jsid is not a valid id and only arises as an exceptional API return value,
  14. // such as in JS_NextProperty. Embeddings must not pass JSID_VOID into JSAPI
  15. // entry points expecting a jsid and do not need to handle JSID_VOID in hooks
  16. // receiving a jsid except when explicitly noted in the API contract.
  17. //
  18. // A jsid is not implicitly convertible to or from a Value; JS_ValueToId or
  19. // JS_IdToValue must be used instead.
  20. #include "jstypes.h"
  21. #include "js/HeapAPI.h"
  22. #include "js/RootingAPI.h"
  23. #include "js/TypeDecls.h"
  24. #include "js/Utility.h"
  25. struct jsid
  26. {
  27. size_t asBits;
  28. bool operator==(const jsid& rhs) const { return asBits == rhs.asBits; }
  29. bool operator!=(const jsid& rhs) const { return asBits != rhs.asBits; }
  30. } JS_HAZ_GC_POINTER;
  31. #define JSID_BITS(id) (id.asBits)
  32. #define JSID_TYPE_STRING 0x0
  33. #define JSID_TYPE_INT 0x1
  34. #define JSID_TYPE_VOID 0x2
  35. #define JSID_TYPE_SYMBOL 0x4
  36. #define JSID_TYPE_MASK 0x7
  37. // Avoid using canonical 'id' for jsid parameters since this is a magic word in
  38. // Objective-C++ which, apparently, wants to be able to #include jsapi.h.
  39. #define id iden
  40. static MOZ_ALWAYS_INLINE bool
  41. JSID_IS_STRING(jsid id)
  42. {
  43. return (JSID_BITS(id) & JSID_TYPE_MASK) == 0;
  44. }
  45. static MOZ_ALWAYS_INLINE JSString*
  46. JSID_TO_STRING(jsid id)
  47. {
  48. MOZ_ASSERT(JSID_IS_STRING(id));
  49. return (JSString*)JSID_BITS(id);
  50. }
  51. /**
  52. * Only JSStrings that have been interned via the JSAPI can be turned into
  53. * jsids by API clients.
  54. *
  55. * N.B. if a jsid is backed by a string which has not been interned, that
  56. * string must be appropriately rooted to avoid being collected by the GC.
  57. */
  58. JS_PUBLIC_API(jsid)
  59. INTERNED_STRING_TO_JSID(JSContext* cx, JSString* str);
  60. static MOZ_ALWAYS_INLINE bool
  61. JSID_IS_INT(jsid id)
  62. {
  63. return !!(JSID_BITS(id) & JSID_TYPE_INT);
  64. }
  65. static MOZ_ALWAYS_INLINE int32_t
  66. JSID_TO_INT(jsid id)
  67. {
  68. MOZ_ASSERT(JSID_IS_INT(id));
  69. return ((uint32_t)JSID_BITS(id)) >> 1;
  70. }
  71. #define JSID_INT_MIN 0
  72. #define JSID_INT_MAX INT32_MAX
  73. static MOZ_ALWAYS_INLINE bool
  74. INT_FITS_IN_JSID(int32_t i)
  75. {
  76. return i >= 0;
  77. }
  78. static MOZ_ALWAYS_INLINE jsid
  79. INT_TO_JSID(int32_t i)
  80. {
  81. jsid id;
  82. MOZ_ASSERT(INT_FITS_IN_JSID(i));
  83. JSID_BITS(id) = ((i << 1) | JSID_TYPE_INT);
  84. return id;
  85. }
  86. static MOZ_ALWAYS_INLINE bool
  87. JSID_IS_SYMBOL(jsid id)
  88. {
  89. return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_SYMBOL &&
  90. JSID_BITS(id) != JSID_TYPE_SYMBOL;
  91. }
  92. static MOZ_ALWAYS_INLINE JS::Symbol*
  93. JSID_TO_SYMBOL(jsid id)
  94. {
  95. MOZ_ASSERT(JSID_IS_SYMBOL(id));
  96. return (JS::Symbol*)(JSID_BITS(id) & ~(size_t)JSID_TYPE_MASK);
  97. }
  98. static MOZ_ALWAYS_INLINE jsid
  99. SYMBOL_TO_JSID(JS::Symbol* sym)
  100. {
  101. jsid id;
  102. MOZ_ASSERT(sym != nullptr);
  103. MOZ_ASSERT((size_t(sym) & JSID_TYPE_MASK) == 0);
  104. MOZ_ASSERT(!js::gc::IsInsideNursery(reinterpret_cast<js::gc::Cell*>(sym)));
  105. JSID_BITS(id) = (size_t(sym) | JSID_TYPE_SYMBOL);
  106. return id;
  107. }
  108. static MOZ_ALWAYS_INLINE bool
  109. JSID_IS_GCTHING(jsid id)
  110. {
  111. return JSID_IS_STRING(id) || JSID_IS_SYMBOL(id);
  112. }
  113. static MOZ_ALWAYS_INLINE JS::GCCellPtr
  114. JSID_TO_GCTHING(jsid id)
  115. {
  116. void* thing = (void*)(JSID_BITS(id) & ~(size_t)JSID_TYPE_MASK);
  117. if (JSID_IS_STRING(id))
  118. return JS::GCCellPtr(thing, JS::TraceKind::String);
  119. MOZ_ASSERT(JSID_IS_SYMBOL(id));
  120. return JS::GCCellPtr(thing, JS::TraceKind::Symbol);
  121. }
  122. static MOZ_ALWAYS_INLINE bool
  123. JSID_IS_VOID(const jsid id)
  124. {
  125. MOZ_ASSERT_IF(((size_t)JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_VOID,
  126. JSID_BITS(id) == JSID_TYPE_VOID);
  127. return (size_t)JSID_BITS(id) == JSID_TYPE_VOID;
  128. }
  129. static MOZ_ALWAYS_INLINE bool
  130. JSID_IS_EMPTY(const jsid id)
  131. {
  132. return (size_t)JSID_BITS(id) == JSID_TYPE_SYMBOL;
  133. }
  134. extern JS_PUBLIC_DATA(const jsid) JSID_VOID;
  135. extern JS_PUBLIC_DATA(const jsid) JSID_EMPTY;
  136. extern JS_PUBLIC_DATA(const JS::HandleId) JSID_VOIDHANDLE;
  137. extern JS_PUBLIC_DATA(const JS::HandleId) JSID_EMPTYHANDLE;
  138. namespace JS {
  139. template <>
  140. struct GCPolicy<jsid>
  141. {
  142. static jsid initial() { return JSID_VOID; }
  143. static void trace(JSTracer* trc, jsid* idp, const char* name) {
  144. js::UnsafeTraceManuallyBarrieredEdge(trc, idp, name);
  145. }
  146. };
  147. } // namespace JS
  148. namespace js {
  149. template <>
  150. struct BarrierMethods<jsid>
  151. {
  152. static void postBarrier(jsid* idp, jsid prev, jsid next) {}
  153. static void exposeToJS(jsid id) {
  154. if (JSID_IS_GCTHING(id))
  155. js::gc::ExposeGCThingToActiveJS(JSID_TO_GCTHING(id));
  156. }
  157. };
  158. // If the jsid is a GC pointer type, convert to that type and call |f| with
  159. // the pointer. If the jsid is not a GC type, calls F::defaultValue.
  160. template <typename F, typename... Args>
  161. auto
  162. DispatchTyped(F f, const jsid& id, Args&&... args)
  163. -> decltype(f(static_cast<JSString*>(nullptr), mozilla::Forward<Args>(args)...))
  164. {
  165. if (JSID_IS_STRING(id))
  166. return f(JSID_TO_STRING(id), mozilla::Forward<Args>(args)...);
  167. if (JSID_IS_SYMBOL(id))
  168. return f(JSID_TO_SYMBOL(id), mozilla::Forward<Args>(args)...);
  169. MOZ_ASSERT(!JSID_IS_GCTHING(id));
  170. return F::defaultValue(id);
  171. }
  172. #undef id
  173. } // namespace js
  174. #endif /* js_Id_h */