@@ -135,6 +135,48 @@ struct CallbackData<Callable, void> {
135
135
void * data;
136
136
};
137
137
138
+ template <void (*Callback)(const CallbackInfo& info)>
139
+ static napi_value
140
+ TemplatedVoidCallback (napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
141
+ return details::WrapCallback ([&] {
142
+ CallbackInfo cbInfo (env, info);
143
+ Callback (cbInfo);
144
+ return nullptr ;
145
+ });
146
+ }
147
+
148
+ template <Napi::Value (*Callback)(const CallbackInfo& info)>
149
+ static napi_value
150
+ TemplatedCallback (napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
151
+ return details::WrapCallback ([&] {
152
+ CallbackInfo cbInfo (env, info);
153
+ return Callback (cbInfo);
154
+ });
155
+ }
156
+
157
+ template <typename T,
158
+ Napi::Value (T::*UnwrapCallback)(const CallbackInfo& info)>
159
+ static napi_value
160
+ TemplatedInstanceCallback(napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
161
+ return details::WrapCallback ([&] {
162
+ CallbackInfo cbInfo (env, info);
163
+ T* instance = T::Unwrap (cbInfo.This ().As <Object>());
164
+ return (instance->*UnwrapCallback)(cbInfo);
165
+ });
166
+ }
167
+
168
+ template <typename T, void (T::*UnwrapCallback)(const CallbackInfo& info)>
169
+ static napi_value
170
+ TemplatedInstanceVoidCallback (napi_env env,
171
+ napi_callback_info info) NAPI_NOEXCEPT {
172
+ return details::WrapCallback ([&] {
173
+ CallbackInfo cbInfo (env, info);
174
+ T* instance = T::Unwrap (cbInfo.This ().As <Object>());
175
+ (instance->*UnwrapCallback)(cbInfo);
176
+ return nullptr ;
177
+ });
178
+ }
179
+
138
180
template <typename T, typename Finalizer, typename Hint = void >
139
181
struct FinalizeData {
140
182
static inline
@@ -1845,30 +1887,25 @@ CreateFunction(napi_env env,
1845
1887
template <Function::VoidCallback cb>
1846
1888
inline Function Function::New (napi_env env, const char * utf8name, void * data) {
1847
1889
napi_value result = nullptr ;
1848
- napi_status status = napi_create_function (
1849
- env, utf8name, NAPI_AUTO_LENGTH,
1850
- [](napi_env env, napi_callback_info info) {
1851
- CallbackInfo callbackInfo (env, info);
1852
- return details::WrapCallback ([&] {
1853
- cb (callbackInfo);
1854
- return nullptr ;
1855
- });
1856
- }, data, &result);
1890
+ napi_status status = napi_create_function (env,
1891
+ utf8name,
1892
+ NAPI_AUTO_LENGTH,
1893
+ details::TemplatedVoidCallback<cb>,
1894
+ data,
1895
+ &result);
1857
1896
NAPI_THROW_IF_FAILED (env, status, Function ());
1858
1897
return Function (env, result);
1859
1898
}
1860
1899
1861
1900
template <Function::Callback cb>
1862
1901
inline Function Function::New (napi_env env, const char * utf8name, void * data) {
1863
1902
napi_value result = nullptr ;
1864
- napi_status status = napi_create_function (
1865
- env, utf8name, NAPI_AUTO_LENGTH,
1866
- [](napi_env env, napi_callback_info info) {
1867
- CallbackInfo callbackInfo (env, info);
1868
- return details::WrapCallback ([&] {
1869
- return cb (callbackInfo);
1870
- });
1871
- }, data, &result);
1903
+ napi_status status = napi_create_function (env,
1904
+ utf8name,
1905
+ NAPI_AUTO_LENGTH,
1906
+ details::TemplatedCallback<cb>,
1907
+ data,
1908
+ &result);
1872
1909
NAPI_THROW_IF_FAILED (env, status, Function ());
1873
1910
return Function (env, result);
1874
1911
}
@@ -2859,7 +2896,7 @@ PropertyDescriptor::Accessor(const char* utf8name,
2859
2896
napi_property_descriptor desc = napi_property_descriptor ();
2860
2897
2861
2898
desc.utf8name = utf8name;
2862
- desc.getter = &GetterCallbackWrapper <Getter>;
2899
+ desc.getter = details::TemplatedCallback <Getter>;
2863
2900
desc.attributes = attributes;
2864
2901
desc.data = data;
2865
2902
@@ -2882,7 +2919,7 @@ PropertyDescriptor::Accessor(Name name,
2882
2919
napi_property_descriptor desc = napi_property_descriptor ();
2883
2920
2884
2921
desc.name = name;
2885
- desc.getter = &GetterCallbackWrapper <Getter>;
2922
+ desc.getter = details::TemplatedCallback <Getter>;
2886
2923
desc.attributes = attributes;
2887
2924
desc.data = data;
2888
2925
@@ -2900,8 +2937,8 @@ PropertyDescriptor::Accessor(const char* utf8name,
2900
2937
napi_property_descriptor desc = napi_property_descriptor ();
2901
2938
2902
2939
desc.utf8name = utf8name;
2903
- desc.getter = &GetterCallbackWrapper <Getter>;
2904
- desc.setter = &SetterCallbackWrapper <Setter>;
2940
+ desc.getter = details::TemplatedCallback <Getter>;
2941
+ desc.setter = details::TemplatedVoidCallback <Setter>;
2905
2942
desc.attributes = attributes;
2906
2943
desc.data = data;
2907
2944
@@ -2928,31 +2965,14 @@ PropertyDescriptor::Accessor(Name name,
2928
2965
napi_property_descriptor desc = napi_property_descriptor ();
2929
2966
2930
2967
desc.name = name;
2931
- desc.getter = &GetterCallbackWrapper <Getter>;
2932
- desc.setter = &SetterCallbackWrapper <Setter>;
2968
+ desc.getter = details::TemplatedCallback <Getter>;
2969
+ desc.setter = details::TemplatedVoidCallback <Setter>;
2933
2970
desc.attributes = attributes;
2934
2971
desc.data = data;
2935
2972
2936
2973
return desc;
2937
2974
}
2938
2975
2939
- template <typename PropertyDescriptor::GetterCallback Getter>
2940
- napi_value
2941
- PropertyDescriptor::GetterCallbackWrapper (napi_env env,
2942
- napi_callback_info info) {
2943
- CallbackInfo cbInfo (env, info);
2944
- return Getter (cbInfo);
2945
- }
2946
-
2947
- template <typename PropertyDescriptor::SetterCallback Setter>
2948
- napi_value
2949
- PropertyDescriptor::SetterCallbackWrapper (napi_env env,
2950
- napi_callback_info info) {
2951
- CallbackInfo cbInfo (env, info);
2952
- Setter (cbInfo);
2953
- return nullptr ;
2954
- }
2955
-
2956
2976
template <typename Getter>
2957
2977
inline PropertyDescriptor
2958
2978
PropertyDescriptor::Accessor (Napi::Env env,
@@ -3283,7 +3303,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
3283
3303
void * data) {
3284
3304
napi_property_descriptor desc = napi_property_descriptor ();
3285
3305
desc.utf8name = utf8name;
3286
- desc.method = &InstanceWrap<T>::WrappedMethod< method>;
3306
+ desc.method = details::TemplatedInstanceVoidCallback<T, method>;
3287
3307
desc.data = data;
3288
3308
desc.attributes = attributes;
3289
3309
return desc;
@@ -3297,7 +3317,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
3297
3317
void * data) {
3298
3318
napi_property_descriptor desc = napi_property_descriptor ();
3299
3319
desc.utf8name = utf8name;
3300
- desc.method = &InstanceWrap<T>::WrappedMethod< method>;
3320
+ desc.method = details::TemplatedInstanceCallback<T, method>;
3301
3321
desc.data = data;
3302
3322
desc.attributes = attributes;
3303
3323
return desc;
@@ -3311,7 +3331,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
3311
3331
void * data) {
3312
3332
napi_property_descriptor desc = napi_property_descriptor ();
3313
3333
desc.name = name;
3314
- desc.method = &InstanceWrap<T>::WrappedMethod< method>;
3334
+ desc.method = details::TemplatedInstanceVoidCallback<T, method>;
3315
3335
desc.data = data;
3316
3336
desc.attributes = attributes;
3317
3337
return desc;
@@ -3325,7 +3345,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceMethod(
3325
3345
void * data) {
3326
3346
napi_property_descriptor desc = napi_property_descriptor ();
3327
3347
desc.name = name;
3328
- desc.method = &InstanceWrap<T>::WrappedMethod< method>;
3348
+ desc.method = details::TemplatedInstanceCallback<T, method>;
3329
3349
desc.data = data;
3330
3350
desc.attributes = attributes;
3331
3351
return desc;
@@ -3378,7 +3398,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
3378
3398
void * data) {
3379
3399
napi_property_descriptor desc = napi_property_descriptor ();
3380
3400
desc.utf8name = utf8name;
3381
- desc.getter = This::WrapGetter (This::GetterTag< getter>()) ;
3401
+ desc.getter = details::TemplatedInstanceCallback<T, getter>;
3382
3402
desc.setter = This::WrapSetter (This::SetterTag<setter>());
3383
3403
desc.data = data;
3384
3404
desc.attributes = attributes;
@@ -3394,7 +3414,7 @@ inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceAccessor(
3394
3414
void * data) {
3395
3415
napi_property_descriptor desc = napi_property_descriptor ();
3396
3416
desc.name = name;
3397
- desc.getter = This::WrapGetter (This::GetterTag< getter>()) ;
3417
+ desc.getter = details::TemplatedInstanceCallback<T, getter>;
3398
3418
desc.setter = This::WrapSetter (This::SetterTag<setter>());
3399
3419
desc.data = data;
3400
3420
desc.attributes = attributes;
@@ -3487,27 +3507,6 @@ inline napi_value InstanceWrap<T>::InstanceSetterCallbackWrapper(
3487
3507
});
3488
3508
}
3489
3509
3490
- template <typename T>
3491
- template <typename InstanceWrap<T>::InstanceVoidMethodCallback method>
3492
- inline napi_value InstanceWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
3493
- return details::WrapCallback ([&] {
3494
- const CallbackInfo cbInfo (env, info);
3495
- T* instance = T::Unwrap (cbInfo.This ().As <Object>());
3496
- (instance->*method)(cbInfo);
3497
- return nullptr ;
3498
- });
3499
- }
3500
-
3501
- template <typename T>
3502
- template <typename InstanceWrap<T>::InstanceMethodCallback method>
3503
- inline napi_value InstanceWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
3504
- return details::WrapCallback ([&] {
3505
- const CallbackInfo cbInfo (env, info);
3506
- T* instance = T::Unwrap (cbInfo.This ().As <Object>());
3507
- return (instance->*method)(cbInfo);
3508
- });
3509
- }
3510
-
3511
3510
template <typename T>
3512
3511
template <typename InstanceWrap<T>::InstanceSetterCallback method>
3513
3512
inline napi_value InstanceWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
@@ -3732,7 +3731,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3732
3731
void * data) {
3733
3732
napi_property_descriptor desc = napi_property_descriptor ();
3734
3733
desc.utf8name = utf8name;
3735
- desc.method = &ObjectWrap<T>::WrappedMethod <method>;
3734
+ desc.method = details::TemplatedVoidCallback <method>;
3736
3735
desc.data = data;
3737
3736
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
3738
3737
return desc;
@@ -3746,7 +3745,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3746
3745
void * data) {
3747
3746
napi_property_descriptor desc = napi_property_descriptor ();
3748
3747
desc.name = name;
3749
- desc.method = &ObjectWrap<T>::WrappedMethod <method>;
3748
+ desc.method = details::TemplatedVoidCallback <method>;
3750
3749
desc.data = data;
3751
3750
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
3752
3751
return desc;
@@ -3760,7 +3759,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3760
3759
void * data) {
3761
3760
napi_property_descriptor desc = napi_property_descriptor ();
3762
3761
desc.utf8name = utf8name;
3763
- desc.method = &ObjectWrap<T>::WrappedMethod <method>;
3762
+ desc.method = details::TemplatedCallback <method>;
3764
3763
desc.data = data;
3765
3764
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
3766
3765
return desc;
@@ -3774,7 +3773,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3774
3773
void * data) {
3775
3774
napi_property_descriptor desc = napi_property_descriptor ();
3776
3775
desc.name = name;
3777
- desc.method = &ObjectWrap<T>::WrappedMethod <method>;
3776
+ desc.method = details::TemplatedCallback <method>;
3778
3777
desc.data = data;
3779
3778
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
3780
3779
return desc;
@@ -3827,7 +3826,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
3827
3826
void * data) {
3828
3827
napi_property_descriptor desc = napi_property_descriptor ();
3829
3828
desc.utf8name = utf8name;
3830
- desc.getter = This::WrapStaticGetter (This::StaticGetterTag <getter>()) ;
3829
+ desc.getter = details::TemplatedCallback <getter>;
3831
3830
desc.setter = This::WrapStaticSetter (This::StaticSetterTag<setter>());
3832
3831
desc.data = data;
3833
3832
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
@@ -3843,7 +3842,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
3843
3842
void * data) {
3844
3843
napi_property_descriptor desc = napi_property_descriptor ();
3845
3844
desc.name = name;
3846
- desc.getter = This::WrapStaticGetter (This::StaticGetterTag <getter>()) ;
3845
+ desc.getter = details::TemplatedCallback <getter>;
3847
3846
desc.setter = This::WrapStaticSetter (This::StaticSetterTag<setter>());
3848
3847
desc.data = data;
3849
3848
desc.attributes = static_cast <napi_property_attributes>(attributes | napi_static);
@@ -3969,23 +3968,6 @@ inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* /*hi
3969
3968
delete instance;
3970
3969
}
3971
3970
3972
- template <typename T>
3973
- template <typename ObjectWrap<T>::StaticVoidMethodCallback method>
3974
- inline napi_value ObjectWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
3975
- return details::WrapCallback ([&] {
3976
- method (CallbackInfo (env, info));
3977
- return nullptr ;
3978
- });
3979
- }
3980
-
3981
- template <typename T>
3982
- template <typename ObjectWrap<T>::StaticMethodCallback method>
3983
- inline napi_value ObjectWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
3984
- return details::WrapCallback ([&] {
3985
- return method (CallbackInfo (env, info));
3986
- });
3987
- }
3988
-
3989
3971
template <typename T>
3990
3972
template <typename ObjectWrap<T>::StaticSetterCallback method>
3991
3973
inline napi_value ObjectWrap<T>::WrappedMethod(napi_env env, napi_callback_info info) noexcept {
0 commit comments