11cb0ef41Sopenharmony_ci#include "myobject.h" 21cb0ef41Sopenharmony_ci#include "../common.h" 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_cisize_t finalize_count = 0; 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciMyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {} 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciMyObject::~MyObject() { 91cb0ef41Sopenharmony_ci finalize_count++; 101cb0ef41Sopenharmony_ci napi_delete_reference(env_, wrapper_); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_civoid MyObject::Destructor( 141cb0ef41Sopenharmony_ci napi_env env, void* nativeObject, void* /*finalize_hint*/) { 151cb0ef41Sopenharmony_ci MyObject* obj = static_cast<MyObject*>(nativeObject); 161cb0ef41Sopenharmony_ci delete obj; 171cb0ef41Sopenharmony_ci} 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cinapi_ref MyObject::constructor; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cinapi_status MyObject::Init(napi_env env) { 221cb0ef41Sopenharmony_ci napi_status status; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci napi_value cons; 251cb0ef41Sopenharmony_ci status = napi_define_class( 261cb0ef41Sopenharmony_ci env, "MyObject", -1, New, nullptr, 0, nullptr, &cons); 271cb0ef41Sopenharmony_ci if (status != napi_ok) return status; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci status = napi_create_reference(env, cons, 1, &constructor); 301cb0ef41Sopenharmony_ci if (status != napi_ok) return status; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci return napi_ok; 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_cinapi_value MyObject::New(napi_env env, napi_callback_info info) { 361cb0ef41Sopenharmony_ci size_t argc = 1; 371cb0ef41Sopenharmony_ci napi_value args[1]; 381cb0ef41Sopenharmony_ci napi_value _this; 391cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr)); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci MyObject* obj = new MyObject(); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci napi_valuetype valuetype; 441cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype)); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci if (valuetype == napi_undefined) { 471cb0ef41Sopenharmony_ci obj->val_ = 0; 481cb0ef41Sopenharmony_ci } else { 491cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_double(env, args[0], &obj->val_)); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci obj->env_ = env; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci // The below call to napi_wrap() must request a reference to the wrapped 551cb0ef41Sopenharmony_ci // object via the out-parameter, because this ensures that we test the code 561cb0ef41Sopenharmony_ci // path that deals with a reference that is destroyed from its own finalizer. 571cb0ef41Sopenharmony_ci NODE_API_CALL(env, 581cb0ef41Sopenharmony_ci napi_wrap(env, _this, obj, MyObject::Destructor, 591cb0ef41Sopenharmony_ci nullptr /* finalize_hint */, &obj->wrapper_)); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci return _this; 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_cinapi_status MyObject::NewInstance(napi_env env, 651cb0ef41Sopenharmony_ci napi_value arg, 661cb0ef41Sopenharmony_ci napi_value* instance) { 671cb0ef41Sopenharmony_ci napi_status status; 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci const int argc = 1; 701cb0ef41Sopenharmony_ci napi_value argv[argc] = {arg}; 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci napi_value cons; 731cb0ef41Sopenharmony_ci status = napi_get_reference_value(env, constructor, &cons); 741cb0ef41Sopenharmony_ci if (status != napi_ok) return status; 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci status = napi_new_instance(env, cons, argc, argv, instance); 771cb0ef41Sopenharmony_ci if (status != napi_ok) return status; 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci return napi_ok; 801cb0ef41Sopenharmony_ci} 81