• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 #include "src/builtins/builtins-utils-inl.h"
6 #include "src/builtins/builtins.h"
7 #include "src/counters.h"
8 #include "src/json-parser.h"
9 #include "src/json-stringifier.h"
10 #include "src/objects-inl.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 // ES6 section 24.3.1 JSON.parse.
BUILTIN(JsonParse)16 BUILTIN(JsonParse) {
17   HandleScope scope(isolate);
18   Handle<Object> source = args.atOrUndefined(isolate, 1);
19   Handle<Object> reviver = args.atOrUndefined(isolate, 2);
20   Handle<String> string;
21   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
22                                      Object::ToString(isolate, source));
23   string = String::Flatten(isolate, string);
24   RETURN_RESULT_OR_FAILURE(
25       isolate, string->IsSeqOneByteString()
26                    ? JsonParser<true>::Parse(isolate, string, reviver)
27                    : JsonParser<false>::Parse(isolate, string, reviver));
28 }
29 
30 // ES6 section 24.3.2 JSON.stringify.
BUILTIN(JsonStringify)31 BUILTIN(JsonStringify) {
32   HandleScope scope(isolate);
33   Handle<Object> object = args.atOrUndefined(isolate, 1);
34   Handle<Object> replacer = args.atOrUndefined(isolate, 2);
35   Handle<Object> indent = args.atOrUndefined(isolate, 3);
36   RETURN_RESULT_OR_FAILURE(isolate,
37                            JsonStringify(isolate, object, replacer, indent));
38 }
39 
40 }  // namespace internal
41 }  // namespace v8
42