2011-05-24 12:16:23 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
#ifndef V8_JSON_PARSER_H_
|
|
|
|
#define V8_JSON_PARSER_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2011-06-08 08:09:48 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/char-predicates-inl.h"
|
|
|
|
#include "src/conversions.h"
|
2014-08-05 08:18:22 +00:00
|
|
|
#include "src/heap/spaces-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/messages.h"
|
|
|
|
#include "src/token.h"
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2011-10-05 11:09:34 +00:00
|
|
|
// A simple json parser.
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
2011-05-24 12:16:23 +00:00
|
|
|
class JsonParser BASE_EMBEDDED {
|
|
|
|
public:
|
2014-04-04 12:06:11 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> Parse(Handle<String> source) {
|
2013-06-26 13:36:16 +00:00
|
|
|
return JsonParser(source).ParseJson();
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const int kEndOfString = -1;
|
|
|
|
|
|
|
|
private:
|
2013-06-26 13:36:16 +00:00
|
|
|
explicit JsonParser(Handle<String> source)
|
|
|
|
: source_(source),
|
|
|
|
source_length_(source->length()),
|
|
|
|
isolate_(source->map()->GetHeap()->isolate()),
|
|
|
|
factory_(isolate_->factory()),
|
|
|
|
zone_(isolate_),
|
|
|
|
object_constructor_(isolate_->native_context()->object_function(),
|
|
|
|
isolate_),
|
|
|
|
position_(-1) {
|
2014-04-08 09:49:49 +00:00
|
|
|
source_ = String::Flatten(source_);
|
2013-06-26 13:36:16 +00:00
|
|
|
pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED;
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
// Optimized fast case where we only have Latin1 characters.
|
|
|
|
if (seq_one_byte) {
|
2013-06-26 13:36:16 +00:00
|
|
|
seq_source_ = Handle<SeqOneByteString>::cast(source_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-05 11:09:34 +00:00
|
|
|
// Parse a string containing a single JSON value.
|
2014-04-03 14:25:59 +00:00
|
|
|
MaybeHandle<Object> ParseJson();
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
inline void Advance() {
|
2011-06-01 14:06:30 +00:00
|
|
|
position_++;
|
2011-06-29 10:54:20 +00:00
|
|
|
if (position_ >= source_length_) {
|
2011-05-24 12:16:23 +00:00
|
|
|
c0_ = kEndOfString;
|
2014-09-10 12:38:12 +00:00
|
|
|
} else if (seq_one_byte) {
|
2012-11-15 13:31:27 +00:00
|
|
|
c0_ = seq_source_->SeqOneByteStringGet(position_);
|
2011-05-24 12:16:23 +00:00
|
|
|
} else {
|
2011-10-05 11:09:34 +00:00
|
|
|
c0_ = source_->Get(position_);
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 14:06:30 +00:00
|
|
|
// 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();
|
2012-09-24 14:23:46 +00:00
|
|
|
} while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r');
|
2011-06-01 14:06:30 +00:00
|
|
|
}
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2011-06-01 14:06:30 +00:00
|
|
|
inline void SkipWhitespace() {
|
2012-09-24 14:23:46 +00:00
|
|
|
while (c0_ == ' ' || c0_ == '\t' || c0_ == '\n' || c0_ == '\r') {
|
2011-06-01 14:06:30 +00:00
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2011-06-01 14:06:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
// 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.
|
2011-06-01 14:55:55 +00:00
|
|
|
Handle<String> ParseJsonString() {
|
2011-06-01 14:06:30 +00:00
|
|
|
return ScanJsonString<false>();
|
|
|
|
}
|
2013-04-09 16:49:28 +00:00
|
|
|
|
|
|
|
bool ParseJsonString(Handle<String> expected) {
|
|
|
|
int length = expected->length();
|
|
|
|
if (source_->length() - position_ - 1 > length) {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-04-09 16:49:28 +00:00
|
|
|
String::FlatContent content = expected->GetFlatContent();
|
2014-09-10 12:38:12 +00:00
|
|
|
if (content.IsOneByte()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ('"', c0_);
|
2013-04-09 16:49:28 +00:00
|
|
|
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];
|
|
|
|
if (c0 != expected_chars[i] ||
|
|
|
|
c0 == '"' || c0 < 0x20 || c0 == '\\') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (input_chars[length] == '"') {
|
|
|
|
position_ = position_ + length + 1;
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> ParseJsonInternalizedString() {
|
2011-06-01 14:06:30 +00:00
|
|
|
return ScanJsonString<true>();
|
|
|
|
}
|
2013-04-09 16:49:28 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
template <bool is_internalized>
|
2011-06-01 14:55:55 +00:00
|
|
|
Handle<String> ScanJsonString();
|
2011-06-29 10:54:20 +00:00
|
|
|
// 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
|
2014-09-10 12:38:12 +00:00
|
|
|
// prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char.
|
2011-10-05 11:09:34 +00:00
|
|
|
template <typename StringType, typename SinkChar>
|
2011-06-29 10:54:20 +00:00
|
|
|
Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
// 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.
|
2011-06-01 14:06:30 +00:00
|
|
|
Handle<Object> ParseJsonNumber();
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
// 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<Object> 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.
|
2012-01-16 12:38:59 +00:00
|
|
|
// A JSON array doesn't allow numbers and identifiers as keys, like a
|
2011-05-24 12:16:23 +00:00
|
|
|
// JavaScript array.
|
|
|
|
Handle<Object> 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<Object> ParseJsonArray();
|
|
|
|
|
|
|
|
|
|
|
|
// Mark that a parsing error has happened at the current token, and
|
|
|
|
// return a null handle. Primarily for readability.
|
2011-06-01 14:06:30 +00:00
|
|
|
inline Handle<Object> ReportUnexpectedCharacter() {
|
|
|
|
return Handle<Object>::null();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Isolate* isolate() { return isolate_; }
|
2012-10-19 15:37:28 +00:00
|
|
|
inline Factory* factory() { return factory_; }
|
|
|
|
inline Handle<JSFunction> object_constructor() { return object_constructor_; }
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2011-06-29 10:54:20 +00:00
|
|
|
static const int kInitialSpecialStringLength = 1024;
|
2012-10-23 08:06:28 +00:00
|
|
|
static const int kPretenureTreshold = 100 * 1024;
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2011-10-05 11:09:34 +00:00
|
|
|
|
|
|
|
private:
|
2013-06-26 13:36:16 +00:00
|
|
|
Zone* zone() { return &zone_; }
|
|
|
|
|
2014-10-23 14:41:39 +00:00
|
|
|
void CommitStateToJsonObject(Handle<JSObject> json_object, Handle<Map> map,
|
|
|
|
ZoneList<Handle<Object> >* properties);
|
|
|
|
|
2011-10-05 11:09:34 +00:00
|
|
|
Handle<String> source_;
|
2011-09-26 11:14:41 +00:00
|
|
|
int source_length_;
|
2012-11-15 13:31:27 +00:00
|
|
|
Handle<SeqOneByteString> seq_source_;
|
2011-10-05 11:09:34 +00:00
|
|
|
|
2012-10-23 08:06:28 +00:00
|
|
|
PretenureFlag pretenure_;
|
2011-10-05 11:09:34 +00:00
|
|
|
Isolate* isolate_;
|
2012-10-19 15:37:28 +00:00
|
|
|
Factory* factory_;
|
2013-06-26 13:36:16 +00:00
|
|
|
Zone zone_;
|
2012-10-19 15:37:28 +00:00
|
|
|
Handle<JSFunction> object_constructor_;
|
2011-09-26 11:14:41 +00:00
|
|
|
uc32 c0_;
|
2011-10-05 11:09:34 +00:00
|
|
|
int position_;
|
2011-05-24 12:16:23 +00:00
|
|
|
};
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
|
|
|
MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
|
2012-01-16 12:38:59 +00:00
|
|
|
// Advance to the first character (possibly EOS)
|
2011-06-08 08:09:48 +00:00
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
Handle<Object> result = ParseJsonValue();
|
|
|
|
if (result.is_null() || c0_ != kEndOfString) {
|
2012-10-25 12:36:40 +00:00
|
|
|
// Some exception (for example stack overflow) is already pending.
|
|
|
|
if (isolate_->has_pending_exception()) return Handle<Object>::null();
|
2011-10-05 11:09:34 +00:00
|
|
|
|
2012-10-25 12:36:40 +00:00
|
|
|
// Parse failed. Current character is the unexpected token.
|
2011-06-08 08:09:48 +00:00
|
|
|
const char* message;
|
2012-10-19 15:37:28 +00:00
|
|
|
Factory* factory = this->factory();
|
2011-06-08 08:09:48 +00:00
|
|
|
Handle<JSArray> 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";
|
2014-04-08 06:45:53 +00:00
|
|
|
Handle<Object> name = factory->LookupSingleCharacterStringFromCode(c0_);
|
2011-06-08 08:09:48 +00:00
|
|
|
Handle<FixedArray> element = factory->NewFixedArray(1);
|
|
|
|
element->set(0, *name);
|
|
|
|
array = factory->NewJSArrayWithElements(element);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-26 13:36:16 +00:00
|
|
|
MessageLocation location(factory->NewScript(source_),
|
2011-06-08 08:09:48 +00:00
|
|
|
position_,
|
|
|
|
position_ + 1);
|
2014-09-01 09:11:44 +00:00
|
|
|
Handle<Object> error;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(isolate(), error,
|
|
|
|
factory->NewSyntaxError(message, array), Object);
|
2014-04-03 14:25:59 +00:00
|
|
|
return isolate()->template Throw<Object>(error, &location);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parse any JSON value.
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
|
|
|
Handle<Object> JsonParser<seq_one_byte>::ParseJsonValue() {
|
2012-10-25 12:36:40 +00:00
|
|
|
StackLimitCheck stack_check(isolate_);
|
|
|
|
if (stack_check.HasOverflowed()) {
|
|
|
|
isolate_->StackOverflow();
|
|
|
|
return Handle<Object>::null();
|
|
|
|
}
|
|
|
|
|
2012-10-19 15:37:28 +00:00
|
|
|
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();
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
2012-10-19 15:37:28 +00:00
|
|
|
return ReportUnexpectedCharacter();
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parse a JSON object. Position must be right at '{'.
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
|
|
|
Handle<Object> JsonParser<seq_one_byte>::ParseJsonObject() {
|
2013-03-25 17:10:33 +00:00
|
|
|
HandleScope scope(isolate());
|
2011-06-08 08:09:48 +00:00
|
|
|
Handle<JSObject> json_object =
|
2012-10-23 08:06:28 +00:00
|
|
|
factory()->NewJSObject(object_constructor(), pretenure_);
|
2013-04-09 16:49:28 +00:00
|
|
|
Handle<Map> map(json_object->map());
|
|
|
|
ZoneList<Handle<Object> > properties(8, zone());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(c0_, '{');
|
2011-06-08 08:09:48 +00:00
|
|
|
|
2013-04-09 16:49:28 +00:00
|
|
|
bool transitioning = true;
|
|
|
|
|
2011-06-08 08:09:48 +00:00
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
if (c0_ != '}') {
|
|
|
|
do {
|
|
|
|
if (c0_ != '"') return ReportUnexpectedCharacter();
|
|
|
|
|
2012-10-18 14:44:40 +00:00
|
|
|
int start_position = position_;
|
|
|
|
Advance();
|
|
|
|
|
|
|
|
uint32_t index = 0;
|
2012-10-29 12:01:29 +00:00
|
|
|
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');
|
|
|
|
}
|
2012-10-18 14:44:40 +00:00
|
|
|
|
2012-10-29 12:01:29 +00:00
|
|
|
if (c0_ == '"') {
|
|
|
|
// Successfully parsed index, parse and store element.
|
|
|
|
AdvanceSkipWhitespace();
|
2012-10-18 14:44:40 +00:00
|
|
|
|
2012-10-29 12:01:29 +00:00
|
|
|
if (c0_ != ':') return ReportUnexpectedCharacter();
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
Handle<Object> value = ParseJsonValue();
|
|
|
|
if (value.is_null()) return ReportUnexpectedCharacter();
|
2012-10-18 14:44:40 +00:00
|
|
|
|
2014-04-08 07:04:13 +00:00
|
|
|
JSObject::SetOwnElement(json_object, index, value, SLOPPY).Assert();
|
2012-10-29 12:01:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Not an index, fallback to the slow path.
|
|
|
|
}
|
|
|
|
|
|
|
|
position_ = start_position;
|
2012-10-18 14:44:40 +00:00
|
|
|
#ifdef DEBUG
|
2012-10-29 12:01:29 +00:00
|
|
|
c0_ = '"';
|
2012-10-18 14:44:40 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-09 16:49:28 +00:00
|
|
|
Handle<String> key;
|
|
|
|
Handle<Object> 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;
|
2013-05-08 15:02:08 +00:00
|
|
|
Handle<Map> target;
|
2014-09-10 12:38:12 +00:00
|
|
|
if (seq_one_byte) {
|
2014-06-23 13:46:49 +00:00
|
|
|
key = Map::ExpectedTransitionKey(map);
|
2013-04-09 16:49:28 +00:00
|
|
|
follow_expected = !key.is_null() && ParseJsonString(key);
|
|
|
|
}
|
|
|
|
// If the expected transition hits, follow it.
|
|
|
|
if (follow_expected) {
|
2014-06-23 13:46:49 +00:00
|
|
|
target = Map::ExpectedTransitionTarget(map);
|
2013-04-09 16:49:28 +00:00
|
|
|
} 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();
|
|
|
|
|
2014-06-23 13:46:49 +00:00
|
|
|
target = Map::FindTransitionToField(map, key);
|
2013-04-09 16:49:28 +00:00
|
|
|
// If a transition was found, follow it and continue.
|
2013-05-08 15:02:08 +00:00
|
|
|
transitioning = !target.is_null();
|
2013-04-09 16:49:28 +00:00
|
|
|
}
|
|
|
|
if (c0_ != ':') return ReportUnexpectedCharacter();
|
2012-10-18 14:44:40 +00:00
|
|
|
|
2013-04-09 16:49:28 +00:00
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
value = ParseJsonValue();
|
|
|
|
if (value.is_null()) return ReportUnexpectedCharacter();
|
2012-10-18 14:44:40 +00:00
|
|
|
|
2013-04-26 15:30:41 +00:00
|
|
|
if (transitioning) {
|
2013-05-08 15:02:08 +00:00
|
|
|
int descriptor = map->NumberOfOwnDescriptors();
|
|
|
|
PropertyDetails details =
|
|
|
|
target->instance_descriptors()->GetDetails(descriptor);
|
|
|
|
Representation expected_representation = details.representation();
|
|
|
|
|
|
|
|
if (value->FitsRepresentation(expected_representation)) {
|
2014-07-01 15:02:31 +00:00
|
|
|
if (expected_representation.IsDouble()) {
|
|
|
|
value = Object::NewStorageFor(isolate(), value,
|
|
|
|
expected_representation);
|
2014-04-15 07:36:47 +00:00
|
|
|
} else if (expected_representation.IsHeapObject() &&
|
|
|
|
!target->instance_descriptors()->GetFieldType(
|
|
|
|
descriptor)->NowContains(value)) {
|
|
|
|
Handle<HeapType> value_type(value->OptimalType(
|
|
|
|
isolate(), expected_representation));
|
2014-11-13 10:56:13 +00:00
|
|
|
Map::GeneralizeFieldType(target, descriptor,
|
|
|
|
expected_representation, value_type);
|
2013-05-08 15:02:08 +00:00
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(target->instance_descriptors()->GetFieldType(
|
2014-04-15 07:36:47 +00:00
|
|
|
descriptor)->NowContains(value));
|
2013-05-08 15:02:08 +00:00
|
|
|
properties.Add(value, zone());
|
|
|
|
map = target;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
transitioning = false;
|
2013-04-26 15:30:41 +00:00
|
|
|
}
|
2013-05-08 15:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the intermediate state to the object and stop transitioning.
|
2014-10-23 14:41:39 +00:00
|
|
|
CommitStateToJsonObject(json_object, map, &properties);
|
2012-10-29 12:01:29 +00:00
|
|
|
} else {
|
2013-04-09 16:49:28 +00:00
|
|
|
key = ParseJsonInternalizedString();
|
|
|
|
if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter();
|
|
|
|
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
value = ParseJsonValue();
|
|
|
|
if (value.is_null()) return ReportUnexpectedCharacter();
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
2013-04-09 16:49:28 +00:00
|
|
|
|
2014-09-22 15:21:19 +00:00
|
|
|
Runtime::DefineObjectProperty(json_object, key, value, NONE).Check();
|
2011-06-08 08:09:48 +00:00
|
|
|
} while (MatchSkipWhiteSpace(','));
|
|
|
|
if (c0_ != '}') {
|
|
|
|
return ReportUnexpectedCharacter();
|
|
|
|
}
|
2013-04-09 16:49:28 +00:00
|
|
|
|
|
|
|
// If we transitioned until the very end, transition the map now.
|
|
|
|
if (transitioning) {
|
2014-10-23 14:41:39 +00:00
|
|
|
CommitStateToJsonObject(json_object, map, &properties);
|
2013-04-09 16:49:28 +00:00
|
|
|
}
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
AdvanceSkipWhitespace();
|
2013-03-25 17:10:33 +00:00
|
|
|
return scope.CloseAndEscape(json_object);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 14:41:39 +00:00
|
|
|
|
|
|
|
template <bool seq_one_byte>
|
|
|
|
void JsonParser<seq_one_byte>::CommitStateToJsonObject(
|
|
|
|
Handle<JSObject> json_object, Handle<Map> map,
|
|
|
|
ZoneList<Handle<Object> >* 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<Object> 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-08 08:09:48 +00:00
|
|
|
// Parse a JSON array. Position must be right at '['.
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
|
|
|
Handle<Object> JsonParser<seq_one_byte>::ParseJsonArray() {
|
2013-03-25 17:10:33 +00:00
|
|
|
HandleScope scope(isolate());
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<Handle<Object> > elements(4, zone());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(c0_, '[');
|
2011-06-08 08:09:48 +00:00
|
|
|
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
if (c0_ != ']') {
|
|
|
|
do {
|
|
|
|
Handle<Object> element = ParseJsonValue();
|
|
|
|
if (element.is_null()) return ReportUnexpectedCharacter();
|
2012-06-11 12:42:31 +00:00
|
|
|
elements.Add(element, zone());
|
2011-06-08 08:09:48 +00:00
|
|
|
} while (MatchSkipWhiteSpace(','));
|
|
|
|
if (c0_ != ']') {
|
|
|
|
return ReportUnexpectedCharacter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
// Allocate a fixed array with all the elements.
|
|
|
|
Handle<FixedArray> fast_elements =
|
2012-10-23 08:06:28 +00:00
|
|
|
factory()->NewFixedArray(elements.length(), pretenure_);
|
2011-06-08 08:09:48 +00:00
|
|
|
for (int i = 0, n = elements.length(); i < n; i++) {
|
|
|
|
fast_elements->set(i, *elements[i]);
|
|
|
|
}
|
2013-03-25 17:10:33 +00:00
|
|
|
Handle<Object> json_array = factory()->NewJSArrayWithElements(
|
2012-10-23 08:06:28 +00:00
|
|
|
fast_elements, FAST_ELEMENTS, pretenure_);
|
2013-03-25 17:10:33 +00:00
|
|
|
return scope.CloseAndEscape(json_array);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
|
|
|
Handle<Object> JsonParser<seq_one_byte>::ParseJsonNumber() {
|
2011-06-08 08:09:48 +00:00
|
|
|
bool negative = false;
|
2011-06-22 14:20:23 +00:00
|
|
|
int beg_pos = position_;
|
2011-06-08 08:09:48 +00:00
|
|
|
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;
|
2012-10-18 18:43:19 +00:00
|
|
|
int digits = 0;
|
2011-06-08 08:09:48 +00:00
|
|
|
if (c0_ < '1' || c0_ > '9') return ReportUnexpectedCharacter();
|
|
|
|
do {
|
|
|
|
i = i * 10 + c0_ - '0';
|
2012-10-18 18:43:19 +00:00
|
|
|
digits++;
|
2011-06-08 08:09:48 +00:00
|
|
|
Advance();
|
2012-10-18 18:43:19 +00:00
|
|
|
} while (c0_ >= '0' && c0_ <= '9');
|
|
|
|
if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) {
|
2011-06-08 08:09:48 +00:00
|
|
|
SkipWhitespace();
|
2011-06-22 14:20:23 +00:00
|
|
|
return Handle<Smi>(Smi::FromInt((negative ? -i : i)), isolate());
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
}
|
2011-06-22 14:20:23 +00:00
|
|
|
int length = position_ - beg_pos;
|
|
|
|
double number;
|
2014-09-10 12:38:12 +00:00
|
|
|
if (seq_one_byte) {
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<const uint8_t> chars(seq_source_->GetChars() + beg_pos, length);
|
2011-10-05 11:09:34 +00:00
|
|
|
number = StringToDouble(isolate()->unicode_cache(),
|
2014-04-11 07:27:25 +00:00
|
|
|
chars,
|
|
|
|
NO_FLAGS, // Hex, octal or trailing junk.
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::nan_value());
|
2011-10-05 11:09:34 +00:00
|
|
|
} else {
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<uint8_t> buffer = Vector<uint8_t>::New(length);
|
2011-10-05 11:09:34 +00:00
|
|
|
String::WriteToFlat(*source_, buffer.start(), beg_pos, position_);
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<const uint8_t> result =
|
|
|
|
Vector<const uint8_t>(buffer.start(), length);
|
2011-10-05 11:09:34 +00:00
|
|
|
number = StringToDouble(isolate()->unicode_cache(),
|
2014-04-11 07:27:25 +00:00
|
|
|
result,
|
2013-01-09 15:47:53 +00:00
|
|
|
NO_FLAGS, // Hex, octal or trailing junk.
|
|
|
|
0.0);
|
2011-10-05 11:09:34 +00:00
|
|
|
buffer.Dispose();
|
|
|
|
}
|
2011-06-08 08:09:48 +00:00
|
|
|
SkipWhitespace();
|
2012-10-23 08:06:28 +00:00
|
|
|
return factory()->NewNumber(number, pretenure_);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 10:54:20 +00:00
|
|
|
|
|
|
|
template <typename StringType>
|
|
|
|
inline void SeqStringSet(Handle<StringType> seq_str, int i, uc32 c);
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void SeqStringSet(Handle<SeqTwoByteString> seq_str, int i, uc32 c) {
|
|
|
|
seq_str->SeqTwoByteStringSet(i, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2012-11-15 13:31:27 +00:00
|
|
|
inline void SeqStringSet(Handle<SeqOneByteString> seq_str, int i, uc32 c) {
|
|
|
|
seq_str->SeqOneByteStringSet(i, c);
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StringType>
|
2012-10-23 08:06:28 +00:00
|
|
|
inline Handle<StringType> NewRawString(Factory* factory,
|
|
|
|
int length,
|
|
|
|
PretenureFlag pretenure);
|
2011-06-29 10:54:20 +00:00
|
|
|
|
|
|
|
template <>
|
2012-10-23 08:06:28 +00:00
|
|
|
inline Handle<SeqTwoByteString> NewRawString(Factory* factory,
|
|
|
|
int length,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-03 12:30:08 +00:00
|
|
|
return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked();
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2012-11-15 13:31:27 +00:00
|
|
|
inline Handle<SeqOneByteString> NewRawString(Factory* factory,
|
2012-10-23 08:06:28 +00:00
|
|
|
int length,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-03 12:30:08 +00:00
|
|
|
return factory->NewRawOneByteString(length, pretenure).ToHandleChecked();
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
2011-10-05 11:09:34 +00:00
|
|
|
template <typename StringType, typename SinkChar>
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> JsonParser<seq_one_byte>::SlowScanJsonString(
|
2011-06-29 10:54:20 +00:00
|
|
|
Handle<String> 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));
|
2013-03-28 11:19:38 +00:00
|
|
|
Handle<StringType> seq_string =
|
2012-10-23 08:06:28 +00:00
|
|
|
NewRawString<StringType>(factory(), length, pretenure_);
|
2011-06-29 10:54:20 +00:00
|
|
|
// Copy prefix into seq_str.
|
2013-03-28 11:19:38 +00:00
|
|
|
SinkChar* dest = seq_string->GetChars();
|
2011-06-29 10:54:20 +00:00
|
|
|
String::WriteToFlat(*prefix, dest, start, end);
|
2011-06-08 08:09:48 +00:00
|
|
|
|
|
|
|
while (c0_ != '"') {
|
2011-08-05 12:55:29 +00:00
|
|
|
// Check for control character (0x00-0x1f) or unterminated string (<0).
|
|
|
|
if (c0_ < 0x20) return Handle<String>::null();
|
2011-06-29 10:54:20 +00:00
|
|
|
if (count >= length) {
|
|
|
|
// We need to create a longer sequential string for the result.
|
2013-03-28 11:19:38 +00:00
|
|
|
return SlowScanJsonString<StringType, SinkChar>(seq_string, 0, count);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
if (c0_ != '\\') {
|
2011-06-29 10:54:20 +00:00
|
|
|
// If the sink can contain UC16 characters, or source_ contains only
|
2014-09-10 12:38:12 +00:00
|
|
|
// Latin1 characters, there's no need to test whether we can store the
|
2011-06-29 10:54:20 +00:00
|
|
|
// character. Otherwise check whether the UC16 source character can fit
|
2014-09-10 12:38:12 +00:00
|
|
|
// in the Latin1 sink.
|
|
|
|
if (sizeof(SinkChar) == kUC16Size || seq_one_byte ||
|
2013-01-09 10:30:54 +00:00
|
|
|
c0_ <= String::kMaxOneByteCharCode) {
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, c0_);
|
2011-06-29 10:54:20 +00:00
|
|
|
Advance();
|
|
|
|
} else {
|
2014-09-10 12:38:12 +00:00
|
|
|
// StringType is SeqOneByteString and we just read a non-Latin1 char.
|
2013-03-28 11:19:38 +00:00
|
|
|
return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string, 0, count);
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
2011-06-08 08:09:48 +00:00
|
|
|
} else {
|
2011-10-05 11:09:34 +00:00
|
|
|
Advance(); // Advance past the \.
|
2011-06-08 08:09:48 +00:00
|
|
|
switch (c0_) {
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, c0_);
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, '\x08');
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, '\x0c');
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, '\x0a');
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, '\x0d');
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, '\x09');
|
2011-06-08 08:09:48 +00:00
|
|
|
break;
|
|
|
|
case 'u': {
|
|
|
|
uc32 value = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
Advance();
|
|
|
|
int digit = HexValue(c0_);
|
|
|
|
if (digit < 0) {
|
|
|
|
return Handle<String>::null();
|
|
|
|
}
|
|
|
|
value = value * 16 + digit;
|
|
|
|
}
|
2013-01-09 10:30:54 +00:00
|
|
|
if (sizeof(SinkChar) == kUC16Size ||
|
|
|
|
value <= String::kMaxOneByteCharCode) {
|
2013-03-28 11:19:38 +00:00
|
|
|
SeqStringSet(seq_string, count++, value);
|
2011-06-29 10:54:20 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2014-09-10 12:38:12 +00:00
|
|
|
// StringType is SeqOneByteString and we just read a non-Latin1
|
|
|
|
// char.
|
2011-06-29 10:54:20 +00:00
|
|
|
position_ -= 6; // Rewind position_ to \ in \uxxxx.
|
|
|
|
Advance();
|
2013-03-28 11:19:38 +00:00
|
|
|
return SlowScanJsonString<SeqTwoByteString, uc16>(seq_string,
|
2011-10-05 11:09:34 +00:00
|
|
|
0,
|
|
|
|
count);
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Handle<String>::null();
|
|
|
|
}
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 11:19:38 +00:00
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ('"', c0_);
|
2011-06-29 10:54:20 +00:00
|
|
|
// Advance past the last '"'.
|
|
|
|
AdvanceSkipWhitespace();
|
2013-03-28 11:19:38 +00:00
|
|
|
|
|
|
|
// Shrink seq_string length to count and return.
|
|
|
|
return SeqString::Truncate(seq_string, count);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 10:54:20 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
template <bool seq_one_byte>
|
2013-02-28 17:03:34 +00:00
|
|
|
template <bool is_internalized>
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> JsonParser<seq_one_byte>::ScanJsonString() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ('"', c0_);
|
2011-06-08 08:09:48 +00:00
|
|
|
Advance();
|
2011-06-29 10:54:20 +00:00
|
|
|
if (c0_ == '"') {
|
|
|
|
AdvanceSkipWhitespace();
|
2012-11-05 10:50:36 +00:00
|
|
|
return factory()->empty_string();
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
2012-09-24 14:23:46 +00:00
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
if (seq_one_byte && is_internalized) {
|
2013-02-28 17:03:34 +00:00
|
|
|
// 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.
|
2012-09-24 14:23:46 +00:00
|
|
|
uint32_t running_hash = isolate()->heap()->HashSeed();
|
|
|
|
int position = position_;
|
|
|
|
uc32 c0 = c0_;
|
|
|
|
do {
|
|
|
|
if (c0 == '\\') {
|
2012-10-18 13:15:05 +00:00
|
|
|
c0_ = c0;
|
|
|
|
int beg_pos = position_;
|
|
|
|
position_ = position;
|
2013-01-09 15:47:53 +00:00
|
|
|
return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
|
2013-03-28 11:19:38 +00:00
|
|
|
beg_pos,
|
|
|
|
position_);
|
2012-09-24 14:23:46 +00:00
|
|
|
}
|
2012-10-18 11:47:06 +00:00
|
|
|
if (c0 < 0x20) return Handle<String>::null();
|
2012-12-19 13:27:20 +00:00
|
|
|
if (static_cast<uint32_t>(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);
|
|
|
|
}
|
2012-09-24 14:23:46 +00:00
|
|
|
position++;
|
2012-09-25 16:59:27 +00:00
|
|
|
if (position >= source_length_) return Handle<String>::null();
|
2012-11-15 13:31:27 +00:00
|
|
|
c0 = seq_source_->SeqOneByteStringGet(position);
|
2012-09-24 14:23:46 +00:00
|
|
|
} while (c0 != '"');
|
|
|
|
int length = position - position_;
|
|
|
|
uint32_t hash = (length <= String::kMaxHashCalcLength)
|
2014-08-05 13:23:55 +00:00
|
|
|
? StringHasher::GetHashCore(running_hash)
|
|
|
|
: static_cast<uint32_t>(length);
|
2013-01-09 10:30:54 +00:00
|
|
|
Vector<const uint8_t> string_vector(
|
2013-01-09 15:47:53 +00:00
|
|
|
seq_source_->GetChars() + position_, length);
|
2013-02-28 17:03:34 +00:00
|
|
|
StringTable* string_table = isolate()->heap()->string_table();
|
|
|
|
uint32_t capacity = string_table->Capacity();
|
|
|
|
uint32_t entry = StringTable::FirstProbe(hash, capacity);
|
2012-09-24 14:23:46 +00:00
|
|
|
uint32_t count = 1;
|
2013-04-09 16:49:28 +00:00
|
|
|
Handle<String> result;
|
2012-09-24 14:23:46 +00:00
|
|
|
while (true) {
|
2013-02-28 17:03:34 +00:00
|
|
|
Object* element = string_table->KeyAt(entry);
|
2012-10-25 11:52:37 +00:00
|
|
|
if (element == isolate()->heap()->undefined_value()) {
|
2012-09-24 14:23:46 +00:00
|
|
|
// Lookup failure.
|
2013-04-09 16:49:28 +00:00
|
|
|
result = factory()->InternalizeOneByteString(
|
|
|
|
seq_source_, position_, length);
|
2012-09-24 14:23:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-10-25 11:52:37 +00:00
|
|
|
if (element != isolate()->heap()->the_hole_value() &&
|
2013-01-09 10:30:54 +00:00
|
|
|
String::cast(element)->IsOneByteEqualTo(string_vector)) {
|
2013-04-09 16:49:28 +00:00
|
|
|
result = Handle<String>(String::cast(element), isolate());
|
|
|
|
#ifdef DEBUG
|
|
|
|
uint32_t hash_field =
|
|
|
|
(hash << String::kHashShift) | String::kIsNotArrayIndexMask;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(static_cast<int>(result->Hash()),
|
2013-04-09 16:49:28 +00:00
|
|
|
static_cast<int>(hash_field >> String::kHashShift));
|
|
|
|
#endif
|
|
|
|
break;
|
2012-09-24 14:23:46 +00:00
|
|
|
}
|
2013-02-28 17:03:34 +00:00
|
|
|
entry = StringTable::NextProbe(entry, count++, capacity);
|
2012-09-24 14:23:46 +00:00
|
|
|
}
|
2013-04-09 16:49:28 +00:00
|
|
|
position_ = position;
|
|
|
|
// Advance past the last '"'.
|
|
|
|
AdvanceSkipWhitespace();
|
|
|
|
return result;
|
2012-09-24 14:23:46 +00:00
|
|
|
}
|
|
|
|
|
2011-06-22 14:20:23 +00:00
|
|
|
int beg_pos = position_;
|
2014-09-10 12:38:12 +00:00
|
|
|
// Fast case for Latin1 only without escape characters.
|
2011-06-29 10:54:20 +00:00
|
|
|
do {
|
2011-06-08 08:09:48 +00:00
|
|
|
// Check for control character (0x00-0x1f) or unterminated string (<0).
|
|
|
|
if (c0_ < 0x20) return Handle<String>::null();
|
2011-06-29 10:54:20 +00:00
|
|
|
if (c0_ != '\\') {
|
2014-09-10 12:38:12 +00:00
|
|
|
if (seq_one_byte || c0_ <= String::kMaxOneByteCharCode) {
|
2011-06-29 10:54:20 +00:00
|
|
|
Advance();
|
|
|
|
} else {
|
2011-10-05 11:09:34 +00:00
|
|
|
return SlowScanJsonString<SeqTwoByteString, uc16>(source_,
|
|
|
|
beg_pos,
|
|
|
|
position_);
|
2011-06-29 10:54:20 +00:00
|
|
|
}
|
2011-06-08 08:09:48 +00:00
|
|
|
} else {
|
2013-01-09 15:47:53 +00:00
|
|
|
return SlowScanJsonString<SeqOneByteString, uint8_t>(source_,
|
|
|
|
beg_pos,
|
|
|
|
position_);
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
2011-06-29 10:54:20 +00:00
|
|
|
} while (c0_ != '"');
|
|
|
|
int length = position_ - beg_pos;
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<String> result =
|
|
|
|
factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked();
|
2013-04-09 16:49:28 +00:00
|
|
|
uint8_t* dest = SeqOneByteString::cast(*result)->GetChars();
|
|
|
|
String::WriteToFlat(*source_, dest, beg_pos, position_);
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ('"', c0_);
|
2011-06-08 08:09:48 +00:00
|
|
|
// Advance past the last '"'.
|
|
|
|
AdvanceSkipWhitespace();
|
2011-06-29 10:54:20 +00:00
|
|
|
return result;
|
2011-06-08 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:16:23 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_JSON_PARSER_H_
|