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 "JSGlobalObjectFunctions.h" |
30 | #include "PrivateName.h" |
31 | #include <wtf/Optional.h> |
32 | #include <wtf/dtoa.h> |
33 | |
34 | namespace JSC { |
35 | |
36 | class PropertyName { |
37 | public: |
38 | PropertyName(UniquedStringImpl* propertyName) |
39 | : m_impl(propertyName) |
40 | { |
41 | } |
42 | |
43 | PropertyName(const Identifier& propertyName) |
44 | : PropertyName(propertyName.impl()) |
45 | { |
46 | } |
47 | |
48 | PropertyName(const PrivateName& propertyName) |
49 | : m_impl(&propertyName.uid()) |
50 | { |
51 | ASSERT(m_impl); |
52 | ASSERT(m_impl->isSymbol()); |
53 | } |
54 | |
55 | bool isNull() const { return !m_impl; } |
56 | |
57 | bool isSymbol() const |
58 | { |
59 | return m_impl && m_impl->isSymbol(); |
60 | } |
61 | |
62 | bool isPrivateName() const |
63 | { |
64 | return isSymbol() && static_cast<const SymbolImpl*>(m_impl)->isPrivate(); |
65 | } |
66 | |
67 | UniquedStringImpl* uid() const |
68 | { |
69 | return m_impl; |
70 | } |
71 | |
72 | AtomStringImpl* publicName() const |
73 | { |
74 | return (!m_impl || m_impl->isSymbol()) ? nullptr : static_cast<AtomStringImpl*>(m_impl); |
75 | } |
76 | |
77 | void dump(PrintStream& out) const |
78 | { |
79 | if (m_impl) |
80 | out.print(m_impl); |
81 | else |
82 | out.print("<null property name>" ); |
83 | } |
84 | |
85 | private: |
86 | UniquedStringImpl* m_impl; |
87 | }; |
88 | static_assert(sizeof(PropertyName) == sizeof(UniquedStringImpl*), "UniquedStringImpl* and PropertyName should be compatible to invoke easily from JIT code." ); |
89 | |
90 | inline bool operator==(PropertyName a, const Identifier& b) |
91 | { |
92 | return a.uid() == b.impl(); |
93 | } |
94 | |
95 | inline bool operator==(const Identifier& a, PropertyName b) |
96 | { |
97 | return a.impl() == b.uid(); |
98 | } |
99 | |
100 | inline bool operator==(PropertyName a, PropertyName b) |
101 | { |
102 | return a.uid() == b.uid(); |
103 | } |
104 | |
105 | inline bool operator==(PropertyName a, const char* b) |
106 | { |
107 | return equal(a.uid(), b); |
108 | } |
109 | |
110 | inline bool operator!=(PropertyName a, const Identifier& b) |
111 | { |
112 | return a.uid() != b.impl(); |
113 | } |
114 | |
115 | inline bool operator!=(const Identifier& a, PropertyName b) |
116 | { |
117 | return a.impl() != b.uid(); |
118 | } |
119 | |
120 | inline bool operator!=(PropertyName a, PropertyName b) |
121 | { |
122 | return a.uid() != b.uid(); |
123 | } |
124 | |
125 | ALWAYS_INLINE Optional<uint32_t> parseIndex(PropertyName propertyName) |
126 | { |
127 | auto uid = propertyName.uid(); |
128 | if (!uid) |
129 | return WTF::nullopt; |
130 | if (uid->isSymbol()) |
131 | return WTF::nullopt; |
132 | return parseIndex(*uid); |
133 | } |
134 | |
135 | // https://www.ecma-international.org/ecma-262/9.0/index.html#sec-canonicalnumericindexstring |
136 | ALWAYS_INLINE bool isCanonicalNumericIndexString(const PropertyName& propertyName) |
137 | { |
138 | StringImpl* property = propertyName.uid(); |
139 | if (!property) |
140 | return false; |
141 | if (property->isSymbol()) |
142 | return false; |
143 | if (equal(property, "-0" )) |
144 | return true; |
145 | double index = jsToNumber(property); |
146 | NumberToStringBuffer buffer; |
147 | const char* indexString = WTF::numberToString(index, buffer); |
148 | if (!equal(property, indexString)) |
149 | return false; |
150 | return true; |
151 | } |
152 | |
153 | } // namespace JSC |
154 | |