Id.h 5.6 KB

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