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_INL_H
17#define ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_INL_H
18
19#include "ecmascript/serializer/base_serializer.h"
20
21namespace panda::ecmascript {
22
23template<SerializeType serializeType>
24void BaseSerializer::SerializeObjectField(TaggedObject *object)
25{
26    auto visitor = [this](TaggedObject *root, ObjectSlot start, ObjectSlot end, VisitObjectArea area) {
27        switch (area) {
28            case VisitObjectArea::RAW_DATA:
29                WriteMultiRawData(start.SlotAddress(), end.SlotAddress() - start.SlotAddress());
30                break;
31            case VisitObjectArea::NATIVE_POINTER:
32                if (serializeType == SerializeType::VALUE_SERIALIZE) {
33                    WriteMultiRawData(start.SlotAddress(), end.SlotAddress() - start.SlotAddress());
34                }
35                break;
36            case VisitObjectArea::IN_OBJECT: {
37                SerializeInObjField(root, start, end);
38                break;
39            }
40            default:
41                SerializeTaggedObjField(serializeType, root, start, end);
42                break;
43        }
44    };
45
46    ObjectXRay::VisitObjectBody<VisitType::ALL_VISIT>(object, object->GetClass(), visitor);
47}
48
49template<SerializeType serializeType>
50void BaseSerializer::SerializeTaggedObject(TaggedObject *object)
51{
52    JSHClass *hclass = object->GetClass();
53    size_t objectSize = hclass->SizeFromJSHClass(object);
54    SerializedObjectSpace space = GetSerializedObjectSpace(object);
55    data_->WriteUint8(SerializeData::EncodeNewObject(space));
56    data_->WriteUint32(objectSize);
57    data_->CalculateSerializedObjectSize(space, objectSize);
58    referenceMap_.emplace(object, objectIndex_++);
59
60    SerializeObjectField<serializeType>(object);
61}
62}
63
64#endif  // ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_INL_H