14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/snapshot/mem/snapshot_env.h"
174514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_cinamespace panda::ecmascript {
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_civoid SnapshotEnv::AddGlobalConstToMap()
234514f5e3Sopenharmony_ci{
244514f5e3Sopenharmony_ci    auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
254514f5e3Sopenharmony_ci    for (size_t index = 0; index < globalConst->GetConstantCount(); index++) {
264514f5e3Sopenharmony_ci        JSTaggedValue objectValue = globalConst->GetGlobalConstantObject(index);
274514f5e3Sopenharmony_ci        if (objectValue.IsHeapObject() && !objectValue.IsString()) {
284514f5e3Sopenharmony_ci            rootObjectMap_.emplace(objectValue.GetRawData(), index);
294514f5e3Sopenharmony_ci        }
304514f5e3Sopenharmony_ci    }
314514f5e3Sopenharmony_ci}
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ciJSTaggedType SnapshotEnv::RelocateRootObjectAddr(uint32_t index)
344514f5e3Sopenharmony_ci{
354514f5e3Sopenharmony_ci    auto globalConst = const_cast<GlobalEnvConstants *>(vm_->GetJSThread()->GlobalConstants());
364514f5e3Sopenharmony_ci    size_t globalConstCount = globalConst->GetConstantCount();
374514f5e3Sopenharmony_ci    if (index < globalConstCount) {
384514f5e3Sopenharmony_ci        JSTaggedValue obj = globalConst->GetGlobalConstantObject(index);
394514f5e3Sopenharmony_ci        return obj.GetRawData();
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = vm_->GetGlobalEnv()->GetNoLazyEnvObjectByIndex(index - globalConstCount);
424514f5e3Sopenharmony_ci    return value->GetRawData();
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_civoid SnapshotEnv::Iterate(const RootVisitor &v, VMRootVisitType type)
464514f5e3Sopenharmony_ci{
474514f5e3Sopenharmony_ci    if (multiThreadCheckValue_.exchange(JSThread::GetCurrentThreadId()) != 0) {
484514f5e3Sopenharmony_ci        LOG_ECMA(FATAL) << "SnapshotEnv push multi-thread check fail, thread id: " << multiThreadCheckValue_;
494514f5e3Sopenharmony_ci    }
504514f5e3Sopenharmony_ci    if (type == VMRootVisitType::UPDATE_ROOT) {
514514f5e3Sopenharmony_ci        // during update root, rootObjectMap_ key need update
524514f5e3Sopenharmony_ci        std::unordered_map<JSTaggedType, uint32_t> rootObjectMap(rootObjectMap_.size());
534514f5e3Sopenharmony_ci        for (auto &it : rootObjectMap_) {
544514f5e3Sopenharmony_ci            auto objectAddr = it.first;
554514f5e3Sopenharmony_ci            ObjectSlot slot(reinterpret_cast<uintptr_t>(&objectAddr));
564514f5e3Sopenharmony_ci            v(Root::ROOT_VM, slot);
574514f5e3Sopenharmony_ci            rootObjectMap.emplace(slot.GetTaggedType(), it.second);
584514f5e3Sopenharmony_ci        }
594514f5e3Sopenharmony_ci        std::swap(rootObjectMap_, rootObjectMap);
604514f5e3Sopenharmony_ci        rootObjectMap.clear();
614514f5e3Sopenharmony_ci    } else {
624514f5e3Sopenharmony_ci        for (auto &it : rootObjectMap_) {
634514f5e3Sopenharmony_ci            ObjectSlot slot(reinterpret_cast<uintptr_t>(&it));
644514f5e3Sopenharmony_ci            v(Root::ROOT_VM, slot);
654514f5e3Sopenharmony_ci        }
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci    multiThreadCheckValue_ = 0;
684514f5e3Sopenharmony_ci}
694514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
704514f5e3Sopenharmony_ci
71