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_STRING_TABLE_H_ | |
rossberg
2014/06/11 14:58:12
How about renaming this module to AstValue, since
marja
2014/06/12 09:57:53
Done (as discussed, renamed AstStringTable to AstV
| |
29 #define V8_AST_STRING_TABLE_H_ | |
30 | |
31 #include "api.h" | |
32 #include "hashmap.h" | |
33 #include "utils.h" | |
34 | |
35 // AstString, AstValue and AstStringTable 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 // AstStringTable. After parsing, the strings and values are internalized (moved | |
39 // 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. | |
rossberg
2014/06/11 14:58:12
Nit: null-terminated, since it's not NULL the poin
marja
2014/06/12 09:57:53
Done.
| |
57 const unsigned char* raw_data() const { return literal_bytes_.start(); } | |
58 int length() const { return literal_bytes_.length(); } | |
59 bool is_one_byte() const { return is_one_byte_; } | |
60 bool IsOneByteEqualTo(const char* data) const; | |
61 | |
62 // Puts the string into the V8 heap. | |
63 void Internalize(Isolate* isolate); | |
64 | |
65 // This function can be called after internalizing. | |
66 V8_INLINE Handle<String> string() const { | |
67 ASSERT(!string_.is_null()); | |
68 return string_; | |
69 } | |
70 | |
71 // For storing AstStrings in a hash map. | |
72 int hash() const { return hash_; } | |
73 static bool Compare(void* a, void* b); | |
74 | |
75 private: | |
76 friend class AstStringTable; | |
77 | |
78 bool is_one_byte_; | |
79 // Weak. Points to memory owned by AstStringTable. | |
80 Vector<const byte> literal_bytes_; | |
81 int hash_; | |
82 | |
83 // This is null until the string is internalized. | |
84 Handle<String> string_; | |
85 }; | |
86 | |
87 | |
88 // AstValue is either a string, a number, a string array, a boolean, or a | |
89 // special value (null, undefined, the hole). | |
90 class AstValue : public ZoneObject { | |
91 public: | |
92 enum Type { | |
rossberg
2014/06/11 14:58:12
Can we add the missing New* constructors below and
marja
2014/06/12 09:57:53
Done.
| |
93 STRING, | |
94 NUMBER, | |
95 SMI, | |
96 BOOLEAN, | |
97 STRING_ARRAY, | |
98 NULL_TYPE, | |
99 UNDEFINED, | |
100 THE_HOLE | |
101 }; | |
102 | |
103 bool IsString() const { | |
104 return type_ == STRING; | |
105 } | |
106 | |
107 bool IsNumber() const { | |
108 return type_ == NUMBER || type_ == SMI; | |
109 } | |
110 | |
111 const AstString* AsString() const { | |
112 if (type_ == STRING) | |
113 return string_; | |
114 UNREACHABLE(); | |
115 return 0; | |
116 } | |
117 | |
118 double AsNumber() const { | |
119 if (type_ == NUMBER) | |
120 return number_; | |
121 if (type_ == SMI) | |
122 return smi_; | |
123 UNREACHABLE(); | |
124 return 0; | |
125 } | |
126 | |
127 bool EqualsString(const AstString* string) const { | |
128 return type_ == STRING && string_ == string; | |
129 } | |
130 | |
131 bool IsPropertyName() const; | |
132 | |
133 bool BooleanValue() const; | |
134 | |
135 void Internalize(Isolate* isolate); | |
136 | |
137 // Can be called after Internalize has been called. | |
138 V8_INLINE Handle<Object> value() const { | |
139 if (type_ == STRING) { | |
140 return string_->string(); | |
141 } | |
142 ASSERT(!value_.is_null()); | |
143 return value_; | |
144 } | |
145 | |
146 private: | |
147 friend class AstStringTable; | |
148 | |
149 explicit AstValue(const AstString* s) : | |
150 type_(STRING) { | |
151 string_ = s; | |
152 } | |
153 | |
154 explicit AstValue(double n) : | |
155 type_(NUMBER) { | |
156 number_ = n; | |
157 } | |
158 | |
159 AstValue(Type t, int i) : | |
160 type_(t) { | |
161 ASSERT(type_ == SMI); | |
162 smi_ = i; | |
163 } | |
164 | |
165 explicit AstValue(bool b) : | |
166 type_(BOOLEAN) { | |
167 bool_ = b; | |
168 } | |
169 | |
170 explicit AstValue(ZoneList<const AstString*>* s) : | |
171 type_(STRING_ARRAY) { | |
172 strings_ = s; | |
173 } | |
174 | |
175 explicit AstValue(Type t) : | |
rossberg
2014/06/11 14:58:12
...and get rid of this one.
marja
2014/06/12 09:57:53
No, I still need this one to be able to construct
| |
176 type_(t) {} | |
177 | |
178 Type type_; | |
179 | |
180 // Uninternalized value. | |
181 union { | |
182 const AstString* string_; | |
183 double number_; | |
184 int smi_; | |
185 bool bool_; | |
186 ZoneList<const AstString*>* strings_; | |
187 }; | |
188 | |
189 // Internalized value (empty before internalized). | |
190 Handle<Object> value_; | |
191 }; | |
192 | |
193 | |
194 // For generating string constants. | |
195 #define STRING_CONSTANTS(F) \ | |
196 F(anonymous_function, "(anonymous function)") \ | |
197 F(arguments, "arguments") \ | |
198 F(done, "done") \ | |
199 F(dot_for, ".for") \ | |
200 F(dot_generator, ".generator") \ | |
201 F(dot_generator_object, ".generator_object") \ | |
202 F(dot_iterator, ".iterator") \ | |
203 F(dot_module, ".module") \ | |
204 F(dot_result, ".result") \ | |
205 F(empty, "") \ | |
206 F(eval, "eval") \ | |
207 F(initialize_const_global, "initializeConstGlobal") \ | |
208 F(initialize_var_global, "initializeVarGlobal") \ | |
209 F(make_reference_error, "MakeReferenceError") \ | |
210 F(make_syntax_error, "MakeSyntaxError") \ | |
211 F(make_type_error, "MakeTypeError") \ | |
212 F(module, "module") \ | |
213 F(native, "native") \ | |
214 F(next, "next") \ | |
215 F(proto, "__proto__") \ | |
216 F(prototype, "prototype") \ | |
217 F(this, "this") \ | |
218 F(use_strict, "use strict") \ | |
219 F(value, "value") | |
220 | |
221 class AstStringTable { | |
222 public: | |
223 explicit AstStringTable(Zone* zone) | |
224 : literal_chars_(0), | |
225 string_table_keys_(0), | |
226 string_table_(AstString::Compare), | |
227 zone_(zone), | |
228 isolate_(NULL) { | |
229 #define F(name, str) { \ | |
230 const char* data = str; \ | |
231 name##_string_ = GetOneByteString( \ | |
232 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \ | |
233 strlen(data))); \ | |
234 } | |
235 STRING_CONSTANTS(F) | |
236 #undef F | |
237 } | |
238 | |
239 const AstString* GetOneByteString(Vector<const uint8_t> literal); | |
240 const AstString* GetTwoByteString(Vector<const uint16_t> literal); | |
241 const AstString* GetString(Handle<String> literal); | |
242 | |
243 void Internalize(Isolate* isolate); | |
244 | |
245 #define F(name, str) \ | |
246 const AstString* name##_string() const { return name##_string_; } | |
247 STRING_CONSTANTS(F) | |
248 #undef F | |
249 | |
250 const AstValue* NewValue(const AstString* string); | |
rossberg
2014/06/11 14:58:12
It's weird that these functions are all members of
marja
2014/06/12 09:57:53
Done.
| |
251 const AstValue* NewNumberValue(double number); | |
252 const AstValue* NewSmiValue(int number); | |
253 const AstValue* NewValue(bool b); | |
254 const AstValue* NewValue(ZoneList<const AstString*>* strings); | |
255 const AstValue* NewValue(AstValue::Type t); | |
256 | |
257 private: | |
258 const AstString* GetString(int hash, bool is_one_byte, | |
259 Vector<const byte> literal_bytes); | |
260 | |
261 // All strings are copied here, one after another (no NULLs inbetween). | |
262 Collector<byte> literal_chars_; | |
263 // List of all AstStrings we have created; keys of string_table_ are pointers | |
264 // into AstStrings in string_table_keys_. | |
265 Collector<AstString> string_table_keys_; | |
266 HashMap string_table_; | |
267 // For keeping track of all AstValues we've created (so that they can be | |
268 // internalized later). | |
269 List<AstValue*> values_; | |
270 Zone* zone_; | |
271 Isolate* isolate_; | |
272 | |
273 #define F(name, str) \ | |
274 const AstString* name##_string_; | |
275 STRING_CONSTANTS(F) | |
276 #undef F | |
277 }; | |
278 | |
279 } } // namespace v8::internal | |
280 | |
281 #undef STRING_CONSTANTS | |
282 | |
283 #endif // V8_AST_STRING_TABLE_H_ | |
OLD | NEW |