|
20 | 20 |
|
21 | 21 | import static org.junit.jupiter.api.Assertions.*; |
22 | 22 |
|
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; |
26 | 25 | import java.util.Arrays; |
27 | 26 | import java.util.concurrent.atomic.AtomicLong; |
28 | 27 | import java.util.stream.IntStream; |
29 | 28 |
|
30 | 29 | public class WithBufferTest { |
31 | 30 |
|
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 | + } |
45 | 53 | } |
46 | | - } |
47 | 54 |
|
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 | + } |
55 | 68 |
|
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 | + } |
57 | 79 | } |
| 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 | +} |
58 | 94 | } |
0 commit comments