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
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public struct MySwiftStruct {

private var cap: Int
private var len: Int
private var subscriptValue: Int
private var subscriptArray: [Int]

public init(cap: Int, len: Int) {
self.cap = cap
self.len = len
self.subscriptValue = 0
self.subscriptArray = [10, 20, 15, 75]
}

public func voidMethod() {
Expand Down Expand Up @@ -61,4 +65,22 @@ public struct MySwiftStruct {
public func makeRandomIntMethod() -> Int {
return Int.random(in: 1..<256)
}

public func getSubscriptValue() -> Int {
return self.subscriptValue
}

public func getSubscriptArrayValue(index: Int) -> Int {
return self.subscriptArray[index]
}

public subscript() -> Int {
get { return subscriptValue }
set { subscriptValue = newValue }
}

public subscript(index: Int) -> Int {
get { return subscriptArray[index] }
set { subscriptArray[index] = newValue }
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about a case with parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't notice I lost them in git stash :/ Fixed

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,26 @@ void create_struct() {
assertEquals(len, struct.getLength());
}
}

@Test
void testSubscript() {
try (var arena = AllocatingSwiftArena.ofConfined()) {
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
long currentValue = s.getSubscript();
s.setSubscript(66);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice

assertEquals(0, currentValue);
assertEquals(66, s.getSubscriptValue());
}
}

@Test
void testSubscriptWithParams() {
try (var arena = AllocatingSwiftArena.ofConfined()) {
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
long currentValue = s.getSubscript(1);
s.setSubscript(1, 66);
assertEquals(20, currentValue);
assertEquals(66, s.getSubscriptArrayValue(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
public struct MySwiftStruct {
private var cap: Int64
public var len: Int64
private var subscriptValue: Int64
private var subscriptArray: [Int64]

public init(cap: Int64, len: Int64) {
self.cap = cap
self.len = len
self.subscriptValue = 0
self.subscriptArray = [10, 20, 15, 75]
}

public init?(doInit: Bool) {
Expand All @@ -38,4 +42,22 @@ public struct MySwiftStruct {
self.cap += value
return self.cap
}

public func getSubscriptValue() -> Int64 {
return self.subscriptValue
}

public func getSubscriptArrayValue(index: Int64) -> Int64 {
return self.subscriptArray[Int(index)]
}

public subscript() -> Int64 {
get { return subscriptValue }
set { subscriptValue = newValue }
}

public subscript(index: Int64) -> Int64 {
get { return subscriptArray[Int(index)] }
set { subscriptArray[Int(index)] = newValue }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,26 @@ void increaseCap() {
assertEquals(1347, s.getCapacity());
}
}

@Test
void testSubscript() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
long currentValue = s.getSubscript();
s.setSubscript(66);
assertEquals(0, currentValue);
assertEquals(66, s.getSubscriptValue());
}
}

@Test
void testSubscriptWithParams() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftStruct s = MySwiftStruct.init(1337, 42, arena);
long currentValue = s.getSubscript(1);
s.setSubscript(1, 66);
assertEquals(20, currentValue);
assertEquals(66, s.getSubscriptArrayValue(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,10 @@ extension LoweredFunctionSignature {

// Build callee expression.
let callee: ExprSyntax = if let selfExpr {
if case .initializer = apiKind {
switch apiKind {
// Don't bother to create explicit ${Self}.init expression.
selfExpr
} else {
ExprSyntax(MemberAccessExprSyntax(base: selfExpr, name: .identifier(swiftAPIName)))
case .initializer, .subscriptGetter, .subscriptSetter: selfExpr
default: ExprSyntax(MemberAccessExprSyntax(base: selfExpr, name: .identifier(swiftAPIName)))
}
} else {
ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(swiftAPIName)))
Expand Down Expand Up @@ -845,6 +844,18 @@ extension LoweredFunctionSignature {
case .enumCase:
// This should not be called, but let's fatalError.
fatalError("Enum cases are not supported with FFM.")

case .subscriptGetter:
let parameters = paramExprs.map { $0.description }.joined(separator: ", ")
resultExpr = "\(callee)[\(raw: parameters)]"
case .subscriptSetter:
assert(paramExprs.count >= 1)

var argumentsWithoutNewValue = paramExprs
let newValueArgument = argumentsWithoutNewValue.removeLast()

let parameters = argumentsWithoutNewValue.map { $0.description }.joined(separator: ", ")
resultExpr = "\(callee)[\(raw: parameters)] = \(newValueArgument)"
}

// Lower the result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ extension FFMSwift2JavaGenerator {

// Name.
let javaName = switch decl.apiKind {
case .getter: decl.javaGetterName
case .setter: decl.javaSetterName
case .getter, .subscriptGetter: decl.javaGetterName
case .setter, .subscriptSetter: decl.javaSetterName
case .function, .initializer, .enumCase: decl.name
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/JExtractSwiftLib/ImportedDecls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package enum SwiftAPIKind {
case getter
case setter
case enumCase
case subscriptGetter
case subscriptSetter
}

/// Describes a Swift nominal type (e.g., a class, struct, enum) that has been
Expand Down Expand Up @@ -179,6 +181,8 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
case .setter: "setter:"
case .enumCase: "case:"
case .function, .initializer: ""
case .subscriptGetter: "subscriptGetter:"
case .subscriptSetter: "subscriptSetter:"
}

let context = if let parentType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ extension JNISwift2JavaGenerator {

// Name.
let javaName = switch decl.apiKind {
case .getter: decl.javaGetterName
case .setter: decl.javaSetterName
case .getter, .subscriptGetter: decl.javaGetterName
case .setter, .subscriptSetter: decl.javaSetterName
case .function, .initializer, .enumCase: decl.name
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@ extension JNISwift2JavaGenerator {
}

result = "\(callee).\(decl.name) = \(newValueArgument)"
case .subscriptGetter:
let parameters = arguments.joined(separator: ", ")
result = "\(callee)[\(parameters)]"
case .subscriptSetter:
guard let newValueArgument = arguments.last else {
fatalError("Setter did not contain newValue parameter: \(decl)")
}

var argumentsWithoutNewValue = arguments
argumentsWithoutNewValue.removeLast()

let parameters = argumentsWithoutNewValue.joined(separator: ", ")
result = "\(callee)[\(parameters)] = \(newValueArgument)"
}

// Lower the result.
Expand Down
Loading