Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yulingtianxia committed Dec 15, 2021
2 parents 50e8226 + 038f774 commit dc99cfd
Show file tree
Hide file tree
Showing 77 changed files with 1,015 additions and 484 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Replaces the low-performing Flutter channel with faster and more concise code.
* Under development

[![pub package](https://img.shields.io/pub/v/dart_native.svg)](https://pub.dev/packages/dart_native)
[![Build Status](https://travis-ci.org/dart-native/dart_native.svg?branch=master)](https://travis-ci.org/dart-native/dart_native)
[![Build Status](https://app.travis-ci.com/dart-native/dart_native.svg?branch=master)](https://app.travis-ci.com/dart-native/dart_native)
[![Dart CI](https://github.com/dart-native/dart_native/actions/workflows/dart.yml/badge.svg)](https://github.com/dart-native/dart_native/actions/workflows/dart.yml)

This package is the blue part(DartNative Bridge):

Expand Down
6 changes: 6 additions & 0 deletions dart_native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.5.0

* [Feature] Add annotation for Android: `@native(javaClass:)`
* [Feature] Update annotation for iOS: `@native` -> `@native()`
* [Fix] Crash on Android simulator.

## 0.4.0

* [Feature] Adapted to Flutter 2.2 and nullsafety.
Expand Down
Binary file modified dart_native/android/src/libs/arm64-v8a/libdart_native.so
Binary file not shown.
Binary file modified dart_native/android/src/libs/armeabi-v7a/libdart_native.so
Binary file not shown.
Binary file modified dart_native/android/src/libs/x86/libdart_native.so
Binary file not shown.
Binary file modified dart_native/android/src/libs/x86_64/libdart_native.so
Binary file not shown.
43 changes: 27 additions & 16 deletions dart_native/android/src/main/jni/dart_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ void _fillArgs(void **arguments, char **argumentTypes,
}
}

void *getClassName(void *objectPtr) {
if (objectPtr == nullptr) {
return nullptr;
}
auto env = _getEnv();
auto cls = _findClass(env, "java/lang/Class");
jmethodID getName = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
auto object = static_cast<jobject>(objectPtr);
auto objCls = env->GetObjectClass(object);
auto jstr = (jstring) env->CallObjectMethod(objCls, getName);
uint16_t* clsName = ConvertToDartUtf16(env, jstr);

env->DeleteLocalRef(cls);
env->DeleteLocalRef(objCls);
return clsName;
}

jobject _newObject(jclass cls,
void **arguments,
char **argumentTypes,
Expand Down Expand Up @@ -280,7 +297,7 @@ void *_doInvokeMethod(jobject object,
/// mark the last pointer as string
/// dart will check this pointer
typePointers[argumentCount] = (char *) "java.lang.String";
nativeInvokeResult = convertToDartUtf16(env, (jstring) obj);
nativeInvokeResult = ConvertToDartUtf16(env, (jstring) obj);
} else {
typePointers[argumentCount] = (char *) "java.lang.Object";
jobject gObj = env->NewGlobalRef(obj);
Expand Down Expand Up @@ -331,6 +348,7 @@ void *_doInvokeMethod(jobject object,
free(returnType);
free(arguments);
free(methodSignature);
env->DeleteLocalRef(cls);
return nativeInvokeResult;
}

Expand All @@ -353,7 +371,7 @@ void *invokeNativeMethod(void *objPtr,
return nullptr;
}
auto type = TaskThread(thread);
if (type == TaskThread::kFlutterUI) {
auto invokeFunction = [=] {
return _doInvokeMethod(object,
methodName,
arguments,
Expand All @@ -364,20 +382,12 @@ void *invokeNativeMethod(void *objPtr,
callback,
dartPort,
type);
};
if (type == TaskThread::kFlutterUI) {
return invokeFunction();
}

gTaskRunner->ScheduleInvokeTask(type, [=] {
_doInvokeMethod(object,
methodName,
arguments,
dataTypes,
argumentCount,
returnType,
stringTypeBitmask,
callback,
dartPort,
type);
});
gTaskRunner->ScheduleInvokeTask(type, invokeFunction);
return nullptr;
}

Expand Down Expand Up @@ -489,15 +499,16 @@ Java_com_dartnative_dart_1native_CallbackInvocationHandler_hookCallback(JNIEnv *
auto argument = env->GetObjectArrayElement(argumentsArray, i);
dataTypes[i] = (char *) env->GetStringUTFChars(argTypeString, 0);
if (strcmp(dataTypes[i], "java.lang.String") == 0) {
arguments[i] = convertToDartUtf16(env, (jstring) argument);
/// argument will delete in ConvertToDartUtf16
arguments[i] = ConvertToDartUtf16(env, (jstring) argument);
} else {
jobject gObj = env->NewGlobalRef(argument);
_addGlobalObject(gObj);
arguments[i] = gObj;
env->DeleteLocalRef(argument);
}

env->DeleteLocalRef(argTypeString);
env->DeleteLocalRef(argument);
}

/// when return void, jstring which from native is null.
Expand Down
2 changes: 1 addition & 1 deletion dart_native/android/src/main/jni/dn_method_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ void *callNativeStringMethod(JNIEnv *env,
jvalue *arguments) {
auto javaString =
(jstring) env->CallObjectMethodA(object, methodId, arguments);
return convertToDartUtf16(env, javaString);
return ConvertToDartUtf16(env, javaString);
}
2 changes: 1 addition & 1 deletion dart_native/android/src/main/jni/dn_type_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jstring convertToJavaUtf16(JNIEnv *env, void *value) {
}

/// nativeString not null
uint16_t *convertToDartUtf16(JNIEnv *env, jstring nativeString) {
uint16_t *ConvertToDartUtf16(JNIEnv *env, jstring nativeString) {
if (nativeString == nullptr) {
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion dart_native/android/src/main/jni/dn_type_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef bool BasicTypeToNative(void *, jvalue *, int);

jstring convertToJavaUtf16(JNIEnv *env, void *value);

uint16_t *convertToDartUtf16(JNIEnv *env, jstring nativeString);
uint16_t *ConvertToDartUtf16(JNIEnv *env, jstring nativeString);

std::map<char, std::function<BasicTypeToNative>> GetTypeConvertMap();

Expand Down
7 changes: 5 additions & 2 deletions dart_native/example/lib/android/delegate_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import 'dart:ffi';

import 'package:dart_native/dart_native.dart';
import 'package:dart_native_example/android/runtimestub.dart';
import 'package:dart_native_gen/dart_native_gen.dart';

@native(javaClass: 'com/dartnative/dart_native_example/SampleDelegate')
class DelegateStub extends JObject with SampleDelegate {
DelegateStub()
: super("com/dartnative/dart_native_example/SampleDelegate", isInterface: true) {
DelegateStub() : super(isInterface: true) {
super.registerSampleDelegate();
}

DelegateStub.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);

@override
callbackFloat(double f) {
print("callback from native $f");
Expand Down
7 changes: 4 additions & 3 deletions dart_native/example/lib/android/entity.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:ffi';

import 'package:dart_native/dart_native.dart';
import 'package:dart_native_gen/dart_native_gen.dart';

@native(javaClass: 'com/dartnative/dart_native_example/Entity')
class Entity extends JObject {
Entity(): super("com/dartnative/dart_native_example/Entity");
Entity() : super();

Entity.fromPointer(Pointer<Void> ptr)
: super.fromPointer("com/dartnative/dart_native_example/Entity", ptr);
Entity.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);

int getCurrentTime() {
return invoke('getCurrentTime', "I", args: []);
Expand Down
41 changes: 22 additions & 19 deletions dart_native/example/lib/android/runtimestub.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:ffi';

import 'package:dart_native/dart_native.dart';
import 'package:dart_native_example/android/entity.dart';
import 'package:dart_native_gen/dart_native_gen.dart';

abstract class SampleDelegate {
registerSampleDelegate() {
Expand All @@ -17,8 +20,11 @@ abstract class SampleDelegate {
callbackComplex(int i, double d, String s);
}

@native(javaClass: 'com/dartnative/dart_native_example/RuntimeStub')
class RuntimeStub extends JObject {
RuntimeStub() : super("com/dartnative/dart_native_example/RuntimeStub");
RuntimeStub() : super();

RuntimeStub.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);

int getInt(int i) {
return invokeInt('getInt', args: [i]);
Expand Down Expand Up @@ -66,24 +72,21 @@ class RuntimeStub extends JObject {

bool complexCall(String s, int i, String c, double d, double f, int b, int sh,
int l, bool boo) {
return invokeBool(
'complexCall',
args: [
s,
i,
jchar(c.codeUnitAt(0)),
d,
float(f),
byte(b),
short(sh),
long(l),
boo
]);
return invokeBool('complexCall', args: [
s,
i,
jchar(c.codeUnitAt(0)),
d,
float(f),
byte(b),
short(sh),
long(l),
boo
]);
}

Entity createEntity() {
return Entity.fromPointer(invoke(
'createEntity', "Lcom/dartnative/dart_native_example/Entity;"));
return invokeObject<Entity>('createEntity');
}

int getTime(Entity entity) {
Expand All @@ -95,8 +98,7 @@ class RuntimeStub extends JObject {
}

int getInteger() {
return JInteger.fromPointer(
invoke("getInteger", "Ljava/lang/Integer;"))
return JInteger.fromPointer(invoke("getInteger", "Ljava/lang/Integer;"))
.raw;
}

Expand All @@ -121,7 +123,8 @@ class RuntimeStub extends JObject {
}

List getByteArray(List list) {
return JArray.fromPointer(invoke("getByteArray", "[B", args: [JArray(list)])).raw;
return JArray.fromPointer(
invoke("getByteArray", "[B", args: [JArray(list)])).raw;
}

Set? getIntSet(Set set) {
Expand Down
4 changes: 2 additions & 2 deletions dart_native/example/lib/android/runtimestub_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:dart_native_example/android/runtimestub.dart';

extension RuntimeStubAsync on RuntimeStub {
Future<String> getStringAsync(String s) async {
return invokeAsync('getString', "Ljava/lang/String;", args: [s],
thread: Thread.MainThread)
return invokeAsync('getString', "Ljava/lang/String;",
args: [s], thread: Thread.MainThread)
.then((value) => value);
}
}
2 changes: 1 addition & 1 deletion dart_native/example/lib/android/unit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ testAndroid(RuntimeStub stub) async {
print('entity get time : ${entity.getCurrentTime()}');
print('stub get time : ${stub.getTime(entity)}');

print('new entity get time : ${stub.getTime(new Entity())}');
print('new entity get time : ${stub.getTime(Entity())}');

List? list = stub.getList([1, 2, 3, 4]);
if (list != null) {
Expand Down
2 changes: 1 addition & 1 deletion dart_native/example/lib/ios/runtimeson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:dart_native/dart_native.dart';
import 'package:dart_native_gen/dart_native_gen.dart';
import 'runtimestub.dart';

@native
@native()
class RuntimeSon extends RuntimeStub {
RuntimeSon([Class? isa]) : super(isa ?? Class('RuntimeSon'));
RuntimeSon.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);
Expand Down
2 changes: 1 addition & 1 deletion dart_native/example/lib/ios/runtimestub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef NSDictionaryRetBlock = NSDictionary? Function(NSDictionary? a);

typedef CGFloatRetBlock = CGFloat? Function(CGFloat? a);

@native
@native()
class RuntimeStub extends NSObject {
RuntimeStub([Class? isa]) : super(isa ?? Class('RuntimeStub'));
RuntimeStub.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);
Expand Down
2 changes: 1 addition & 1 deletion dart_native/example/lib/ios/swiftstub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:ffi';
import 'package:dart_native/dart_native.dart';
import 'package:dart_native_gen/dart_native_gen.dart';

@native
@native()
class SwiftStub extends NSObject {
static final _objcClassName = 'Runner.SwiftStub';
SwiftStub([Class? isa]) : super(isa ?? Class(_objcClassName));
Expand Down
16 changes: 4 additions & 12 deletions dart_native/example/lib/main.dn.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions dart_native/example/lib/main.java.dn.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions dart_native/example/lib/main.oc.dn.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dart_native/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.0"
version: "0.5.0"
dart_native_gen:
dependency: transitive
description:
name: dart_native_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
version: "0.3.0"
dart_style:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit dc99cfd

Please sign in to comment.