/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H #define ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H #include "ecmascript/js_tagged_value.h" #include "ecmascript/js_handle.h" #include "ecmascript/object_factory.h" #include "ecmascript/global_env.h" #include "ecmascript/mem/c_containers.h" namespace panda::ecmascript::base { class JsonStringifier { public: explicit JsonStringifier() = default; explicit JsonStringifier(JSThread *thread) : thread_(thread) {} ~JsonStringifier() = default; NO_COPY_SEMANTIC(JsonStringifier); NO_MOVE_SEMANTIC(JsonStringifier); JSHandle Stringify(const JSHandle &value, const JSHandle &replacer, const JSHandle &gap); private: CString ValueToQuotedString(CString str); bool IsFastValueToQuotedString(const char *value); void AddDeduplicateProp(const JSHandle &property); JSTaggedValue SerializeJSONProperty(const JSHandle &value, const JSHandle &replacer); JSTaggedValue GetSerializeValue(const JSHandle &object, const JSHandle &key, const JSHandle &value, const JSHandle &replacer); void SerializeObjectKey(const JSHandle &key, bool hasContent); bool SerializeJSONObject(const JSHandle &value, const JSHandle &replacer); bool SerializeJSArray(const JSHandle &value, const JSHandle &replacer); bool SerializeJSProxy(const JSHandle &object, const JSHandle &replacer); void SerializePrimitiveRef(const JSHandle &primitiveRef); bool PushValue(const JSHandle &value); void PopValue(); bool CalculateNumberGap(JSTaggedValue gap); bool CalculateStringGap(const JSHandle &primString); bool AppendJsonString(const JSHandle &obj, const JSHandle &replacer, bool hasContent); bool SerializeElements(const JSHandle &obj, const JSHandle &replacer, bool hasContent); bool SerializeKeys(const JSHandle &obj, const JSHandle &replacer, bool hasContent); static inline bool CompareKey(const std::pair, PropertyAttributes> &a, const std::pair, PropertyAttributes> &b) { return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder(); } static inline bool CompareNumber(const JSHandle &a, const JSHandle &b) { return a->GetNumber() < b->GetNumber(); } CString gap_; CString result_; CString indent_; JSThread *thread_ {nullptr}; ObjectFactory *factory_ {nullptr}; CVector> stack_; CVector> propList_; JSMutableHandle handleKey_ {}; JSMutableHandle handleValue_ {}; }; } // namespace panda::ecmascript::base #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H