Skip to content

Commit cf5b631

Browse files
[jni] Upgrade package:jni's ffigen (#1954)
Co-authored-by: Levi Lesches <[email protected]>
1 parent fbeafe0 commit cf5b631

16 files changed

+957
-800
lines changed

.github/workflows/ffigen.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ jobs:
5858
with:
5959
channel: 'stable'
6060
- name: Install dependencies
61-
run: flutter pub get
61+
run: flutter pub get && flutter pub get --directory="../jni"
6262
- name: Install libclang-14-dev
6363
run: sudo apt-get install libclang-14-dev
6464
- name: Build test dylib and bindings
6565
run: dart test/setup.dart
6666
- name: Run VM tests
6767
run: dart test
68+
- name: Generate package:jni bindings
69+
run: dart run tool/generate_ffi_bindings.dart
70+
working-directory: pkgs/jni/
6871

6972
# Keep in sync with ffigen_weekly.yaml:test-mac-arm64
7073
test-mac:
@@ -79,11 +82,18 @@ jobs:
7982
with:
8083
channel: 'stable'
8184
- name: Install dependencies
82-
run: flutter pub get && flutter pub get --directory="../objective_c"
85+
run: flutter pub get && flutter pub get --directory="../objective_c" && flutter pub get --directory="../jni"
86+
- name: Install clang-format
87+
uses: ConorMacBride/install-package@3e7ad059e07782ee54fa35f827df52aae0626f30
88+
with:
89+
brew: clang-format
8390
- name: Build test dylib and bindings
8491
run: dart test/setup.dart
8592
- name: Run VM tests and collect coverage
8693
run: dart run coverage:test_with_coverage --scope-output=ffigen --scope-output=objective_c
94+
- name: Generate package:jni bindings
95+
run: dart run tool/generate_ffi_bindings.dart
96+
working-directory: pkgs/jni/
8797
- name: Upload coverage
8898
uses: coverallsapp/github-action@cfd0633edbd2411b532b808ba7a8b5e04f76d2c8
8999
with:
@@ -128,11 +138,14 @@ jobs:
128138
with:
129139
channel: 'stable'
130140
- name: Install dependencies
131-
run: flutter pub get
141+
run: flutter pub get && flutter pub get --directory="../jni"
132142
- name: Build test dylib and bindings
133143
run: dart test/setup.dart
134144
- name: Run VM tests
135145
run: dart test
146+
- name: Generate package:jni bindings
147+
run: dart run tool/generate_ffi_bindings.dart
148+
working-directory: pkgs/jni/
136149

137150
# Sanity check the latest `flutter create --template plugin_ffi`.
138151
# This will break if we change the Flutter template or the generated code.

.github/workflows/ffigen_weekly.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ jobs:
3232
with:
3333
channel: 'stable'
3434
- name: Install dependencies
35-
run: flutter pub get && flutter pub get --directory="../objective_c"
35+
run: flutter pub get && flutter pub get --directory="../objective_c" && flutter pub get --directory="../jni"
36+
- name: Install clang-format
37+
uses: ConorMacBride/install-package@3e7ad059e07782ee54fa35f827df52aae0626f30
38+
with:
39+
brew: clang-format
3640
- name: Build test dylib and bindings
3741
run: dart test/setup.dart --main-thread-dispatcher
3842
- name: Run VM tests
3943
run: flutter test
44+
- name: Generate package:jni bindings
45+
run: dart run tool/generate_ffi_bindings.dart
46+
working-directory: pkgs/jni/

pkgs/jni/lib/src/accessors.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ extension JniResultMethods on JniResult {
6262
return value.l;
6363
}
6464

65+
JObjectRefType get referenceType {
66+
check();
67+
return JObjectRefType.fromValue(value.i);
68+
}
69+
6570
JReference get reference {
6671
final pointer = objectPointer;
6772
return pointer == nullptr ? jNullReference : JGlobalReference(pointer);

pkgs/jni/lib/src/errors.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@ final class DoubleReleaseError extends StateError {
2222
DoubleReleaseError() : super('Double release error');
2323
}
2424

25-
/// Represents JNI errors that might be returned by methods like
26-
/// `JNI_CreateJavaVM`.
25+
/// Represents JNI errors that might be returned by methods like `CreateJavaVM`.
2726
sealed class JniError extends Error {
2827
static const _errors = {
29-
JniErrorCode.JNI_ERR: JniGenericError.new,
30-
JniErrorCode.JNI_EDETACHED: JniThreadDetachedError.new,
31-
JniErrorCode.JNI_EVERSION: JniVersionError.new,
32-
JniErrorCode.JNI_ENOMEM: JniOutOfMemoryError.new,
33-
JniErrorCode.JNI_EEXIST: JniVmExistsError.new,
34-
JniErrorCode.JNI_EINVAL: JniArgumentError.new,
28+
JniErrorCode.ERR: JniGenericError.new,
29+
JniErrorCode.EDETACHED: JniThreadDetachedError.new,
30+
JniErrorCode.EVERSION: JniVersionError.new,
31+
JniErrorCode.ENOMEM: JniOutOfMemoryError.new,
32+
JniErrorCode.EEXIST: JniVmExistsError.new,
33+
JniErrorCode.EINVAL: JniArgumentError.new,
3534
};
3635

3736
final String message;
3837

3938
JniError(this.message);
4039

41-
factory JniError.of(int status) {
40+
factory JniError.of(JniErrorCode status) {
4241
if (!_errors.containsKey(status)) {
43-
status = JniErrorCode.JNI_ERR;
42+
status = JniErrorCode.ERR;
4443
}
4544
return _errors[status]!();
4645
}

pkgs/jni/lib/src/jni.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ abstract final class Jni {
8585
List<String> jvmOptions = const [],
8686
List<String> classPath = const [],
8787
bool ignoreUnrecognized = false,
88-
int jniVersion = JniVersions.JNI_VERSION_1_6,
88+
JniVersions jniVersion = JniVersions.VERSION_1_6,
8989
}) {
9090
final status = spawnIfNotExists(
9191
dylibDir: dylibDir,
@@ -109,7 +109,7 @@ abstract final class Jni {
109109
List<String> jvmOptions = const [],
110110
List<String> classPath = const [],
111111
bool ignoreUnrecognized = false,
112-
int jniVersion = JniVersions.JNI_VERSION_1_6,
112+
JniVersions jniVersion = JniVersions.VERSION_1_6,
113113
}) =>
114114
using((arena) {
115115
_dylibDir = dylibDir ?? _dylibDir;
@@ -122,9 +122,9 @@ abstract final class Jni {
122122
allocator: arena,
123123
);
124124
final status = _bindings.SpawnJvm(jvmArgs);
125-
if (status == JniErrorCode.JNI_OK) {
125+
if (status == JniErrorCode.OK) {
126126
return true;
127-
} else if (status == DART_JNI_SINGLETON_EXISTS) {
127+
} else if (status == JniErrorCode.SINGLETON_EXISTS) {
128128
return false;
129129
} else {
130130
throw JniError.of(status);
@@ -136,7 +136,7 @@ abstract final class Jni {
136136
List<String> classPath = const [],
137137
String? dylibPath,
138138
bool ignoreUnrecognized = false,
139-
int version = JniVersions.JNI_VERSION_1_6,
139+
JniVersions version = JniVersions.VERSION_1_6,
140140
required Allocator allocator,
141141
}) {
142142
final args = allocator<JavaVMInitArgs>();
@@ -163,7 +163,7 @@ abstract final class Jni {
163163
args.ref.nOptions = count;
164164
}
165165
args.ref.ignoreUnrecognized = ignoreUnrecognized ? 1 : 0;
166-
args.ref.version = version;
166+
args.ref.version = version.value;
167167
return args;
168168
}
169169

@@ -288,7 +288,7 @@ extension ProtectedJniExtensions on Jni {
288288
static Dart_FinalizableHandle newJObjectFinalizableHandle(
289289
Object object,
290290
Pointer<Void> reference,
291-
int refType,
291+
JObjectRefType refType,
292292
) {
293293
ensureInitialized();
294294
return Jni._bindings

0 commit comments

Comments
 (0)