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 #include "ecmascript/snapshot/mem/snapshot_env.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20
21 namespace panda::ecmascript {
Initialize()22 void SnapshotEnv::Initialize()
23 {
24 InitGlobalConst();
25 InitGlobalEnv();
26 }
27
InitializeStringClass()28 void SnapshotEnv::InitializeStringClass()
29 {
30 auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
31 auto lineStringClass = globalConst->GetLineStringClass();
32 rootObjectMap_.emplace(ToUintPtr(lineStringClass.GetTaggedObject()), globalConst->GetLineStringClassIndex());
33 auto constStringClass = globalConst->GetConstantStringClass();
34 rootObjectMap_.emplace(ToUintPtr(constStringClass.GetTaggedObject()), globalConst->GetConstStringClassIndex());
35 }
36
InitGlobalConst()37 void SnapshotEnv::InitGlobalConst()
38 {
39 auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
40 for (size_t index = 0; index < globalConst->GetConstantCount(); index++) {
41 JSTaggedValue objectValue = globalConst->GetGlobalConstantObject(index);
42 if (objectValue.IsHeapObject()) {
43 rootObjectMap_.emplace(ToUintPtr(objectValue.GetTaggedObject()), index);
44 }
45 }
46 }
47
InitGlobalEnv()48 void SnapshotEnv::InitGlobalEnv()
49 {
50 auto globalEnv = vm_->GetGlobalEnv();
51 auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
52 size_t globalEnvIndexStart = globalConst->GetConstantCount();
53 for (size_t index = 0; index < globalEnv->GetGlobalEnvFieldSize(); index++) {
54 JSHandle<JSTaggedValue> objectValue = globalEnv->GetGlobalEnvObjectByIndex(index);
55 if (objectValue->IsHeapObject() && !objectValue->IsInternalAccessor()) {
56 rootObjectMap_.emplace(ToUintPtr(objectValue->GetTaggedObject()), index + globalEnvIndexStart);
57 }
58 }
59 }
60
Iterate(const RootVisitor & v)61 void SnapshotEnv::Iterate(const RootVisitor &v)
62 {
63 for (auto &it : rootObjectMap_) {
64 v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&(it.first))));
65 }
66 }
67 } // namespace panda::ecmascript
68
69