1{ 2 "Napi Class": { 3 "prefix": "napiclass", 4 "body": [ 5 "class A {", 6 " public:", 7 " static napi_value Init(napi_env env, napi_value exports);", 8 " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", 9 " private:", 10 " explicit A();", 11 " ~A();", 12 " static napi_value New(napi_env env, napi_callback_info info);", 13 " static napi_value GetValue(napi_env env, napi_callback_info info);", 14 " static napi_value SetValue(napi_env env, napi_callback_info info);", 15 " static napi_value Hello(napi_env env, napi_callback_info info);", 16 " std::string value_;", 17 " napi_env env_;", 18 " napi_ref wrapper_;", 19 "};", 20 "A::A(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", 21 "A::~A() { napi_delete_reference(env_, wrapper_); }", 22 "void A::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", 23 "{", 24 " reinterpret_cast<A*>(nativeObject)->~A();", 25 "}", 26 "napi_value A::New(napi_env env, napi_callback_info info)", 27 "{", 28 " napi_value newTarget;", 29 " // Check if the constructor was invoked with new.", 30 " napi_get_new_target(env, info, &newTarget);", 31 " if (newTarget != nullptr) {", 32 " // Invoked as the constructor `new A()`.", 33 " napi_value jsThis;", 34 " // Retrieve the callback's context and arguments.", 35 " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", 36 " A* obj = new A();", 37 " obj->env_ = env;", 38 " // Wrap the C++ object obj in the ArkTS object jsThis.", 39 " napi_wrap(env, jsThis, reinterpret_cast<void*>(obj), A::Destructor, nullptr, &obj->wrapper_);", 40 " return jsThis;", 41 " } else {", 42 " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A must be invoked as a constructor with `new`\");", 43 " return nullptr;", 44 " }", 45 "}", 46 "napi_value A::GetValue(napi_env env, napi_callback_info info)", 47 "{", 48 " napi_value jsThis;", 49 " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", 50 " A* obj;", 51 " // Retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.", 52 " napi_unwrap(env, jsThis, reinterpret_cast<void**>(&obj));", 53 " // Todo: you can use \"napistringutf8out\" command that return string utf8 value to js.", 54 " return stringOut;", 55 "}", 56 "napi_value A::SetValue(napi_env env, napi_callback_info info)", 57 "{", 58 " napi_value jsThis;", 59 " size_t argc = 1;", 60 " napi_value args[1];", 61 " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", 62 " A* obj;", 63 " napi_unwrap(env, jsThis, reinterpret_cast<void**>(&obj));", 64 " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", 65 " return nullptr;", 66 "}", 67 "napi_value A::Hello(napi_env env, napi_callback_info info)", 68 "{", 69 " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", 70 " return nullptr;", 71 "}", 72 "napi_value A::Init(napi_env env, napi_value exports)", 73 "{", 74 " // Define properties and methods for a N-API object", 75 " napi_property_descriptor properties[] = {", 76 " { \"value\", 0, 0, GetValue, SetValue, 0, napi_default, 0 },", 77 " { \"hello\", nullptr, Hello, nullptr, nullptr, nullptr, napi_default, nullptr }", 78 " };", 79 " napi_value cons;", 80 " // Define a js class", 81 " napi_define_class(env, \"A\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", 82 " // Set the 'A' class on the exports object.", 83 " napi_set_named_property(env, exports, \"A\", cons);", 84 " return exports;", 85 "}" 86 ] 87 } 88}