-
Notifications
You must be signed in to change notification settings - Fork 43
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
[jnigen] Implement multiple interfaces #1584
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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,118 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:ffi'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../_internal.dart'; | ||
import 'jobject.dart'; | ||
import 'lang/jstring.dart'; | ||
import 'third_party/generated_bindings.dart'; | ||
import 'types.dart'; | ||
|
||
/// A builder that builds proxy objects that implement one or more interfaces. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// final implementer = JImplemeneter(); | ||
/// Foo.implementIn(implementer, fooImpl); | ||
/// Bar.implementIn(implementer, barImpl); | ||
/// final foobar = implementer.build(Foo.type); // Or `Bar.type`. | ||
/// ``` | ||
class JImplementer extends JObject { | ||
JImplementer.fromReference(super.reference) : super.fromReference(); | ||
|
||
static final _class = | ||
JClass.forName(r'com/github/dart_lang/jni/PortProxyBuilder'); | ||
|
||
static final _newId = _class.constructorId(r'(J)V'); | ||
|
||
static final _new = ProtectedJniExtensions.lookup< | ||
NativeFunction< | ||
JniResult Function(Pointer<Void>, JMethodIDPtr, | ||
VarArgs<(Int64,)>)>>('globalEnv_NewObject') | ||
.asFunction<JniResult Function(Pointer<Void>, JMethodIDPtr, int)>(); | ||
|
||
factory JImplementer() { | ||
ProtectedJniExtensions.ensureInitialized(); | ||
return JImplementer.fromReference(_new( | ||
_class.reference.pointer, | ||
_newId as JMethodIDPtr, | ||
ProtectedJniExtensions.getCurrentIsolateId()) | ||
.reference); | ||
} | ||
|
||
static final _addImplementationId = _class.instanceMethodId( | ||
r'addImplementation', | ||
r'(Ljava/lang/String;JJ)V', | ||
); | ||
|
||
static final _addImplementation = ProtectedJniExtensions.lookup< | ||
NativeFunction< | ||
JThrowablePtr Function(Pointer<Void>, JMethodIDPtr, | ||
VarArgs<(Pointer<Void>, Int64, Int64)>)>>( | ||
'globalEnv_CallVoidMethod') | ||
.asFunction< | ||
JThrowablePtr Function( | ||
Pointer<Void>, JMethodIDPtr, Pointer<Void>, int, int)>(); | ||
|
||
/// Should not be used directly. | ||
/// | ||
/// Use `implementIn` from the generated interface instead. | ||
@internal | ||
void add( | ||
String binaryName, | ||
RawReceivePort port, | ||
Pointer<NativeFunction<JObjectPtr Function(Int64, JObjectPtr, JObjectPtr)>> | ||
pointer, | ||
) { | ||
using((arena) { | ||
_addImplementation( | ||
reference.pointer, | ||
_addImplementationId as JMethodIDPtr, | ||
(binaryName.toJString()..releasedBy(arena)).reference.pointer, | ||
port.sendPort.nativePort, | ||
pointer.address) | ||
.check(); | ||
}); | ||
} | ||
|
||
static final _buildId = _class.instanceMethodId( | ||
r'build', | ||
r'()Ljava/lang/Object;', | ||
); | ||
|
||
static final _build = ProtectedJniExtensions.lookup< | ||
NativeFunction< | ||
JniResult Function( | ||
Pointer<Void>, | ||
JMethodIDPtr, | ||
)>>('globalEnv_CallObjectMethod') | ||
.asFunction< | ||
JniResult Function( | ||
Pointer<Void>, | ||
JMethodIDPtr, | ||
)>(); | ||
|
||
/// Builds an proxy object with the specified [type] that implements all the | ||
/// added interfaces with the given implementations. | ||
/// | ||
/// Releases this implementer. | ||
T implement<T extends JObject>(JObjType<T> type) { | ||
return type.fromReference(implementReference()); | ||
} | ||
|
||
/// Used in the JNIgen generated code. | ||
/// | ||
/// It is unnecessary to construct the type object when the code is generated. | ||
@internal | ||
JReference implementReference() { | ||
final ref = _build(reference.pointer, _buildId as JMethodIDPtr).reference; | ||
release(); | ||
return ref; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but have you considered using Dart's
null
instead ofjNullReference
? That's what the ObjC bindings do. It means you get Dart's null safety for free.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every Java object can be null, so it creates a lot of question mark noise. Could be an option though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait you mean only for the reference type? Yeah I could do that!