Skip to content

Commit 6716f4e

Browse files
committed
almost getting the upcall passed
1 parent 67154d5 commit 6716f4e

File tree

10 files changed

+346
-111
lines changed

10 files changed

+346
-111
lines changed

Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/WithBufferTest.java

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,75 @@
2020

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

23-
import java.lang.foreign.Arena;
24-
import java.lang.foreign.MemorySegment;
25-
import java.lang.foreign.ValueLayout;
23+
import java.lang.foreign.*;
24+
import java.lang.invoke.MethodHandle;
2625
import java.util.Arrays;
2726
import java.util.concurrent.atomic.AtomicLong;
2827
import java.util.stream.IntStream;
2928

3029
public class WithBufferTest {
3130

32-
public static byte[] returnArray() {
33-
try (var arena$ = Arena.ofConfined()) {
34-
MemorySegment _result_pointer = arena$.allocate(SwiftValueLayout.SWIFT_POINTER);
35-
MemorySegment _result_count = arena$.allocate(SwiftValueLayout.SWIFT_INT64);
36-
// swiftjava_SwiftModule_returnArray.call(_result_pointer, _result_count);
37-
// return _result_pointer
38-
// .get(SwiftValueLayout.SWIFT_POINTER, 0)
39-
// .reinterpret(_result_count.get(SwiftValueLayout.SWIFT_INT64, 0));
40-
MemorySegment memorySegment = _result_pointer
41-
.get(SwiftValueLayout.SWIFT_POINTER, 0);
42-
long newSize = _result_count.get(SwiftValueLayout.SWIFT_INT64, 0);
43-
MemorySegment arraySegment = memorySegment.reinterpret(newSize);
44-
return arraySegment.toArray(ValueLayout.JAVA_BYTE);
31+
/**
32+
* {@snippet lang = c:
33+
* void swiftjava_SwiftModule_returnArray(void (*_result_initialize)(const void *, ptrdiff_t))
34+
*}
35+
*/
36+
private static class swiftjava_SwiftModule_returnArray {
37+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
38+
/* _result_initialize: */SwiftValueLayout.SWIFT_POINTER
39+
);
40+
private static final MemorySegment ADDR = null;
41+
// SwiftModule.findOrThrow("swiftjava_SwiftModule_returnArray");
42+
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
43+
44+
public static void call(java.lang.foreign.MemorySegment _result_initialize) {
45+
try {
46+
if (CallTraces.TRACE_DOWNCALLS) {
47+
CallTraces.traceDowncall(_result_initialize);
48+
}
49+
HANDLE.invokeExact(_result_initialize);
50+
} catch (Throwable ex$) {
51+
throw new AssertionError("should not reach here", ex$);
52+
}
4553
}
46-
}
4754

48-
@Test
49-
void test_withBuffer() {
50-
AtomicLong bufferSize = new AtomicLong();
51-
MySwiftLibrary.withBuffer((buf) -> {
52-
CallTraces.trace("withBuffer{$0.byteSize()}=" + buf.byteSize());
53-
bufferSize.set(buf.byteSize());
54-
});
55+
/**
56+
* {snippet lang=c :
57+
* void (*)(const void *, ptrdiff_t)
58+
* }
59+
*/
60+
private static class $_result_initialize {
61+
public static final class Function {
62+
byte[] result = null;
63+
64+
void apply(java.lang.foreign.MemorySegment _0, long _1) {
65+
this.result = _0.reinterpret(_1).toArray(ValueLayout.JAVA_BYTE);
66+
}
67+
}
5568

56-
assertEquals(124, bufferSize.get());
69+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
70+
/* _0: */SwiftValueLayout.SWIFT_POINTER,
71+
/* _1: */SwiftValueLayout.SWIFT_INT
72+
);
73+
private static final MethodHandle HANDLE = SwiftRuntime.upcallHandle(Function.class, "apply", DESC);
74+
75+
private static MemorySegment toUpcallStub(Function fi, Arena arena) {
76+
return Linker.nativeLinker().upcallStub(HANDLE.bindTo(fi), DESC, arena);
77+
}
78+
}
5779
}
80+
81+
82+
}
83+
84+
@Test
85+
void test_withBuffer() {
86+
AtomicLong bufferSize = new AtomicLong();
87+
MySwiftLibrary.withBuffer((buf) -> {
88+
CallTraces.trace("withBuffer{$0.byteSize()}=" + buf.byteSize());
89+
bufferSize.set(buf.byteSize());
90+
});
91+
92+
assertEquals(124, bufferSize.get());
93+
}
5894
}

Samples/untitled/.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Kotlin ###
7+
.kotlin
8+
9+
### Eclipse ###
10+
.apt_generated
11+
.classpath
12+
.factorypath
13+
.project
14+
.settings
15+
.springBeans
16+
.sts4-cache
17+
bin/
18+
!**/src/main/**/bin/
19+
!**/src/test/**/bin/
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
28+
### VS Code ###
29+
.vscode/
30+
31+
### Mac OS ###
32+
.DS_Store

Samples/untitled/src/Main.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
2+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
3+
fun main() {
4+
val name = "Kotlin"
5+
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
6+
// to see how IntelliJ IDEA suggests fixing it.
7+
println("Hello, " + name + "!")
8+
9+
for (i in 1..5) {
10+
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
11+
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
12+
println("i = $i")
13+
}
14+
}

Samples/untitled/untitled.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
14+
</component>
15+
</module>

Sources/JExtractSwiftLib/Convenience/String+Extensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,13 @@ extension String {
7979
return .class(package: javaPackageName, name: javaClassName)
8080
}
8181
}
82+
83+
extension Array where Element == String {
84+
func joinedJavaStatements(indent: Int) -> String {
85+
if self.count == 1 {
86+
return "\(self.first!);"
87+
}
88+
let indentation = String(repeating: " ", count: indent)
89+
return self.joined(separator: ";\n\(indentation)")
90+
}
91+
}

0 commit comments

Comments
 (0)