• 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             globalObjectMap_.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 
ProcessSnapShotEnv(const WeakRootVisitor & visitor)52 void SnapshotEnv::ProcessSnapShotEnv(const WeakRootVisitor& visitor)
53 {
54     std::unordered_map<JSTaggedType, uint32_t> globalObjectMap(globalObjectMap_.size());
55     auto it = globalObjectMap_.begin();
56     while (it != globalObjectMap_.end()) {
57         JSTaggedValue globalVal = JSTaggedValue(it->first);
58         if (globalVal.IsHeapObject()) {
59             TaggedObject *obj = globalVal.GetTaggedObject();
60             auto fwd = visitor(obj);
61             if (fwd == nullptr) {
62                 ++it;
63                 continue;
64             }
65             globalObjectMap.emplace(JSTaggedValue(fwd).GetRawData(), it->second);
66         }
67         ++it;
68     }
69     std::swap(globalObjectMap_, globalObjectMap);
70     globalObjectMap.clear();
71 }
72 
73 // for cmc-gc
IteratorSnapShotEnv(WeakVisitor & visitor)74 void SnapshotEnv::IteratorSnapShotEnv(WeakVisitor& visitor)
75 {
76     // const key cannot modify.
77     std::unordered_map<JSTaggedType, uint32_t> globalObjectMap(globalObjectMap_.size());
78     auto it = globalObjectMap_.begin();
79     while (it != globalObjectMap_.end()) {
80         JSTaggedValue globalVal = JSTaggedValue(it->first);
81         if (globalVal.IsHeapObject()) {
82             bool isAlive = visitor.VisitRoot(Root::ROOT_VM,
83                 ObjectSlot(reinterpret_cast<uintptr_t>(&globalVal)));
84             if (!isAlive) {
85                 ++it;
86                 continue;
87             }
88             globalObjectMap.emplace(globalVal.GetRawData(), it->second);
89         }
90         ++it;
91     }
92     std::swap(globalObjectMap_, globalObjectMap);
93     globalObjectMap.clear();
94 }
95 }  // namespace panda::ecmascript
96 
97