• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (obj.IsUndefined()) {
40             LOG_ECMA(FATAL) << "Relocate GlobalConstants Root undefined, index: " << index;
41         }
42         return obj.GetRawData();
43     }
44     JSHandle<JSTaggedValue> value = vm_->GetGlobalEnv()->GetNoLazyEnvObjectByIndex(index - globalConstCount);
45     if (value->IsUndefined()) {
46         LOG_ECMA(FATAL) << "Relocate GlobalEnv Root undefined, index: " << index
47                         << ", globalConstCount:" << globalConstCount;
48     }
49     return value->GetRawData();
50 }
51 
Iterate(RootVisitor & v,VMRootVisitType type)52 void SnapshotEnv::Iterate(RootVisitor &v, VMRootVisitType type)
53 {
54     if (type == VMRootVisitType::UPDATE_ROOT) {
55         // during update root, rootObjectMap_ key need update
56         std::unordered_map<JSTaggedType, uint32_t> rootObjectMap(rootObjectMap_.size());
57         for (auto &it : rootObjectMap_) {
58             auto objectAddr = it.first;
59             ObjectSlot slot(reinterpret_cast<uintptr_t>(&objectAddr));
60             v.VisitRoot(Root::ROOT_VM, slot);
61             rootObjectMap.emplace(slot.GetTaggedType(), it.second);
62         }
63         std::swap(rootObjectMap_, rootObjectMap);
64         rootObjectMap.clear();
65     } else {
66         for (auto &it : rootObjectMap_) {
67             ObjectSlot slot(reinterpret_cast<uintptr_t>(&it));
68             v.VisitRoot(Root::ROOT_VM, slot);
69         }
70     }
71 }
72 }  // namespace panda::ecmascript
73 
74