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 void InitializeStringClass(); 35 ClearEnvMap()36 void ClearEnvMap() 37 { 38 rootObjectMap_.clear(); 39 } 40 41 void Iterate(const RootVisitor &v); 42 FindEnvObjectIndex(uintptr_t objectAddr)43 uint32_t FindEnvObjectIndex(uintptr_t objectAddr) const 44 { 45 if (rootObjectMap_.find(objectAddr) != rootObjectMap_.end()) { 46 return rootObjectMap_.find(objectAddr)->second; 47 } 48 return MAX_UINT_32; 49 } 50 RelocateRootObjectAddr(uint32_t index)51 JSTaggedType RelocateRootObjectAddr(uint32_t index) 52 { 53 auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants()); 54 size_t globalConstCount = globalConst->GetConstantCount(); 55 if (index < globalConstCount) { 56 JSTaggedValue obj = globalConst->GetGlobalConstantObject(index); 57 return obj.GetRawData(); 58 } 59 JSHandle<JSTaggedValue> value = vm_->GetGlobalEnv()->GetNoLazyEnvObjectByIndex(index - globalConstCount); 60 return value->GetRawData(); 61 } 62 63 static constexpr size_t MAX_UINT_32 = 0xFFFFFFFF; 64 65 private: 66 NO_MOVE_SEMANTIC(SnapshotEnv); 67 NO_COPY_SEMANTIC(SnapshotEnv); 68 69 void InitGlobalConst(); 70 void InitGlobalEnv(); 71 72 EcmaVM *vm_; 73 ObjectXRay objXRay_; 74 CUnorderedMap<uintptr_t, uint32_t> rootObjectMap_; 75 }; 76 } // namespace panda::ecmascript 77 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 78