Skip to content

Commit c881168

Browse files
KevinEadymhdawson
authored andcommitted
tsfn: add error checking on GetContext (#583)
PR-URL: #583 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 24d75dd commit c881168

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

napi-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4118,7 +4118,8 @@ inline napi_status ThreadSafeFunction::Abort() {
41184118
inline ThreadSafeFunction::ConvertibleContext
41194119
ThreadSafeFunction::GetContext() const {
41204120
void* context;
4121-
napi_get_threadsafe_function_context(_tsfn, &context);
4121+
napi_status status = napi_get_threadsafe_function_context(_tsfn, &context);
4122+
NAPI_FATAL_IF_FAILED(status, "ThreadSafeFunction::GetContext", "napi_get_threadsafe_function_context");
41224123
return ConvertibleContext({ context });
41234124
}
41244125

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Object InitObjectDeprecated(Env env);
4040
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
4141
Object InitPromise(Env env);
4242
#if (NAPI_VERSION > 3)
43+
Object InitThreadSafeFunctionCtx(Env env);
4344
Object InitThreadSafeFunctionExistingTsfn(Env env);
4445
Object InitThreadSafeFunctionPtr(Env env);
4546
Object InitThreadSafeFunctionSum(Env env);
@@ -92,6 +93,7 @@ Object Init(Env env, Object exports) {
9293
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
9394
exports.Set("promise", InitPromise(env));
9495
#if (NAPI_VERSION > 3)
96+
exports.Set("threadsafe_function_ctx", InitThreadSafeFunctionCtx(env));
9597
exports.Set("threadsafe_function_existing_tsfn", InitThreadSafeFunctionExistingTsfn(env));
9698
exports.Set("threadsafe_function_ptr", InitThreadSafeFunctionPtr(env));
9799
exports.Set("threadsafe_function_sum", InitThreadSafeFunctionSum(env));

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'object/object.cc',
3737
'object/set_property.cc',
3838
'promise.cc',
39+
'threadsafe_function/threadsafe_function_ctx.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let testModules = [
3939
'object/object_deprecated',
4040
'object/set_property',
4141
'promise',
42+
'threadsafe_function/threadsafe_function_ctx',
4243
'threadsafe_function/threadsafe_function_existing_tsfn',
4344
'threadsafe_function/threadsafe_function_ptr',
4445
'threadsafe_function/threadsafe_function_sum',
@@ -69,6 +70,7 @@ if (napiVersion < 3) {
6970

7071
if (napiVersion < 4) {
7172
testModules.splice(testModules.indexOf('asyncprogressworker'), 1);
73+
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ctx'), 1);
7274
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_existing_tsfn'), 1);
7375
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_ptr'), 1);
7476
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function_sum'), 1);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "napi.h"
2+
3+
#if (NAPI_VERSION > 3)
4+
5+
using namespace Napi;
6+
7+
namespace {
8+
9+
class TSFNWrap : public ObjectWrap<TSFNWrap> {
10+
public:
11+
static Object Init(Napi::Env env, Object exports);
12+
TSFNWrap(const CallbackInfo &info);
13+
14+
Napi::Value GetContext(const CallbackInfo & /*info*/) {
15+
Reference<Napi::Value> *ctx = _tsfn.GetContext();
16+
return ctx->Value();
17+
};
18+
19+
Napi::Value Release(const CallbackInfo &info) {
20+
Napi::Env env = info.Env();
21+
_deferred = std::unique_ptr<Promise::Deferred>(new Promise::Deferred(env));
22+
_tsfn.Release();
23+
return _deferred->Promise();
24+
};
25+
26+
private:
27+
ThreadSafeFunction _tsfn;
28+
std::unique_ptr<Promise::Deferred> _deferred;
29+
};
30+
31+
Object TSFNWrap::Init(Napi::Env env, Object exports) {
32+
Function func =
33+
DefineClass(env, "TSFNWrap",
34+
{InstanceMethod("getContext", &TSFNWrap::GetContext),
35+
InstanceMethod("release", &TSFNWrap::Release)});
36+
37+
exports.Set("TSFNWrap", func);
38+
return exports;
39+
}
40+
41+
TSFNWrap::TSFNWrap(const CallbackInfo &info) : ObjectWrap<TSFNWrap>(info) {
42+
Napi::Env env = info.Env();
43+
44+
Reference<Napi::Value> *_ctx = new Reference<Napi::Value>;
45+
*_ctx = Persistent(info[0]);
46+
47+
_tsfn = ThreadSafeFunction::New(
48+
info.Env(), Function::New(env, [](const CallbackInfo & /*info*/) {}),
49+
Object::New(env), "Test", 1, 1, _ctx,
50+
[this](Napi::Env env, Reference<Napi::Value> *ctx) {
51+
_deferred->Resolve(env.Undefined());
52+
ctx->Reset();
53+
delete ctx;
54+
});
55+
}
56+
57+
} // namespace
58+
59+
Object InitThreadSafeFunctionCtx(Env env) {
60+
return TSFNWrap::Init(env, Object::New(env));
61+
}
62+
63+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const buildType = process.config.target_defaults.default_configuration;
5+
6+
module.exports = Promise.all[
7+
test(require(`../build/${buildType}/binding.node`)),
8+
test(require(`../build/${buildType}/binding_noexcept.node`))
9+
];
10+
11+
async function test(binding) {
12+
const ctx = { };
13+
const tsfn = new binding.threadsafe_function_ctx.TSFNWrap(ctx);
14+
assert(tsfn.getContext() === ctx);
15+
await tsfn.release();
16+
}

0 commit comments

Comments
 (0)