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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ Package.resolved


BuildLogic/.kotlin/

# Ignore Gemini IDE agent files
.agents/
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ public func globalThrowingString(doThrow: Bool) throws -> String {
return "Hello from throwing Swift!"
}

// ==== -----------------------------------------------------------------------
// MARK: Async functions

public func asyncSum(a: Int64, b: Int64) async -> Int64 {
a + b
}

public func asyncThrowsVoid(doThrow: Bool) async throws {
if doThrow {
throw SwiftExampleError(message: "expected error in asyncThrowsVoid")
}
}

// ==== -----------------------------------------------------------------------
// MARK: Overloaded functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ void call_globalCallMeDoubleSupplier_noThrow() {
assertEquals(2.0, result);
}

// ==== ----------------------------------------------------------------
// Async functions

@Test
void call_asyncSum() throws Exception {
java.util.concurrent.CompletableFuture<Long> future = MySwiftLibrary.asyncSum(10, 12);
Long result = future.get();
assertEquals(22, result);
}

@Test
void call_asyncThrowsVoid_noThrow() throws Exception {
java.util.concurrent.CompletableFuture<Void> future = MySwiftLibrary.asyncThrowsVoid(false);
future.get(); // Should complete normally
}

@Test
void call_asyncThrowsVoid_throws() {
java.util.concurrent.CompletableFuture<Void> future = MySwiftLibrary.asyncThrowsVoid(true);
java.util.concurrent.ExecutionException ex = assertThrows(java.util.concurrent.ExecutionException.class, future::get);

Throwable cause = ex.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SwiftJavaErrorException);
assertTrue(cause.getMessage().contains("expected error in asyncThrowsVoid"));
}

@Test
void call_globalCallMeIntConsumer_noThrow() {
MySwiftLibrary.globalCallMeIntConsumer((int a) -> { });
Expand Down
91 changes: 91 additions & 0 deletions Snippets/AsyncJavaFFM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 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 com.example.swift.MySwiftClass;
import com.example.swift.MySwiftLibrary;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.AllocatingSwiftArena;
import org.swift.swiftkit.ffm.generated.SwiftJavaErrorException;

import java.time.Duration;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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

public class AsyncTest {
@Test
void asyncSum() throws Exception {
// snippet.asyncUsageJava
Future<Long> future = MySwiftLibrary.asyncSum(10, 12);

Long result = future.get();
assertEquals(22, result);
// snippet.end
}

@Test
void asyncSleep() throws Exception {
// snippet.asyncSleepUsageJava
Future<Void> future = MySwiftLibrary.asyncSleep();
future.get();
// snippet.end
}

@Test
void asyncCopy() throws Exception {
// snippet.asyncCopyUsageJava
try (var arena = AllocatingSwiftArena.ofConfined()) {
MySwiftClass obj = MySwiftClass.init(10, 5, arena);
Future<MySwiftClass> future = MySwiftLibrary.asyncCopy(obj, arena);

MySwiftClass result = future.get();

assertEquals(10, result.getX());
assertEquals(5, result.getY());
}
// snippet.end
}

@Test
void asyncThrows() {
Future<Void> future = MySwiftLibrary.asyncThrows();

ExecutionException ex = assertThrows(ExecutionException.class, future::get);

Throwable cause = ex.getCause();
assertNotNull(cause);
assertEquals(SwiftJavaErrorException.class, cause.getClass());
assertTrue(cause.getMessage().contains("swiftError"));
}

@Test
void asyncOptional() throws Exception {
Future<OptionalLong> future = MySwiftLibrary.asyncOptional(42);
assertEquals(OptionalLong.of(42), future.get());
}

@Test
void asyncString() throws Exception {
Future<String> future = MySwiftLibrary.asyncString("hey");
assertEquals("hey", future.get());
}
}
Loading