Skip to content
Merged
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
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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 SwiftJava

public func booleanArray(array: [Bool]) -> [Bool] {
return array
}

public func byteArray(array: [UInt8]) -> [UInt8] {
return array
}

public func byteArrayExplicit(array: Array<UInt8>) -> Array<UInt8> {
return array
}

public func charArray(array: [UInt16]) -> [UInt16] {
return array
}

public func shortArray(array: [Int16]) -> [Int16] {
return array
}

public func intArray(array: [Int32]) -> [Int32] {
return array
}

public func longArray(array: [Int64]) -> [Int64] {
return array
}

public func floatArray(array: [Float]) -> [Float] {
return array
}

public func doubleArray(array: [Double]) -> [Double] {
return array
}

public func stringArray(array: [String]) -> [String] {
return array
}

public func objectArray(array: [MySwiftClass]) -> [MySwiftClass] {
return array
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftArena;

import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.*;

public class ArraysTest {
@Test
void booleanArray() {
boolean[] input = new boolean[] { true, false, false, true };
assertArrayEquals(input, MySwiftLibrary.booleanArray(input));
}

@Test
void byteArray() {
byte[] input = new byte[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.byteArray(input));
}

@Test
void byteArray_empty() {
byte[] input = new byte[] {};
assertArrayEquals(input, MySwiftLibrary.byteArray(input));
}

@Test
void byteArray_null() {
assertThrows(NullPointerException.class, () -> MySwiftLibrary.byteArray(null));
}

@Test
void byteArrayExplicit() {
byte[] input = new byte[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.byteArrayExplicit(input));
}

@Test
void charArray() {
char[] input = new char[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.charArray(input));
}

@Test
void shortArray() {
short[] input = new short[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.shortArray(input));
}

@Test
void intArray() {
int[] input = new int[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.intArray(input));
}

@Test
void longArray() {
long[] input = new long[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.longArray(input));
}

@Test
void stringArray() {
String[] input = new String[] { "hey", "there", "my", "friend" };
assertArrayEquals(input, MySwiftLibrary.stringArray(input));
}

@Test
void floatArray() {
float[] input = new float[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.floatArray(input));
}

@Test
void doubleArray() {
double[] input = new double[] { 10, 20, 30, 40 };
assertArrayEquals(input, MySwiftLibrary.doubleArray(input));
}

@Test
void objectArray() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass[] input = new MySwiftClass[]{MySwiftClass.init(arena), MySwiftClass.init(arena), MySwiftClass.init(arena) };
assertEquals(3, MySwiftLibrary.objectArray(input, arena).length);
}
}
}
7 changes: 6 additions & 1 deletion Sources/JExtractSwiftLib/CodePrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,17 @@ public struct CodePrinter {

public mutating func printBraceBlock(
_ header: Any,
parameters: [String]? = nil,
function: String = #function,
file: String = #fileID,
line: UInt = #line,
body: (inout CodePrinter) throws -> ()
) rethrows {
print("\(header) {")
print("\(header) {", .continue)
if let parameters {
print(" (\(parameters.joined(separator: ", "))) in", .continue)
}
println()
indent()
try body(&self)
outdent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension CType {
case .optional(let wrapped) where wrapped.isPointer:
try self.init(cdeclType: wrapped)

case .genericParameter, .metatype, .optional, .tuple, .opaque, .existential, .composite:
case .genericParameter, .metatype, .optional, .tuple, .opaque, .existential, .composite, .array:
throw CDeclToCLoweringError.invalidCDeclType(cdeclType)
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ extension SwiftKnownTypeDeclKind {
case .void: .void
case .unsafePointer, .unsafeMutablePointer, .unsafeRawBufferPointer, .unsafeMutableRawBufferPointer,
.unsafeBufferPointer, .unsafeMutableBufferPointer, .string, .foundationData, .foundationDataProtocol,
.essentialsData, .essentialsDataProtocol, .optional:
.essentialsData, .essentialsDataProtocol, .optional, .array:
nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ struct CdeclLowering {

case .composite:
throw LoweringError.unhandledType(type)

case .array:
throw LoweringError.unhandledType(type)
}
}

Expand Down Expand Up @@ -415,7 +418,7 @@ struct CdeclLowering {
}
throw LoweringError.unhandledType(.optional(wrappedType))

case .function, .metatype, .optional, .composite:
case .function, .metatype, .optional, .composite, .array:
throw LoweringError.unhandledType(.optional(wrappedType))
}
}
Expand Down Expand Up @@ -516,7 +519,7 @@ struct CdeclLowering {
// Custom types are not supported yet.
throw LoweringError.unhandledType(type)

case .genericParameter, .function, .metatype, .optional, .tuple, .existential, .opaque, .composite:
case .genericParameter, .function, .metatype, .optional, .tuple, .existential, .opaque, .composite, .array:
// TODO: Implement
throw LoweringError.unhandledType(type)
}
Expand Down Expand Up @@ -670,7 +673,7 @@ struct CdeclLowering {
conversion: .tupleExplode(conversions, name: outParameterName)
)

case .genericParameter, .function, .optional, .existential, .opaque, .composite:
case .genericParameter, .function, .optional, .existential, .opaque, .composite, .array:
throw LoweringError.unhandledType(type)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ extension FFMSwift2JavaGenerator {

case .composite:
throw JavaTranslationError.unhandledType(swiftType)

case .array(let elementType):
throw JavaTranslationError.unhandledType(swiftType)
}
}

Expand Down Expand Up @@ -694,7 +697,7 @@ extension FFMSwift2JavaGenerator {
// TODO: Implement.
throw JavaTranslationError.unhandledType(swiftType)

case .genericParameter, .optional, .function, .existential, .opaque, .composite:
case .genericParameter, .optional, .function, .existential, .opaque, .composite, .array:
throw JavaTranslationError.unhandledType(swiftType)
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/JExtractSwiftLib/JNI/JNIJavaTypeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ enum JNIJavaTypeTranslator {
case .void: return .void

case .string: return .javaLangString

case .int, .uint, // FIXME: why not supported int/uint?
.unsafeRawPointer, .unsafeMutableRawPointer,
.unsafePointer, .unsafeMutablePointer,
.unsafeRawBufferPointer, .unsafeMutableRawBufferPointer,
.unsafeBufferPointer, .unsafeMutableBufferPointer,
.optional, .foundationData, .foundationDataProtocol, .essentialsData, .essentialsDataProtocol:
.optional, .foundationData, .foundationDataProtocol, .essentialsData, .essentialsDataProtocol, .array:
return nil
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension JNISwift2JavaGenerator {
"java.util.concurrent.atomic.AtomicBoolean",

// NonNull, Unsigned and friends
"org.swift.swiftkit.core.annotations.*",
"org.swift.swiftkit.core.annotations.*"
]
}

Expand Down
Loading
Loading