| 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 |
} // namespace js |
| 44 |
|
| 45 |
namespace mozilla { |
| 46 |
|
| 47 |
template <> |
| 48 |
struct DefaultHasher<JS::PropertyKey> { |
| 49 |
using Lookup = JS::PropertyKey; |
| 50 |
static HashNumber hash(JS::PropertyKey key) { |
| 51 |
return js::HashPropertyKey(key); |
| 52 |
} |
| 53 |
static bool match(JS::PropertyKey key1, JS::PropertyKey key2) { |
| 54 |
return key1 == key2; |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
} // namespace mozilla |
| 59 |
|
| 60 |
#endif /* vm_PropertyKey_h */ |