json-parser.h 5.88 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_JSON_PARSER_H_
#define V8_JSON_PARSER_H_
7

8
#include "src/factory.h"
9
#include "src/objects.h"
10 11 12 13

namespace v8 {
namespace internal {

14 15
enum ParseElementResult { kElementFound, kElementNotFound, kNullHandle };

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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_;
};
34

35
// A simple json parser.
36
template <bool seq_one_byte>
37 38
class JsonParser BASE_EMBEDDED {
 public:
39 40 41 42 43 44 45 46 47 48
  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;
49 50 51 52 53
  }

  static const int kEndOfString = -1;

 private:
54
  JsonParser(Isolate* isolate, Handle<String> source);
55

56
  // Parse a string containing a single JSON value.
57
  MaybeHandle<Object> ParseJson();
58

59
  INLINE(void Advance());
60

61 62 63 64
  // 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.

65 66 67
  INLINE(void AdvanceSkipWhitespace());
  INLINE(void SkipWhitespace());
  INLINE(uc32 AdvanceGetChar());
68 69 70

  // Checks that current charater is c.
  // If so, then consume c and skip whitespace.
71
  INLINE(bool MatchSkipWhiteSpace(uc32 c));
72 73 74 75 76

  // 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.
77
  Handle<String> ParseJsonString() {
78 79
    return ScanJsonString<false>();
  }
80

81
  bool ParseJsonString(Handle<String> expected);
82

83
  Handle<String> ParseJsonInternalizedString() {
84 85 86
    Handle<String> result = ScanJsonString<true>();
    if (result.is_null()) return result;
    return factory()->InternalizeString(result);
87
  }
88

89
  template <bool is_internalized>
90
  Handle<String> ScanJsonString();
91 92
  // 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
93
  // prefix. Called by ScanJsonString when reaching a '\' or non-Latin1 char.
94
  template <typename StringType, typename SinkChar>
95
  Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end);
96 97 98 99 100 101 102

  // 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.
103
  Handle<Object> ParseJsonNumber();
104 105 106 107 108 109 110 111 112 113

  // 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.
114
  // A JSON array doesn't allow numbers and identifiers as keys, like a
115 116 117
  // JavaScript array.
  Handle<Object> ParseJsonObject();

118 119 120 121
  // Helper for ParseJsonObject. Parses the form "123": obj, which is recorded
  // as an element, not a property.
  ParseElementResult ParseElement(Handle<JSObject> json_object);

122 123 124 125 126 127 128 129 130 131
  // 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.
132 133 134 135 136
  inline Handle<Object> ReportUnexpectedCharacter() {
    return Handle<Object>::null();
  }

  inline Isolate* isolate() { return isolate_; }
137 138
  inline Factory* factory() { return factory_; }
  inline Handle<JSFunction> object_constructor() { return object_constructor_; }
139

140
  static const int kInitialSpecialStringLength = 32;
141
  static const int kPretenureTreshold = 100 * 1024;
142

143
 private:
144 145
  Zone* zone() { return &zone_; }

146 147 148
  void CommitStateToJsonObject(Handle<JSObject> json_object, Handle<Map> map,
                               ZoneList<Handle<Object> >* properties);

149
  Handle<String> source_;
150
  int source_length_;
151
  Handle<SeqOneByteString> seq_source_;
152

153
  PretenureFlag pretenure_;
154
  Isolate* isolate_;
155
  Factory* factory_;
156
  Zone zone_;
157
  Handle<JSFunction> object_constructor_;
158
  uc32 c0_;
159
  int position_;
160 161
};

162 163
}  // namespace internal
}  // namespace v8
164

165
#endif  // V8_JSON_PARSER_H_