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
|
|
|
|
2016-02-10 11:28:13 +00:00
|
|
|
#ifndef V8_JSON_PARSER_H_
|
|
|
|
#define V8_JSON_PARSER_H_
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2015-08-18 17:55:19 +00:00
|
|
|
#include "src/factory.h"
|
2016-05-23 10:35:28 +00:00
|
|
|
#include "src/objects.h"
|
2011-05-24 12:16:23 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2015-04-01 16:58:32 +00:00
|
|
|
enum ParseElementResult { kElementFound, kElementNotFound, kNullHandle };
|
|
|
|
|
2016-05-31 05:43:39 +00:00
|
|
|
class JsonParseInternalizer BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
static MaybeHandle<Object> Internalize(Isolate* isolate,
|
|
|
|
Handle<Object> object,
|
|
|
|
Handle<Object> reviver);
|
|
|
|
|
|
|
|
private:
|
|
|
|
JsonParseInternalizer(Isolate* isolate, Handle<JSReceiver> reviver)
|
|
|
|
: isolate_(isolate), reviver_(reviver) {}
|
|
|
|
|
|
|
|
MaybeHandle<Object> InternalizeJsonProperty(Handle<JSReceiver> holder,
|
|
|
|
Handle<String> key);
|
|
|
|
|
|
|
|
bool RecurseAndApply(Handle<JSReceiver> holder, Handle<String> name);
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
Handle<JSReceiver> reviver_;
|
|
|
|
};
|
2015-04-01 16:58:32 +00:00
|
|
|
|
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:
|
2016-05-31 05:43:39 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> Parse(Isolate* isolate,
|
|
|
|
Handle<String> source,
|
|
|
|
Handle<Object> reviver) {
|
|
|
|
Handle<Object> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
|
|
|
|
JsonParser(isolate, source).ParseJson(), Object);
|
|
|
|
if (reviver->IsCallable()) {
|
|
|
|
return JsonParseInternalizer::Internalize(isolate, result, reviver);
|
|
|
|
}
|
|
|
|
return result;
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const int kEndOfString = -1;
|
|
|
|
|
|
|
|
private:
|
2016-05-31 05:43:39 +00:00
|
|
|
JsonParser(Isolate* isolate, Handle<String> source);
|
2013-06-26 13:36:16 +00:00
|
|
|
|
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
|
|
|
|
2016-05-23 10:35:28 +00:00
|
|
|
INLINE(void Advance());
|
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.
|
|
|
|
|
2016-05-23 10:35:28 +00:00
|
|
|
INLINE(void AdvanceSkipWhitespace());
|
|
|
|
INLINE(void SkipWhitespace());
|
|
|
|
INLINE(uc32 AdvanceGetChar());
|
2011-06-01 14:06:30 +00:00
|
|
|
|
|
|
|
// Checks that current charater is c.
|
|
|
|
// If so, then consume c and skip whitespace.
|
2016-05-23 10:35:28 +00:00
|
|
|
INLINE(bool MatchSkipWhiteSpace(uc32 c));
|
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
|
|
|
|
2016-05-23 10:35:28 +00:00
|
|
|
bool ParseJsonString(Handle<String> expected);
|
2013-04-09 16:49:28 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> ParseJsonInternalizedString() {
|
2016-02-18 14:33:34 +00:00
|
|
|
Handle<String> result = ScanJsonString<true>();
|
|
|
|
if (result.is_null()) return result;
|
|
|
|
return factory()->InternalizeString(result);
|
2011-06-01 14:06:30 +00:00
|
|
|
}
|
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();
|
|
|
|
|
2015-04-01 16:58:32 +00:00
|
|
|
// Helper for ParseJsonObject. Parses the form "123": obj, which is recorded
|
|
|
|
// as an element, not a property.
|
|
|
|
ParseElementResult ParseElement(Handle<JSObject> json_object);
|
|
|
|
|
2011-05-24 12:16:23 +00:00
|
|
|
// 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
|
|
|
|
2015-05-05 14:10:38 +00:00
|
|
|
static const int kInitialSpecialStringLength = 32;
|
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
|
|
|
};
|
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2016-02-10 11:28:13 +00:00
|
|
|
#endif // V8_JSON_PARSER_H_
|