Skip to content

Commit 630d88b

Browse files
committed
statically check CallJs; add no-Finalizer overload
1 parent 561b968 commit 630d88b

3 files changed

Lines changed: 51 additions & 23 deletions

File tree

napi-inl.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ struct ThreadSafeFinalize {
196196
FinalizerDataType* data;
197197
Finalizer callback;
198198
};
199+
200+
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
201+
typename std::enable_if<call != nullptr>::type
202+
static inline CallJsWrapper(napi_env env, napi_value jsCallback, void *context, void *data) {
203+
call(env, Function(env, jsCallback), static_cast<ContextType *>(context),
204+
static_cast<DataType *>(data));
205+
}
206+
207+
template <typename ContextType, typename DataType, typename CallJs, CallJs call>
208+
typename std::enable_if<call == nullptr>::type
209+
static inline CallJsWrapper(napi_env env, napi_value jsCallback, void * /*context*/,
210+
void * /*data*/) {
211+
if (jsCallback != nullptr) {
212+
Function(env, jsCallback).Call(0, nullptr);
213+
}
214+
}
199215
#endif
200216

201217
template <typename Getter, typename Setter>
@@ -4260,6 +4276,20 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
42604276
// ThreadSafeFunctionEx<ContextType,DataType,CallJs> class
42614277
////////////////////////////////////////////////////////////////////////////////
42624278

4279+
// static
4280+
template <typename ContextType, typename DataType,
4281+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4282+
template <typename ResourceString>
4283+
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4284+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4285+
napi_env env, const Function &callback, const Object &resource,
4286+
ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount,
4287+
ContextType *context) {
4288+
return New(
4289+
env, callback, resource, resourceName, maxQueueSize, initialThreadCount,
4290+
context, [](Env, void *, ContextType *) {}, static_cast<void *>(nullptr));
4291+
}
4292+
42634293
// static
42644294
template <typename ContextType, typename DataType,
42654295
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
@@ -4397,19 +4427,13 @@ ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
43974427
return tsfn;
43984428
}
43994429

4430+
// static
44004431
template <typename ContextType, typename DataType,
44014432
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
44024433
void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::CallJsInternal(
44034434
napi_env env, napi_value jsCallback, void *context, void *data) {
4404-
4405-
if (CallJs == nullptr) {
4406-
if (jsCallback != nullptr) {
4407-
Function(env, jsCallback).Call(0, nullptr);
4408-
}
4409-
} else {
4410-
CallJs(env, Function(env, jsCallback), static_cast<ContextType *>(context),
4411-
static_cast<DataType *>(data));
4412-
}
4435+
details::CallJsWrapper<ContextType, DataType, decltype(CallJs), CallJs>(
4436+
env, jsCallback, context, data);
44134437
}
44144438

44154439
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,17 @@ namespace Napi {
20412041
public:
20422042

20432043
// This API may only be called from the main thread.
2044-
template <typename ResourceString, typename Finalizer, typename FinalizerDataType>
2044+
template <typename ResourceString>
2045+
static ThreadSafeFunctionEx<ContextType, DataType, CallJs> New(napi_env env,
2046+
const Function& callback,
2047+
const Object& resource,
2048+
ResourceString resourceName,
2049+
size_t maxQueueSize,
2050+
size_t initialThreadCount,
2051+
ContextType* context = nullptr);
2052+
2053+
// This API may only be called from the main thread.
2054+
template <typename ResourceString, typename Finalizer, typename FinalizerDataType = void>
20452055
static ThreadSafeFunctionEx<ContextType, DataType, CallJs> New(napi_env env,
20462056
const Function& callback,
20472057
const Object& resource,
@@ -2050,7 +2060,7 @@ namespace Napi {
20502060
size_t initialThreadCount,
20512061
ContextType* context,
20522062
Finalizer finalizeCallback,
2053-
FinalizerDataType* data);
2063+
FinalizerDataType* data = nullptr);
20542064

20552065
ThreadSafeFunctionEx<ContextType, DataType, CallJs>();
20562066
ThreadSafeFunctionEx<ContextType, DataType, CallJs>(napi_threadsafe_function tsFunctionValue);

test/threadsafe_function_ex/simple.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,12 @@ TSFNWrap::TSFNWrap(const CallbackInfo &info)
4343
: ObjectWrap<TSFNWrap>(info),
4444
_deferred(Promise::Deferred::New(info.Env())) {
4545

46-
_tsfn = TSFN::New(
47-
info.Env(), // napi_env env,
48-
Function(), // const Function& callback,
49-
Value(), // const Object& resource,
50-
"Test", // ResourceString resourceName,
51-
1, // size_t maxQueueSize,
52-
1, // size_t initialThreadCount,
53-
static_cast<void *>(nullptr), // ContextType* context,
54-
[this](Napi::Env env, // Finalizer finalizeCallback,
55-
void * /*data*/,
56-
void * /*ctx*/) { _deferred.Resolve(env.Undefined()); },
57-
static_cast<void *>(nullptr) // FinalizerDataType* data,
46+
_tsfn = TSFN::New(info.Env(), // napi_env env,
47+
Function(), // const Function& callback,
48+
Value(), // const Object& resource,
49+
"Test", // ResourceString resourceName,
50+
1, // size_t maxQueueSize,
51+
1 // size_t initialThreadCount
5852
);
5953
}
6054
} // namespace

0 commit comments

Comments
 (0)