|
28 | 28 |
|
29 | 29 | public class WithBufferTest { |
30 | 30 |
|
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 | | - } |
53 | | - } |
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 | | - } |
68 | | - |
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 | | - } |
| 31 | + @Test |
| 32 | + void test_withBuffer() { |
| 33 | + AtomicLong bufferSize = new AtomicLong(); |
| 34 | + MySwiftLibrary.withBuffer((buf) -> { |
| 35 | + CallTraces.trace("withBuffer{$0.byteSize()}=" + buf.byteSize()); |
| 36 | + bufferSize.set(buf.byteSize()); |
| 37 | + }); |
| 38 | + |
| 39 | + assertEquals(124, bufferSize.get()); |
79 | 40 | } |
80 | 41 |
|
| 42 | + @Test |
| 43 | + void test_getArray() { |
| 44 | + AtomicLong bufferSize = new AtomicLong(); |
| 45 | + byte[] javaBytes = MySwiftLibrary.getArray() |
81 | 46 |
|
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 | | -} |
| 47 | + assertEquals({1, 2, 3}, bufferSize.get()); |
| 48 | + } |
94 | 49 | } |
0 commit comments