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