]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/PropertyName.h
7d2464b4449264b4313f05ed6021d5a462694900
[apple/javascriptcore.git] / runtime / PropertyName.h
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 #ifndef PropertyName_h
27 #define PropertyName_h
28
29 #include "Identifier.h"
30 #include "PrivateName.h"
31
32 namespace JSC {
33
34 template <typename CharType>
35 ALWAYS_INLINE uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length)
36 {
37 // An empty string is not a number.
38 if (!length)
39 return UINT_MAX;
40
41 // Get the first character, turning it into a digit.
42 uint32_t value = characters[0] - '0';
43 if (value > 9)
44 return UINT_MAX;
45
46 // Check for leading zeros. If the first characher is 0, then the
47 // length of the string must be one - e.g. "042" is not equal to "42".
48 if (!value && length > 1)
49 return UINT_MAX;
50
51 while (--length) {
52 // Multiply value by 10, checking for overflow out of 32 bits.
53 if (value > 0xFFFFFFFFU / 10)
54 return UINT_MAX;
55 value *= 10;
56
57 // Get the next character, turning it into a digit.
58 uint32_t newValue = *(++characters) - '0';
59 if (newValue > 9)
60 return UINT_MAX;
61
62 // Add in the old value, checking for overflow out of 32 bits.
63 newValue += value;
64 if (newValue < value)
65 return UINT_MAX;
66 value = newValue;
67 }
68
69 return value;
70 }
71
72 ALWAYS_INLINE uint32_t toUInt32FromStringImpl(StringImpl* impl)
73 {
74 if (impl->is8Bit())
75 return toUInt32FromCharacters(impl->characters8(), impl->length());
76 return toUInt32FromCharacters(impl->characters16(), impl->length());
77 }
78
79 class PropertyName {
80 public:
81 PropertyName(const Identifier& propertyName)
82 : m_impl(propertyName.impl())
83 {
84 ASSERT(!m_impl || m_impl->isAtomic());
85 }
86
87 PropertyName(const PrivateName& propertyName)
88 : m_impl(propertyName.uid())
89 {
90 ASSERT(m_impl && m_impl->isEmptyUnique());
91 }
92
93 StringImpl* uid() const
94 {
95 return m_impl;
96 }
97
98 StringImpl* publicName() const
99 {
100 return m_impl->isEmptyUnique() ? 0 : m_impl;
101 }
102
103 static const uint32_t NotAnIndex = UINT_MAX;
104
105 uint32_t asIndex()
106 {
107 return m_impl ? toUInt32FromStringImpl(m_impl) : NotAnIndex;
108 }
109
110 private:
111 StringImpl* m_impl;
112 };
113
114 inline bool operator==(PropertyName a, const Identifier& b)
115 {
116 return a.uid() == b.impl();
117 }
118
119 inline bool operator==(const Identifier& a, PropertyName b)
120 {
121 return a.impl() == b.uid();
122 }
123
124 inline bool operator==(PropertyName a, PropertyName b)
125 {
126 return a.uid() == b.uid();
127 }
128
129 inline bool operator!=(PropertyName a, const Identifier& b)
130 {
131 return a.uid() != b.impl();
132 }
133
134 inline bool operator!=(const Identifier& a, PropertyName b)
135 {
136 return a.impl() != b.uid();
137 }
138
139 inline bool operator!=(PropertyName a, PropertyName b)
140 {
141 return a.uid() != b.uid();
142 }
143
144 }
145
146 #endif