swiftlang/swift-java

[JExtract] Translate String to a imported type

Opened this issue · 2 comments

Swift.String should be translated to an imported String type instead of java.lang.String, just like Foundation.Data.

In JExtract/FFM, String is currently translated to java.lang.String and lowered to C-string (UnsafePointer<Int8>).
That works for parameters, but is making passing String value between Swift and Java more expensive than it should be. Also it does not work well for API returning String because the client has to manually free the memory.

For example, using this interface,

public func getSomeString() -> String
public func receiveString(str: String)

In Java:

var str = MyLib.getSomeString()
receiveString(str)

This receiving and passing string value should not cause copy.

Like Data hardcoded API is available here.

public struct String {
public init(cString: UnsafePointer<Int8>)
public func withCString(_ body: (UnsafePointer<Int8>) -> Void)
}

Very good idea, maybe we'll end up with a SwiftString extends CharSequence for this? 🤔 https://docs.oracle.com/javase/jp/8/docs/api/java/lang/CharSequence.html and a toString on it would cause a copy if we needed it.