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