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 #ifndef ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 17 #define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 18 19 #include <unordered_map> 20 21 #include "ecmascript/mem/object_xray.h" 22 #include "ecmascript/mem/visitor.h" 23 #include "libpandabase/macros.h" 24 25 namespace panda::ecmascript { 26 class EcmaVM; 27 28 class SnapshotEnv final { 29 public: SnapshotEnv(EcmaVM * vm)30 explicit SnapshotEnv(EcmaVM *vm) : vm_(vm), objXRay_(vm) {} 31 ~SnapshotEnv() = default; 32 33 void Initialize(); 34 35 void Iterate(const RootVisitor &v); 36 ClearEnvMap()37 void ClearEnvMap() 38 { 39 objectVector_.clear(); 40 } 41 GetEnvObjectIndex(uintptr_t objectAddr)42 size_t GetEnvObjectIndex(uintptr_t objectAddr) const 43 { 44 auto it = std::find(objectVector_.begin(), objectVector_.end(), objectAddr); 45 if (it == objectVector_.end()) { 46 return MAX_UINT_32; 47 } else { 48 return std::distance(objectVector_.begin(), it); 49 } 50 } 51 FindEnvObjectByIndex(size_t index)52 uintptr_t FindEnvObjectByIndex(size_t index) 53 { 54 ASSERT(index < objectVector_.size()); 55 return objectVector_.at(index); 56 } 57 58 static constexpr size_t MAX_UINT_32 = 0xFFFFFFFF; 59 60 private: 61 NO_MOVE_SEMANTIC(SnapshotEnv); 62 NO_COPY_SEMANTIC(SnapshotEnv); 63 64 void HandleObjectField(TaggedObject *objectHeader, CQueue<TaggedObject *> *objectQueue, 65 std::set<TaggedObject *> *objectSet); 66 void InitGlobalConst(); 67 void InitGlobalEnv(); 68 69 EcmaVM *vm_; 70 ObjectXRay objXRay_; 71 CVector<uintptr_t> objectVector_; 72 }; 73 } // namespace panda::ecmascript 74 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 75