builtins-json.cc 1.47 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5
#include "src/builtins/builtins-utils-inl.h"
6
#include "src/builtins/builtins.h"
Yang Guo's avatar
Yang Guo committed
7 8
#include "src/json/json-parser.h"
#include "src/json/json-stringifier.h"
9
#include "src/logging/counters.h"
10
#include "src/objects/objects-inl.h"
11 12 13 14 15 16 17 18 19 20 21 22

namespace v8 {
namespace internal {

// ES6 section 24.3.1 JSON.parse.
BUILTIN(JsonParse) {
  HandleScope scope(isolate);
  Handle<Object> source = args.atOrUndefined(isolate, 1);
  Handle<Object> reviver = args.atOrUndefined(isolate, 2);
  Handle<String> string;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
                                     Object::ToString(isolate, source));
23
  string = String::Flatten(isolate, string);
24
  RETURN_RESULT_OR_FAILURE(
25
      isolate, String::IsOneByteRepresentationUnderneath(*string)
26 27
                   ? JsonParser<uint8_t>::Parse(isolate, string, reviver)
                   : JsonParser<uint16_t>::Parse(isolate, string, reviver));
28 29 30 31 32 33 34 35 36
}

// ES6 section 24.3.2 JSON.stringify.
BUILTIN(JsonStringify) {
  HandleScope scope(isolate);
  Handle<Object> object = args.atOrUndefined(isolate, 1);
  Handle<Object> replacer = args.atOrUndefined(isolate, 2);
  Handle<Object> indent = args.atOrUndefined(isolate, 3);
  RETURN_RESULT_OR_FAILURE(isolate,
37
                           JsonStringify(isolate, object, replacer, indent));
38 39 40 41
}

}  // namespace internal
}  // namespace v8