v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
json-parser.h
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_JSON_PARSER_H_
29 #define V8_JSON_PARSER_H_
30 
31 #include "v8.h"
32 
33 #include "char-predicates-inl.h"
34 #include "v8conversions.h"
35 #include "messages.h"
36 #include "spaces-inl.h"
37 #include "token.h"
38 
39 namespace v8 {
40 namespace internal {
41 
42 // A simple json parser.
43 template <bool seq_ascii>
44 class JsonParser BASE_EMBEDDED {
45  public:
47  return JsonParser(source).ParseJson();
48  }
49 
50  static const int kEndOfString = -1;
51 
52  private:
53  explicit JsonParser(Handle<String> source)
54  : source_(source),
55  source_length_(source->length()),
56  isolate_(source->map()->GetHeap()->isolate()),
57  factory_(isolate_->factory()),
58  zone_(isolate_),
59  object_constructor_(isolate_->native_context()->object_function(),
60  isolate_),
61  position_(-1) {
62  FlattenString(source_);
63  pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
64 
65  // Optimized fast case where we only have ASCII characters.
66  if (seq_ascii) {
67  seq_source_ = Handle<SeqOneByteString>::cast(source_);
68  }
69  }
70 
71  // Parse a string containing a single JSON value.
72  Handle<Object> ParseJson();
73 
74  inline void Advance() {
75  position_++;
76  if (position_ >= source_length_) {
77  c0_ = kEndOfString;
78  } else if (seq_ascii) {
79  c0_ = seq_source_->SeqOneByteStringGet(position_);
80  } else {
81  c0_ = source_->Get(position_);
82  }
83  }
84 
85  // The JSON lexical grammar is specified in the ECMAScript 5 standard,
86  // section 15.12.1.1. The only allowed whitespace characters between tokens
87  // are tab, carriage-return, newline and space.
88 
89  inline void AdvanceSkipWhitespace() {
90  do {
91  Advance();
92  } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r');
93  }
94 
95  inline void SkipWhitespace() {
96  while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') {
97  Advance();
98  }
99  }
100 
101  inline uc32 AdvanceGetChar() {
102  Advance();
103  return c0_;
104  }
105 
106  // Checks that current charater is c.
107  // If so, then consume c and skip whitespace.
108  inline bool MatchSkipWhiteSpace(uc32 c) {
109  if (c0_ == c) {
110  AdvanceSkipWhitespace();
111  return true;
112  }
113  return false;
114  }
115 
116  // A JSON string (production JSONString) is subset of valid JavaScript string
117  // literals. The string must only be double-quoted (not single-quoted), and
118  // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and
119  // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid.
120  Handle<String> ParseJsonString() {
121  return ScanJsonString<false>();
122  }
123 
124  bool ParseJsonString(Handle<String> expected) {
125  int length = expected->length();
126  if (source_->length() - position_ - 1 > length) {
128  String::FlatContent content = expected->GetFlatContent();
129  if (content.IsAscii()) {
130  ASSERT_EQ('"', c0_);
131  const uint8_t* input_chars = seq_source_->GetChars() + position_ + 1;
132  const uint8_t* expected_chars = content.ToOneByteVector().start();
133  for (int i = 0; i < length; i++) {
134  uint8_t c0 = input_chars[i];
135  if (c0 != expected_chars[i] ||
136  c0 == '"' || c0 < 0x20 || c0 == '\\') {
137  return false;
138  }
139  }
140  if (input_chars[length] == '"') {
141  position_ = position_ + length + 1;
142  AdvanceSkipWhitespace();
143  return true;
144  }
145  }
146  }
147  return false;
148  }
149 
150  Handle<String> ParseJsonInternalizedString() {
151  return ScanJsonString<true>();
152  }
153 
154  template <bool is_internalized>
155  Handle<String> ScanJsonString();
156  // Creates a new string and copies prefix[start..end] into the beginning
157  // of it. Then scans the rest of the string, adding characters after the
158  // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char.
159  template <typename StringType, typename SinkChar>
160  Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
161 
162  // A JSON number (production JSONNumber) is a subset of the valid JavaScript
163  // decimal number literals.
164  // It includes an optional minus sign, must have at least one
165  // digit before and after a decimal point, may not have prefixed zeros (unless
166  // the integer part is zero), and may include an exponent part (e.g., "e-10").
167  // Hexadecimal and octal numbers are not allowed.
168  Handle<Object> ParseJsonNumber();
169 
170  // Parse a single JSON value from input (grammar production JSONValue).
171  // A JSON value is either a (double-quoted) string literal, a number literal,
172  // one of "true", "false", or "null", or an object or array literal.
173  Handle<Object> ParseJsonValue();
174 
175  // Parse a JSON object literal (grammar production JSONObject).
176  // An object literal is a squiggly-braced and comma separated sequence
177  // (possibly empty) of key/value pairs, where the key is a JSON string
178  // literal, the value is a JSON value, and the two are separated by a colon.
179  // A JSON array doesn't allow numbers and identifiers as keys, like a
180  // JavaScript array.
181  Handle<Object> ParseJsonObject();
182 
183  // Parses a JSON array literal (grammar production JSONArray). An array
184  // literal is a square-bracketed and comma separated sequence (possibly empty)
185  // of JSON values.
186  // A JSON array doesn't allow leaving out values from the sequence, nor does
187  // it allow a terminal comma, like a JavaScript array does.
188  Handle<Object> ParseJsonArray();
189 
190 
191  // Mark that a parsing error has happened at the current token, and
192  // return a null handle. Primarily for readability.
193  inline Handle<Object> ReportUnexpectedCharacter() {
194  return Handle<Object>::null();
195  }
196 
197  inline Isolate* isolate() { return isolate_; }
198  inline Factory* factory() { return factory_; }
199  inline Handle<JSFunction> object_constructor() { return object_constructor_; }
200 
201  static const int kInitialSpecialStringLength = 1024;
202  static const int kPretenureTreshold = 100 * 1024;
203 
204 
205  private:
206  Zone* zone() { return &zone_; }
207 
208  Handle<String> source_;
209  int source_length_;
210  Handle<SeqOneByteString> seq_source_;
211 
212  PretenureFlag pretenure_;
213  Isolate* isolate_;
214  Factory* factory_;
215  Zone zone_;
216  Handle<JSFunction> object_constructor_;
217  uc32 c0_;
218  int position_;
219 };
220 
221 template <bool seq_ascii>
222 Handle<Object> JsonParser<seq_ascii>::ParseJson() {
223  // Advance to the first character (possibly EOS)
224  AdvanceSkipWhitespace();
225  Handle<Object> result = ParseJsonValue();
226  if (result.is_null() || c0_ != kEndOfString) {
227  // Some exception (for example stack overflow) is already pending.
228  if (isolate_->has_pending_exception()) return Handle<Object>::null();
229 
230  // Parse failed. Current character is the unexpected token.
231  const char* message;
232  Factory* factory = this->factory();
233  Handle<JSArray> array;
234 
235  switch (c0_) {
236  case kEndOfString:
237  message = "unexpected_eos";
238  array = factory->NewJSArray(0);
239  break;
240  case '-':
241  case '0':
242  case '1':
243  case '2':
244  case '3':
245  case '4':
246  case '5':
247  case '6':
248  case '7':
249  case '8':
250  case '9':
251  message = "unexpected_token_number";
252  array = factory->NewJSArray(0);
253  break;
254  case '"':
255  message = "unexpected_token_string";
256  array = factory->NewJSArray(0);
257  break;
258  default:
259  message = "unexpected_token";
260  Handle<Object> name =
262  Handle<FixedArray> element = factory->NewFixedArray(1);
263  element->set(0, *name);
264  array = factory->NewJSArrayWithElements(element);
265  break;
266  }
267 
268  MessageLocation location(factory->NewScript(source_),
269  position_,
270  position_ + 1);
271  Handle<Object> result = factory->NewSyntaxError(message, array);
272  isolate()->Throw(*result, &location);
273  return Handle<Object>::null();
274  }
275  return result;
276 }
277 
278 
279 // Parse any JSON value.
280 template <bool seq_ascii>
281 Handle<Object> JsonParser<seq_ascii>::ParseJsonValue() {
282  StackLimitCheck stack_check(isolate_);
283  if (stack_check.HasOverflowed()) {
284  isolate_->StackOverflow();
285  return Handle<Object>::null();
286  }
287 
288  if (c0_ == '"') return ParseJsonString();
289  if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber();
290  if (c0_ == '{') return ParseJsonObject();
291  if (c0_ == '[') return ParseJsonArray();
292  if (c0_ == 'f') {
293  if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' &&
294  AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') {
295  AdvanceSkipWhitespace();
296  return factory()->false_value();
297  }
298  return ReportUnexpectedCharacter();
299  }
300  if (c0_ == 't') {
301  if (AdvanceGetChar() == 'r' && AdvanceGetChar() == 'u' &&
302  AdvanceGetChar() == 'e') {
303  AdvanceSkipWhitespace();
304  return factory()->true_value();
305  }
306  return ReportUnexpectedCharacter();
307  }
308  if (c0_ == 'n') {
309  if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' &&
310  AdvanceGetChar() == 'l') {
311  AdvanceSkipWhitespace();
312  return factory()->null_value();
313  }
314  return ReportUnexpectedCharacter();
315  }
316  return ReportUnexpectedCharacter();
317 }
318 
319 
320 // Parse a JSON object. Position must be right at '{'.
321 template <bool seq_ascii>
322 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() {
323  HandleScope scope(isolate());
324  Handle<JSObject> json_object =
325  factory()->NewJSObject(object_constructor(), pretenure_);
326  Handle<Map> map(json_object->map());
327  ZoneList<Handle<Object> > properties(8, zone());
328  ASSERT_EQ(c0_, '{');
329 
330  bool transitioning = true;
331 
332  AdvanceSkipWhitespace();
333  if (c0_ != '}') {
334  do {
335  if (c0_ != '"') return ReportUnexpectedCharacter();
336 
337  int start_position = position_;
338  Advance();
339 
340  uint32_t index = 0;
341  if (c0_ >= '0' && c0_ <= '9') {
342  // Maybe an array index, try to parse it.
343  if (c0_ == '0') {
344  // With a leading zero, the string has to be "0" only to be an index.
345  Advance();
346  } else {
347  do {
348  int d = c0_ - '0';
349  if (index > 429496729U - ((d > 5) ? 1 : 0)) break;
350  index = (index * 10) + d;
351  Advance();
352  } while (c0_ >= '0' && c0_ <= '9');
353  }
354 
355  if (c0_ == '"') {
356  // Successfully parsed index, parse and store element.
357  AdvanceSkipWhitespace();
358 
359  if (c0_ != ':') return ReportUnexpectedCharacter();
360  AdvanceSkipWhitespace();
361  Handle<Object> value = ParseJsonValue();
362  if (value.is_null()) return ReportUnexpectedCharacter();
363 
364  JSObject::SetOwnElement(json_object, index, value, SLOPPY);
365  continue;
366  }
367  // Not an index, fallback to the slow path.
368  }
369 
370  position_ = start_position;
371 #ifdef DEBUG
372  c0_ = '"';
373 #endif
374 
375  Handle<String> key;
376  Handle<Object> value;
377 
378  // Try to follow existing transitions as long as possible. Once we stop
379  // transitioning, no transition can be found anymore.
380  if (transitioning) {
381  // First check whether there is a single expected transition. If so, try
382  // to parse it first.
383  bool follow_expected = false;
384  Handle<Map> target;
385  if (seq_ascii) {
387  follow_expected = !key.is_null() && ParseJsonString(key);
388  }
389  // If the expected transition hits, follow it.
390  if (follow_expected) {
392  } else {
393  // If the expected transition failed, parse an internalized string and
394  // try to find a matching transition.
395  key = ParseJsonInternalizedString();
396  if (key.is_null()) return ReportUnexpectedCharacter();
397 
398  target = JSObject::FindTransitionToField(map, key);
399  // If a transition was found, follow it and continue.
400  transitioning = !target.is_null();
401  }
402  if (c0_ != ':') return ReportUnexpectedCharacter();
403 
404  AdvanceSkipWhitespace();
405  value = ParseJsonValue();
406  if (value.is_null()) return ReportUnexpectedCharacter();
407 
408  if (transitioning) {
409  int descriptor = map->NumberOfOwnDescriptors();
410  PropertyDetails details =
411  target->instance_descriptors()->GetDetails(descriptor);
412  Representation expected_representation = details.representation();
413 
414  if (value->FitsRepresentation(expected_representation)) {
415  // If the target representation is double and the value is already
416  // double, use the existing box.
417  if (value->IsSmi() && expected_representation.IsDouble()) {
418  value = factory()->NewHeapNumber(
419  Handle<Smi>::cast(value)->value());
420  }
421  properties.Add(value, zone());
422  map = target;
423  continue;
424  } else {
425  transitioning = false;
426  }
427  }
428 
429  // Commit the intermediate state to the object and stop transitioning.
430  JSObject::AllocateStorageForMap(json_object, map);
431  int length = properties.length();
432  for (int i = 0; i < length; i++) {
433  Handle<Object> value = properties[i];
434  json_object->FastPropertyAtPut(i, *value);
435  }
436  } else {
437  key = ParseJsonInternalizedString();
438  if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter();
439 
440  AdvanceSkipWhitespace();
441  value = ParseJsonValue();
442  if (value.is_null()) return ReportUnexpectedCharacter();
443  }
444 
446  json_object, key, value, NONE);
447  } while (MatchSkipWhiteSpace(','));
448  if (c0_ != '}') {
449  return ReportUnexpectedCharacter();
450  }
451 
452  // If we transitioned until the very end, transition the map now.
453  if (transitioning) {
454  JSObject::AllocateStorageForMap(json_object, map);
455  int length = properties.length();
456  for (int i = 0; i < length; i++) {
457  Handle<Object> value = properties[i];
458  json_object->FastPropertyAtPut(i, *value);
459  }
460  }
461  }
462  AdvanceSkipWhitespace();
463  return scope.CloseAndEscape(json_object);
464 }
465 
466 // Parse a JSON array. Position must be right at '['.
467 template <bool seq_ascii>
468 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() {
469  HandleScope scope(isolate());
470  ZoneList<Handle<Object> > elements(4, zone());
471  ASSERT_EQ(c0_, '[');
472 
473  AdvanceSkipWhitespace();
474  if (c0_ != ']') {
475  do {
476  Handle<Object> element = ParseJsonValue();
477  if (element.is_null()) return ReportUnexpectedCharacter();
478  elements.Add(element, zone());
479  } while (MatchSkipWhiteSpace(','));
480  if (c0_ != ']') {
481  return ReportUnexpectedCharacter();
482  }
483  }
484  AdvanceSkipWhitespace();
485  // Allocate a fixed array with all the elements.
486  Handle<FixedArray> fast_elements =
487  factory()->NewFixedArray(elements.length(), pretenure_);
488  for (int i = 0, n = elements.length(); i < n; i++) {
489  fast_elements->set(i, *elements[i]);
490  }
491  Handle<Object> json_array = factory()->NewJSArrayWithElements(
492  fast_elements, FAST_ELEMENTS, pretenure_);
493  return scope.CloseAndEscape(json_array);
494 }
495 
496 
497 template <bool seq_ascii>
498 Handle<Object> JsonParser<seq_ascii>::ParseJsonNumber() {
499  bool negative = false;
500  int beg_pos = position_;
501  if (c0_ == '-') {
502  Advance();
503  negative = true;
504  }
505  if (c0_ == '0') {
506  Advance();
507  // Prefix zero is only allowed if it's the only digit before
508  // a decimal point or exponent.
509  if ('0' <= c0_ && c0_ <= '9') return ReportUnexpectedCharacter();
510  } else {
511  int i = 0;
512  int digits = 0;
513  if (c0_ < '1' || c0_ > '9') return ReportUnexpectedCharacter();
514  do {
515  i = i * 10 + c0_ - '0';
516  digits++;
517  Advance();
518  } while (c0_ >= '0' && c0_ <= '9');
519  if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) {
520  SkipWhitespace();
521  return Handle<Smi>(Smi::FromInt((negative ? -i : i)), isolate());
522  }
523  }
524  if (c0_ == '.') {
525  Advance();
526  if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter();
527  do {
528  Advance();
529  } while (c0_ >= '0' && c0_ <= '9');
530  }
531  if (AsciiAlphaToLower(c0_) == 'e') {
532  Advance();
533  if (c0_ == '-' || c0_ == '+') Advance();
534  if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter();
535  do {
536  Advance();
537  } while (c0_ >= '0' && c0_ <= '9');
538  }
539  int length = position_ - beg_pos;
540  double number;
541  if (seq_ascii) {
542  Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length);
543  number = StringToDouble(isolate()->unicode_cache(),
545  NO_FLAGS, // Hex, octal or trailing junk.
546  OS::nan_value());
547  } else {
548  Vector<uint8_t> buffer = Vector<uint8_t>::New(length);
549  String::WriteToFlat(*source_, buffer.start(), beg_pos, position_);
550  Vector<const uint8_t> result =
551  Vector<const uint8_t>(buffer.start(), length);
552  number = StringToDouble(isolate()->unicode_cache(),
553  // TODO(dcarney): Convert StringToDouble to uint_t.
554  Vector<const char>::cast(result),
555  NO_FLAGS, // Hex, octal or trailing junk.
556  0.0);
557  buffer.Dispose();
558  }
559  SkipWhitespace();
560  return factory()->NewNumber(number, pretenure_);
561 }
562 
563 
564 template <typename StringType>
565 inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c);
566 
567 template <>
568 inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) {
569  seq_str->SeqTwoByteStringSet(i, c);
570 }
571 
572 template <>
573 inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) {
574  seq_str->SeqOneByteStringSet(i, c);
575 }
576 
577 template <typename StringType>
578 inline Handle<StringType> NewRawString(Factory* factory,
579  int length,
580  PretenureFlag pretenure);
581 
582 template <>
584  int length,
585  PretenureFlag pretenure) {
586  return factory->NewRawTwoByteString(length, pretenure);
587 }
588 
589 template <>
591  int length,
592  PretenureFlag pretenure) {
593  return factory->NewRawOneByteString(length, pretenure);
594 }
595 
596 
597 // Scans the rest of a JSON string starting from position_ and writes
598 // prefix[start..end] along with the scanned characters into a
599 // sequential string of type StringType.
600 template <bool seq_ascii>
601 template <typename StringType, typename SinkChar>
602 Handle<String> JsonParser<seq_ascii>::SlowScanJsonString(
603  Handle<String> prefix, int start, int end) {
604  int count = end - start;
605  int max_length = count + source_length_ - position_;
606  int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count));
607  Handle<StringType> seq_string =
608  NewRawString<StringType>(factory(), length, pretenure_);
609  ASSERT(!seq_string.is_null());
610  // Copy prefix into seq_str.
611  SinkChar* dest = seq_string->GetChars();
612  String::WriteToFlat(*prefix, dest, start, end);
613 
614  while (c0_ != '"') {
615  // Check for control character (0x00-0x1f) or unterminated string (<0).
616  if (c0_ < 0x20) return Handle<String>::null();
617  if (count >= length) {
618  // We need to create a longer sequential string for the result.
619  return SlowScanJsonString<StringType, SinkChar>(seq_string, 0, count);
620  }
621  if (c0_ != '\\') {
622  // If the sink can contain UC16 characters, or source_ contains only
623  // ASCII characters, there's no need to test whether we can store the
624  // character. Otherwise check whether the UC16 source character can fit
625  // in the ASCII sink.
626  if (sizeof(SinkChar) == kUC16Size ||
627  seq_ascii ||
629  SeqStringSet(seq_string, count++, c0_);
630  Advance();
631  } else {
632  // StringType is SeqOneByteString and we just read a non-ASCII char.
633  return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string, 0, count);
634  }
635  } else {
636  Advance(); // Advance past the \.
637  switch (c0_) {
638  case '"':
639  case '\\':
640  case '/':
641  SeqStringSet(seq_string, count++, c0_);
642  break;
643  case 'b':
644  SeqStringSet(seq_string, count++, '\x08');
645  break;
646  case 'f':
647  SeqStringSet(seq_string, count++, '\x0c');
648  break;
649  case 'n':
650  SeqStringSet(seq_string, count++, '\x0a');
651  break;
652  case 'r':
653  SeqStringSet(seq_string, count++, '\x0d');
654  break;
655  case 't':
656  SeqStringSet(seq_string, count++, '\x09');
657  break;
658  case 'u': {
659  uc32 value = 0;
660  for (int i = 0; i < 4; i++) {
661  Advance();
662  int digit = HexValue(c0_);
663  if (digit < 0) {
664  return Handle<String>::null();
665  }
666  value = value * 16 + digit;
667  }
668  if (sizeof(SinkChar) == kUC16Size ||
669  value <= String::kMaxOneByteCharCode) {
670  SeqStringSet(seq_string, count++, value);
671  break;
672  } else {
673  // StringType is SeqOneByteString and we just read a non-ASCII char.
674  position_ -= 6; // Rewind position_ to \ in \uxxxx.
675  Advance();
676  return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string,
677  0,
678  count);
679  }
680  }
681  default:
682  return Handle<String>::null();
683  }
684  Advance();
685  }
686  }
687 
688  ASSERT_EQ('"', c0_);
689  // Advance past the last '"'.
690  AdvanceSkipWhitespace();
691 
692  // Shrink seq_string length to count and return.
693  return SeqString::Truncate(seq_string, count);
694 }
695 
696 
697 template <bool seq_ascii>
698 template <bool is_internalized>
699 Handle<String> JsonParser<seq_ascii>::ScanJsonString() {
700  ASSERT_EQ('"', c0_);
701  Advance();
702  if (c0_ == '"') {
703  AdvanceSkipWhitespace();
704  return factory()->empty_string();
705  }
706 
707  if (seq_ascii && is_internalized) {
708  // Fast path for existing internalized strings. If the the string being
709  // parsed is not a known internalized string, contains backslashes or
710  // unexpectedly reaches the end of string, return with an empty handle.
711  uint32_t running_hash = isolate()->heap()->HashSeed();
712  int position = position_;
713  uc32 c0 = c0_;
714  do {
715  if (c0 == '\\') {
716  c0_ = c0;
717  int beg_pos = position_;
718  position_ = position;
719  return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
720  beg_pos,
721  position_);
722  }
723  if (c0 < 0x20) return Handle<String>::null();
724  if (static_cast<uint32_t>(c0) >
726  running_hash =
727  StringHasher::AddCharacterCore(running_hash,
729  running_hash =
730  StringHasher::AddCharacterCore(running_hash,
732  } else {
733  running_hash = StringHasher::AddCharacterCore(running_hash, c0);
734  }
735  position++;
736  if (position >= source_length_) return Handle<String>::null();
737  c0 = seq_source_->SeqOneByteStringGet(position);
738  } while (c0 != '"');
739  int length = position - position_;
740  uint32_t hash = (length <= String::kMaxHashCalcLength)
741  ? StringHasher::GetHashCore(running_hash) : length;
742  Vector<const uint8_t> string_vector(
743  seq_source_->GetChars() + position_, length);
744  StringTable* string_table = isolate()->heap()->string_table();
745  uint32_t capacity = string_table->Capacity();
746  uint32_t entry = StringTable::FirstProbe(hash, capacity);
747  uint32_t count = 1;
748  Handle<String> result;
749  while (true) {
750  Object* element = string_table->KeyAt(entry);
751  if (element == isolate()->heap()->undefined_value()) {
752  // Lookup failure.
753  result = factory()->InternalizeOneByteString(
754  seq_source_, position_, length);
755  break;
756  }
757  if (element != isolate()->heap()->the_hole_value() &&
758  String::cast(element)->IsOneByteEqualTo(string_vector)) {
759  result = Handle<String>(String::cast(element), isolate());
760 #ifdef DEBUG
761  uint32_t hash_field =
763  ASSERT_EQ(static_cast<int>(result->Hash()),
764  static_cast<int>(hash_field >> String::kHashShift));
765 #endif
766  break;
767  }
768  entry = StringTable::NextProbe(entry, count++, capacity);
769  }
770  position_ = position;
771  // Advance past the last '"'.
772  AdvanceSkipWhitespace();
773  return result;
774  }
775 
776  int beg_pos = position_;
777  // Fast case for ASCII only without escape characters.
778  do {
779  // Check for control character (0x00-0x1f) or unterminated string (<0).
780  if (c0_ < 0x20) return Handle<String>::null();
781  if (c0_ != '\\') {
782  if (seq_ascii || c0_ <= String::kMaxOneByteCharCode) {
783  Advance();
784  } else {
785  return SlowScanJsonString<SeqTwoByteString, uc16>(source_,
786  beg_pos,
787  position_);
788  }
789  } else {
790  return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
791  beg_pos,
792  position_);
793  }
794  } while (c0_ != '"');
795  int length = position_ - beg_pos;
796  Handle<String> result = factory()->NewRawOneByteString(length, pretenure_);
797  ASSERT(!result.is_null());
798  uint8_t* dest = SeqOneByteString::cast(*result)->GetChars();
799  String::WriteToFlat(*source_, dest, beg_pos, position_);
800 
801  ASSERT_EQ('"', c0_);
802  // Advance past the last '"'.
803  AdvanceSkipWhitespace();
804  return result;
805 }
806 
807 } } // namespace v8::internal
808 
809 #endif // V8_JSON_PARSER_H_
void FlattenString(Handle< String > string)
Definition: handles.cc:151
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf map
Definition: flags.cc:350
static String * cast(Object *obj)
static Smi * FromInt(int value)
Definition: objects-inl.h:1209
static Handle< T > cast(Handle< S > that)
Definition: handles.h:75
static const int kMaxHashCalcLength
Definition: objects.h:8926
int AsciiAlphaToLower(uc32 c)
int32_t uc32
Definition: globals.h:310
static uint16_t TrailSurrogate(uint32_t char_code)
Definition: unicode.h:134
kSerializedDataOffset Object
Definition: objects-inl.h:5016
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in only print modified registers Don t break for ASM_UNIMPLEMENTED_BREAK macros print stack trace when an illegal exception is thrown randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot testing_bool_flag testing_int_flag string flag tmp file in which to serialize heap Print the time it takes to lazily compile hydrogen code stubs concurrent_recompilation concurrent_sweeping Print usage message
Definition: flags.cc:665
static SeqOneByteString * cast(Object *obj)
#define ASSERT(condition)
Definition: checks.h:329
static Handle< Object > SetLocalPropertyIgnoreAttributes(Handle< JSObject > object, Handle< Name > key, Handle< Object > value, PropertyAttributes attributes, ValueType value_type=OPTIMAL_REPRESENTATION, StoreMode mode=ALLOW_AS_CONSTANT, ExtensibilityCheck extensibility_check=PERFORM_EXTENSIBILITY_CHECK)
Definition: objects.cc:4141
static Handle< Map > ExpectedTransitionTarget(Handle< Map > map)
Definition: objects-inl.h:1798
Handle< StringType > NewRawString(Factory *factory, int length, PretenureFlag pretenure)
Definition: json-parser.h:583
static MUST_USE_RESULT Handle< String > Truncate(Handle< SeqString > string, int new_length)
Definition: objects.cc:9144
double StringToDouble(UnicodeCache *unicode_cache, const char *str, int flags, double empty_string_val)
Definition: conversions.cc:47
static Handle< String > ExpectedTransitionKey(Handle< Map > map)
Definition: objects-inl.h:1783
Handle< SeqTwoByteString > NewRawTwoByteString(int length, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:300
static const uchar kMaxNonSurrogateCharCode
Definition: unicode.h:121
static uint16_t LeadSurrogate(uint32_t char_code)
Definition: unicode.h:131
static Handle< Map > FindTransitionToField(Handle< Map > map, Handle< Name > key)
Definition: objects-inl.h:1805
int HexValue(uc32 c)
Definition: scanner.h:52
static const int kIsNotArrayIndexMask
Definition: objects.h:8638
static Handle< Object > SetOwnElement(Handle< JSObject > object, uint32_t index, Handle< Object > value, StrictMode strict_mode)
Definition: objects.cc:12401
static Vector< T > New(int length)
Definition: utils.h:406
#define BASE_EMBEDDED
Definition: allocation.h:68
static void WriteToFlat(String *source, sinkchar *sink, int from, int to)
Definition: objects.cc:8635
static double nan_value()
Handle< String > LookupSingleCharacterStringFromCode(Isolate *isolate, uint32_t index)
Definition: handles.cc:212
static Vector< const char > cast(Vector< S > input)
Definition: utils.h:477
static void AllocateStorageForMap(Handle< JSObject > object, Handle< Map > map)
Definition: objects.cc:3831
static Handle< T > null()
Definition: handles.h:80
Handle< SeqOneByteString > NewRawOneByteString(int length, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:291
#define ASSERT_EQ(v1, v2)
Definition: checks.h:330
static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size)
Definition: objects.h:3789
PerThreadAssertScopeDebugOnly< HEAP_ALLOCATION_ASSERT, false > DisallowHeapAllocation
Definition: assert-scope.h:214
static Handle< Object > Parse(Handle< String > source)
Definition: json-parser.h:46
static const int kHashShift
Definition: objects.h:8642
static uint32_t FirstProbe(uint32_t hash, uint32_t size)
Definition: objects.h:3785
void SeqStringSet(Handle< StringType > seq_str, int i, uc32 c)
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in name
Definition: flags.cc:505
static const int32_t kMaxOneByteCharCode
Definition: objects.h:8914
const int kUC16Size
Definition: globals.h:312