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 ES2PANDA_EVALUATE_ENTITY_DECLARATOR_INL_H
17 #define ES2PANDA_EVALUATE_ENTITY_DECLARATOR_INL_H
18
19 #include "evaluate/scopedDebugInfoPlugin.h"
20 #include "evaluate/entityDeclarator.h"
21 #include "evaluate/irCheckHelper.h"
22
23 #include <utility>
24
25 namespace ark::es2panda::evaluate {
26
27 template <typename F>
ImportGlobalEntity(util::StringView pathToDeclSource, util::StringView declName, parser::Program *importerProgram, util::StringView importedName, F &&irCreator)28 varbinder::Variable *EntityDeclarator::ImportGlobalEntity(util::StringView pathToDeclSource, util::StringView declName,
29 parser::Program *importerProgram,
30 util::StringView importedName, F &&irCreator)
31 {
32 ASSERT(importerProgram);
33 parser::Program *program = debugInfoPlugin_.GetProxyProgramsCache()->GetProgram(pathToDeclSource);
34
35 helpers::SafeStateScope s(debugInfoPlugin_.GetIrCheckHelper()->GetChecker(), debugInfoPlugin_.GetETSBinder());
36
37 auto &entitiesMap = GetOrCreateEntitiesMap(program);
38 auto *var = FindEntityVariable(entitiesMap, declName);
39
40 if (var == nullptr) {
41 var = std::invoke(std::forward<F>(irCreator), debugInfoPlugin_.GetDebugInfoDeserializer(), program,
42 pathToDeclSource, declName);
43 if (var != nullptr) {
44 if (!entitiesMap.emplace(declName, var).second) {
45 LOG(FATAL, ES2PANDA) << "Failed to emplace " << importedName << " in entity map.";
46 }
47 }
48 }
49
50 if (var != nullptr && program != importerProgram) {
51 CreateAndInsertImportStatement(pathToDeclSource, declName, importerProgram, importedName, var);
52 }
53 return var;
54 }
55
56 } // namespace ark::es2panda::evaluate
57
58 #endif // ES2PANDA_EVALUATE_ENTITY_DECLARATOR_INL_H
59