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

HarmonyOS
2024-12-19 17:34:41
浏览
收藏 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();
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
分享
微博
QQ
微信
回复
2024-12-19 19:12:08
相关问题
HarmonyOS C++层如何同步调用JS函数 -
825浏览 • 0回复 待解决
C++同步调ArkTS里面的方法
1757浏览 • 1回复 待解决
c++侧可以直接调用ts的static方法
3189浏览 • 1回复 待解决
C++调用ArkTS 定义的方法
3413浏览 • 1回复 待解决
网络监听的最佳实践哪些?
1376浏览 • 1回复 待解决
HarmonyOS jsBridge 最佳实践
1255浏览 • 1回复 待解决