-
Notifications
You must be signed in to change notification settings - Fork 100
extract CustomStringConvertible as toString() and fix extensions
#473
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66f2581
fix extensions
madsodgaard abe5642
add runtime test
madsodgaard 05ea0f4
update docs
madsodgaard a21ab11
add `toString` and `toDebugString`
madsodgaard 2d554a8
Merge branch 'main' into to-string
madsodgaard f8b5b25
fix merge
madsodgaard 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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 JExtractSwiftLib | ||
| import Testing | ||
|
|
||
| @Suite | ||
| struct JNIExtensionTests { | ||
| let interfaceFile = | ||
| """ | ||
| extension MyStruct { | ||
| public var variableInExtension: String { get } | ||
| public func methodInExtension() {} | ||
| } | ||
|
|
||
| public protocol MyProtocol {} | ||
| public struct MyStruct {} | ||
| extension MyStruct: MyProtocol {} | ||
| """ | ||
|
|
||
| @Test("Import extensions: Java methods") | ||
| func import_javaMethods() throws { | ||
| try assertOutput( | ||
| input: interfaceFile, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public final class MyStruct implements JNISwiftInstance, MyProtocol { | ||
| ... | ||
| public void methodInExtension() { | ||
| ... | ||
| } | ||
| """ | ||
| ]) | ||
| } | ||
|
|
||
| @Test("Import extensions: Computed variables") | ||
| func import_computedVariables() throws { | ||
| try assertOutput( | ||
| input: interfaceFile, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public final class MyStruct implements JNISwiftInstance, MyProtocol { | ||
| ... | ||
| public java.lang.String getVariableInExtension() { | ||
| ... | ||
| } | ||
| """ | ||
| ]) | ||
| } | ||
| } | ||
This file contains hidden or 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,96 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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 JExtractSwiftLib | ||
| import Testing | ||
| import SwiftJavaConfigurationShared | ||
|
|
||
| @Suite | ||
| struct JNIToStringTests { | ||
| let source = | ||
| """ | ||
| public struct MyType {} | ||
| """ | ||
|
|
||
| @Test("JNI toString (Java)") | ||
| func toString_java() throws { | ||
| try assertOutput( | ||
| input: source, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public String toString() { | ||
| return $toString(this.$memoryAddress()); | ||
| } | ||
| """, | ||
| """ | ||
| private static native java.lang.String $toString(long selfPointer); | ||
| """ | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test("JNI toString (Swift)") | ||
| func toString_swift() throws { | ||
| try assertOutput( | ||
| input: source, | ||
| .jni, .swift, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| @_cdecl("Java_com_example_swift_MyType__00024toString__J") | ||
| func Java_com_example_swift_MyType__00024toString__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) -> jstring? { | ||
| ... | ||
| return String(describing: self$.pointee).getJNIValue(in: environment) | ||
| } | ||
| """, | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test("JNI toDebugString (Java)") | ||
| func toDebugString_java() throws { | ||
| try assertOutput( | ||
| input: source, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public String toDebugString() { | ||
| return $toDebugString(this.$memoryAddress()); | ||
| } | ||
| """, | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test("JNI toDebugString (Swift)") | ||
| func toDebugString_swift() throws { | ||
| try assertOutput( | ||
| input: source, | ||
| .jni, .swift, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| @_cdecl("Java_com_example_swift_MyType__00024toDebugString__J") | ||
| func Java_com_example_swift_MyType__00024toDebugString__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) -> jstring? { | ||
| ... | ||
| return String(reflecting: self$.pointee).getJNIValue(in: environment) | ||
| } | ||
| """, | ||
| ] | ||
| ) | ||
| } | ||
| } |
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.
Please add an FFM test, I think this would either just work or require very minimal work. The README also has flipped both, so let's make the impl match that 😉
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.
There is one already
https://github.com/swiftlang/swift-java/blob/main/Tests/JExtractSwiftTests/ExtensionImportTests.swift
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.
Ah thank you.
I guess we really have to start doing "in the same file" tests that do both modes (like in #475), it is getting hard to track what is covered 🤔