Skip to content

Commit 561b968

Browse files
committed
make CallJS template parameter
1 parent fd530b2 commit 561b968

10 files changed

Lines changed: 224 additions & 157 deletions

File tree

napi-inl.h

Lines changed: 103 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,164 +4257,161 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
42574257

42584258
#if (NAPI_VERSION > 3)
42594259
////////////////////////////////////////////////////////////////////////////////
4260-
// ThreadSafeFunctionEx<Context> class
4260+
// ThreadSafeFunctionEx<ContextType,DataType,CallJs> class
42614261
////////////////////////////////////////////////////////////////////////////////
42624262

42634263
// static
4264-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4265-
template <typename ResourceString, typename Finalizer, typename FinalizerDataType>
4266-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs> ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(napi_env env,
4267-
const Function& callback,
4268-
const Object& resource,
4269-
ResourceString resourceName,
4270-
size_t maxQueueSize,
4271-
size_t initialThreadCount,
4272-
ContextType* context,
4273-
Finalizer finalizeCallback,
4274-
FinalizerDataType* data) {
4275-
return New(env, callback, resource, resourceName, maxQueueSize,
4276-
initialThreadCount, context, finalizeCallback, data,
4277-
details::ThreadSafeFinalize<ContextType, Finalizer,
4278-
FinalizerDataType>::FinalizeFinalizeWrapperWithDataAndContext);
4279-
}
4280-
4281-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4282-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>::ThreadSafeFunctionEx()
4283-
: _tsfn() {
4284-
}
4285-
4286-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4287-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>::ThreadSafeFunctionEx(
4288-
napi_threadsafe_function tsfn)
4289-
: _tsfn(tsfn) {
4290-
}
4291-
4292-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4293-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>::operator napi_threadsafe_function() const {
4264+
template <typename ContextType, typename DataType,
4265+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4266+
template <typename ResourceString, typename Finalizer,
4267+
typename FinalizerDataType>
4268+
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4269+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4270+
napi_env env, const Function &callback, const Object &resource,
4271+
ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount,
4272+
ContextType *context, Finalizer finalizeCallback, FinalizerDataType *data) {
4273+
return New(
4274+
env, callback, resource, resourceName, maxQueueSize, initialThreadCount,
4275+
context, finalizeCallback, data,
4276+
details::ThreadSafeFinalize<ContextType, Finalizer, FinalizerDataType>::
4277+
FinalizeFinalizeWrapperWithDataAndContext);
4278+
}
4279+
4280+
template <typename ContextType, typename DataType,
4281+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4282+
inline ThreadSafeFunctionEx<ContextType, DataType,
4283+
CallJs>::ThreadSafeFunctionEx()
4284+
: _tsfn() {}
4285+
4286+
template <typename ContextType, typename DataType,
4287+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4288+
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>::
4289+
ThreadSafeFunctionEx(napi_threadsafe_function tsfn)
4290+
: _tsfn(tsfn) {}
4291+
4292+
template <typename ContextType, typename DataType,
4293+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4294+
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>::
4295+
operator napi_threadsafe_function() const {
42944296
return _tsfn;
42954297
}
42964298

4297-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4298-
inline napi_status ThreadSafeFunctionEx<ContextType, DataType, CallJs>::BlockingCall(
4299-
DataType* data) const {
4299+
template <typename ContextType, typename DataType,
4300+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4301+
inline napi_status
4302+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::BlockingCall(
4303+
DataType *data) const {
43004304
return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_blocking);
43014305
}
43024306

4303-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4304-
inline napi_status ThreadSafeFunctionEx<ContextType, DataType, CallJs>::NonBlockingCall(
4305-
DataType* data) const {
4307+
template <typename ContextType, typename DataType,
4308+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4309+
inline napi_status
4310+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::NonBlockingCall(
4311+
DataType *data) const {
43064312
return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_nonblocking);
43074313
}
43084314

4309-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4310-
inline void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Ref(napi_env env) const {
4315+
template <typename ContextType, typename DataType,
4316+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4317+
inline void
4318+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Ref(napi_env env) const {
43114319
if (_tsfn != nullptr) {
43124320
napi_status status = napi_ref_threadsafe_function(env, _tsfn);
43134321
NAPI_THROW_IF_FAILED_VOID(env, status);
43144322
}
43154323
}
43164324

4317-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4318-
inline void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Unref(napi_env env) const {
4325+
template <typename ContextType, typename DataType,
4326+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4327+
inline void
4328+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Unref(napi_env env) const {
43194329
if (_tsfn != nullptr) {
43204330
napi_status status = napi_unref_threadsafe_function(env, _tsfn);
43214331
NAPI_THROW_IF_FAILED_VOID(env, status);
43224332
}
43234333
}
43244334

4325-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4326-
inline napi_status ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Acquire() const {
4335+
template <typename ContextType, typename DataType,
4336+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4337+
inline napi_status
4338+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Acquire() const {
43274339
return napi_acquire_threadsafe_function(_tsfn);
43284340
}
43294341

4330-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4331-
inline napi_status ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Release() {
4342+
template <typename ContextType, typename DataType,
4343+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4344+
inline napi_status
4345+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Release() {
43324346
return napi_release_threadsafe_function(_tsfn, napi_tsfn_release);
43334347
}
43344348

4335-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4336-
inline napi_status ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Abort() {
4349+
template <typename ContextType, typename DataType,
4350+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4351+
inline napi_status
4352+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::Abort() {
43374353
return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort);
43384354
}
43394355

4340-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4341-
inline ContextType* ThreadSafeFunctionEx<ContextType, DataType, CallJs>::GetContext() const {
4342-
void* context;
4356+
template <typename ContextType, typename DataType,
4357+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4358+
inline ContextType *
4359+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::GetContext() const {
4360+
void *context;
43434361
napi_status status = napi_get_threadsafe_function_context(_tsfn, &context);
4344-
NAPI_FATAL_IF_FAILED(status, "ThreadSafeFunctionEx::GetContext", "napi_get_threadsafe_function_context");
4345-
return static_cast<ContextType*>(context);
4362+
NAPI_FATAL_IF_FAILED(status, "ThreadSafeFunctionEx::GetContext",
4363+
"napi_get_threadsafe_function_context");
4364+
return static_cast<ContextType *>(context);
43464365
}
43474366

43484367
// static
4349-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4350-
template <typename ResourceString, typename Finalizer, typename FinalizerDataType>
4351-
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs> ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(napi_env env,
4352-
const Function& callback,
4353-
const Object& resource,
4354-
ResourceString resourceName,
4355-
size_t maxQueueSize,
4356-
size_t initialThreadCount,
4357-
ContextType* context,
4358-
Finalizer finalizeCallback,
4359-
FinalizerDataType* data,
4360-
napi_finalize wrapper) {
4361-
static_assert(details::can_make_string<ResourceString>::value
4362-
|| std::is_convertible<ResourceString, napi_value>::value,
4363-
"Resource name should be convertible to the string type");
4368+
template <typename ContextType, typename DataType,
4369+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4370+
template <typename ResourceString, typename Finalizer,
4371+
typename FinalizerDataType>
4372+
inline ThreadSafeFunctionEx<ContextType, DataType, CallJs>
4373+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>::New(
4374+
napi_env env, const Function &callback, const Object &resource,
4375+
ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount,
4376+
ContextType *context, Finalizer finalizeCallback, FinalizerDataType *data,
4377+
napi_finalize wrapper) {
4378+
static_assert(details::can_make_string<ResourceString>::value ||
4379+
std::is_convertible<ResourceString, napi_value>::value,
4380+
"Resource name should be convertible to the string type");
43644381

43654382
ThreadSafeFunctionEx<ContextType, DataType, CallJs> tsfn;
4366-
// details::ThreadSafeCallJs<ContextType, DataType, ThreadSafeFunctionCallJS> cb{call_js_cb};
43674383

4368-
auto* finalizeData = new details::ThreadSafeFinalize<ContextType, Finalizer,
4369-
FinalizerDataType>({ data, finalizeCallback });
4370-
napi_status status = napi_create_threadsafe_function(env, callback, resource,
4371-
Value::From(env, resourceName), maxQueueSize, initialThreadCount,
4372-
finalizeData, wrapper, context,
4373-
// [=](napi_env env, napi_value jsCallback, void* context, void* data) {
4374-
// if (env == nullptr && jsCallback == nullptr) {
4375-
// return;
4376-
// }
4377-
// call_js_cb( Napi::Env(env), Function(env, jsCallback), static_cast<ContextType*>(context), data);
4378-
// },
4379-
CallJsInternal,
4380-
&tsfn._tsfn);
4384+
auto *finalizeData = new details::ThreadSafeFinalize<ContextType, Finalizer,
4385+
FinalizerDataType>(
4386+
{data, finalizeCallback});
4387+
napi_status status = napi_create_threadsafe_function(
4388+
env, callback, resource, Value::From(env, resourceName), maxQueueSize,
4389+
initialThreadCount, finalizeData, wrapper, context, CallJsInternal,
4390+
&tsfn._tsfn);
43814391
if (status != napi_ok) {
43824392
delete finalizeData;
4383-
NAPI_THROW_IF_FAILED(env, status, ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
4393+
NAPI_THROW_IF_FAILED(env, status,
4394+
ThreadSafeFunctionEx<ContextType, DataType, CallJs>());
43844395
}
43854396

43864397
return tsfn;
43874398
}
43884399

4389-
template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4390-
void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::CallJsInternal(napi_env env, napi_value jsCallback,
4391-
void *context, void *data) {
4400+
template <typename ContextType, typename DataType,
4401+
void (*CallJs)(Napi::Env, Napi::Function, ContextType *, DataType *)>
4402+
void ThreadSafeFunctionEx<ContextType, DataType, CallJs>::CallJsInternal(
4403+
napi_env env, napi_value jsCallback, void *context, void *data) {
43924404

4393-
if (CallJs == nullptr && jsCallback != nullptr) {
4394-
Function(env, jsCallback).Call(0, nullptr);
4405+
if (CallJs == nullptr) {
4406+
if (jsCallback != nullptr) {
4407+
Function(env, jsCallback).Call(0, nullptr);
4408+
}
43954409
} else {
4396-
CallJs(env, Function(env, jsCallback), static_cast<ContextType*>(context), static_cast<DataType*>(data));
4410+
CallJs(env, Function(env, jsCallback), static_cast<ContextType *>(context),
4411+
static_cast<DataType *>(data));
43974412
}
43984413
}
43994414

4400-
4401-
// template <typename ContextType, typename DataType, void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*)>
4402-
// inline void
4403-
// ThreadSafeFunctionEx<ContextType, DataType, CallJs>::CallJS(napi_env env, napi_value jsCallback,
4404-
// void *context, void *data) {
4405-
// if (env == nullptr && jsCallback == nullptr) {
4406-
// return;
4407-
// }
4408-
4409-
// if (data != nullptr) {
4410-
// auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
4411-
// (*callbackWrapper)(env, Function(env, jsCallback));
4412-
// delete callbackWrapper;
4413-
// } else if (jsCallback != nullptr) {
4414-
// Function(env, jsCallback).Call({});
4415-
// }
4416-
// }
4417-
44184415
////////////////////////////////////////////////////////////////////////////////
44194416
// ThreadSafeFunction class
44204417
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,15 +2057,9 @@ namespace Napi {
20572057

20582058
operator napi_threadsafe_function() const;
20592059

2060-
// // This API may be called from any thread.
2061-
// napi_status BlockingCall() const;
2062-
20632060
// This API may be called from any thread.
20642061
napi_status BlockingCall(DataType* data = nullptr) const;
20652062

2066-
// // This API may be called from any thread.
2067-
// napi_status NonBlockingCall() const;
2068-
20692063
// This API may be called from any thread.
20702064
napi_status NonBlockingCall(DataType* data = nullptr) const;
20712065

test/binding.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ Object InitPromise(Env env);
4343
Object InitRunScript(Env env);
4444
#if (NAPI_VERSION > 3)
4545
Object InitThreadSafeFunctionCtx(Env env);
46-
Object InitThreadSafeFunctionEx(Env env);
4746
Object InitThreadSafeFunctionExistingTsfn(Env env);
4847
Object InitThreadSafeFunctionPtr(Env env);
4948
Object InitThreadSafeFunctionSum(Env env);
5049
Object InitThreadSafeFunctionUnref(Env env);
5150
Object InitThreadSafeFunction(Env env);
51+
Object InitThreadSafeFunctionExContext(Env env);
52+
Object InitThreadSafeFunctionExSimple(Env env);
5253
#endif
5354
Object InitTypedArray(Env env);
5455
Object InitObjectWrap(Env env);
@@ -101,12 +102,13 @@ Object Init(Env env, Object exports) {
101102
exports.Set("run_script", InitRunScript(env));
102103
#if (NAPI_VERSION > 3)
103104
exports.Set("threadsafe_function_ctx", InitThreadSafeFunctionCtx(env));
104-
exports.Set("threadsafe_function_ex", InitThreadSafeFunctionEx(env));
105105
exports.Set("threadsafe_function_existing_tsfn", InitThreadSafeFunctionExistingTsfn(env));
106106
exports.Set("threadsafe_function_ptr", InitThreadSafeFunctionPtr(env));
107107
exports.Set("threadsafe_function_sum", InitThreadSafeFunctionSum(env));
108108
exports.Set("threadsafe_function_unref", InitThreadSafeFunctionUnref(env));
109109
exports.Set("threadsafe_function", InitThreadSafeFunction(env));
110+
exports.Set("threadsafe_function_ex_context", InitThreadSafeFunctionExContext(env));
111+
exports.Set("threadsafe_function_ex_simple", InitThreadSafeFunctionExSimple(env));
110112
#endif
111113
exports.Set("typedarray", InitTypedArray(env));
112114
exports.Set("objectwrap", InitObjectWrap(env));

test/binding.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
'object/set_property.cc',
3535
'promise.cc',
3636
'run_script.cc',
37+
'threadsafe_function_ex/context.cc',
38+
'threadsafe_function_ex/simple.cc',
3739
'threadsafe_function/threadsafe_function_ctx.cc',
38-
'threadsafe_function/threadsafe_function_ex.cc',
3940
'threadsafe_function/threadsafe_function_existing_tsfn.cc',
4041
'threadsafe_function/threadsafe_function_ptr.cc',
4142
'threadsafe_function/threadsafe_function_sum.cc',

test/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ let testModules = [
4141
'object/set_property',
4242
'promise',
4343
'run_script',
44+
'threadsafe_function_ex/context',
45+
'threadsafe_function_ex/simple',
4446
'threadsafe_function/threadsafe_function_ctx',
45-
'threadsafe_function/threadsafe_function_ex',
4647
'threadsafe_function/threadsafe_function_existing_tsfn',
4748
'threadsafe_function/threadsafe_function_ptr',
4849
'threadsafe_function/threadsafe_function_sum',
@@ -77,12 +78,13 @@ if (napiVersion < 4) {
7778
testModules.splice(testModules.indexOf('asyncprogressqueueworker'), 1);
7879
testModules.splice(testModules.indexOf('asyncprogressworker'), 1);
7980
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ctx'), 1);
80-
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ex'), 1);
8181
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_existing_tsfn'), 1);
8282
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ptr'), 1);
8383
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_sum'), 1);
8484
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_unref'), 1);
8585
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function'), 1);
86+
testModules.splice(testModules.indexOf('threadsafe_function_ex/context'), 1);
87+
testModules.splice(testModules.indexOf('threadsafe_function_ex/simple'), 1);
8688
}
8789

8890
if (napiVersion < 5) {

test/threadsafe_function/threadsafe_function_ex.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)