Skip to content

Commit 3b24e74

Browse files
committed
src: consolidate duplicated tsfnex code
1 parent c20685b commit 3b24e74

File tree

2 files changed

+18
-151
lines changed

2 files changed

+18
-151
lines changed

napi-inl.h

Lines changed: 12 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ static inline CallJsWrapper(napi_env env, napi_value jsCallback, void * /*contex
213213
Function(env, jsCallback).Call(0, nullptr);
214214
}
215215
}
216+
217+
template <typename CallbackType>
218+
typename ThreadSafeFunctionEx<>::DefaultFunctionType
219+
DefaultCallbackWrapper(
220+
napi_env env, CallbackType cb) {
221+
return ThreadSafeFunctionEx<>::DefaultFunctionFactory(env);
222+
}
223+
216224
#endif
217225

218226
template <typename Getter, typename Setter>
@@ -4342,28 +4350,6 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
43424350
return tsfn;
43434351
}
43444352

4345-
// static, with Callback [nullptr] Resource [missing] Finalizer [missing]
4346-
template <typename ContextType, typename DataType,
4347-
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4348-
template <typename ResourceString>
4349-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4350-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4351-
napi_env env, std::nullptr_t callback, ResourceString resourceName, size_t maxQueueSize,
4352-
size_t initialThreadCount, ContextType *context) {
4353-
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
4354-
4355-
napi_status status = napi_create_threadsafe_function(
4356-
env, nullptr, nullptr, String::From(env, resourceName), maxQueueSize,
4357-
initialThreadCount, nullptr, nullptr, context,
4358-
CallJsInternal, &tsfn._tsfn);
4359-
if (status != napi_ok) {
4360-
NAPI_THROW_IF_FAILED(env, status,
4361-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
4362-
}
4363-
4364-
return tsfn;
4365-
}
4366-
43674353
// static, with Callback [missing] Resource [passed] Finalizer [missing]
43684354
template <typename ContextType, typename DataType,
43694355
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
@@ -4386,28 +4372,6 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
43864372
return tsfn;
43874373
}
43884374

4389-
// static, with Callback [nullptr] Resource [passed] Finalizer [missing]
4390-
template <typename ContextType, typename DataType,
4391-
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4392-
template <typename ResourceString>
4393-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4394-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4395-
napi_env env, std::nullptr_t callback, const Object &resource, ResourceString resourceName,
4396-
size_t maxQueueSize, size_t initialThreadCount, ContextType *context) {
4397-
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
4398-
4399-
napi_status status = napi_create_threadsafe_function(
4400-
env, nullptr, resource, String::From(env, resourceName), maxQueueSize,
4401-
initialThreadCount, nullptr, nullptr, context, CallJsInternal,
4402-
&tsfn._tsfn);
4403-
if (status != napi_ok) {
4404-
NAPI_THROW_IF_FAILED(env, status,
4405-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
4406-
}
4407-
4408-
return tsfn;
4409-
}
4410-
44114375
// static, with Callback [missing] Resource [missing] Finalizer [passed]
44124376
template <typename ContextType, typename DataType,
44134377
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
@@ -4438,36 +4402,6 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
44384402
return tsfn;
44394403
}
44404404

4441-
// static, with Callback [nullptr] Resource [missing] Finalizer [passed]
4442-
template <typename ContextType, typename DataType,
4443-
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4444-
template <typename ResourceString, typename Finalizer,
4445-
typename FinalizerDataType>
4446-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4447-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4448-
napi_env env, std::nullptr_t callback, ResourceString resourceName, size_t maxQueueSize,
4449-
size_t initialThreadCount, ContextType *context, Finalizer finalizeCallback,
4450-
FinalizerDataType *data) {
4451-
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
4452-
4453-
auto *finalizeData = new details::ThreadSafeFinalize<ContextType, Finalizer,
4454-
FinalizerDataType>(
4455-
{data, finalizeCallback});
4456-
napi_status status = napi_create_threadsafe_function(
4457-
env, nullptr, nullptr, String::From(env, resourceName), maxQueueSize,
4458-
initialThreadCount, finalizeData,
4459-
details::ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>::
4460-
FinalizeFinalizeWrapperWithDataAndContext,
4461-
context, CallJsInternal, &tsfn._tsfn);
4462-
if (status != napi_ok) {
4463-
delete finalizeData;
4464-
NAPI_THROW_IF_FAILED(env, status,
4465-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
4466-
}
4467-
4468-
return tsfn;
4469-
}
4470-
44714405
// static, with Callback [missing] Resource [passed] Finalizer [passed]
44724406
template <typename ContextType, typename DataType,
44734407
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
@@ -4497,36 +4431,6 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
44974431

44984432
return tsfn;
44994433
}
4500-
4501-
// static, with Callback [nullptr] Resource [passed] Finalizer [passed]
4502-
template <typename ContextType, typename DataType,
4503-
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4504-
template <typename ResourceString, typename Finalizer,
4505-
typename FinalizerDataType>
4506-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4507-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4508-
napi_env env, std::nullptr_t callback, const Object &resource, ResourceString resourceName,
4509-
size_t maxQueueSize, size_t initialThreadCount, ContextType *context,
4510-
Finalizer finalizeCallback, FinalizerDataType *data) {
4511-
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
4512-
4513-
auto *finalizeData = new details::ThreadSafeFinalize<ContextType, Finalizer,
4514-
FinalizerDataType>(
4515-
{data, finalizeCallback});
4516-
napi_status status = napi_create_threadsafe_function(
4517-
env, nullptr, resource, String::From(env, resourceName), maxQueueSize,
4518-
initialThreadCount, finalizeData,
4519-
details::ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>::
4520-
FinalizeFinalizeWrapperWithDataAndContext,
4521-
context, CallJsInternal, &tsfn._tsfn);
4522-
if (status != napi_ok) {
4523-
delete finalizeData;
4524-
NAPI_THROW_IF_FAILED(env, status,
4525-
ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
4526-
}
4527-
4528-
return tsfn;
4529-
}
45304434
#endif
45314435

45324436
// static, with Callback [passed] Resource [missing] Finalizer [missing]
@@ -4607,11 +4511,11 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
46074511
// static, with: Callback [passed] Resource [passed] Finalizer [passed]
46084512
template <typename ContextType, typename DataType,
46094513
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4610-
template <typename ResourceString, typename Finalizer,
4514+
template <typename CallbackType, typename ResourceString, typename Finalizer,
46114515
typename FinalizerDataType>
46124516
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
46134517
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4614-
napi_env env, const Function &callback, const Object &resource,
4518+
napi_env env, CallbackType callback, const Object &resource,
46154519
ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount,
46164520
ContextType *context, Finalizer finalizeCallback, FinalizerDataType *data) {
46174521
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
@@ -4620,7 +4524,7 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
46204524
FinalizerDataType>(
46214525
{data, finalizeCallback});
46224526
napi_status status = napi_create_threadsafe_function(
4623-
env, callback, resource, String::From(env, resourceName), maxQueueSize,
4527+
env, details::DefaultCallbackWrapper<CallbackType>(env, callback), resource, String::From(env, resourceName), maxQueueSize,
46244528
initialThreadCount, finalizeData,
46254529
details::ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>::
46264530
FinalizeFinalizeWrapperWithDataAndContext,
@@ -4743,6 +4647,7 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::DefaultFunctionFactory(
47434647
#endif
47444648
}
47454649

4650+
47464651
////////////////////////////////////////////////////////////////////////////////
47474652
// ThreadSafeFunction class
47484653
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,21 +2049,21 @@ namespace Napi {
20492049
void (*CallJs)(Napi::Env, Napi::Function, ContextType *,
20502050
DataType *) = nullptr>
20512051
class ThreadSafeFunctionEx {
2052-
private:
2052+
2053+
public:
20532054
#if NAPI_VERSION > 4
20542055
using DefaultFunctionType = std::nullptr_t;
20552056
#else
20562057
using DefaultFunctionType = const Napi::Function;
20572058
#endif
2058-
2059-
public:
20602059
// This API may only be called from the main thread.
20612060
// Helper function that returns nullptr if running N-API 5+, otherwise a
20622061
// non-empty, no-op Function. This provides the ability to specify at
20632062
// compile-time a callback parameter to `New` that safely does no action
20642063
// when targeting _any_ N-API version.
20652064
static DefaultFunctionType DefaultFunctionFactory(Napi::Env env);
20662065

2066+
20672067
#if NAPI_VERSION > 4
20682068
// This API may only be called from the main thread.
20692069
// Creates a new threadsafe function with:
@@ -2073,14 +2073,6 @@ namespace Napi {
20732073
New(napi_env env, ResourceString resourceName, size_t maxQueueSize,
20742074
size_t initialThreadCount, ContextType *context = nullptr);
20752075

2076-
// This API may only be called from the main thread.
2077-
// Callback [nullptr] Resource [missing] Finalizer [missing]
2078-
template <typename ResourceString>
2079-
static ThreadSafeFunctionEx<ContextType, DataType, CallJs>
2080-
New(napi_env env, std::nullptr_t callback, ResourceString resourceName,
2081-
size_t maxQueueSize, size_t initialThreadCount,
2082-
ContextType *context = nullptr);
2083-
20842076
// This API may only be called from the main thread.
20852077
// Creates a new threadsafe function with:
20862078
// Callback [missing] Resource [passed] Finalizer [missing]
@@ -2090,15 +2082,6 @@ namespace Napi {
20902082
size_t maxQueueSize, size_t initialThreadCount,
20912083
ContextType *context = nullptr);
20922084

2093-
// This API may only be called from the main thread.
2094-
// Creates a new threadsafe function with:
2095-
// Callback [nullptr] Resource [passed] Finalizer [missing]
2096-
template <typename ResourceString>
2097-
static ThreadSafeFunctionEx<ContextType, DataType, CallJs>
2098-
New(napi_env env, std::nullptr_t callback, const Object &resource,
2099-
ResourceString resourceName, size_t maxQueueSize,
2100-
size_t initialThreadCount, ContextType *context = nullptr);
2101-
21022085
// This API may only be called from the main thread.
21032086
// Creates a new threadsafe function with:
21042087
// Callback [missing] Resource [missing] Finalizer [passed]
@@ -2109,16 +2092,6 @@ namespace Napi {
21092092
size_t initialThreadCount, ContextType *context,
21102093
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
21112094

2112-
// This API may only be called from the main thread.
2113-
// Creates a new threadsafe function with:
2114-
// Callback [nullptr] Resource [missing] Finalizer [passed]
2115-
template <typename ResourceString, typename Finalizer,
2116-
typename FinalizerDataType = void>
2117-
static ThreadSafeFunctionEx<ContextType, DataType, CallJs>
2118-
New(napi_env env, std::nullptr_t callback, ResourceString resourceName,
2119-
size_t maxQueueSize, size_t initialThreadCount, ContextType *context,
2120-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2121-
21222095
// This API may only be called from the main thread.
21232096
// Creates a new threadsafe function with:
21242097
// Callback [missing] Resource [passed] Finalizer [passed]
@@ -2128,17 +2101,6 @@ namespace Napi {
21282101
New(napi_env env, const Object &resource, ResourceString resourceName,
21292102
size_t maxQueueSize, size_t initialThreadCount, ContextType *context,
21302103
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2131-
2132-
// This API may only be called from the main thread.
2133-
// Creates a new threadsafe function with:
2134-
// Callback [nullptr] Resource [passed] Finalizer [passed]
2135-
template <typename ResourceString, typename Finalizer,
2136-
typename FinalizerDataType = void>
2137-
static ThreadSafeFunctionEx<ContextType, DataType, CallJs>
2138-
New(napi_env env, std::nullptr_t callback, const Object &resource,
2139-
ResourceString resourceName, size_t maxQueueSize,
2140-
size_t initialThreadCount, ContextType *context,
2141-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
21422104
#endif
21432105

21442106
// This API may only be called from the main thread.
@@ -2172,10 +2134,10 @@ namespace Napi {
21722134
// This API may only be called from the main thread.
21732135
// Creates a new threadsafe function with:
21742136
// Callback [passed] Resource [passed] Finalizer [passed]
2175-
template <typename ResourceString, typename Finalizer,
2176-
typename FinalizerDataType = void>
2137+
template <typename CallbackType, typename ResourceString, typename Finalizer,
2138+
typename FinalizerDataType>
21772139
static ThreadSafeFunctionEx<ContextType, DataType, CallJs>
2178-
New(napi_env env, const Function &callback, const Object &resource,
2140+
New(napi_env env, CallbackType callback, const Object &resource,
21792141
ResourceString resourceName, size_t maxQueueSize,
21802142
size_t initialThreadCount, ContextType *context,
21812143
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);

0 commit comments

Comments
 (0)