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