Skip to content

Commit 05dcbc2

Browse files
committed
refactor: fix boxing error
1 parent cedb995 commit 05dcbc2

File tree

4 files changed

+139
-132
lines changed

4 files changed

+139
-132
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ extern "C"
418418
if (notifyResult)
419419
{
420420
sem_wait(&sem);
421-
if (strcmp(returnType, "Ljava/lang/String;") == 0)
421+
if (strcmp(returnType, "java.lang.String") == 0)
422422
{
423423
callbackResult = convertToJavaUtf16(env, (char *)arguments[argumentCount], nullptr, 0);
424424
}

dart_native/example/lib/android/unit_test.dart

Lines changed: 122 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,132 @@ import 'package:dart_native/dart_native.dart';
55
import 'package:flutter/services.dart';
66

77
testAndroid(RuntimeStub stub) {
8-
// int ms = currentTimeMillis();
9-
// double resultDouble = stub.getDouble(10.0);
10-
// int use = currentTimeMillis() - ms;
11-
// print('getDouble result:$resultDouble , cost:$use');
12-
//
13-
// ms = currentTimeMillis();
14-
// String resultChar = stub.getChar('a');
15-
// use = currentTimeMillis() - ms;
16-
// print('getChar result:$resultChar , cost:$use');
17-
//
18-
// ms = currentTimeMillis();
19-
// int resultInt = stub.getInt(10);
20-
// use = currentTimeMillis() - ms;
21-
// print('getInt result:$resultInt , cost:$use');
22-
//
23-
// ms = currentTimeMillis();
24-
// bool resultBool = stub.getBool(true);
25-
// use = currentTimeMillis() - ms;
26-
// print('getBool result:$resultBool');
27-
//
28-
// ms = currentTimeMillis();
29-
// double resultFloat = stub.getFloat(10.5);
30-
// use = currentTimeMillis() - ms;
31-
// print('getFloat result:$resultFloat , cost:$use');
32-
//
33-
// ms = currentTimeMillis();
34-
// int resultByte = stub.getByte(1);
35-
// use = currentTimeMillis() - ms;
36-
// print('getByte result:$resultByte , cost:$use');
37-
//
38-
// ms = currentTimeMillis();
39-
// int resultShort = stub.getShort(1);
40-
// use = currentTimeMillis() - ms;
41-
// print('getShort result:$resultShort , cost:$use');
42-
//
43-
// ms = currentTimeMillis();
44-
// int resultLong = stub.getLong(100);
45-
// use = currentTimeMillis() - ms;
46-
// print('getLong result:$resultLong , cost:$use');
47-
//
48-
// ms = currentTimeMillis();
49-
// String resultString = stub.getString("test is success?");
50-
// use = currentTimeMillis() - ms;
51-
// print('getString result:$resultString');
52-
//
53-
// // int time = DateTime.now().millisecondsSinceEpoch;
54-
// // for (var i = 0; i < 10000; i++) {
55-
// // stub.getString("This is a long string: sdlfdksjflksndhiofuu2893873(*(%¥#@)*&……¥撒肥料开发时傅雷家书那份会计师东方丽景三等奖");
56-
// // }
57-
// // print("DartNative int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
58-
// //
59-
//
60-
// ms = currentTimeMillis();
61-
// int resultAdd = stub.add(10, 20);
62-
// use = currentTimeMillis() - ms;
63-
// print('add result:$resultAdd, cost:$use');
64-
//
65-
// ms = currentTimeMillis();
66-
// stub.log("testlog", "log test");
67-
// use = currentTimeMillis() - ms;
68-
// print('testlog, cost:$use');
69-
//
70-
// bool resultCall =
71-
// stub.complexCall("test", 10, 'a', 10.0, 12.0, 1, 2, 10000, 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())}');
8+
int ms = currentTimeMillis();
9+
double resultDouble = stub.getDouble(10.0);
10+
int use = currentTimeMillis() - ms;
11+
print('getDouble result:$resultDouble , cost:$use');
7912

80-
stub.setDelegateListener(DelegateStub());
13+
ms = currentTimeMillis();
14+
String resultChar = stub.getChar('a');
15+
use = currentTimeMillis() - ms;
16+
print('getChar result:$resultChar , cost:$use');
8117

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([
100-
// [1, 2, 3],
101-
// [4, 5, 6],
102-
// [7, 8, 9]
103-
// ]);
104-
// for (List items in list) {
105-
// for (int item in items) {
106-
// print("item $item");
107-
// }
108-
// }
109-
//
110-
// List byteArray = stub.getByteArray([byte(1), byte(2), byte(3)]);
111-
// for (int byte in byteArray) {
112-
// print("item $byte");
113-
// }
114-
//
115-
// Set intSet = stub.getIntSet(Set.from([1, 2, 3]));
116-
// for (int setInt in intSet) {
117-
// print("intSet $setInt");
118-
// }
119-
//
120-
// Set fSet = stub.getFloatSet(Set.from([float(1.0), float(2.0), float(4.0)]));
121-
// for (double setF in fSet) {
122-
// print("fSet $setF");
18+
ms = currentTimeMillis();
19+
int resultInt = stub.getInt(10);
20+
use = currentTimeMillis() - ms;
21+
print('getInt result:$resultInt , cost:$use');
22+
23+
ms = currentTimeMillis();
24+
bool resultBool = stub.getBool(true);
25+
use = currentTimeMillis() - ms;
26+
print('getBool result:$resultBool');
27+
28+
ms = currentTimeMillis();
29+
double resultFloat = stub.getFloat(10.5);
30+
use = currentTimeMillis() - ms;
31+
print('getFloat result:$resultFloat , cost:$use');
32+
33+
ms = currentTimeMillis();
34+
int resultByte = stub.getByte(1);
35+
use = currentTimeMillis() - ms;
36+
print('getByte result:$resultByte , cost:$use');
37+
38+
ms = currentTimeMillis();
39+
int resultShort = stub.getShort(1);
40+
use = currentTimeMillis() - ms;
41+
print('getShort result:$resultShort , cost:$use');
42+
43+
ms = currentTimeMillis();
44+
int resultLong = stub.getLong(100);
45+
use = currentTimeMillis() - ms;
46+
print('getLong result:$resultLong , cost:$use');
47+
48+
ms = currentTimeMillis();
49+
String resultString = stub.getString("test is success?");
50+
use = currentTimeMillis() - ms;
51+
print('getString result:$resultString');
52+
53+
// int time = DateTime.now().millisecondsSinceEpoch;
54+
// for (var i = 0; i < 10000; i++) {
55+
// stub.getString("This is a long string: sdlfdksjflksndhiofuu2893873(*(%¥#@)*&……¥撒肥料开发时傅雷家书那份会计师东方丽景三等奖");
12356
// }
57+
// print("DartNative int Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
12458
//
125-
// Map map = stub.getMap({"1": 10, "2": 20, "3": 30});
126-
// map.forEach((key, value) {
127-
// print("map from native $key : $value");
128-
// });
129-
//
130-
// List strList = stub.getStringList(["test啊 emoji🤣", "emoji🤣"]);
131-
// for (var item in strList) {
132-
// print("item $item");
133-
// }
59+
60+
ms = currentTimeMillis();
61+
int resultAdd = stub.add(10, 20);
62+
use = currentTimeMillis() - ms;
63+
print('add result:$resultAdd, cost:$use');
64+
65+
ms = currentTimeMillis();
66+
stub.log("testlog", "log test");
67+
use = currentTimeMillis() - ms;
68+
print('testlog, cost:$use');
69+
70+
bool resultCall =
71+
stub.complexCall("test", 10, 'a', 10.0, 12.0, 1, 2, 10000, 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([
100+
[1, 2, 3],
101+
[4, 5, 6],
102+
[7, 8, 9]
103+
]);
104+
for (List items in list) {
105+
for (int item in items) {
106+
print("item $item");
107+
}
108+
}
109+
110+
List byteArray = stub.getByteArray([byte(1), byte(2), byte(3)]);
111+
for (int byte in byteArray) {
112+
print("item $byte");
113+
}
114+
115+
Set intSet = stub.getIntSet(Set.from([1, 2, 3]));
116+
for (int setInt in intSet) {
117+
print("intSet $setInt");
118+
}
119+
120+
Set fSet = stub.getFloatSet(Set.from([float(1.0), float(2.0), float(4.0)]));
121+
for (double setF in fSet) {
122+
print("fSet $setF");
123+
}
124+
125+
Map map = stub.getMap({"1": 10, "2": 20, "3": 30});
126+
map.forEach((key, value) {
127+
print("map from native $key : $value");
128+
});
129+
//
130+
List strList = stub.getStringList(["test啊 emoji🤣", "emoji🤣"]);
131+
for (var item in strList) {
132+
print("item $item");
133+
}
134134
}
135135

136136
int currentTimeMillis() {

dart_native/lib/src/android/foundation/wrapperclass/boxing_unboxing.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:ffi';
22

33
import 'package:dart_native/dart_native.dart';
4-
import 'package:dart_native/src/android/common/pointer_encoding.dart';
54

65
dynamic boxingWrapperClass(dynamic value) {
76
if (value is byte) {
@@ -22,8 +21,6 @@ dynamic boxingWrapperClass(dynamic value) {
2221
return JSet(value);
2322
} else if (value is bool) {
2423
return Boolean(value);
25-
} else if (value is String) {
26-
return toUtf16(value);
2724
} else {
2825
return value;
2926
}
@@ -54,8 +51,9 @@ dynamic unBoxingWrapperClass(dynamic value, String valueType) {
5451
case "java.util.HashSet":
5552
return JSet.fromPointer(value).raw;
5653
case "java.lang.String":
57-
return fromUtf16(value);
54+
return value;
5855
default:
59-
return JObject(valueType?.replaceAll(".", "/") ?? "java.lang.Object", pointer: value);
56+
return JObject(valueType?.replaceAll(".", "/") ?? "java.lang.Object",
57+
pointer: value);
6058
}
6159
}

dart_native/lib/src/android/runtime/call_back.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:ffi';
22

33
import 'package:dart_native/dart_native.dart';
44
import 'package:dart_native/src/android/common/callback_manager.dart';
5+
import 'package:dart_native/src/android/common/pointer_encoding.dart';
56
import 'package:dart_native/src/android/runtime/functions.dart';
67
import 'package:ffi/ffi.dart';
78

@@ -39,17 +40,25 @@ _callback(
3940
for (var i = 0; i < argCount; i++) {
4041
Pointer<Utf8> argTypePtr = argTypesPtrPtr.elementAt(i).value;
4142
Pointer<Void> argPtr = argsPtrPtr.elementAt(i).value;
42-
dynamic arg =
43-
unBoxingWrapperClass(argPtr, Utf8.fromUtf8(argTypePtr.cast()));
43+
final String argType = Utf8.fromUtf8(argTypePtr.cast());
44+
dynamic arg = argType == "java.lang.String"
45+
? fromUtf16(argPtr)
46+
: unBoxingWrapperClass(argPtr, argType);
4447
args.add(arg);
4548
}
4649

4750
dynamic result = Function.apply(function, args);
4851

4952
if (result != null) {
53+
if (result is String) {
54+
argsPtrPtr.elementAt(argCount).value = toUtf16(result).cast();
55+
return;
56+
}
57+
5058
dynamic wrapperResult = boxingWrapperClass(result);
51-
argsPtrPtr.elementAt(argCount).value = wrapperResult is JObject ? wrapperResult.pointer
52-
: wrapperResult;
59+
argsPtrPtr.elementAt(argCount).value = wrapperResult is JObject
60+
? wrapperResult.pointer
61+
: wrapperResult;
5362
}
5463
}
5564

0 commit comments

Comments
 (0)