1 Star 0 Fork 1

itopen/napi_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
x_napi_tool.cpp 16.59 KB
一键复制 编辑 原始数据 按行查看 历史
wenfei6316 提交于 2024-07-23 10:54 . dayu800 napi demo
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
/*
* Copyright (C) 2021-2022 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 "x_napi_tool.h"
#include <cassert>
#include <cstring>
#include <hilog/log.h>
#include "log/serialport_log_wrapper.h"
#define CC_ASSERT(btrue) \
if (!(btrue)) { \
\
} \
assert(btrue);
XNapiTool::XNapiTool(napi_env env, napi_callback_info info)
{
env_ = env;
bFailed_ = false;
executeFunction_ = nullptr;
completeFunction_ = nullptr;
valueData_ = nullptr;
asyncNeedRelease_ = false;
asyncMode_ = AsyncMode::NONE;
pInstance_ = nullptr;
releaseInstance_ = nullptr;
wrapper_ = nullptr;
argc_size = XNapiTool::DEFAULT_ARG_COUNT;
napi_status result_status = napi_get_cb_info(env, info, &argc_size, argv_, &thisVar_, &data_);
CheckFailed(result_status == napi_ok, "get args fail");
}
XNapiTool::XNapiTool(napi_env env, napi_value exports)
{
env_ = env;
exports_ = exports;
asyncMode_ = AsyncMode::NONE;
wrapper_ = nullptr;
}
XNapiTool::~XNapiTool()
{
if (asyncMode_ == AsyncMode::PROMISE) {
napi_status result_status = napi_delete_async_work(env_, work_);
CC_ASSERT(result_status == napi_ok);
}
if (asyncMode_ == AsyncMode::CALLBACK) {
napi_status result_status = napi_delete_reference(env_, callbackFunc_);
CC_ASSERT(result_status == napi_ok);
result_status = napi_delete_async_work(env_, work_);
CC_ASSERT(result_status == napi_ok);
}
if (wrapper_ != nullptr) {
napi_status result_status = napi_delete_reference(env_, wrapper_);
CC_ASSERT(result_status == napi_ok);
}
}
bool XNapiTool::SwapJs2CBool(napi_value value)
{
bool result;
napi_status result_status = napi_get_value_bool(env_, value, &result);
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_bool fail"))
return -1;
return result;
}
napi_value XNapiTool::GetArgv(uint32_t p)
{
if (CheckFailed(p < argc_size, "GetArgv failed"))
return error_;
return argv_[p];
}
uint32_t XNapiTool::GetArgc()
{
return argc_size;
}
napi_value XNapiTool::GetValueProperty(napi_value value, const char *propertyName)
{
napi_value result;
napi_status result_status = napi_get_named_property(env_, value, propertyName, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SetValueProperty(napi_value &value, const char *propertyName, napi_value property)
{
napi_status result_status;
if (value == nullptr) {
result_status = napi_create_object(env_, &value);
CC_ASSERT(result_status == napi_ok);
}
result_status = napi_set_named_property(env_, value, propertyName, property);
CC_ASSERT(result_status == napi_ok);
return value;
}
uint32_t XNapiTool::GetArrayLength(napi_value value)
{
uint32_t ret;
napi_status result_status = napi_get_array_length(env_, value, &ret);
CC_ASSERT(result_status == napi_ok);
return ret;
}
napi_value XNapiTool::GetArrayElement(napi_value value, uint32_t p)
{
napi_value result;
napi_status result_status = napi_get_element(env_, value, p, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SetArrayElement(napi_value &value, uint32_t p, napi_value ele)
{
napi_status result_status;
if (value == nullptr) {
result_status = napi_create_array(env_, &value);
CC_ASSERT(result_status == napi_ok);
}
result_status = napi_set_element(env_, value, p, ele);
CC_ASSERT(result_status == napi_ok);
return value;
}
uint32_t XNapiTool::GetMapLength(napi_value value)
{
napi_value name_result;
napi_get_property_names(env_, value, &name_result);
uint32_t ret;
napi_status result_status = napi_get_array_length(env_, name_result, &ret);
CC_ASSERT(result_status == napi_ok);
return ret;
}
napi_value XNapiTool::GetMapElementName(napi_value value, uint32_t p)
{
napi_value name_result;
napi_get_property_names(env_, value, &name_result);
napi_value result;
napi_status result_status = napi_get_element(env_, name_result, p, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::GetMapElementValue(napi_value value, const char * utf8Name)
{
napi_value result;
napi_status result_status = napi_get_named_property(env_, value, utf8Name, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SetMapElement(napi_value &value, const char * ele_key, napi_value ele_value)
{
napi_status result_status;
if (value == nullptr) {
result_status = napi_create_object(env_, &value);
CC_ASSERT(result_status == napi_ok);
}
result_status = napi_set_named_property(env_, value, ele_key, ele_value);
CC_ASSERT(result_status == napi_ok);
return value;
}
bool XNapiTool::CheckFailed(bool b, const char *errStr)
{
if (bFailed_) {
return true;
}
if (b) {
return false;
}
napi_value errCode = nullptr;
napi_value errMessage = nullptr;
SERIALPORT_LOGE("CheckFailed x_napi_tool err : %{public}s", errStr);
napi_create_string_utf8(env_, "x_tool", strlen("x_tool"), &errCode);
napi_create_string_utf8(env_, errStr, strlen(errStr), &errMessage);
napi_create_error(env_, errCode, errMessage, &error_);
bFailed_ = true;
return true;
}
int32_t XNapiTool::SwapJs2CInt32(napi_value value)
{
int32_t result;
napi_status result_status = napi_get_value_int32(env_, value, &result);
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
return -1;
return result;
}
uint32_t XNapiTool::SwapJs2CUint32(napi_value value)
{
uint32_t result;
napi_status result_status = napi_get_value_uint32(env_, value, &result);
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_uint32 fail"))
return -1;
return result;
}
int64_t XNapiTool::SwapJs2CInt64(napi_value value)
{
int64_t result;
napi_status result_status = napi_get_value_int64(env_, value, &result);
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
return -1;
return result;
}
double XNapiTool::SwapJs2CDouble(napi_value value)
{
double result;
napi_status result_status = napi_get_value_double(env_, value, &result);
if (CheckFailed(result_status == napi_ok, "swap_js_2_c_int32 fail"))
return -1;
return result;
}
size_t XNapiTool::SwapJs2CUtf8(napi_value value, std::string &str)
{
char buf[1024];
size_t result;
napi_status result_status = napi_get_value_string_utf8(env_, value, buf, 1024, &result);
if (CheckFailed(result_status == napi_ok, "napi_get_value_string_utf8 fail"))
return -1;
str = buf;
return result;
}
size_t XNapiTool::SwapJs2CUint8Array(napi_value value, std::vector<uint8_t> &data)
{
napi_typedarray_type arrayType;
napi_value arrayBuffer = nullptr;
size_t length = 0;
size_t offset = 0;
uint8_t *rawData = nullptr;
napi_valuetype valuetype = napi_valuetype::napi_null;
napi_typeof(env_, value, &valuetype);
if (valuetype == napi_valuetype::napi_string) {
char buf[1024] = {0};
napi_status result_status = napi_get_value_string_utf8(env_, value, buf, 1024, &length);
if (CheckFailed(result_status == napi_ok, "napi_get_value_string_utf8 failed"))
return -1;
if (length > 0) {
data.assign(buf, buf+length);
}
return length;
}
napi_status result_status = napi_get_typedarray_info(env_, value, &arrayType, &length, (void **)&rawData, &arrayBuffer, &offset);
if (CheckFailed(result_status == napi_ok, "napi_get_typedarray_info fail")) {
return -1;
}
if (CheckFailed(arrayType == napi_uint8_array, "it's not uint8 array")) {
return -1;
}
if (length > 10240) {
SERIALPORT_LOGE("SwapJs2CUint8Array data len is too large, len = %{public}d", (int)length);
return -1;
} else if (length > 0) {
data.assign(rawData, rawData + length);
}
return length;
}
napi_value XNapiTool::SwapC2JsBool(bool value)
{
napi_value result;
napi_status result_status = napi_create_int32(env_, value, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsInt32(int32_t value)
{
napi_value result;
napi_status result_status = napi_create_int32(env_, value, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsUint32(uint32_t value)
{
napi_value result;
napi_status result_status = napi_create_uint32(env_, value, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsInt64(int64_t value)
{
napi_value result;
napi_status result_status = napi_create_int64(env_, value, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsDouble(double value)
{
napi_value result;
napi_status result_status = napi_create_double(env_, value, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsUtf8(const char *value)
{
napi_value result;
napi_status result_status = napi_create_string_utf8(env_, value, NAPI_AUTO_LENGTH, &result);
CC_ASSERT(result_status == napi_ok);
return result;
}
napi_value XNapiTool::SwapC2JsUint8Array(const unsigned char *value, size_t size)
{
if (value == 0 || size < 0) {
size = 0;
}
napi_value arrayBuffer = nullptr;
void *arrayBufferPtr = nullptr;
napi_create_arraybuffer(env_, size, &arrayBufferPtr, &arrayBuffer);
if (size > 0) {
CC_ASSERT(arrayBufferPtr != nullptr);
memcpy(arrayBufferPtr, value, size);
}
napi_value result = nullptr;
napi_create_typedarray(env_, napi_uint8_array, size, arrayBuffer, 0, &result);
return result;
}
bool XNapiTool::CheckValueType(napi_value value, napi_valuetype type)
{
napi_valuetype valueType;
napi_status result_status = napi_typeof(env_, value, &valueType);
CC_ASSERT(result_status == napi_ok);
if (CheckFailed(valueType == type, "The incoming parameter type is not a callback function"))
return false;
return true;
}
napi_value XNapiTool::SyncCallBack(napi_value func, size_t argc, napi_value *args)
{
napi_value cb_result;
napi_status result_status = napi_call_function(env_, thisVar_, func, argc, args, &cb_result);
CC_ASSERT(result_status == napi_ok);
return cb_result;
}
void XNapiTool::AsyncExecuteFunction()
{
if (executeFunction_ != nullptr) {
executeFunction_(this, valueData_);
}
}
void XNapiTool::AsyncExecute(napi_env env, void *p)
{
XNapiTool *pxt = (XNapiTool *)p;
pxt->AsyncExecuteFunction();
}
void XNapiTool::AsyncCompleteFunction()
{
if (completeFunction_ != nullptr) {
completeFunction_(this, valueData_);
}
}
void XNapiTool::AsyncComplete(napi_env env, napi_status status, void *p)
{
XNapiTool *pxt = (XNapiTool *)p;
pxt->AsyncCompleteFunction();
delete pxt;
}
napi_value XNapiTool::StartAsync(CallbackFunction pe, void *data, CallbackFunction pc, napi_value func)
{
napi_value result;
napi_status result_status;
if (func == nullptr) {
// promise
result_status = napi_create_promise(env_, &deferred_, &result);
CC_ASSERT(result_status == napi_ok);
asyncMode_ = AsyncMode::PROMISE;
} else {
// callback
result_status = napi_create_reference(env_, func, 1, &callbackFunc_);
CC_ASSERT(result_status == napi_ok);
asyncMode_ = AsyncMode::CALLBACK;
result = UndefinedValue(env_);
}
napi_value resourceName = nullptr;
result_status = napi_create_string_utf8(env_, "x_napi_tool", NAPI_AUTO_LENGTH, &resourceName);
CC_ASSERT(result_status == napi_ok);
result_status = napi_create_async_work(env_, nullptr, resourceName, XNapiTool::AsyncExecute,
XNapiTool::AsyncComplete, this, &work_);
CC_ASSERT(result_status == napi_ok);
asyncNeedRelease_ = true;
executeFunction_ = pe;
completeFunction_ = pc;
valueData_ = data;
result_status = napi_queue_async_work(env_, work_);
CC_ASSERT(result_status == napi_ok);
return result;
}
void XNapiTool::FinishAsync(int32_t cppRlt, napi_value *args)
{
if (asyncMode_ == AsyncMode::PROMISE) {
if (cppRlt == 0) {
bool typeOk = false;
napi_valuetype valueType;
napi_status result_status = napi_typeof(env_, args[1], &valueType);
if (result_status == napi_ok) {
typeOk = true;
}
napi_resolve_deferred(env_, deferred_, typeOk ? args[1] : args[0]);
} else {
napi_reject_deferred(env_, deferred_, args[0]);
}
return;
}
napi_value recv = nullptr;
napi_value result = 0;
napi_value cb = 0;
napi_get_undefined(env_, &recv);
napi_status result_status = napi_get_reference_value(env_, callbackFunc_, &cb);
if (result_status != napi_ok) {
SERIALPORT_LOGE("napi_get_reference_value error.");
return;
}
result_status = napi_call_function(env_, recv, cb, XNapiTool::ARGV_CNT, args, &result);
if (result_status != napi_ok) {
SERIALPORT_LOGE("napi_call_function error.");
return;
}
}
napi_value XNapiTool::UndefinedValue(napi_env env)
{
napi_value result;
napi_get_undefined(env, &result);
return result;
}
napi_value XNapiTool::UndefinedValue()
{
napi_value result;
napi_get_undefined(env_, &result);
return result;
}
napi_value XNapiTool::CreateSubObject(napi_value parent, const char *name)
{
napi_value result;
napi_status result_status = napi_create_object(env_, &result);
CC_ASSERT(result_status == napi_ok);
result_status = napi_set_named_property(env_, parent, name, result);
CC_ASSERT(result_status == napi_ok);
return result;
}
void XNapiTool::DefineFunction(const char *funcName, napi_callback callback, napi_value dest)
{
if (dest == nullptr)
dest = exports_;
napi_property_descriptor descriptor[] = {
{funcName, 0, callback, 0, 0, 0, napi_default, 0}};
napi_status result_status = napi_define_properties(env_, dest, 1, descriptor);
CC_ASSERT(result_status == napi_ok);
}
void XNapiTool::DefineClass(const char *className, napi_callback constructorFunc,
std::map<const char *, std::map<const char *, napi_callback>> &valueList,
std::map<const char *, napi_callback> &funcList, napi_value dest)
{
if (dest == nullptr)
dest = exports_;
napi_value tmpClass = nullptr;
napi_property_descriptor funcs[funcList.size() + valueList.size()];
uint32_t p = 0;
for (auto it = valueList.begin(); it != valueList.end(); it++) {
funcs[p++] = {it->first, 0, 0, it->second["getvalue"], it->second["setvalue"], 0, napi_default, 0}; // get,set
}
for (auto it = funcList.begin(); it != funcList.end(); it++) {
funcs[p++] = {it->first, 0, it->second, 0, 0, 0, napi_default, 0};
}
napi_status result_status = napi_define_class(env_, className, NAPI_AUTO_LENGTH, constructorFunc,
nullptr, p, funcs, &tmpClass);
CC_ASSERT(result_status == napi_ok);
result_status = napi_set_named_property(env_, dest, className, tmpClass);
CC_ASSERT(result_status == napi_ok);
}
void XNapiTool::WrapFinalize(napi_env env, void *data, void *hint)
{
XNapiTool *pxt = (XNapiTool *)data;
pxt->ReleaseInstance();
delete pxt;
}
void XNapiTool::ReleaseInstance()
{
if (releaseInstance_ != nullptr) {
releaseInstance_(pInstance_);
}
}
napi_value XNapiTool::WrapInstance(void *instance, RELEASE_INSTANCE ri)
{
pInstance_ = instance;
releaseInstance_ = ri;
napi_status result_status = napi_wrap(env_, thisVar_, this, WrapFinalize, nullptr, &wrapper_);
CC_ASSERT(result_status == napi_ok);
return thisVar_;
}
void *XNapiTool::UnWarpInstance()
{
XNapiTool *p;
napi_status result_status = napi_unwrap(env_, thisVar_, (void **)&p);
CC_ASSERT(result_status == napi_ok);
return p->pInstance_;
}
void XNapiTool::SetAsyncInstance(void *p)
{
asyncInstance_ = p;
}
void *XNapiTool::GetAsyncInstance()
{
return asyncInstance_;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/itopen/napi_demo.git
git@gitee.com:itopen/napi_demo.git
itopen
napi_demo
napi_demo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385