1 /* 2 * Copyright (c) 2023 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_SERIALIZER_BASE_SERIALIZER_H 17 #define ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_H 18 19 #include "ecmascript/mem/object_xray.h" 20 #include "ecmascript/serializer/serialize_data.h" 21 22 namespace panda::ecmascript { 23 24 class Ecmavm; 25 class JSThread; 26 class BaseSerializer { 27 public: BaseSerializer(JSThread * thread)28 explicit BaseSerializer(JSThread *thread) : thread_(thread), vm_(thread->GetEcmaVM()), objXRay_(vm_) 29 { 30 data_.reset(new SerializeData(thread)); 31 } ~BaseSerializer()32 virtual ~BaseSerializer() 33 { 34 referenceMap_.clear(); 35 } 36 NO_COPY_SEMANTIC(BaseSerializer); 37 NO_MOVE_SEMANTIC(BaseSerializer); 38 39 void SerializeJSTaggedValue(JSTaggedValue value); Release()40 std::unique_ptr<SerializeData> Release() 41 { 42 return std::move(data_); 43 } 44 45 protected: 46 // Different serialize mode can implement this interface to custom processing 47 virtual void SerializeObjectImpl(TaggedObject *object, bool isWeak = false) = 0; 48 void WriteMultiRawData(uintptr_t beginAddr, size_t fieldSize); 49 template<SerializeType serializeType> 50 void SerializeTaggedObject(TaggedObject *object); 51 bool SerializeReference(TaggedObject *object); 52 bool SerializeRootObject(TaggedObject *object); 53 template<SerializeType serializeType> 54 void SerializeObjectField(TaggedObject *object); 55 bool SerializeSpecialObjIndividually(JSType objectType, TaggedObject *root, ObjectSlot start, ObjectSlot end); 56 void SerializeHClassFieldIndividually(TaggedObject *root, ObjectSlot start, ObjectSlot end); 57 void SerializeSFunctionFieldIndividually(TaggedObject *root, ObjectSlot start, ObjectSlot end); 58 void SerializeLexicalEnvFieldIndividually(TaggedObject *root, ObjectSlot start, ObjectSlot end); 59 void SerializeAsyncFunctionFieldIndividually(TaggedObject *root, ObjectSlot start, ObjectSlot end); 60 void SerializeMethodFieldIndividually(TaggedObject *root, ObjectSlot start, ObjectSlot end); 61 void SerializeObjectProto(JSHClass *kclass, JSTaggedValue proto); 62 void SerializeTaggedObjField(SerializeType serializeType, TaggedObject *root, ObjectSlot start, ObjectSlot end); 63 void SerializeInObjField(TaggedObject *object, ObjectSlot start, ObjectSlot end); 64 SerializedObjectSpace GetSerializedObjectSpace(TaggedObject *object) const; 65 66 protected: 67 JSThread *thread_; 68 EcmaVM *vm_; 69 ObjectXRay objXRay_; 70 std::unique_ptr<SerializeData> data_; 71 CUnorderedMap<TaggedObject *, uint32_t> referenceMap_; 72 size_t objectIndex_ {0}; 73 static constexpr size_t PARENT_ENV_SLOT = sizeof(TaggedObject); 74 static constexpr size_t SCOPE_INFO_SLOT = PARENT_ENV_SLOT * 2; // 2: the second object slot of lexical env 75 int32_t serializeSharedEvent_ = 0; 76 }; 77 } 78 79 #endif // ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_H