From 2e06854d61aca3890d0c4356d5d70363f2ff5f6e Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 15 Nov 2024 16:48:13 +0800 Subject: [PATCH 1/5] add napi in && out snippets Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 4 + .../snippets/napi_variable_snippets.json | 172 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/vscode_plugin/snippets/napi_variable_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 3d361525..a226551a 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -123,6 +123,10 @@ { "language": "cpp", "path": "./snippets/napi_callback_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_variable_snippets.json" } ] }, diff --git a/src/vscode_plugin/snippets/napi_variable_snippets.json b/src/vscode_plugin/snippets/napi_variable_snippets.json new file mode 100644 index 00000000..a3fceb41 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_variable_snippets.json @@ -0,0 +1,172 @@ +{ + "Napi object in":{ + "prefix": "napiobjectin", + "body": [ + "// Get js context and arguments.", + "napi_get_cb_info(env, info, &argc, args, &jsThis, &data);" + ] + }, + "Napi double in": { + "prefix": "napidoublein", + "body": [ + "// Convert a JS number type value to a C double type.", + "napi_get_value_double(env, args[0], &value0);" + ] + }, + "Napi int32_t in": { + "prefix": "napiint32in", + "body": [ + "// Convert a JS number type value to a C int32_t type.", + "napi_get_value_int32(env, args[0], &value0);" + ] + }, + "Napi uint32_t in": { + "prefix": "napiuint32in", + "body": [ + "// Convert a JS number type value to a C uint32_t type.", + "napi_get_value_uint32(env, args[0], &value0);" + ] + }, + "Napi int64_t in": { + "prefix": "napiint64in", + "body": [ + "// Convert a JS number type value to a C int64_t type.", + "napi_get_value_int64(env, args[0], &value0);" + ] + }, + "Napi bool in": { + "prefix": "napiboolin", + "body": [ + "// Convert a JS boolean type value to a C bool type.", + "napi_get_value_bool(env, args[0], &value0);" + ] + }, + "Napi string utf8 in": { + "prefix": "napistringutf8in", + "body": [ + "// Get the js string as a UTF-8 encoded C string.", + "napi_get_value_string_utf8(env, args[0], value0, size, &result);" + ] + }, + "Napi string utf16 in": { + "prefix": "napistringutf16in", + "body": [ + "// Get the js string as a UTF-16 encoded C string.", + "napi_get_value_string_utf16(env, args[0], value0, size, &result);" + ] + }, + "Napi is array": { + "prefix": "napiisarray", + "body": [ + "// Check if the provided napi_value is a JavaScript array.", + "napi_is_array(env, args[0], &isArray);" + ] + }, + "Napi array in": { + "prefix": "napiarrayin", + "body": [ + "// Retrieve the length of a JavaScript array.", + "napi_get_array_length(env, array, &length);", + "for (uint32_t i = 0; i < length; i++) {", + " // Get an element from a JavaScript array by index.", + " napi_get_element(env, array, i, &element);", + "}" + ] + }, + "Napi get value external": { + "prefix": "napigetvalueexternal", + "body": [ + "// Retrieve the external data associated with a N-API external value.", + "napi_get_value_external(env, external, &result);" + ] + }, + + "Napi objetc out": { + "prefix": "napiobjectout", + "body": [ + "// Convert a C object type to a JavaScript Object type.", + "napi_create_object(env, &objectOut);" + ] + }, + "Napi set value to object": { + "prefix": "napisetvalue2object", + "body": [ + "// Set a property on a JavaScript object by name.", + "napi_set_named_property(env, objectOut, propertyname, propertyValue);" + ] + }, + "Napi get value from object": { + "prefix": "napigetvaluefromobject", + "body": [ + "// Retrieve a named property from a JavaScript object.", + "napi_get_named_property(env, args[0], propertyName, &propertyValue);" + ] + }, + "Napi double out": { + "preix": "napidoubleout", + "body": [ + "// Convert a C double type to a JavaScript number type.", + "napi_create_double(env, doubleRes, &doubleOut);" + ] + }, + "Napi int32_t out": { + "prefix": "napiint32out", + "body": [ + "// Convert a C int32_t type to a JavaScript number type.", + "napi_create_int32(env, int32Res, &int32Out);" + ] + }, + "Napi uint32_t out": { + "prefix": "", + "body": [ + "// Convert a C uint32_t type to a JavaScript number type.", + "napi_create_uint32(env, uint32Res, &uint32Out);" + ] + }, + "Napi int64_t out": { + "prefix": "napiint64out", + "body": [ + "// Convert a C int64_t type to a JavaScript number type.", + "napi_create_int64(env, int64Res, &int64Out);" + ] + }, + "Napi bool out": { + "prefix": "napiboolout", + "body": [ + "// Convert a C bool type to a JavaScript boolean type.", + "napi_get_boolean(env, boolRes, &boolOut);" + ] + }, + "Napi string utf8 out": { + "prefix": "napistringutf8out", + "body": [ + "// Convert a C string uft8 type to a JavaScript string type", + "napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &stringOut);" + ] + }, + "Napi string utf16 out": { + "prefix": "napistringutf16out", + "body": [ + "// Convert a C string uft16 type to a JavaScript string type", + "napi_create_string_utf16(env, str, NAPI_AUTO_LENGTH, &stringOut);" + ] + }, + "Napi array out": { + "prefix": "napiarrayout", + "body": [ + "// Create a new JavaScript array with a specified length.", + "napi_create_array_with_length(env, length, &resultArray);", + "for (uint32_t i = 0; i < length; i++) {", + " // Set an element to a JavaScript array by index.", + " napi_set_element(env, resultArray, i, element);", + "}" + ] + }, + "Napi create external": { + "prefix": "napicreateexternal", + "body": [ + "// Create a new JavaScript object that serves as a wrapper for external data.", + "napi_create_external(env, data, finalizeCb, finalizeHint, &external);" + ] + } +} \ No newline at end of file -- Gitee From 53a2afc04c3046e91eb62c7811ecd7ca222b7e8b Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 15 Nov 2024 16:51:08 +0800 Subject: [PATCH 2/5] modify package.json tab Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index a226551a..7956676d 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -124,7 +124,7 @@ "language": "cpp", "path": "./snippets/napi_callback_snippets.json" }, - { + { "language": "cpp", "path": "./snippets/napi_variable_snippets.json" } -- Gitee From af8088223fb172744f48090fbf33de90665aea2c Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 21 Nov 2024 14:18:35 +0800 Subject: [PATCH 3/5] modify napi in/out snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_class_snippets.json | 10 +- .../snippets/napi_variable_snippets.json | 117 ++++++++++-------- 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index b94eb08f..f6f7bfcb 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -45,7 +45,8 @@ "}", "napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napiobjectin\" command that get object param from js.", + " napi_value jsThis;", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", " 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));", @@ -54,7 +55,10 @@ "}", "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napiobjectin\" command that get object param from js.", + " napi_value jsThis;", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", " HelloWorld* obj;", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", @@ -62,7 +66,7 @@ "}", "napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)", "{", - " OH_LOG_INFO(LOG_APP, \"HelloWorld::Hello World!\");", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"HelloWorld::Hello World!\");", " return nullptr;", "}", "napi_value HelloWorld::Init(napi_env env, napi_value exports)", diff --git a/src/vscode_plugin/snippets/napi_variable_snippets.json b/src/vscode_plugin/snippets/napi_variable_snippets.json index a3fceb41..2d6f8d50 100644 --- a/src/vscode_plugin/snippets/napi_variable_snippets.json +++ b/src/vscode_plugin/snippets/napi_variable_snippets.json @@ -1,14 +1,8 @@ { - "Napi object in":{ - "prefix": "napiobjectin", - "body": [ - "// Get js context and arguments.", - "napi_get_cb_info(env, info, &argc, args, &jsThis, &data);" - ] - }, "Napi double in": { "prefix": "napidoublein", "body": [ + "double value0 = 0;", "// Convert a JS number type value to a C double type.", "napi_get_value_double(env, args[0], &value0);" ] @@ -16,6 +10,7 @@ "Napi int32_t in": { "prefix": "napiint32in", "body": [ + "int32_t value0 = 0;", "// Convert a JS number type value to a C int32_t type.", "napi_get_value_int32(env, args[0], &value0);" ] @@ -23,6 +18,7 @@ "Napi uint32_t in": { "prefix": "napiuint32in", "body": [ + "uint32_t value0 = 0;", "// Convert a JS number type value to a C uint32_t type.", "napi_get_value_uint32(env, args[0], &value0);" ] @@ -30,6 +26,7 @@ "Napi int64_t in": { "prefix": "napiint64in", "body": [ + "int64_t value0 = 0;", "// Convert a JS number type value to a C int64_t type.", "napi_get_value_int64(env, args[0], &value0);" ] @@ -37,6 +34,7 @@ "Napi bool in": { "prefix": "napiboolin", "body": [ + "bool value0 = false;", "// Convert a JS boolean type value to a C bool type.", "napi_get_value_bool(env, args[0], &value0);" ] @@ -44,20 +42,33 @@ "Napi string utf8 in": { "prefix": "napistringutf8in", "body": [ + "size_t strUtf8Length = 0;", + "// Get the length of the js string.", + "napi_get_value_string_utf8(env, args[0], NULL, 0, &strUtf8Length);", + "char *value0 = new char[strUtf8Length + 1];", "// Get the js string as a UTF-8 encoded C string.", - "napi_get_value_string_utf8(env, args[0], value0, size, &result);" + "napi_get_value_string_utf8(env, args[0], value0, strUtf8Length + 1, &strUtf8Length);", + "// Todo: Assign value0 to actual business value", + "delete[] value0;" ] }, "Napi string utf16 in": { "prefix": "napistringutf16in", "body": [ + "size_t strUtf16Length = 0;", + "// Get the length of the js string.", + "napi_get_value_string_utf16(env, args[0], NULL, 0, &strUtf16Length);", + "char16_t *value0 = new char16_t[strUtf16Length + 1];", "// Get the js string as a UTF-16 encoded C string.", - "napi_get_value_string_utf16(env, args[0], value0, size, &result);" + "napi_get_value_string_utf16(env, args[0], value0, strUtf16Length + 1, &strUtf16Length);", + "// Todo: Assign value0 to actual business value", + "delete[] value0;" ] }, "Napi is array": { "prefix": "napiisarray", "body": [ + "bool isArray = false;", "// Check if the provided napi_value is a JavaScript array.", "napi_is_array(env, args[0], &isArray);" ] @@ -65,46 +76,56 @@ "Napi array in": { "prefix": "napiarrayin", "body": [ + "uint32_t length = 0;", "// Retrieve the length of a JavaScript array.", - "napi_get_array_length(env, array, &length);", + "napi_get_array_length(env, args[0], &length);", "for (uint32_t i = 0; i < length; i++) {", + " napi_value element;", " // Get an element from a JavaScript array by index.", - " napi_get_element(env, array, i, &element);", + " napi_get_element(env, args[0], i, &element);", "}" ] }, - "Napi get value external": { - "prefix": "napigetvalueexternal", + "Napi array reference in": { + "prefix": "napiarrayrefin", "body": [ - "// Retrieve the external data associated with a N-API external value.", - "napi_get_value_external(env, external, &result);" + "size_t argc = 1;", + "napi_value args[1];", + "int32_t* cppArray;", + "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + "// Get int32_t array from args[0].", + "napi_get_value_external(env, args[0], (void**)&cppArray);" ] }, - - "Napi objetc out": { - "prefix": "napiobjectout", - "body": [ - "// Convert a C object type to a JavaScript Object type.", - "napi_create_object(env, &objectOut);" - ] - }, - "Napi set value to object": { - "prefix": "napisetvalue2object", + "Napi array out": { + "prefix": "napiarrayout", "body": [ - "// Set a property on a JavaScript object by name.", - "napi_set_named_property(env, objectOut, propertyname, propertyValue);" + "napi_value resultArray;", + "size_t length = 3;", + "// Create a new JavaScript array with a specified length.", + "napi_create_array_with_length(env, length, &resultArray);", + "for (uint32_t i = 0; i < length; i++) {", + " napi_value element;", + " napi_create_int32(env, i, &element);", + " // Set an element to a JavaScript array by index.", + " napi_set_element(env, resultArray, i, element);", + "}" ] }, - "Napi get value from object": { - "prefix": "napigetvaluefromobject", + "Napi array reference out": { + "prefix": "napiarrayrefout", "body": [ - "// Retrieve a named property from a JavaScript object.", - "napi_get_named_property(env, args[0], propertyName, &propertyValue);" + "int32_t* cppArray = new int32_t[5]{1, 2, 3, 4, 5};", + "napi_value jsArray;", + "// FinalizeCallback is a optional callback that used to release data. (e.g.:delete[] cppArray;).", + "napi_create_external(env, cppArray, FinalizeCallback, nullptr, &jsArray);" ] }, "Napi double out": { "preix": "napidoubleout", "body": [ + "double doubleRes = 0;", + "napi_value doubleOut", "// Convert a C double type to a JavaScript number type.", "napi_create_double(env, doubleRes, &doubleOut);" ] @@ -112,6 +133,8 @@ "Napi int32_t out": { "prefix": "napiint32out", "body": [ + "int32_t int32Res = 0;", + "napi_value int32Out", "// Convert a C int32_t type to a JavaScript number type.", "napi_create_int32(env, int32Res, &int32Out);" ] @@ -119,6 +142,8 @@ "Napi uint32_t out": { "prefix": "", "body": [ + "uint32_t uint32Res = 0;", + "napi_value uint32Out", "// Convert a C uint32_t type to a JavaScript number type.", "napi_create_uint32(env, uint32Res, &uint32Out);" ] @@ -126,6 +151,8 @@ "Napi int64_t out": { "prefix": "napiint64out", "body": [ + "int64_t int64Res = 0;", + "napi_value int64Out", "// Convert a C int64_t type to a JavaScript number type.", "napi_create_int64(env, int64Res, &int64Out);" ] @@ -133,6 +160,8 @@ "Napi bool out": { "prefix": "napiboolout", "body": [ + "bool boolRes = false;", + "napi_value boolOut", "// Convert a C bool type to a JavaScript boolean type.", "napi_get_boolean(env, boolRes, &boolOut);" ] @@ -140,33 +169,19 @@ "Napi string utf8 out": { "prefix": "napistringutf8out", "body": [ + "const char* stringRes = \"hello world!\";", + "napi_value stringOut", "// Convert a C string uft8 type to a JavaScript string type", - "napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &stringOut);" + "napi_create_string_utf8(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" ] }, "Napi string utf16 out": { "prefix": "napistringutf16out", "body": [ + "const char16_t* stringRes = u\"hello world!\";", + "napi_value stringOut", "// Convert a C string uft16 type to a JavaScript string type", - "napi_create_string_utf16(env, str, NAPI_AUTO_LENGTH, &stringOut);" - ] - }, - "Napi array out": { - "prefix": "napiarrayout", - "body": [ - "// Create a new JavaScript array with a specified length.", - "napi_create_array_with_length(env, length, &resultArray);", - "for (uint32_t i = 0; i < length; i++) {", - " // Set an element to a JavaScript array by index.", - " napi_set_element(env, resultArray, i, element);", - "}" - ] - }, - "Napi create external": { - "prefix": "napicreateexternal", - "body": [ - "// Create a new JavaScript object that serves as a wrapper for external data.", - "napi_create_external(env, data, finalizeCb, finalizeHint, &external);" + "napi_create_string_utf16(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" ] } } \ No newline at end of file -- Gitee From dfa7472d71b3e7a506db64add32c8e636604df69 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 22 Nov 2024 17:32:14 +0800 Subject: [PATCH 4/5] modify napi in/out snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_class_snippets.json | 44 +++++++++---------- .../snippets/napi_variable_snippets.json | 20 --------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index f6f7bfcb..b6dc2990 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -2,13 +2,13 @@ "Napi Class": { "prefix": "napiclass", "body": [ - "class HelloWorld {", + "class A {", " 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();", + " explicit A();", + " ~A();", " 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);", @@ -17,59 +17,59 @@ " napi_env env_;", " napi_ref wrapper_;", "};", - "HelloWorld::HelloWorld(): 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)", + "A::A(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + "A::~A() { napi_delete_reference(env_, wrapper_); }", + "void A::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", "{", - " reinterpret_cast(nativeObject)->~HelloWorld();", + " reinterpret_cast(nativeObject)->~A();", "}", - "napi_value HelloWorld::New(napi_env env, napi_callback_info info)", + "napi_value A::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()`.", + " // Invoked as the constructor `new A()`.", " napi_value jsThis;", " // Retrieve the callback's context and arguments.", " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", - " HelloWorld* obj = new HelloWorld();", + " A* obj = new A();", " 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_);", + " napi_wrap(env, jsThis, reinterpret_cast(obj), A::Destructor, nullptr, &obj->wrapper_);", " return jsThis;", " } else {", - " OH_LOG_ERROR(LOG_APP, \"HelloWorld must be invoked as a constructor with `new`\");", + " OH_LOG_ERROR(LOG_APP, \"A must be invoked as a constructor with `new`\");", " return nullptr;", " }", "}", - "napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)", + "napi_value A::GetValue(napi_env env, napi_callback_info info)", "{", " napi_value jsThis;", " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", - " HelloWorld* obj;", + " A* 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 \"napistringutf8out\" command that return string utf8 value to js.", " return stringOut;", "}", - "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", + "napi_value A::SetValue(napi_env env, napi_callback_info info)", "{", " napi_value jsThis;", " size_t argc = 1;", " napi_value args[1];", " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", - " HelloWorld* obj;", + " A* obj;", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", " // 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)", + "napi_value A::Hello(napi_env env, napi_callback_info info)", "{", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"HelloWorld::Hello World!\");", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", " return nullptr;", "}", - "napi_value HelloWorld::Init(napi_env env, napi_value exports)", + "napi_value A::Init(napi_env env, napi_value exports)", "{", " // Define properties and methods for a N-API object", " napi_property_descriptor properties[] = {", @@ -78,9 +78,9 @@ " };", " 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);", + " napi_define_class(env, \"A\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Set the 'A' class on the exports object.", + " napi_set_named_property(env, exports, \"A\", cons);", " return exports;", "}" ] diff --git a/src/vscode_plugin/snippets/napi_variable_snippets.json b/src/vscode_plugin/snippets/napi_variable_snippets.json index 2d6f8d50..5e20621a 100644 --- a/src/vscode_plugin/snippets/napi_variable_snippets.json +++ b/src/vscode_plugin/snippets/napi_variable_snippets.json @@ -86,17 +86,6 @@ "}" ] }, - "Napi array reference in": { - "prefix": "napiarrayrefin", - "body": [ - "size_t argc = 1;", - "napi_value args[1];", - "int32_t* cppArray;", - "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", - "// Get int32_t array from args[0].", - "napi_get_value_external(env, args[0], (void**)&cppArray);" - ] - }, "Napi array out": { "prefix": "napiarrayout", "body": [ @@ -112,15 +101,6 @@ "}" ] }, - "Napi array reference out": { - "prefix": "napiarrayrefout", - "body": [ - "int32_t* cppArray = new int32_t[5]{1, 2, 3, 4, 5};", - "napi_value jsArray;", - "// FinalizeCallback is a optional callback that used to release data. (e.g.:delete[] cppArray;).", - "napi_create_external(env, cppArray, FinalizeCallback, nullptr, &jsArray);" - ] - }, "Napi double out": { "preix": "napidoubleout", "body": [ -- Gitee From 741076ff848167aab0a8170c13bd9117e5a5128d Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Mon, 25 Nov 2024 10:46:52 +0800 Subject: [PATCH 5/5] modify struct snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_struct_snippets.json | 88 +++++++++++++++++-- .../snippets/napi_variable_snippets.json | 14 +-- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_struct_snippets.json b/src/vscode_plugin/snippets/napi_struct_snippets.json index 69feb2eb..6f23b81d 100644 --- a/src/vscode_plugin/snippets/napi_struct_snippets.json +++ b/src/vscode_plugin/snippets/napi_struct_snippets.json @@ -2,12 +2,88 @@ "Napi Struct": { "prefix": "napistruct", "body": [ - "// Struct in", - "// Todo: you can use \"napiobjectin\" command that get object value from js.", - "// Todo: you can use \"napigetvaluefromobject\" command that get value from js object.", - "// Struct out", - "// Todo: you can use \"napiobjectout\" command that return object to js.", - "// Todo: you can use \"napisetvalue2object\" command that set value to objectOut." + "struct B {", + " public:", + " static napi_value Init(napi_env env, napi_value exports);", + " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", + " private:", + " explicit B();", + " ~B();", + " 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_;", + "};", + "B::B(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + "B::~B() { napi_delete_reference(env_, wrapper_); }", + "void B::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", + "{", + " reinterpret_cast(nativeObject)->~B();", + "}", + "napi_value B::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 B()`.", + " napi_value jsThis;", + " // Retrieve the callback's context and arguments.", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " B* obj = new B();", + " obj->env_ = env;", + " // Wrap the C++ object obj in the ArkTS object jsThis.", + " napi_wrap(env, jsThis, reinterpret_cast(obj), B::Destructor, nullptr, &obj->wrapper_);", + " return jsThis;", + " } else {", + " OH_LOG_ERROR(LOG_APP, \"B must be invoked as a constructor with `new`\");", + " return nullptr;", + " }", + "}", + "napi_value B::GetValue(napi_env env, napi_callback_info info)", + "{", + " napi_value jsThis;", + " // Get js context and arguments", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " B* 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 \"napistringutf8out\" command that return string utf8 value to js.", + " return stringOut;", + "}", + "napi_value B::SetValue(napi_env env, napi_callback_info info)", + "{", + " napi_value jsThis;", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", + " B* obj;", + " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", + " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", + " return nullptr;", + "}", + "napi_value B::Hello(napi_env env, napi_callback_info info)", + "{", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"Struct\", \"B::Hello World!\");", + " return nullptr;", + "}", + "napi_value B::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, \"B\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Set the 'B' class on the exports object.", + " napi_set_named_property(env, exports, \"B\", cons);", + " return exports;", + "}" ] } } \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_variable_snippets.json b/src/vscode_plugin/snippets/napi_variable_snippets.json index 5e20621a..b6e23409 100644 --- a/src/vscode_plugin/snippets/napi_variable_snippets.json +++ b/src/vscode_plugin/snippets/napi_variable_snippets.json @@ -105,7 +105,7 @@ "preix": "napidoubleout", "body": [ "double doubleRes = 0;", - "napi_value doubleOut", + "napi_value doubleOut;", "// Convert a C double type to a JavaScript number type.", "napi_create_double(env, doubleRes, &doubleOut);" ] @@ -114,7 +114,7 @@ "prefix": "napiint32out", "body": [ "int32_t int32Res = 0;", - "napi_value int32Out", + "napi_value int32Out;", "// Convert a C int32_t type to a JavaScript number type.", "napi_create_int32(env, int32Res, &int32Out);" ] @@ -123,7 +123,7 @@ "prefix": "", "body": [ "uint32_t uint32Res = 0;", - "napi_value uint32Out", + "napi_value uint32Out;", "// Convert a C uint32_t type to a JavaScript number type.", "napi_create_uint32(env, uint32Res, &uint32Out);" ] @@ -132,7 +132,7 @@ "prefix": "napiint64out", "body": [ "int64_t int64Res = 0;", - "napi_value int64Out", + "napi_value int64Out;", "// Convert a C int64_t type to a JavaScript number type.", "napi_create_int64(env, int64Res, &int64Out);" ] @@ -141,7 +141,7 @@ "prefix": "napiboolout", "body": [ "bool boolRes = false;", - "napi_value boolOut", + "napi_value boolOut;", "// Convert a C bool type to a JavaScript boolean type.", "napi_get_boolean(env, boolRes, &boolOut);" ] @@ -150,7 +150,7 @@ "prefix": "napistringutf8out", "body": [ "const char* stringRes = \"hello world!\";", - "napi_value stringOut", + "napi_value stringOut;", "// Convert a C string uft8 type to a JavaScript string type", "napi_create_string_utf8(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" ] @@ -159,7 +159,7 @@ "prefix": "napistringutf16out", "body": [ "const char16_t* stringRes = u\"hello world!\";", - "napi_value stringOut", + "napi_value stringOut;", "// Convert a C string uft16 type to a JavaScript string type", "napi_create_string_utf16(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" ] -- Gitee