OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 #include "char-predicates-inl.h" | 33 #include "char-predicates-inl.h" |
34 #include "v8conversions.h" | 34 #include "v8conversions.h" |
35 #include "messages.h" | 35 #include "messages.h" |
36 #include "spaces-inl.h" | 36 #include "spaces-inl.h" |
37 #include "token.h" | 37 #include "token.h" |
38 | 38 |
39 namespace v8 { | 39 namespace v8 { |
40 namespace internal { | 40 namespace internal { |
41 | 41 |
42 // A simple JSON parser. | 42 // A simple json parser. |
43 template <typename StringType> | 43 template <bool seq_ascii> |
44 class JsonParser BASE_EMBEDDED { | 44 class JsonParser BASE_EMBEDDED { |
45 public: | 45 public: |
46 static Handle<Object> Parse(Handle<String> source) { | 46 static Handle<Object> Parse(Handle<String> source) { |
47 return JsonParser(Handle<StringType>::cast(source)).ParseJson(); | 47 return JsonParser().ParseJson(source); |
48 } | 48 } |
49 | 49 |
50 static const int kEndOfString = -1; | 50 static const int kEndOfString = -1; |
51 | 51 |
52 private: | 52 private: |
53 typedef typename StringType::CharType SourceChar; | 53 // Parse a string containing a single JSON value. |
54 | 54 Handle<Object> ParseJson(Handle<String> source); |
55 explicit JsonParser(Handle<StringType> source) | |
56 : isolate_(source->GetHeap()->isolate()), | |
57 source_(source), | |
58 characters_(NULL), | |
59 source_length_(source->length()), | |
60 position_(-1) { | |
61 InitializeSource(); | |
62 } | |
63 | |
64 | |
65 // Parse the source string as containing a single JSON value. | |
66 Handle<Object> ParseJson(); | |
67 | |
68 // Set up the object so GetChar works, in case it needs more than just | |
69 // the constructor. | |
70 void InitializeSource(); | |
71 | |
72 inline uc32 GetChar(int position); | |
73 inline const SourceChar* GetChars(); | |
74 | 55 |
75 inline void Advance() { | 56 inline void Advance() { |
76 position_++; | 57 position_++; |
77 if (position_ >= source_length_) { | 58 if (position_ >= source_length_) { |
78 c0_ = kEndOfString; | 59 c0_ = kEndOfString; |
| 60 } else if (seq_ascii) { |
| 61 c0_ = seq_source_->SeqAsciiStringGet(position_); |
79 } else { | 62 } else { |
80 c0_ = GetChar(position_); | 63 c0_ = source_->Get(position_); |
81 } | 64 } |
82 } | 65 } |
83 | 66 |
84 // The JSON lexical grammar is specified in the ECMAScript 5 standard, | 67 // The JSON lexical grammar is specified in the ECMAScript 5 standard, |
85 // section 15.12.1.1. The only allowed whitespace characters between tokens | 68 // section 15.12.1.1. The only allowed whitespace characters between tokens |
86 // are tab, carriage-return, newline and space. | 69 // are tab, carriage-return, newline and space. |
87 | 70 |
88 | |
89 static inline bool IsJsonWhitespace(uc32 ch) { | |
90 const char* whitespaces = "\x20\x09\x0a\0\0\x0d\0\0"; | |
91 return (static_cast<uc32>(whitespaces[ch & 0x07]) == ch); | |
92 } | |
93 | |
94 inline void AdvanceSkipWhitespace() { | 71 inline void AdvanceSkipWhitespace() { |
95 do { | 72 do { |
96 Advance(); | 73 Advance(); |
97 } while (IsJsonWhitespace(c0_)); | 74 } while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' '); |
98 } | 75 } |
99 | 76 |
100 inline void SkipWhitespace() { | 77 inline void SkipWhitespace() { |
101 while (IsJsonWhitespace(c0_)) { | 78 while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' ') { |
102 Advance(); | 79 Advance(); |
103 } | 80 } |
104 } | 81 } |
105 | 82 |
106 inline uc32 AdvanceGetChar() { | 83 inline uc32 AdvanceGetChar() { |
107 Advance(); | 84 Advance(); |
108 return c0_; | 85 return c0_; |
109 } | 86 } |
110 | 87 |
111 // Checks that current charater is c. | 88 // Checks that current charater is c. |
(...skipping 14 matching lines...) Expand all Loading... |
126 return ScanJsonString<false>(); | 103 return ScanJsonString<false>(); |
127 } | 104 } |
128 Handle<String> ParseJsonSymbol() { | 105 Handle<String> ParseJsonSymbol() { |
129 return ScanJsonString<true>(); | 106 return ScanJsonString<true>(); |
130 } | 107 } |
131 template <bool is_symbol> | 108 template <bool is_symbol> |
132 Handle<String> ScanJsonString(); | 109 Handle<String> ScanJsonString(); |
133 // Creates a new string and copies prefix[start..end] into the beginning | 110 // Creates a new string and copies prefix[start..end] into the beginning |
134 // of it. Then scans the rest of the string, adding characters after the | 111 // of it. Then scans the rest of the string, adding characters after the |
135 // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char. | 112 // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char. |
136 template <typename SinkStringType> | 113 template <typename StringType, typename SinkChar> |
137 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end); | 114 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end); |
138 | 115 |
139 // A JSON number (production JSONNumber) is a subset of the valid JavaScript | 116 // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
140 // decimal number literals. | 117 // decimal number literals. |
141 // It includes an optional minus sign, must have at least one | 118 // It includes an optional minus sign, must have at least one |
142 // digit before and after a decimal point, may not have prefixed zeros (unless | 119 // digit before and after a decimal point, may not have prefixed zeros (unless |
143 // the integer part is zero), and may include an exponent part (e.g., "e-10"). | 120 // the integer part is zero), and may include an exponent part (e.g., "e-10"). |
144 // Hexadecimal and octal numbers are not allowed. | 121 // Hexadecimal and octal numbers are not allowed. |
145 Handle<Object> ParseJsonNumber(); | 122 Handle<Object> ParseJsonNumber(); |
146 | 123 |
(...skipping 21 matching lines...) Expand all Loading... |
168 // Mark that a parsing error has happened at the current token, and | 145 // Mark that a parsing error has happened at the current token, and |
169 // return a null handle. Primarily for readability. | 146 // return a null handle. Primarily for readability. |
170 inline Handle<Object> ReportUnexpectedCharacter() { | 147 inline Handle<Object> ReportUnexpectedCharacter() { |
171 return Handle<Object>::null(); | 148 return Handle<Object>::null(); |
172 } | 149 } |
173 | 150 |
174 inline Isolate* isolate() { return isolate_; } | 151 inline Isolate* isolate() { return isolate_; } |
175 | 152 |
176 static const int kInitialSpecialStringLength = 1024; | 153 static const int kInitialSpecialStringLength = 1024; |
177 | 154 |
| 155 |
| 156 private: |
| 157 Handle<String> source_; |
| 158 int source_length_; |
| 159 Handle<SeqAsciiString> seq_source_; |
| 160 |
178 Isolate* isolate_; | 161 Isolate* isolate_; |
179 Handle<StringType> source_; | 162 uc32 c0_; |
180 // Used for external strings, to avoid going through the resource on | |
181 // every access. | |
182 const SourceChar* characters_; | |
183 int source_length_; | |
184 int position_; | 163 int position_; |
185 uc32 c0_; | |
186 }; | 164 }; |
187 | 165 |
188 template <typename StringType> | 166 template <bool seq_ascii> |
189 Handle<Object> JsonParser<StringType>::ParseJson() { | 167 Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source) { |
190 // Initial position is right before the string. | 168 isolate_ = source->map()->GetHeap()->isolate(); |
191 ASSERT(position_ == -1); | 169 FlattenString(source); |
| 170 source_ = source; |
| 171 source_length_ = source_->length(); |
| 172 |
| 173 // Optimized fast case where we only have ASCII characters. |
| 174 if (seq_ascii) { |
| 175 seq_source_ = Handle<SeqAsciiString>::cast(source_); |
| 176 } |
| 177 |
| 178 // Set initial position right before the string. |
| 179 position_ = -1; |
192 // Advance to the first character (posibly EOS) | 180 // Advance to the first character (posibly EOS) |
193 AdvanceSkipWhitespace(); | 181 AdvanceSkipWhitespace(); |
194 // ParseJsonValue also consumes following whitespace. | |
195 Handle<Object> result = ParseJsonValue(); | 182 Handle<Object> result = ParseJsonValue(); |
196 if (result.is_null() || c0_ != kEndOfString) { | 183 if (result.is_null() || c0_ != kEndOfString) { |
197 // Parse failed. Current character is the unexpected token. | 184 // Parse failed. Current character is the unexpected token. |
| 185 |
198 const char* message; | 186 const char* message; |
199 Factory* factory = isolate()->factory(); | 187 Factory* factory = isolate()->factory(); |
200 Handle<JSArray> array; | 188 Handle<JSArray> array; |
201 | 189 |
202 switch (c0_) { | 190 switch (c0_) { |
203 case kEndOfString: | 191 case kEndOfString: |
204 message = "unexpected_eos"; | 192 message = "unexpected_eos"; |
205 array = factory->NewJSArray(0); | 193 array = factory->NewJSArray(0); |
206 break; | 194 break; |
207 case '-': | 195 case '-': |
(...skipping 16 matching lines...) Expand all Loading... |
224 break; | 212 break; |
225 default: | 213 default: |
226 message = "unexpected_token"; | 214 message = "unexpected_token"; |
227 Handle<Object> name = LookupSingleCharacterStringFromCode(c0_); | 215 Handle<Object> name = LookupSingleCharacterStringFromCode(c0_); |
228 Handle<FixedArray> element = factory->NewFixedArray(1); | 216 Handle<FixedArray> element = factory->NewFixedArray(1); |
229 element->set(0, *name); | 217 element->set(0, *name); |
230 array = factory->NewJSArrayWithElements(element); | 218 array = factory->NewJSArrayWithElements(element); |
231 break; | 219 break; |
232 } | 220 } |
233 | 221 |
234 MessageLocation location(factory->NewScript(source_), | 222 MessageLocation location(factory->NewScript(source), |
235 position_, | 223 position_, |
236 position_ + 1); | 224 position_ + 1); |
237 Handle<Object> result = factory->NewSyntaxError(message, array); | 225 Handle<Object> result = factory->NewSyntaxError(message, array); |
238 isolate()->Throw(*result, &location); | 226 isolate()->Throw(*result, &location); |
239 return Handle<Object>::null(); | 227 return Handle<Object>::null(); |
240 } | 228 } |
241 return result; | 229 return result; |
242 } | 230 } |
243 | 231 |
244 | 232 |
245 // Parse any JSON value. | 233 // Parse any JSON value. |
246 template <typename StringType> | 234 template <bool seq_ascii> |
247 Handle<Object> JsonParser<StringType>::ParseJsonValue() { | 235 Handle<Object> JsonParser<seq_ascii>::ParseJsonValue() { |
248 switch (c0_) { | 236 switch (c0_) { |
249 case '"': | 237 case '"': |
250 return ParseJsonString(); | 238 return ParseJsonString(); |
251 case '-': | 239 case '-': |
252 case '0': | 240 case '0': |
253 case '1': | 241 case '1': |
254 case '2': | 242 case '2': |
255 case '3': | 243 case '3': |
256 case '4': | 244 case '4': |
257 case '5': | 245 case '5': |
(...skipping 30 matching lines...) Expand all Loading... |
288 return ParseJsonObject(); | 276 return ParseJsonObject(); |
289 case '[': | 277 case '[': |
290 return ParseJsonArray(); | 278 return ParseJsonArray(); |
291 default: | 279 default: |
292 return ReportUnexpectedCharacter(); | 280 return ReportUnexpectedCharacter(); |
293 } | 281 } |
294 } | 282 } |
295 | 283 |
296 | 284 |
297 // Parse a JSON object. Position must be right at '{'. | 285 // Parse a JSON object. Position must be right at '{'. |
298 template <typename StringType> | 286 template <bool seq_ascii> |
299 Handle<Object> JsonParser<StringType>::ParseJsonObject() { | 287 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { |
300 Handle<JSFunction> object_constructor( | 288 Handle<JSFunction> object_constructor( |
301 isolate()->global_context()->object_function()); | 289 isolate()->global_context()->object_function()); |
302 Handle<JSObject> json_object = | 290 Handle<JSObject> json_object = |
303 isolate()->factory()->NewJSObject(object_constructor); | 291 isolate()->factory()->NewJSObject(object_constructor); |
304 ASSERT_EQ('{', c0_); | 292 ASSERT_EQ(c0_, '{'); |
305 | 293 |
306 AdvanceSkipWhitespace(); | 294 AdvanceSkipWhitespace(); |
307 if (c0_ != '}') { | 295 if (c0_ != '}') { |
308 do { | 296 do { |
309 if (c0_ != '"') return ReportUnexpectedCharacter(); | 297 if (c0_ != '"') return ReportUnexpectedCharacter(); |
310 Handle<String> key = ParseJsonSymbol(); | 298 Handle<String> key = ParseJsonSymbol(); |
311 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); | 299 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); |
312 AdvanceSkipWhitespace(); | 300 AdvanceSkipWhitespace(); |
313 Handle<Object> value = ParseJsonValue(); | 301 Handle<Object> value = ParseJsonValue(); |
314 if (value.is_null()) return ReportUnexpectedCharacter(); | 302 if (value.is_null()) return ReportUnexpectedCharacter(); |
315 | 303 |
316 uint32_t index; | 304 uint32_t index; |
317 if (key->AsArrayIndex(&index)) { | 305 if (key->AsArrayIndex(&index)) { |
318 SetOwnElement(json_object, index, value, kNonStrictMode); | 306 SetOwnElement(json_object, index, value, kNonStrictMode); |
319 } else if (key->Equals(isolate()->heap()->Proto_symbol())) { | 307 } else if (key->Equals(isolate()->heap()->Proto_symbol())) { |
320 SetPrototype(json_object, value); | 308 SetPrototype(json_object, value); |
321 } else { | 309 } else { |
322 SetLocalPropertyIgnoreAttributes(json_object, key, value, NONE); | 310 SetLocalPropertyIgnoreAttributes(json_object, key, value, NONE); |
323 } | 311 } |
324 } while (MatchSkipWhiteSpace(',')); | 312 } while (MatchSkipWhiteSpace(',')); |
325 if (c0_ != '}') { | 313 if (c0_ != '}') { |
326 return ReportUnexpectedCharacter(); | 314 return ReportUnexpectedCharacter(); |
327 } | 315 } |
328 } | 316 } |
329 AdvanceSkipWhitespace(); | 317 AdvanceSkipWhitespace(); |
330 return json_object; | 318 return json_object; |
331 } | 319 } |
332 | 320 |
333 // Parse a JSON array. Position must be right at '['. | 321 // Parse a JSON array. Position must be right at '['. |
334 template <typename StringType> | 322 template <bool seq_ascii> |
335 Handle<Object> JsonParser<StringType>::ParseJsonArray() { | 323 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { |
336 ZoneScope zone_scope(isolate(), DELETE_ON_EXIT); | 324 ZoneScope zone_scope(isolate(), DELETE_ON_EXIT); |
337 ZoneList<Handle<Object> > elements(4); | 325 ZoneList<Handle<Object> > elements(4); |
338 ASSERT_EQ(c0_, '['); | 326 ASSERT_EQ(c0_, '['); |
339 | 327 |
340 AdvanceSkipWhitespace(); | 328 AdvanceSkipWhitespace(); |
341 if (c0_ != ']') { | 329 if (c0_ != ']') { |
342 do { | 330 do { |
343 Handle<Object> element = ParseJsonValue(); | 331 Handle<Object> element = ParseJsonValue(); |
344 if (element.is_null()) return ReportUnexpectedCharacter(); | 332 if (element.is_null()) return ReportUnexpectedCharacter(); |
345 elements.Add(element); | 333 elements.Add(element); |
346 } while (MatchSkipWhiteSpace(',')); | 334 } while (MatchSkipWhiteSpace(',')); |
347 if (c0_ != ']') { | 335 if (c0_ != ']') { |
348 return ReportUnexpectedCharacter(); | 336 return ReportUnexpectedCharacter(); |
349 } | 337 } |
350 } | 338 } |
351 AdvanceSkipWhitespace(); | 339 AdvanceSkipWhitespace(); |
352 // Allocate a fixed array with all the elements. | 340 // Allocate a fixed array with all the elements. |
353 Handle<FixedArray> fast_elements = | 341 Handle<FixedArray> fast_elements = |
354 isolate()->factory()->NewFixedArray(elements.length()); | 342 isolate()->factory()->NewFixedArray(elements.length()); |
355 for (int i = 0, n = elements.length(); i < n; i++) { | 343 for (int i = 0, n = elements.length(); i < n; i++) { |
356 fast_elements->set(i, *elements[i]); | 344 fast_elements->set(i, *elements[i]); |
357 } | 345 } |
358 return isolate()->factory()->NewJSArrayWithElements(fast_elements); | 346 return isolate()->factory()->NewJSArrayWithElements(fast_elements); |
359 } | 347 } |
360 | 348 |
361 | 349 |
362 template <typename StringType> | 350 template <bool seq_ascii> |
363 Handle<Object> JsonParser<StringType>::ParseJsonNumber() { | 351 Handle<Object> JsonParser<seq_ascii>::ParseJsonNumber() { |
364 bool negative = false; | 352 bool negative = false; |
365 int beg_pos = position_; | 353 int beg_pos = position_; |
366 if (c0_ == '-') { | 354 if (c0_ == '-') { |
367 Advance(); | 355 Advance(); |
368 negative = true; | 356 negative = true; |
369 } | 357 } |
370 if (c0_ == '0') { | 358 if (c0_ == '0') { |
371 Advance(); | 359 Advance(); |
372 // Prefix zero is only allowed if it's the only digit before | 360 // Prefix zero is only allowed if it's the only digit before |
373 // a decimal point or exponent. | 361 // a decimal point or exponent. |
(...skipping 22 matching lines...) Expand all Loading... |
396 if (AsciiAlphaToLower(c0_) == 'e') { | 384 if (AsciiAlphaToLower(c0_) == 'e') { |
397 Advance(); | 385 Advance(); |
398 if (c0_ == '-' || c0_ == '+') Advance(); | 386 if (c0_ == '-' || c0_ == '+') Advance(); |
399 if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); | 387 if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); |
400 do { | 388 do { |
401 Advance(); | 389 Advance(); |
402 } while (c0_ >= '0' && c0_ <= '9'); | 390 } while (c0_ >= '0' && c0_ <= '9'); |
403 } | 391 } |
404 int length = position_ - beg_pos; | 392 int length = position_ - beg_pos; |
405 double number; | 393 double number; |
406 | 394 if (seq_ascii) { |
407 Vector<const SourceChar> chars(GetChars() + beg_pos, length); | 395 Vector<const char> chars(seq_source_->GetChars() + beg_pos, length); |
408 number = StringToDouble(isolate()->unicode_cache(), | 396 number = StringToDouble(isolate()->unicode_cache(), |
409 chars, | 397 chars, |
410 NO_FLAGS, // Hex, octal or trailing junk. | 398 NO_FLAGS, // Hex, octal or trailing junk. |
411 OS::nan_value()); | 399 OS::nan_value()); |
| 400 } else { |
| 401 Vector<char> buffer = Vector<char>::New(length); |
| 402 String::WriteToFlat(*source_, buffer.start(), beg_pos, position_); |
| 403 Vector<const char> result = |
| 404 Vector<const char>(reinterpret_cast<const char*>(buffer.start()), |
| 405 length); |
| 406 number = StringToDouble(isolate()->unicode_cache(), |
| 407 result, |
| 408 NO_FLAGS, // Hex, octal or trailing junk. |
| 409 0.0); |
| 410 buffer.Dispose(); |
| 411 } |
412 SkipWhitespace(); | 412 SkipWhitespace(); |
413 return isolate()->factory()->NewNumber(number); | 413 return isolate()->factory()->NewNumber(number); |
414 } | 414 } |
415 | 415 |
416 | 416 |
417 template <typename StringType> | 417 template <typename StringType> |
418 inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c); | 418 inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c); |
419 | 419 |
420 template <> | 420 template <> |
421 inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) { | 421 inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) { |
(...skipping 15 matching lines...) Expand all Loading... |
437 | 437 |
438 template <> | 438 template <> |
439 inline Handle<SeqAsciiString> NewRawString(Factory* factory, int length) { | 439 inline Handle<SeqAsciiString> NewRawString(Factory* factory, int length) { |
440 return factory->NewRawAsciiString(length, NOT_TENURED); | 440 return factory->NewRawAsciiString(length, NOT_TENURED); |
441 } | 441 } |
442 | 442 |
443 | 443 |
444 // Scans the rest of a JSON string starting from position_ and writes | 444 // Scans the rest of a JSON string starting from position_ and writes |
445 // prefix[start..end] along with the scanned characters into a | 445 // prefix[start..end] along with the scanned characters into a |
446 // sequential string of type StringType. | 446 // sequential string of type StringType. |
447 template <typename StringType> | 447 template <bool seq_ascii> |
448 template <typename SinkStringType> | 448 template <typename StringType, typename SinkChar> |
449 Handle<String> JsonParser<StringType>::SlowScanJsonString( | 449 Handle<String> JsonParser<seq_ascii>::SlowScanJsonString( |
450 Handle<String> prefix, int start, int end) { | 450 Handle<String> prefix, int start, int end) { |
451 typedef typename SinkStringType::CharType SinkChar; | |
452 int count = end - start; | 451 int count = end - start; |
453 int max_length = count + source_length_ - position_; | 452 int max_length = count + source_length_ - position_; |
454 int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); | 453 int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); |
455 Handle<SinkStringType> seq_str = | 454 Handle<StringType> seq_str = NewRawString<StringType>(isolate()->factory(), |
456 NewRawString<SinkStringType>(isolate()->factory(), | 455 length); |
457 length); | |
458 // Copy prefix into seq_str. | 456 // Copy prefix into seq_str. |
459 SinkChar* dest = seq_str->GetChars(); | 457 SinkChar* dest = seq_str->GetChars(); |
460 String::WriteToFlat(*prefix, dest, start, end); | 458 String::WriteToFlat(*prefix, dest, start, end); |
461 | 459 |
462 while (c0_ != '"') { | 460 while (c0_ != '"') { |
463 // Check for control character (0x00-0x1f) or unterminated string (<0). | 461 // Check for control character (0x00-0x1f) or unterminated string (<0). |
464 if (c0_ < 0x20) return Handle<String>::null(); | 462 if (c0_ < 0x20) return Handle<String>::null(); |
465 if (count >= length) { | 463 if (count >= length) { |
466 // We need to create a longer sequential string for the result. | 464 // We need to create a longer sequential string for the result. |
467 return SlowScanJsonString<SinkStringType>(seq_str, 0, count); | 465 return SlowScanJsonString<StringType, SinkChar>(seq_str, 0, count); |
468 } | 466 } |
469 if (c0_ != '\\') { | 467 if (c0_ != '\\') { |
470 // If the sink can contain UC16 characters, or source_ contains only | 468 // If the sink can contain UC16 characters, or source_ contains only |
471 // ASCII characters, there's no need to test whether we can store the | 469 // ASCII characters, there's no need to test whether we can store the |
472 // character. Otherwise check whether the UC16 source character can fit | 470 // character. Otherwise check whether the UC16 source character can fit |
473 // in the ASCII sink. | 471 // in the ASCII sink. |
474 if (sizeof(SinkChar) == kUC16Size || | 472 if (sizeof(SinkChar) == kUC16Size || |
475 sizeof(SourceChar) == kCharSize || | 473 seq_ascii || |
476 c0_ <= kMaxAsciiCharCode) { | 474 c0_ <= kMaxAsciiCharCode) { |
477 SeqStringSet(seq_str, count++, c0_); | 475 SeqStringSet(seq_str, count++, c0_); |
478 Advance(); | 476 Advance(); |
479 } else { | 477 } else { |
480 // SinkStringType is SeqAsciiString and we just read a non-ASCII char. | 478 // StringType is SeqAsciiString and we just read a non-ASCII char. |
481 return SlowScanJsonString<SeqTwoByteString>(seq_str, 0, count); | 479 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_str, 0, count); |
482 } | 480 } |
483 } else { | 481 } else { |
484 Advance(); // Advance past the '\'. | 482 Advance(); // Advance past the \. |
485 switch (c0_) { | 483 switch (c0_) { |
486 case '"': | 484 case '"': |
487 case '\\': | 485 case '\\': |
488 case '/': | 486 case '/': |
489 SeqStringSet(seq_str, count++, c0_); | 487 SeqStringSet(seq_str, count++, c0_); |
490 break; | 488 break; |
491 case 'b': | 489 case 'b': |
492 SeqStringSet(seq_str, count++, '\x08'); | 490 SeqStringSet(seq_str, count++, '\x08'); |
493 break; | 491 break; |
494 case 'f': | 492 case 'f': |
(...skipping 18 matching lines...) Expand all Loading... |
513 } | 511 } |
514 value = value * 16 + digit; | 512 value = value * 16 + digit; |
515 } | 513 } |
516 if (sizeof(SinkChar) == kUC16Size || value <= kMaxAsciiCharCode) { | 514 if (sizeof(SinkChar) == kUC16Size || value <= kMaxAsciiCharCode) { |
517 SeqStringSet(seq_str, count++, value); | 515 SeqStringSet(seq_str, count++, value); |
518 break; | 516 break; |
519 } else { | 517 } else { |
520 // StringType is SeqAsciiString and we just read a non-ASCII char. | 518 // StringType is SeqAsciiString and we just read a non-ASCII char. |
521 position_ -= 6; // Rewind position_ to \ in \uxxxx. | 519 position_ -= 6; // Rewind position_ to \ in \uxxxx. |
522 Advance(); | 520 Advance(); |
523 return SlowScanJsonString<SeqTwoByteString>(seq_str, | 521 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_str, |
524 0, | 522 0, |
525 count); | 523 count); |
526 } | 524 } |
527 } | 525 } |
528 default: | 526 default: |
529 return Handle<String>::null(); | 527 return Handle<String>::null(); |
530 } | 528 } |
531 Advance(); | 529 Advance(); |
532 } | 530 } |
533 } | 531 } |
534 // Shrink seq_string length to count. | 532 // Shrink seq_string length to count. |
535 if (isolate()->heap()->InNewSpace(*seq_str)) { | 533 if (isolate()->heap()->InNewSpace(*seq_str)) { |
536 isolate()->heap()->new_space()-> | 534 isolate()->heap()->new_space()-> |
537 template ShrinkStringAtAllocationBoundary<SinkStringType>( | 535 template ShrinkStringAtAllocationBoundary<StringType>( |
538 *seq_str, count); | 536 *seq_str, count); |
539 } else { | 537 } else { |
540 int string_size = SinkStringType::SizeFor(count); | 538 int string_size = StringType::SizeFor(count); |
541 int allocated_string_size = SinkStringType::SizeFor(length); | 539 int allocated_string_size = StringType::SizeFor(length); |
542 int delta = allocated_string_size - string_size; | 540 int delta = allocated_string_size - string_size; |
543 Address start_filler_object = seq_str->address() + string_size; | 541 Address start_filler_object = seq_str->address() + string_size; |
544 seq_str->set_length(count); | 542 seq_str->set_length(count); |
545 isolate()->heap()->CreateFillerObjectAt(start_filler_object, delta); | 543 isolate()->heap()->CreateFillerObjectAt(start_filler_object, delta); |
546 } | 544 } |
547 ASSERT_EQ('"', c0_); | 545 ASSERT_EQ('"', c0_); |
548 // Advance past the last '"'. | 546 // Advance past the last '"'. |
549 AdvanceSkipWhitespace(); | 547 AdvanceSkipWhitespace(); |
550 return seq_str; | 548 return seq_str; |
551 } | 549 } |
552 | 550 |
553 | 551 |
554 template <typename StringType> | 552 template <bool seq_ascii> |
555 template <bool is_symbol> | 553 template <bool is_symbol> |
556 Handle<String> JsonParser<StringType>::ScanJsonString() { | 554 Handle<String> JsonParser<seq_ascii>::ScanJsonString() { |
557 ASSERT_EQ('"', c0_); | 555 ASSERT_EQ('"', c0_); |
558 Advance(); | 556 Advance(); |
559 if (c0_ == '"') { | 557 if (c0_ == '"') { |
560 AdvanceSkipWhitespace(); | 558 AdvanceSkipWhitespace(); |
561 return Handle<String>(isolate()->heap()->empty_string()); | 559 return Handle<String>(isolate()->heap()->empty_string()); |
562 } | 560 } |
563 int beg_pos = position_; | 561 int beg_pos = position_; |
564 // Fast case for ASCII only without escape characters. | 562 // Fast case for ASCII only without escape characters. |
565 do { | 563 do { |
566 // Check for control character (0x00-0x1f) or unterminated string (<0). | 564 // Check for control character (0x00-0x1f) or unterminated string (<0). |
567 if (c0_ < 0x20) return Handle<String>::null(); | 565 if (c0_ < 0x20) return Handle<String>::null(); |
568 if (c0_ != '\\') { | 566 if (c0_ != '\\') { |
569 if (c0_ <= kMaxAsciiCharCode) { | 567 if (seq_ascii || c0_ <= kMaxAsciiCharCode) { |
570 Advance(); | 568 Advance(); |
571 } else { | 569 } else { |
572 return SlowScanJsonString<SeqTwoByteString>(source_, | 570 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, |
573 beg_pos, | 571 beg_pos, |
574 position_); | 572 position_); |
575 } | 573 } |
576 } else { | 574 } else { |
577 return SlowScanJsonString<SeqAsciiString>(source_, | 575 return SlowScanJsonString<SeqAsciiString, char>(source_, |
578 beg_pos, | 576 beg_pos, |
579 position_); | 577 position_); |
580 } | 578 } |
581 } while (c0_ != '"'); | 579 } while (c0_ != '"'); |
582 int length = position_ - beg_pos; | 580 int length = position_ - beg_pos; |
583 Handle<String> result; | 581 Handle<String> result; |
584 if (is_symbol && source_->IsSeqAsciiString()) { | 582 if (seq_ascii && is_symbol) { |
585 result = isolate()->factory()->LookupAsciiSymbol( | 583 result = isolate()->factory()->LookupAsciiSymbol(seq_source_, |
586 Handle<SeqAsciiString>::cast(source_), beg_pos, length); | 584 beg_pos, |
| 585 length); |
587 } else { | 586 } else { |
588 result = isolate()->factory()->NewRawAsciiString(length); | 587 result = isolate()->factory()->NewRawAsciiString(length); |
589 char* dest = SeqAsciiString::cast(*result)->GetChars(); | 588 char* dest = SeqAsciiString::cast(*result)->GetChars(); |
590 String::WriteToFlat(*source_, dest, beg_pos, position_); | 589 String::WriteToFlat(*source_, dest, beg_pos, position_); |
591 } | 590 } |
592 ASSERT_EQ('"', c0_); | 591 ASSERT_EQ('"', c0_); |
593 // Advance past the last '"'. | 592 // Advance past the last '"'. |
594 AdvanceSkipWhitespace(); | 593 AdvanceSkipWhitespace(); |
595 return result; | 594 return result; |
596 } | 595 } |
597 | 596 |
598 | |
599 template <typename StringType> | |
600 void JsonParser<StringType>::InitializeSource() { } | |
601 | |
602 | |
603 template <> | |
604 void JsonParser<ExternalAsciiString>::InitializeSource() { | |
605 characters_ = source_->resource()->data(); | |
606 } | |
607 | |
608 | |
609 template <> | |
610 void JsonParser<ExternalTwoByteString>::InitializeSource() { | |
611 characters_ = source_->resource()->data(); | |
612 } | |
613 | |
614 | |
615 template <> | |
616 uc32 JsonParser<SeqAsciiString>::GetChar(int pos) { | |
617 return static_cast<uc32>(source_->SeqAsciiStringGet(pos)); | |
618 } | |
619 | |
620 | |
621 template <> | |
622 uc32 JsonParser<SeqTwoByteString>::GetChar(int pos) { | |
623 return static_cast<uc32>(source_->SeqTwoByteStringGet(pos)); | |
624 } | |
625 | |
626 | |
627 template <> | |
628 uc32 JsonParser<ExternalAsciiString>::GetChar(int pos) { | |
629 ASSERT(pos >= 0); | |
630 ASSERT(pos < source_length_); | |
631 return static_cast<uc32>(characters_[pos]); | |
632 } | |
633 | |
634 | |
635 template <> | |
636 uc32 JsonParser<ExternalTwoByteString>::GetChar(int pos) { | |
637 ASSERT(pos >= 0); | |
638 ASSERT(pos < source_length_); | |
639 return static_cast<uc32>(characters_[pos]); | |
640 } | |
641 | |
642 | |
643 template <> | |
644 const char* JsonParser<SeqAsciiString>::GetChars() { | |
645 return source_->GetChars(); | |
646 } | |
647 | |
648 | |
649 template <> | |
650 const uc16* JsonParser<SeqTwoByteString>::GetChars() { | |
651 return source_->GetChars(); | |
652 } | |
653 | |
654 | |
655 template <> | |
656 const char* JsonParser<ExternalAsciiString>::GetChars() { | |
657 return characters_; | |
658 } | |
659 | |
660 | |
661 template <> | |
662 const uc16* JsonParser<ExternalTwoByteString>::GetChars() { | |
663 return characters_; | |
664 } | |
665 | |
666 } } // namespace v8::internal | 597 } } // namespace v8::internal |
667 | 598 |
668 #endif // V8_JSON_PARSER_H_ | 599 #endif // V8_JSON_PARSER_H_ |
OLD | NEW |