// Copyright 2011 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_JSON_PARSER_H_ #define V8_JSON_PARSER_H_ #include "src/v8.h" #include "src/char-predicates-inl.h" #include "src/conversions.h" #include "src/heap/spaces-inl.h" #include "src/messages.h" #include "src/token.h" namespace v8 { namespace internal { // A simple json parser. template class JsonParser BASE_EMBEDDED { public: MUST_USE_RESULT static MaybeHandle Parse(Handle source) { return JsonParser(source).ParseJson(); } static const int kEndOfString = -1; private: explicit JsonParser(Handle source) : source_(source), source_length_(source->length()), isolate_(source->map()->GetHeap()->isolate()), factory_(isolate_->factory()), object_constructor_(isolate_->native_context()->object_function(), isolate_), position_(-1) { source_ = String::Flatten(source_); pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED; // Optimized fast case where we only have Latin1 characters. if (seq_one_byte) { seq_source_ = Handle::cast(source_); } } // Parse a string containing a single JSON value. MaybeHandle ParseJson(); inline void Advance() { position_++; if (position_ >= source_length_) { c0_ = kEndOfString; } else if (seq_one_byte) { c0_ = seq_source_->SeqOneByteStringGet(position_); } else { c0_ = source_->Get(position_); } } // The JSON lexical grammar is specified in the ECMAScript 5 standard, // section 15.12.1.1. The only allowed whitespace characters between tokens // are tab, carriage-return, newline and space. inline void AdvanceSkipWhitespace() { do { Advance(); } while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r'); } inline void SkipWhitespace() { while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') { Advance(); } } inline uc32 AdvanceGetChar() { Advance(); return c0_; } // Checks that current charater is c. // If so, then consume c and skip whitespace. inline bool MatchSkipWhiteSpace(uc32 c) { if (c0_ == c) { AdvanceSkipWhitespace(); return true; } return false; } // A JSON string (production JSONString) is subset of valid JavaScript string // literals. The string must only be double-quoted (not single-quoted), and // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. Handle ParseJsonString() { return ScanJsonString(); } bool ParseJsonString(Handle expected) { int length = expected->length(); if (source_->length() - position_ - 1 > length) { DisallowHeapAllocation no_gc; String::FlatContent content = expected->GetFlatContent(); if (content.IsOneByte()) { DCHECK_EQ('"', c0_); const uint8_t* input_chars = seq_source_->GetChars() + position_ + 1; const uint8_t* expected_chars = content.ToOneByteVector().start(); for (int i = 0; i < length; i++) { uint8_t c0 = input_chars[i]; // The expected string has to be free of \, " and characters < 0x20. if (c0 != expected_chars[i]) return false; } if (input_chars[length] == '"') { position_ = position_ + length + 1; AdvanceSkipWhitespace(); return true; } } } return false; } Handle ParseJsonInternalizedString() { return ScanJsonString(); } template Handle ScanJsonString(); // Creates a new string and copies prefix[start..end] into the beginning // of it. Then scans the rest of the string, adding characters after the // prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char. template Handle SlowScanJsonString(Handle prefix, int start, int end); // A JSON number (production JSONNumber) is a subset of the valid JavaScript // decimal number literals. // It includes an optional minus sign, must have at least one // digit before and after a decimal point, may not have prefixed zeros (unless // the integer part is zero), and may include an exponent part (e.g., "e-10"). // Hexadecimal and octal numbers are not allowed. Handle ParseJsonNumber(); // Parse a single JSON value from input (grammar production JSONValue). // A JSON value is either a (double-quoted) string literal, a number literal, // one of "true", "false", or "null", or an object or array literal. Handle ParseJsonValue(); // Parse a JSON object literal (grammar production JSONObject). // An object literal is a squiggly-braced and comma separated sequence // (possibly empty) of key/value pairs, where the key is a JSON string // literal, the value is a JSON value, and the two are separated by a colon. // A JSON array doesn't allow numbers and identifiers as keys, like a // JavaScript array. Handle ParseJsonObject(); // Parses a JSON array literal (grammar production JSONArray). An array // literal is a square-bracketed and comma separated sequence (possibly empty) // of JSON values. // A JSON array doesn't allow leaving out values from the sequence, nor does // it allow a terminal comma, like a JavaScript array does. Handle ParseJsonArray(); // Mark that a parsing error has happened at the current token, and // return a null handle. Primarily for readability. inline Handle ReportUnexpectedCharacter() { return Handle::null(); } inline Isolate* isolate() { return isolate_; } inline Factory* factory() { return factory_; } inline Handle object_constructor() { return object_constructor_; } static const int kInitialSpecialStringLength = 1024; static const int kPretenureTreshold = 100 * 1024; private: Zone* zone() { return &zone_; } void CommitStateToJsonObject(Handle json_object, Handle map, ZoneList >* properties); Handle source_; int source_length_; Handle seq_source_; PretenureFlag pretenure_; Isolate* isolate_; Factory* factory_; Zone zone_; Handle object_constructor_; uc32 c0_; int position_; }; template MaybeHandle JsonParser::ParseJson() { // Advance to the first character (possibly EOS) AdvanceSkipWhitespace(); Handle result = ParseJsonValue(); if (result.is_null() || c0_ != kEndOfString) { // Some exception (for example stack overflow) is already pending. if (isolate_->has_pending_exception()) return Handle::null(); // Parse failed. Current character is the unexpected token. const char* message; Factory* factory = this->factory(); Handle array; switch (c0_) { case kEndOfString: message = "unexpected_eos"; array = factory->NewJSArray(0); break; case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': message = "unexpected_token_number"; array = factory->NewJSArray(0); break; case '"': message = "unexpected_token_string"; array = factory->NewJSArray(0); break; default: message = "unexpected_token"; Handle name = factory->LookupSingleCharacterStringFromCode(c0_); Handle element = factory->NewFixedArray(1); element->set(0, *name); array = factory->NewJSArrayWithElements(element); break; } MessageLocation location(factory->NewScript(source_), position_, position_ + 1); Handle error; ASSIGN_RETURN_ON_EXCEPTION(isolate(), error, factory->NewSyntaxError(message, array), Object); return isolate()->template Throw(error, &location); } return result; } // Parse any JSON value. template Handle JsonParser::ParseJsonValue() { StackLimitCheck stack_check(isolate_); if (stack_check.HasOverflowed()) { isolate_->StackOverflow(); return Handle::null(); } if (c0_ == '"') return ParseJsonString(); if ((c0_ >= '0' && c0_ <= '9') || c0_ == '-') return ParseJsonNumber(); if (c0_ == '{') return ParseJsonObject(); if (c0_ == '[') return ParseJsonArray(); if (c0_ == 'f') { if (AdvanceGetChar() == 'a' && AdvanceGetChar() == 'l' && AdvanceGetChar() == 's' && AdvanceGetChar() == 'e') { AdvanceSkipWhitespace(); return factory()->false_value(); } return ReportUnexpectedCharacter(); } if (c0_ == 't') { if (AdvanceGetChar() == 'r' && AdvanceGetChar() == 'u' && AdvanceGetChar() == 'e') { AdvanceSkipWhitespace(); return factory()->true_value(); } return ReportUnexpectedCharacter(); } if (c0_ == 'n') { if (AdvanceGetChar() == 'u' && AdvanceGetChar() == 'l' && AdvanceGetChar() == 'l') { AdvanceSkipWhitespace(); return factory()->null_value(); } return ReportUnexpectedCharacter(); } return ReportUnexpectedCharacter(); } // Parse a JSON object. Position must be right at '{'. template Handle JsonParser::ParseJsonObject() { HandleScope scope(isolate()); Handle json_object = factory()->NewJSObject(object_constructor(), pretenure_); Handle map(json_object->map()); ZoneList > properties(8, zone()); DCHECK_EQ(c0_, '{'); bool transitioning = true; AdvanceSkipWhitespace(); if (c0_ != '}') { do { if (c0_ != '"') return ReportUnexpectedCharacter(); int start_position = position_; Advance(); uint32_t index = 0; if (c0_ >= '0' && c0_ <= '9') { // Maybe an array index, try to parse it. if (c0_ == '0') { // With a leading zero, the string has to be "0" only to be an index. Advance(); } else { do { int d = c0_ - '0'; if (index > 429496729U - ((d > 5) ? 1 : 0)) break; index = (index * 10) + d; Advance(); } while (c0_ >= '0' && c0_ <= '9'); } if (c0_ == '"') { // Successfully parsed index, parse and store element. AdvanceSkipWhitespace(); if (c0_ != ':') return ReportUnexpectedCharacter(); AdvanceSkipWhitespace(); Handle value = ParseJsonValue(); if (value.is_null()) return ReportUnexpectedCharacter(); JSObject::SetOwnElement(json_object, index, value, SLOPPY).Assert(); continue; } // Not an index, fallback to the slow path. } position_ = start_position; #ifdef DEBUG c0_ = '"'; #endif Handle key; Handle value; // Try to follow existing transitions as long as possible. Once we stop // transitioning, no transition can be found anymore. if (transitioning) { // First check whether there is a single expected transition. If so, try // to parse it first. bool follow_expected = false; Handle target; if (seq_one_byte) { key = Map::ExpectedTransitionKey(map); follow_expected = !key.is_null() && ParseJsonString(key); } // If the expected transition hits, follow it. if (follow_expected) { target = Map::ExpectedTransitionTarget(map); } else { // If the expected transition failed, parse an internalized string and // try to find a matching transition. key = ParseJsonInternalizedString(); if (key.is_null()) return ReportUnexpectedCharacter(); target = Map::FindTransitionToField(map, key); // If a transition was found, follow it and continue. transitioning = !target.is_null(); } if (c0_ != ':') return ReportUnexpectedCharacter(); AdvanceSkipWhitespace(); value = ParseJsonValue(); if (value.is_null()) return ReportUnexpectedCharacter(); if (transitioning) { int descriptor = map->NumberOfOwnDescriptors(); PropertyDetails details = target->instance_descriptors()->GetDetails(descriptor); Representation expected_representation = details.representation(); if (value->FitsRepresentation(expected_representation)) { if (expected_representation.IsDouble()) { value = Object::NewStorageFor(isolate(), value, expected_representation); } else if (expected_representation.IsHeapObject() && !target->instance_descriptors()->GetFieldType( descriptor)->NowContains(value)) { Handle value_type(value->OptimalType( isolate(), expected_representation)); Map::GeneralizeFieldType(target, descriptor, expected_representation, value_type); } DCHECK(target->instance_descriptors()->GetFieldType( descriptor)->NowContains(value)); properties.Add(value, zone()); map = target; continue; } else { transitioning = false; } } // Commit the intermediate state to the object and stop transitioning. CommitStateToJsonObject(json_object, map, &properties); } else { key = ParseJsonInternalizedString(); if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); AdvanceSkipWhitespace(); value = ParseJsonValue(); if (value.is_null()) return ReportUnexpectedCharacter(); } Runtime::DefineObjectProperty(json_object, key, value, NONE).Check(); } while (MatchSkipWhiteSpace(',')); if (c0_ != '}') { return ReportUnexpectedCharacter(); } // If we transitioned until the very end, transition the map now. if (transitioning) { CommitStateToJsonObject(json_object, map, &properties); } } AdvanceSkipWhitespace(); return scope.CloseAndEscape(json_object); } template void JsonParser::CommitStateToJsonObject( Handle json_object, Handle map, ZoneList >* properties) { JSObject::AllocateStorageForMap(json_object, map); DCHECK(!json_object->map()->is_dictionary_map()); DisallowHeapAllocation no_gc; Factory* factory = isolate()->factory(); // If the |json_object|'s map is exactly the same as |map| then the // |properties| values correspond to the |map| and nothing more has to be // done. But if the |json_object|'s map is different then we have to // iterate descriptors to ensure that properties still correspond to the // map. bool slow_case = json_object->map() != *map; DescriptorArray* descriptors = NULL; int length = properties->length(); if (slow_case) { descriptors = json_object->map()->instance_descriptors(); DCHECK(json_object->map()->NumberOfOwnDescriptors() == length); } for (int i = 0; i < length; i++) { Handle value = (*properties)[i]; if (slow_case && value->IsMutableHeapNumber() && !descriptors->GetDetails(i).representation().IsDouble()) { // Turn mutable heap numbers into immutable if the field representation // is not double. HeapNumber::cast(*value)->set_map(*factory->heap_number_map()); } FieldIndex index = FieldIndex::ForPropertyIndex(*map, i); json_object->FastPropertyAtPut(index, *value); } } // Parse a JSON array. Position must be right at '['. template Handle JsonParser::ParseJsonArray() { HandleScope scope(isolate()); ZoneList > elements(4, zone()); DCHECK_EQ(c0_, '['); AdvanceSkipWhitespace(); if (c0_ != ']') { do { Handle element = ParseJsonValue(); if (element.is_null()) return ReportUnexpectedCharacter(); elements.Add(element, zone()); } while (MatchSkipWhiteSpace(',')); if (c0_ != ']') { return ReportUnexpectedCharacter(); } } AdvanceSkipWhitespace(); // Allocate a fixed array with all the elements. Handle fast_elements = factory()->NewFixedArray(elements.length(), pretenure_); for (int i = 0, n = elements.length(); i < n; i++) { fast_elements->set(i, *elements[i]); } Handle json_array = factory()->NewJSArrayWithElements( fast_elements, FAST_ELEMENTS, pretenure_); return scope.CloseAndEscape(json_array); } template Handle JsonParser::ParseJsonNumber() { bool negative = false; int beg_pos = position_; if (c0_ == '-') { Advance(); negative = true; } if (c0_ == '0') { Advance(); // Prefix zero is only allowed if it's the only digit before // a decimal point or exponent. if ('0' <= c0_ && c0_ <= '9') return ReportUnexpectedCharacter(); } else { int i = 0; int digits = 0; if (c0_ < '1' || c0_ > '9') return ReportUnexpectedCharacter(); do { i = i * 10 + c0_ - '0'; digits++; Advance(); } while (c0_ >= '0' && c0_ <= '9'); if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) { SkipWhitespace(); return Handle(Smi::FromInt((negative ? -i : i)), isolate()); } } if (c0_ == '.') { Advance(); if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); do { Advance(); } while (c0_ >= '0' && c0_ <= '9'); } if (AsciiAlphaToLower(c0_) == 'e') { Advance(); if (c0_ == '-' || c0_ == '+') Advance(); if (c0_ < '0' || c0_ > '9') return ReportUnexpectedCharacter(); do { Advance(); } while (c0_ >= '0' && c0_ <= '9'); } int length = position_ - beg_pos; double number; if (seq_one_byte) { Vector chars(seq_source_->GetChars() + beg_pos, length); number = StringToDouble(isolate()->unicode_cache(), chars, NO_FLAGS, // Hex, octal or trailing junk. std::numeric_limits::quiet_NaN()); } else { Vector buffer = Vector::New(length); String::WriteToFlat(*source_, buffer.start(), beg_pos, position_); Vector result = Vector(buffer.start(), length); number = StringToDouble(isolate()->unicode_cache(), result, NO_FLAGS, // Hex, octal or trailing junk. 0.0); buffer.Dispose(); } SkipWhitespace(); return factory()->NewNumber(number, pretenure_); } template inline void SeqStringSet(Handle seq_str, int i, uc32 c); template <> inline void SeqStringSet(Handle seq_str, int i, uc32 c) { seq_str->SeqTwoByteStringSet(i, c); } template <> inline void SeqStringSet(Handle seq_str, int i, uc32 c) { seq_str->SeqOneByteStringSet(i, c); } template inline Handle NewRawString(Factory* factory, int length, PretenureFlag pretenure); template <> inline Handle NewRawString(Factory* factory, int length, PretenureFlag pretenure) { return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked(); } template <> inline Handle NewRawString(Factory* factory, int length, PretenureFlag pretenure) { return factory->NewRawOneByteString(length, pretenure).ToHandleChecked(); } // Scans the rest of a JSON string starting from position_ and writes // prefix[start..end] along with the scanned characters into a // sequential string of type StringType. template template Handle JsonParser::SlowScanJsonString( Handle prefix, int start, int end) { int count = end - start; int max_length = count + source_length_ - position_; int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); Handle seq_string = NewRawString(factory(), length, pretenure_); // Copy prefix into seq_str. SinkChar* dest = seq_string->GetChars(); String::WriteToFlat(*prefix, dest, start, end); while (c0_ != '"') { // Check for control character (0x00-0x1f) or unterminated string (<0). if (c0_ < 0x20) return Handle::null(); if (count >= length) { // We need to create a longer sequential string for the result. return SlowScanJsonString(seq_string, 0, count); } if (c0_ != '\\') { // If the sink can contain UC16 characters, or source_ contains only // Latin1 characters, there's no need to test whether we can store the // character. Otherwise check whether the UC16 source character can fit // in the Latin1 sink. if (sizeof(SinkChar) == kUC16Size || seq_one_byte || c0_ <= String::kMaxOneByteCharCode) { SeqStringSet(seq_string, count++, c0_); Advance(); } else { // StringType is SeqOneByteString and we just read a non-Latin1 char. return SlowScanJsonString(seq_string, 0, count); } } else { Advance(); // Advance past the \. switch (c0_) { case '"': case '\\': case '/': SeqStringSet(seq_string, count++, c0_); break; case 'b': SeqStringSet(seq_string, count++, '\x08'); break; case 'f': SeqStringSet(seq_string, count++, '\x0c'); break; case 'n': SeqStringSet(seq_string, count++, '\x0a'); break; case 'r': SeqStringSet(seq_string, count++, '\x0d'); break; case 't': SeqStringSet(seq_string, count++, '\x09'); break; case 'u': { uc32 value = 0; for (int i = 0; i < 4; i++) { Advance(); int digit = HexValue(c0_); if (digit < 0) { return Handle::null(); } value = value * 16 + digit; } if (sizeof(SinkChar) == kUC16Size || value <= String::kMaxOneByteCharCode) { SeqStringSet(seq_string, count++, value); break; } else { // StringType is SeqOneByteString and we just read a non-Latin1 // char. position_ -= 6; // Rewind position_ to \ in \uxxxx. Advance(); return SlowScanJsonString(seq_string, 0, count); } } default: return Handle::null(); } Advance(); } } DCHECK_EQ('"', c0_); // Advance past the last '"'. AdvanceSkipWhitespace(); // Shrink seq_string length to count and return. return SeqString::Truncate(seq_string, count); } template template Handle JsonParser::ScanJsonString() { DCHECK_EQ('"', c0_); Advance(); if (c0_ == '"') { AdvanceSkipWhitespace(); return factory()->empty_string(); } if (seq_one_byte && is_internalized) { // Fast path for existing internalized strings. If the the string being // parsed is not a known internalized string, contains backslashes or // unexpectedly reaches the end of string, return with an empty handle. uint32_t running_hash = isolate()->heap()->HashSeed(); int position = position_; uc32 c0 = c0_; do { if (c0 == '\\') { c0_ = c0; int beg_pos = position_; position_ = position; return SlowScanJsonString(source_, beg_pos, position_); } if (c0 < 0x20) return Handle::null(); if (static_cast(c0) > unibrow::Utf16::kMaxNonSurrogateCharCode) { running_hash = StringHasher::AddCharacterCore(running_hash, unibrow::Utf16::LeadSurrogate(c0)); running_hash = StringHasher::AddCharacterCore(running_hash, unibrow::Utf16::TrailSurrogate(c0)); } else { running_hash = StringHasher::AddCharacterCore(running_hash, c0); } position++; if (position >= source_length_) return Handle::null(); c0 = seq_source_->SeqOneByteStringGet(position); } while (c0 != '"'); int length = position - position_; uint32_t hash = (length <= String::kMaxHashCalcLength) ? StringHasher::GetHashCore(running_hash) : static_cast(length); Vector string_vector( seq_source_->GetChars() + position_, length); StringTable* string_table = isolate()->heap()->string_table(); uint32_t capacity = string_table->Capacity(); uint32_t entry = StringTable::FirstProbe(hash, capacity); uint32_t count = 1; Handle result; while (true) { Object* element = string_table->KeyAt(entry); if (element == isolate()->heap()->undefined_value()) { // Lookup failure. result = factory()->InternalizeOneByteString( seq_source_, position_, length); break; } if (element != isolate()->heap()->the_hole_value() && String::cast(element)->IsOneByteEqualTo(string_vector)) { result = Handle(String::cast(element), isolate()); #ifdef DEBUG uint32_t hash_field = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; DCHECK_EQ(static_cast(result->Hash()), static_cast(hash_field >> String::kHashShift)); #endif break; } entry = StringTable::NextProbe(entry, count++, capacity); } position_ = position; // Advance past the last '"'. AdvanceSkipWhitespace(); return result; } int beg_pos = position_; // Fast case for Latin1 only without escape characters. do { // Check for control character (0x00-0x1f) or unterminated string (<0). if (c0_ < 0x20) return Handle::null(); if (c0_ != '\\') { if (seq_one_byte || c0_ <= String::kMaxOneByteCharCode) { Advance(); } else { return SlowScanJsonString(source_, beg_pos, position_); } } else { return SlowScanJsonString(source_, beg_pos, position_); } } while (c0_ != '"'); int length = position_ - beg_pos; Handle result = factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked(); uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); String::WriteToFlat(*source_, dest, beg_pos, position_); DCHECK_EQ('"', c0_); // Advance past the last '"'. AdvanceSkipWhitespace(); return result; } } } // namespace v8::internal #endif // V8_JSON_PARSER_H_