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