1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/HashTable.h>
29
30namespace JSC {
31
32class FunctionExecutable;
33class JSGlobalObject;
34class JSObject;
35
36class PrototypeKey {
37public:
38 PrototypeKey() { }
39
40 PrototypeKey(JSObject* prototype, FunctionExecutable* executable, unsigned inlineCapacity, const ClassInfo* classInfo, JSGlobalObject* globalObject)
41 : m_prototype(prototype)
42 , m_executable(executable)
43 , m_inlineCapacity(inlineCapacity)
44 , m_classInfo(classInfo)
45 , m_globalObject(globalObject)
46 {
47 }
48
49 PrototypeKey(WTF::HashTableDeletedValueType)
50 : m_inlineCapacity(1)
51 {
52 }
53
54 JSObject* prototype() const { return m_prototype; }
55 FunctionExecutable* executable() const { return m_executable; }
56 unsigned inlineCapacity() const { return m_inlineCapacity; }
57 const ClassInfo* classInfo() const { return m_classInfo; }
58 JSGlobalObject* globalObject() const { return m_globalObject; }
59
60 bool operator==(const PrototypeKey& other) const
61 {
62 return m_prototype == other.m_prototype
63 && m_executable == other.m_executable
64 && m_inlineCapacity == other.m_inlineCapacity
65 && m_classInfo == other.m_classInfo
66 && m_globalObject == other.m_globalObject;
67 }
68
69 bool operator!=(const PrototypeKey& other) const { return !(*this == other); }
70 explicit operator bool() const { return *this != PrototypeKey(); }
71 bool isHashTableDeletedValue() const { return *this == PrototypeKey(WTF::HashTableDeletedValue); }
72
73 unsigned hash() const
74 {
75 return WTF::IntHash<uintptr_t>::hash(bitwise_cast<uintptr_t>(m_prototype) ^ bitwise_cast<uintptr_t>(m_executable) ^ bitwise_cast<uintptr_t>(m_classInfo) ^ bitwise_cast<uintptr_t>(m_globalObject)) + m_inlineCapacity;
76 }
77
78private:
79 // WARNING: We require all of these default values to be zero. Otherwise, you'll need to add
80 // "static const bool emptyValueIsZero = false;" to the HashTraits at the bottom of this file.
81 JSObject* m_prototype { nullptr };
82 FunctionExecutable* m_executable { nullptr };
83 unsigned m_inlineCapacity { 0 };
84 const ClassInfo* m_classInfo { nullptr };
85 JSGlobalObject* m_globalObject { nullptr };
86};
87
88struct PrototypeKeyHash {
89 static unsigned hash(const PrototypeKey& key) { return key.hash(); }
90 static bool equal(const PrototypeKey& a, const PrototypeKey& b) { return a == b; }
91 static const bool safeToCompareToEmptyOrDeleted = true;
92};
93
94} // namespace JSC
95
96namespace WTF {
97
98template<typename T> struct DefaultHash;
99template<> struct DefaultHash<JSC::PrototypeKey> {
100 typedef JSC::PrototypeKeyHash Hash;
101};
102
103template<typename T> struct HashTraits;
104template<> struct HashTraits<JSC::PrototypeKey> : SimpleClassHashTraits<JSC::PrototypeKey> { };
105
106} // namespace WTF
107
108