Skip to content

[WIP] Allow returning new java objects from JNI downcall #227

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ Package.resolved
*/**/*.o
*/**/*.swiftdeps
*/**/*.swiftdeps~

*/**/*.index-build
.index-build
.build-vscode
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ enum SwiftWrappedError: Error {

@JavaImplementation("com.example.swift.HelloSwift")
extension HelloSwift: HelloSwiftNativeMethods {

@JavaMethod
func compute(_ a: JavaInteger?, _ b: JavaInteger?) -> JavaInteger? {
guard let a else { fatalError("parameter 'a' must not be null!") }
guard let b else { fatalError("parameter 'b' must not be null!") }

print("[swift] a = \(a.intValue())")
print("[swift] b = \(b.intValue())")
print("[swift] a + b = \(a.intValue() + b.intValue())")
let result = JavaInteger(a.intValue() + b.intValue())
let x = JavaObjectHolder(object: result.javaThis, environment: try! JavaVirtualMachine.shared().environment())
print("[swift] Integer(a + b) = \(result)")
return result
}

@JavaMethod
func sayHello(_ i: Int32, _ j: Int32) -> Int32 {
print("Hello from Swift!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public HelloSwift() {
}

native int sayHello(int x, int y);

native Integer compute(Integer x, Integer y);

native String throwMessageFromSwift(String message) throws Exception;

// To be called back by the native code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
*/
public class JavaKitSampleMain {
public static void main(String[] args) {
int result = new HelloSubclass("Swift").sayHello(17, 25);
System.out.println("sayHello(17, 25) = " + result);
var subclass = new HelloSubclass("Swift");

int intResult = subclass.sayHello(17, 25);
System.out.println("sayHello(17, 25) = " + intResult);

Integer integerResult = subclass.compute(16, 25);
System.out.println("compute(17, 25) = " + integerResult);

if (integerResult == null) {
throw AssertionError("integerResult was null!")
}
}
}
37 changes: 37 additions & 0 deletions Samples/SwiftKitSampleApp/com_example_swift_HelloJava2Swift.h

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

4 changes: 3 additions & 1 deletion Sources/Java2SwiftLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ extension JavaClassTranslator {
let resultType = try translator.getSwiftTypeNameAsString(
javaMethod.getGenericReturnType()!,
preferValueTypes: true,
outerOptional: .implicitlyUnwrappedOptional
// outerOptional: .implicitlyUnwrappedOptional // FIXME: why? it's allowed to return null objects hmmm
outerOptional: .optional // FIXME: why? it's allowed to return null objects hmmm
)

// FIXME: cleanup the checking here
Expand All @@ -555,6 +556,7 @@ extension JavaClassTranslator {
} else {
resultTypeStr = ""
}
assert(!resultTypeStr.contains("!"), "contained !: '\(resultTypeStr)'")

let throwsStr = javaMethod.throwsCheckedException ? "throws" : ""
let swiftMethodName = javaMethod.getName().escapedSwiftName
Expand Down
99 changes: 99 additions & 0 deletions Sources/JavaKit/BridgedValues/JavaValue+Integer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import JavaTypes

// TODO: extension JavaValue: CustomStringConvertible where JNIType == jobject? {
extension JavaInteger: CustomStringConvertible {
public var description: String {
"JavaKit.\(Self.self)(\(toString()))"
}
}

extension JavaInteger: JavaValue {
public typealias JNIType = jobject?

public static var jvalueKeyPath: WritableKeyPath<jvalue, JNIType> { \.l }

public static var javaType: JavaType {
.class(package: "java.lang", name: "Integer")
}

// FIXME: cannot implement in extension, need to fix source generator
// public required init(fromJNI value: JNIType, in environment: JNIEnvironment) {
// fatalError()
// }


public func getJNIValue(in environment: JNIEnvironment) -> JNIType {
fatalError()
}

public static func jniMethodCall(in environment: JNIEnvironment) -> JNIMethodCall<JNIType> {
environment.interface.CallObjectMethodA
}

public static func jniFieldGet(in environment: JNIEnvironment) -> JNIFieldGet<JNIType> {
environment.interface.GetObjectField
}

public static func jniFieldSet(in environment: JNIEnvironment) -> JNIFieldSet<JNIType> {
environment.interface.SetObjectField
}

public static func jniStaticMethodCall(in environment: JNIEnvironment) -> JNIStaticMethodCall<JNIType> {
environment.interface.CallStaticObjectMethodA
}

public static func jniStaticFieldGet(in environment: JNIEnvironment) -> JNIStaticFieldGet<JNIType> {
environment.interface.GetStaticObjectField
}

public static func jniStaticFieldSet(in environment: JNIEnvironment) -> JNIStaticFieldSet<JNIType> {
environment.interface.SetStaticObjectField
}

public static func jniNewArray(in environment: JNIEnvironment) -> JNINewArray {
return { environment, size in
// FIXME: Introduce a JavaString class that we can use for this.
let clazz = environment.interface.FindClass(environment, "java/lang/Integer")
return environment.interface.NewObjectArray(environment, size, clazz, nil)
}
}

public static func jniGetArrayRegion(in environment: JNIEnvironment) -> JNIGetArrayRegion<JNIType> {
return { environment, array, start, length, outPointer in
let buffer = UnsafeMutableBufferPointer(start: outPointer, count: Int(length))
for i in 0..<length {
buffer.initializeElement(
at: Int(i),
to: environment.interface.GetObjectArrayElement(environment, array, Int32(start + i))
)
}
}
}

public static func jniSetArrayRegion(in environment: JNIEnvironment) -> JNISetArrayRegion<JNIType> {
return { environment, array, start, length, outPointer in
let buffer = UnsafeBufferPointer(start: outPointer, count: Int(length))
for i in start..<start + length {
environment.interface.SetObjectArrayElement(environment, array, i, buffer[Int(i)])
}
}
}

public static var jniPlaceholderValue: jobject? {
nil
}
}
1 change: 1 addition & 0 deletions Sources/JavaKit/JavaObject+MethodCalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ extension AnyJavaObject {
parameterTypes: repeat (each Param).self,
resultType: Result.self
)
print("[swift][jni] Call for result \(Result.self)")
return try javaMethodCall(
method: methodID,
args: repeat each arguments
Expand Down
5 changes: 3 additions & 2 deletions Sources/JavaKit/JavaObjectHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import JavaRuntime
/// Stores a reference to a Java object, managing it as a global reference so
/// that the Java virtual machine will not move or deallocate the object
/// while this instance is live.
public class JavaObjectHolder {
public final class JavaObjectHolder {
// FIXME: thread safety!
public private(set) var object: jobject?
public let environment: JNIEnvironment

Expand All @@ -38,6 +39,6 @@ public class JavaObjectHolder {
}

deinit {
self.forget()
// self.forget()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixme, not real solution ofc.

}
}
6 changes: 6 additions & 0 deletions Sources/JavaKit/generated/JavaInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import JavaRuntime

@JavaClass("java.lang.Integer")
open class JavaInteger: JavaNumber {

// FIXME: move ot extension?
public required init(fromJNI value: JNIType, in environment: JNIEnvironment) {
fatalError()
}

@JavaMethod
@_nonoverride public convenience init(_ arg0: Int32, environment: JNIEnvironment? = nil)

Expand Down
Loading