1
+ #include < assert.h>
1
2
#include " napi.h"
2
3
3
4
#if (NAPI_VERSION > 3)
@@ -11,7 +12,7 @@ namespace {
11
12
12
13
class TSFNWrap : public ObjectWrap <TSFNWrap> {
13
14
public:
14
- static Object Init (Napi::Env env, Object exports );
15
+ static Function Init (Napi::Env env);
15
16
TSFNWrap (const CallbackInfo& info);
16
17
17
18
Napi::Value GetContext (const CallbackInfo& /* info*/ ) {
@@ -31,15 +32,14 @@ class TSFNWrap : public ObjectWrap<TSFNWrap> {
31
32
std::unique_ptr<Promise::Deferred> _deferred;
32
33
};
33
34
34
- Object TSFNWrap::Init (Napi::Env env, Object exports ) {
35
+ Function TSFNWrap::Init (Napi::Env env) {
35
36
Function func =
36
37
DefineClass (env,
37
38
" TSFNWrap" ,
38
39
{InstanceMethod (" getContext" , &TSFNWrap::GetContext),
39
40
InstanceMethod (" release" , &TSFNWrap::Release)});
40
41
41
- exports.Set (" TSFNWrap" , func);
42
- return exports;
42
+ return func;
43
43
}
44
44
45
45
TSFNWrap::TSFNWrap (const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
@@ -61,8 +61,60 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
61
61
62
62
} // namespace
63
63
64
+ struct SimpleTestContext {
65
+ SimpleTestContext (int val) : _val(val) {}
66
+ int _val = -1 ;
67
+ };
68
+
69
+ // A simple test to check that the context has been set successfully
70
+ void AssertGetContextFromTSFNNoFinalizerIsCorrect (const CallbackInfo& info) {
71
+ // Test the overload where we provide a resource name but no finalizer
72
+ using TSFN = TypedThreadSafeFunction<SimpleTestContext>;
73
+ SimpleTestContext* ctx = new SimpleTestContext (42 );
74
+ TSFN tsfn = TSFN::New (info.Env (), " testRes" , 1 , 1 , ctx);
75
+
76
+ assert (tsfn.GetContext () == ctx);
77
+ delete ctx;
78
+ tsfn.Release ();
79
+
80
+ // Test the other overload where we provide a async resource object, res name
81
+ // but no finalizer
82
+ ctx = new SimpleTestContext (52 );
83
+ tsfn = TSFN::New (
84
+ info.Env (), Object::New (info.Env ()), " testResourceObject" , 1 , 1 , ctx);
85
+
86
+ assert (tsfn.GetContext () == ctx);
87
+ delete ctx;
88
+ tsfn.Release ();
89
+
90
+ ctx = new SimpleTestContext (52 );
91
+ tsfn = TSFN::New (info.Env (),
92
+ " resStrings" ,
93
+ 1 ,
94
+ 1 ,
95
+ ctx,
96
+ [](Napi::Env, void *, SimpleTestContext*) {});
97
+
98
+ assert (tsfn.GetContext () == ctx);
99
+ delete ctx;
100
+ tsfn.Release ();
101
+
102
+ ctx = new SimpleTestContext (52 );
103
+ Function emptyFunc;
104
+ tsfn = TSFN::New (info.Env (), emptyFunc, " resString" , 1 , 1 , ctx);
105
+ assert (tsfn.GetContext () == ctx);
106
+ delete ctx;
107
+ tsfn.Release ();
108
+ }
109
+
64
110
Object InitTypedThreadSafeFunctionCtx (Env env) {
65
- return TSFNWrap::Init (env, Object::New (env));
111
+ Object exports = Object::New (env);
112
+ Function tsfnWrap = TSFNWrap::Init (env);
113
+
114
+ exports.Set (" TSFNWrap" , tsfnWrap);
115
+ exports.Set (" AssertTSFNReturnCorrectCxt" ,
116
+ Function::New (env, AssertGetContextFromTSFNNoFinalizerIsCorrect));
117
+ return exports;
66
118
}
67
119
68
120
#endif
0 commit comments