| 1 | /* |
| 2 | * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>. |
| 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. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <limits> |
| 29 | #include <wtf/Vector.h> |
| 30 | #include <wtf/text/StringHash.h> |
| 31 | #include <wtf/text/WTFString.h> |
| 32 | |
| 33 | namespace JSC { |
| 34 | |
| 35 | class TemplateObjectDescriptorTable; |
| 36 | |
| 37 | class TemplateObjectDescriptor : public RefCounted<TemplateObjectDescriptor> { |
| 38 | public: |
| 39 | typedef Vector<String, 4> StringVector; |
| 40 | typedef Vector<Optional<String>, 4> OptionalStringVector; |
| 41 | |
| 42 | enum DeletedValueTag { DeletedValue }; |
| 43 | TemplateObjectDescriptor(DeletedValueTag); |
| 44 | enum EmptyValueTag { EmptyValue }; |
| 45 | TemplateObjectDescriptor(EmptyValueTag); |
| 46 | |
| 47 | bool isDeletedValue() const { return m_rawStrings.isEmpty() && m_hash == std::numeric_limits<unsigned>::max(); } |
| 48 | |
| 49 | bool isEmptyValue() const { return m_rawStrings.isEmpty() && !m_hash; } |
| 50 | |
| 51 | unsigned hash() const { return m_hash; } |
| 52 | |
| 53 | const StringVector& rawStrings() const { return m_rawStrings; } |
| 54 | const OptionalStringVector& cookedStrings() const { return m_cookedStrings; } |
| 55 | |
| 56 | bool operator==(const TemplateObjectDescriptor& other) const { return m_hash == other.m_hash && m_rawStrings == other.m_rawStrings; } |
| 57 | bool operator!=(const TemplateObjectDescriptor& other) const { return m_hash != other.m_hash || m_rawStrings != other.m_rawStrings; } |
| 58 | |
| 59 | struct Hasher { |
| 60 | static unsigned hash(const TemplateObjectDescriptor& key) { return key.hash(); } |
| 61 | static bool equal(const TemplateObjectDescriptor& a, const TemplateObjectDescriptor& b) { return a == b; } |
| 62 | static const bool safeToCompareToEmptyOrDeleted = false; |
| 63 | }; |
| 64 | |
| 65 | static unsigned calculateHash(const StringVector& rawStrings); |
| 66 | ~TemplateObjectDescriptor(); |
| 67 | |
| 68 | static Ref<TemplateObjectDescriptor> create(StringVector&& rawStrings, OptionalStringVector&& cookedStrings) |
| 69 | { |
| 70 | return adoptRef(*new TemplateObjectDescriptor(WTFMove(rawStrings), WTFMove(cookedStrings))); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | TemplateObjectDescriptor(StringVector&& rawStrings, OptionalStringVector&& cookedStrings); |
| 75 | |
| 76 | StringVector m_rawStrings; |
| 77 | OptionalStringVector m_cookedStrings; |
| 78 | unsigned m_hash { 0 }; |
| 79 | }; |
| 80 | |
| 81 | inline TemplateObjectDescriptor::TemplateObjectDescriptor(StringVector&& rawStrings, OptionalStringVector&& cookedStrings) |
| 82 | : m_rawStrings(WTFMove(rawStrings)) |
| 83 | , m_cookedStrings(WTFMove(cookedStrings)) |
| 84 | , m_hash(calculateHash(m_rawStrings)) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | inline TemplateObjectDescriptor::TemplateObjectDescriptor(DeletedValueTag) |
| 89 | : m_hash(std::numeric_limits<unsigned>::max()) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | inline TemplateObjectDescriptor::TemplateObjectDescriptor(EmptyValueTag) |
| 94 | : m_hash(0) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | inline unsigned TemplateObjectDescriptor::calculateHash(const StringVector& rawStrings) |
| 99 | { |
| 100 | StringHasher hasher; |
| 101 | for (const String& string : rawStrings) { |
| 102 | if (string.is8Bit()) |
| 103 | hasher.addCharacters(string.characters8(), string.length()); |
| 104 | else |
| 105 | hasher.addCharacters(string.characters16(), string.length()); |
| 106 | } |
| 107 | return hasher.hash(); |
| 108 | } |
| 109 | |
| 110 | } // namespace JSC |
| 111 | |
| 112 | namespace WTF { |
| 113 | template<typename T> struct DefaultHash; |
| 114 | |
| 115 | template<> struct DefaultHash<JSC::TemplateObjectDescriptor> { |
| 116 | typedef JSC::TemplateObjectDescriptor::Hasher Hash; |
| 117 | }; |
| 118 | |
| 119 | template<> struct HashTraits<JSC::TemplateObjectDescriptor> : CustomHashTraits<JSC::TemplateObjectDescriptor> { |
| 120 | }; |
| 121 | |
| 122 | } // namespace WTF |
| 123 | |