1 /*
2  * Copyright (c) 2021-2024 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/snapshot_env.h"
17 #include "ecmascript/js_tagged_value-inl.h"
18 #include "ecmascript/global_env.h"
19 
20 namespace panda::ecmascript {
21 
AddGlobalConstToMap()22 void SnapshotEnv::AddGlobalConstToMap()
23 {
24     auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
25     for (size_t index = 0; index < globalConst->GetConstantCount(); index++) {
26         JSTaggedValue objectValue = globalConst->GetGlobalConstantObject(index);
27         if (objectValue.IsHeapObject() && !objectValue.IsString()) {
28             rootObjectMap_.emplace(objectValue.GetRawData(), index);
29         }
30     }
31 }
32 
RelocateRootObjectAddr(uint32_t index)33 JSTaggedType SnapshotEnv::RelocateRootObjectAddr(uint32_t index)
34 {
35     auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
36     size_t globalConstCount = globalConst->GetConstantCount();
37     if (index < globalConstCount) {
38         JSTaggedValue obj = globalConst->GetGlobalConstantObject(index);
39         return obj.GetRawData();
40     }
41     JSHandle<JSTaggedValue> value = vm_->GetGlobalEnv()->GetNoLazyEnvObjectByIndex(index - globalConstCount);
42     return value->GetRawData();
43 }
44 
Iterate(const RootVisitor &v, VMRootVisitType type)45 void SnapshotEnv::Iterate(const RootVisitor &v, VMRootVisitType type)
46 {
47     if (multiThreadCheckValue_.exchange(JSThread::GetCurrentThreadId()) != 0) {
48         LOG_ECMA(FATAL) << "SnapshotEnv push multi-thread check fail, thread id: " << multiThreadCheckValue_;
49     }
50     if (type == VMRootVisitType::UPDATE_ROOT) {
51         // during update root, rootObjectMap_ key need update
52         std::unordered_map<JSTaggedType, uint32_t> rootObjectMap(rootObjectMap_.size());
53         for (auto &it : rootObjectMap_) {
54             auto objectAddr = it.first;
55             ObjectSlot slot(reinterpret_cast<uintptr_t>(&objectAddr));
56             v(Root::ROOT_VM, slot);
57             rootObjectMap.emplace(slot.GetTaggedType(), it.second);
58         }
59         std::swap(rootObjectMap_, rootObjectMap);
60         rootObjectMap.clear();
61     } else {
62         for (auto &it : rootObjectMap_) {
63             ObjectSlot slot(reinterpret_cast<uintptr_t>(&it));
64             v(Root::ROOT_VM, slot);
65         }
66     }
67     multiThreadCheckValue_ = 0;
68 }
69 }  // namespace panda::ecmascript
70 
71