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/js_thread.h" 22 #include "ecmascript/mem/visitor.h" 23 24 namespace panda::ecmascript { 25 class EcmaVM; 26 27 class SnapshotEnv final { 28 public: SnapshotEnv(EcmaVM * vm)29 explicit SnapshotEnv(EcmaVM *vm) : vm_(vm) {} 30 ~SnapshotEnv() = default; 31 32 void AddGlobalConstToMap(); 33 ClearEnvMap()34 void ClearEnvMap() 35 { 36 globalObjectMap_.clear(); 37 } 38 39 void ProcessSnapShotEnv(const WeakRootVisitor& visitor); 40 void IteratorSnapShotEnv(WeakVisitor& visitor); 41 Push(JSTaggedType objectAddr,uint32_t index)42 void Push(JSTaggedType objectAddr, uint32_t index) 43 { 44 globalObjectMap_.emplace(objectAddr, index); 45 } 46 Remove(JSTaggedType objectAddr)47 void Remove(JSTaggedType objectAddr) 48 { 49 globalObjectMap_.erase(objectAddr); 50 } 51 FindEnvObjectIndex(JSTaggedType objectAddr)52 uint32_t FindEnvObjectIndex(JSTaggedType objectAddr) const 53 { 54 if (globalObjectMap_.find(objectAddr) != globalObjectMap_.end()) { 55 return globalObjectMap_.find(objectAddr)->second; 56 } 57 return MAX_UINT_32; 58 } 59 60 JSTaggedType RelocateRootObjectAddr(uint32_t index); 61 62 static constexpr size_t MAX_UINT_32 = 0xFFFFFFFF; 63 64 private: 65 NO_MOVE_SEMANTIC(SnapshotEnv); 66 NO_COPY_SEMANTIC(SnapshotEnv); 67 68 void InitGlobalConst(); 69 void InitGlobalEnv(); 70 71 EcmaVM *vm_; 72 std::unordered_map<JSTaggedType, uint32_t> globalObjectMap_; 73 std::atomic<uint32_t> multiThreadCheckValue_ {0}; 74 }; 75 } // namespace panda::ecmascript 76 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 77