!470 napi接口修改:napi_wrap

Merge pull request !470 from zhangmenghan/master
This commit is contained in:
openharmony_ci 2024-10-25 10:58:21 +00:00 committed by Gitee
commit 4fa31687f3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -17,20 +17,19 @@
#include "javascriptapi.h"
static const char *TAG = "[javascriptapi_object_wrap]";
static const int MAX_BUFFER_SIZE = 128;
class Node {
public:
Node(napi_env env, napi_value id)
{
// 将 JavaScript 字符串转换为 C++ 字符串
size_t idLength;
napi_get_value_string_utf8(env, id, nullptr, 0, &idLength);
char *buffer = new char[idLength + 1];
napi_get_value_string_utf8(env, id, buffer, idLength + 1, nullptr);
size_t idLength = MAX_BUFFER_SIZE;
char buf[MAX_BUFFER_SIZE];
char *buffer = buf;
napi_get_value_string_utf8(env, id, buffer, idLength, nullptr);
// 将 C++ 字符串转换为 std::string
_id = std::string(buffer);
// 释放分配的内存
delete[] buffer;
}
std::string GetId() { return _id; }
private:
@ -45,7 +44,6 @@ napi_value testNapiWrap(napi_env env, napi_callback_info info)
void *data = nullptr;
napi_status status;
napi_value cons;
const napi_extended_error_info *extended_error_info;
// 获取回调函数的参数信息
status = napi_get_cb_info(env, info, &argc, argv, &thisObj, &data);
@ -74,4 +72,3 @@ napi_value testNapiWrap(napi_env env, napi_callback_info info)
}
return thisObj;
}