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 { | |
44 public: | |
45 AstString(bool i, Vector<const byte> lb, int h) | |
46 : is_one_byte_(i), | |
47 literal_bytes_(lb), | |
48 hash_(h) {} | |
49 | |
50 AstString() | |
51 : is_one_byte_(true), | |
52 hash_(0) {} | |
53 | |
54 bool AsArrayIndex(uint32_t* index) const; | |
55 | |
56 // The string is not null-terminated, use length() to find out the length. | |
57 const unsigned char* raw_data() const { return literal_bytes_.start(); } | |
58 int length() const { | |
59 if (is_one_byte_) | |
60 return literal_bytes_.length(); | |
61 return literal_bytes_.length() / 2; | |
62 } | |
63 bool is_one_byte() const { return is_one_byte_; } | |
64 bool IsEmpty() const { return literal_bytes_.length() == 0; } | |
65 bool IsOneByteEqualTo(const char* data) const; | |
66 uint16_t FirstCharacter() const { | |
67 if (is_one_byte_) | |
68 return literal_bytes_[0]; | |
69 const uint16_t* c = | |
70 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); | |
71 return *c; | |
72 } | |
73 | |
74 // Puts the string into the V8 heap. | |
75 void Internalize(Isolate* isolate); | |
76 | |
77 // This function can be called after internalizing. | |
78 V8_INLINE Handle<String> string() const { | |
79 ASSERT(!string_.is_null()); | |
80 return string_; | |
81 } | |
82 | |
83 // For storing AstStrings in a hash map. | |
84 int hash() const { return hash_; } | |
85 static bool Compare(void* a, void* b); | |
86 | |
87 private: | |
88 friend class AstValueFactory; | |
89 | |
90 bool is_one_byte_; | |
91 // Weak. Points to memory owned by AstValueFactory. | |
92 Vector<const byte> literal_bytes_; | |
93 int hash_; | |
94 | |
95 // This is null until the string is internalized. | |
96 Handle<String> string_; | |
97 }; | |
98 | |
99 | |
100 // AstValue is either a string, a number, a string array, a boolean, or a | |
101 // special value (null, undefined, the hole). | |
102 class AstValue : public ZoneObject { | |
103 public: | |
104 bool IsString() const { | |
105 return type_ == STRING; | |
106 } | |
107 | |
108 bool IsNumber() const { | |
109 return type_ == NUMBER || type_ == SMI; | |
110 } | |
111 | |
112 const AstString* AsString() const { | |
113 if (type_ == STRING) | |
114 return string_; | |
115 UNREACHABLE(); | |
116 return 0; | |
117 } | |
118 | |
119 double AsNumber() const { | |
120 if (type_ == NUMBER) | |
121 return number_; | |
122 if (type_ == SMI) | |
123 return smi_; | |
124 UNREACHABLE(); | |
125 return 0; | |
126 } | |
127 | |
128 bool EqualsString(const AstString* string) const { | |
129 return type_ == STRING && string_ == string; | |
130 } | |
131 | |
132 bool IsPropertyName() const; | |
133 | |
134 bool BooleanValue() const; | |
135 | |
136 void Internalize(Isolate* isolate); | |
137 | |
138 // Can be called after Internalize has been called. | |
139 V8_INLINE Handle<Object> value() const { | |
140 if (type_ == STRING) { | |
141 return string_->string(); | |
142 } | |
143 ASSERT(!value_.is_null()); | |
144 return value_; | |
145 } | |
146 | |
147 private: | |
148 friend class AstValueFactory; | |
149 | |
150 enum Type { | |
151 STRING, | |
152 SYMBOL, | |
153 NUMBER, | |
154 SMI, | |
155 BOOLEAN, | |
156 STRING_ARRAY, | |
157 NULL_TYPE, | |
158 UNDEFINED, | |
159 THE_HOLE | |
160 }; | |
161 | |
162 explicit AstValue(const AstString* s) : type_(STRING) { string_ = s; } | |
163 | |
164 explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; } | |
165 | |
166 explicit AstValue(double n) : type_(NUMBER) { number_ = n; } | |
167 | |
168 AstValue(Type t, int i) : type_(t) { | |
169 ASSERT(type_ == SMI); | |
170 smi_ = i; | |
171 } | |
172 | |
173 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; } | |
174 | |
175 explicit AstValue(ZoneList<const AstString*>* s) : type_(STRING_ARRAY) { | |
176 strings_ = s; | |
177 } | |
178 | |
179 explicit AstValue(Type t) : type_(t) { | |
180 ASSERT(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE); | |
181 } | |
182 | |
183 Type type_; | |
184 | |
185 // Uninternalized value. | |
186 union { | |
187 const AstString* string_; | |
188 double number_; | |
189 int smi_; | |
190 bool bool_; | |
191 ZoneList<const AstString*>* strings_; | |
192 const char* symbol_name_; | |
193 }; | |
194 | |
195 // Internalized value (empty before internalized). | |
196 Handle<Object> value_; | |
197 }; | |
198 | |
199 | |
200 // For generating string constants. | |
201 #define STRING_CONSTANTS(F) \ | |
202 F(anonymous_function, "(anonymous function)") \ | |
203 F(arguments, "arguments") \ | |
204 F(done, "done") \ | |
205 F(dot_for, ".for") \ | |
206 F(dot_generator, ".generator") \ | |
207 F(dot_generator_object, ".generator_object") \ | |
208 F(dot_iterable, ".iterable") \ | |
209 F(dot_iterator, ".iterator") \ | |
210 F(dot_module, ".module") \ | |
211 F(dot_result, ".result") \ | |
212 F(empty, "") \ | |
213 F(eval, "eval") \ | |
214 F(initialize_const_global, "initializeConstGlobal") \ | |
215 F(initialize_var_global, "initializeVarGlobal") \ | |
216 F(make_reference_error, "MakeReferenceError") \ | |
217 F(make_syntax_error, "MakeSyntaxError") \ | |
218 F(make_type_error, "MakeTypeError") \ | |
219 F(module, "module") \ | |
220 F(native, "native") \ | |
221 F(next, "next") \ | |
222 F(proto, "__proto__") \ | |
223 F(prototype, "prototype") \ | |
224 F(this, "this") \ | |
225 F(use_strict, "use strict") \ | |
226 F(value, "value") | |
227 | |
228 class AstValueFactory { | |
229 public: | |
230 explicit AstValueFactory(Zone* zone) | |
231 : literal_chars_(0), | |
232 string_table_keys_(0), | |
233 string_table_(AstString::Compare), | |
234 zone_(zone), | |
235 isolate_(NULL) { | |
236 #define F(name, str) { \ | |
237 const char* data = str; \ | |
238 name##_string_ = GetOneByteString( \ | |
239 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \ | |
240 static_cast<int>(strlen(data)))); \ | |
241 } | |
242 STRING_CONSTANTS(F) | |
243 #undef F | |
244 } | |
245 | |
246 const AstString* GetOneByteString(Vector<const uint8_t> literal); | |
247 const AstString* GetTwoByteString(Vector<const uint16_t> literal); | |
248 const AstString* GetString(Handle<String> literal); | |
249 | |
250 void Internalize(Isolate* isolate); | |
251 bool IsInternalized() { | |
252 return isolate_ != NULL; | |
253 } | |
254 | |
255 #define F(name, str) \ | |
256 const AstString* name##_string() const { return name##_string_; } | |
257 STRING_CONSTANTS(F) | |
258 #undef F | |
259 | |
260 const AstValue* NewString(const AstString* string); | |
261 // A JavaScript symbol (ECMA-262 edition 6). | |
262 const AstValue* NewSymbol(const char* name); | |
263 const AstValue* NewNumber(double number); | |
264 const AstValue* NewSmi(int number); | |
265 const AstValue* NewBoolean(bool b); | |
266 const AstValue* NewStringList(ZoneList<const AstString*>* strings); | |
267 const AstValue* NewNull(); | |
268 const AstValue* NewUndefined(); | |
269 const AstValue* NewTheHole(); | |
270 | |
271 private: | |
272 const AstString* GetString(int hash, bool is_one_byte, | |
273 Vector<const byte> literal_bytes); | |
274 | |
275 // All strings are copied here, one after another (no NULLs inbetween). | |
276 Collector<byte> literal_chars_; | |
277 // List of all AstStrings we have created; keys of string_table_ are pointers | |
278 // into AstStrings in string_table_keys_. | |
279 Collector<AstString> string_table_keys_; | |
280 HashMap string_table_; | |
281 // For keeping track of all AstValues we've created (so that they can be | |
282 // internalized later). | |
283 List<AstValue*> values_; | |
284 Zone* zone_; | |
285 Isolate* isolate_; | |
286 | |
287 #define F(name, str) \ | |
288 const AstString* name##_string_; | |
289 STRING_CONSTANTS(F) | |
290 #undef F | |
291 }; | |
292 | |
293 } } // namespace v8::internal | |
294 | |
295 #undef STRING_CONSTANTS | |
296 | |
297 #endif // V8_AST_VALUE_FACTORY_H_ | |
OLD | NEW |