修改napi class的snippets

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-11-05 17:27:20 +08:00
parent 78b060854f
commit 866d668fcb

View File

@ -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<HelloWorld*>(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<void*>(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<void**>(&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<void**>(&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<void**>(&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<HelloWorld*>(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<void*>(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<void**>(&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<void**>(&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;",
"}"
]
}
}