Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Side by Side Diff: src/ast-value-factory.cc

Issue 345513003: Revert "Parser: Delay internalizing strings and values." (r21841) (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ast-value-factory.h ('k') | src/compiler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "src/ast-value-factory.h"
29
30 #include "src/api.h"
31 #include "src/objects.h"
32
33 namespace v8 {
34 namespace internal {
35
36 namespace {
37
38 template <typename Char>
39 int vector_hash(Vector<const Char> string) {
40 int hash = 0;
41 for (int i = 0; i < string.length(); i++) {
42 int c = static_cast<int>(string[i]);
43 hash += c;
44 hash += (hash << 10);
45 hash ^= (hash >> 6);
46 }
47 return hash;
48 }
49
50
51 // For using StringToArrayIndex.
52 class OneByteStringStream {
53 public:
54 explicit OneByteStringStream(Vector<const byte> lb) :
55 literal_bytes_(lb), pos_(0) {}
56
57 bool HasMore() { return pos_ < literal_bytes_.length(); }
58 uint16_t GetNext() { return literal_bytes_[pos_++]; }
59
60 private:
61 Vector<const byte> literal_bytes_;
62 int pos_;
63 };
64
65 }
66
67
68 bool AstString::AsArrayIndex(uint32_t* index) const {
69 if (!string_.is_null())
70 return string_->AsArrayIndex(index);
71 if (!is_one_byte_ || literal_bytes_.length() == 0 ||
72 literal_bytes_.length() > String::kMaxArrayIndexSize)
73 return false;
74 OneByteStringStream stream(literal_bytes_);
75 return StringToArrayIndex(&stream, index);
76 }
77
78
79 bool AstString::IsOneByteEqualTo(const char* data) const {
80 int length = static_cast<int>(strlen(data));
81 if (is_one_byte_ && literal_bytes_.length() == length) {
82 const char* token = reinterpret_cast<const char*>(literal_bytes_.start());
83 return !strncmp(token, data, length);
84 }
85 return false;
86 }
87
88
89 void AstString::Internalize(Isolate* isolate) {
90 if (!string_.is_null()) return;
91 if (literal_bytes_.length() == 0) {
92 string_ = isolate->factory()->empty_string();
93 } else if (is_one_byte_) {
94 string_ = isolate->factory()->InternalizeOneByteString(literal_bytes_);
95 } else {
96 string_ = isolate->factory()->InternalizeTwoByteString(
97 Vector<const uint16_t>::cast(literal_bytes_));
98 }
99 }
100
101
102 bool AstString::Compare(void* a, void* b) {
103 AstString* string1 = reinterpret_cast<AstString*>(a);
104 AstString* string2 = reinterpret_cast<AstString*>(b);
105 if (string1->is_one_byte_ != string2->is_one_byte_) return false;
106 if (string1->hash_ != string2->hash_) return false;
107 int length = string1->literal_bytes_.length();
108 if (string2->literal_bytes_.length() != length) return false;
109 return memcmp(string1->literal_bytes_.start(),
110 string2->literal_bytes_.start(), length) == 0;
111 }
112
113
114 bool AstValue::IsPropertyName() const {
115 if (type_ == STRING) {
116 uint32_t index;
117 return !string_->AsArrayIndex(&index);
118 }
119 return false;
120 }
121
122
123 bool AstValue::BooleanValue() const {
124 switch (type_) {
125 case STRING:
126 ASSERT(string_ != NULL);
127 return !string_->IsEmpty();
128 case SYMBOL:
129 UNREACHABLE();
130 break;
131 case NUMBER:
132 return DoubleToBoolean(number_);
133 case SMI:
134 return smi_ != 0;
135 case STRING_ARRAY:
136 UNREACHABLE();
137 break;
138 case BOOLEAN:
139 return bool_;
140 case NULL_TYPE:
141 return false;
142 case THE_HOLE:
143 UNREACHABLE();
144 break;
145 case UNDEFINED:
146 return false;
147 }
148 UNREACHABLE();
149 return false;
150 }
151
152
153 void AstValue::Internalize(Isolate* isolate) {
154 switch (type_) {
155 case STRING:
156 ASSERT(string_ != NULL);
157 // Strings are already internalized.
158 ASSERT(!string_->string().is_null());
159 break;
160 case SYMBOL:
161 value_ = Object::GetProperty(
162 isolate, handle(isolate->native_context()->builtins()),
163 symbol_name_).ToHandleChecked();
164 break;
165 case NUMBER:
166 value_ = isolate->factory()->NewNumber(number_, TENURED);
167 break;
168 case SMI:
169 value_ = handle(Smi::FromInt(smi_), isolate);
170 break;
171 case BOOLEAN:
172 if (bool_) {
173 value_ = isolate->factory()->true_value();
174 } else {
175 value_ = isolate->factory()->false_value();
176 }
177 break;
178 case STRING_ARRAY: {
179 ASSERT(strings_ != NULL);
180 Factory* factory = isolate->factory();
181 int len = strings_->length();
182 Handle<FixedArray> elements = factory->NewFixedArray(len, TENURED);
183 for (int i = 0; i < len; i++) {
184 const AstString* string = (*strings_)[i];
185 Handle<Object> element = string->string();
186 // Strings are already internalized.
187 ASSERT(!element.is_null());
188 elements->set(i, *element);
189 }
190 value_ =
191 factory->NewJSArrayWithElements(elements, FAST_ELEMENTS, TENURED);
192 break;
193 }
194 case NULL_TYPE:
195 value_ = isolate->factory()->null_value();
196 break;
197 case THE_HOLE:
198 value_ = isolate->factory()->the_hole_value();
199 break;
200 case UNDEFINED:
201 value_ = isolate->factory()->undefined_value();
202 break;
203 }
204 }
205
206
207 const AstString* AstValueFactory::GetOneByteString(
208 Vector<const uint8_t> literal) {
209 return GetString(vector_hash(literal), true, literal);
210 }
211
212
213 const AstString* AstValueFactory::GetTwoByteString(
214 Vector<const uint16_t> literal) {
215 return GetString(vector_hash(literal), false,
216 Vector<const byte>::cast(literal));
217 }
218
219
220 const AstString* AstValueFactory::GetString(Handle<String> literal) {
221 DisallowHeapAllocation no_gc;
222 String::FlatContent content = literal->GetFlatContent();
223 if (content.IsAscii()) {
224 return GetOneByteString(content.ToOneByteVector());
225 }
226 ASSERT(content.IsTwoByte());
227 return GetTwoByteString(content.ToUC16Vector());
228 }
229
230
231 void AstValueFactory::Internalize(Isolate* isolate) {
232 if (isolate_) {
233 // Everything is already internalized.
234 return;
235 }
236 // Strings need to be internalized before values, because values refer to
237 // strings.
238 for (HashMap::Entry* p = string_table_.Start(); p != NULL;
239 p = string_table_.Next(p)) {
240 AstString* string = reinterpret_cast<AstString*>(p->key);
241 string->Internalize(isolate);
242 }
243 for (int i = 0; i < values_.length(); ++i) {
244 values_[i]->Internalize(isolate);
245 }
246 isolate_ = isolate;
247 }
248
249
250 const AstValue* AstValueFactory::NewString(const AstString* string) {
251 AstValue* value = new (zone_) AstValue(string);
252 ASSERT(string != NULL);
253 if (isolate_) {
254 value->Internalize(isolate_);
255 }
256 values_.Add(value);
257 return value;
258 }
259
260
261 const AstValue* AstValueFactory::NewSymbol(const char* name) {
262 AstValue* value = new (zone_) AstValue(name);
263 if (isolate_) {
264 value->Internalize(isolate_);
265 }
266 values_.Add(value);
267 return value;
268 }
269
270
271 const AstValue* AstValueFactory::NewNumber(double number) {
272 AstValue* value = new (zone_) AstValue(number);
273 if (isolate_) {
274 value->Internalize(isolate_);
275 }
276 values_.Add(value);
277 return value;
278 }
279
280
281 const AstValue* AstValueFactory::NewSmi(int number) {
282 AstValue* value =
283 new (zone_) AstValue(AstValue::SMI, number);
284 if (isolate_) {
285 value->Internalize(isolate_);
286 }
287 values_.Add(value);
288 return value;
289 }
290
291
292 const AstValue* AstValueFactory::NewBoolean(bool b) {
293 AstValue* value = new (zone_) AstValue(b);
294 if (isolate_) {
295 value->Internalize(isolate_);
296 }
297 values_.Add(value);
298 return value;
299 }
300
301
302 const AstValue* AstValueFactory::NewStringList(
303 ZoneList<const AstString*>* strings) {
304 AstValue* value = new (zone_) AstValue(strings);
305 if (isolate_) {
306 value->Internalize(isolate_);
307 }
308 values_.Add(value);
309 return value;
310 }
311
312
313 const AstValue* AstValueFactory::NewNull() {
314 AstValue* value = new (zone_) AstValue(AstValue::NULL_TYPE);
315 if (isolate_) {
316 value->Internalize(isolate_);
317 }
318 values_.Add(value);
319 return value;
320 }
321
322
323 const AstValue* AstValueFactory::NewUndefined() {
324 AstValue* value = new (zone_) AstValue(AstValue::UNDEFINED);
325 if (isolate_) {
326 value->Internalize(isolate_);
327 }
328 values_.Add(value);
329 return value;
330 }
331
332
333 const AstValue* AstValueFactory::NewTheHole() {
334 AstValue* value = new (zone_) AstValue(AstValue::THE_HOLE);
335 if (isolate_) {
336 value->Internalize(isolate_);
337 }
338 values_.Add(value);
339 return value;
340 }
341
342
343 const AstString* AstValueFactory::GetString(int hash, bool is_one_byte,
344 Vector<const byte> literal_bytes) {
345 // literal_bytes here points to whatever the user passed, and this is OK
346 // because we use vector_compare (which checks the contents) to compare
347 // against the AstStrings which are in the string_table_. We should not return
348 // this AstString.
349 AstString key(is_one_byte, literal_bytes, hash);
350 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true);
351 if (entry->value == NULL) {
352 // Copy literal contents for later comparison.
353 key.literal_bytes_ =
354 Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes));
355 // This Vector will be valid as long as the Collector is alive (meaning that
356 // the AstString will not be moved).
357 Vector<AstString> new_string = string_table_keys_.AddBlock(1, key);
358 entry->key = &new_string[0];
359 if (isolate_) {
360 new_string[0].Internalize(isolate_);
361 }
362 entry->value = reinterpret_cast<void*>(1);
363 }
364 return reinterpret_cast<AstString*>(entry->key);
365 }
366
367
368 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ast-value-factory.h ('k') | src/compiler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698