From 73a0461f69a3e7c4827783b4fb40e30eff9c66c8 Mon Sep 17 00:00:00 2001 From: lWX1337837 Date: Wed, 2 Oct 2024 16:42:01 +0300 Subject: [PATCH 1/3] Modified vmloader --- interop/src/cpp/vmloader.cc | 66 ++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index 4dd0a333d..d62c1a845 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -29,7 +29,7 @@ #include "etsapi.h" #endif -#if defined(KOALA_LINUX) || defined(KOALA_MACOS) +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) || defined(KOALA_OHOS) #include "sys/stat.h" #include "dirent.h" #endif @@ -61,7 +61,16 @@ const VMLibInfo javaVMLib = { #ifdef KOALA_ETS_NAPI const VMLibInfo pandaVMLib = { - getenv("PANDA_HOME"), + #if defined(KOALA_OHOS) + #if defined(__arm) + "/system/lib" + #else + "/data/storage/el1/bundle/libs/arm64" + #endif + #else + getenv("PANDA_HOME") + #endif + , #ifdef KOALA_LINUX "linux_host_tools/lib" #elif KOALA_MACOS @@ -117,6 +126,8 @@ int loadES2Panda(const char* appClassPath, const char* appLibPath) { return 0; } +#include "hilog/log.h" + extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassPath, const char* appLibPath) { if (vmKind == ES2PANDA_KIND) { return loadES2Panda(appClassPath, appLibPath); @@ -136,16 +147,36 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP return -1; } - std::string libPath = std::string(thisVM->sdkPath) + "/" + std::string(thisVM->platform) + "/" + libName(thisVM->lib); + createVM_t createVM = nullptr; + #ifdef KOALA_DYNAMIC_LOADER + std::string libPath = + #if defined(KOALA_LINUX) || defined(KOALA_MACOS) || defined(KOALA_WINDOWS) + std::string(thisVM->sdkPath) + "/" + std::string(thisVM->platform) + "/" + libName(thisVM->lib) + #elif defined(KOALA_OHOS) + libName(thisVM->lib) + #else + #error "Library path not specified for this platform" + #endif + ; + void *handle = loadLibrary(libPath); if (!handle) { fprintf(stderr, "Cannot load library %s: %s\n", libPath.c_str(), libraryError()); return -1; } - auto createVM = (createVM_t)findSymbol(handle, thisVM->createVM); + createVM = (createVM_t)findSymbol(handle, thisVM->createVM); + #else + #ifdef KOALA_JNI + createVM = (createVM_t)JNI_CreateJavaVM; + #endif + #ifdef KOALA_ETS_NAPI + createVM = (createVM_t)ETS_CreateVM; + #endif + #endif + if (!createVM) { - fprintf(stderr, "Cannot find %s in %s\n", thisVM->createVM, libPath.c_str()); + fprintf(stderr, "Cannot find %s\n", thisVM->createVM); return -1; } @@ -175,8 +206,8 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP // Alternatives: // - use setenv("LD_LIBRARY_PATH", ...) here - won't work // - use process.env.LD_LIBRARY_PATH=... in loader.js - won't work - // - run "LD_LIBRARY_PATH=... node lib/loader.js" - should work - // - use process.env.LD_LIBRARY_PATH=... in preloader.js, then run node loader.js - used now + // - run "LD_LIBRARY_PATH=... node lib/loader.js" - used now (linux) + // - use process.env.LD_LIBRARY_PATH=... in preloader.js, then run node loader.js - should work // - set LD_LIBRARY_PATH manually - should work // - cache the required library here: // void* libptr = dlopen((char*)strdup((std::string(appLibPath.c_str()) + "/" + std::string(libName("NativeBridgeArk"))).c_str()), RTLD_LAZY); @@ -187,7 +218,14 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP std::vector files; traverseDir(std::string(appClassPath), files); etsVMOptions = { + #if defined(KOALA_LINUX) {EtsOptionType::ETS_BOOT_FILE, (char*)strdup((std::string(thisVM->sdkPath) + "/ets/etsstdlib.abc").c_str())}, + #endif + #if defined(KOALA_OHOS) + #if defined(__arm) + {EtsOptionType::ETS_BOOT_FILE, (char*)strdup((std::string(thisVM->sdkPath) + "../etc/etsstdlib.abc").c_str())}, + #endif + #endif }; for (const std::string& path : files) { etsVMOptions.push_back({EtsOptionType::ETS_BOOT_FILE, (char*)strdup(path.c_str())}); @@ -195,7 +233,13 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP pandaVMArgs.nOptions = etsVMOptions.size(); pandaVMArgs.options = etsVMOptions.data(); g_vmEntry.vmKind = PANDA_VM_KIND; + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}d\n", etsVMOptions.size()); + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[0].extraInfo)); + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[1].extraInfo)); + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[2].extraInfo)); result = createVM(&vm, &env, &pandaVMArgs); + //result = -1; + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}d\n", result); } #endif @@ -315,7 +359,11 @@ void traverseDir(std::string root, std::vector& paths, int depth) { if (depth >= 50) { return; } -#if defined(KOALA_LINUX) || defined(KOALA_MACOS) +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) || defined(KOALA_OHOS) + std::string suffix = ".abc"; + #if defined(KOALA_OHOS) + suffix += ".so"; + #endif DIR* d = opendir(root.c_str()); struct dirent* ent = NULL; struct stat statbuf; @@ -326,7 +374,7 @@ void traverseDir(std::string root, std::vector& paths, int depth) { } std::string filepath = root + "/" + filename; stat(filepath.c_str(), &statbuf); - if (filepath.size() >= 4 && filepath.substr(filepath.size() - 4) == ".abc" && (statbuf.st_mode & S_IFMT) == S_IFREG) { + if (filepath.size() >= suffix.size() && filepath.substr(filepath.size() - suffix.size()) == suffix && (statbuf.st_mode & S_IFMT) == S_IFREG) { paths.push_back(filepath); } if ((statbuf.st_mode & S_IFMT) == S_IFDIR) { -- Gitee From 48b13629909b20ea467784de6c8c06d6ff147ec6 Mon Sep 17 00:00:00 2001 From: lWX1337837 Date: Wed, 2 Oct 2024 16:54:02 +0300 Subject: [PATCH 2/3] fix --- interop/src/cpp/vmloader.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index d62c1a845..6fa53be05 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -223,7 +223,7 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP #endif #if defined(KOALA_OHOS) #if defined(__arm) - {EtsOptionType::ETS_BOOT_FILE, (char*)strdup((std::string(thisVM->sdkPath) + "../etc/etsstdlib.abc").c_str())}, + {EtsOptionType::ETS_BOOT_FILE, (char*)strdup((std::string(thisVM->sdkPath) + "/../etc/etsstdlib.abc").c_str())}, #endif #endif }; -- Gitee From 2ba31c4d468db601ddec5dc69995cb0fdb0238d0 Mon Sep 17 00:00:00 2001 From: lWX1337837 Date: Wed, 2 Oct 2024 17:16:26 +0300 Subject: [PATCH 3/3] remove dbg --- interop/src/cpp/vmloader.cc | 8 -------- 1 file changed, 8 deletions(-) diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index 6fa53be05..3c1750ab3 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -126,8 +126,6 @@ int loadES2Panda(const char* appClassPath, const char* appLibPath) { return 0; } -#include "hilog/log.h" - extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassPath, const char* appLibPath) { if (vmKind == ES2PANDA_KIND) { return loadES2Panda(appClassPath, appLibPath); @@ -233,13 +231,7 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP pandaVMArgs.nOptions = etsVMOptions.size(); pandaVMArgs.options = etsVMOptions.data(); g_vmEntry.vmKind = PANDA_VM_KIND; - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}d\n", etsVMOptions.size()); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[0].extraInfo)); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[1].extraInfo)); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}s\n", (char*)(etsVMOptions.data()[2].extraInfo)); result = createVM(&vm, &env, &pandaVMArgs); - //result = -1; - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Koala", "%{public}d\n", result); } #endif -- Gitee