@@ -50,29 +50,24 @@ DynamicLibrary dlopenPlatformSpecific(String name) {
50
50
return DynamicLibrary .open ('$_dylibPrefix $name $_dylibExtension ' );
51
51
}
52
52
53
- class NativeLibrary {
54
- late final FnRunnerType callFunctionOnSameThread;
55
- late final FnRunnerType callFunctionOnNewThreadBlocking;
56
- late final FnRunnerType callFunctionOnNewThreadNonBlocking;
57
- late final TwoIntFnType callTwoIntFunction;
58
- late final FnSleepType sleep;
59
-
60
- NativeLibrary (DynamicLibrary ffiTestFunctions) {
61
- callFunctionOnNewThreadNonBlocking = ffiTestFunctions
62
- .lookupFunction <FnRunnerNativeType , FnRunnerType >(
63
- "CallFunctionOnNewThreadNonBlocking" ,
64
- );
65
- callFunctionOnNewThreadBlocking = ffiTestFunctions
66
- .lookupFunction <FnRunnerNativeType , FnRunnerType >(
67
- "CallFunctionOnNewThreadBlocking" ,
68
- );
69
- callTwoIntFunction = ffiTestFunctions
70
- .lookupFunction <TwoIntFnNativeType , TwoIntFnType >("CallTwoIntFunction" );
71
- sleep = ffiTestFunctions.lookupFunction <FnSleepNativeType , FnSleepType >(
72
- "SleepFor" ,
53
+ DynamicLibrary get ffiTestFunctions =>
54
+ dlopenPlatformSpecific ("ffi_test_functions" );
55
+
56
+ FnRunnerType get callFunctionOnNewThreadNonBlocking =>
57
+ ffiTestFunctions.lookupFunction <FnRunnerNativeType , FnRunnerType >(
58
+ "CallFunctionOnNewThreadNonBlocking" ,
73
59
);
74
- }
75
- }
60
+
61
+ FnRunnerType get callFunctionOnNewThreadBlocking =>
62
+ ffiTestFunctions.lookupFunction <FnRunnerNativeType , FnRunnerType >(
63
+ "CallFunctionOnNewThreadBlocking" ,
64
+ );
65
+
66
+ TwoIntFnType get callTwoIntFunction => ffiTestFunctions
67
+ .lookupFunction <TwoIntFnNativeType , TwoIntFnType >("CallTwoIntFunction" );
68
+
69
+ FnSleepType get sleep =>
70
+ ffiTestFunctions.lookupFunction <FnSleepNativeType , FnSleepType >("SleepFor" );
76
71
77
72
@pragma ('vm:shared' )
78
73
late Mutex mutexCondvar;
@@ -88,16 +83,14 @@ const int sleepForMs = 1000;
88
83
89
84
void simpleFunction (int a, int b) {
90
85
result += (a * b);
91
- final ffiTestFunctions = dlopenPlatformSpecific ("ffi_test_functions" );
92
- final lib = NativeLibrary (ffiTestFunctions);
93
- lib.sleep (sleepForMs);
86
+ sleep (sleepForMs);
94
87
mutexCondvar.runLocked (() {
95
88
resultIsReady = true ;
96
89
conditionVariable.notify ();
97
90
});
98
91
}
99
92
100
- Future <void > testNativeCallableHelloWorld (NativeLibrary lib ) async {
93
+ Future <void > testNativeCallableHelloWorld () async {
101
94
mutexCondvar = Mutex ();
102
95
conditionVariable = ConditionVariable ();
103
96
final callback = NativeCallable <CallbackNativeType >.isolateGroupBound (
@@ -106,7 +99,7 @@ Future<void> testNativeCallableHelloWorld(NativeLibrary lib) async {
106
99
107
100
result = 42 ;
108
101
resultIsReady = false ;
109
- lib. callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
102
+ callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
110
103
111
104
mutexCondvar.runLocked (() {
112
105
while (! resultIsReady) {
@@ -118,7 +111,7 @@ Future<void> testNativeCallableHelloWorld(NativeLibrary lib) async {
118
111
Expect .equals (42 + (1001 * 123 ), result);
119
112
120
113
resultIsReady = false ;
121
- lib. callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
114
+ callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
122
115
mutexCondvar.runLocked (() {
123
116
while (! resultIsReady) {
124
117
conditionVariable.wait (mutexCondvar, 10 * sleepForMs);
@@ -134,7 +127,7 @@ void simpleFunctionThatThrows(int a, int b) {
134
127
throw 'hello, world' ;
135
128
}
136
129
137
- Future <void > testNativeCallableThrows (NativeLibrary lib ) async {
130
+ Future <void > testNativeCallableThrows () async {
138
131
mutexCondvar = Mutex ();
139
132
conditionVariable = ConditionVariable ();
140
133
final callback = NativeCallable <CallbackNativeType >.isolateGroupBound (
@@ -147,7 +140,7 @@ Future<void> testNativeCallableThrows(NativeLibrary lib) async {
147
140
// race between invoking the callback and closing it few lines down below.
148
141
// So the main thing this test checks is condition variable timeout,
149
142
// which is still valuable.
150
- lib. callFunctionOnNewThreadBlocking (1001 , callback.nativeFunction);
143
+ callFunctionOnNewThreadBlocking (1001 , callback.nativeFunction);
151
144
152
145
mutexCondvar.runLocked (() {
153
146
// Just have short one second sleep - the condition variable is not
@@ -158,15 +151,32 @@ Future<void> testNativeCallableThrows(NativeLibrary lib) async {
158
151
callback.close ();
159
152
}
160
153
161
- Future <void > testNativeCallableHelloWorldClosure (NativeLibrary lib) async {
154
+ @pragma ('vm:shared' )
155
+ SendPort ? sp;
156
+
157
+ Future <void > testFailToCaptureReceivePort () async {
158
+ final rp = ReceivePort ();
159
+ Expect .throws (
160
+ () {
161
+ NativeCallable <CallbackNativeType >.isolateGroupBound ((int a, int b) {
162
+ sp = rp.sendPort;
163
+ });
164
+ },
165
+ (e) =>
166
+ e is ArgumentError && e.toString ().contains ('Only trivially-immutable' ),
167
+ );
168
+ rp.close ();
169
+ }
170
+
171
+ Future <void > testNativeCallableHelloWorldClosure () async {
162
172
mutexCondvar = Mutex ();
163
173
conditionVariable = ConditionVariable ();
164
174
final callback = NativeCallable <CallbackNativeType >.isolateGroupBound ((
165
175
int a,
166
176
int b,
167
177
) {
168
178
result += (a * b);
169
- lib. sleep (sleepForMs);
179
+ sleep (sleepForMs);
170
180
mutexCondvar.runLocked (() {
171
181
resultIsReady = true ;
172
182
conditionVariable.notify ();
@@ -175,7 +185,7 @@ Future<void> testNativeCallableHelloWorldClosure(NativeLibrary lib) async {
175
185
176
186
result = 42 ;
177
187
resultIsReady = false ;
178
- lib. callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
188
+ callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
179
189
180
190
mutexCondvar.runLocked (() {
181
191
while (! resultIsReady) {
@@ -186,7 +196,7 @@ Future<void> testNativeCallableHelloWorldClosure(NativeLibrary lib) async {
186
196
Expect .equals (42 + (1001 * 123 ), result);
187
197
188
198
resultIsReady = false ;
189
- lib. callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
199
+ callFunctionOnNewThreadNonBlocking (1001 , callback.nativeFunction);
190
200
mutexCondvar.runLocked (() {
191
201
while (! resultIsReady) {
192
202
conditionVariable.wait (mutexCondvar);
@@ -196,7 +206,7 @@ Future<void> testNativeCallableHelloWorldClosure(NativeLibrary lib) async {
196
206
callback.close ();
197
207
}
198
208
199
- void testNativeCallableSync (NativeLibrary lib ) {
209
+ void testNativeCallableSync () {
200
210
final callback =
201
211
NativeCallable <CallbackReturningIntNativeType >.isolateGroupBound ((
202
212
int a,
@@ -205,14 +215,11 @@ void testNativeCallableSync(NativeLibrary lib) {
205
215
return a + b;
206
216
}, exceptionalReturn: 1111 );
207
217
208
- Expect .equals (
209
- 1234 ,
210
- lib.callTwoIntFunction (callback.nativeFunction, 1000 , 234 ),
211
- );
218
+ Expect .equals (1234 , callTwoIntFunction (callback.nativeFunction, 1000 , 234 ));
212
219
callback.close ();
213
220
}
214
221
215
- void testNativeCallableSyncThrows (NativeLibrary lib ) {
222
+ void testNativeCallableSyncThrows () {
216
223
final callback =
217
224
NativeCallable <CallbackReturningIntNativeType >.isolateGroupBound (
218
225
(int a, int b) {
@@ -222,16 +229,13 @@ void testNativeCallableSyncThrows(NativeLibrary lib) {
222
229
exceptionalReturn: 1111 ,
223
230
);
224
231
225
- Expect .equals (
226
- 1111 ,
227
- lib.callTwoIntFunction (callback.nativeFunction, 1000 , 234 ),
228
- );
232
+ Expect .equals (1111 , callTwoIntFunction (callback.nativeFunction, 1000 , 234 ));
229
233
callback.close ();
230
234
}
231
235
232
236
int isolateVar = 10 ;
233
237
234
- void testNativeCallableAccessNonSharedVar (NativeLibrary lib ) {
238
+ void testNativeCallableAccessNonSharedVar () {
235
239
final callback =
236
240
NativeCallable <CallbackReturningIntNativeType >.isolateGroupBound ((
237
241
int a,
@@ -241,10 +245,7 @@ void testNativeCallableAccessNonSharedVar(NativeLibrary lib) {
241
245
}, exceptionalReturn: 1111 );
242
246
243
247
isolateVar = 42 ;
244
- Expect .equals (
245
- 1111 ,
246
- lib.callTwoIntFunction (callback.nativeFunction, 1000 , 234 ),
247
- );
248
+ Expect .equals (1111 , callTwoIntFunction (callback.nativeFunction, 1000 , 234 ));
248
249
callback.close ();
249
250
}
250
251
@@ -304,14 +305,13 @@ Future<void> testKeepIsolateAliveFalse() async {
304
305
main (args, message) async {
305
306
asyncStart ();
306
307
// Simple tests.
307
- final ffiTestFunctions = dlopenPlatformSpecific ("ffi_test_functions" );
308
- final lib = NativeLibrary (ffiTestFunctions);
309
- await testNativeCallableHelloWorld (lib);
310
- await testNativeCallableThrows (lib);
311
- await testNativeCallableHelloWorldClosure (lib);
312
- testNativeCallableSync (lib);
313
- testNativeCallableSyncThrows (lib);
314
- testNativeCallableAccessNonSharedVar (lib);
308
+ await testNativeCallableHelloWorld ();
309
+ await testNativeCallableThrows ();
310
+ await testFailToCaptureReceivePort ();
311
+ await testNativeCallableHelloWorldClosure ();
312
+ testNativeCallableSync ();
313
+ testNativeCallableSyncThrows ();
314
+ testNativeCallableAccessNonSharedVar ();
315
315
await testKeepIsolateAliveTrue ();
316
316
await testKeepIsolateAliveFalse ();
317
317
asyncEnd ();
0 commit comments