diff options
Diffstat (limited to 'test/addons-napi/7_factory_wrap/myobject.cc')
-rw-r--r-- | test/addons-napi/7_factory_wrap/myobject.cc | 62 |
1 files changed, 23 insertions, 39 deletions
diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index a3fbf6f9ec..d6b374d7bb 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -1,4 +1,5 @@ #include "myobject.h" +#include "../common.h" MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {} @@ -11,15 +12,12 @@ void MyObject::Destructor(napi_env env, delete obj; } -#define DECLARE_NAPI_METHOD(name, func) \ - { name, func, 0, 0, 0, napi_default, 0 } - napi_ref MyObject::constructor; napi_status MyObject::Init(napi_env env) { napi_status status; napi_property_descriptor properties[] = { - DECLARE_NAPI_METHOD("plusOne", PlusOne), + DECLARE_NAPI_PROPERTY("plusOne", PlusOne), }; napi_value cons; @@ -33,41 +31,32 @@ napi_status MyObject::Init(napi_env env) { return napi_ok; } -void MyObject::New(napi_env env, napi_callback_info info) { - napi_status status; - +napi_value MyObject::New(napi_env env, napi_callback_info info) { + size_t argc = 1; napi_value args[1]; - status = napi_get_cb_args(env, info, args, 1); - if (status != napi_ok) return; + napi_value _this; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr)); napi_valuetype valuetype; - status = napi_typeof(env, args[0], &valuetype); - if (status != napi_ok) return; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); MyObject* obj = new MyObject(); if (valuetype == napi_undefined) { obj->counter_ = 0; } else { - status = napi_get_value_double(env, args[0], &obj->counter_); - if (status != napi_ok) return; + NAPI_CALL(env, napi_get_value_double(env, args[0], &obj->counter_)); } - napi_value jsthis; - status = napi_get_cb_this(env, info, &jsthis); - if (status != napi_ok) return; - obj->env_ = env; - status = napi_wrap(env, - jsthis, - obj, - MyObject::Destructor, - nullptr, /* finalize_hint */ - &obj->wrapper_); - if (status != napi_ok) return; - - status = napi_set_return_value(env, info, jsthis); - if (status != napi_ok) return; + NAPI_CALL(env, napi_wrap(env, + _this, + obj, + MyObject::Destructor, + nullptr, /* finalize_hint */ + &obj->wrapper_)); + + return _this; } napi_status MyObject::NewInstance(napi_env env, @@ -88,23 +77,18 @@ napi_status MyObject::NewInstance(napi_env env, return napi_ok; } -void MyObject::PlusOne(napi_env env, napi_callback_info info) { - napi_status status; - - napi_value jsthis; - status = napi_get_cb_this(env, info, &jsthis); - if (status != napi_ok) return; +napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) { + napi_value _this; + NAPI_CALL(env, + napi_get_cb_info(env, info, nullptr, nullptr, &_this, nullptr)); MyObject* obj; - status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj)); - if (status != napi_ok) return; + NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj))); obj->counter_ += 1; napi_value num; - status = napi_create_number(env, obj->counter_, &num); - if (status != napi_ok) return; + NAPI_CALL(env, napi_create_number(env, obj->counter_, &num)); - status = napi_set_return_value(env, info, num); - if (status != napi_ok) return; + return num; } |