blob: efd3c04b98f7e323a79076e5672df745c9611810 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch097c5b22016-05-18 11:27:45 +01005#ifndef V8_JSON_PARSER_H_
6#define V8_JSON_PARSER_H_
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007
8#include "src/char-predicates.h"
9#include "src/conversions.h"
10#include "src/debug/debug.h"
11#include "src/factory.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010012#include "src/field-type.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/messages.h"
14#include "src/parsing/scanner.h"
15#include "src/parsing/token.h"
16#include "src/transitions.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017
18namespace v8 {
19namespace internal {
20
21enum ParseElementResult { kElementFound, kElementNotFound, kNullHandle };
22
23
24// A simple json parser.
25template <bool seq_one_byte>
26class JsonParser BASE_EMBEDDED {
27 public:
28 MUST_USE_RESULT static MaybeHandle<Object> Parse(Handle<String> source) {
29 return JsonParser(source).ParseJson();
30 }
31
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 object_constructor_(isolate_->native_context()->object_function(),
41 isolate_),
42 position_(-1) {
43 source_ = String::Flatten(source_);
44 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
45
46 // Optimized fast case where we only have Latin1 characters.
47 if (seq_one_byte) {
48 seq_source_ = Handle<SeqOneByteString>::cast(source_);
49 }
50 }
51
52 // Parse a string containing a single JSON value.
53 MaybeHandle<Object> ParseJson();
54
55 inline void Advance() {
56 position_++;
57 if (position_ >= source_length_) {
58 c0_ = kEndOfString;
59 } else if (seq_one_byte) {
60 c0_ = seq_source_->SeqOneByteStringGet(position_);
61 } else {
62 c0_ = source_->Get(position_);
63 }
64 }
65
66 // The JSON lexical grammar is specified in the ECMAScript 5 standard,
67 // section 15.12.1.1. The only allowed whitespace characters between tokens
68 // are tab, carriage-return, newline and space.
69
70 inline void AdvanceSkipWhitespace() {
71 do {
72 Advance();
73 } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r');
74 }
75
76 inline void SkipWhitespace() {
77 while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') {
78 Advance();
79 }
80 }
81
82 inline uc32 AdvanceGetChar() {
83 Advance();
84 return c0_;
85 }
86
87 // Checks that current charater is c.
88 // If so, then consume c and skip whitespace.
89 inline bool MatchSkipWhiteSpace(uc32 c) {
90 if (c0_ == c) {
91 AdvanceSkipWhitespace();
92 return true;
93 }
94 return false;
95 }
96
97 // A JSON string (production JSONString) is subset of valid JavaScript string
98 // literals. The string must only be double-quoted (not single-quoted), and
99 // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and
100 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid.
101 Handle<String> ParseJsonString() {
102 return ScanJsonString<false>();
103 }
104
105 bool ParseJsonString(Handle<String> expected) {
106 int length = expected->length();
107 if (source_->length() - position_ - 1 > length) {
108 DisallowHeapAllocation no_gc;
109 String::FlatContent content = expected->GetFlatContent();
110 if (content.IsOneByte()) {
111 DCHECK_EQ('"', c0_);
112 const uint8_t* input_chars = seq_source_->GetChars() + position_ + 1;
113 const uint8_t* expected_chars = content.ToOneByteVector().start();
114 for (int i = 0; i < length; i++) {
115 uint8_t c0 = input_chars[i];
116 if (c0 != expected_chars[i] || c0 == '"' || c0 < 0x20 || c0 == '\\') {
117 return false;
118 }
119 }
120 if (input_chars[length] == '"') {
121 position_ = position_ + length + 1;
122 AdvanceSkipWhitespace();
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
130 Handle<String> ParseJsonInternalizedString() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100131 Handle<String> result = ScanJsonString<true>();
132 if (result.is_null()) return result;
133 return factory()->InternalizeString(result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 }
135
136 template <bool is_internalized>
137 Handle<String> ScanJsonString();
138 // Creates a new string and copies prefix[start..end] into the beginning
139 // of it. Then scans the rest of the string, adding characters after the
140 // prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char.
141 template <typename StringType, typename SinkChar>
142 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
143
144 // A JSON number (production JSONNumber) is a subset of the valid JavaScript
145 // decimal number literals.
146 // It includes an optional minus sign, must have at least one
147 // digit before and after a decimal point, may not have prefixed zeros (unless
148 // the integer part is zero), and may include an exponent part (e.g., "e-10").
149 // Hexadecimal and octal numbers are not allowed.
150 Handle<Object> ParseJsonNumber();
151
152 // Parse a single JSON value from input (grammar production JSONValue).
153 // A JSON value is either a (double-quoted) string literal, a number literal,
154 // one of "true", "false", or "null", or an object or array literal.
155 Handle<Object> ParseJsonValue();
156
157 // Parse a JSON object literal (grammar production JSONObject).
158 // An object literal is a squiggly-braced and comma separated sequence
159 // (possibly empty) of key/value pairs, where the key is a JSON string
160 // literal, the value is a JSON value, and the two are separated by a colon.
161 // A JSON array doesn't allow numbers and identifiers as keys, like a
162 // JavaScript array.
163 Handle<Object> ParseJsonObject();
164
165 // Helper for ParseJsonObject. Parses the form "123": obj, which is recorded
166 // as an element, not a property.
167 ParseElementResult ParseElement(Handle<JSObject> json_object);
168
169 // Parses a JSON array literal (grammar production JSONArray). An array
170 // literal is a square-bracketed and comma separated sequence (possibly empty)
171 // of JSON values.
172 // A JSON array doesn't allow leaving out values from the sequence, nor does
173 // it allow a terminal comma, like a JavaScript array does.
174 Handle<Object> ParseJsonArray();
175
176
177 // Mark that a parsing error has happened at the current token, and
178 // return a null handle. Primarily for readability.
179 inline Handle<Object> ReportUnexpectedCharacter() {
180 return Handle<Object>::null();
181 }
182
183 inline Isolate* isolate() { return isolate_; }
184 inline Factory* factory() { return factory_; }
185 inline Handle<JSFunction> object_constructor() { return object_constructor_; }
186
187 static const int kInitialSpecialStringLength = 32;
188 static const int kPretenureTreshold = 100 * 1024;
189
190
191 private:
192 Zone* zone() { return &zone_; }
193
194 void CommitStateToJsonObject(Handle<JSObject> json_object, Handle<Map> map,
195 ZoneList<Handle<Object> >* properties);
196
197 Handle<String> source_;
198 int source_length_;
199 Handle<SeqOneByteString> seq_source_;
200
201 PretenureFlag pretenure_;
202 Isolate* isolate_;
203 Factory* factory_;
204 Zone zone_;
205 Handle<JSFunction> object_constructor_;
206 uc32 c0_;
207 int position_;
208};
209
210template <bool seq_one_byte>
211MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
212 // Advance to the first character (possibly EOS)
213 AdvanceSkipWhitespace();
214 Handle<Object> result = ParseJsonValue();
215 if (result.is_null() || c0_ != kEndOfString) {
216 // Some exception (for example stack overflow) is already pending.
217 if (isolate_->has_pending_exception()) return Handle<Object>::null();
218
219 // Parse failed. Current character is the unexpected token.
220 Factory* factory = this->factory();
221 MessageTemplate::Template message;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100222 Handle<Object> arg1 = Handle<Smi>(Smi::FromInt(position_), isolate());
223 Handle<Object> arg2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224
225 switch (c0_) {
226 case kEndOfString:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100227 message = MessageTemplate::kJsonParseUnexpectedEOS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228 break;
229 case '-':
230 case '0':
231 case '1':
232 case '2':
233 case '3':
234 case '4':
235 case '5':
236 case '6':
237 case '7':
238 case '8':
239 case '9':
Ben Murdoch097c5b22016-05-18 11:27:45 +0100240 message = MessageTemplate::kJsonParseUnexpectedTokenNumber;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 break;
242 case '"':
Ben Murdoch097c5b22016-05-18 11:27:45 +0100243 message = MessageTemplate::kJsonParseUnexpectedTokenString;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 break;
245 default:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100246 message = MessageTemplate::kJsonParseUnexpectedToken;
247 arg2 = arg1;
248 arg1 = factory->LookupSingleCharacterStringFromCode(c0_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 break;
250 }
251
252 Handle<Script> script(factory->NewScript(source_));
253 // We should sent compile error event because we compile JSON object in
254 // separated source file.
255 isolate()->debug()->OnCompileError(script);
256 MessageLocation location(script, position_, position_ + 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100257 Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 return isolate()->template Throw<Object>(error, &location);
259 }
260 return result;
261}
262
263
264// Parse any JSON value.
265template <bool seq_one_byte>
266Handle<Object> JsonParser<seq_one_byte>::ParseJsonValue() {
267 StackLimitCheck stack_check(isolate_);
268 if (stack_check.HasOverflowed()) {
269 isolate_->StackOverflow();
270 return Handle<Object>::null();
271 }
272
273 if (stack_check.InterruptRequested()) {
274 ExecutionAccess access(isolate_);
275 // Avoid blocking GC in long running parser (v8:3974).
276 isolate_->stack_guard()->HandleGCInterrupt();
277 }
278
279 if (c0_ == '"') return ParseJsonString();
280 if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber();
281 if (c0_ == '{') return ParseJsonObject();
282 if (c0_ == '[') return ParseJsonArray();
283 if (c0_ == 'f') {
284 if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' &&
285 AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') {
286 AdvanceSkipWhitespace();
287 return factory()->false_value();
288 }
289 return ReportUnexpectedCharacter();
290 }
291 if (c0_ == 't') {
292 if (AdvanceGetChar() == 'r' && AdvanceGetChar() == 'u' &&
293 AdvanceGetChar() == 'e') {
294 AdvanceSkipWhitespace();
295 return factory()->true_value();
296 }
297 return ReportUnexpectedCharacter();
298 }
299 if (c0_ == 'n') {
300 if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' &&
301 AdvanceGetChar() == 'l') {
302 AdvanceSkipWhitespace();
303 return factory()->null_value();
304 }
305 return ReportUnexpectedCharacter();
306 }
307 return ReportUnexpectedCharacter();
308}
309
310
311template <bool seq_one_byte>
312ParseElementResult JsonParser<seq_one_byte>::ParseElement(
313 Handle<JSObject> json_object) {
314 uint32_t index = 0;
315 // Maybe an array index, try to parse it.
316 if (c0_ == '0') {
317 // With a leading zero, the string has to be "0" only to be an index.
318 Advance();
319 } else {
320 do {
321 int d = c0_ - '0';
322 if (index > 429496729U - ((d + 3) >> 3)) break;
323 index = (index * 10) + d;
324 Advance();
325 } while (IsDecimalDigit(c0_));
326 }
327
328 if (c0_ == '"') {
329 // Successfully parsed index, parse and store element.
330 AdvanceSkipWhitespace();
331
332 if (c0_ == ':') {
333 AdvanceSkipWhitespace();
334 Handle<Object> value = ParseJsonValue();
335 if (!value.is_null()) {
336 JSObject::SetOwnElementIgnoreAttributes(json_object, index, value, NONE)
337 .Assert();
338 return kElementFound;
339 } else {
340 return kNullHandle;
341 }
342 }
343 }
344 return kElementNotFound;
345}
346
347// Parse a JSON object. Position must be right at '{'.
348template <bool seq_one_byte>
349Handle<Object> JsonParser<seq_one_byte>::ParseJsonObject() {
350 HandleScope scope(isolate());
351 Handle<JSObject> json_object =
352 factory()->NewJSObject(object_constructor(), pretenure_);
353 Handle<Map> map(json_object->map());
354 int descriptor = 0;
355 ZoneList<Handle<Object> > properties(8, zone());
356 DCHECK_EQ(c0_, '{');
357
358 bool transitioning = true;
359
360 AdvanceSkipWhitespace();
361 if (c0_ != '}') {
362 do {
363 if (c0_ != '"') return ReportUnexpectedCharacter();
364
365 int start_position = position_;
366 Advance();
367
368 if (IsDecimalDigit(c0_)) {
369 ParseElementResult element_result = ParseElement(json_object);
370 if (element_result == kNullHandle) return Handle<Object>::null();
371 if (element_result == kElementFound) continue;
372 }
373 // Not an index, fallback to the slow path.
374
375 position_ = start_position;
376#ifdef DEBUG
377 c0_ = '"';
378#endif
379
380 Handle<String> key;
381 Handle<Object> value;
382
383 // Try to follow existing transitions as long as possible. Once we stop
384 // transitioning, no transition can be found anymore.
385 DCHECK(transitioning);
386 // First check whether there is a single expected transition. If so, try
387 // to parse it first.
388 bool follow_expected = false;
389 Handle<Map> target;
390 if (seq_one_byte) {
391 key = TransitionArray::ExpectedTransitionKey(map);
392 follow_expected = !key.is_null() && ParseJsonString(key);
393 }
394 // If the expected transition hits, follow it.
395 if (follow_expected) {
396 target = TransitionArray::ExpectedTransitionTarget(map);
397 } else {
398 // If the expected transition failed, parse an internalized string and
399 // try to find a matching transition.
400 key = ParseJsonInternalizedString();
401 if (key.is_null()) return ReportUnexpectedCharacter();
402
403 target = TransitionArray::FindTransitionToField(map, key);
404 // If a transition was found, follow it and continue.
405 transitioning = !target.is_null();
406 }
407 if (c0_ != ':') return ReportUnexpectedCharacter();
408
409 AdvanceSkipWhitespace();
410 value = ParseJsonValue();
411 if (value.is_null()) return ReportUnexpectedCharacter();
412
413 if (transitioning) {
414 PropertyDetails details =
415 target->instance_descriptors()->GetDetails(descriptor);
416 Representation expected_representation = details.representation();
417
418 if (value->FitsRepresentation(expected_representation)) {
419 if (expected_representation.IsHeapObject() &&
420 !target->instance_descriptors()
421 ->GetFieldType(descriptor)
422 ->NowContains(value)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100423 Handle<FieldType> value_type(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424 value->OptimalType(isolate(), expected_representation));
425 Map::GeneralizeFieldType(target, descriptor,
426 expected_representation, value_type);
427 }
428 DCHECK(target->instance_descriptors()
429 ->GetFieldType(descriptor)
430 ->NowContains(value));
431 properties.Add(value, zone());
432 map = target;
433 descriptor++;
434 continue;
435 } else {
436 transitioning = false;
437 }
438 }
439
440 DCHECK(!transitioning);
441
442 // Commit the intermediate state to the object and stop transitioning.
443 CommitStateToJsonObject(json_object, map, &properties);
444
445 JSObject::DefinePropertyOrElementIgnoreAttributes(json_object, key, value)
446 .Check();
447 } while (transitioning && MatchSkipWhiteSpace(','));
448
449 // If we transitioned until the very end, transition the map now.
450 if (transitioning) {
451 CommitStateToJsonObject(json_object, map, &properties);
452 } else {
453 while (MatchSkipWhiteSpace(',')) {
454 HandleScope local_scope(isolate());
455 if (c0_ != '"') return ReportUnexpectedCharacter();
456
457 int start_position = position_;
458 Advance();
459
460 if (IsDecimalDigit(c0_)) {
461 ParseElementResult element_result = ParseElement(json_object);
462 if (element_result == kNullHandle) return Handle<Object>::null();
463 if (element_result == kElementFound) continue;
464 }
465 // Not an index, fallback to the slow path.
466
467 position_ = start_position;
468#ifdef DEBUG
469 c0_ = '"';
470#endif
471
472 Handle<String> key;
473 Handle<Object> value;
474
475 key = ParseJsonInternalizedString();
476 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter();
477
478 AdvanceSkipWhitespace();
479 value = ParseJsonValue();
480 if (value.is_null()) return ReportUnexpectedCharacter();
481
482 JSObject::DefinePropertyOrElementIgnoreAttributes(json_object, key,
483 value).Check();
484 }
485 }
486
487 if (c0_ != '}') {
488 return ReportUnexpectedCharacter();
489 }
490 }
491 AdvanceSkipWhitespace();
492 return scope.CloseAndEscape(json_object);
493}
494
495
496template <bool seq_one_byte>
497void JsonParser<seq_one_byte>::CommitStateToJsonObject(
498 Handle<JSObject> json_object, Handle<Map> map,
499 ZoneList<Handle<Object> >* properties) {
500 JSObject::AllocateStorageForMap(json_object, map);
501 DCHECK(!json_object->map()->is_dictionary_map());
502
503 DisallowHeapAllocation no_gc;
504
505 int length = properties->length();
506 for (int i = 0; i < length; i++) {
507 Handle<Object> value = (*properties)[i];
508 json_object->WriteToField(i, *value);
509 }
510}
511
512
513// Parse a JSON array. Position must be right at '['.
514template <bool seq_one_byte>
515Handle<Object> JsonParser<seq_one_byte>::ParseJsonArray() {
516 HandleScope scope(isolate());
517 ZoneList<Handle<Object> > elements(4, zone());
518 DCHECK_EQ(c0_, '[');
519
520 AdvanceSkipWhitespace();
521 if (c0_ != ']') {
522 do {
523 Handle<Object> element = ParseJsonValue();
524 if (element.is_null()) return ReportUnexpectedCharacter();
525 elements.Add(element, zone());
526 } while (MatchSkipWhiteSpace(','));
527 if (c0_ != ']') {
528 return ReportUnexpectedCharacter();
529 }
530 }
531 AdvanceSkipWhitespace();
532 // Allocate a fixed array with all the elements.
533 Handle<FixedArray> fast_elements =
534 factory()->NewFixedArray(elements.length(), pretenure_);
535 for (int i = 0, n = elements.length(); i < n; i++) {
536 fast_elements->set(i, *elements[i]);
537 }
538 Handle<Object> json_array = factory()->NewJSArrayWithElements(
539 fast_elements, FAST_ELEMENTS, Strength::WEAK, pretenure_);
540 return scope.CloseAndEscape(json_array);
541}
542
543
544template <bool seq_one_byte>
545Handle<Object> JsonParser<seq_one_byte>::ParseJsonNumber() {
546 bool negative = false;
547 int beg_pos = position_;
548 if (c0_ == '-') {
549 Advance();
550 negative = true;
551 }
552 if (c0_ == '0') {
553 Advance();
554 // Prefix zero is only allowed if it's the only digit before
555 // a decimal point or exponent.
556 if (IsDecimalDigit(c0_)) return ReportUnexpectedCharacter();
557 } else {
558 int i = 0;
559 int digits = 0;
560 if (c0_ < '1' || c0_ > '9') return ReportUnexpectedCharacter();
561 do {
562 i = i * 10 + c0_ - '0';
563 digits++;
564 Advance();
565 } while (IsDecimalDigit(c0_));
566 if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) {
567 SkipWhitespace();
568 return Handle<Smi>(Smi::FromInt((negative ? -i : i)), isolate());
569 }
570 }
571 if (c0_ == '.') {
572 Advance();
573 if (!IsDecimalDigit(c0_)) return ReportUnexpectedCharacter();
574 do {
575 Advance();
576 } while (IsDecimalDigit(c0_));
577 }
578 if (AsciiAlphaToLower(c0_) == 'e') {
579 Advance();
580 if (c0_ == '-' || c0_ == '+') Advance();
581 if (!IsDecimalDigit(c0_)) return ReportUnexpectedCharacter();
582 do {
583 Advance();
584 } while (IsDecimalDigit(c0_));
585 }
586 int length = position_ - beg_pos;
587 double number;
588 if (seq_one_byte) {
589 Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length);
590 number = StringToDouble(isolate()->unicode_cache(), chars,
591 NO_FLAGS, // Hex, octal or trailing junk.
592 std::numeric_limits<double>::quiet_NaN());
593 } else {
594 Vector<uint8_t> buffer = Vector<uint8_t>::New(length);
595 String::WriteToFlat(*source_, buffer.start(), beg_pos, position_);
596 Vector<const uint8_t> result =
597 Vector<const uint8_t>(buffer.start(), length);
598 number = StringToDouble(isolate()->unicode_cache(),
599 result,
600 NO_FLAGS, // Hex, octal or trailing junk.
601 0.0);
602 buffer.Dispose();
603 }
604 SkipWhitespace();
605 return factory()->NewNumber(number, pretenure_);
606}
607
608
609template <typename StringType>
610inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c);
611
612template <>
613inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) {
614 seq_str->SeqTwoByteStringSet(i, c);
615}
616
617template <>
618inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) {
619 seq_str->SeqOneByteStringSet(i, c);
620}
621
622template <typename StringType>
623inline Handle<StringType> NewRawString(Factory* factory,
624 int length,
625 PretenureFlag pretenure);
626
627template <>
628inline Handle<SeqTwoByteString> NewRawString(Factory* factory,
629 int length,
630 PretenureFlag pretenure) {
631 return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked();
632}
633
634template <>
635inline Handle<SeqOneByteString> NewRawString(Factory* factory,
636 int length,
637 PretenureFlag pretenure) {
638 return factory->NewRawOneByteString(length, pretenure).ToHandleChecked();
639}
640
641
642// Scans the rest of a JSON string starting from position_ and writes
643// prefix[start..end] along with the scanned characters into a
644// sequential string of type StringType.
645template <bool seq_one_byte>
646template <typename StringType, typename SinkChar>
647Handle<String> JsonParser<seq_one_byte>::SlowScanJsonString(
648 Handle<String> prefix, int start, int end) {
649 int count = end - start;
650 int max_length = count + source_length_ - position_;
651 int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count));
652 Handle<StringType> seq_string =
653 NewRawString<StringType>(factory(), length, pretenure_);
654 // Copy prefix into seq_str.
655 SinkChar* dest = seq_string->GetChars();
656 String::WriteToFlat(*prefix, dest, start, end);
657
658 while (c0_ != '"') {
659 // Check for control character (0x00-0x1f) or unterminated string (<0).
660 if (c0_ < 0x20) return Handle<String>::null();
661 if (count >= length) {
662 // We need to create a longer sequential string for the result.
663 return SlowScanJsonString<StringType, SinkChar>(seq_string, 0, count);
664 }
665 if (c0_ != '\\') {
666 // If the sink can contain UC16 characters, or source_ contains only
667 // Latin1 characters, there's no need to test whether we can store the
668 // character. Otherwise check whether the UC16 source character can fit
669 // in the Latin1 sink.
670 if (sizeof(SinkChar) == kUC16Size || seq_one_byte ||
671 c0_ <= String::kMaxOneByteCharCode) {
672 SeqStringSet(seq_string, count++, c0_);
673 Advance();
674 } else {
675 // StringType is SeqOneByteString and we just read a non-Latin1 char.
676 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string, 0, count);
677 }
678 } else {
679 Advance(); // Advance past the \.
680 switch (c0_) {
681 case '"':
682 case '\\':
683 case '/':
684 SeqStringSet(seq_string, count++, c0_);
685 break;
686 case 'b':
687 SeqStringSet(seq_string, count++, '\x08');
688 break;
689 case 'f':
690 SeqStringSet(seq_string, count++, '\x0c');
691 break;
692 case 'n':
693 SeqStringSet(seq_string, count++, '\x0a');
694 break;
695 case 'r':
696 SeqStringSet(seq_string, count++, '\x0d');
697 break;
698 case 't':
699 SeqStringSet(seq_string, count++, '\x09');
700 break;
701 case 'u': {
702 uc32 value = 0;
703 for (int i = 0; i < 4; i++) {
704 Advance();
705 int digit = HexValue(c0_);
706 if (digit < 0) {
707 return Handle<String>::null();
708 }
709 value = value * 16 + digit;
710 }
711 if (sizeof(SinkChar) == kUC16Size ||
712 value <= String::kMaxOneByteCharCode) {
713 SeqStringSet(seq_string, count++, value);
714 break;
715 } else {
716 // StringType is SeqOneByteString and we just read a non-Latin1
717 // char.
718 position_ -= 6; // Rewind position_ to \ in \uxxxx.
719 Advance();
720 return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string,
721 0,
722 count);
723 }
724 }
725 default:
726 return Handle<String>::null();
727 }
728 Advance();
729 }
730 }
731
732 DCHECK_EQ('"', c0_);
733 // Advance past the last '"'.
734 AdvanceSkipWhitespace();
735
736 // Shrink seq_string length to count and return.
737 return SeqString::Truncate(seq_string, count);
738}
739
740
741template <bool seq_one_byte>
742template <bool is_internalized>
743Handle<String> JsonParser<seq_one_byte>::ScanJsonString() {
744 DCHECK_EQ('"', c0_);
745 Advance();
746 if (c0_ == '"') {
747 AdvanceSkipWhitespace();
748 return factory()->empty_string();
749 }
750
751 if (seq_one_byte && is_internalized) {
752 // Fast path for existing internalized strings. If the the string being
753 // parsed is not a known internalized string, contains backslashes or
754 // unexpectedly reaches the end of string, return with an empty handle.
755 uint32_t running_hash = isolate()->heap()->HashSeed();
756 int position = position_;
757 uc32 c0 = c0_;
758 do {
759 if (c0 == '\\') {
760 c0_ = c0;
761 int beg_pos = position_;
762 position_ = position;
763 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
764 beg_pos,
765 position_);
766 }
767 if (c0 < 0x20) return Handle<String>::null();
768 running_hash = StringHasher::AddCharacterCore(running_hash,
769 static_cast<uint16_t>(c0));
770 position++;
771 if (position >= source_length_) return Handle<String>::null();
772 c0 = seq_source_->SeqOneByteStringGet(position);
773 } while (c0 != '"');
774 int length = position - position_;
775 uint32_t hash = (length <= String::kMaxHashCalcLength)
776 ? StringHasher::GetHashCore(running_hash)
777 : static_cast<uint32_t>(length);
778 Vector<const uint8_t> string_vector(
779 seq_source_->GetChars() + position_, length);
780 StringTable* string_table = isolate()->heap()->string_table();
781 uint32_t capacity = string_table->Capacity();
782 uint32_t entry = StringTable::FirstProbe(hash, capacity);
783 uint32_t count = 1;
784 Handle<String> result;
785 while (true) {
786 Object* element = string_table->KeyAt(entry);
787 if (element == isolate()->heap()->undefined_value()) {
788 // Lookup failure.
789 result = factory()->InternalizeOneByteString(
790 seq_source_, position_, length);
791 break;
792 }
793 if (element != isolate()->heap()->the_hole_value() &&
794 String::cast(element)->IsOneByteEqualTo(string_vector)) {
795 result = Handle<String>(String::cast(element), isolate());
796#ifdef DEBUG
797 uint32_t hash_field =
798 (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
799 DCHECK_EQ(static_cast<int>(result->Hash()),
800 static_cast<int>(hash_field >> String::kHashShift));
801#endif
802 break;
803 }
804 entry = StringTable::NextProbe(entry, count++, capacity);
805 }
806 position_ = position;
807 // Advance past the last '"'.
808 AdvanceSkipWhitespace();
809 return result;
810 }
811
812 int beg_pos = position_;
813 // Fast case for Latin1 only without escape characters.
814 do {
815 // Check for control character (0x00-0x1f) or unterminated string (<0).
816 if (c0_ < 0x20) return Handle<String>::null();
817 if (c0_ != '\\') {
818 if (seq_one_byte || c0_ <= String::kMaxOneByteCharCode) {
819 Advance();
820 } else {
821 return SlowScanJsonString<SeqTwoByteString, uc16>(source_,
822 beg_pos,
823 position_);
824 }
825 } else {
826 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
827 beg_pos,
828 position_);
829 }
830 } while (c0_ != '"');
831 int length = position_ - beg_pos;
832 Handle<String> result =
833 factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked();
834 uint8_t* dest = SeqOneByteString::cast(*result)->GetChars();
835 String::WriteToFlat(*source_, dest, beg_pos, position_);
836
837 DCHECK_EQ('"', c0_);
838 // Advance past the last '"'.
839 AdvanceSkipWhitespace();
840 return result;
841}
842
843} // namespace internal
844} // namespace v8
845
Ben Murdoch097c5b22016-05-18 11:27:45 +0100846#endif // V8_JSON_PARSER_H_