-
Notifications
You must be signed in to change notification settings - Fork 69
Make JNI handling of Int/UInt match FFM mode. #460
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
base: main
Are you sure you want to change the base?
Conversation
|
Thanks for this! Definitely an important step that we handle I do however, think we need to give this some more thought. Especially since the JNI mode will probably be used a lot by Android folks, which is commonly 32-bit. If you have the following Swift function: func f(i: Int)with this PR it gets translated to void f(long i)however, the downcall to Swift would be Also we should add some codegen tests and runtime tests while we are it, as well as add the |
|
I am aware of Int64 -> Int32 conversion problem but I didn't think about it as problem needed to be addressed in the same PR. To be honest I thought 32-bit is rare for Android, so I have no problem to work on solution for it in this PR. |
|
Thanks for the PR, this would be good to address. Overall the two "modes" should behave the same wherever possible and it is a problem that they're a bit independent currently so this would be good to align the two. 32bit is very rare and AFAICS there is a number of prominent apps that choose to not support 32bit Android devices. The Java annotation mentioned is the https://github.com/swiftlang/swift-java/blob/main/SwiftKitCore/src/main/java/org/swift/swiftkit/core/annotations/Unsigned.java so please have a look how we're using it in the ffm mode to mark something was an "U"Int. The imported parameters or return types basically get an |
|
I know 32-bit is rare these days. But since it's still a supported platform, for both Swift and the Swift Android SDK, I think we should still handle it. For example for us, most of our devices are 32-bit, and that is in the millions:) OK by me to merge this and we can tackle it in another PR, just wanted to flag this, as we need to handle it somehow. @_cdecl()
func $f(i: jlong) {
let swiftInt = Int64(fromJNI: i, ...)
#if _pointerBitWidth(_32)
guard swiftInt <= Int32.max, swiftInt >= Int32.min else {
// throw overflow exception
}
#endif
}maybe this could even just be in the |
|
Well we can't merge anything this week, so yes, let's please polish this up before branches get unlocked next week. |
…, implemented besic check for overflow of Int/UInt.
|
I committed small change with a proposed solution for check and basic tests. Let me know what you think, and if the response is positive, I'll cover rest of JNI cases(enum cases etc) and apply it to FFM. |
ktoso
left a comment
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.
Overall this looks quite neat, thank you.
Can you add some runtime tests please?
| } | ||
| } | ||
|
|
||
| static func indirectConversionSetepSwiftType(for knownKind: SwiftKnownTypeDeclKind, from knownTypes: SwiftKnownTypes) -> SwiftType? { |
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.
likely indirectConversionStepSwiftType?
| //===----------------------------------------------------------------------===// | ||
|
|
||
| /// Describes a Java exception class (e.g. `SwiftIntegerOverflowException`) | ||
| public struct JavaException: Equatable, Hashable { |
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.
Probably need to call this JavaExceptionType so we don't confuse ourselfes with a JavaExpcetion that would be a @javaclass
SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftIntegerOverflowException.java
Show resolved
Hide resolved
Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift
Outdated
Show resolved
Hide resolved
| let conversion: NativeSwiftConversionStep | ||
|
|
||
| /// Represents swift type for indirect variable used in required checks, e.g Int64 for Int overflow check on 32-bit platforms | ||
| let indirectConversion: NativeSwiftConversionStep? |
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.
Maybe let's add that "This will introduce a new name$indirect variabler" so it is easier to mentally connect the two? The "indirect" name sounds good!
Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift
Show resolved
Hide resolved
Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift
Outdated
Show resolved
Hide resolved
|
This looks great! I only have one comment regarding the implementation for maintainability. Up until now all conversions have just been done in the Was there a specific reason for introducing the "indirect" and "check" types? And not just doing it inside a |
|
Firstly, thank you @madsodgaard for sharing your concern :)
Of course, but I realise that for others this may not be a sufficient reason. As someone who just started to contribute I felt |
| } | ||
| } | ||
|
|
||
| var isArchDependingInteger: Bool { |
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.
Typo
| var isArchDependingInteger: Bool { | |
| var isArchDependentInteger: Bool { |
| case secondCase(UInt) | ||
| } | ||
| """ | ||
|
|
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.
Can you please add a test to UnsignedNumberTests.swift that the .java output in .jni mode also looks correct? The unsigned integers should appear as void unsignedInt(@Unsigned int arg) { to Java.
| * <li>{@link java.lang.RuntimeException}</li> | ||
| * <li>SwiftIntegerOverflowException</li> | ||
| * </ul> | ||
| * </p> |
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.
Thank you for the docs, ok to drop the inheritance though -- javadoc does this automatically very well
| ], | ||
| conversion: .initFromJNI(.placeholder, swiftType: type) | ||
| conversion: indirectStepType != nil ? .labelessAssignmentOfVariable(.placeholder, swiftType: type) : .initFromJNI(.placeholder, swiftType: type), | ||
| indirectConversion: indirectStepType.flatMap { .initFromJNI(.placeholder, swiftType: $0) }, |
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.
Hmm, would we be able to make this indirect conversion step the parameter the same as conversionStep? However, I do like the conversionCheck, so we'd keep that perhaps, WDYT? Makes sense?
I don't love the fact we have two conversion steps and if one is nil we want the other one etc. The check is nice because it is always in addition to a conversion
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.
Sure, I will move indirectConversion into conversion. I am not sure how soon I can do it cuz my PC died, but I will try to do it asap.
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 that sucks to hear :\ Hope you can get a functional computer soon enough. thanks for the info!
|
A few comments, this is shaping up quite well |
I believe that both modes should behave similarly when handling Int/UInt types. This PR makes JNI handle these types in the same way as FFM.