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 explicit 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 SerilaizePrimitiveRef(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 CompareKey(const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & a,const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & b)69 static inline bool CompareKey(const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &a, 70 const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &b) 71 { 72 return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder(); 73 } 74 CompareNumber(const JSHandle<JSTaggedValue> & a,const JSHandle<JSTaggedValue> & b)75 static inline bool CompareNumber(const JSHandle<JSTaggedValue> &a, const JSHandle<JSTaggedValue> &b) 76 { 77 return a->GetNumber() < b->GetNumber(); 78 } 79 80 CString gap_; 81 CString result_; 82 CString indent_; 83 JSThread *thread_{nullptr}; 84 ObjectFactory *factory_{nullptr}; 85 CVector<JSHandle<JSTaggedValue>> stack_; 86 CVector<JSHandle<JSTaggedValue>> propList_; 87 JSMutableHandle<JSTaggedValue> handleKey_{}; 88 JSMutableHandle<JSTaggedValue> handleValue_{}; 89 }; 90 } // namespace panda::ecmascript::base 91 #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 92