OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #ifndef V8_AST_VALUE_FACTORY_H_ | |
29 #define V8_AST_VALUE_FACTORY_H_ | |
30 | |
31 #include "src/api.h" | |
32 #include "src/hashmap.h" | |
33 #include "src/utils.h" | |
34 | |
35 // AstString, AstValue and AstValueFactory are for storing strings and values | |
36 // independent of the V8 heap and internalizing them later. During parsing, | |
37 // AstStrings and AstValues are created and stored outside the heap, in | |
38 // AstValueFactory. After parsing, the strings and values are internalized | |
39 // (moved into the V8 heap). | |
40 namespace v8 { | |
41 namespace internal { | |
42 | |
43 class AstString : public ZoneObject { | |
44 public: | |
45 AstString(bool is_one_byte, Vector<const byte> literal_bytes, uint32_t hash) | |
46 : type_(NORMAL), | |
47 is_one_byte_(is_one_byte), | |
48 literal_bytes_(literal_bytes), | |
49 hash_(hash), | |
50 left_(NULL), | |
51 right_(NULL) {} | |
52 | |
53 AstString() | |
54 : type_(NORMAL), | |
55 is_one_byte_(true), | |
56 hash_(0), | |
57 left_(NULL), | |
58 right_(NULL) {} | |
59 | |
60 // Creates a cons string. The only allowed operations for cons strings are | |
rossberg
2014/06/23 09:44:27
Disallowing a whole set of operations in some case
marja
2014/06/23 11:46:45
Done.
I named the classes AstStringBase, AstStrin
rossberg
2014/06/23 13:30:53
Hm, from a stylistic point, I don't like naming an
| |
61 // is_one_byte(), length(), Internalize() and string(). | |
62 AstString(const AstString* left, const AstString* right) | |
63 : type_(CONS), | |
64 is_one_byte_(left->is_one_byte_ && right->is_one_byte_), | |
65 hash_(0), | |
66 left_(left), | |
67 right_(right) {} | |
68 | |
69 bool AsArrayIndex(uint32_t* index) const; | |
70 | |
71 // The string is not null-terminated, use length() to find out the length. | |
72 const unsigned char* raw_data() const { | |
73 ASSERT(type_ == NORMAL); | |
74 return literal_bytes_.start(); | |
75 } | |
76 int length() const { | |
77 if (type_ == CONS) { | |
78 return left_->length() + right_->length(); | |
79 } | |
80 if (is_one_byte_) | |
81 return literal_bytes_.length(); | |
82 return literal_bytes_.length() / 2; | |
83 } | |
84 bool is_one_byte() const { return is_one_byte_; } | |
85 bool IsEmpty() const { return length() == 0; } | |
86 bool IsOneByteEqualTo(const char* data) const; | |
87 uint16_t FirstCharacter() const { | |
88 ASSERT(type_ == NORMAL); | |
89 if (is_one_byte_) | |
90 return literal_bytes_[0]; | |
91 const uint16_t* c = | |
92 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); | |
93 return *c; | |
94 } | |
95 | |
96 // Puts the string into the V8 heap. | |
97 void Internalize(Isolate* isolate); | |
98 | |
99 // This function can be called after internalizing. | |
100 V8_INLINE Handle<String> string() const { | |
101 ASSERT(!string_.is_null()); | |
102 return string_; | |
103 } | |
104 | |
105 // For storing AstStrings in a hash map. | |
106 uint32_t hash() const { | |
107 ASSERT(type_ == NORMAL); | |
108 return hash_; | |
109 } | |
110 static bool Compare(void* a, void* b); | |
111 | |
112 private: | |
113 friend class AstValueFactory; | |
114 friend class AstStringInternalizationKey; | |
115 | |
116 enum Type { | |
117 NORMAL, | |
118 CONS | |
119 }; | |
120 | |
121 Type type_; | |
122 bool is_one_byte_; | |
123 | |
124 // For normal strings. Points to memory owned by Zone. | |
125 Vector<const byte> literal_bytes_; | |
126 uint32_t hash_; | |
127 | |
128 // For cons strings. | |
129 const AstString* left_; | |
130 const AstString* right_; | |
131 | |
132 // This is null until the string is internalized. | |
133 Handle<String> string_; | |
134 }; | |
135 | |
136 | |
137 // AstValue is either a string, a number, a string array, a boolean, or a | |
138 // special value (null, undefined, the hole). | |
139 class AstValue : public ZoneObject { | |
140 public: | |
141 bool IsString() const { | |
142 return type_ == STRING; | |
143 } | |
144 | |
145 bool IsNumber() const { | |
146 return type_ == NUMBER || type_ == SMI; | |
147 } | |
148 | |
149 const AstString* AsString() const { | |
150 if (type_ == STRING) | |
151 return string_; | |
152 UNREACHABLE(); | |
153 return 0; | |
154 } | |
155 | |
156 double AsNumber() const { | |
157 if (type_ == NUMBER) | |
158 return number_; | |
159 if (type_ == SMI) | |
160 return smi_; | |
161 UNREACHABLE(); | |
162 return 0; | |
163 } | |
164 | |
165 bool EqualsString(const AstString* string) const { | |
166 return type_ == STRING && string_ == string; | |
167 } | |
168 | |
169 bool IsPropertyName() const; | |
170 | |
171 bool BooleanValue() const; | |
172 | |
173 void Internalize(Isolate* isolate); | |
174 | |
175 // Can be called after Internalize has been called. | |
176 V8_INLINE Handle<Object> value() const { | |
177 if (type_ == STRING) { | |
178 return string_->string(); | |
179 } | |
180 ASSERT(!value_.is_null()); | |
181 return value_; | |
182 } | |
183 | |
184 private: | |
185 friend class AstValueFactory; | |
186 | |
187 enum Type { | |
188 STRING, | |
189 SYMBOL, | |
190 NUMBER, | |
191 SMI, | |
192 BOOLEAN, | |
193 STRING_ARRAY, | |
194 NULL_TYPE, | |
195 UNDEFINED, | |
196 THE_HOLE | |
197 }; | |
198 | |
199 explicit AstValue(const AstString* s) : type_(STRING) { string_ = s; } | |
200 | |
201 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; } | |
202 | |
203 explicit AstValue(double n) : type_(NUMBER) { number_ = n; } | |
204 | |
205 AstValue(Type t, int i) : type_(t) { | |
206 ASSERT(type_ == SMI); | |
207 smi_ = i; | |
208 } | |
209 | |
210 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; } | |
211 | |
212 explicit AstValue(ZoneList<const AstString*>* s) : type_(STRING_ARRAY) { | |
213 strings_ = s; | |
214 } | |
215 | |
216 explicit AstValue(Type t) : type_(t) { | |
217 ASSERT(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); | |
218 } | |
219 | |
220 Type type_; | |
221 | |
222 // Uninternalized value. | |
223 union { | |
224 const AstString* string_; | |
225 double number_; | |
226 int smi_; | |
227 bool bool_; | |
228 ZoneList<const AstString*>* strings_; | |
229 const char* symbol_name_; | |
230 }; | |
231 | |
232 // Internalized value (empty before internalized). | |
233 Handle<Object> value_; | |
234 }; | |
235 | |
236 | |
237 // For generating string constants. | |
238 #define STRING_CONSTANTS(F) \ | |
239 F(anonymous_function, "(anonymous function)") \ | |
240 F(arguments, "arguments") \ | |
241 F(done, "done") \ | |
242 F(dot, ".") \ | |
243 F(dot_for, ".for") \ | |
244 F(dot_generator, ".generator") \ | |
245 F(dot_generator_object, ".generator_object") \ | |
246 F(dot_iterable, ".iterable") \ | |
247 F(dot_iterator, ".iterator") \ | |
248 F(dot_module, ".module") \ | |
249 F(dot_result, ".result") \ | |
250 F(empty, "") \ | |
251 F(eval, "eval") \ | |
252 F(initialize_const_global, "initializeConstGlobal") \ | |
253 F(initialize_var_global, "initializeVarGlobal") \ | |
254 F(make_reference_error, "MakeReferenceError") \ | |
255 F(make_syntax_error, "MakeSyntaxError") \ | |
256 F(make_type_error, "MakeTypeError") \ | |
257 F(module, "module") \ | |
258 F(native, "native") \ | |
259 F(next, "next") \ | |
260 F(proto, "__proto__") \ | |
261 F(prototype, "prototype") \ | |
262 F(this, "this") \ | |
263 F(use_strict, "use strict") \ | |
264 F(value, "value") | |
265 | |
266 class AstValueFactory { | |
267 public: | |
268 AstValueFactory(Zone* zone, uint32_t hash_seed) | |
269 : string_table_(AstString::Compare), | |
270 zone_(zone), | |
271 isolate_(NULL), | |
272 hash_seed_(hash_seed) { | |
273 #define F(name, str) \ | |
274 name##_string_ = NULL; | |
275 STRING_CONSTANTS(F) | |
276 #undef F | |
277 } | |
278 | |
279 const AstString* GetOneByteString(const Vector<const uint8_t>& literal); | |
280 const AstString* GetTwoByteString(const Vector<const uint16_t>& literal); | |
281 const AstString* GetString(Handle<String> literal); | |
282 const AstString* NewConsString(const AstString* left, const AstString* right); | |
283 | |
284 void Internalize(Isolate* isolate); | |
285 bool IsInternalized() { | |
286 return isolate_ != NULL; | |
287 } | |
288 | |
289 #define F(name, str) \ | |
290 const AstString* name##_string() { \ | |
291 if (name##_string_ == NULL) { \ | |
292 const char* data = str; \ | |
293 name##_string_ = GetOneByteString( \ | |
294 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \ | |
295 static_cast<int>(strlen(data)))); \ | |
296 } \ | |
297 return name##_string_; \ | |
298 } | |
299 STRING_CONSTANTS(F) | |
300 #undef F | |
301 | |
302 const AstValue* NewString(const AstString* string); | |
303 // A JavaScript symbol (ECMA-262 edition 6). | |
304 const AstValue* NewSymbol(const char* name); | |
305 const AstValue* NewNumber(double number); | |
306 const AstValue* NewSmi(int number); | |
307 const AstValue* NewBoolean(bool b); | |
308 const AstValue* NewStringList(ZoneList<const AstString*>* strings); | |
309 const AstValue* NewNull(); | |
310 const AstValue* NewUndefined(); | |
311 const AstValue* NewTheHole(); | |
312 | |
313 private: | |
314 const AstString* GetString(uint32_t hash, bool is_one_byte, | |
315 const Vector<const byte>& literal_bytes); | |
316 | |
317 // All strings are copied here, one after another (no NULLs inbetween). | |
318 HashMap string_table_; | |
319 // For keeping track of all AstValues and AstStrings we've created (so that | |
320 // they can be internalized later). | |
321 List<AstValue*> values_; | |
322 List<AstString*> strings_; | |
323 Zone* zone_; | |
324 Isolate* isolate_; | |
325 | |
326 uint32_t hash_seed_; | |
327 | |
328 #define F(name, str) \ | |
329 const AstString* name##_string_; | |
330 STRING_CONSTANTS(F) | |
331 #undef F | |
332 }; | |
333 | |
334 } } // namespace v8::internal | |
335 | |
336 #undef STRING_CONSTANTS | |
337 | |
338 #endif // V8_AST_VALUE_FACTORY_H_ | |
OLD | NEW |