• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fast_json_stringifier.h"
19 #include "ecmascript/base/json_parser.h"
20 #include "ecmascript/base/json_stringifier.h"
21 #include "ecmascript/base/number_helper.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/js_handle.h"
25 #include "ecmascript/js_primitive_ref.h"
26 #include "ecmascript/js_tagged_value-inl.h"
27 #include "ecmascript/object_factory.h"
28 
29 namespace panda::ecmascript::builtins {
30 // 24.5.1
Parse(EcmaRuntimeCallInfo * argv)31 JSTaggedValue BuiltinsJson::Parse(EcmaRuntimeCallInfo *argv)
32 {
33     BUILTINS_API_TRACE(argv->GetThread(), Json, Parse);
34     ASSERT(argv);
35     JSThread *thread = argv->GetThread();
36     [[maybe_unused]] EcmaHandleScope handleScope(thread);
37 
38     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
39 
40     uint32_t argc = argv->GetArgsNumber();
41     if (argc == 0) {
42         JSHandle<JSObject> syntaxError = factory->GetJSError(base::ErrorType::SYNTAX_ERROR, "arg is empty");
43         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, syntaxError.GetTaggedValue(), JSTaggedValue::Exception());
44     }
45 
46     JSHandle<JSTaggedValue> msg = GetCallArg(argv, 0);
47     JSHandle<EcmaString> parseString = JSTaggedValue::ToString(thread, msg);
48     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
49     JSHandle<JSTaggedValue> result;
50     if (EcmaStringAccessor(parseString).IsUtf8()) {
51         panda::ecmascript::base::Utf8JsonParser parser(thread);
52         result = parser.Parse(*parseString);
53     } else {
54         panda::ecmascript::base::Utf16JsonParser parser(thread);
55         result = parser.Parse(*parseString);
56     }
57     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
58 
59     JSTaggedValue reviver = JSTaggedValue::Undefined();
60     if (argc == 2) {  // 2: 2 args
61         reviver = GetCallArg(argv, 1).GetTaggedValue();
62         if (reviver.IsCallable()) {
63             JSHandle<JSTaggedValue> callbackfnHandle(thread, reviver);
64             // Let root be ! OrdinaryObjectCreate(%Object.prototype%).
65             JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
66             JSHandle<JSFunction> constructor(env->GetObjectFunction());
67             JSHandle<JSObject> root = factory->NewJSObjectByConstructor(constructor);
68             // Let rootName be the empty String.
69             JSHandle<JSTaggedValue> rootName(factory->GetEmptyString());
70             // Perform ! CreateDataPropertyOrThrow(root, rootName, unfiltered).
71             bool success = JSObject::CreateDataProperty(thread, root, rootName, result);
72             if (success) {
73                 result = base::Internalize::InternalizeJsonProperty(thread, root, rootName, callbackfnHandle);
74                 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
75             }
76         }
77     }
78     return result.GetTaggedValue();
79 }
80 
81 // 24.5.2
Stringify(EcmaRuntimeCallInfo * argv)82 JSTaggedValue BuiltinsJson::Stringify(EcmaRuntimeCallInfo *argv)
83 {
84     BUILTINS_API_TRACE(argv->GetThread(), Json, Stringify);
85     ASSERT(argv);
86     JSThread *thread = argv->GetThread();
87     [[maybe_unused]] EcmaHandleScope handleScope(thread);
88 
89     uint32_t argc = argv->GetArgsNumber();
90     JSTaggedValue value = GetCallArg(argv, 0).GetTaggedValue();
91     if (argc == 1 && thread->GetCurrentEcmaContext()->IsAotEntry()) {
92         JSHandle<JSTaggedValue> handleValue(thread, value);
93         panda::ecmascript::base::FastJsonStringifier stringifier(thread);
94         JSHandle<JSTaggedValue> result = stringifier.Stringify(handleValue);
95         return result.GetTaggedValue();
96     }
97     JSTaggedValue replacer = JSTaggedValue::Undefined();
98     JSTaggedValue gap = JSTaggedValue::Undefined();
99 
100     if (argc == 2) {  // 2: 2 args
101         replacer = GetCallArg(argv, 1).GetTaggedValue();
102     } else if (argc == 3) {  // 3: 3 args
103         replacer = GetCallArg(argv, 1).GetTaggedValue();
104         gap = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD).GetTaggedValue();
105     }
106 
107     JSHandle<JSTaggedValue> handleValue(thread, value);
108     JSHandle<JSTaggedValue> handleReplacer(thread, replacer);
109     JSHandle<JSTaggedValue> handleGap(thread, gap);
110     panda::ecmascript::base::JsonStringifier stringifier(thread);
111     JSHandle<JSTaggedValue> result = stringifier.Stringify(handleValue, handleReplacer, handleGap);
112 
113     return result.GetTaggedValue();
114 }
115 }  // namespace panda::ecmascript::builtins
116