-
-
Notifications
You must be signed in to change notification settings - Fork 277
fix(jni): dart to native type conversion #3372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
buenaflor
wants to merge
11
commits into
main
Choose a base branch
from
fix/ffi-jni-serialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0ea034a
Update
buenaflor 9e0ccbe
Update
buenaflor 7dc837d
Fix test
buenaflor 07d2248
Update
buenaflor 149803b
Merge branch 'main' into fix/ffi-jni-serialization
buenaflor 2d05329
Update
buenaflor 330ac1a
Update
buenaflor 5f689b3
Update
buenaflor 328a5b9
Merge branch 'main' into fix/ffi-jni-serialization
buenaflor d3251d0
Add JNI utility tests for Dart to Java object conversion
buenaflor 735ff6e
Rename native FFI JNI utility test file to native JNI utility test fo…
buenaflor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
packages/flutter/example/integration_test/native_jni_utils_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| // ignore_for_file: depend_on_referenced_packages | ||
|
|
||
| @TestOn('vm') | ||
|
|
||
| import 'dart:io'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:jni/jni.dart'; | ||
| import 'package:sentry_flutter/src/native/java/sentry_native_java.dart'; | ||
|
|
||
| import 'utils.dart'; | ||
|
|
||
| final _customObject = CustomObject(); | ||
|
|
||
| final _nestedMap = { | ||
| 'innerString': 'nested', | ||
| 'innerList': [1, null, 2], | ||
| 'innerNull': null, | ||
| }; | ||
|
|
||
| final _testList = [ | ||
| 'value', | ||
| 1, | ||
| 1.1, | ||
| true, | ||
| _customObject, | ||
| ['nestedList', 2], | ||
| _nestedMap, | ||
| null, | ||
| ]; | ||
|
|
||
| final _testMap = { | ||
| 'key': 'value', | ||
| 'key2': 1, | ||
| 'key3': 1.1, | ||
| 'key4': true, | ||
| 'key5': _customObject, | ||
| 'list': _testList, | ||
| 'nestedMap': _nestedMap, | ||
| 'nullEntry': null, | ||
| }; | ||
|
|
||
| const _expectedListLength = 7; | ||
| const _expectedNestedListLength = 2; | ||
| const _expectedMapLength = 7; | ||
|
|
||
| void main() { | ||
| group('JNI (Android)', () { | ||
| test('dartToJObject converts primitives', () { | ||
| _expectJniString(dartToJObject('value'), 'value'); | ||
| _expectJniInt(dartToJObject(1), 1); | ||
| _expectJniDouble(dartToJObject(1.1), 1.1); | ||
| _expectJniBool(dartToJObject(true), true); | ||
| _expectJniString(dartToJObject(_customObject), _customObject.toString()); | ||
| }); | ||
|
|
||
| test('dartToJObject converts list (drops nulls)', () { | ||
| final jList = dartToJObject(_testList).as(JList.type(JObject.type)); | ||
| addTearDown(jList.release); | ||
| _verifyJniList(jList); | ||
| }); | ||
|
|
||
| test('dartToJObject converts map (drops null values)', () { | ||
| final jMap = | ||
| dartToJObject(_testMap).as(JMap.type(JString.type, JObject.type)); | ||
| addTearDown(jMap.release); | ||
| _verifyJniMap(jMap); | ||
| }); | ||
|
|
||
| test('dartToJList', () { | ||
| final jList = dartToJList(_testList); | ||
| addTearDown(jList.release); | ||
| _verifyJniList(jList); | ||
| }); | ||
|
|
||
| test('dartToJMap', () { | ||
| final jMap = dartToJMap(_testMap); | ||
| addTearDown(jMap.release); | ||
| _verifyJniMap(jMap); | ||
| }); | ||
| }, skip: !Platform.isAndroid); | ||
| } | ||
|
|
||
| void _expectJniString(JObject obj, String expected) { | ||
| expect(obj, isA<JString>()); | ||
| expect((obj as JString).toDartString(releaseOriginal: true), expected); | ||
| } | ||
|
|
||
| void _expectJniInt(JObject obj, int expected) { | ||
| expect(obj, isA<JLong>()); | ||
| expect((obj as JLong).longValue(releaseOriginal: true), expected); | ||
| } | ||
|
|
||
| void _expectJniDouble(JObject obj, double expected) { | ||
| expect(obj, isA<JDouble>()); | ||
| expect((obj as JDouble).doubleValue(releaseOriginal: true), expected); | ||
| } | ||
|
|
||
| void _expectJniBool(JObject obj, bool expected) { | ||
| expect(obj, isA<JBoolean>()); | ||
| expect((obj as JBoolean).booleanValue(releaseOriginal: true), expected); | ||
| } | ||
|
|
||
| JObject? _jniGet(JMap<JString, JObject> map, String key) { | ||
| final jKey = key.toJString(); | ||
| final value = map[jKey]; | ||
| jKey.release(); | ||
| return value; | ||
| } | ||
|
|
||
| bool _jniIsNull(JObject? obj) => obj == null || obj.toString() == 'null'; | ||
|
|
||
| void _verifyJniList(JList<JObject> list) { | ||
| expect(list.length, _expectedListLength); | ||
|
|
||
| // Verify primitives | ||
| expect(list[0].as(JString.type).toDartString(), 'value'); | ||
| expect(list[1].as(JLong.type).longValue(), 1); | ||
| expect(list[2].as(JDouble.type).doubleValue(), 1.1); | ||
| expect(list[3].as(JBoolean.type).booleanValue(), isTrue); | ||
| expect(list[4].as(JString.type).toDartString(), _customObject.toString()); | ||
|
|
||
| // Verify nested list | ||
| final nestedList = list[5].as(JList.type(JObject.type)); | ||
| expect(nestedList.length, 2); | ||
| expect(nestedList[0].as(JString.type).toDartString(), 'nestedList'); | ||
| expect(nestedList[1].as(JLong.type).longValue(), 2); | ||
| nestedList.release(); | ||
|
|
||
| // Verify nested map | ||
| final nestedMap = list[6].as(JMap.type(JString.type, JObject.type)); | ||
| _verifyJniNestedMap(nestedMap); | ||
| nestedMap.release(); | ||
| } | ||
|
|
||
| void _verifyJniMap(JMap<JString, JObject> map) { | ||
| expect(map.length, _expectedMapLength); | ||
|
|
||
| // Verify primitives | ||
| expect(_jniGet(map, 'key')!.as(JString.type).toDartString(), 'value'); | ||
| expect(_jniGet(map, 'key2')!.as(JLong.type).longValue(), 1); | ||
| expect(_jniGet(map, 'key3')!.as(JDouble.type).doubleValue(), 1.1); | ||
| expect(_jniGet(map, 'key4')!.as(JBoolean.type).booleanValue(), isTrue); | ||
| expect(_jniGet(map, 'key5')!.as(JString.type).toDartString(), | ||
| _customObject.toString()); | ||
|
|
||
| // Verify nested list | ||
| final nestedList = _jniGet(map, 'list')!.as(JList.type(JObject.type)); | ||
| _verifyJniList(nestedList); | ||
| nestedList.release(); | ||
|
|
||
| // Verify nested map | ||
| final nestedMap = | ||
| _jniGet(map, 'nestedMap')!.as(JMap.type(JString.type, JObject.type)); | ||
| _verifyJniNestedMap(nestedMap); | ||
| nestedMap.release(); | ||
|
|
||
| // Verify null was dropped | ||
| expect(_jniIsNull(_jniGet(map, 'nullEntry')), isTrue); | ||
| } | ||
|
|
||
| void _verifyJniNestedMap(JMap<JString, JObject> map) { | ||
| expect( | ||
| _jniGet(map, 'innerString')!.as(JString.type).toDartString(), 'nested'); | ||
|
|
||
| final innerList = _jniGet(map, 'innerList')!.as(JList.type(JObject.type)); | ||
| expect(innerList.length, _expectedNestedListLength); | ||
| expect(innerList[0].as(JLong.type).longValue(), 1); | ||
| expect(innerList[1].as(JLong.type).longValue(), 2); | ||
| innerList.release(); | ||
|
|
||
| // Verify null was dropped | ||
| expect(_jniIsNull(_jniGet(map, 'innerNull')), isTrue); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,7 +192,7 @@ class SentryNativeJava extends SentryNativeChannel { | |
| final nativeOptions = native.ScopesAdapter.getInstance()?.getOptions() | ||
| ?..releasedBy(arena); | ||
| if (nativeOptions == null) return; | ||
| final jMap = _dartToJMap(breadcrumb.toJson()); | ||
| final jMap = dartToJMap(breadcrumb.toJson()); | ||
| final nativeBreadcrumb = | ||
| native.Breadcrumb.fromMap(jMap, nativeOptions) | ||
| ?..releasedBy(arena); | ||
|
|
@@ -219,7 +219,7 @@ class SentryNativeJava extends SentryNativeChannel { | |
| ?..releasedBy(arena); | ||
| if (nativeOptions == null) return; | ||
|
|
||
| final jMap = _dartToJMap(user.toJson()); | ||
| final jMap = dartToJMap(user.toJson()); | ||
| final nativeUser = native.User.fromMap(jMap, nativeOptions) | ||
| ?..releasedBy(arena); | ||
| // release jMap directly after use | ||
|
|
@@ -233,15 +233,14 @@ class SentryNativeJava extends SentryNativeChannel { | |
|
|
||
| @override | ||
| void setContexts(String key, value) => tryCatchSync('setContexts', () { | ||
| if (value == null) return; | ||
| native.Sentry.configureScope( | ||
| native.ScopeCallback.implement( | ||
| native.$ScopeCallback( | ||
| run: (iScope) { | ||
| using((arena) { | ||
| final jKey = key.toJString()..releasedBy(arena); | ||
| final jVal = _dartToJObject(value)?..releasedBy(arena); | ||
|
|
||
| if (jVal == null) return; | ||
| final jVal = dartToJObject(value)..releasedBy(arena); | ||
|
|
||
| final scope = iScope.as(const native.$Scope$Type()) | ||
| ..releasedBy(arena); | ||
|
|
@@ -390,35 +389,37 @@ class SentryNativeJava extends SentryNativeChannel { | |
| }); | ||
| } | ||
|
|
||
| JObject? _dartToJObject(Object? value) => switch (value) { | ||
| null => null, | ||
| @visibleForTesting | ||
| JObject dartToJObject(Object? value) => switch (value) { | ||
| String s => s.toJString(), | ||
| bool b => b.toJBoolean(), | ||
| int i => i.toJLong(), | ||
| double d => d.toJDouble(), | ||
| List<dynamic> l => _dartToJList(l), | ||
| Map<String, dynamic> m => _dartToJMap(m), | ||
| _ => null | ||
| List<dynamic> l => dartToJList(l), | ||
| Map<String, dynamic> m => dartToJMap(m), | ||
| _ => value.toString().toJString() | ||
buenaflor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| JList<JObject?> _dartToJList(List<dynamic> values) { | ||
| final jList = JList.array(JObject.nullableType); | ||
| for (final v in values) { | ||
| final j = _dartToJObject(v); | ||
| @visibleForTesting | ||
| JList<JObject> dartToJList(List<dynamic> values) { | ||
| final jList = JList.array(JObject.type); | ||
| for (final v in values.nonNulls) { | ||
| final j = dartToJObject(v); | ||
| jList.add(j); | ||
| j?.release(); | ||
| j.release(); | ||
| } | ||
| return jList; | ||
| } | ||
|
|
||
| JMap<JString, JObject?> _dartToJMap(Map<String, dynamic> json) { | ||
| final jMap = JMap.hash(JString.type, JObject.nullableType); | ||
| for (final entry in json.entries) { | ||
| @visibleForTesting | ||
| JMap<JString, JObject> dartToJMap(Map<String, dynamic> json) { | ||
| final jMap = JMap.hash(JString.type, JObject.type); | ||
| for (final entry in json.entries.where((e) => e.value != null)) { | ||
| final jk = entry.key.toJString(); | ||
| final jv = _dartToJObject(entry.value); | ||
| final jv = dartToJObject(entry.value); | ||
| jMap[jk] = jv; | ||
| jk.release(); | ||
| jv?.release(); | ||
| jv.release(); | ||
| } | ||
| return jMap; | ||
|
Comment on lines
+392
to
424
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. java drops null values so we can just directly filter null values out |
||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.