From 8a1e803f94a713ffca2cbfafa20bf95302e94e34 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Mon, 4 Nov 2024 11:51:09 +0800 Subject: [PATCH 1/6] =?UTF-8?q?vscode=E6=8F=92=E4=BB=B6snippets:=20napi=20?= =?UTF-8?q?class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 10 +- .../snippets/napi_class_snippets.json | 162 ++++++++++++++++++ 2 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 src/vscode_plugin/snippets/napi_class_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 47638629..c2eeda75 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -10,7 +10,8 @@ "vscode": "^1.34.0" }, "categories": [ - "Other" + "Other", + "Snippets" ], "activationEvents": [ "onCommand:extension.h2dts", @@ -106,7 +107,12 @@ "group": "2_workspace" } ] - } + "snippets": [ + { + "language": "cpp", + "path": "./snippets/napi_class_snippets.json" + } + ] }, "scripts": { "vscode:prepublish": "npm run compile", diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json new file mode 100644 index 00000000..ee2c07dd --- /dev/null +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -0,0 +1,162 @@ +{ + "Napi Class": { + "prefix": "napiclass", + "body": [ + "class testNapiWrap {", + " public:", + " static napi_value Init(napi_env env, napi_value exports);", + " static void Destructor(napi_env env, void *nativeObject, void *finalizeHint);", + " ", + " 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_;", + " };", + " ", + "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)", + "{", + " reinterpret_cast(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_value testNapiWrap::Tyof(napi_env env, napi_callback_info info)", + "{", + " napi_value jsThis;", + " napi_valuetype result;", + " napi_value resultStr;", + " napi_status status;", + " size_t argc = 1;", + " napi_value argv[1];", + " status = napi_get_cb_info(env, info, &argc, argv, &jsThis, nullptr);", + " 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;", + " }", + " testNapiWrap *obj;", + " status = napi_unwrap(env, jsThis, reinterpret_cast(&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);", + " if (status != napi_ok) {", + " std::string errMsg = \"napi_typeof failed: \" + std::to_string(status);", + " napi_throw_error(env, NULL, errMsg.c_str());", + " return NULL;", + " }", + " 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;", + "}", + "", + "napi_value testNapiWrap::New(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(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;", + " }", + "}", + "", + "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);", + " return exports;", + "}", + "EXTERN_C_END" + ], + "description": "" + } +} \ No newline at end of file -- Gitee From ad4cc31d77f4da6e089e3e5012e8b279ed4e5331 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Mon, 4 Nov 2024 14:44:18 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9napi=20class?= =?UTF-8?q?=E7=9A=84snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- .../snippets/napi_class_snippets.json | 284 ++++++++++-------- 1 file changed, 157 insertions(+), 127 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index ee2c07dd..8846ec4c 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -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(nativeObject)->~testNapiWrap();", + " napi_delete_reference(env_, wrapper_);", "}", - " ", - "napi_status napiValueType2Str(const napi_env &env, const napi_valuetype type, napi_value *result)", + "", + "void HelloWorld::Destructor(napi_env env,", + " void* nativeObject,", + " [[maybe_unused]] void* finalize_hint)", "{", - " 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;", + " OH_LOG_INFO(LOG_APP, \"HelloWorld::Destructor called\");", + " reinterpret_cast(nativeObject)->~HelloWorld();", "}", "", - "napi_value testNapiWrap::Tyof(napi_env env, napi_callback_info info)", + "napi_value HelloWorld::New(napi_env env, napi_callback_info info)", "{", - " napi_value jsThis;", - " napi_valuetype result;", - " napi_value resultStr;", - " napi_status status;", + " 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;", - " }", - " testNapiWrap *obj;", - " status = napi_unwrap(env, jsThis, reinterpret_cast(&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;", + " return nullptr;", " }", - " 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());", + " 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(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 HelloWorld::GetValue(napi_env env, napi_callback_info info)", + "{", + " 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(&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 resultStr;", + " }", + " return str;", "}", "", - "napi_value testNapiWrap::New(napi_env env, napi_callback_info info)", + "napi_value HelloWorld::SetValue(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(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::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(&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;", "}", "", - "EXTERN_C_START", - "napi_value testNapiWrap::Init(napi_env env, napi_value exports)", + "napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)", "{", - " 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;", + " 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(&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", "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." } } \ No newline at end of file -- Gitee From 04b8479ec3af075800895308c7fedb27088cfede Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Mon, 4 Nov 2024 14:53:27 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B1=BB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/snippets/napi_class_snippets.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index 8846ec4c..6c8ff2e3 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -171,7 +171,7 @@ " };", "", " napi_value cons;", - " napi_define_class(env, \"HelloWorldClass\", NAPI_AUTO_LENGTH, New, nullptr, 2,", + " napi_define_class(env, \"HelloWorld\", NAPI_AUTO_LENGTH, New, nullptr, 2,", " properties, &cons);", "", " napi_create_reference(env, cons, 1, &g_ref);", @@ -186,7 +186,6 @@ " return exports;", "}", "EXTERN_C_END" - ], - "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." + ] } } \ No newline at end of file -- Gitee From 78b060854fc15f51a705e53344b72ac39ddc3479 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Tue, 5 Nov 2024 11:07:05 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9napi=20class=20snippets?= =?UTF-8?q?=E7=89=87=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- .../snippets/napi_class_snippets.json | 232 +++++------------- 1 file changed, 62 insertions(+), 170 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index 6c8ff2e3..b4459b62 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -6,186 +6,78 @@ " public:", " static napi_value Init(napi_env env, napi_value exports);", " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", - " ", " private:", - " explicit HelloWorld(std::string value_ = \"\");", + " explicit HelloWorld();", " ~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;", - "", - "HelloWorld::HelloWorld(std::string value)", - " : value_(value), env_(nullptr), wrapper_(nullptr) {}", - "", - "HelloWorld::~HelloWorld()", - "{", - " napi_delete_reference(env_, wrapper_);", - "}", - "", - "void HelloWorld::Destructor(napi_env env,", - " void* nativeObject,", - " [[maybe_unused]] void* finalize_hint)", - "{", - " OH_LOG_INFO(LOG_APP, \"HelloWorld::Destructor called\");", - " reinterpret_cast(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 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) {", - " return nullptr;", - " }", - " char *strValue = new char[strLength + 1];", - " status = napi_get_value_string_utf8(env, args[0], strValue, strLength + 1, &strLength);", - " if (status != napi_ok) {", - " delete[] strValue;", - " return nullptr;", - " }", - " 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(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 HelloWorld::GetValue(napi_env env, napi_callback_info info)", - "{", - " 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(&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(&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(&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, \"HelloWorld\", 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", - "static napi_value Init(napi_env env, napi_value exports)", - "{", - " HelloWorld::Init(env, exports);", - " return exports;", - "}", - "EXTERN_C_END" + " HelloWorld::HelloWorld(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + " HelloWorld::~HelloWorld() {}", + " void HelloWorld::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint) { reinterpret_cast(nativeObject)->~HelloWorld(); }", + " napi_value HelloWorld::New(napi_env env, napi_callback_info info) {", + " napi_value newTarget;", + " napi_get_new_target(env, info, &newTarget); // Use napi_get_new_target to check if the constructor was invoked with new.", + " if (newTarget != nullptr) { // Invoked as the constructor `new HelloWorld()`.", + " napi_value jsThis;", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", + " HelloWorld* obj = new HelloWorld();", + " obj->env_ = env;", + " // Use napi_wrap to wrap the C++ object obj in the ArkTS object jsThis.", + " napi_wrap(env, jsThis, reinterpret_cast(obj), HelloWorld::Destructor, nullptr, &obj->wrapper_);", + " return jsThis;", + " } else {", + " // Use napi_throw_error to throw an error indicating incorrect constructor invocation.", + " napi_throw_error(env, nullptr, \"HelloWorld must be invoked as a constructor with `new`\");", + " return nullptr;", + " }", + " }", + " napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info) {", + " napi_value jsThis;", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", + " 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(&obj));", + " napi_value value;", + " // Todo: get value from native", + " return value;", + " }", + " napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info) {", + " // Todo: get value from js", + " 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(&obj));", + " // Todo: set value to native", + " return nullptr;", + " }", + " napi_value HelloWorld::Hello(napi_env env, napi_callback_info info) {", + " napi_value jsThis;", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", + " 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(&obj));", + " napi_value value;", + " // Todo: assign a value to obj and return to ArkTs.", + " return value;", + " }", + " napi_value HelloWorld::Init(napi_env env, napi_value exports) {", + " // Use napi_property_descriptor to define properties and methods for a N-API object.", + " 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;", + " // Use napi_define_class to define a new JavaScript class backed by a C++ class.", + " napi_define_class(env, \"HelloWorld\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Use napi_set_named_property to add a property to the exports object, making the constructor accessible in ArkTs.", + " napi_set_named_property(env, exports, \"HelloWorld\", cons);", + " return exports;", + " }" ] } } \ No newline at end of file -- Gitee From 866d668fcb298ea65a82d77bf12f0c2efb85fb65 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Tue, 5 Nov 2024 17:27:20 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9napi=20class=E7=9A=84snip?= =?UTF-8?q?pets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- .../snippets/napi_class_snippets.json | 152 +++++++++--------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index b4459b62..424f298b 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -2,82 +2,82 @@ "Napi Class": { "prefix": "napiclass", "body": [ - "class HelloWorld {", - " public:", - " static napi_value Init(napi_env env, napi_value exports);", - " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", - " private:", - " explicit HelloWorld();", - " ~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_;", - " };", - " HelloWorld::HelloWorld(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", - " HelloWorld::~HelloWorld() {}", - " void HelloWorld::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint) { reinterpret_cast(nativeObject)->~HelloWorld(); }", - " napi_value HelloWorld::New(napi_env env, napi_callback_info info) {", - " napi_value newTarget;", - " napi_get_new_target(env, info, &newTarget); // Use napi_get_new_target to check if the constructor was invoked with new.", - " if (newTarget != nullptr) { // Invoked as the constructor `new HelloWorld()`.", - " napi_value jsThis;", - " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", - " HelloWorld* obj = new HelloWorld();", - " obj->env_ = env;", - " // Use napi_wrap to wrap the C++ object obj in the ArkTS object jsThis.", - " napi_wrap(env, jsThis, reinterpret_cast(obj), HelloWorld::Destructor, nullptr, &obj->wrapper_);", - " return jsThis;", - " } else {", - " // Use napi_throw_error to throw an error indicating incorrect constructor invocation.", - " napi_throw_error(env, nullptr, \"HelloWorld must be invoked as a constructor with `new`\");", - " return nullptr;", - " }", - " }", - " napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info) {", - " napi_value jsThis;", - " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", - " 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(&obj));", - " napi_value value;", - " // Todo: get value from native", - " return value;", - " }", - " napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info) {", - " // Todo: get value from js", - " 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(&obj));", - " // Todo: set value to native", - " return nullptr;", - " }", - " napi_value HelloWorld::Hello(napi_env env, napi_callback_info info) {", - " napi_value jsThis;", - " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); // Use napi_get_cb_info to retrieve the callback's context and arguments.", - " 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(&obj));", - " napi_value value;", - " // Todo: assign a value to obj and return to ArkTs.", - " return value;", - " }", - " napi_value HelloWorld::Init(napi_env env, napi_value exports) {", - " // Use napi_property_descriptor to define properties and methods for a N-API object.", - " 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;", - " // Use napi_define_class to define a new JavaScript class backed by a C++ class.", - " napi_define_class(env, \"HelloWorld\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", - " // Use napi_set_named_property to add a property to the exports object, making the constructor accessible in ArkTs.", - " napi_set_named_property(env, exports, \"HelloWorld\", cons);", - " return exports;", - " }" + "class HelloWorld {", + " public:", + " static napi_value Init(napi_env env, napi_value exports);", + " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", + " private:", + " explicit HelloWorld();", + " ~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_;", + "};", + "HelloWorld::HelloWorld(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + "HelloWorld::~HelloWorld() {}", + "void HelloWorld::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", + "{", + " reinterpret_cast(nativeObject)->~HelloWorld();", + "}", + "napi_value HelloWorld::New(napi_env env, napi_callback_info info)", + "{", + " napi_value newTarget;", + " // Check if the constructor was invoked with new.", + " napi_get_new_target(env, info, &newTarget);", + " if (newTarget != nullptr) {", + " // Invoked as the constructor `new HelloWorld()`.", + " napi_value jsThis;", + " // Retrieve the callback's context and arguments.", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " HelloWorld* obj = new HelloWorld();", + " obj->env_ = env;", + " // Wrap the C++ object obj in the ArkTS object jsThis.", + " napi_wrap(env, jsThis, reinterpret_cast(obj), HelloWorld::Destructor, nullptr, &obj->wrapper_);", + " return jsThis;", + " } else {", + " OH_LOG_ERROR(LOG_APP, \"HelloWorld must be invoked as a constructor with `new`\");", + " return nullptr;", + " }", + "}", + "napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)", + "{", + " // Todo: you can use \"napigetinfo\" command to get snippets that get js context and arguments.", + " HelloWorld* obj;", + " // Retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.", + " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", + " // Todo: you can use \"napireturn_\" to choose command that return value to js.", + "}", + "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", + "{", + " // Todo: you can use \"napigetinfo\" command to get snippets that get js context and arguments.", + " HelloWorld* obj;", + " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", + " // Todo: you can use \"napigetparam_\" to choose command that get params from js.", + " return nullptr;", + "}", + "napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)", + "{", + " OH_LOG_INFO(LOG_APP, \"HelloWorld::Hello World!\");", + " return nullptr;", + "}", + "napi_value HelloWorld::Init(napi_env env, napi_value exports)", + "{", + " // Define properties and methods for a N-API object", + " napi_property_descriptor properties[] = {", + " { \"value\", 0, 0, GetValue, SetValue, 0, napi_default, 0 },", + " { \"hello\", nullptr, Hello, nullptr, nullptr, nullptr, napi_default, nullptr }", + " };", + " napi_value cons;", + " // Define a js class", + " napi_define_class(env, \"HelloWorld\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Set the 'HelloWorld' class on the exports object.", + " napi_set_named_property(env, exports, \"HelloWorld\", cons);", + " return exports;", + "}" ] } } \ No newline at end of file -- Gitee From a051ab86596f25410bd55b235240cd040f01b3d3 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Tue, 5 Nov 2024 19:46:34 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9napi=20class=20snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/snippets/napi_class_snippets.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index 424f298b..a5c03b40 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -18,7 +18,7 @@ " napi_ref wrapper_;", "};", "HelloWorld::HelloWorld(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", - "HelloWorld::~HelloWorld() {}", + "HelloWorld::~HelloWorld() { napi_delete_reference(env_, wrapper_); }", "void HelloWorld::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", "{", " reinterpret_cast(nativeObject)->~HelloWorld();", @@ -45,18 +45,18 @@ "}", "napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napigetinfo\" command to get snippets that get js context and arguments.", + " // Todo: you can use \"napiobjectin\" command that get object param from js.", " HelloWorld* obj;", " // Retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", - " // Todo: you can use \"napireturn_\" to choose command that return value to js.", + " // Todo: you can use \"napistringutf8out\" command that return string utf8 value to js.", "}", "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napigetinfo\" command to get snippets that get js context and arguments.", + " // Todo: you can use \"napiobjectin\" command that get object param from js.", " HelloWorld* obj;", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", - " // Todo: you can use \"napigetparam_\" to choose command that get params from js.", + " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", " return nullptr;", "}", "napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)", -- Gitee