11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/builtins-utils-inl.h"
61cb0ef41Sopenharmony_ci#include "src/builtins/builtins.h"
71cb0ef41Sopenharmony_ci#include "src/json/json-parser.h"
81cb0ef41Sopenharmony_ci#include "src/json/json-stringifier.h"
91cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace internal {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// ES6 section 24.3.1 JSON.parse.
161cb0ef41Sopenharmony_ciBUILTIN(JsonParse) {
171cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
181cb0ef41Sopenharmony_ci  Handle<Object> source = args.atOrUndefined(isolate, 1);
191cb0ef41Sopenharmony_ci  Handle<Object> reviver = args.atOrUndefined(isolate, 2);
201cb0ef41Sopenharmony_ci  Handle<String> string;
211cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
221cb0ef41Sopenharmony_ci                                     Object::ToString(isolate, source));
231cb0ef41Sopenharmony_ci  string = String::Flatten(isolate, string);
241cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(
251cb0ef41Sopenharmony_ci      isolate, String::IsOneByteRepresentationUnderneath(*string)
261cb0ef41Sopenharmony_ci                   ? JsonParser<uint8_t>::Parse(isolate, string, reviver)
271cb0ef41Sopenharmony_ci                   : JsonParser<uint16_t>::Parse(isolate, string, reviver));
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// ES6 section 24.3.2 JSON.stringify.
311cb0ef41Sopenharmony_ciBUILTIN(JsonStringify) {
321cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
331cb0ef41Sopenharmony_ci  Handle<Object> object = args.atOrUndefined(isolate, 1);
341cb0ef41Sopenharmony_ci  Handle<Object> replacer = args.atOrUndefined(isolate, 2);
351cb0ef41Sopenharmony_ci  Handle<Object> indent = args.atOrUndefined(isolate, 3);
361cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate,
371cb0ef41Sopenharmony_ci                           JsonStringify(isolate, object, replacer, indent));
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci}  // namespace internal
411cb0ef41Sopenharmony_ci}  // namespace v8
42