1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_JSON_PARSER_H_
6 #define V8_JSON_PARSER_H_
7 
8 #include "src/heap/factory.h"
9 #include "src/isolate.h"
10 #include "src/objects.h"
11 #include "src/zone/zone-containers.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 enum ParseElementResult { kElementFound, kElementNotFound, kNullHandle };
17 
18 class JsonParseInternalizer BASE_EMBEDDED {
19  public:
20   static MaybeHandle<Object> Internalize(Isolate* isolate,
21                                          Handle<Object> object,
22                                          Handle<Object> reviver);
23 
24  private:
JsonParseInternalizer(Isolate * isolate,Handle<JSReceiver> reviver)25   JsonParseInternalizer(Isolate* isolate, Handle<JSReceiver> reviver)
26       : isolate_(isolate), reviver_(reviver) {}
27 
28   MaybeHandle<Object> InternalizeJsonProperty(Handle<JSReceiver> holder,
29                                               Handle<String> key);
30 
31   bool RecurseAndApply(Handle<JSReceiver> holder, Handle<String> name);
32 
33   Isolate* isolate_;
34   Handle<JSReceiver> reviver_;
35 };
36 
37 // A simple json parser.
38 template <bool seq_one_byte>
39 class JsonParser BASE_EMBEDDED {
40  public:
Parse(Isolate * isolate,Handle<String> source,Handle<Object> reviver)41   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Parse(
42       Isolate* isolate, Handle<String> source, Handle<Object> reviver) {
43     Handle<Object> result;
44     ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
45                                JsonParser(isolate, source).ParseJson(), Object);
46     if (reviver->IsCallable()) {
47       return JsonParseInternalizer::Internalize(isolate, result, reviver);
48     }
49     return result;
50   }
51 
52   static const int kEndOfString = -1;
53 
54  private:
55   JsonParser(Isolate* isolate, Handle<String> source);
56 
57   // Parse a string containing a single JSON value.
58   MaybeHandle<Object> ParseJson();
59 
60   V8_INLINE void Advance();
61 
62   // The JSON lexical grammar is specified in the ECMAScript 5 standard,
63   // section 15.12.1.1. The only allowed whitespace characters between tokens
64   // are tab, carriage-return, newline and space.
65 
66   V8_INLINE void AdvanceSkipWhitespace();
67   V8_INLINE void SkipWhitespace();
68   V8_INLINE uc32 AdvanceGetChar();
69 
70   // Checks that current charater is c.
71   // If so, then consume c and skip whitespace.
72   V8_INLINE bool MatchSkipWhiteSpace(uc32 c);
73 
74   // A JSON string (production JSONString) is subset of valid JavaScript string
75   // literals. The string must only be double-quoted (not single-quoted), and
76   // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and
77   // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid.
78   bool ParseJsonString(Handle<String> expected);
79 
ParseJsonString()80   Handle<String> ParseJsonString() {
81     Handle<String> result = ScanJsonString();
82     if (result.is_null()) return result;
83     return factory()->InternalizeString(result);
84   }
85 
86   Handle<String> ScanJsonString();
87   // Creates a new string and copies prefix[start..end] into the beginning
88   // of it. Then scans the rest of the string, adding characters after the
89   // prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char.
90   template <typename StringType, typename SinkChar>
91   Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
92 
93   // A JSON number (production JSONNumber) is a subset of the valid JavaScript
94   // decimal number literals.
95   // It includes an optional minus sign, must have at least one
96   // digit before and after a decimal point, may not have prefixed zeros (unless
97   // the integer part is zero), and may include an exponent part (e.g., "e-10").
98   // Hexadecimal and octal numbers are not allowed.
99   Handle<Object> ParseJsonNumber();
100 
101   // Parse a single JSON value from input (grammar production JSONValue).
102   // A JSON value is either a (double-quoted) string literal, a number literal,
103   // one of "true", "false", or "null", or an object or array literal.
104   Handle<Object> ParseJsonValue();
105 
106   // Parse a JSON object literal (grammar production JSONObject).
107   // An object literal is a squiggly-braced and comma separated sequence
108   // (possibly empty) of key/value pairs, where the key is a JSON string
109   // literal, the value is a JSON value, and the two are separated by a colon.
110   // A JSON array doesn't allow numbers and identifiers as keys, like a
111   // JavaScript array.
112   Handle<Object> ParseJsonObject();
113 
114   // Helper for ParseJsonObject. Parses the form "123": obj, which is recorded
115   // as an element, not a property.
116   ParseElementResult ParseElement(Handle<JSObject> json_object);
117 
118   // Parses a JSON array literal (grammar production JSONArray). An array
119   // literal is a square-bracketed and comma separated sequence (possibly empty)
120   // of JSON values.
121   // A JSON array doesn't allow leaving out values from the sequence, nor does
122   // it allow a terminal comma, like a JavaScript array does.
123   Handle<Object> ParseJsonArray();
124 
125 
126   // Mark that a parsing error has happened at the current token, and
127   // return a null handle. Primarily for readability.
ReportUnexpectedCharacter()128   inline Handle<Object> ReportUnexpectedCharacter() {
129     return Handle<Object>::null();
130   }
131 
isolate()132   inline Isolate* isolate() { return isolate_; }
factory()133   inline Factory* factory() { return isolate_->factory(); }
object_constructor()134   inline Handle<JSFunction> object_constructor() { return object_constructor_; }
135 
136   static const int kInitialSpecialStringLength = 32;
137   static const int kPretenureTreshold = 100 * 1024;
138 
139  private:
zone()140   Zone* zone() { return &zone_; }
141 
142   void CommitStateToJsonObject(Handle<JSObject> json_object, Handle<Map> map,
143                                Vector<const Handle<Object>> properties);
144 
145   Handle<String> source_;
146   int source_length_;
147   Handle<SeqOneByteString> seq_source_;
148 
149   PretenureFlag pretenure_;
150   Isolate* isolate_;
151   Zone zone_;
152   Handle<JSFunction> object_constructor_;
153   uc32 c0_;
154   int position_;
155 
156   // Property handles are stored here inside ParseJsonObject.
157   ZoneVector<Handle<Object>> properties_;
158 };
159 
160 }  // namespace internal
161 }  // namespace v8
162 
163 #endif  // V8_JSON_PARSER_H_
164