• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 <sstream>
17 
18 #include "libpandabase/macros.h"
19 #include "libpandabase/os/system_environment.h"
20 #include "plugins/ets/runtime/ani/ani.h"
21 
22 namespace {
23 
SplitString(ani_env * env,std::string_view from,char delim)24 std::vector<ani_string> SplitString(ani_env *env, std::string_view from, char delim)
25 {
26     std::vector<ani_string> list;
27     std::istringstream iss {from.data()};
28     std::string part;
29     while (std::getline(iss, part, delim)) {
30         ani_string newString;
31         env->String_NewUTF8(part.data(), part.size(), &newString);
32         list.emplace_back(newString);
33     }
34     return list;
35 }
36 
37 }  // namespace
38 
39 extern "C" {
40 // NOLINTNEXTLINE(readability-identifier-naming)
getAppAbcFiles(ani_env * env,ani_class)41 ANI_EXPORT ani_array getAppAbcFiles(ani_env *env, [[maybe_unused]] ani_class /* unused */)
42 {
43     auto appAbcFiles = ark::os::system_environment::GetEnvironmentVar("APP_ABC_FILES");
44     const auto paths = SplitString(env, appAbcFiles, ':');
45     ASSERT(!paths.empty());
46 
47     ani_class stringClass;
48     env->FindClass("Lstd/core/String;", &stringClass);
49     ASSERT(stringClass != nullptr);
50     ani_array_ref pathsArray;
51     env->Array_New_Ref(stringClass, paths.size(), paths[0], &pathsArray);
52     for (size_t i = 1; i < paths.size(); ++i) {
53         env->Array_Set_Ref(pathsArray, i, paths[i]);
54     }
55     return pathsArray;
56 }
57 
ANI_Constructor(ani_vm * vm,uint32_t * version)58 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *version)
59 {
60     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
61     ani_env *env;
62     *version = ANI_VERSION_1;
63     if (ANI_OK != vm->GetEnv(*version, &env)) {
64         std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
65         return ANI_ERROR;
66     }
67 
68     ani_native_function function {"getAppAbcFiles", ":[Lstd/core/String;", reinterpret_cast<void *>(getAppAbcFiles)};
69 
70     std::string_view mdl = "L@spawn/spawn;";
71     ani_module spawnModule;
72     if (ANI_OK != env->FindModule(mdl.data(), &spawnModule)) {
73         std::cerr << "Not found '" << mdl << "'" << std::endl;
74         return ANI_ERROR;
75     }
76 
77     if (ANI_OK != env->Module_BindNativeFunctions(spawnModule, &function, 1)) {
78         std::cerr << "Failed binds an array of methods to the specified class" << std::endl;
79         return ANI_ERROR;
80     }
81 
82     return ANI_OK;
83 }
84 }
85