Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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,18 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public enum Alignment: String {
case horizontal
case vertical
}
54 changes: 54 additions & 0 deletions Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/Vehicle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public enum Vehicle {
case bicycle
case car(String)
case motorbike(String, horsePower: Int64)

public init?(name: String) {
switch name {
case "bicycle": self = .bicycle
case "car": self = .car("Unknown")
case "motorbike": self = .motorbike("Unknown", horsePower: 0)
default: return nil
}
}

public var name: String {
switch self {
case .bicycle: "bicycle"
case .car: "car"
case .motorbike: "motorbike"
}
}

public func isFasterThan(other: Vehicle) -> Bool {
switch (self, other) {
case (.bicycle, .bicycle), (.bicycle, .car), (.bicycle, .motorbike): false
case (.car, .bicycle): true
case (.car, .motorbike), (.car, .car): false
case (.motorbike, .bicycle), (.motorbike, .car): true
case (.motorbike, .motorbike): false
}
}

public mutating func upgrade() {
switch self {
case .bicycle: self = .car("Unknown")
case .car: self = .motorbike("Unknown", horsePower: 0)
case .motorbike: break
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"javaPackage": "com.example.swift",
"mode": "jni",
"logLevel": ["debug"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 20245Apple 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.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.swift.swiftkit.core.ClosableSwiftArena;
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
public class EnumBenchmark {

@State(Scope.Benchmark)
public static class BenchmarkState {
ClosableSwiftArena arena;
Vehicle vehicle;

@Setup(Level.Trial)
public void beforeAll() {
arena = new ConfinedSwiftMemorySession();
vehicle = Vehicle.motorbike("Yamaha", 900, arena);
}

@TearDown(Level.Trial)
public void afterAll() {
arena.close();
}
}

@Benchmark
public Vehicle.Motorbike java_copy(BenchmarkState state, Blackhole bh) {
Vehicle.Motorbike motorbike = state.vehicle.getAsMotorbike().orElseThrow();
bh.consume(motorbike.arg0());
bh.consume(motorbike.horsePower());

return motorbike;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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.ConfinedSwiftMemorySession;

import java.util.Optional;

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

public class AlignmentEnumTest {
@Test
void rawValue() {
try (var arena = new ConfinedSwiftMemorySession()) {
Optional<Alignment> invalid = Alignment.init("invalid", arena);
assertFalse(invalid.isPresent());

Optional<Alignment> horizontal = Alignment.init("horizontal", arena);
assertTrue(horizontal.isPresent());
assertEquals("horizontal", horizontal.get().getRawValue());

Optional<Alignment> vertical = Alignment.init("vertical", arena);
assertTrue(vertical.isPresent());
assertEquals("vertical", vertical.get().getRawValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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.ConfinedSwiftMemorySession;

import java.util.Optional;

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

public class VehicleEnumTest {
@Test
void bicycle() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertNotNull(vehicle);
}
}

@Test
void car() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.car("Porsche 911", arena);
assertNotNull(vehicle);
}
}

@Test
void motorbike() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.motorbike("Yamaha", 750, arena);
assertNotNull(vehicle);
}
}

@Test
void initName() {
try (var arena = new ConfinedSwiftMemorySession()) {
assertFalse(Vehicle.init("bus", arena).isPresent());
Optional<Vehicle> vehicle = Vehicle.init("car", arena);
assertTrue(vehicle.isPresent());
assertNotNull(vehicle.get());
}
}

@Test
void nameProperty() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertEquals("bicycle", vehicle.getName());
}
}

@Test
void isFasterThan() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle bicycle = Vehicle.bicycle(arena);
Vehicle car = Vehicle.car("Porsche 911", arena);
assertFalse(bicycle.isFasterThan(car));
assertTrue(car.isFasterThan(bicycle));
}
}

@Test
void upgrade() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertEquals("bicycle", vehicle.getName());
vehicle.upgrade();
assertEquals("car", vehicle.getName());
vehicle.upgrade();
assertEquals("motorbike", vehicle.getName());
}
}

@Test
void getAsBicycle() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.bicycle(arena);
Vehicle.Bicycle bicycle = vehicle.getAsBicycle().orElseThrow();
assertNotNull(bicycle);
}
}

@Test
void getAsCar() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.car("BMW", arena);
Vehicle.Car car = vehicle.getAsCar().orElseThrow();
assertEquals("BMW", car.arg0());
}
}

@Test
void getAsMotorbike() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.motorbike("Yamaha", 750, arena);
Vehicle.Motorbike motorbike = vehicle.getAsMotorbike().orElseThrow();
assertEquals("Yamaha", motorbike.arg0());
assertEquals(750, motorbike.horsePower());
}
}

@Test
void associatedValuesAreCopied() {
try (var arena = new ConfinedSwiftMemorySession()) {
Vehicle vehicle = Vehicle.car("BMW", arena);
Vehicle.Car car = vehicle.getAsCar().orElseThrow();
assertEquals("BMW", car.arg0());
vehicle.upgrade();
Vehicle.Motorbike motorbike = vehicle.getAsMotorbike().orElseThrow();
assertNotNull(motorbike);
// Motorbike should still remain
assertEquals("BMW", car.arg0());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ extension DeclSyntaxProtocol {
}
))
.triviaSanitizedDescription
case .enumCaseDecl(let node):
node.triviaSanitizedDescription
default:
fatalError("unimplemented \(self.kind)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,10 @@ extension LoweredFunctionSignature {
case .setter:
assert(paramExprs.count == 1)
resultExpr = "\(callee) = \(paramExprs[0])"

case .enumCase:
// This should not be called, but let's fatalError.
fatalError("Enum cases are not supported with FFM.")
}

// Lower the result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ extension FFMSwift2JavaGenerator {
let javaName = switch decl.apiKind {
case .getter: decl.javaGetterName
case .setter: decl.javaSetterName
case .function, .initializer: decl.name
case .function, .initializer, .enumCase: decl.name
}

// Signature.
Expand Down
Loading
Loading