/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "common.h" #include #include #include "public/es2panda_lib.h" #include "dynamic-loader.h" // NOLINTBEGIN using std::string, std::cout, std::endl, std::vector; static es2panda_Impl const *impl = nullptr; #ifdef _WIN32 #include #define PLUGIN_DIR "windows_host_tools" #define LIB_PREFIX "lib" #define LIB_SUFFIX ".dll" #else #include #ifdef __x86_64__ #define PLUGIN_DIR "linux_host_tools" #else #define PLUGIN_DIR "linux_arm64_host_tools" #endif #define LIB_PREFIX "lib" #define LIB_SUFFIX ".so" #endif const char *LIB_ES2PANDA_PUBLIC = LIB_PREFIX "es2panda_public" LIB_SUFFIX; void *FindLibrary() { std::string libraryName; char *envValue = getenv("PANDA_SDK_PATH"); if (envValue) { libraryName = string(envValue) + ("/" PLUGIN_DIR "/lib/") + LIB_ES2PANDA_PUBLIC; } else { if (g_pandaLibPath == "") { libraryName = LIB_ES2PANDA_PUBLIC; } else { libraryName = g_pandaLibPath + "/" + LIB_ES2PANDA_PUBLIC; } } return LoadLibrary(libraryName); } const es2panda_Impl *GetPublicImpl() { if (impl) { return impl; } auto library = FindLibrary(); if (!library) { std::cout << "Cannot find " << LIB_ES2PANDA_PUBLIC << endl; } auto symbol = FindSymbol(library, "es2panda_GetImpl"); if (!symbol) { std::cout << "Cannot find Impl Entry point" << endl; } impl = reinterpret_cast(symbol)(ES2PANDA_LIB_VERSION); return impl; } std::string GetString(KStringPtr ptr) { return ptr.data(); } char *GetStringCopy(KStringPtr &ptr) { return strdup(ptr.c_str()); } inline KUInt UnpackUInt(const KByte *bytes) { return (bytes[0] | (bytes[1] << 8U) | (bytes[2U] << 16U) | (bytes[3U] << 24U)); } inline std::string_view GetStringView(KStringPtr &ptr) { return std::string_view(ptr.c_str(), static_cast(ptr.length())); } KNativePointer impl_CreateConfig(KInt argc, KStringArray argvPtr, KStringPtr &pandaLibPath) { const std::size_t HEADER_LEN = 4; g_pandaLibPath = GetStringView(pandaLibPath); const char **argv = new const char *[static_cast(argc)]; std::size_t position = HEADER_LEN; std::size_t strLen; for (std::size_t i = 0; i < static_cast(argc); ++i) { strLen = UnpackUInt(argvPtr + position); position += HEADER_LEN; argv[i] = strdup(std::string(reinterpret_cast(argvPtr + position), strLen).c_str()); position += strLen; } return GetPublicImpl()->CreateConfig(argc, argv); } TS_INTEROP_3(CreateConfig, KNativePointer, KInt, KStringArray, KStringPtr) KNativePointer impl_DestroyConfig(KNativePointer configPtr) { auto config = reinterpret_cast(configPtr); GetPublicImpl()->DestroyConfig(config); return nullptr; } TS_INTEROP_1(DestroyConfig, KNativePointer, KNativePointer) KNativePointer impl_DestroyContext(KNativePointer contextPtr) { auto context = reinterpret_cast(contextPtr); GetPublicImpl()->DestroyContext(context); return nullptr; } TS_INTEROP_1(DestroyContext, KNativePointer, KNativePointer) // NOLINTEND