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 
HashPropertyKey(PropertyKey key)21 static MOZ_ALWAYS_INLINE js::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.asBits);
31 }
32 
33 }  // namespace js
34 
35 namespace mozilla {
36 
37 template <>
38 struct DefaultHasher<JS::PropertyKey> {
39   using Lookup = JS::PropertyKey;
40   static HashNumber hash(JS::PropertyKey key) {
41     return js::HashPropertyKey(key);
42   }
43   static bool match(JS::PropertyKey key1, JS::PropertyKey key2) {
44     return key1 == key2;
45   }
46 };
47 
48 }  // namespace mozilla
49 
50 #endif /* vm_PropertyKey_h */
51