tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PropertyKey.h (2069B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      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 
      7 #ifndef vm_PropertyKey_h
      8 #define vm_PropertyKey_h
      9 
     10 #include "mozilla/HashFunctions.h"  // mozilla::HashGeneric
     11 
     12 #include "NamespaceImports.h"  // js::PropertyKey
     13 
     14 #include "js/HashTable.h"   // js::DefaultHasher
     15 #include "js/Id.h"          // JS::PropertyKey
     16 #include "vm/StringType.h"  // JSAtom::hash
     17 #include "vm/SymbolType.h"  // JS::Symbol::hash
     18 
     19 namespace js {
     20 
     21 static MOZ_ALWAYS_INLINE HashNumber HashPropertyKey(PropertyKey key) {
     22  // HashGeneric alone would work, but bits of atom and symbol addresses
     23  // could then be recovered from the hash code. See bug 1330769.
     24  if (MOZ_LIKELY(key.isAtom())) {
     25    return key.toAtom()->hash();
     26  }
     27  if (key.isSymbol()) {
     28    return key.toSymbol()->hash();
     29  }
     30  return mozilla::HashGeneric(key.asRawBits());
     31 }
     32 
     33 // Like HashPropertyKey but optimized for callers that only use atom or symbol
     34 // keys.
     35 static MOZ_ALWAYS_INLINE HashNumber
     36 HashAtomOrSymbolPropertyKey(PropertyKey key) {
     37  if (MOZ_LIKELY(key.isAtom())) {
     38    return key.toAtom()->hash();
     39  }
     40  return key.toSymbol()->hash();
     41 }
     42 
     43 // Like HashPropertyKey but safe to call off-thread.
     44 static MOZ_ALWAYS_INLINE HashNumber HashPropertyKeyThreadSafe(PropertyKey key) {
     45  if (MOZ_LIKELY(key.isAtom())) {
     46    return key.toAtom()->asOffThreadAtom().hash();
     47  }
     48  if (key.isSymbol()) {
     49    return key.toSymbol()->hash();
     50  }
     51  return mozilla::HashGeneric(key.asRawBits());
     52 }
     53 
     54 }  // namespace js
     55 
     56 namespace mozilla {
     57 
     58 template <>
     59 struct DefaultHasher<JS::PropertyKey> {
     60  using Lookup = JS::PropertyKey;
     61  static HashNumber hash(JS::PropertyKey key) {
     62    return js::HashPropertyKey(key);
     63  }
     64  static bool match(JS::PropertyKey key1, JS::PropertyKey key2) {
     65    return key1 == key2;
     66  }
     67 };
     68 
     69 }  // namespace mozilla
     70 
     71 #endif /* vm_PropertyKey_h */