Skip to content

Commit d45ba72

Browse files
Sampson Gaojasongin
authored andcommitted
Move napi_wrap into ObjectWrap constructor (#67)
1 parent 8a990fc commit d45ba72

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

napi-inl.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,17 @@ inline PropertyDescriptor::operator const napi_property_descriptor&() const {
22742274
////////////////////////////////////////////////////////////////////////////////
22752275

22762276
template <typename T>
2277-
inline ObjectWrap<T>::ObjectWrap() {
2277+
inline ObjectWrap<T>::ObjectWrap(Napi::CallbackInfo callbackInfo) {
2278+
napi_env env = callbackInfo.Env();
2279+
napi_value wrapper = callbackInfo.This();
2280+
napi_status status;
2281+
napi_ref ref;
2282+
T* instance = static_cast<T*>(this);
2283+
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
2284+
NAPI_THROW_IF_FAILED(env, status)
2285+
2286+
Reference<Object>* instanceRef = instance;
2287+
*instanceRef = Reference<Object>(env, ref);
22782288
}
22792289

22802290
template<typename T>
@@ -2468,13 +2478,6 @@ inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
24682478
return nullptr;
24692479
});
24702480

2471-
napi_ref ref;
2472-
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
2473-
if (status != napi_ok) return nullptr;
2474-
2475-
Reference<Object>* instanceRef = instance;
2476-
*instanceRef = Reference<Object>(env, ref);
2477-
24782481
return wrapper;
24792482
}
24802483

napi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ namespace Napi {
12741274
template <typename T>
12751275
class ObjectWrap : public Reference<Object> {
12761276
public:
1277-
ObjectWrap();
1277+
ObjectWrap(CallbackInfo callbackInfo);
12781278

12791279
static T* Unwrap(Object wrapper);
12801280

0 commit comments

Comments
 (0)