• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "helpers_runtime.h"
17 #include "libabckit/src/logger.h"
18 #include "static_core/plugins/ets/runtime/napi/ets_napi.h"
19 #include "mem_manager/mem_manager.h"
20 
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 
26 #ifndef ABCKIT_ETS_STD_LIB
27 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
28 #define ABCKIT_ETS_STD_LIB ""
29 #endif
30 
31 namespace libabckit::test::helpers {
32 
ExecuteStaticAbc(const std::string & abcPath,const std::string & klassName,const std::string & methodName)33 std::string ExecuteStaticAbc(const std::string &abcPath, const std::string &klassName, const std::string &methodName)
34 {
35     LIBABCKIT_LOG(DEBUG) << "ExecuteStaticAbc: " << abcPath << ' ' << klassName << ' ' << methodName << '\n';
36 
37     EtsEnv *env {nullptr};
38     EtsVM *vm {nullptr};
39 
40     std::vector<EtsVMOption> optionsVector;
41 
42     optionsVector = {
43         {EtsOptionType::ETS_BOOT_FILE, ABCKIT_ETS_STD_LIB},   {EtsOptionType::ETS_BOOT_FILE, abcPath.c_str()},
44         {EtsOptionType::ETS_ARK_FILE, abcPath.c_str()},       {EtsOptionType::ETS_INTERPRETER_TYPE, "cpp"},
45         {EtsOptionType::ETS_GC_TRIGGER_TYPE, "heap-trigger"}, {EtsOptionType::ETS_NO_JIT, nullptr}};
46 
47     EtsVMInitArgs vmArgs;
48     vmArgs.version = ETS_NAPI_VERSION_1_0;
49     vmArgs.options = optionsVector.data();
50     vmArgs.nOptions = static_cast<ets_int>(optionsVector.size());
51 
52     MemManager::Finalize();
53     ETS_CreateVM(&vm, &env, &vmArgs);
54 
55     auto klass = env->FindClass(klassName.c_str());
56     if (klass == nullptr) {
57         LIBABCKIT_LOG(FATAL) << "No class " << klassName << " in " << abcPath << '\n';
58     }
59     auto method = env->GetStaticp_method(klass, methodName.c_str(), nullptr);
60     if (klass == nullptr) {
61         LIBABCKIT_LOG(FATAL) << "No method " << methodName << " in " << klassName << '\n';
62     }
63 
64     std::stringstream ss;
65     {
66         CoutRedirect coutRedirect(ss.rdbuf());
67         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
68         env->CallStaticVoidMethod(klass, method);
69     }
70 
71     vm->DestroyEtsVM();
72 
73     LIBABCKIT_LOG(DEBUG) << "Output:\n" << ss.str() << '\n';
74 
75     return ss.str();
76 }
77 
78 }  // namespace libabckit::test::helpers
79