1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <cstdlib>
#include "napi.h"
#include "test_helper.h"
#if (NAPI_VERSION > 3)
using namespace Napi;
namespace {
struct TestContext {
TestContext(Promise::Deferred&& deferred)
: deferred(std::move(deferred)), callData(nullptr){};
napi_threadsafe_function tsfn;
Promise::Deferred deferred;
double* callData;
~TestContext() {
if (callData != nullptr) delete callData;
};
};
void FinalizeCB(napi_env env, void* /*finalizeData */, void* context) {
TestContext* testContext = static_cast<TestContext*>(context);
if (testContext->callData != nullptr) {
testContext->deferred.Resolve(Number::New(env, *testContext->callData));
} else {
testContext->deferred.Resolve(Napi::Env(env).Undefined());
}
delete testContext;
}
void CallJSWithData(napi_env env,
napi_value /* callback */,
void* context,
void* data) {
TestContext* testContext = static_cast<TestContext*>(context);
testContext->callData = static_cast<double*>(data);
napi_status status =
napi_release_threadsafe_function(testContext->tsfn, napi_tsfn_release);
NAPI_THROW_IF_FAILED_VOID(env, status);
}
void CallJSNoData(napi_env env,
napi_value /* callback */,
void* context,
void* /*data*/) {
TestContext* testContext = static_cast<TestContext*>(context);
testContext->callData = nullptr;
napi_status status =
napi_release_threadsafe_function(testContext->tsfn, napi_tsfn_release);
NAPI_THROW_IF_FAILED_VOID(env, status);
}
static Value TestCall(const CallbackInfo& info) {
Napi::Env env = info.Env();
bool isBlocking = false;
bool hasData = false;
if (info.Length() > 0) {
Object opts = info[0].As<Object>();
bool hasProperty = MaybeUnwrap(opts.Has("blocking"));
if (hasProperty) {
isBlocking = MaybeUnwrap(MaybeUnwrap(opts.Get("blocking")).ToBoolean());
}
hasProperty = MaybeUnwrap(opts.Has("data"));
if (hasProperty) {
hasData = MaybeUnwrap(MaybeUnwrap(opts.Get("data")).ToBoolean());
}
}
// Allow optional callback passed from JS. Useful for testing.
Function cb = Function::New(env, [](const CallbackInfo& /*info*/) {});
TestContext* testContext = new TestContext(Napi::Promise::Deferred(env));
napi_status status =
napi_create_threadsafe_function(env,
cb,
Object::New(env),
String::New(env, "Test"),
0,
1,
nullptr, /*finalize data*/
FinalizeCB,
testContext,
hasData ? CallJSWithData : CallJSNoData,
&testContext->tsfn);
NAPI_THROW_IF_FAILED(env, status, Value());
ThreadSafeFunction wrapped = ThreadSafeFunction(testContext->tsfn);
// Test the four napi_threadsafe_function direct-accessing calls
if (isBlocking) {
if (hasData) {
wrapped.BlockingCall(static_cast<void*>(new double(std::rand())));
} else {
wrapped.BlockingCall(static_cast<void*>(nullptr));
}
} else {
if (hasData) {
wrapped.NonBlockingCall(static_cast<void*>(new double(std::rand())));
} else {
wrapped.NonBlockingCall(static_cast<void*>(nullptr));
}
}
return testContext->deferred.Promise();
}
} // namespace
Object InitThreadSafeFunctionExistingTsfn(Env env) {
Object exports = Object::New(env);
exports["testCall"] = Function::New(env, TestCall);
return exports;
}
#endif
|