Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .agents/workflows/review-swift-java-pr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
description:
---

Review the current staged changes and draft a Pull Request description. Before drafting, verify that the code includes both source and runtime tests. Check specifically if the Samples/FFM sample has been updated with runtime tests for any substantial new functionality. Also, verify that the supported feature list in the documentation has been updated. If any of these are missing, list them clearly as action items before generating the PR draft.
Comment thread
amanmaurya92 marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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 @@ -189,4 +189,31 @@ void call_globalCallMeDoubleSupplier_noThrow() {
double result = MySwiftLibrary.globalCallMeDoubleSupplier(() -> { return 2.0; });
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"));
}
}
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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This directory should not have new files; please link into existing tests in samples. These will never run or be validated.

This is explained by Snippets/__DO_NOT_EDIT_SYMLINK_ONLY__

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So you should symlink into the Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java and put the snippets there


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