Skip to content

Commit ee71532

Browse files
[objective_c] Make autoReleasePool return callback value (dart-lang#3033)
1 parent 4c03068 commit ee71532

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎pkgs/objective_c/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 9.3.0
2+
- `autoReleasePool` now returns the value produced by its callback.
3+
14
## 9.2.5
25
- Fix a [bug](https://github.com/dart-lang/native/issues/3011) by adding
36
minimum OS version flags to the build script.

‎pkgs/objective_c/lib/src/autorelease.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ import 'runtime_bindings_generated.dart';
3535
/// here (the [Future] it returns will not be awaited). Objective-C autorelease
3636
/// pools form a strict stack, and allowing async execution gaps inside the pool
3737
/// scope could easily break this nesting, so async functions are not supported.
38-
void autoReleasePool(void Function() function) {
38+
T autoReleasePool<T>(T Function() function) {
3939
final pool = autoreleasePoolPush();
4040
try {
41-
function();
41+
return function();
4242
} finally {
4343
autoreleasePoolPop(pool);
4444
}

‎pkgs/objective_c/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: objective_c
66
description: 'A library to access Objective C from Flutter that acts as a support library for package:ffigen.'
7-
version: 9.2.5
7+
version: 9.3.0
88
repository: https://github.com/dart-lang/native/tree/main/pkgs/objective_c
99
issue_tracker: https://github.com/dart-lang/native/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aobjective_c
1010

‎pkgs/objective_c/test/autorelease_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,25 @@ void main() {
5656

5757
expect(objectRetainCount(pointer), 0);
5858
});
59+
60+
test('returns callback value', () async {
61+
late Pointer<ObjCObjectImpl> pointer;
62+
63+
final returnedPointer = autoReleasePool(() {
64+
final object = NSObject();
65+
pointer = object.ref.retainAndAutorelease();
66+
return pointer;
67+
});
68+
69+
// Returned value should be exactly what the callback returned
70+
expect(returnedPointer, same(pointer));
71+
72+
doGC();
73+
await Future<void>.delayed(Duration.zero);
74+
doGC();
75+
76+
// Object should be released once the pool is popped
77+
expect(objectRetainCount(pointer), 0);
78+
});
5979
});
6080
}

0 commit comments

Comments
 (0)