1/*
2 * Copyright (C) 2012 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. 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 "Identifier.h"
29#include "PrivateName.h"
30#include <wtf/Optional.h>
31
32namespace JSC {
33
34class PropertyName {
35public:
36 PropertyName(UniquedStringImpl* propertyName)
37 : m_impl(propertyName)
38 {
39 }
40
41 PropertyName(const Identifier& propertyName)
42 : PropertyName(propertyName.impl())
43 {
44 }
45
46 PropertyName(const PrivateName& propertyName)
47 : m_impl(&propertyName.uid())
48 {
49 ASSERT(m_impl);
50 ASSERT(m_impl->isSymbol());
51 }
52
53 bool isNull() const { return !m_impl; }
54
55 bool isSymbol() const
56 {
57 return m_impl && m_impl->isSymbol();
58 }
59
60 bool isPrivateName() const
61 {
62 return isSymbol() && static_cast<const SymbolImpl*>(m_impl)->isPrivate();
63 }
64
65 UniquedStringImpl* uid() const
66 {
67 return m_impl;
68 }
69
70 AtomicStringImpl* publicName() const
71 {
72 return (!m_impl || m_impl->isSymbol()) ? nullptr : static_cast<AtomicStringImpl*>(m_impl);
73 }
74
75 void dump(PrintStream& out) const
76 {
77 if (m_impl)
78 out.print(m_impl);
79 else
80 out.print("<null property name>");
81 }
82
83private:
84 UniquedStringImpl* m_impl;
85};
86static_assert(sizeof(PropertyName) == sizeof(UniquedStringImpl*), "UniquedStringImpl* and PropertyName should be compatible to invoke easily from JIT code.");
87
88inline bool operator==(PropertyName a, const Identifier& b)
89{
90 return a.uid() == b.impl();
91}
92
93inline bool operator==(const Identifier& a, PropertyName b)
94{
95 return a.impl() == b.uid();
96}
97
98inline bool operator==(PropertyName a, PropertyName b)
99{
100 return a.uid() == b.uid();
101}
102
103inline bool operator==(PropertyName a, const char* b)
104{
105 return equal(a.uid(), b);
106}
107
108inline bool operator!=(PropertyName a, const Identifier& b)
109{
110 return a.uid() != b.impl();
111}
112
113inline bool operator!=(const Identifier& a, PropertyName b)
114{
115 return a.impl() != b.uid();
116}
117
118inline bool operator!=(PropertyName a, PropertyName b)
119{
120 return a.uid() != b.uid();
121}
122
123ALWAYS_INLINE Optional<uint32_t> parseIndex(PropertyName propertyName)
124{
125 auto uid = propertyName.uid();
126 if (!uid)
127 return WTF::nullopt;
128 if (uid->isSymbol())
129 return WTF::nullopt;
130 return parseIndex(*uid);
131}
132
133} // namespace JSC
134