1
+ #include < assert.h>
1
2
#include " napi.h"
2
3
3
4
#if (NAPI_VERSION > 3)
@@ -8,7 +9,7 @@ namespace {
8
9
9
10
class TSFNWrap : public ObjectWrap <TSFNWrap> {
10
11
public:
11
- static Object Init (Napi::Env env, Object exports );
12
+ static Function Init (Napi::Env env);
12
13
TSFNWrap (const CallbackInfo& info);
13
14
14
15
Napi::Value GetContext (const CallbackInfo& /* info*/ ) {
@@ -28,15 +29,13 @@ class TSFNWrap : public ObjectWrap<TSFNWrap> {
28
29
std::unique_ptr<Promise::Deferred> _deferred;
29
30
};
30
31
31
- Object TSFNWrap::Init (Napi::Env env, Object exports ) {
32
+ Function TSFNWrap::Init (Napi::Env env) {
32
33
Function func =
33
34
DefineClass (env,
34
35
" TSFNWrap" ,
35
36
{InstanceMethod (" getContext" , &TSFNWrap::GetContext),
36
37
InstanceMethod (" release" , &TSFNWrap::Release)});
37
-
38
- exports.Set (" TSFNWrap" , func);
39
- return exports;
38
+ return func;
40
39
}
41
40
42
41
TSFNWrap::TSFNWrap (const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
@@ -59,11 +58,98 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
59
58
delete ctx;
60
59
});
61
60
}
61
+ struct SimpleTestContext {
62
+ SimpleTestContext (int val) : _val(val) {}
63
+ int _val = -1 ;
64
+ };
65
+
66
+ void AssertGetContextFromVariousTSOverloads (const CallbackInfo& info) {
67
+ Env env = info.Env ();
68
+ Function emptyFunc;
69
+
70
+ SimpleTestContext* ctx = new SimpleTestContext (42 );
71
+ ThreadSafeFunction fn =
72
+ ThreadSafeFunction::New (env, emptyFunc, " testResource" , 1 , 1 , ctx);
73
+
74
+ assert (fn.GetContext () == ctx);
75
+ delete ctx;
76
+ fn.Release ();
77
+
78
+ fn = ThreadSafeFunction::New (env, emptyFunc, " testRes" , 1 , 1 , [](Env) {});
79
+ fn.Release ();
80
+
81
+ ctx = new SimpleTestContext (42 );
82
+ fn = ThreadSafeFunction::New (env,
83
+ emptyFunc,
84
+ Object::New (env),
85
+ " resStrObj" ,
86
+ 1 ,
87
+ 1 ,
88
+ ctx,
89
+ [](Env, SimpleTestContext*) {});
90
+ assert (fn.GetContext () == ctx);
91
+ delete ctx;
92
+ fn.Release ();
93
+
94
+ fn = ThreadSafeFunction::New (
95
+ env, emptyFunc, Object::New (env), " resStrObj" , 1 , 1 );
96
+ fn.Release ();
97
+
98
+ ctx = new SimpleTestContext (42 );
99
+ fn = ThreadSafeFunction::New (
100
+ env, emptyFunc, Object::New (env), " resStrObj" , 1 , 1 , ctx);
101
+ assert (fn.GetContext () == ctx);
102
+ delete ctx;
103
+ fn.Release ();
104
+
105
+ using FinalizerDataType = int ;
106
+ FinalizerDataType* finalizerData = new int (42 );
107
+ fn = ThreadSafeFunction::New (
108
+ env,
109
+ emptyFunc,
110
+ Object::New (env),
111
+ " resObject" ,
112
+ 1 ,
113
+ 1 ,
114
+ [](Env, FinalizerDataType* data) {
115
+ assert (*data == 42 );
116
+ delete data;
117
+ },
118
+ finalizerData);
119
+ fn.Release ();
120
+
121
+ ctx = new SimpleTestContext (42 );
122
+ FinalizerDataType* finalizerDataB = new int (42 );
123
+
124
+ fn = ThreadSafeFunction::New (
125
+ env,
126
+ emptyFunc,
127
+ Object::New (env),
128
+ " resObject" ,
129
+ 1 ,
130
+ 1 ,
131
+ ctx,
132
+ [](Env, FinalizerDataType* _data, SimpleTestContext* _ctx) {
133
+ assert (*_data == 42 );
134
+ assert (_ctx->_val == 42 );
135
+ delete _data;
136
+ delete _ctx;
137
+ },
138
+ finalizerDataB);
139
+ assert (fn.GetContext () == ctx);
140
+ fn.Release ();
141
+ }
62
142
63
143
} // namespace
64
144
65
145
Object InitThreadSafeFunctionCtx (Env env) {
66
- return TSFNWrap::Init (env, Object::New (env));
146
+ Object exports = Object::New (env);
147
+ Function tsfnWrap = TSFNWrap::Init (env);
148
+ exports.Set (" TSFNWrap" , tsfnWrap);
149
+ exports.Set (" AssertFnReturnCorrectCxt" ,
150
+ Function::New (env, AssertGetContextFromVariousTSOverloads));
151
+
152
+ return exports;
67
153
}
68
154
69
155
#endif
0 commit comments