Skip to content

Commit 333460e

Browse files
aamCommit Queue
authored andcommitted
[gardening] Fix httpIG samples - ensure only shareable vars are captured.
Fixes https://ci.chromium.org/p/dart/builders/ci.sandbox/vm-linux-release-x64/6047 Follow-up to 650ed9c. TEST=samples/ffi/httpIG/test/http_test Change-Id: I435f1be6dbcf17fb16fbd589a52ad1fcd38971c0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444102 Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent 13edb28 commit 333460e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

samples/ffi/httpIG/lib/http.dart

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import 'package:ffi/ffi.dart';
1212

1313
import 'dylib_utils.dart';
1414

15+
Function (Pointer<Utf8>) createGetSender(SendPort sendPort) {
16+
return (Pointer<Utf8> responsePointer) {
17+
final typedList = responsePointer.cast<Uint8>().asTypedList(
18+
responsePointer.length,
19+
);
20+
sendPort.send(utf8.decode(typedList));
21+
};
22+
}
23+
1524
// Runs a simple HTTP GET request using a native HTTP library that runs
1625
// the request on a background thread.
1726
Future<String> httpGet(String uri) async {
@@ -26,16 +35,8 @@ Future<String> httpGet(String uri) async {
2635
print('httpGet receiver get error $e $st');
2736
},
2837
);
29-
final sendPort = rp.sendPort;
30-
final callback = NativeCallable<HttpCallback>.isolateGroupBound((
31-
Pointer<Utf8> responsePointer,
32-
) {
33-
final typedList = responsePointer.cast<Uint8>().asTypedList(
34-
responsePointer.length,
35-
);
36-
final s = utf8.decode(typedList);
37-
sendPort.send(s);
38-
});
38+
final callback = NativeCallable<HttpCallback>.isolateGroupBound(
39+
createGetSender(rp.sendPort));
3940

4041
// Invoke the native HTTP API. Our example HTTP library runs our GET
4142
// request on a background thread, and calls the callback on that same
@@ -56,20 +57,22 @@ Future<String> httpGet(String uri) async {
5657
@pragma('vm:shared')
5758
late int counter;
5859

59-
// Start a HTTP server on a background thread.
60-
ReceivePort httpServe(void Function(String) onRequest) {
61-
counter = 0;
62-
final rp = ReceivePort();
63-
final callback = NativeCallable<HttpCallback>.isolateGroupBound((
64-
Pointer<Utf8> requestPointer,
65-
) {
60+
Function (Pointer<Utf8>) createServeSender(SendPort sendPort) {
61+
return (Pointer<Utf8> requestPointer) {
6662
counter++;
6763
final typedList = requestPointer.cast<Uint8>().asTypedList(
6864
requestPointer.length,
6965
);
70-
final s = utf8.decode(typedList);
71-
rp.sendPort.send(s);
72-
});
66+
sendPort.send(utf8.decode(typedList));
67+
};
68+
}
69+
70+
// Start a HTTP server on a background thread.
71+
ReceivePort httpServe(void Function(String) onRequest) {
72+
counter = 0;
73+
final rp = ReceivePort();
74+
final callback = NativeCallable<HttpCallback>.isolateGroupBound(
75+
createServeSender(rp.sendPort));
7376
rp.listen(
7477
(s) {
7578
print('httpServe counter: $counter');

0 commit comments

Comments
 (0)