diff --git a/benchmark/function_args.cc b/benchmark/function_args.cc index e265bca95..90702872a 100644 --- a/benchmark/function_args.cc +++ b/benchmark/function_args.cc @@ -82,49 +82,49 @@ class FunctionArgs : public Napi::Addon { public: FunctionArgs(Napi::Env env, Napi::Object exports) { // Define the core bindings using plain N-API. - napi_value no_arg_function, one_arg_function, two_arg_function, - three_arg_function, four_arg_function; - napi_status status; - - status = napi_create_function(env, - "noArgFunction", - NAPI_AUTO_LENGTH, - NoArgFunction_Core, - nullptr, - &no_arg_function); - NAPI_THROW_IF_FAILED_VOID(env, status); - - status = napi_create_function(env, - "oneArgFunction", - NAPI_AUTO_LENGTH, - OneArgFunction_Core, - nullptr, - &one_arg_function); - NAPI_THROW_IF_FAILED_VOID(env, status); - - status = napi_create_function(env, - "twoArgFunction", - NAPI_AUTO_LENGTH, - TwoArgFunction_Core, - nullptr, - &two_arg_function); - NAPI_THROW_IF_FAILED_VOID(env, status); - - status = napi_create_function(env, - "threeArgFunction", - NAPI_AUTO_LENGTH, - ThreeArgFunction_Core, - nullptr, - &three_arg_function); - NAPI_THROW_IF_FAILED_VOID(env, status); - - status = napi_create_function(env, - "fourArgFunction", - NAPI_AUTO_LENGTH, - FourArgFunction_Core, - nullptr, - &four_arg_function); - NAPI_THROW_IF_FAILED_VOID(env, status); + napi_value no_arg_function, one_arg_function, two_arg_function, + three_arg_function, four_arg_function; + napi_status status; + + status = napi_create_function(env, + "noArgFunction", + NAPI_AUTO_LENGTH, + NoArgFunction_Core, + nullptr, + &no_arg_function); + NAPI_THROW_IF_FAILED_VOID(env, status); + + status = napi_create_function(env, + "oneArgFunction", + NAPI_AUTO_LENGTH, + OneArgFunction_Core, + nullptr, + &one_arg_function); + NAPI_THROW_IF_FAILED_VOID(env, status); + + status = napi_create_function(env, + "twoArgFunction", + NAPI_AUTO_LENGTH, + TwoArgFunction_Core, + nullptr, + &two_arg_function); + NAPI_THROW_IF_FAILED_VOID(env, status); + + status = napi_create_function(env, + "threeArgFunction", + NAPI_AUTO_LENGTH, + ThreeArgFunction_Core, + nullptr, + &three_arg_function); + NAPI_THROW_IF_FAILED_VOID(env, status); + + status = napi_create_function(env, + "fourArgFunction", + NAPI_AUTO_LENGTH, + FourArgFunction_Core, + nullptr, + &four_arg_function); + NAPI_THROW_IF_FAILED_VOID(env, status); DefineAddon(exports, { InstanceValue("core", DefineProperties(Napi::Object::New(env), { diff --git a/benchmark/property_descriptor.cc b/benchmark/property_descriptor.cc index c1e454bd8..c5e5db648 100644 --- a/benchmark/property_descriptor.cc +++ b/benchmark/property_descriptor.cc @@ -29,32 +29,32 @@ static void Setter(const Napi::CallbackInfo& info) { class PropDescBenchmark : public Napi::Addon { public: PropDescBenchmark(Napi::Env env, Napi::Object exports) { - napi_status status; - napi_property_descriptor core_prop = { - "core", - nullptr, - nullptr, - Getter_Core, - Setter_Core, - nullptr, - napi_enumerable, - nullptr - }; + napi_status status; + napi_property_descriptor core_prop = { + "core", + nullptr, + nullptr, + Getter_Core, + Setter_Core, + nullptr, + napi_enumerable, + nullptr + }; - status = napi_define_properties(env, exports, 1, &core_prop); - NAPI_THROW_IF_FAILED_VOID(env, status); + status = napi_define_properties(env, exports, 1, &core_prop); + NAPI_THROW_IF_FAILED_VOID(env, status); - exports.DefineProperty( - Napi::PropertyDescriptor::Accessor(env, - exports, - "cplusplus", - Getter, - Setter, - napi_enumerable)); + exports.DefineProperty( + Napi::PropertyDescriptor::Accessor(env, + exports, + "cplusplus", + Getter, + Setter, + napi_enumerable)); - exports.DefineProperty( - Napi::PropertyDescriptor::Accessor("templated", - napi_enumerable)); + exports.DefineProperty( + Napi::PropertyDescriptor::Accessor("templated", + napi_enumerable)); DefineAddon(exports, { InstanceAccessor("instance", diff --git a/napi-inl.h b/napi-inl.h index 95b9d0a87..885e27eb6 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3187,6 +3187,316 @@ inline void InstanceWrap::AttachPropData(napi_env env, } } +template +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + const char* utf8name, + InstanceVoidMethodCallback method, + napi_property_attributes attributes, + void* data) { + InstanceVoidMethodCallbackData* callbackData = + new InstanceVoidMethodCallbackData({ method, data}); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.method = T::InstanceVoidMethodCallbackWrapper; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + const char* utf8name, + InstanceMethodCallback method, + napi_property_attributes attributes, + void* data) { + InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data }); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.method = T::InstanceMethodCallbackWrapper; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + Symbol name, + InstanceVoidMethodCallback method, + napi_property_attributes attributes, + void* data) { + InstanceVoidMethodCallbackData* callbackData = + new InstanceVoidMethodCallbackData({ method, data}); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.method = T::InstanceVoidMethodCallbackWrapper; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + Symbol name, + InstanceMethodCallback method, + napi_property_attributes attributes, + void* data) { + InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data }); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.method = T::InstanceMethodCallbackWrapper; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceVoidMethodCallback method> +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + const char* utf8name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.method = &InstanceWrap::WrappedMethod; + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceMethodCallback method> +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + const char* utf8name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.method = &InstanceWrap::WrappedMethod; + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceVoidMethodCallback method> +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + Symbol name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.method = &InstanceWrap::WrappedMethod; + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceMethodCallback method> +inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( + Symbol name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.method = &InstanceWrap::WrappedMethod; + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( + const char* utf8name, + InstanceGetterCallback getter, + InstanceSetterCallback setter, + napi_property_attributes attributes, + void* data) { + InstanceAccessorCallbackData* callbackData = + new InstanceAccessorCallbackData({ getter, setter, data }); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr; + desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( + Symbol name, + InstanceGetterCallback getter, + InstanceSetterCallback setter, + napi_property_attributes attributes, + void* data) { + InstanceAccessorCallbackData* callbackData = + new InstanceAccessorCallbackData({ getter, setter, data }); + + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr; + desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr; + desc.data = callbackData; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceGetterCallback getter, + typename InstanceWrap::InstanceSetterCallback setter> +inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( + const char* utf8name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.getter = This::WrapGetter(This::GetterTag()); + desc.setter = This::WrapSetter(This::SetterTag()); + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +template ::InstanceGetterCallback getter, + typename InstanceWrap::InstanceSetterCallback setter> +inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( + Symbol name, + napi_property_attributes attributes, + void* data) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.getter = This::WrapGetter(This::GetterTag()); + desc.setter = This::WrapSetter(This::SetterTag()); + desc.data = data; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceValue( + const char* utf8name, + Napi::Value value, + napi_property_attributes attributes) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.utf8name = utf8name; + desc.value = value; + desc.attributes = attributes; + return desc; +} + +template +inline ClassPropertyDescriptor InstanceWrap::InstanceValue( + Symbol name, + Napi::Value value, + napi_property_attributes attributes) { + napi_property_descriptor desc = napi_property_descriptor(); + desc.name = name; + desc.value = value; + desc.attributes = attributes; + return desc; +} + +template +inline napi_value InstanceWrap::InstanceVoidMethodCallbackWrapper( + napi_env env, + napi_callback_info info) { + return details::WrapCallback([&] { + CallbackInfo callbackInfo(env, info); + InstanceVoidMethodCallbackData* callbackData = + reinterpret_cast(callbackInfo.Data()); + callbackInfo.SetData(callbackData->data); + T* instance = T::Unwrap(callbackInfo.This().As()); + auto cb = callbackData->callback; + (instance->*cb)(callbackInfo); + return nullptr; + }); +} + +template +inline napi_value InstanceWrap::InstanceMethodCallbackWrapper( + napi_env env, + napi_callback_info info) { + return details::WrapCallback([&] { + CallbackInfo callbackInfo(env, info); + InstanceMethodCallbackData* callbackData = + reinterpret_cast(callbackInfo.Data()); + callbackInfo.SetData(callbackData->data); + T* instance = T::Unwrap(callbackInfo.This().As()); + auto cb = callbackData->callback; + return (instance->*cb)(callbackInfo); + }); +} + +template +inline napi_value InstanceWrap::InstanceGetterCallbackWrapper( + napi_env env, + napi_callback_info info) { + return details::WrapCallback([&] { + CallbackInfo callbackInfo(env, info); + InstanceAccessorCallbackData* callbackData = + reinterpret_cast(callbackInfo.Data()); + callbackInfo.SetData(callbackData->data); + T* instance = T::Unwrap(callbackInfo.This().As()); + auto cb = callbackData->getterCallback; + return (instance->*cb)(callbackInfo); + }); +} + +template +inline napi_value InstanceWrap::InstanceSetterCallbackWrapper( + napi_env env, + napi_callback_info info) { + return details::WrapCallback([&] { + CallbackInfo callbackInfo(env, info); + InstanceAccessorCallbackData* callbackData = + reinterpret_cast(callbackInfo.Data()); + callbackInfo.SetData(callbackData->data); + T* instance = T::Unwrap(callbackInfo.This().As()); + auto cb = callbackData->setterCallback; + (instance->*cb)(callbackInfo, callbackInfo[0]); + return nullptr; + }); +} + +template +template ::InstanceVoidMethodCallback method> +inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { + return details::WrapCallback([&] { + const CallbackInfo cbInfo(env, info); + T* instance = T::Unwrap(cbInfo.This().As()); + (instance->*method)(cbInfo); + return nullptr; + }); +} + +template +template ::InstanceMethodCallback method> +inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { + return details::WrapCallback([&] { + const CallbackInfo cbInfo(env, info); + T* instance = T::Unwrap(cbInfo.This().As()); + return (instance->*method)(cbInfo); + }); +} + +template +template ::InstanceSetterCallback method> +inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { + return details::WrapCallback([&] { + const CallbackInfo cbInfo(env, info); + T* instance = T::Unwrap(cbInfo.This().As()); + (instance->*method)(cbInfo, cbInfo[0]); + return nullptr; + }); +} + //////////////////////////////////////////////////////////////////////////////// // ObjectWrap class //////////////////////////////////////////////////////////////////////////////// @@ -3250,19 +3560,19 @@ ObjectWrap::DefineClass(Napi::Env env, napi_property_descriptor* prop = &props[index]; if (prop->method == T::StaticMethodCallbackWrapper) { status = CreateFunction(env, - utf8name, - prop->method, - static_cast(prop->data), + utf8name, + prop->method, + static_cast(prop->data), &(prop->value)); NAPI_THROW_IF_FAILED(env, status, Function()); prop->method = nullptr; prop->data = nullptr; } else if (prop->method == T::StaticVoidMethodCallbackWrapper) { status = CreateFunction(env, - utf8name, - prop->method, - static_cast(prop->data), - &(prop->value)); + utf8name, + prop->method, + static_cast(prop->data), + &(prop->value)); NAPI_THROW_IF_FAILED(env, status, Function()); prop->method = nullptr; prop->data = nullptr; @@ -3329,384 +3639,192 @@ inline Function ObjectWrap::DefineClass( } template -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - const char* utf8name, - StaticVoidMethodCallback method, - napi_property_attributes attributes, - void* data) { - StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.method = T::StaticVoidMethodCallbackWrapper; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - const char* utf8name, - StaticMethodCallback method, - napi_property_attributes attributes, - void* data) { - StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.method = T::StaticMethodCallbackWrapper; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - Symbol name, - StaticVoidMethodCallback method, - napi_property_attributes attributes, - void* data) { - StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.method = T::StaticVoidMethodCallbackWrapper; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - Symbol name, - StaticMethodCallback method, - napi_property_attributes attributes, - void* data) { - StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.method = T::StaticMethodCallbackWrapper; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticVoidMethodCallback method> -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - const char* utf8name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.method = &ObjectWrap::WrappedMethod; - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticVoidMethodCallback method> -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - Symbol name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.method = &ObjectWrap::WrappedMethod; - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticMethodCallback method> -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - const char* utf8name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.method = &ObjectWrap::WrappedMethod; - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticMethodCallback method> -inline ClassPropertyDescriptor ObjectWrap::StaticMethod( - Symbol name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.method = &ObjectWrap::WrappedMethod; - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( - const char* utf8name, - StaticGetterCallback getter, - StaticSetterCallback setter, - napi_property_attributes attributes, - void* data) { - StaticAccessorCallbackData* callbackData = - new StaticAccessorCallbackData({ getter, setter, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr; - desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( - Symbol name, - StaticGetterCallback getter, - StaticSetterCallback setter, - napi_property_attributes attributes, - void* data) { - StaticAccessorCallbackData* callbackData = - new StaticAccessorCallbackData({ getter, setter, data }); - - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr; - desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr; - desc.data = callbackData; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticGetterCallback getter, - typename ObjectWrap::StaticSetterCallback setter> -inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( - const char* utf8name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.getter = This::WrapStaticGetter(This::StaticGetterTag()); - desc.setter = This::WrapStaticSetter(This::StaticSetterTag()); - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -template ::StaticGetterCallback getter, - typename ObjectWrap::StaticSetterCallback setter> -inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( - Symbol name, - napi_property_attributes attributes, - void* data) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.getter = This::WrapStaticGetter(This::StaticGetterTag()); - desc.setter = This::WrapStaticSetter(This::StaticSetterTag()); - desc.data = data; - desc.attributes = static_cast(attributes | napi_static); - return desc; -} - -template -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( const char* utf8name, - InstanceVoidMethodCallback method, + StaticVoidMethodCallback method, napi_property_attributes attributes, void* data) { - InstanceVoidMethodCallbackData* callbackData = - new InstanceVoidMethodCallbackData({ method, data}); + StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.utf8name = utf8name; - desc.method = T::InstanceVoidMethodCallbackWrapper; + desc.method = T::StaticVoidMethodCallbackWrapper; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( const char* utf8name, - InstanceMethodCallback method, + StaticMethodCallback method, napi_property_attributes attributes, void* data) { - InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data }); + StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.utf8name = utf8name; - desc.method = T::InstanceMethodCallbackWrapper; + desc.method = T::StaticMethodCallbackWrapper; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( Symbol name, - InstanceVoidMethodCallback method, + StaticVoidMethodCallback method, napi_property_attributes attributes, void* data) { - InstanceVoidMethodCallbackData* callbackData = - new InstanceVoidMethodCallbackData({ method, data}); + StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.name = name; - desc.method = T::InstanceVoidMethodCallbackWrapper; + desc.method = T::StaticVoidMethodCallbackWrapper; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( Symbol name, - InstanceMethodCallback method, + StaticMethodCallback method, napi_property_attributes attributes, void* data) { - InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data }); + StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.name = name; - desc.method = T::InstanceMethodCallbackWrapper; + desc.method = T::StaticMethodCallbackWrapper; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceVoidMethodCallback method> -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +template ::StaticVoidMethodCallback method> +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( const char* utf8name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); desc.utf8name = utf8name; - desc.method = &InstanceWrap::WrappedMethod; + desc.method = &ObjectWrap::WrappedMethod; desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceMethodCallback method> -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( - const char* utf8name, +template ::StaticVoidMethodCallback method> +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( + Symbol name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.method = &InstanceWrap::WrappedMethod; + desc.name = name; + desc.method = &ObjectWrap::WrappedMethod; desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceVoidMethodCallback method> -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( - Symbol name, +template ::StaticMethodCallback method> +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( + const char* utf8name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.method = &InstanceWrap::WrappedMethod; + desc.utf8name = utf8name; + desc.method = &ObjectWrap::WrappedMethod; desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceMethodCallback method> -inline ClassPropertyDescriptor InstanceWrap::InstanceMethod( +template ::StaticMethodCallback method> +inline ClassPropertyDescriptor ObjectWrap::StaticMethod( Symbol name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); desc.name = name; - desc.method = &InstanceWrap::WrappedMethod; + desc.method = &ObjectWrap::WrappedMethod; desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( +inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( const char* utf8name, - InstanceGetterCallback getter, - InstanceSetterCallback setter, + StaticGetterCallback getter, + StaticSetterCallback setter, napi_property_attributes attributes, void* data) { - InstanceAccessorCallbackData* callbackData = - new InstanceAccessorCallbackData({ getter, setter, data }); + StaticAccessorCallbackData* callbackData = + new StaticAccessorCallbackData({ getter, setter, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.utf8name = utf8name; - desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr; - desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr; + desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr; + desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( +inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( Symbol name, - InstanceGetterCallback getter, - InstanceSetterCallback setter, + StaticGetterCallback getter, + StaticSetterCallback setter, napi_property_attributes attributes, void* data) { - InstanceAccessorCallbackData* callbackData = - new InstanceAccessorCallbackData({ getter, setter, data }); + StaticAccessorCallbackData* callbackData = + new StaticAccessorCallbackData({ getter, setter, data }); napi_property_descriptor desc = napi_property_descriptor(); desc.name = name; - desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr; - desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr; + desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr; + desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr; desc.data = callbackData; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceGetterCallback getter, - typename InstanceWrap::InstanceSetterCallback setter> -inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( +template ::StaticGetterCallback getter, + typename ObjectWrap::StaticSetterCallback setter> +inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( const char* utf8name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); desc.utf8name = utf8name; - desc.getter = This::WrapGetter(This::GetterTag()); - desc.setter = This::WrapSetter(This::SetterTag()); + desc.getter = This::WrapStaticGetter(This::StaticGetterTag()); + desc.setter = This::WrapStaticSetter(This::StaticSetterTag()); desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } template -template ::InstanceGetterCallback getter, - typename InstanceWrap::InstanceSetterCallback setter> -inline ClassPropertyDescriptor InstanceWrap::InstanceAccessor( +template ::StaticGetterCallback getter, + typename ObjectWrap::StaticSetterCallback setter> +inline ClassPropertyDescriptor ObjectWrap::StaticAccessor( Symbol name, napi_property_attributes attributes, void* data) { napi_property_descriptor desc = napi_property_descriptor(); desc.name = name; - desc.getter = This::WrapGetter(This::GetterTag()); - desc.setter = This::WrapSetter(This::SetterTag()); + desc.getter = This::WrapStaticGetter(This::StaticGetterTag()); + desc.setter = This::WrapStaticSetter(This::StaticSetterTag()); desc.data = data; - desc.attributes = attributes; + desc.attributes = static_cast(attributes | napi_static); return desc; } @@ -3730,30 +3848,6 @@ inline ClassPropertyDescriptor ObjectWrap::StaticValue(Symbol name, return desc; } -template -inline ClassPropertyDescriptor InstanceWrap::InstanceValue( - const char* utf8name, - Napi::Value value, - napi_property_attributes attributes) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.utf8name = utf8name; - desc.value = value; - desc.attributes = attributes; - return desc; -} - -template -inline ClassPropertyDescriptor InstanceWrap::InstanceValue( - Symbol name, - Napi::Value value, - napi_property_attributes attributes) { - napi_property_descriptor desc = napi_property_descriptor(); - desc.name = name; - desc.value = value; - desc.attributes = attributes; - return desc; -} - template inline void ObjectWrap::Finalize(Napi::Env /*env*/) {} @@ -3846,68 +3940,6 @@ inline napi_value ObjectWrap::StaticSetterCallbackWrapper( }); } -template -inline napi_value InstanceWrap::InstanceVoidMethodCallbackWrapper( - napi_env env, - napi_callback_info info) { - return details::WrapCallback([&] { - CallbackInfo callbackInfo(env, info); - InstanceVoidMethodCallbackData* callbackData = - reinterpret_cast(callbackInfo.Data()); - callbackInfo.SetData(callbackData->data); - T* instance = T::Unwrap(callbackInfo.This().As()); - auto cb = callbackData->callback; - (instance->*cb)(callbackInfo); - return nullptr; - }); -} - -template -inline napi_value InstanceWrap::InstanceMethodCallbackWrapper( - napi_env env, - napi_callback_info info) { - return details::WrapCallback([&] { - CallbackInfo callbackInfo(env, info); - InstanceMethodCallbackData* callbackData = - reinterpret_cast(callbackInfo.Data()); - callbackInfo.SetData(callbackData->data); - T* instance = T::Unwrap(callbackInfo.This().As()); - auto cb = callbackData->callback; - return (instance->*cb)(callbackInfo); - }); -} - -template -inline napi_value InstanceWrap::InstanceGetterCallbackWrapper( - napi_env env, - napi_callback_info info) { - return details::WrapCallback([&] { - CallbackInfo callbackInfo(env, info); - InstanceAccessorCallbackData* callbackData = - reinterpret_cast(callbackInfo.Data()); - callbackInfo.SetData(callbackData->data); - T* instance = T::Unwrap(callbackInfo.This().As()); - auto cb = callbackData->getterCallback; - return (instance->*cb)(callbackInfo); - }); -} - -template -inline napi_value InstanceWrap::InstanceSetterCallbackWrapper( - napi_env env, - napi_callback_info info) { - return details::WrapCallback([&] { - CallbackInfo callbackInfo(env, info); - InstanceAccessorCallbackData* callbackData = - reinterpret_cast(callbackInfo.Data()); - callbackInfo.SetData(callbackData->data); - T* instance = T::Unwrap(callbackInfo.This().As()); - auto cb = callbackData->setterCallback; - (instance->*cb)(callbackInfo, callbackInfo[0]); - return nullptr; - }); -} - template inline void ObjectWrap::FinalizeCallback(napi_env env, void* data, void* /*hint*/) { T* instance = static_cast(data); @@ -3932,27 +3964,6 @@ inline napi_value ObjectWrap::WrappedMethod(napi_env env, napi_callback_info }); } -template -template ::InstanceVoidMethodCallback method> -inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { - return details::WrapCallback([&] { - const CallbackInfo cbInfo(env, info); - T* instance = T::Unwrap(cbInfo.This().As()); - (instance->*method)(cbInfo); - return nullptr; - }); -} - -template -template ::InstanceMethodCallback method> -inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { - return details::WrapCallback([&] { - const CallbackInfo cbInfo(env, info); - T* instance = T::Unwrap(cbInfo.This().As()); - return (instance->*method)(cbInfo); - }); -} - template template ::StaticSetterCallback method> inline napi_value ObjectWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { @@ -3963,17 +3974,6 @@ inline napi_value ObjectWrap::WrappedMethod(napi_env env, napi_callback_info }); } -template -template ::InstanceSetterCallback method> -inline napi_value InstanceWrap::WrappedMethod(napi_env env, napi_callback_info info) noexcept { - return details::WrapCallback([&] { - const CallbackInfo cbInfo(env, info); - T* instance = T::Unwrap(cbInfo.This().As()); - (instance->*method)(cbInfo, cbInfo[0]); - return nullptr; - }); -} - //////////////////////////////////////////////////////////////////////////////// // HandleScope class //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index 7c0ec5c46..b4b7c8eff 100644 --- a/napi.h +++ b/napi.h @@ -1910,8 +1910,8 @@ namespace Napi { template static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept; - template struct StaticGetterTag {}; - template struct StaticSetterTag {}; + template struct StaticGetterTag {}; + template struct StaticSetterTag {}; template static napi_callback WrapStaticGetter(StaticGetterTag) noexcept { return &This::WrappedMethod; }