fix cpp
This commit is contained in:
@@ -1 +1,2 @@
|
||||
export { CalculateAdapter } from './src/main/cpp/types/libhmcalculate/index'
|
||||
export { Calculator } from './src/main/ets/Calculator'
|
||||
export { CalculateInfo } from './src/main/ets/CalculateInfo'
|
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.4.1)
|
||||
project(hmcalculate)
|
||||
|
||||
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_definitions(-DOHOS_PLATFORM)
|
||||
|
||||
if(DEFINED PACKAGE_FIND_FILE)
|
||||
include(${PACKAGE_FIND_FILE})
|
||||
@@ -13,16 +14,61 @@ include_directories(${NATIVERENDER_ROOT_PATH}
|
||||
|
||||
add_library(hmcalculate SHARED
|
||||
napi_init.cpp
|
||||
CalculateInfo.h
|
||||
CalculateAdaptor.cpp)
|
||||
|
||||
# 根据不同的架构选择不同的库文件路径
|
||||
IF(OHOS_ARCH STREQUAL "arm64-v8a")
|
||||
set(LIB_DIR ${NATIVERENDER_ROOT_PATH}/../../../../cppLib/dist/lib/arm64-v8a)
|
||||
ELSEIF(OHOS_ARCH STREQUAL "x86_64")
|
||||
set(LIB_DIR ${NATIVERENDER_ROOT_PATH}/../../../../cppLib/dist/lib/x86_64)
|
||||
ELSE()
|
||||
MESSAGE(FATAL_ERROR "Unsupported architecture: ${OHOS_ARCH}")
|
||||
ENDIF()
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
EGL-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
EGL
|
||||
)
|
||||
|
||||
target_link_libraries(hmcalculate PUBLIC ${LIB_DIR}/libcalculate.so)
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
GLES-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
GLESv3
|
||||
)
|
||||
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
hilog-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
hilog_ndk.z
|
||||
)
|
||||
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
libnapi-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
ace_napi.z
|
||||
)
|
||||
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
libace-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
ace_ndk.z
|
||||
)
|
||||
|
||||
find_library(
|
||||
# Sets the name of the path variable.
|
||||
libuv-lib
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
uv
|
||||
)
|
||||
|
||||
target_link_libraries(hmcalculate PUBLIC
|
||||
${EGL-lib}
|
||||
${GLES-lib}
|
||||
${hilog-lib}
|
||||
${libnapi-lib}
|
||||
${libace-lib}
|
||||
${libuv-lib}
|
||||
${NATIVERENDER_ROOT_PATH}/../../../../cppLib/dist/lib/${OHOS_ARCH}/libcalculate.so)
|
@@ -1,7 +1,6 @@
|
||||
#ifndef CPPLIB_LIBRARY_H
|
||||
#define CPPLIB_LIBRARY_H
|
||||
|
||||
#include <string>
|
||||
#include "CalculateInfo.h"
|
||||
|
||||
class Calculate {
|
||||
|
@@ -5,15 +5,15 @@
|
||||
// please include "napi/native_api.h".
|
||||
|
||||
#include "CalculateAdaptor.h"
|
||||
#include "js_native_api.h"
|
||||
#include "Calculate.h"
|
||||
#include "CalculateInfo.h"
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
|
||||
CalculateAdaptor::CalculateAdaptor() {
|
||||
_calculate = &Calculate::getInstance();
|
||||
}
|
||||
|
||||
CalculateAdaptor::CalculateAdaptor(napi_env env, napi_value thisVar) {
|
||||
_calculate = &Calculate::getInstance();
|
||||
}
|
||||
|
||||
CalculateAdaptor::~CalculateAdaptor() {
|
||||
@@ -30,7 +30,6 @@ CalculateAdaptor *util_get_napi_info(napi_env env, napi_callback_info cbinfo, si
|
||||
return calculator;
|
||||
}
|
||||
|
||||
|
||||
napi_value calculate_add(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 2;
|
||||
napi_value argv[2] = {0};
|
||||
@@ -58,7 +57,57 @@ napi_value calculate_getInfo(napi_env env, napi_callback_info info) {
|
||||
info2.versionCode = calculator->_calculate->getInfo().versionCode;
|
||||
info2.versionName = calculator->_calculate->getInfo().versionName;
|
||||
|
||||
napi_value value;
|
||||
napi_value js_frame;
|
||||
napi_create_object(env, &js_frame);
|
||||
util_set_object_string_property_value(env, js_frame, "name", info2.name);
|
||||
util_set_object_string_property_value(env, js_frame, "versionName", &info2.versionName);
|
||||
util_set_object_int32_property_value(env, js_frame, "versionCode", info2.versionCode);
|
||||
|
||||
return js_frame;
|
||||
}
|
||||
|
||||
napi_value util_create_int32_value(napi_env env, int32_t arg)
|
||||
{
|
||||
napi_value value;
|
||||
napi_status status = napi_create_int32(env, arg, &value);
|
||||
return value;
|
||||
}
|
||||
|
||||
napi_status util_set_object_int32_property_value(napi_env env, napi_value object, const char *name, int32_t value)
|
||||
{
|
||||
napi_value js_value = util_create_int32_value(env, value);
|
||||
return napi_set_named_property(env, object, name, js_value);
|
||||
}
|
||||
|
||||
napi_value util_create_string_value(napi_env env, const char *arg)
|
||||
{
|
||||
napi_value value;
|
||||
napi_status status = napi_create_string_utf8(env, arg, NAPI_AUTO_LENGTH, &value);
|
||||
return value;
|
||||
}
|
||||
|
||||
napi_status util_set_object_string_property_value(napi_env env, napi_value object, const char *name, const char *value)
|
||||
{
|
||||
napi_value js_value = util_create_string_value(env, value);
|
||||
return napi_set_named_property(env, object, name, js_value);
|
||||
}
|
||||
|
||||
char *util_get_object_string_property_value(napi_env env, napi_value arg, const char *name)
|
||||
{
|
||||
napi_status status = napi_ok;
|
||||
napi_value js_result;
|
||||
status = napi_get_named_property(env, arg, name, &js_result);
|
||||
if (napi_ok == status) {
|
||||
return util_get_string_value(env, js_result);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *util_get_string_value(napi_env env, napi_value arg)
|
||||
{
|
||||
size_t len = 0;
|
||||
napi_get_value_string_utf8(env, arg, nullptr, 0, &len); // 获取字符串长度到len
|
||||
char *buf = new char[len + 1]; // 分配合适大小的char数组
|
||||
napi_get_value_string_utf8(env, arg, buf, len + 1, &len); // 获取字符串
|
||||
return buf;
|
||||
}
|
||||
|
@@ -7,9 +7,9 @@
|
||||
#ifndef HM4DEMO_CALCULATOR_H
|
||||
#define HM4DEMO_CALCULATOR_H
|
||||
|
||||
#include "CalculateInfo.h"
|
||||
#include "Calculate.h"
|
||||
#include "js_native_api.h"
|
||||
#include <stdint.h>
|
||||
|
||||
class CalculateAdaptor {
|
||||
public:
|
||||
@@ -24,5 +24,12 @@ CalculateAdaptor *util_get_napi_info(napi_env env, napi_callback_info cbinfo, si
|
||||
napi_value calculate_add(napi_env env, napi_callback_info info);
|
||||
napi_value calculate_getInfo(napi_env env, napi_callback_info info);
|
||||
|
||||
napi_value util_create_int32_value(napi_env env, int32_t arg);
|
||||
napi_status util_set_object_int32_property_value(napi_env env, napi_value object, const char *name, int32_t value);
|
||||
|
||||
napi_value util_create_string_value(napi_env env, const char *arg);
|
||||
napi_status util_set_object_string_property_value(napi_env env, napi_value object, const char *name, const char *value);
|
||||
char *util_get_object_string_property_value(napi_env env, napi_value arg, const char *name);
|
||||
char *util_get_string_value(napi_env env, napi_value arg);
|
||||
|
||||
#endif //HM4DEMO_CALCULATOR_H
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "napi/native_api.h"
|
||||
#include "CalculateAdaptor.h"
|
||||
#include "js_native_api.h"
|
||||
#include "CalculateAdaptor.h"
|
||||
|
||||
napi_value JS_Constructor(napi_env env, napi_callback_info info) {
|
||||
napi_value thisVar = nullptr;
|
||||
@@ -38,12 +38,12 @@ static napi_module demoModule = {
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = Init,
|
||||
.nm_modname = "native_lib",
|
||||
.nm_modname = "hmcalculate",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__((constructor)) void RegisterNatvie_libModule(void)
|
||||
extern "C" __attribute__((constructor)) void RegisterHmcalculateModule(void)
|
||||
{
|
||||
napi_module_register(&demoModule);
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
export class CalculateAdapter {
|
||||
export class CalculateAdaptor {
|
||||
add(a: number, b: number): number;
|
||||
getInfo(): Object;
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libhmcalculate.so",
|
||||
"types": "./index.d.ts",
|
||||
"version": "",
|
||||
"version": "1.0.0",
|
||||
"description": "Please describe the basic information."
|
||||
}
|
@@ -1,12 +1,12 @@
|
||||
import { CalculateAdapter } from 'native_lib.so'
|
||||
import { CalculateAdaptor } from 'libhmcalculate.so'
|
||||
import { CalculateInfo } from './CalculateInfo'
|
||||
|
||||
export class Calculator {
|
||||
|
||||
private adaptor!:CalculateAdapter;
|
||||
private adaptor!:CalculateAdaptor;
|
||||
|
||||
constructor() {
|
||||
this.adaptor = new CalculateAdapter();
|
||||
this.adaptor = new CalculateAdaptor();
|
||||
}
|
||||
|
||||
add(a:number, b: number): number {
|
||||
|
Reference in New Issue
Block a user