• 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 #ifndef ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H
17 #define ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H
18 
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/base/json_helper.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/mem/c_containers.h"
25 
26 namespace panda::ecmascript::base {
27 class JsonStringifier {
28     using TransformType = base::JsonHelper::TransformType;
29 public:
30     JsonStringifier() = default;
31 
32     explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL)
thread_(thread)33         : thread_(thread), transformType_(transformType) {}
34 
35     ~JsonStringifier() = default;
36     NO_COPY_SEMANTIC(JsonStringifier);
37     NO_MOVE_SEMANTIC(JsonStringifier);
38 
39     JSHandle<JSTaggedValue> Stringify(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer,
40                                       const JSHandle<JSTaggedValue> &gap);
41 
42 private:
43     CString ValueToQuotedString(CString str);
44 
45     bool IsFastValueToQuotedString(const char *value);
46 
47     void AddDeduplicateProp(const JSHandle<JSTaggedValue> &property);
48 
49     JSTaggedValue SerializeJSONProperty(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer);
50     JSTaggedValue GetSerializeValue(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &key,
51                                     const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer);
52     void SerializeObjectKey(const JSHandle<JSTaggedValue> &key, bool hasContent);
53 
54     bool SerializeJSONObject(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer);
55 
56     bool SerializeJSArray(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer);
57     bool SerializeJSProxy(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &replacer);
58 
59     void SerializePrimitiveRef(const JSHandle<JSTaggedValue> &primitiveRef);
60 
61     bool PushValue(const JSHandle<JSTaggedValue> &value);
62 
63     void PopValue();
64 
65     bool CalculateNumberGap(JSTaggedValue gap);
66 
67     bool CalculateStringGap(const JSHandle<EcmaString> &primString);
68     bool AppendJsonString(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent);
69     bool SerializeElements(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent);
70     bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent);
71     JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object,
72                                             const JSHandle<JSTaggedValue> &value);
73     bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value);
74     CString gap_;
75     CString result_;
76     CString indent_;
77     JSThread *thread_ {nullptr};
78     ObjectFactory *factory_ {nullptr};
79     CVector<JSHandle<JSTaggedValue>> stack_;
80     CVector<JSHandle<JSTaggedValue>> propList_;
81     JSMutableHandle<JSTaggedValue> handleKey_ {};
82     JSMutableHandle<JSTaggedValue> handleValue_ {};
83     TransformType transformType_ {};
84 };
85 }  // namespace panda::ecmascript::base
86 #endif  // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H
87