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