1 | /* |
2 | * Copyright (C) 2013-2018 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 "JSType.h" |
29 | #include <wtf/PrintStream.h> |
30 | |
31 | namespace JSC { |
32 | |
33 | struct ClassInfo; |
34 | |
35 | // Keep in sync with the order of JSType. |
36 | #define FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(macro) \ |
37 | macro(Int8) \ |
38 | macro(Uint8) \ |
39 | macro(Uint8Clamped) \ |
40 | macro(Int16) \ |
41 | macro(Uint16) \ |
42 | macro(Int32) \ |
43 | macro(Uint32) \ |
44 | macro(Float32) \ |
45 | macro(Float64) |
46 | |
47 | #define FOR_EACH_TYPED_ARRAY_TYPE(macro) \ |
48 | FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(macro) \ |
49 | macro(DataView) |
50 | |
51 | enum TypedArrayType { |
52 | NotTypedArray, |
53 | #define DECLARE_TYPED_ARRAY_TYPE(name) Type ## name, |
54 | FOR_EACH_TYPED_ARRAY_TYPE(DECLARE_TYPED_ARRAY_TYPE) |
55 | #undef DECLARE_TYPED_ARRAY_TYPE |
56 | }; |
57 | |
58 | #define ASSERT_TYPED_ARRAY_TYPE(name) \ |
59 | static_assert(static_cast<uint32_t>(Type ## name) == (static_cast<uint32_t>(name ## ArrayType) - FirstTypedArrayType + static_cast<uint32_t>(TypeInt8)), ""); |
60 | FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(ASSERT_TYPED_ARRAY_TYPE) |
61 | #undef ASSERT_TYPED_ARRAY_TYPE |
62 | |
63 | static_assert(TypeDataView == (DataViewType - FirstTypedArrayType + TypeInt8), "" ); |
64 | |
65 | inline unsigned toIndex(TypedArrayType type) |
66 | { |
67 | return static_cast<unsigned>(type) - 1; |
68 | } |
69 | |
70 | inline TypedArrayType indexToTypedArrayType(unsigned index) |
71 | { |
72 | TypedArrayType result = static_cast<TypedArrayType>(index + 1); |
73 | ASSERT(result >= TypeInt8 && result <= TypeDataView); |
74 | return result; |
75 | } |
76 | |
77 | inline bool isTypedView(TypedArrayType type) |
78 | { |
79 | switch (type) { |
80 | case NotTypedArray: |
81 | case TypeDataView: |
82 | return false; |
83 | default: |
84 | return true; |
85 | } |
86 | } |
87 | |
88 | inline unsigned logElementSize(TypedArrayType type) |
89 | { |
90 | switch (type) { |
91 | case NotTypedArray: |
92 | break; |
93 | case TypeInt8: |
94 | case TypeUint8: |
95 | case TypeUint8Clamped: |
96 | case TypeDataView: |
97 | return 0; |
98 | case TypeInt16: |
99 | case TypeUint16: |
100 | return 1; |
101 | case TypeInt32: |
102 | case TypeUint32: |
103 | case TypeFloat32: |
104 | return 2; |
105 | case TypeFloat64: |
106 | return 3; |
107 | } |
108 | RELEASE_ASSERT_NOT_REACHED(); |
109 | return 0; |
110 | } |
111 | |
112 | inline size_t elementSize(TypedArrayType type) |
113 | { |
114 | return static_cast<size_t>(1) << logElementSize(type); |
115 | } |
116 | |
117 | const ClassInfo* constructorClassInfoForType(TypedArrayType); |
118 | |
119 | inline TypedArrayType typedArrayTypeForType(JSType type) |
120 | { |
121 | if (type >= FirstTypedArrayType && type <= LastTypedArrayType) |
122 | return static_cast<TypedArrayType>(type - FirstTypedArrayType + TypeInt8); |
123 | return NotTypedArray; |
124 | } |
125 | |
126 | inline JSType typeForTypedArrayType(TypedArrayType type) |
127 | { |
128 | if (type >= TypeInt8 && type <= TypeDataView) |
129 | return static_cast<JSType>(type - TypeInt8 + FirstTypedArrayType); |
130 | |
131 | RELEASE_ASSERT_NOT_REACHED(); |
132 | return Int8ArrayType; |
133 | } |
134 | |
135 | inline bool isInt(TypedArrayType type) |
136 | { |
137 | switch (type) { |
138 | case TypeInt8: |
139 | case TypeUint8: |
140 | case TypeUint8Clamped: |
141 | case TypeInt16: |
142 | case TypeUint16: |
143 | case TypeInt32: |
144 | case TypeUint32: |
145 | return true; |
146 | default: |
147 | return false; |
148 | } |
149 | } |
150 | |
151 | inline bool isFloat(TypedArrayType type) |
152 | { |
153 | switch (type) { |
154 | case TypeFloat32: |
155 | case TypeFloat64: |
156 | return true; |
157 | default: |
158 | return false; |
159 | } |
160 | } |
161 | |
162 | inline bool isSigned(TypedArrayType type) |
163 | { |
164 | switch (type) { |
165 | case TypeInt8: |
166 | case TypeInt16: |
167 | case TypeInt32: |
168 | case TypeFloat32: |
169 | case TypeFloat64: |
170 | return true; |
171 | default: |
172 | return false; |
173 | } |
174 | } |
175 | |
176 | inline bool isClamped(TypedArrayType type) |
177 | { |
178 | return type == TypeUint8Clamped; |
179 | } |
180 | |
181 | } // namespace JSC |
182 | |
183 | namespace WTF { |
184 | |
185 | void printInternal(PrintStream&, JSC::TypedArrayType); |
186 | |
187 | } // namespace WTF |
188 | |