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

Side by Side Diff: src/json-parser.cc

Issue 2002933002: [json] move json parser and stringifier into own compilation units. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rebase Created 4 years, 7 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
« no previous file with comments | « src/json-parser.h ('k') | src/json-stringifier.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_JSON_PARSER_H_ 5 #include "src/json-parser.h"
6 #define V8_JSON_PARSER_H_
7 6
8 #include "src/char-predicates.h" 7 #include "src/char-predicates-inl.h"
9 #include "src/conversions.h" 8 #include "src/conversions.h"
10 #include "src/debug/debug.h" 9 #include "src/debug/debug.h"
11 #include "src/factory.h" 10 #include "src/factory.h"
12 #include "src/field-type.h" 11 #include "src/field-type.h"
13 #include "src/messages.h" 12 #include "src/messages.h"
13 #include "src/objects-inl.h"
14 #include "src/parsing/scanner.h" 14 #include "src/parsing/scanner.h"
15 #include "src/parsing/token.h" 15 #include "src/parsing/token.h"
16 #include "src/transitions.h" 16 #include "src/transitions.h"
17 17
18 namespace v8 { 18 namespace v8 {
19 namespace internal { 19 namespace internal {
20 20
21 enum ParseElementResult { kElementFound, kElementNotFound, kNullHandle }; 21 template <bool seq_one_byte>
22 JsonParser<seq_one_byte>::JsonParser(Handle<String> source)
23 : source_(source),
24 source_length_(source->length()),
25 isolate_(source->map()->GetHeap()->isolate()),
26 factory_(isolate_->factory()),
27 zone_(isolate_->allocator()),
28 object_constructor_(isolate_->native_context()->object_function(),
29 isolate_),
30 position_(-1) {
31 source_ = String::Flatten(source_);
32 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
22 33
23 34 // Optimized fast case where we only have Latin1 characters.
24 // A simple json parser. 35 if (seq_one_byte) {
25 template <bool seq_one_byte> 36 seq_source_ = Handle<SeqOneByteString>::cast(source_);
26 class JsonParser BASE_EMBEDDED {
27 public:
28 MUST_USE_RESULT static MaybeHandle<Object> Parse(Handle<String> source) {
29 return JsonParser(source).ParseJson();
30 } 37 }
31 38 }
32 static const int kEndOfString = -1;
33
34 private:
35 explicit JsonParser(Handle<String> source)
36 : source_(source),
37 source_length_(source->length()),
38 isolate_(source->map()->GetHeap()->isolate()),
39 factory_(isolate_->factory()),
40 zone_(isolate_->allocator()),
41 object_constructor_(isolate_->native_context()->object_function(),
42 isolate_),
43 position_(-1) {
44 source_ = String::Flatten(source_);
45 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
46
47 // Optimized fast case where we only have Latin1 characters.
48 if (seq_one_byte) {
49 seq_source_ = Handle<SeqOneByteString>::cast(source_);
50 }
51 }
52
53 // Parse a string containing a single JSON value.
54 MaybeHandle<Object> ParseJson();
55
56 inline void Advance() {
57 position_++;
58 if (position_ >= source_length_) {
59 c0_ = kEndOfString;
60 } else if (seq_one_byte) {
61 c0_ = seq_source_->SeqOneByteStringGet(position_);
62 } else {
63 c0_ = source_->Get(position_);
64 }
65 }
66
67 // The JSON lexical grammar is specified in the ECMAScript 5 standard,
68 // section 15.12.1.1. The only allowed whitespace characters between tokens
69 // are tab, carriage-return, newline and space.
70
71 inline void AdvanceSkipWhitespace() {
72 do {
73 Advance();
74 } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r');
75 }
76
77 inline void SkipWhitespace() {
78 while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') {
79 Advance();
80 }
81 }
82
83 inline uc32 AdvanceGetChar() {
84 Advance();
85 return c0_;
86 }
87
88 // Checks that current charater is c.
89 // If so, then consume c and skip whitespace.
90 inline bool MatchSkipWhiteSpace(uc32 c) {
91 if (c0_ == c) {
92 AdvanceSkipWhitespace();
93 return true;
94 }
95 return false;
96 }
97
98 // A JSON string (production JSONString) is subset of valid JavaScript string
99 // literals. The string must only be double-quoted (not single-quoted), and
100 // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and
101 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid.
102 Handle<String> ParseJsonString() {
103 return ScanJsonString<false>();
104 }
105
106 bool ParseJsonString(Handle<String> expected) {
107 int length = expected->length();
108 if (source_->length() - position_ - 1 > length) {
109 DisallowHeapAllocation no_gc;
110 String::FlatContent content = expected->GetFlatContent();
111 if (content.IsOneByte()) {
112 DCHECK_EQ('"', c0_);
113 const uint8_t* input_chars = seq_source_->GetChars() + position_ + 1;
114 const uint8_t* expected_chars = content.ToOneByteVector().start();
115 for (int i = 0; i < length; i++) {
116 uint8_t c0 = input_chars[i];
117 if (c0 != expected_chars[i] || c0 == '"' || c0 < 0x20 || c0 == '\\') {
118 return false;
119 }
120 }
121 if (input_chars[length] == '"') {
122 position_ = position_ + length + 1;
123 AdvanceSkipWhitespace();
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130
131 Handle<String> ParseJsonInternalizedString() {
132 Handle<String> result = ScanJsonString<true>();
133 if (result.is_null()) return result;
134 return factory()->InternalizeString(result);
135 }
136
137 template <bool is_internalized>
138 Handle<String> ScanJsonString();
139 // Creates a new string and copies prefix[start..end] into the beginning
140 // of it. Then scans the rest of the string, adding characters after the
141 // prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char.
142 template <typename StringType, typename SinkChar>
143 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
144
145 // A JSON number (production JSONNumber) is a subset of the valid JavaScript
146 // decimal number literals.
147 // It includes an optional minus sign, must have at least one
148 // digit before and after a decimal point, may not have prefixed zeros (unless
149 // the integer part is zero), and may include an exponent part (e.g., "e-10").
150 // Hexadecimal and octal numbers are not allowed.
151 Handle<Object> ParseJsonNumber();
152
153 // Parse a single JSON value from input (grammar production JSONValue).
154 // A JSON value is either a (double-quoted) string literal, a number literal,
155 // one of "true", "false", or "null", or an object or array literal.
156 Handle<Object> ParseJsonValue();
157
158 // Parse a JSON object literal (grammar production JSONObject).
159 // An object literal is a squiggly-braced and comma separated sequence
160 // (possibly empty) of key/value pairs, where the key is a JSON string
161 // literal, the value is a JSON value, and the two are separated by a colon.
162 // A JSON array doesn't allow numbers and identifiers as keys, like a
163 // JavaScript array.
164 Handle<Object> ParseJsonObject();
165
166 // Helper for ParseJsonObject. Parses the form "123": obj, which is recorded
167 // as an element, not a property.
168 ParseElementResult ParseElement(Handle<JSObject> json_object);
169
170 // Parses a JSON array literal (grammar production JSONArray). An array
171 // literal is a square-bracketed and comma separated sequence (possibly empty)
172 // of JSON values.
173 // A JSON array doesn't allow leaving out values from the sequence, nor does
174 // it allow a terminal comma, like a JavaScript array does.
175 Handle<Object> ParseJsonArray();
176
177
178 // Mark that a parsing error has happened at the current token, and
179 // return a null handle. Primarily for readability.
180 inline Handle<Object> ReportUnexpectedCharacter() {
181 return Handle<Object>::null();
182 }
183
184 inline Isolate* isolate() { return isolate_; }
185 inline Factory* factory() { return factory_; }
186 inline Handle<JSFunction> object_constructor() { return object_constructor_; }
187
188 static const int kInitialSpecialStringLength = 32;
189 static const int kPretenureTreshold = 100 * 1024;
190
191
192 private:
193 Zone* zone() { return &zone_; }
194
195 void CommitStateToJsonObject(Handle<JSObject> json_object, Handle<Map> map,
196 ZoneList<Handle<Object> >* properties);
197
198 Handle<String> source_;
199 int source_length_;
200 Handle<SeqOneByteString> seq_source_;
201
202 PretenureFlag pretenure_;
203 Isolate* isolate_;
204 Factory* factory_;
205 Zone zone_;
206 Handle<JSFunction> object_constructor_;
207 uc32 c0_;
208 int position_;
209 };
210 39
211 template <bool seq_one_byte> 40 template <bool seq_one_byte>
212 MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() { 41 MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
213 // Advance to the first character (possibly EOS) 42 // Advance to the first character (possibly EOS)
214 AdvanceSkipWhitespace(); 43 AdvanceSkipWhitespace();
215 Handle<Object> result = ParseJsonValue(); 44 Handle<Object> result = ParseJsonValue();
216 if (result.is_null() || c0_ != kEndOfString) { 45 if (result.is_null() || c0_ != kEndOfString) {
217 // Some exception (for example stack overflow) is already pending. 46 // Some exception (for example stack overflow) is already pending.
218 if (isolate_->has_pending_exception()) return Handle<Object>::null(); 47 if (isolate_->has_pending_exception()) return Handle<Object>::null();
219 48
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // We should sent compile error event because we compile JSON object in 83 // We should sent compile error event because we compile JSON object in
255 // separated source file. 84 // separated source file.
256 isolate()->debug()->OnCompileError(script); 85 isolate()->debug()->OnCompileError(script);
257 MessageLocation location(script, position_, position_ + 1); 86 MessageLocation location(script, position_, position_ + 1);
258 Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2); 87 Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2);
259 return isolate()->template Throw<Object>(error, &location); 88 return isolate()->template Throw<Object>(error, &location);
260 } 89 }
261 return result; 90 return result;
262 } 91 }
263 92
93 template <bool seq_one_byte>
94 void JsonParser<seq_one_byte>::Advance() {
95 position_++;
96 if (position_ >= source_length_) {
97 c0_ = kEndOfString;
98 } else if (seq_one_byte) {
99 c0_ = seq_source_->SeqOneByteStringGet(position_);
100 } else {
101 c0_ = source_->Get(position_);
102 }
103 }
104
105 template <bool seq_one_byte>
106 void JsonParser<seq_one_byte>::AdvanceSkipWhitespace() {
107 do {
108 Advance();
109 } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r');
110 }
111
112 template <bool seq_one_byte>
113 void JsonParser<seq_one_byte>::SkipWhitespace() {
114 while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') {
115 Advance();
116 }
117 }
118
119 template <bool seq_one_byte>
120 uc32 JsonParser<seq_one_byte>::AdvanceGetChar() {
121 Advance();
122 return c0_;
123 }
124
125 template <bool seq_one_byte>
126 bool JsonParser<seq_one_byte>::MatchSkipWhiteSpace(uc32 c) {
127 if (c0_ == c) {
128 AdvanceSkipWhitespace();
129 return true;
130 }
131 return false;
132 }
133
134 template <bool seq_one_byte>
135 bool JsonParser<seq_one_byte>::ParseJsonString(Handle<String> expected) {
136 int length = expected->length();
137 if (source_->length() - position_ - 1 > length) {
138 DisallowHeapAllocation no_gc;
139 String::FlatContent content = expected->GetFlatContent();
140 if (content.IsOneByte()) {
141 DCHECK_EQ('"', c0_);
142 const uint8_t* input_chars = seq_source_->GetChars() + position_ + 1;
143 const uint8_t* expected_chars = content.ToOneByteVector().start();
144 for (int i = 0; i < length; i++) {
145 uint8_t c0 = input_chars[i];
146 if (c0 != expected_chars[i] || c0 == '"' || c0 < 0x20 || c0 == '\\') {
147 return false;
148 }
149 }
150 if (input_chars[length] == '"') {
151 position_ = position_ + length + 1;
152 AdvanceSkipWhitespace();
153 return true;
154 }
155 }
156 }
157 return false;
158 }
264 159
265 // Parse any JSON value. 160 // Parse any JSON value.
266 template <bool seq_one_byte> 161 template <bool seq_one_byte>
267 Handle<Object> JsonParser<seq_one_byte>::ParseJsonValue() { 162 Handle<Object> JsonParser<seq_one_byte>::ParseJsonValue() {
268 StackLimitCheck stack_check(isolate_); 163 StackLimitCheck stack_check(isolate_);
269 if (stack_check.HasOverflowed()) { 164 if (stack_check.HasOverflowed()) {
270 isolate_->StackOverflow(); 165 isolate_->StackOverflow();
271 return Handle<Object>::null(); 166 return Handle<Object>::null();
272 } 167 }
273 168
(...skipping 27 matching lines...) Expand all
301 if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' && 196 if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' &&
302 AdvanceGetChar() == 'l') { 197 AdvanceGetChar() == 'l') {
303 AdvanceSkipWhitespace(); 198 AdvanceSkipWhitespace();
304 return factory()->null_value(); 199 return factory()->null_value();
305 } 200 }
306 return ReportUnexpectedCharacter(); 201 return ReportUnexpectedCharacter();
307 } 202 }
308 return ReportUnexpectedCharacter(); 203 return ReportUnexpectedCharacter();
309 } 204 }
310 205
311
312 template <bool seq_one_byte> 206 template <bool seq_one_byte>
313 ParseElementResult JsonParser<seq_one_byte>::ParseElement( 207 ParseElementResult JsonParser<seq_one_byte>::ParseElement(
314 Handle<JSObject> json_object) { 208 Handle<JSObject> json_object) {
315 uint32_t index = 0; 209 uint32_t index = 0;
316 // Maybe an array index, try to parse it. 210 // Maybe an array index, try to parse it.
317 if (c0_ == '0') { 211 if (c0_ == '0') {
318 // With a leading zero, the string has to be "0" only to be an index. 212 // With a leading zero, the string has to be "0" only to be an index.
319 Advance(); 213 Advance();
320 } else { 214 } else {
321 do { 215 do {
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 Handle<Object> value; 368 Handle<Object> value;
475 369
476 key = ParseJsonInternalizedString(); 370 key = ParseJsonInternalizedString();
477 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); 371 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter();
478 372
479 AdvanceSkipWhitespace(); 373 AdvanceSkipWhitespace();
480 value = ParseJsonValue(); 374 value = ParseJsonValue();
481 if (value.is_null()) return ReportUnexpectedCharacter(); 375 if (value.is_null()) return ReportUnexpectedCharacter();
482 376
483 JSObject::DefinePropertyOrElementIgnoreAttributes(json_object, key, 377 JSObject::DefinePropertyOrElementIgnoreAttributes(json_object, key,
484 value).Check(); 378 value)
379 .Check();
485 } 380 }
486 } 381 }
487 382
488 if (c0_ != '}') { 383 if (c0_ != '}') {
489 return ReportUnexpectedCharacter(); 384 return ReportUnexpectedCharacter();
490 } 385 }
491 } 386 }
492 AdvanceSkipWhitespace(); 387 AdvanceSkipWhitespace();
493 return scope.CloseAndEscape(json_object); 388 return scope.CloseAndEscape(json_object);
494 } 389 }
495 390
496
497 template <bool seq_one_byte> 391 template <bool seq_one_byte>
498 void JsonParser<seq_one_byte>::CommitStateToJsonObject( 392 void JsonParser<seq_one_byte>::CommitStateToJsonObject(
499 Handle<JSObject> json_object, Handle<Map> map, 393 Handle<JSObject> json_object, Handle<Map> map,
500 ZoneList<Handle<Object> >* properties) { 394 ZoneList<Handle<Object> >* properties) {
501 JSObject::AllocateStorageForMap(json_object, map); 395 JSObject::AllocateStorageForMap(json_object, map);
502 DCHECK(!json_object->map()->is_dictionary_map()); 396 DCHECK(!json_object->map()->is_dictionary_map());
503 397
504 DisallowHeapAllocation no_gc; 398 DisallowHeapAllocation no_gc;
505 399
506 int length = properties->length(); 400 int length = properties->length();
507 for (int i = 0; i < length; i++) { 401 for (int i = 0; i < length; i++) {
508 Handle<Object> value = (*properties)[i]; 402 Handle<Object> value = (*properties)[i];
509 json_object->WriteToField(i, *value); 403 json_object->WriteToField(i, *value);
510 } 404 }
511 } 405 }
512 406
513
514 // Parse a JSON array. Position must be right at '['. 407 // Parse a JSON array. Position must be right at '['.
515 template <bool seq_one_byte> 408 template <bool seq_one_byte>
516 Handle<Object> JsonParser<seq_one_byte>::ParseJsonArray() { 409 Handle<Object> JsonParser<seq_one_byte>::ParseJsonArray() {
517 HandleScope scope(isolate()); 410 HandleScope scope(isolate());
518 ZoneList<Handle<Object> > elements(4, zone()); 411 ZoneList<Handle<Object> > elements(4, zone());
519 DCHECK_EQ(c0_, '['); 412 DCHECK_EQ(c0_, '[');
520 413
521 AdvanceSkipWhitespace(); 414 AdvanceSkipWhitespace();
522 if (c0_ != ']') { 415 if (c0_ != ']') {
523 do { 416 do {
(...skipping 10 matching lines...) Expand all
534 Handle<FixedArray> fast_elements = 427 Handle<FixedArray> fast_elements =
535 factory()->NewFixedArray(elements.length(), pretenure_); 428 factory()->NewFixedArray(elements.length(), pretenure_);
536 for (int i = 0, n = elements.length(); i < n; i++) { 429 for (int i = 0, n = elements.length(); i < n; i++) {
537 fast_elements->set(i, *elements[i]); 430 fast_elements->set(i, *elements[i]);
538 } 431 }
539 Handle<Object> json_array = factory()->NewJSArrayWithElements( 432 Handle<Object> json_array = factory()->NewJSArrayWithElements(
540 fast_elements, FAST_ELEMENTS, pretenure_); 433 fast_elements, FAST_ELEMENTS, pretenure_);
541 return scope.CloseAndEscape(json_array); 434 return scope.CloseAndEscape(json_array);
542 } 435 }
543 436
544
545 template <bool seq_one_byte> 437 template <bool seq_one_byte>
546 Handle<Object> JsonParser<seq_one_byte>::ParseJsonNumber() { 438 Handle<Object> JsonParser<seq_one_byte>::ParseJsonNumber() {
547 bool negative = false; 439 bool negative = false;
548 int beg_pos = position_; 440 int beg_pos = position_;
549 if (c0_ == '-') { 441 if (c0_ == '-') {
550 Advance(); 442 Advance();
551 negative = true; 443 negative = true;
552 } 444 }
553 if (c0_ == '0') { 445 if (c0_ == '0') {
554 Advance(); 446 Advance();
(...skipping 25 matching lines...) Expand all
580 Advance(); 472 Advance();
581 if (c0_ == '-' || c0_ == '+') Advance(); 473 if (c0_ == '-' || c0_ == '+') Advance();
582 if (!IsDecimalDigit(c0_)) return ReportUnexpectedCharacter(); 474 if (!IsDecimalDigit(c0_)) return ReportUnexpectedCharacter();
583 do { 475 do {
584 Advance(); 476 Advance();
585 } while (IsDecimalDigit(c0_)); 477 } while (IsDecimalDigit(c0_));
586 } 478 }
587 int length = position_ - beg_pos; 479 int length = position_ - beg_pos;
588 double number; 480 double number;
589 if (seq_one_byte) { 481 if (seq_one_byte) {
590 Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length); 482 Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length);
591 number = StringToDouble(isolate()->unicode_cache(), chars, 483 number = StringToDouble(isolate()->unicode_cache(), chars,
592 NO_FLAGS, // Hex, octal or trailing junk. 484 NO_FLAGS, // Hex, octal or trailing junk.
593 std::numeric_limits<double>::quiet_NaN()); 485 std::numeric_limits<double>::quiet_NaN());
594 } else { 486 } else {
595 Vector<uint8_t> buffer = Vector<uint8_t>::New(length); 487 Vector<uint8_t> buffer = Vector<uint8_t>::New(length);
596 String::WriteToFlat(*source_, buffer.start(), beg_pos, position_); 488 String::WriteToFlat(*source_, buffer.start(), beg_pos, position_);
597 Vector<const uint8_t> result = 489 Vector<const uint8_t> result =
598 Vector<const uint8_t>(buffer.start(), length); 490 Vector<const uint8_t>(buffer.start(), length);
599 number = StringToDouble(isolate()->unicode_cache(), 491 number = StringToDouble(isolate()->unicode_cache(), result,
600 result,
601 NO_FLAGS, // Hex, octal or trailing junk. 492 NO_FLAGS, // Hex, octal or trailing junk.
602 0.0); 493 0.0);
603 buffer.Dispose(); 494 buffer.Dispose();
604 } 495 }
605 SkipWhitespace(); 496 SkipWhitespace();
606 return factory()->NewNumber(number, pretenure_); 497 return factory()->NewNumber(number, pretenure_);
607 } 498 }
608 499
609
610 template <typename StringType> 500 template <typename StringType>
611 inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c); 501 inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c);
612 502
613 template <> 503 template <>
614 inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) { 504 inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) {
615 seq_str->SeqTwoByteStringSet(i, c); 505 seq_str->SeqTwoByteStringSet(i, c);
616 } 506 }
617 507
618 template <> 508 template <>
619 inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) { 509 inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) {
620 seq_str->SeqOneByteStringSet(i, c); 510 seq_str->SeqOneByteStringSet(i, c);
621 } 511 }
622 512
623 template <typename StringType> 513 template <typename StringType>
624 inline Handle<StringType> NewRawString(Factory* factory, 514 inline Handle<StringType> NewRawString(Factory* factory, int length,
625 int length,
626 PretenureFlag pretenure); 515 PretenureFlag pretenure);
627 516
628 template <> 517 template <>
629 inline Handle<SeqTwoByteString> NewRawString(Factory* factory, 518 inline Handle<SeqTwoByteString> NewRawString(Factory* factory, int length,
630 int length,
631 PretenureFlag pretenure) { 519 PretenureFlag pretenure) {
632 return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked(); 520 return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked();
633 } 521 }
634 522
635 template <> 523 template <>
636 inline Handle<SeqOneByteString> NewRawString(Factory* factory, 524 inline Handle<SeqOneByteString> NewRawString(Factory* factory, int length,
637 int length, 525 PretenureFlag pretenure) {
638 PretenureFlag pretenure) {
639 return factory->NewRawOneByteString(length, pretenure).ToHandleChecked(); 526 return factory->NewRawOneByteString(length, pretenure).ToHandleChecked();
640 } 527 }
641 528
642
643 // Scans the rest of a JSON string starting from position_ and writes 529 // Scans the rest of a JSON string starting from position_ and writes
644 // prefix[start..end] along with the scanned characters into a 530 // prefix[start..end] along with the scanned characters into a
645 // sequential string of type StringType. 531 // sequential string of type StringType.
646 template <bool seq_one_byte> 532 template <bool seq_one_byte>
647 template <typename StringType, typename SinkChar> 533 template <typename StringType, typename SinkChar>
648 Handle<String> JsonParser<seq_one_byte>::SlowScanJsonString( 534 Handle<String> JsonParser<seq_one_byte>::SlowScanJsonString(
649 Handle<String> prefix, int start, int end) { 535 Handle<String> prefix, int start, int end) {
650 int count = end - start; 536 int count = end - start;
651 int max_length = count + source_length_ - position_; 537 int max_length = count + source_length_ - position_;
652 int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); 538 int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 } 597 }
712 if (sizeof(SinkChar) == kUC16Size || 598 if (sizeof(SinkChar) == kUC16Size ||
713 value <= String::kMaxOneByteCharCode) { 599 value <= String::kMaxOneByteCharCode) {
714 SeqStringSet(seq_string, count++, value); 600 SeqStringSet(seq_string, count++, value);
715 break; 601 break;
716 } else { 602 } else {
717 // StringType is SeqOneByteString and we just read a non-Latin1 603 // StringType is SeqOneByteString and we just read a non-Latin1
718 // char. 604 // char.
719 position_ -= 6; // Rewind position_ to \ in \uxxxx. 605 position_ -= 6; // Rewind position_ to \ in \uxxxx.
720 Advance(); 606 Advance();
721 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string, 607 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string, 0,
722 0,
723 count); 608 count);
724 } 609 }
725 } 610 }
726 default: 611 default:
727 return Handle<String>::null(); 612 return Handle<String>::null();
728 } 613 }
729 Advance(); 614 Advance();
730 } 615 }
731 } 616 }
732 617
733 DCHECK_EQ('"', c0_); 618 DCHECK_EQ('"', c0_);
734 // Advance past the last '"'. 619 // Advance past the last '"'.
735 AdvanceSkipWhitespace(); 620 AdvanceSkipWhitespace();
736 621
737 // Shrink seq_string length to count and return. 622 // Shrink seq_string length to count and return.
738 return SeqString::Truncate(seq_string, count); 623 return SeqString::Truncate(seq_string, count);
739 } 624 }
740 625
741
742 template <bool seq_one_byte> 626 template <bool seq_one_byte>
743 template <bool is_internalized> 627 template <bool is_internalized>
744 Handle<String> JsonParser<seq_one_byte>::ScanJsonString() { 628 Handle<String> JsonParser<seq_one_byte>::ScanJsonString() {
745 DCHECK_EQ('"', c0_); 629 DCHECK_EQ('"', c0_);
746 Advance(); 630 Advance();
747 if (c0_ == '"') { 631 if (c0_ == '"') {
748 AdvanceSkipWhitespace(); 632 AdvanceSkipWhitespace();
749 return factory()->empty_string(); 633 return factory()->empty_string();
750 } 634 }
751 635
752 if (seq_one_byte && is_internalized) { 636 if (seq_one_byte && is_internalized) {
753 // Fast path for existing internalized strings. If the the string being 637 // Fast path for existing internalized strings. If the the string being
754 // parsed is not a known internalized string, contains backslashes or 638 // parsed is not a known internalized string, contains backslashes or
755 // unexpectedly reaches the end of string, return with an empty handle. 639 // unexpectedly reaches the end of string, return with an empty handle.
756 uint32_t running_hash = isolate()->heap()->HashSeed(); 640 uint32_t running_hash = isolate()->heap()->HashSeed();
757 int position = position_; 641 int position = position_;
758 uc32 c0 = c0_; 642 uc32 c0 = c0_;
759 do { 643 do {
760 if (c0 == '\\') { 644 if (c0 == '\\') {
761 c0_ = c0; 645 c0_ = c0;
762 int beg_pos = position_; 646 int beg_pos = position_;
763 position_ = position; 647 position_ = position;
764 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, 648 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, beg_pos,
765 beg_pos,
766 position_); 649 position_);
767 } 650 }
768 if (c0 < 0x20) return Handle<String>::null(); 651 if (c0 < 0x20) return Handle<String>::null();
769 running_hash = StringHasher::AddCharacterCore(running_hash, 652 running_hash = StringHasher::AddCharacterCore(running_hash,
770 static_cast<uint16_t>(c0)); 653 static_cast<uint16_t>(c0));
771 position++; 654 position++;
772 if (position >= source_length_) return Handle<String>::null(); 655 if (position >= source_length_) return Handle<String>::null();
773 c0 = seq_source_->SeqOneByteStringGet(position); 656 c0 = seq_source_->SeqOneByteStringGet(position);
774 } while (c0 != '"'); 657 } while (c0 != '"');
775 int length = position - position_; 658 int length = position - position_;
776 uint32_t hash = (length <= String::kMaxHashCalcLength) 659 uint32_t hash = (length <= String::kMaxHashCalcLength)
777 ? StringHasher::GetHashCore(running_hash) 660 ? StringHasher::GetHashCore(running_hash)
778 : static_cast<uint32_t>(length); 661 : static_cast<uint32_t>(length);
779 Vector<const uint8_t> string_vector( 662 Vector<const uint8_t> string_vector(seq_source_->GetChars() + position_,
780 seq_source_->GetChars() + position_, length); 663 length);
781 StringTable* string_table = isolate()->heap()->string_table(); 664 StringTable* string_table = isolate()->heap()->string_table();
782 uint32_t capacity = string_table->Capacity(); 665 uint32_t capacity = string_table->Capacity();
783 uint32_t entry = StringTable::FirstProbe(hash, capacity); 666 uint32_t entry = StringTable::FirstProbe(hash, capacity);
784 uint32_t count = 1; 667 uint32_t count = 1;
785 Handle<String> result; 668 Handle<String> result;
786 while (true) { 669 while (true) {
787 Object* element = string_table->KeyAt(entry); 670 Object* element = string_table->KeyAt(entry);
788 if (element == isolate()->heap()->undefined_value()) { 671 if (element == isolate()->heap()->undefined_value()) {
789 // Lookup failure. 672 // Lookup failure.
790 result = factory()->InternalizeOneByteString( 673 result =
791 seq_source_, position_, length); 674 factory()->InternalizeOneByteString(seq_source_, position_, length);
792 break; 675 break;
793 } 676 }
794 if (element != isolate()->heap()->the_hole_value() && 677 if (element != isolate()->heap()->the_hole_value() &&
795 String::cast(element)->IsOneByteEqualTo(string_vector)) { 678 String::cast(element)->IsOneByteEqualTo(string_vector)) {
796 result = Handle<String>(String::cast(element), isolate()); 679 result = Handle<String>(String::cast(element), isolate());
797 #ifdef DEBUG 680 #ifdef DEBUG
798 uint32_t hash_field = 681 uint32_t hash_field =
799 (hash << String::kHashShift) | String::kIsNotArrayIndexMask; 682 (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
800 DCHECK_EQ(static_cast<int>(result->Hash()), 683 DCHECK_EQ(static_cast<int>(result->Hash()),
801 static_cast<int>(hash_field >> String::kHashShift)); 684 static_cast<int>(hash_field >> String::kHashShift));
(...skipping 10 matching lines...) Expand all
812 695
813 int beg_pos = position_; 696 int beg_pos = position_;
814 // Fast case for Latin1 only without escape characters. 697 // Fast case for Latin1 only without escape characters.
815 do { 698 do {
816 // Check for control character (0x00-0x1f) or unterminated string (<0). 699 // Check for control character (0x00-0x1f) or unterminated string (<0).
817 if (c0_ < 0x20) return Handle<String>::null(); 700 if (c0_ < 0x20) return Handle<String>::null();
818 if (c0_ != '\\') { 701 if (c0_ != '\\') {
819 if (seq_one_byte || c0_ <= String::kMaxOneByteCharCode) { 702 if (seq_one_byte || c0_ <= String::kMaxOneByteCharCode) {
820 Advance(); 703 Advance();
821 } else { 704 } else {
822 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, 705 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, beg_pos,
823 beg_pos,
824 position_); 706 position_);
825 } 707 }
826 } else { 708 } else {
827 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, 709 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, beg_pos,
828 beg_pos,
829 position_); 710 position_);
830 } 711 }
831 } while (c0_ != '"'); 712 } while (c0_ != '"');
832 int length = position_ - beg_pos; 713 int length = position_ - beg_pos;
833 Handle<String> result = 714 Handle<String> result =
834 factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked(); 715 factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked();
835 uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); 716 uint8_t* dest = SeqOneByteString::cast(*result)->GetChars();
836 String::WriteToFlat(*source_, dest, beg_pos, position_); 717 String::WriteToFlat(*source_, dest, beg_pos, position_);
837 718
838 DCHECK_EQ('"', c0_); 719 DCHECK_EQ('"', c0_);
839 // Advance past the last '"'. 720 // Advance past the last '"'.
840 AdvanceSkipWhitespace(); 721 AdvanceSkipWhitespace();
841 return result; 722 return result;
842 } 723 }
843 724
725 // Explicit instantiation.
726 template class JsonParser<true>;
727 template class JsonParser<false>;
728
844 } // namespace internal 729 } // namespace internal
845 } // namespace v8 730 } // namespace v8
846
847 #endif // V8_JSON_PARSER_H_
OLDNEW
« no previous file with comments | « src/json-parser.h ('k') | src/json-stringifier.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698