11cb0ef41Sopenharmony_ci#include "myobject.h"
21cb0ef41Sopenharmony_ci#include "../common.h"
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cistatic int finalize_count = 0;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciMyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {}
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciMyObject::~MyObject() { napi_delete_reference(env_, wrapper_); }
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_civoid MyObject::Destructor(napi_env env,
111cb0ef41Sopenharmony_ci                          void* nativeObject,
121cb0ef41Sopenharmony_ci                          void* /*finalize_hint*/) {
131cb0ef41Sopenharmony_ci  ++finalize_count;
141cb0ef41Sopenharmony_ci  MyObject* obj = static_cast<MyObject*>(nativeObject);
151cb0ef41Sopenharmony_ci  delete obj;
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cinapi_value MyObject::GetFinalizeCount(napi_env env, napi_callback_info info) {
191cb0ef41Sopenharmony_ci  napi_value result;
201cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_int32(env, finalize_count, &result));
211cb0ef41Sopenharmony_ci  return result;
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cinapi_ref MyObject::constructor;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cinapi_status MyObject::Init(napi_env env) {
271cb0ef41Sopenharmony_ci  napi_status status;
281cb0ef41Sopenharmony_ci  napi_property_descriptor properties[] = {
291cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("plusOne", PlusOne),
301cb0ef41Sopenharmony_ci  };
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  napi_value cons;
331cb0ef41Sopenharmony_ci  status = napi_define_class(
341cb0ef41Sopenharmony_ci      env, "MyObject", -1, New, nullptr, 1, properties, &cons);
351cb0ef41Sopenharmony_ci  if (status != napi_ok) return status;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  status = napi_create_reference(env, cons, 1, &constructor);
381cb0ef41Sopenharmony_ci  if (status != napi_ok) return status;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  return napi_ok;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cinapi_value MyObject::New(napi_env env, napi_callback_info info) {
441cb0ef41Sopenharmony_ci  size_t argc = 1;
451cb0ef41Sopenharmony_ci  napi_value args[1];
461cb0ef41Sopenharmony_ci  napi_value _this;
471cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  napi_valuetype valuetype;
501cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype));
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  MyObject* obj = new MyObject();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  if (valuetype == napi_undefined) {
551cb0ef41Sopenharmony_ci    obj->counter_ = 0;
561cb0ef41Sopenharmony_ci  } else {
571cb0ef41Sopenharmony_ci    NODE_API_CALL(env, napi_get_value_uint32(env, args[0], &obj->counter_));
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  obj->env_ = env;
611cb0ef41Sopenharmony_ci  NODE_API_CALL(env,
621cb0ef41Sopenharmony_ci      napi_wrap(
631cb0ef41Sopenharmony_ci          env, _this, obj, MyObject::Destructor, nullptr /* finalize_hint */,
641cb0ef41Sopenharmony_ci          &obj->wrapper_));
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  return _this;
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cinapi_status MyObject::NewInstance(napi_env env,
701cb0ef41Sopenharmony_ci                                  napi_value arg,
711cb0ef41Sopenharmony_ci                                  napi_value* instance) {
721cb0ef41Sopenharmony_ci  napi_status status;
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  const int argc = 1;
751cb0ef41Sopenharmony_ci  napi_value argv[argc] = {arg};
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  napi_value cons;
781cb0ef41Sopenharmony_ci  status = napi_get_reference_value(env, constructor, &cons);
791cb0ef41Sopenharmony_ci  if (status != napi_ok) return status;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  status = napi_new_instance(env, cons, argc, argv, instance);
821cb0ef41Sopenharmony_ci  if (status != napi_ok) return status;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  return napi_ok;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_cinapi_value MyObject::PlusOne(napi_env env, napi_callback_info info) {
881cb0ef41Sopenharmony_ci  napi_value _this;
891cb0ef41Sopenharmony_ci  NODE_API_CALL(env,
901cb0ef41Sopenharmony_ci      napi_get_cb_info(env, info, nullptr, nullptr, &_this, nullptr));
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  MyObject* obj;
931cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  obj->counter_ += 1;
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  napi_value num;
981cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_uint32(env, obj->counter_, &num));
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  return num;
1011cb0ef41Sopenharmony_ci}
102