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 #ifndef OHOS_ABILITY_RUNTIME_ETS_ENVIRONMENT_H 17 #define OHOS_ABILITY_RUNTIME_ETS_ENVIRONMENT_H 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 #include <uv.h> 23 24 #include "ets_exception_callback.h" 25 #include "ets_interface.h" 26 #include "ets_native_reference.h" 27 #include "event_handler.h" 28 29 #ifndef ETS_EXPORT 30 #ifndef __WINDOWS__ 31 #define ETS_EXPORT __attribute__((visibility("default"))) 32 #else 33 #define ETS_EXPORT __declspec(dllexport) 34 #endif 35 #endif 36 37 namespace OHOS { 38 namespace EtsEnv { 39 struct ETSRuntimeAPI { 40 ani_status (*ANI_GetCreatedVMs)(ani_vm **vms_buffer, ani_size vms_buffer_length, ani_size *result); 41 ani_status (*ANI_CreateVM)(const ani_options *options, uint32_t version, ani_vm **result); 42 }; 43 44 class ETSEnvironment final : public std::enable_shared_from_this<ETSEnvironment> { 45 public: ETSEnvironment()46 ETSEnvironment() {}; 47 48 static std::unique_ptr<ETSEnvironment> &GetInstance(); 49 static void InitETSSDKNS(const std::string &path); 50 static void InitETSSysNS(const std::string &path); 51 static ETSEnvFuncs *RegisterFuncs(); 52 53 bool Initialize(); 54 void RegisterUncaughtExceptionHandler(const ETSUncaughtExceptionInfo &handle); 55 ani_env *GetAniEnv(); 56 bool HandleUncaughtError(); 57 bool PreloadModule(const std::string &modulePath); 58 bool LoadModule(const std::string &modulePath, const std::string &srcEntrance, void *&cls, 59 void *&obj, void *&ref); 60 bool FinishPreload(); 61 bool PostFork(void *napiEnv, const std::string &aotPath); 62 bool PreloadSystemClass(const char *className); 63 64 struct VMEntry { 65 ani_vm *aniVm_; 66 ani_env *aniEnv_; VMEntryVMEntry67 VMEntry() 68 { 69 aniVm_ = nullptr; 70 aniEnv_ = nullptr; 71 } 72 }; 73 74 private: 75 bool LoadRuntimeApis(); 76 bool LoadSymbolCreateVM(void *handle, ETSRuntimeAPI &apis); 77 bool LoadSymbolANIGetCreatedVMs(void *handle, ETSRuntimeAPI &apis); 78 bool LoadBootPathFile(std::string &bootfiles); 79 std::string GetBuildId(std::string stack); 80 EtsEnv::ETSErrorObject GetETSErrorObject(); 81 std::string GetErrorProperty(ani_error aniError, const char *property); 82 bool LoadAbcLinker(ani_env *env, const std::string &modulePath, ani_class &abcCls, ani_object &abcObj); 83 static ETSRuntimeAPI lazyApis_; 84 VMEntry vmEntry_; 85 ETSUncaughtExceptionInfo uncaughtExceptionInfo_; 86 }; 87 } // namespace EtsEnv 88 } // namespace OHOS 89 #endif // OHOS_ABILITY_RUNTIME_ETS_ENVIRONMENT_H 90