-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jnigen] Implement multiple interfaces (#1584)
- Loading branch information
1 parent
7922b20
commit 12bc10a
Showing
28 changed files
with
724 additions
and
318 deletions.
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.