修改对napi class的snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-04 14:44:18 +08:00
parent 8a1e803f94
commit ad4cc31d77

View File

@ -2,161 +2,191 @@
"Napi Class": {
"prefix": "napiclass",
"body": [
"class testNapiWrap {",
"class HelloWorld {",
" public:",
" static napi_value Init(napi_env env, napi_value exports);",
" static void Destructor(napi_env env, void *nativeObject, void *finalizeHint);",
" ",
" static napi_value Init(napi_env env, napi_value exports);",
" static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);",
" ",
" private:",
" explicit testNapiWrap(napi_value value_ = 0);",
" ~testNapiWrap();",
" ",
" static napi_value New(napi_env env, napi_callback_info info);",
" static napi_value Tyof(napi_env env, napi_callback_info info);",
" ",
" napi_value value_;",
" napi_env env_;",
" napi_ref wrapper_;",
" };",
" ",
" explicit HelloWorld(std::string value_ = \"\");",
" ~HelloWorld();",
" ",
" static napi_value New(napi_env env, napi_callback_info info);",
" static napi_value GetValue(napi_env env, napi_callback_info info);",
" static napi_value SetValue(napi_env env, napi_callback_info info);",
" static napi_value Hello(napi_env env, napi_callback_info info);",
" ",
" std::string value_;",
" napi_env env_;",
" napi_ref wrapper_;",
" };",
" ",
"static thread_local napi_ref g_ref = nullptr;",
" ",
"testNapiWrap::testNapiWrap(napi_value value) : value_(value), env_(nullptr), wrapper_(nullptr) {}",
" ",
"testNapiWrap::~testNapiWrap() { napi_delete_reference(env_, wrapper_); }",
" ",
"void testNapiWrap::Destructor(napi_env env, void *nativeObject, [[maybe_unused]] void *finalizeHint)",
"",
"HelloWorld::HelloWorld(std::string value)",
" : value_(value), env_(nullptr), wrapper_(nullptr) {}",
"",
"HelloWorld::~HelloWorld()",
"{",
" reinterpret_cast<testNapiWrap *>(nativeObject)->~testNapiWrap();",
"}",
" ",
"napi_status napiValueType2Str(const napi_env &env, const napi_valuetype type, napi_value *result)",
"{",
" const char *typeStr = \"\";",
" napi_status status;",
" // napi_valuetype -> const char *",
" switch (type) {",
" case napi_undefined:",
" typeStr = \"undefined\";",
" break;",
" case napi_null:",
" typeStr = \"null\";",
" break;",
" case napi_boolean:",
" typeStr = \"boolean\";",
" break;",
" case napi_number:",
" typeStr = \"number\";",
" break;",
" case napi_string:",
" typeStr = \"string\";",
" break;",
" case napi_symbol:",
" typeStr = \"symbol\";",
" break;",
" case napi_object:",
" typeStr = \"object\";",
" break;",
" case napi_function:",
" typeStr = \"function\";",
" break;",
" case napi_external:",
" typeStr = \"external\";",
" break;",
" case napi_bigint:",
" typeStr = \"bigint\";",
" break;",
" default:",
" typeStr = \"unknown\";",
" break;",
" }",
" // const char * -> napi_value",
" status = napi_create_string_utf8(env, typeStr, NAPI_AUTO_LENGTH, result);",
" return status;",
" napi_delete_reference(env_, wrapper_);",
"}",
"",
"napi_value testNapiWrap::Tyof(napi_env env, napi_callback_info info)",
"void HelloWorld::Destructor(napi_env env,",
" void* nativeObject,",
" [[maybe_unused]] void* finalize_hint)",
"{",
" napi_value jsThis;",
" napi_valuetype result;",
" napi_value resultStr;",
" napi_status status;",
" OH_LOG_INFO(LOG_APP, \"HelloWorld::Destructor called\");",
" reinterpret_cast<HelloWorld*>(nativeObject)->~HelloWorld();",
"}",
"",
"napi_value HelloWorld::New(napi_env env, napi_callback_info info)",
"{",
" OH_LOG_INFO(LOG_APP, \"HelloWorld::New called\");",
"",
" napi_value newTarget;",
" napi_get_new_target(env, info, &newTarget);",
" if (newTarget != nullptr) {",
" // Invoked as the constructor `new HelloWorld(...)`.",
" size_t argc = 1;",
" napi_value argv[1];",
" status = napi_get_cb_info(env, info, &argc, argv, &jsThis, nullptr);",
" napi_value args[1];",
" napi_value jsThis;",
" napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);",
"",
" std::string value = \"\";",
" size_t strLength = 0;",
" napi_status status = napi_get_value_string_utf8(env, args[0], NULL, 0, &strLength);",
" if (status != napi_ok) {",
" std::string errMsg = \"napi_get_cb_info failed: \" + std::to_string(status);",
" napi_throw_error(env, NULL, errMsg.c_str());",
" return NULL;",
" return nullptr;",
" }",
" testNapiWrap *obj;",
" status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&obj));",
" if (status != napi_ok || obj == nullptr) {",
" std::string errMsg = \"napi_unwrap failed: \" + std::to_string(status);",
" napi_throw_error(env, NULL, errMsg.c_str());",
" return NULL;",
" }",
" status = napi_typeof(env, argv[0], &result);",
" char *strValue = new char[strLength + 1];",
" status = napi_get_value_string_utf8(env, args[0], strValue, strLength + 1, &strLength);",
" if (status != napi_ok) {",
" std::string errMsg = \"napi_typeof failed: \" + std::to_string(status);",
" napi_throw_error(env, NULL, errMsg.c_str());",
" return NULL;",
" delete[] strValue;",
" return nullptr;",
" }",
" status = napiValueType2Str(env, result, &resultStr);",
" if (status != napi_ok) {",
" std::string errMsg = \"convert napi_valuetype failed: \" + std::to_string(status);",
" napi_throw_error(env, NULL, errMsg.c_str());",
" return NULL;",
" }",
" return resultStr;",
" value = strValue;",
" delete[] strValue;",
"",
" HelloWorld* obj = new HelloWorld(value);",
"",
" obj->env_ = env;",
" // Use napi_wrap to wrap the C++ object obj in the ArkTS object jsThis.",
" napi_wrap(env,",
" jsThis,",
" reinterpret_cast<void*>(obj),",
" HelloWorld::Destructor,",
" nullptr, // finalize_hint",
" &obj->wrapper_);",
"",
" return jsThis;",
" } else {",
" // Invoked as the plain function `Hello(...)`.",
" size_t argc = 1;",
" napi_value args[1];",
" napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);",
"",
" napi_value cons;",
" napi_get_reference_value(env, g_ref, &cons);",
" napi_value instance;",
" napi_new_instance(env, cons, argc, args, &instance);",
"",
" return instance;",
" }",
"}",
"",
"napi_value testNapiWrap::New(napi_env env, napi_callback_info info)",
"napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)",
"{",
" napi_value newTarget;",
" napi_get_new_target(env, info, &newTarget);",
" if (newTarget != nullptr) {",
" size_t argc = 1;",
" napi_value args[1];",
" napi_value jsThis;",
" napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);",
" napi_value value;",
" testNapiWrap *obj = new testNapiWrap(value);",
" obj->env_ = env;",
" napi_wrap(env, jsThis, reinterpret_cast<void *>(obj), testNapiWrap::Destructor,",
" nullptr, // finalize_hint",
" &obj->wrapper_);",
" return jsThis;",
" } else {",
" size_t argc = 1;",
" napi_value args[1];",
" napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);",
" napi_value cons;",
" napi_get_reference_value(env, g_ref, &cons);",
" napi_value instance;",
" napi_new_instance(env, cons, argc, args, &instance);",
" return instance;",
" }",
" OH_LOG_INFO(LOG_APP, \"HelloWorld::GetValue called\");",
"",
" napi_value jsThis;",
" napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);",
"",
" HelloWorld* obj;",
" // Use napi_unwrap to retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.",
" napi_unwrap(env, jsThis, reinterpret_cast<void**>(&obj));",
" napi_value str;",
" napi_status status = napi_create_string_utf8(env, obj->value_.c_str(), NAPI_AUTO_LENGTH, &str);",
" if (status != napi_ok) {",
" napi_throw_error(env, NULL, \"Failed to create result value\");",
" return NULL;",
" }",
" return str;",
"}",
"",
"napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)",
"{",
" OH_LOG_INFO(LOG_APP, \"HelloWorld::SetValue called\");",
"",
" size_t argc = 1;",
" napi_value value;",
" napi_value jsThis;",
"",
" napi_get_cb_info(env, info, &argc, &value, &jsThis, nullptr);",
"",
" HelloWorld* obj;",
" // Use napi_unwrap to retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.",
" napi_unwrap(env, jsThis, reinterpret_cast<void**>(&obj));",
" size_t strLength = 0;",
" napi_status status = napi_get_value_string_utf8(env, value, NULL, 0, &strLength);",
" if (status != napi_ok) {",
" return nullptr;",
" }",
" char *strValue = new char[strLength + 1];",
" status = napi_get_value_string_utf8(env, value, strValue, strLength + 1, &strLength);",
" if (status != napi_ok) {",
" delete[] strValue;",
" return nullptr;",
" }",
" obj->value_ = strValue;",
" delete[] strValue;",
"",
" return nullptr;",
"}",
"",
"napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)",
"{",
" OH_LOG_INFO(LOG_APP, \"HelloWorld::Hello called\");",
"",
" napi_value jsThis;",
" napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);",
"",
" HelloWorld* obj;",
" // Use napi_unwrap to retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.",
" napi_unwrap(env, jsThis, reinterpret_cast<void**>(&obj));",
" obj->value_ = \"native: hello world to js!\";",
" napi_value str;",
" napi_status status = napi_create_string_utf8(env, obj->value_.c_str(), NAPI_AUTO_LENGTH, &str);",
" if (status != napi_ok) {",
" napi_throw_error(env, NULL, \"Failed to create result value\");",
" return NULL;",
" }",
" return str;",
"}",
"",
"napi_value HelloWorld::Init(napi_env env, napi_value exports)",
"{",
" napi_property_descriptor properties[] = {",
" { \"value\", 0, 0, GetValue, SetValue, 0, napi_default, 0 },",
" { \"nativeHello\", nullptr, Hello, nullptr, nullptr, nullptr, napi_default, nullptr }",
" };",
"",
" napi_value cons;",
" napi_define_class(env, \"HelloWorldClass\", NAPI_AUTO_LENGTH, New, nullptr, 2,",
" properties, &cons);",
"",
" napi_create_reference(env, cons, 1, &g_ref);",
" napi_set_named_property(env, exports, \"HelloWorld\", cons);",
" return exports;",
"}",
"",
"EXTERN_C_START",
"napi_value testNapiWrap::Init(napi_env env, napi_value exports)",
"{",
" napi_property_descriptor properties[] = {{\"Tyof\", nullptr, Tyof, nullptr, nullptr, nullptr, napi_default, nullptr}};",
" napi_value cons;",
" napi_define_class(env, \"testNapiWrapClass\", NAPI_AUTO_LENGTH, New, nullptr, 1, properties, &cons);",
" napi_create_reference(env, cons, 1, &g_ref);",
" napi_set_named_property(env, exports, \"testNapiWrap\", cons);",
" return exports;",
"}",
"",
"static napi_value Init(napi_env env, napi_value exports)",
"{",
" testNapiWrap::Init(env, exports);",
" HelloWorld::Init(env, exports);",
" return exports;",
"}",
"EXTERN_C_END"
],
"description": ""
"description": "You can use napi_wrap to wrap a C++ object in an ArkTS object, and use napi_unwrap to retrieve the C++ object previously wrapped in the ArkTS object for subsequent operations."
}
}