Skip to content

[jextract] Generate code for free functions with primitive types in JNI mode #269

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

Merged
merged 4 commits into from
Jun 15, 2025

Conversation

madsodgaard
Copy link
Contributor

This PR adds support for a new mode for jextract that would generate JNI code instead.

We will start of with generating Java bindings and Swift thunks for free functions and primitive types.

private func printFunctionBinding(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
let returnType = decl.functionSignature.result.type.javaType
let params = decl.functionSignature.parameters.map {
"\($0.type.javaType) \($0.parameterName!)"
Copy link
Contributor Author

@madsodgaard madsodgaard Jun 12, 2025

Choose a reason for hiding this comment

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

Should probably safeguard the same way FFM does, by just naming them _0, _1 etc.. however, I am not sure when parameterName actually can be nil? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

func foo(_: Int) case should be nil. Not so common, but for example:

protocol SomeProtocol {
  init()
}
func foo<T: SomeProtocol>(_: T.Type) -> T {
  return T.init()
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah we should handle the nil, it's not very common but we should handle it.

We can maybe centralize it somehow to provide some one place where we do effectiveJavaParameters()? The impl of naming those could be just:

[...].enumerated().map { idx, param in
  "\(param.type.javaType) \(param.paremeterName ?? "arg\(idx)")"
}

At the same time maybe it's a good time to then fix them to become maybe arg0 which is a common Java way to spell those "when names are unknown" 🤔

@ktoso
Copy link
Collaborator

ktoso commented Jun 13, 2025

Good first step -- consider adding a test as well so we can keep the generated signatures tested using unit tests.

I'd be happy to just merge these small steps one by one but let's add a small test here maybe? 👍

@madsodgaard
Copy link
Contributor Author

The last thing I need to handle in this PR, is the unsigned integers which do not conform to JavaValue. I guess we should just not support generating code for them, for now?

@ktoso
Copy link
Collaborator

ktoso commented Jun 15, 2025

Yeah totally ok to do that separately 👍 Please file an issue about it so we don't forget :)

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

LGTM :-) Great first step and thanks for adding the tests!

@madsodgaard madsodgaard force-pushed the jextract-jni-free-functions branch from df1a93a to 1491950 Compare June 15, 2025 13:25
@madsodgaard madsodgaard changed the title [WIP] Generate code for free functions with primitive types in JNI mode [jextract] Generate code for free functions with primitive types in JNI mode Jun 15, 2025
@madsodgaard madsodgaard marked this pull request as ready for review June 15, 2025 13:26
@ktoso ktoso merged commit 660acb7 into swiftlang:main Jun 15, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants