Skip to content

Commit f8de146

Browse files
authored
[JExtract/JNI] Import enums (#346)
1 parent 66f7a15 commit f8de146

28 files changed

+1702
-119
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public enum Alignment: String {
16+
case horizontal
17+
case vertical
18+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public enum Vehicle {
16+
case bicycle
17+
case car(String, trailer: String?)
18+
case motorbike(String, horsePower: Int64, helmets: Int32?)
19+
indirect case transformer(front: Vehicle, back: Vehicle)
20+
case boat(passengers: Int32?, length: Int16?)
21+
22+
public init?(name: String) {
23+
switch name {
24+
case "bicycle": self = .bicycle
25+
case "car": self = .car("Unknown", trailer: nil)
26+
case "motorbike": self = .motorbike("Unknown", horsePower: 0, helmets: nil)
27+
case "boat": self = .boat(passengers: nil, length: nil)
28+
default: return nil
29+
}
30+
}
31+
32+
public var name: String {
33+
switch self {
34+
case .bicycle: "bicycle"
35+
case .car: "car"
36+
case .motorbike: "motorbike"
37+
case .transformer: "transformer"
38+
case .boat: "boat"
39+
}
40+
}
41+
42+
public func isFasterThan(other: Vehicle) -> Bool {
43+
switch (self, other) {
44+
case (.bicycle, .bicycle), (.bicycle, .car), (.bicycle, .motorbike), (.bicycle, .transformer): false
45+
case (.car, .bicycle): true
46+
case (.car, .motorbike), (.car, .transformer), (.car, .car): false
47+
case (.motorbike, .bicycle), (.motorbike, .car): true
48+
case (.motorbike, .motorbike), (.motorbike, .transformer): false
49+
case (.transformer, .bicycle), (.transformer, .car), (.transformer, .motorbike): true
50+
case (.transformer, .transformer): false
51+
default: false
52+
}
53+
}
54+
55+
public mutating func upgrade() {
56+
switch self {
57+
case .bicycle: self = .car("Unknown", trailer: nil)
58+
case .car: self = .motorbike("Unknown", horsePower: 0, helmets: nil)
59+
case .motorbike: self = .transformer(front: .car("BMW", trailer: nil), back: self)
60+
case .transformer, .boat: break
61+
}
62+
}
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"javaPackage": "com.example.swift",
33
"mode": "jni",
4+
"logLevel": ["debug"]
45
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package com.example.swift;
16+
17+
import org.openjdk.jmh.annotations.*;
18+
import org.openjdk.jmh.infra.Blackhole;
19+
import org.swift.swiftkit.core.ClosableSwiftArena;
20+
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
21+
import org.swift.swiftkit.core.SwiftArena;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.Optional;
26+
import java.util.OptionalInt;
27+
import java.util.concurrent.TimeUnit;
28+
29+
@BenchmarkMode(Mode.AverageTime)
30+
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
31+
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
32+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
33+
@Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
34+
public class EnumBenchmark {
35+
36+
@State(Scope.Benchmark)
37+
public static class BenchmarkState {
38+
ClosableSwiftArena arena;
39+
Vehicle vehicle;
40+
41+
@Setup(Level.Trial)
42+
public void beforeAll() {
43+
arena = SwiftArena.ofConfined();
44+
vehicle = Vehicle.motorbike("Yamaha", 900, OptionalInt.empty(), arena);
45+
}
46+
47+
@TearDown(Level.Trial)
48+
public void afterAll() {
49+
arena.close();
50+
}
51+
}
52+
53+
@Benchmark
54+
public Vehicle.Motorbike getAssociatedValues(BenchmarkState state, Blackhole bh) {
55+
Vehicle.Motorbike motorbike = state.vehicle.getAsMotorbike().orElseThrow();
56+
bh.consume(motorbike.arg0());
57+
bh.consume(motorbike.horsePower());
58+
return motorbike;
59+
}
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package com.example.swift;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
19+
import org.swift.swiftkit.core.SwiftArena;
20+
21+
import java.util.Optional;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
25+
public class AlignmentEnumTest {
26+
@Test
27+
void rawValue() {
28+
try (var arena = SwiftArena.ofConfined()) {
29+
Optional<Alignment> invalid = Alignment.init("invalid", arena);
30+
assertFalse(invalid.isPresent());
31+
32+
Optional<Alignment> horizontal = Alignment.init("horizontal", arena);
33+
assertTrue(horizontal.isPresent());
34+
assertEquals("horizontal", horizontal.get().getRawValue());
35+
36+
Optional<Alignment> vertical = Alignment.init("vertical", arena);
37+
assertTrue(vertical.isPresent());
38+
assertEquals("vertical", vertical.get().getRawValue());
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)