forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableImportTests.swift
More file actions
126 lines (122 loc) · 4.53 KB
/
VariableImportTests.swift
File metadata and controls
126 lines (122 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import JExtractSwiftLib
import Testing
@Suite
final class VariableImportTests {
let class_interfaceFile =
"""
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
// swift-module-flags: -target arm64-apple-macosx15.0 -enable-objc-interop -enable-library-evolution -module-name MySwiftLibrary
import Darwin.C
import Darwin
import Swift
import _Concurrency
import _StringProcessing
import _SwiftConcurrencyShims
public class MySwiftClass {
public var counterInt: Int
}
"""
@Test("Import: var counter: Int")
func variable_int() throws {
try assertOutput(
input: class_interfaceFile,
.ffm,
.java,
swiftModuleName: "FakeModule",
detectChunkByInitialLines: 8,
expectedChunks: [
"""
private static class swiftjava_FakeModule_MySwiftClass_counterInt$get {
private static final FunctionDescriptor DESC = FunctionDescriptor.of(
/* -> */SwiftValueLayout.SWIFT_INT,
/* self: */SwiftValueLayout.SWIFT_POINTER
);
private static final MemorySegment ADDR =
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt$get");
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
public static long call(java.lang.foreign.MemorySegment self) {
try {
if (CallTraces.TRACE_DOWNCALLS) {
CallTraces.traceDowncall(self);
}
return (long) HANDLE.invokeExact(self);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}
}
""",
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public var counterInt: Int
* }
*/
public long getCounterInt() throws SwiftIntegerOverflowException {
$ensureAlive();
long result$checked = swiftjava_FakeModule_MySwiftClass_counterInt$get.call(this.$memorySegment());
if (SwiftValueLayout.has32bitSwiftInt) {
if (result$checked < Integer.MIN_VALUE || result$checked > Integer.MAX_VALUE) {
throw new SwiftIntegerOverflowException("Return value overflow: " + result$checked);
}
}
return result$checked;
}
""",
"""
private static class swiftjava_FakeModule_MySwiftClass_counterInt$set {
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
/* newValue: */SwiftValueLayout.SWIFT_INT,
/* self: */SwiftValueLayout.SWIFT_POINTER
);
private static final MemorySegment ADDR =
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt$set");
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
public static void call(long newValue, java.lang.foreign.MemorySegment self) {
try {
if (CallTraces.TRACE_DOWNCALLS) {
CallTraces.traceDowncall(newValue, self);
}
HANDLE.invokeExact(newValue, self);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}
}
""",
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public var counterInt: Int
* }
*/
public void setCounterInt(long newValue) throws SwiftIntegerOverflowException {
$ensureAlive();
if (SwiftValueLayout.has32bitSwiftInt) {
if (newValue < Integer.MIN_VALUE || newValue > Integer.MAX_VALUE) {
throw new SwiftIntegerOverflowException("Parameter 'newValue' overflow: " + newValue);
}
}
swiftjava_FakeModule_MySwiftClass_counterInt$set.call(newValue, this.$memorySegment())
}
""",
]
)
}
}