{ "Napi Class": { "prefix": "napiclass", "body": [ "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 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);", " static napi_value Hello(napi_env env, napi_callback_info info);", " std::string value_;", " napi_env env_;", " napi_ref wrapper_;", "};", "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)->~A();", "}", "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 A()`.", " napi_value jsThis;", " // Retrieve the callback's context and arguments.", " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", " A* obj = new A();", " obj->env_ = env;", " // Wrap the C++ object obj in the ArkTS object jsThis.", " napi_wrap(env, jsThis, reinterpret_cast(obj), A::Destructor, nullptr, &obj->wrapper_);", " return jsThis;", " } else {", " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A must be invoked as a constructor with `new`\");", " return nullptr;", " }", "}", "napi_value A::GetValue(napi_env env, napi_callback_info info)", "{", " napi_value jsThis;", " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", " 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 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);", " 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 A::Hello(napi_env env, napi_callback_info info)", "{", " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", " return nullptr;", "}", "napi_value A::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, \"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;", "}" ] } }