HarmonyOS c++线程同步调用js方法有最佳实践吗?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考demo:

//hello.cpp:
#include "napi/native_api.h"
#define LOG_TAG "testTag"
#include "hilog/log.h"

static napi_ref parentRef = nullptr;
static napi_value RegObject(napi_env env, napi_callback_info info) {
  size_t argc = 1;
  napi_value parentObject;

  napi_get_cb_info(env, info, &argc, &parentObject, nullptr, nullptr);

  napi_create_reference(env, parentObject, 1, &parentRef);

  OH_LOG_INFO(LOG_APP, "end RegObject");

  return nullptr;
}

static napi_value CallObject(napi_env env, napi_callback_info info) {
  OH_LOG_INFO(LOG_APP, "enter CallObject");

  if (parentRef == nullptr) {
    OH_LOG_INFO(LOG_APP, "parentRef == nullptr");
    return nullptr;
  }
  napi_value parentObject = nullptr;

  napi_get_reference_value(env, parentRef, &parentObject);
  napi_delete_reference(env, parentRef);
  parentRef = nullptr;

  napi_value runFunc;
  napi_get_named_property(env, parentObject, "run", &runFunc);
  napi_call_function(env, parentObject, runFunc, 0, nullptr, nullptr);

  OH_LOG_INFO(LOG_APP, "end CallObject");

  return nullptr;
}

static napi_value SyncCall(napi_env env, napi_callback_info info) {
  OH_LOG_INFO(LOG_APP, "enter SyncCall");

  size_t argc = 1;
  napi_value myObject;

  napi_get_cb_info(env, info, &argc, &myObject, nullptr, nullptr);

  napi_value runFunc;
  napi_get_named_property(env, myObject, "run", &runFunc);
  napi_call_function(env, myObject, runFunc, 0, nullptr, nullptr);

  OH_LOG_INFO(LOG_APP, "end SyncCall");

  return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
  napi_property_descriptor desc[] = {
  {"regObject", nullptr, RegObject, nullptr, nullptr, nullptr, napi_default, nullptr},
{"callObject", nullptr, CallObject, nullptr, nullptr, nullptr, napi_default, nullptr},
{"syncCall", nullptr, SyncCall, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END

static napi_module demoModule = {
  .nm_version = 1,
  .nm_flags = 0,
  .nm_filename = nullptr,
  .nm_register_func = Init,
  .nm_modname = "entry",
  .nm_priv = ((void*)0),
  .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
  napi_module_register(&demoModule);
}

//CMakeLists.txt:
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(callback)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

if(DEFINED PACKAGE_FIND_FILE)
include(${PACKAGE_FIND_FILE})
endif()

include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)

add_library(entry SHARED hello.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so)

//Index.ets:
import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';
import { ParentClass} from './TestClass'
let parentObject = new ParentClass();
@Entry
@Component
struct Index {
  @State message: string = 'Register Object';
  @State message2: string = 'Call Object';
  @State message3: string = 'sync call';

  build() {
    Column() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            hilog.info(0x0000, 'testTag', 'start register');
            testNapi.regObject(parentObject);
          })
      }
      Row() {
        Text(this.message2)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            hilog.info(0x0000, 'testTag', 'start Call');
            testNapi.callObject();
          })
      }
      Row() {
        Text(this.message3)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let myObject = new ParentClass();
            hilog.info(0x0000, 'testTag', 'sync Call');
            testNapi.syncCall(myObject);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

//TestClass.ets:
import { hilog } from '@kit.PerformanceAnalysisKit';

export class BaseClass {
  baseValue: number = 1;
  run(): void {
    console.log("Base class run value:" + this.baseValue);
    hilog.info(0x0000, 'testTag', 'Base class run value:%{public}d', this.baseValue);
  }
}

export class ParentClass extends BaseClass {
  parentValue: number = 2;
  run(): void {
    console.log("Parent class run value:" + this.parentValue);
    hilog.info(0x0000, 'testTag', 'Parent class run value:%{public}d', this.parentValue);
    super.run();
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
C++同步调ArkTS里面的方法
1126浏览 • 1回复 待解决
c++侧可以直接调用ts的static方法
2135浏览 • 1回复 待解决
C++调用ArkTS 定义的方法
2024浏览 • 1回复 待解决
网络监听的最佳实践哪些?
447浏览 • 1回复 待解决
HarmonyOS jsBridge 最佳实践
282浏览 • 1回复 待解决
同步方法获取IP地址
359浏览 • 1回复 待解决
HarmonyOS 如何调用标准C++
461浏览 • 1回复 待解决