Commit 1f4ca7a2 authored by jiangyh's avatar jiangyh

lib-airth支持harmony端

parent d84d12ae
......@@ -43,11 +43,19 @@ kotlin {
dependsOn(iosMain)
}
val ohosArm64Main by getting {
dependsOn(commonMain)
dependencies {
implementation(project(":lib-arith"))
}
}
androidMain.dependencies {
implementation(libs.androidx.activity.compose)
implementation(project(":lib-arith"))
}
commonMain.dependencies {
// 保持为空:不在 commonMain 引入 lib-arith
}
commonTest.dependencies {
implementation(libs.kotlin.test)
......
......@@ -4,9 +4,20 @@ package com.example.testdemo
import kotlinx.cinterop.ExperimentalForeignApi
import kotlin.experimental.ExperimentalNativeApi
import com.example.arith.Arith
// 既然去掉了 Compose,我们先通过 NAPI 返回一个简单的标识,证明 Kotlin 代码已成功运行
@CName("getKotlinGreeting")
fun getKotlinGreeting(): String {
return Greeting().greet() + " (HarmonyOS Native)"
}
@CName("getKotlinUiText")
fun getKotlinUiText(): String = ohosUiText()
fun ohosUiText(): String {
val base = appInfo()
val add = Arith.add(7, 3)
val sub = Arith.sub(7, 3)
return "$base\nAdd is $add, Sub is $sub"
}
......@@ -74,6 +74,7 @@ typedef struct {
} libkn_kref_com_example_testdemo_OhosPlatform;
extern const char* getKotlinGreeting();
extern const char* getKotlinUiText();
typedef struct {
/* Service functions. */
......@@ -128,6 +129,8 @@ typedef struct {
} OhosPlatform;
const char* (*appInfo)();
const char* (*getKotlinGreeting_)();
const char* (*getKotlinUiText_)();
const char* (*ohosUiText)();
libkn_kref_com_example_testdemo_Platform (*getPlatform)();
} testdemo;
} example;
......
......@@ -3,10 +3,13 @@
static void* g_lib_handle = nullptr;
typedef const char* (*get_kotlin_greeting_t)();
static get_kotlin_greeting_t g_get_kotlin_greeting = nullptr;
static get_kotlin_greeting_t g_get_kotlin_greeting = nullptr
;
typedef const char* (*get_kotlin_ui_text_t)();
static get_kotlin_ui_text_t g_get_kotlin_ui_text = nullptr;
static bool LoadLibKn() {
if (g_get_kotlin_greeting) return true;
if (g_get_kotlin_greeting && g_get_kotlin_ui_text) return true;
if (!g_lib_handle) {
g_lib_handle = dlopen("libkn.so", RTLD_LAZY);
if (!g_lib_handle) {
......@@ -16,7 +19,10 @@ static bool LoadLibKn() {
g_get_kotlin_greeting = reinterpret_cast<get_kotlin_greeting_t>(
dlsym(g_lib_handle, "getKotlinGreeting")
);
return g_get_kotlin_greeting != nullptr;
g_get_kotlin_ui_text = reinterpret_cast<get_kotlin_ui_text_t>(
dlsym(g_lib_handle, "getKotlinUiText")
);
return g_get_kotlin_greeting != nullptr && g_get_kotlin_ui_text != nullptr;
}
static napi_value GetKotlinGreeting(napi_env env, napi_callback_info /*info*/) {
......@@ -34,11 +40,29 @@ static napi_value GetKotlinGreeting(napi_env env, napi_callback_info /*info*/) {
return out;
}
static napi_value GetKotlinUiText(napi_env env, napi_callback_info /*info*/) {
if (!LoadLibKn()) {
napi_throw_error(env, nullptr, "dlopen/dlsym libkn.so|getKotlinUiText 失败");
return nullptr;
}
const char* s = g_get_kotlin_ui_text();
if (!s) {
napi_throw_error(env, nullptr, "getKotlinUiText 返回空指针");
return nullptr;
}
napi_value out;
napi_create_string_utf8(env, s, NAPI_AUTO_LENGTH, &out);
return out;
}
static napi_value Init(napi_env env, napi_value exports) {
// 仅导出 getKotlinGreeting
napi_value fn;
napi_create_function(env, "getKotlinGreeting", NAPI_AUTO_LENGTH, GetKotlinGreeting, nullptr, &fn);
napi_set_named_property(env, exports, "getKotlinGreeting", fn);
napi_value fn2;
napi_create_function(env, "getKotlinUiText", NAPI_AUTO_LENGTH, GetKotlinUiText, nullptr, &fn2);
napi_set_named_property(env, exports, "getKotlinUiText", fn2);
return exports;
}
......
......@@ -30,25 +30,32 @@ static napi_value Add(napi_env env, napi_callback_info info)
static void* g_lib_handle = nullptr;
typedef const char* (*get_kotlin_greeting_t)();
static get_kotlin_greeting_t g_get_kotlin_greeting = nullptr;
typedef const char* (*get_kotlin_ui_text_t)();
static get_kotlin_ui_text_t g_get_kotlin_ui_text = nullptr;
static bool LoadLibKn() {
if (g_get_kotlin_greeting) return true;
if (!g_lib_handle) {
g_lib_handle = dlopen("libkn.so", RTLD_LAZY);
if (!g_lib_handle) {
// 如需调试可输出 dlerror()
// 可选:printf("%s\n", dlerror());
return false;
}
}
// 必须与 Kotlin 的 @CName("getKotlinGreeting") 完全一致
g_get_kotlin_greeting = reinterpret_cast<get_kotlin_greeting_t>(
dlsym(g_lib_handle, "getKotlinGreeting")
);
return g_get_kotlin_greeting != nullptr;
if (!g_get_kotlin_greeting) {
g_get_kotlin_greeting = reinterpret_cast<get_kotlin_greeting_t>(
dlsym(g_lib_handle, "getKotlinGreeting")
);
}
if (!g_get_kotlin_ui_text) {
g_get_kotlin_ui_text = reinterpret_cast<get_kotlin_ui_text_t>(
dlsym(g_lib_handle, "getKotlinUiText")
);
}
return true;
}
static napi_value NAPI_Global_getKotlinGreeting(napi_env env, napi_callback_info info) {
if (!LoadLibKn()) {
static napi_value NAPI_Global_getKotlinGreeting(napi_env env, napi_callback_info /*info*/) {
if (!LoadLibKn() || !g_get_kotlin_greeting) {
napi_throw_error(env, nullptr, "dlopen/dlsym libkn.so|getKotlinGreeting 失败");
return nullptr;
}
......@@ -61,15 +68,26 @@ static napi_value NAPI_Global_getKotlinGreeting(napi_env env, napi_callback_info
napi_create_string_utf8(env, s, NAPI_AUTO_LENGTH, &out);
return out;
}
static napi_value NAPI_Global_getKotlinGreeting1(napi_env env, napi_callback_info info) {
// TODO: implements the code;
static napi_value NAPI_Global_getKotlinUiText(napi_env env, napi_callback_info /*info*/) {
if (!LoadLibKn() || !g_get_kotlin_ui_text) {
napi_throw_error(env, nullptr, "dlopen/dlsym libkn.so|getKotlinUiText 失败");
return nullptr;
}
const char* s = g_get_kotlin_ui_text();
if (!s) {
napi_throw_error(env, nullptr, "getKotlinUiText 返回空指针");
return nullptr;
}
napi_value out;
napi_create_string_utf8(env, s, NAPI_AUTO_LENGTH, &out);
return out;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {
{"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
{"getKotlinGreeting", nullptr, NAPI_Global_getKotlinGreeting, nullptr, nullptr, nullptr, napi_default, nullptr},
{"getKotlinGreeting", nullptr, NAPI_Global_getKotlinGreeting1, nullptr, nullptr, nullptr, napi_default, nullptr }
{"getKotlinUiText", nullptr, NAPI_Global_getKotlinUiText, nullptr, nullptr, nullptr, napi_default, nullptr}
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
......
export const getKotlinGreeting: () => string;
\ No newline at end of file
export const getKotlinGreeting: () => string;
export const getKotlinUiText: () => string;
\ No newline at end of file
declare module 'libentry.so' {
export const getKotlinGreeting: () => string;
export const getKotlinUiText: () => string;
}
\ No newline at end of file
......@@ -36,7 +36,7 @@ struct Index {
refreshGreeting() {
try {
this.kotlinMsg = testNapi.getKotlinGreeting();
this.kotlinMsg = testNapi.getKotlinUiText();
} catch (e) {
this.kotlinMsg = "调用失败: " + (e?.message ?? String(e));
}
......
......@@ -8,7 +8,7 @@ kotlin {
iosArm64()
iosSimulatorArm64()
iosX64()
ohosArm64()
sourceSets {
val commonMain by getting
......@@ -17,7 +17,7 @@ kotlin {
val iosArm64Main by getting { dependsOn(iosMain) }
val iosSimulatorArm64Main by getting { dependsOn(iosMain) }
val iosX64Main by getting { dependsOn(iosMain) }
val ohosArm64Main by getting { dependsOn(commonMain) }
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment