Skip to content
Open
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
16 changes: 7 additions & 9 deletions Sources/JExtractSwiftLib/JavaExtractDecider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ public struct JavaExtractDecider: ExtractDecider {
return false
}

// Swift operators have no Java mapping
if let fn = decl.as(FunctionDeclSyntax.self) {
switch fn.name.tokenKind {
case .prefixOperator, .postfixOperator:
log.trace("Skip '\(decl.qualifiedNameForDebug)': operators are not supported on Java")
return false
default:
break
}
// Swift prefix and postfix operators have no Java mapping. A declaration's operator name is
// lexed as a binary operator, so only the `prefix` or `postfix` modifier tells them apart.
if let fn = decl.as(FunctionDeclSyntax.self),
fn.modifiers.contains(where: { $0.name.tokenKind == .keyword(.prefix) || $0.name.tokenKind == .keyword(.postfix) })
{
log.trace("Skip '\(decl.qualifiedNameForDebug)': operators are not supported on Java")
return false
}

guard let mod = decl.asProtocol((any WithModifiersSyntax).self) else {
Expand Down
56 changes: 56 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIOperatorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,60 @@ struct JNIOperatorsTests {
]
)
}

let prefixAndPostfixSource =
"""
postfix operator ++

public struct Number {
public static prefix func - (value: Number) -> Number {
value
}

public static postfix func ++ (value: Number) -> Number {
value
}

public static func + (left: Number, right: Number) -> Number {
Number()
}
}
"""

@Test
func prefixAndPostfixOperators_areSkipped_javaBindings() throws {
try assertOutput(
input: prefixAndPostfixSource,
.jni,
.java,
expectedChunks: [
"""
private static native long $plus(long left, long right);
"""
],
notExpectedChunks: [
"prefix func",
"postfix func",
]
)
}

@Test
func prefixAndPostfixOperators_areSkipped_swiftThunks() throws {
try assertOutput(
input: prefixAndPostfixSource,
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_Number__00024plus__JJ")
"""
],
notExpectedChunks: [
"minus",
"++",
]
)
}
}
Loading