1/*
2 * Copyright (c) 2024 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_OHOS_FRAMEWORK_HELPER_H
17#define ECMASCRIPT_OHOS_FRAMEWORK_HELPER_H
18
19#include <string>
20#include <set>
21#include <vector>
22
23#include "ecmascript/platform/file.h"
24#include "macros.h"
25
26namespace panda::ecmascript {
27class FrameworkHelper {
28public:
29
30    FrameworkHelper(JSThread *thread)
31        : thread_(thread),
32          vm_(thread->GetEcmaVM())
33    {
34        FilePathInit();
35    }
36
37    ~FrameworkHelper() = default;
38
39    EcmaVM *GetEcmaVM() const
40    {
41        return vm_;
42    }
43
44    void SetCompilerFrameworkAotPath(std::string frameworkAotPath)
45    {
46        CompilerFrameworkAotPath_ = std::move(frameworkAotPath);
47    }
48
49    static void GetRealRecordName(CString &recordName)
50    {
51        if (recordName == "_GLOBAL::func_main_0") {
52            recordName = "_GLOBAL";
53        }
54    }
55
56    const std::set<std::string> &GetFrameworkAbcFiles() const
57    {
58        return frameworkAbcFiles_;
59    }
60
61    bool IsFrameworkAbcFile(const std::string& fileName) const
62    {
63        return frameworkAbcFiles_.find(fileName) != frameworkAbcFiles_.end();
64    }
65
66private:
67    void FilePathInit()
68    {
69        const std::vector<std::string> arkuiFileNames = {"/stateMgmt.abc"};
70        if (vm_->GetJSOptions().WasSetCompilerFrameworkAbcPath()) {
71            CompilerFrameworkAotPath_ = vm_->GetJSOptions().GetCompilerFrameworkAbcPath();
72        }
73        if (vm_->GetJSOptions().IsEnableFrameworkAOT()) {
74            for (const auto &name : arkuiFileNames) {
75                std::string fileName = CompilerFrameworkAotPath_ + name;
76                if (FileExist(fileName.c_str())) {
77                    frameworkAbcFiles_.insert(fileName);
78                }
79            }
80        }
81    }
82
83    [[maybe_unused]] JSThread *thread_ {nullptr};
84    EcmaVM *vm_ {nullptr};
85
86    std::set<std::string> frameworkAbcFiles_ {};
87    std::string CompilerFrameworkAotPath_ {"/etc/abc/framework"};
88};
89}  // namespace panda::ecmascript::kungfu
90#endif