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 SerializeJSONSharedMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 57 58 bool SerializeJSONSharedSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 59 60 bool SerializeJSONMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 61 62 bool SerializeJSONSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 63 64 bool SerializeJSONHashMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 65 66 bool SerializeJSONHashSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 67 68 bool SerializeJSArray(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 69 70 bool SerializeJSProxy(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &replacer); 71 72 void SerializePrimitiveRef(const JSHandle<JSTaggedValue> &primitiveRef); 73 74 bool PushValue(const JSHandle<JSTaggedValue> &value); 75 76 void PopValue(); 77 78 bool CalculateNumberGap(JSTaggedValue gap); 79 80 bool CalculateStringGap(const JSHandle<EcmaString> &primString); 81 bool AppendJsonString(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 82 bool SerializeElements(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 83 bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 84 JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object, 85 const JSHandle<JSTaggedValue> &value); 86 bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value); 87 CString gap_; 88 CString result_; 89 CString indent_; 90 JSThread *thread_ {nullptr}; 91 ObjectFactory *factory_ {nullptr}; 92 CVector<JSHandle<JSTaggedValue>> stack_; 93 CVector<JSHandle<JSTaggedValue>> propList_; 94 JSMutableHandle<JSTaggedValue> handleKey_ {}; 95 JSMutableHandle<JSTaggedValue> handleValue_ {}; 96 TransformType transformType_ {}; 97 }; 98 } // namespace panda::ecmascript::base 99 #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 100