Skip to content

Commit fc339c4

Browse files
committed
wip
1 parent 90ebd80 commit fc339c4

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

napi-inl.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,7 +4272,7 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
42724272
ContextType* context,
42734273
Finalizer finalizeCallback,
42744274
FinalizerDataType* data,
4275-
napi_threadsafe_function_call_js call_js_cb) {
4275+
ThreadSafeFunctionCallJS call_js_cb) {
42764276
return New(env, callback, resource, resourceName, maxQueueSize,
42774277
initialThreadCount, context, finalizeCallback, data,
42784278
details::ThreadSafeFinalize<ContextType, Finalizer,
@@ -4362,7 +4362,7 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
43624362
Finalizer finalizeCallback,
43634363
FinalizerDataType* data,
43644364
napi_finalize wrapper,
4365-
napi_threadsafe_function_call_js call_js_cb) {
4365+
ThreadSafeFunctionCallJS call_js_cb) {
43664366
static_assert(details::can_make_string<ResourceString>::value
43674367
|| std::is_convertible<ResourceString, napi_value>::value,
43684368
"Resource name should be convertible to the string type");
@@ -4372,7 +4372,15 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
43724372
FinalizerDataType>({ data, finalizeCallback });
43734373
napi_status status = napi_create_threadsafe_function(env, callback, resource,
43744374
Value::From(env, resourceName), maxQueueSize, initialThreadCount,
4375-
finalizeData, wrapper, context, call_js_cb, &tsfn._tsfn);
4375+
finalizeData, wrapper, context,
4376+
// [=](napi_env env, napi_value jsCallback, void* context, void* data) {
4377+
// if (env == nullptr && jsCallback == nullptr) {
4378+
// return;
4379+
// }
4380+
// call_js_cb( Napi::Env(env), Function(env, jsCallback), static_cast<ContextType*>(context), data);
4381+
// },
4382+
CallJS,
4383+
&tsfn._tsfn);
43764384
if (status != napi_ok) {
43774385
delete finalizeData;
43784386
NAPI_THROW_IF_FAILED(env, status, ThreadSafeFunctionEx<ContextType>());
@@ -4381,6 +4389,23 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
43814389
return tsfn;
43824390
}
43834391

4392+
template <typename ContextType>
4393+
inline void
4394+
ThreadSafeFunctionEx<ContextType>::CallJS(napi_env env, napi_value jsCallback,
4395+
void *context, void *data) {
4396+
if (env == nullptr && jsCallback == nullptr) {
4397+
return;
4398+
}
4399+
4400+
if (data != nullptr) {
4401+
auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
4402+
(*callbackWrapper)(env, Function(env, jsCallback));
4403+
delete callbackWrapper;
4404+
} else if (jsCallback != nullptr) {
4405+
Function(env, jsCallback).Call({});
4406+
}
4407+
}
4408+
43844409
////////////////////////////////////////////////////////////////////////////////
43854410
// ThreadSafeFunction class
43864411
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,9 +2037,13 @@ namespace Napi {
20372037

20382038
#if (NAPI_VERSION > 3)
20392039

2040-
template <typename ContextType>
2040+
2041+
template <typename ContextType, typename DataType>
20412042
class ThreadSafeFunctionEx {
20422043
public:
2044+
2045+
using ThreadSafeFunctionCallJS = std::function<void(Napi::Env, Napi::Function, ContextType *context, DataType *data)>;
2046+
20432047
// This API may only be called from the main thread.
20442048
template <typename ResourceString, typename Finalizer, typename FinalizerDataType>
20452049
static ThreadSafeFunctionEx<ContextType> New(napi_env env,
@@ -2051,7 +2055,7 @@ namespace Napi {
20512055
ContextType* context,
20522056
Finalizer finalizeCallback,
20532057
FinalizerDataType* data,
2054-
napi_threadsafe_function_call_js call_js_cb);
2058+
ThreadSafeFunctionCallJS call_js_cb);
20552059

20562060
ThreadSafeFunctionEx<ContextType>();
20572061
ThreadSafeFunctionEx<ContextType>(napi_threadsafe_function tsFunctionValue);
@@ -2062,14 +2066,12 @@ namespace Napi {
20622066
// napi_status BlockingCall() const;
20632067

20642068
// This API may be called from any thread.
2065-
template <typename DataType = void>
20662069
napi_status BlockingCall(DataType* data = nullptr) const;
20672070

20682071
// // This API may be called from any thread.
20692072
// napi_status NonBlockingCall() const;
20702073

20712074
// This API may be called from any thread.
2072-
template <typename DataType = void>
20732075
napi_status NonBlockingCall(DataType* data = nullptr) const;
20742076

20752077
// This API may only be called from the main thread.
@@ -2091,7 +2093,7 @@ namespace Napi {
20912093
ContextType* GetContext() const;
20922094

20932095
private:
2094-
using CallbackWrapper = std::function<void(Napi::Env, Napi::Function, ContextType* context)>;
2096+
// using CallbackWrapper = std::function<void(Napi::Env, Napi::Function, ContextType* context)>;
20952097

20962098
template <typename ResourceString,
20972099
typename Finalizer, typename FinalizerDataType>
@@ -2105,7 +2107,15 @@ namespace Napi {
21052107
Finalizer finalizeCallback,
21062108
FinalizerDataType* data,
21072109
napi_finalize wrapper,
2108-
napi_threadsafe_function_call_js call_js_cb);
2110+
ThreadSafeFunctionCallJS call_js_cb);
2111+
2112+
static void CallJS(napi_env env,
2113+
napi_value jsCallback,
2114+
void* context,
2115+
void* data);
2116+
2117+
protected:
2118+
void CallJS
21092119

21102120
napi_threadsafe_function _tsfn;
21112121
};

test/threadsafe_function/threadsafe_function_ex.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ TSFNWrap::TSFNWrap(const CallbackInfo &info)
7979
delete ctx;
8080
},
8181
static_cast<void*>(nullptr), // FinalizerDataType* data,
82-
[](napi_env, napi_value, void *context, void *data) { // call_js_cb
82+
[](Napi::Env, Napi::Value, TSFNContext *context, void *data) { // call_js_cb
8383
std::unique_ptr<CallJsData> callData(static_cast<CallJsData*>(data));
84-
callData->resolve(static_cast<TSFNContext*>(context));
84+
callData->resolve(context);
8585
});
8686
}
8787
} // namespace

0 commit comments

Comments
 (0)