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 #include "ecmascript/snapshot/mem/slot_bit.h"
17
18 #include "ecmascript/js_hclass-inl.h"
19 #include "ecmascript/js_tagged_value.h"
20
21 namespace panda::ecmascript {
22 /* static */
GetObjectType(TaggedObject * objectHeader)23 uint8_t SerializeHelper::GetObjectType(TaggedObject *objectHeader)
24 {
25 auto hclass = objectHeader->GetClass();
26 return static_cast<uint8_t>(JSHClass::Cast(hclass)->GetObjectType());
27 }
28
29 /* static */
AddObjectHeaderToData(TaggedObject * objectHeader,CQueue<TaggedObject * > * queue,std::unordered_map<uint64_t,ecmascript::SlotBit> * data,size_t index)30 SlotBit SerializeHelper::AddObjectHeaderToData(TaggedObject *objectHeader, CQueue<TaggedObject *> *queue,
31 std::unordered_map<uint64_t, ecmascript::SlotBit> *data, size_t index)
32 {
33 queue->emplace(objectHeader);
34 SlotBit slotBits(data->size());
35 slotBits.SetObjectInConstantsIndex(index);
36 data->emplace(ToUintPtr(objectHeader), slotBits);
37 return slotBits;
38 }
39
AddTaggedObjectRangeToData(ObjectSlot start,ObjectSlot end,CQueue<TaggedObject * > * queue,std::unordered_map<uint64_t,ecmascript::SlotBit> * data)40 void SerializeHelper::AddTaggedObjectRangeToData(ObjectSlot start, ObjectSlot end, CQueue<TaggedObject *> *queue,
41 std::unordered_map<uint64_t, ecmascript::SlotBit> *data)
42 {
43 size_t index = 0;
44 while (start < end) {
45 JSTaggedValue object(start.GetTaggedType());
46 index++;
47 start++;
48 if (object.IsHeapObject()) {
49 SlotBit slotBit(0);
50 if (data->find(object.GetRawData()) == data->end()) {
51 slotBit = AddObjectHeaderToData(object.GetTaggedObject(), queue, data, index);
52 }
53 }
54 }
55 }
56 } // namespace panda::ecmascript
57