1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_json.h"
17
18 #include "ecmascript/base/json_parser.h"
19 #include "ecmascript/base/json_stringifier.h"
20 #include "ecmascript/base/number_helper.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_primitive_ref.h"
25 #include "ecmascript/js_tagged_value-inl.h"
26 #include "ecmascript/object_factory.h"
27
28 namespace panda::ecmascript::builtins {
29 // 24.5.1
Parse(EcmaRuntimeCallInfo * argv)30 JSTaggedValue BuiltinsJson::Parse(EcmaRuntimeCallInfo *argv)
31 {
32 BUILTINS_API_TRACE(argv->GetThread(), Json, Parse);
33 ASSERT(argv);
34 JSThread *thread = argv->GetThread();
35 [[maybe_unused]] EcmaHandleScope handleScope(thread);
36
37 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
38
39 uint32_t argc = argv->GetArgsNumber();
40 if (argc == 0) {
41 JSHandle<JSObject> syntaxError = factory->GetJSError(base::ErrorType::SYNTAX_ERROR, "arg is empty");
42 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, syntaxError.GetTaggedValue(), JSTaggedValue::Exception());
43 }
44
45 JSHandle<JSTaggedValue> msg = GetCallArg(argv, 0);
46 JSHandle<EcmaString> parseString = JSTaggedValue::ToString(thread, msg);
47 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
48 JSHandle<JSTaggedValue> result;
49 if (EcmaStringAccessor(parseString).IsUtf8()) {
50 panda::ecmascript::base::JsonParser<uint8_t> parser(thread);
51 result = parser.ParseUtf8(*parseString);
52 } else {
53 panda::ecmascript::base::JsonParser<uint16_t> parser(thread);
54 result = parser.ParseUtf16(*parseString);
55 }
56 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
57
58 JSTaggedValue reviver = JSTaggedValue::Undefined();
59 if (argc == 2) { // 2: 2 args
60 reviver = GetCallArg(argv, 1).GetTaggedValue();
61 if (reviver.IsCallable()) {
62 JSHandle<JSTaggedValue> callbackfnHandle(thread, reviver);
63 // Let root be ! OrdinaryObjectCreate(%Object.prototype%).
64 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
65 JSHandle<JSFunction> constructor(env->GetObjectFunction());
66 JSHandle<JSObject> root = factory->NewJSObjectByConstructor(constructor);
67 // Let rootName be the empty String.
68 JSHandle<JSTaggedValue> rootName(factory->GetEmptyString());
69 // Perform ! CreateDataPropertyOrThrow(root, rootName, unfiltered).
70 bool success = JSObject::CreateDataProperty(thread, root, rootName, result);
71 if (success) {
72 result = base::Internalize::InternalizeJsonProperty(thread, root, rootName, callbackfnHandle);
73 }
74 }
75 }
76 return result.GetTaggedValue();
77 }
78
79 // 24.5.2
Stringify(EcmaRuntimeCallInfo * argv)80 JSTaggedValue BuiltinsJson::Stringify(EcmaRuntimeCallInfo *argv)
81 {
82 BUILTINS_API_TRACE(argv->GetThread(), Json, Stringify);
83 ASSERT(argv);
84 JSThread *thread = argv->GetThread();
85 [[maybe_unused]] EcmaHandleScope handleScope(thread);
86
87 uint32_t argc = argv->GetArgsNumber();
88 JSTaggedValue value = GetCallArg(argv, 0).GetTaggedValue();
89 JSTaggedValue replacer = JSTaggedValue::Undefined();
90 JSTaggedValue gap = JSTaggedValue::Undefined();
91
92 if (argc == 2) { // 2: 2 args
93 replacer = GetCallArg(argv, 1).GetTaggedValue();
94 } else if (argc == 3) { // 3: 3 args
95 replacer = GetCallArg(argv, 1).GetTaggedValue();
96 gap = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD).GetTaggedValue();
97 }
98
99 JSHandle<JSTaggedValue> handleValue(thread, value);
100 JSHandle<JSTaggedValue> handleReplacer(thread, replacer);
101 JSHandle<JSTaggedValue> handleGap(thread, gap);
102 panda::ecmascript::base::JsonStringifier stringifier(thread);
103 JSHandle<JSTaggedValue> result = stringifier.Stringify(handleValue, handleReplacer, handleGap);
104
105 return result.GetTaggedValue();
106 }
107 } // namespace panda::ecmascript::builtins
108