Skip to content

Commit

Permalink
[jni] Upgrade package:jni's ffigen (#1954)
Browse files Browse the repository at this point in the history
Co-authored-by: Levi Lesches <[email protected]>
  • Loading branch information
HosseinYousefi and Levi-Lesches authored Jan 30, 2025
1 parent fbeafe0 commit cf5b631
Show file tree
Hide file tree
Showing 16 changed files with 957 additions and 800 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/ffigen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ jobs:
with:
channel: 'stable'
- name: Install dependencies
run: flutter pub get
run: flutter pub get && flutter pub get --directory="../jni"
- name: Install libclang-14-dev
run: sudo apt-get install libclang-14-dev
- name: Build test dylib and bindings
run: dart test/setup.dart
- name: Run VM tests
run: dart test
- name: Generate package:jni bindings
run: dart run tool/generate_ffi_bindings.dart
working-directory: pkgs/jni/

# Keep in sync with ffigen_weekly.yaml:test-mac-arm64
test-mac:
Expand All @@ -79,11 +82,18 @@ jobs:
with:
channel: 'stable'
- name: Install dependencies
run: flutter pub get && flutter pub get --directory="../objective_c"
run: flutter pub get && flutter pub get --directory="../objective_c" && flutter pub get --directory="../jni"
- name: Install clang-format
uses: ConorMacBride/install-package@3e7ad059e07782ee54fa35f827df52aae0626f30
with:
brew: clang-format
- name: Build test dylib and bindings
run: dart test/setup.dart
- name: Run VM tests and collect coverage
run: dart run coverage:test_with_coverage --scope-output=ffigen --scope-output=objective_c
- name: Generate package:jni bindings
run: dart run tool/generate_ffi_bindings.dart
working-directory: pkgs/jni/
- name: Upload coverage
uses: coverallsapp/github-action@cfd0633edbd2411b532b808ba7a8b5e04f76d2c8
with:
Expand Down Expand Up @@ -128,11 +138,14 @@ jobs:
with:
channel: 'stable'
- name: Install dependencies
run: flutter pub get
run: flutter pub get && flutter pub get --directory="../jni"
- name: Build test dylib and bindings
run: dart test/setup.dart
- name: Run VM tests
run: dart test
- name: Generate package:jni bindings
run: dart run tool/generate_ffi_bindings.dart
working-directory: pkgs/jni/

# Sanity check the latest `flutter create --template plugin_ffi`.
# This will break if we change the Flutter template or the generated code.
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/ffigen_weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ jobs:
with:
channel: 'stable'
- name: Install dependencies
run: flutter pub get && flutter pub get --directory="../objective_c"
run: flutter pub get && flutter pub get --directory="../objective_c" && flutter pub get --directory="../jni"
- name: Install clang-format
uses: ConorMacBride/install-package@3e7ad059e07782ee54fa35f827df52aae0626f30
with:
brew: clang-format
- name: Build test dylib and bindings
run: dart test/setup.dart --main-thread-dispatcher
- name: Run VM tests
run: flutter test
- name: Generate package:jni bindings
run: dart run tool/generate_ffi_bindings.dart
working-directory: pkgs/jni/
5 changes: 5 additions & 0 deletions pkgs/jni/lib/src/accessors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ extension JniResultMethods on JniResult {
return value.l;
}

JObjectRefType get referenceType {
check();
return JObjectRefType.fromValue(value.i);
}

JReference get reference {
final pointer = objectPointer;
return pointer == nullptr ? jNullReference : JGlobalReference(pointer);
Expand Down
19 changes: 9 additions & 10 deletions pkgs/jni/lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ final class DoubleReleaseError extends StateError {
DoubleReleaseError() : super('Double release error');
}

/// Represents JNI errors that might be returned by methods like
/// `JNI_CreateJavaVM`.
/// Represents JNI errors that might be returned by methods like `CreateJavaVM`.
sealed class JniError extends Error {
static const _errors = {
JniErrorCode.JNI_ERR: JniGenericError.new,
JniErrorCode.JNI_EDETACHED: JniThreadDetachedError.new,
JniErrorCode.JNI_EVERSION: JniVersionError.new,
JniErrorCode.JNI_ENOMEM: JniOutOfMemoryError.new,
JniErrorCode.JNI_EEXIST: JniVmExistsError.new,
JniErrorCode.JNI_EINVAL: JniArgumentError.new,
JniErrorCode.ERR: JniGenericError.new,
JniErrorCode.EDETACHED: JniThreadDetachedError.new,
JniErrorCode.EVERSION: JniVersionError.new,
JniErrorCode.ENOMEM: JniOutOfMemoryError.new,
JniErrorCode.EEXIST: JniVmExistsError.new,
JniErrorCode.EINVAL: JniArgumentError.new,
};

final String message;

JniError(this.message);

factory JniError.of(int status) {
factory JniError.of(JniErrorCode status) {
if (!_errors.containsKey(status)) {
status = JniErrorCode.JNI_ERR;
status = JniErrorCode.ERR;
}
return _errors[status]!();
}
Expand Down
14 changes: 7 additions & 7 deletions pkgs/jni/lib/src/jni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract final class Jni {
List<String> jvmOptions = const [],
List<String> classPath = const [],
bool ignoreUnrecognized = false,
int jniVersion = JniVersions.JNI_VERSION_1_6,
JniVersions jniVersion = JniVersions.VERSION_1_6,
}) {
final status = spawnIfNotExists(
dylibDir: dylibDir,
Expand All @@ -109,7 +109,7 @@ abstract final class Jni {
List<String> jvmOptions = const [],
List<String> classPath = const [],
bool ignoreUnrecognized = false,
int jniVersion = JniVersions.JNI_VERSION_1_6,
JniVersions jniVersion = JniVersions.VERSION_1_6,
}) =>
using((arena) {
_dylibDir = dylibDir ?? _dylibDir;
Expand All @@ -122,9 +122,9 @@ abstract final class Jni {
allocator: arena,
);
final status = _bindings.SpawnJvm(jvmArgs);
if (status == JniErrorCode.JNI_OK) {
if (status == JniErrorCode.OK) {
return true;
} else if (status == DART_JNI_SINGLETON_EXISTS) {
} else if (status == JniErrorCode.SINGLETON_EXISTS) {
return false;
} else {
throw JniError.of(status);
Expand All @@ -136,7 +136,7 @@ abstract final class Jni {
List<String> classPath = const [],
String? dylibPath,
bool ignoreUnrecognized = false,
int version = JniVersions.JNI_VERSION_1_6,
JniVersions version = JniVersions.VERSION_1_6,
required Allocator allocator,
}) {
final args = allocator<JavaVMInitArgs>();
Expand All @@ -163,7 +163,7 @@ abstract final class Jni {
args.ref.nOptions = count;
}
args.ref.ignoreUnrecognized = ignoreUnrecognized ? 1 : 0;
args.ref.version = version;
args.ref.version = version.value;
return args;
}

Expand Down Expand Up @@ -288,7 +288,7 @@ extension ProtectedJniExtensions on Jni {
static Dart_FinalizableHandle newJObjectFinalizableHandle(
Object object,
Pointer<Void> reference,
int refType,
JObjectRefType refType,
) {
ensureInitialized();
return Jni._bindings
Expand Down
Loading

0 comments on commit cf5b631

Please sign in to comment.