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/linked_hash_table.h" 23 #include "ecmascript/object_factory.h" 24 #include "ecmascript/global_env.h" 25 #include "ecmascript/mem/c_containers.h" 26 27 namespace panda::ecmascript::base { 28 class JsonStringifier { 29 using TransformType = base::JsonHelper::TransformType; 30 public: 31 JsonStringifier() = default; 32 33 #if ENABLE_NEXT_OPTIMIZATION 34 explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) thread_(thread)35 : thread_(thread), transformType_(transformType) 36 { 37 encoding_ = Encoding::ONE_BYTE_ENCODING; 38 indent_ = 0; 39 } 40 #else 41 explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) thread_(thread)42 : thread_(thread), transformType_(transformType) {} 43 #endif 44 45 ~JsonStringifier() = default; 46 NO_COPY_SEMANTIC(JsonStringifier); 47 NO_MOVE_SEMANTIC(JsonStringifier); 48 49 JSHandle<JSTaggedValue> Stringify(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer, 50 const JSHandle<JSTaggedValue> &gap); 51 52 private: 53 void AddDeduplicateProp(const JSHandle<JSTaggedValue> &property); 54 55 JSTaggedValue SerializeJSONProperty(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 56 JSTaggedValue GetSerializeValue(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &key, 57 const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 58 void SerializeObjectKey(const JSHandle<JSTaggedValue> &key, bool hasContent); 59 60 bool SerializeJSONObject(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 61 62 bool SerializeJSONSharedMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 63 64 bool SerializeJSONSharedSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 65 66 bool SerializeJSONMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 67 68 bool SerializeJSONSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 69 70 bool SerializeJSONHashMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 71 72 bool SerializeJSONHashSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 73 74 bool SerializeLinkedHashMap(const JSHandle<LinkedHashMap> &hashMap, const JSHandle<JSTaggedValue> &replacer); 75 76 bool SerializeLinkedHashSet(const JSHandle<LinkedHashSet> &hashSet, const JSHandle<JSTaggedValue> &replacer); 77 78 bool SerializeJSArray(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 79 80 bool SerializeJSProxy(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &replacer); 81 82 void SerializePrimitiveRef(const JSHandle<JSTaggedValue> &primitiveRef); 83 84 bool PushValue(const JSHandle<JSTaggedValue> &value); 85 86 void PopValue(); 87 88 bool CalculateNumberGap(JSTaggedValue gap); 89 90 bool CalculateStringGap(const JSHandle<EcmaString> &primString); 91 bool AppendJsonString(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 92 bool SerializeElements(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 93 bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 94 JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object, 95 const JSHandle<JSTaggedValue> &value); 96 bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value); 97 98 #if ENABLE_NEXT_OPTIMIZATION Indent()99 inline void Indent() 100 { 101 indent_++; 102 } 103 Unindent()104 inline void Unindent() 105 { 106 indent_--; 107 } 108 109 inline void NewLine(); 110 111 void ChangeEncoding(); 112 113 template <size_t N> 114 inline void AppendLiteral(const char(&src)[N]); 115 116 inline void AppendChar(const char src); 117 118 inline void AppendNumberToResult(const JSHandle<JSTaggedValue> &value); 119 inline void AppendIntToResult(int32_t key); 120 inline void AppendEcmaStringToResult(JSHandle<EcmaString> &string); 121 inline void AppendBigIntToResult(JSHandle<JSTaggedValue> &valHandle); 122 inline void PopBack(); 123 124 enum class Encoding { 125 ONE_BYTE_ENCODING, 126 TWO_BYTE_ENCODING 127 }; 128 Encoding encoding_; 129 CString oneByteResult_; 130 C16String twoBytesResult_; 131 C16String gap_; 132 int indent_; 133 #else 134 CString gap_; 135 CString result_; 136 CString indent_; 137 #endif 138 139 JSThread *thread_ {nullptr}; 140 ObjectFactory *factory_ {nullptr}; 141 CVector<JSHandle<JSTaggedValue>> stack_; 142 CVector<JSHandle<JSTaggedValue>> propList_; 143 JSMutableHandle<JSTaggedValue> handleKey_ {}; 144 JSMutableHandle<JSTaggedValue> handleValue_ {}; 145 TransformType transformType_ {}; 146 }; 147 } // namespace panda::ecmascript::base 148 #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 149