diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 9241f7bc7b997ef2409471386fee242e116b1281..59ef2b5f447b10feeab4cd9cb64f9a69ba267bd5 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -15,7 +15,9 @@ #include #include + #include "common-interop.h" +#include "dynamic-loader.h" #if KOALA_INTEROP_PROFILER #include "profiler.h" @@ -255,3 +257,41 @@ void impl_StopGroupedLog(KInt index) { stopGroupedLog(index); } KOALA_INTEROP_V1(StopGroupedLog, KInt) + + +typedef KInt (*LoadVirtualMachine_t)(KInt vmKind, const char* appClassPath, const char* appLibPath); +typedef KInt (*StartApplication_t)(); +typedef KInt (*RunApplication_t)(const KInt arg0, const KInt arg1); + +void* getImpl(const char* path, const char* name) { + static void* lib = nullptr; + if (!lib) { + auto name = std::string(path) + "/" + libName("vmloader"); + lib = loadLibrary(name); + if (!lib) { + fprintf(stderr, "Ensure vmloader library at %s was built\n", name.c_str()); + } + } + return findSymbol(lib, name); +} + +KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const KStringPtr& appLibPath) { + static LoadVirtualMachine_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(appClassPath.c_str(), "_LoadVirtualMachine")); + return impl(vmKind, appClassPath.c_str(), appLibPath.c_str()); +} +KOALA_INTEROP_3(LoadVirtualMachine, KInt, KInt, KStringPtr, KStringPtr) + +KInt impl_StartApplication() { + static StartApplication_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "_StartApplication")); + return impl(); +} +KOALA_INTEROP_0(StartApplication, KInt) + +KInt impl_RunApplication(const KInt arg0, const KInt arg1) { + static RunApplication_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "_RunApplication")); + return impl(arg0, arg1); +} +KOALA_INTEROP_2(RunApplication, KInt, KInt, KInt) diff --git a/interop/src/cpp/dynamic-loader.h b/interop/src/cpp/dynamic-loader.h index 58832528ed81e774111e4fd88a32a066df00f8e6..a19c5361a33e62294e9f04f139c92775af3b86ca 100644 --- a/interop/src/cpp/dynamic-loader.h +++ b/interop/src/cpp/dynamic-loader.h @@ -68,6 +68,14 @@ inline std::string libName(const char* lib) { return result; } +inline std::string symbolName(const char* symbol) { + std::string result; +#ifdef KOALA_MACOS + return "_" + std::string(symbol); +#else + return std::string(symbol); +#endif +} #else #error "Unknown platform" #endif diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index b15d14d350a57899e1a7b65e7447959e1819e174..0eac4f20a0341ff653a3254794bfa31b53fa28e6 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -15,8 +15,11 @@ #include #include + #include "dynamic-loader.h" -#include "common-interop.h" +#include "koala-types.h" + +// DO NOT USE KOALA INTEROP MECHANISMS IN THIS FILE! #ifdef KOALA_JNI #include "jni.h" @@ -26,7 +29,7 @@ #include "etsapi.h" #endif -#ifdef KOALA_LINUX +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) #include "sys/stat.h" #include "dirent.h" #endif @@ -44,7 +47,7 @@ struct VMLibInfo { const VMLibInfo javaVMLib = { .sdkPath = getenv("JAVA_HOME"), .platform = - #ifdef KOALA_LINUX + #if defined(KOALA_LINUX) || defined(KOALA_MACOS) "lib/server" #elif KOALA_WINDOWS "bin/server" @@ -63,6 +66,8 @@ const VMLibInfo pandaVMLib = { .platform = #ifdef KOALA_LINUX "linux_host_tools/lib" + #elif KOALA_MACOS + "macos_host_tools/lib" #elif KOALA_OHOS "ohos_arm64/lib" #else @@ -113,7 +118,7 @@ VMEntry g_vmEntry; typedef int (*createVM_t)(void** pVM, void** pEnv, void* vmInitArgs); -KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const KStringPtr& appLibPath) { +extern "C" KInt LoadVirtualMachine(KInt vmKind, const char* appClassPath, const char* appLibPath) { const VMLibInfo* thisVM = #ifdef KOALA_JNI (vmKind == JAVA_VM_KIND) ? &javaVMLib : @@ -143,7 +148,7 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const void* vm = nullptr; void* env = nullptr; - VMInitArgs* vmInitArgs = + VMInitArgs* vmInitArgs = #ifdef KOALA_JNI (vmKind == JAVA_VM_KIND) ? (VMInitArgs*)&javaVMArgs : #endif @@ -161,8 +166,8 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const std::vector javaVMOptions; if (vmKind == JAVA_VM_KIND) { javaVMOptions = { - {(char*)strdup((std::string("-Djava.class.path=") + appClassPath.c_str()).c_str())}, - {(char*)strdup((std::string("-Djava.library.path=") + appLibPath.c_str()).c_str())}, + {(char*)strdup((std::string("-Djava.class.path=") + appClassPath).c_str())}, + {(char*)strdup((std::string("-Djava.library.path=") + appLibPath).c_str())}, }; vmInitArgs->nOptions = javaVMOptions.size(); vmInitArgs->options = (void*)(javaVMOptions.data()); @@ -195,7 +200,7 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const g_vmEntry.vmKind = PANDA_VM_KIND; } #endif - + int res = createVM(&vm, &env, vmInitArgs); if (res != 0) { fprintf(stderr, "Error creating a VM\n"); @@ -204,7 +209,6 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const g_vmEntry.env = env; return 0; } -KOALA_INTEROP_3(LoadVirtualMachine, KInt, KInt, KStringPtr, KStringPtr) struct AppInfo { const char* className; @@ -234,7 +238,7 @@ const AppInfo pandaAppInfo = { }; #endif -KInt impl_StartApplication() { +extern "C" KInt StartApplication() { const AppInfo* appInfo = #ifdef KOALA_JNI (g_vmEntry.vmKind == JAVA_VM_KIND) ? &javaAppInfo : @@ -243,7 +247,7 @@ KInt impl_StartApplication() { (g_vmEntry.vmKind == PANDA_VM_KIND) ? &pandaAppInfo : #endif nullptr; - + if (!appInfo) { fprintf(stderr, "No appInfo provided for VM kind %d (recompile vmloader.cc with the missing flags)\n", g_vmEntry.vmKind); return -1; @@ -273,12 +277,8 @@ KInt impl_StartApplication() { #endif return -1; } -KOALA_INTEROP_0(StartApplication, KInt) -KInt impl_RunApplication( - const KInt arg0, - const KInt arg1 -) { +extern "C" KInt RunApplication(const KInt arg0, const KInt arg1) { #ifdef KOALA_JNI if (g_vmEntry.vmKind == JAVA_VM_KIND) { JNIEnv* jEnv = (JNIEnv*)(g_vmEntry.env); @@ -313,13 +313,12 @@ KInt impl_RunApplication( #endif return -1; } -KOALA_INTEROP_2(RunApplication, KInt, KInt, KInt) void traverseDir(std::string root, std::vector& paths, int depth) { if (depth >= 50) { return; } - #ifdef KOALA_LINUX +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) DIR* d = opendir(root.c_str()); struct dirent* ent = NULL; struct stat statbuf; @@ -337,5 +336,5 @@ void traverseDir(std::string root, std::vector& paths, int depth) { traverseDir(filepath, paths, depth + 1); } } - #endif +#endif }