Skip to content

Commit 9a9f9b0

Browse files
committed
feat: support map
1 parent 2d18c8b commit 9a9f9b0

File tree

7 files changed

+213
-132
lines changed

7 files changed

+213
-132
lines changed

dart_native/android/src/main/jni/dart_native.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ void *invokeNativeMethodNeo(void *classPtr, char *methodName, void **args, char
230230
nativeInvokeResult = (char *) getEnv()->GetStringUTFChars(javaString, &isCopy);
231231
}
232232
else {
233-
jobject obj = getEnv()->NewGlobalRef(getEnv()->CallObjectMethodA(object, method, argValues));
234-
jclass objCls = getEnv()->GetObjectClass(obj);
235-
//store class value
236-
cache[obj] = static_cast<jclass>(getEnv()->NewGlobalRef(objCls));
233+
jobject obj = getEnv()->NewGlobalRef(getEnv()->CallObjectMethodA(object, method, argValues));
234+
if (obj != nullptr) {
235+
jclass objCls = getEnv()->GetObjectClass(obj);
236+
//store class value
237+
cache[obj] = static_cast<jclass>(getEnv()->NewGlobalRef(objCls));
238+
}
237239
nativeInvokeResult = obj;
238240
}
239241
}

dart_native/example/android/app/src/main/java/com/dartnative/dart_native_example/RuntimeStub.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import io.flutter.Log;
99
import java.util.ArrayList;
10+
import java.util.HashMap;
1011
import java.util.HashSet;
1112
import java.util.Iterator;
1213
import java.util.List;
14+
import java.util.Map;
1315
import java.util.Set;
1416
import java.util.HashSet;
1517

@@ -181,4 +183,14 @@ public Set<Float> getFloatSet(HashSet<Float> set) {
181183
backSet.add(70.0f);
182184
return backSet;
183185
}
186+
187+
public Map getMap(HashMap<Integer, Integer> map) {
188+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
189+
Log.d(TAG, "Key = " + entry.getKey() + ", Value = " + entry.getValue());
190+
}
191+
HashMap<Integer, Float> backMap = new HashMap<>();
192+
backMap.put(1, 1.0f);
193+
backMap.put(2, 2.0f);
194+
return backMap;
195+
}
184196
}

dart_native/example/lib/android/android_new_main.dart

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ class _AndroidNewApp extends State<AndroidNewApp> {
2323
// Platform messages are asynchronous, so we initialize in an async method.
2424
Future<void> initPlatformState() async {
2525
// Benchmark
26-
// String testString =
27-
// 'This is a long string: sdlfdksjflksndhiofuu2893873(*(%¥#@)*&……¥撒肥料开发时傅雷家书那份会计师东方丽景三等奖';
28-
// int time = DateTime.now().millisecondsSinceEpoch;
29-
// for (var i = 0; i < 10000; i++) {
30-
// String _ = await platform.invokeMethod('channelString', testString);
31-
// }
32-
// print(
33-
// "Flutter Channel String Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
34-
// time = DateTime.now().millisecondsSinceEpoch;
35-
// for (var i = 0; i < 10000; i++) {
36-
// String _ = stub.getString(testString);
37-
// }
38-
// print("DartNative String Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
39-
//
40-
// time = DateTime.now().millisecondsSinceEpoch;
41-
// for (var i = 0; i < 10000; i++) {
42-
// int _ = await platform.invokeMethod('channelInt', testString);
43-
// }
44-
// print(
45-
// "Flutter Channel int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
46-
// time = DateTime.now().millisecondsSinceEpoch;
47-
// for (var i = 0; i < 10000; i++) {
48-
// int _ = stub.getInt(100);
49-
// }
50-
// print("DartNative int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
26+
String testString =
27+
'This is a long string: sdlfdksjflksndhiofuu2893873(*(%¥#@)*&……¥撒肥料开发时傅雷家书那份会计师东方丽景三等奖';
28+
int time = DateTime.now().millisecondsSinceEpoch;
29+
for (var i = 0; i < 10000; i++) {
30+
String _ = await platform.invokeMethod('channelString', testString);
31+
}
32+
print(
33+
"Flutter Channel String Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
34+
time = DateTime.now().millisecondsSinceEpoch;
35+
for (var i = 0; i < 10000; i++) {
36+
String _ = stub.getString(testString);
37+
}
38+
print("DartNative String Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
39+
40+
time = DateTime.now().millisecondsSinceEpoch;
41+
for (var i = 0; i < 10000; i++) {
42+
int _ = await platform.invokeMethod('channelInt', testString);
43+
}
44+
print(
45+
"Flutter Channel int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
46+
time = DateTime.now().millisecondsSinceEpoch;
47+
for (var i = 0; i < 10000; i++) {
48+
int _ = stub.getInt(100);
49+
}
50+
print("DartNative int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
5151
}
5252

5353
@override

dart_native/example/lib/android/runtimestub.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,8 @@ class RuntimeStub extends JObject {
142142
return JSet.fromPointer(invoke("getFloatSet", [JSet(set)], "Ljava/util/Set;")).raw;
143143
}
144144

145+
Map getMap(Map map) {
146+
return JMap.fromPointer(invoke("getMap", [JMap(map)], "Ljava/util/Map;")).raw;
147+
}
148+
145149
}

dart_native/example/lib/android/unit_test.dart

Lines changed: 108 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,109 @@ import 'package:dart_native_example/android/entity.dart';
44
import 'package:dart_native/dart_native.dart';
55

66
testAndroid(RuntimeStub stub) {
7-
// int ms = currentTimeMillis();
8-
// double resultDouble = stub.getDouble(10.0);
9-
// int use = currentTimeMillis() - ms;
10-
// print('getDouble result:$resultDouble , cost:$use');
11-
//
12-
// ms = currentTimeMillis();
13-
// String resultChar = stub.getChar('a');
14-
// use = currentTimeMillis() - ms;
15-
// print('getChar result:$resultChar , cost:$use');
16-
//
17-
// ms = currentTimeMillis();
18-
// int resultInt = stub.getInt(10);
19-
// use = currentTimeMillis() - ms;
20-
// print('getInt result:$resultInt , cost:$use');
21-
//
22-
// ms = currentTimeMillis();
23-
// bool resultBool = stub.getBool(true);
24-
// use = currentTimeMillis() - ms;
25-
// print('getBool result:$resultBool , cost:$use');
26-
//
27-
// ms = currentTimeMillis();
28-
// double resultFloat = stub.getFloat(10.5);
29-
// use = currentTimeMillis() - ms;
30-
// print('getFloat result:$resultFloat , cost:$use');
31-
//
32-
// ms = currentTimeMillis();
33-
// int resultByte = stub.getByte(1);
34-
// use = currentTimeMillis() - ms;
35-
// print('getByte result:$resultByte , cost:$use');
36-
//
37-
// ms = currentTimeMillis();
38-
// int resultShort = stub.getShort(1);
39-
// use = currentTimeMillis() - ms;
40-
// print('getShort result:$resultShort , cost:$use');
41-
//
42-
// ms = currentTimeMillis();
43-
// int resultLong = stub.getLong(100);
44-
// use = currentTimeMillis() - ms;
45-
// print('getLong result:$resultLong , cost:$use');
46-
//
47-
// ms = currentTimeMillis();
48-
// String resultString = stub.getString("test is success?");
49-
// use = currentTimeMillis() - ms;
50-
// print('getString result:$resultString, cost:$use');
51-
//
52-
// ms = currentTimeMillis();
53-
// int resultAdd = stub.add(10, 20);
54-
// use = currentTimeMillis() - ms;
55-
// print('add result:$resultAdd, cost:$use');
56-
//
57-
// ms = currentTimeMillis();
58-
// stub.log("testlog", "log test");
59-
// use = currentTimeMillis() - ms;
60-
// print('testlog, cost:$use');
61-
//
62-
// bool resultCall = stub.complexCall(
63-
// "test",
64-
// 10,
65-
// 'a',
66-
// 10.0,
67-
// 12.0,
68-
// 1,
69-
// 2,
70-
// 10000,
71-
// false);
72-
// print('call result:$resultCall');
73-
//
74-
// Entity entity = stub.createEntity();
75-
// print('entity get time : ${entity.getCurrentTime()}');
76-
// print('stub get time : ${stub.getTime(entity)}');
77-
//
78-
// print('new entity get time : ${stub.getTime(new Entity())}');
79-
//
80-
// stub.setDelegateListener(DelegateStub());
81-
//
82-
// print("integer ${stub.getInteger()}");
83-
//
84-
// List list = stub.getList([1, 2, 3, 4]);
85-
// for (int item in list) {
86-
// print("item $item");
87-
// }
88-
//
89-
// List list = stub.getByteList([byte(1), byte(2), byte(3), byte(4)]);
90-
// for (int item in list) {
91-
// print("item $item");
92-
// }
93-
//
94-
// list = stub.getFloatList([float(1.0), float(2.0), float(3.0), float(4.0)]);
95-
// for (double item in list) {
96-
// print("item $item");
97-
// }
98-
//
99-
// list = stub.getCycleList([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
100-
// for (List items in list) {
101-
// for (int item in items) {
102-
// print("item $item");
103-
// }
104-
// }
105-
//
106-
// List byteArray = stub.getByteArray([byte(1), byte(2), byte(3)]);
107-
// for (int byte in byteArray) {
108-
// print("item $byte");
109-
// }
7+
int ms = currentTimeMillis();
8+
double resultDouble = stub.getDouble(10.0);
9+
int use = currentTimeMillis() - ms;
10+
print('getDouble result:$resultDouble , cost:$use');
11+
12+
ms = currentTimeMillis();
13+
String resultChar = stub.getChar('a');
14+
use = currentTimeMillis() - ms;
15+
print('getChar result:$resultChar , cost:$use');
16+
17+
ms = currentTimeMillis();
18+
int resultInt = stub.getInt(10);
19+
use = currentTimeMillis() - ms;
20+
print('getInt result:$resultInt , cost:$use');
21+
22+
ms = currentTimeMillis();
23+
bool resultBool = stub.getBool(true);
24+
use = currentTimeMillis() - ms;
25+
print('getBool result:$resultBool , cost:$use');
26+
27+
ms = currentTimeMillis();
28+
double resultFloat = stub.getFloat(10.5);
29+
use = currentTimeMillis() - ms;
30+
print('getFloat result:$resultFloat , cost:$use');
31+
32+
ms = currentTimeMillis();
33+
int resultByte = stub.getByte(1);
34+
use = currentTimeMillis() - ms;
35+
print('getByte result:$resultByte , cost:$use');
36+
37+
ms = currentTimeMillis();
38+
int resultShort = stub.getShort(1);
39+
use = currentTimeMillis() - ms;
40+
print('getShort result:$resultShort , cost:$use');
41+
42+
ms = currentTimeMillis();
43+
int resultLong = stub.getLong(100);
44+
use = currentTimeMillis() - ms;
45+
print('getLong result:$resultLong , cost:$use');
46+
47+
ms = currentTimeMillis();
48+
String resultString = stub.getString("test is success?");
49+
use = currentTimeMillis() - ms;
50+
print('getString result:$resultString, cost:$use');
51+
52+
ms = currentTimeMillis();
53+
int resultAdd = stub.add(10, 20);
54+
use = currentTimeMillis() - ms;
55+
print('add result:$resultAdd, cost:$use');
56+
57+
ms = currentTimeMillis();
58+
stub.log("testlog", "log test");
59+
use = currentTimeMillis() - ms;
60+
print('testlog, cost:$use');
61+
62+
bool resultCall = stub.complexCall(
63+
"test",
64+
10,
65+
'a',
66+
10.0,
67+
12.0,
68+
1,
69+
2,
70+
10000,
71+
false);
72+
print('call result:$resultCall');
73+
74+
Entity entity = stub.createEntity();
75+
print('entity get time : ${entity.getCurrentTime()}');
76+
print('stub get time : ${stub.getTime(entity)}');
77+
78+
print('new entity get time : ${stub.getTime(new Entity())}');
79+
80+
stub.setDelegateListener(DelegateStub());
81+
82+
print("integer ${stub.getInteger()}");
83+
84+
List list = stub.getList([1, 2, 3, 4]);
85+
for (int item in list) {
86+
print("item $item");
87+
}
88+
89+
list = stub.getByteList([byte(1), byte(2), byte(3), byte(4)]);
90+
for (int item in list) {
91+
print("item $item");
92+
}
93+
94+
list = stub.getFloatList([float(1.0), float(2.0), float(3.0), float(4.0)]);
95+
for (double item in list) {
96+
print("item $item");
97+
}
98+
99+
list = stub.getCycleList([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
100+
for (List items in list) {
101+
for (int item in items) {
102+
print("item $item");
103+
}
104+
}
105+
106+
List byteArray = stub.getByteArray([byte(1), byte(2), byte(3)]);
107+
for (int byte in byteArray) {
108+
print("item $byte");
109+
}
110110

111111
Set intSet = stub.getIntSet(Set.from([1, 2, 3]));
112112
for (int setInt in intSet) {
@@ -117,6 +117,11 @@ testAndroid(RuntimeStub stub) {
117117
for (double setF in fSet) {
118118
print("fSet $setF");
119119
}
120+
121+
Map map = stub.getMap({1 : 10, 2 : 20, 3 : 30});
122+
map.forEach((key, value) {
123+
print("map from native $key : $value");
124+
});
120125
}
121126

122127
int currentTimeMillis() {

dart_native/lib/src/android/dart_java.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export 'runtime/call_back.dart';
33
export 'foundation/collection/jlist.dart';
44
export 'foundation/collection/jset.dart';
55
export 'foundation/collection/jarray.dart';
6+
export 'foundation/collection/jmap.dart';
67
export 'foundation/wrapperclass/integer.dart';
78
export 'foundation/wrapperclass/boolean.dart';
89
export 'foundation/wrapperclass/byte.dart';

0 commit comments

Comments
 (0)