Skip to content

Commit afae76e

Browse files
committed
Fix test cases
1 parent c42156f commit afae76e

6 files changed

Lines changed: 58 additions & 382 deletions

File tree

Tests/JExtractSwiftTests/DataImportTests.swift

Lines changed: 9 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,6 @@ final class DataImportTests {
9595
_result.assumingMemoryBound(to: Data.self).initialize(to: returnData())
9696
}
9797
""",
98-
99-
"""
100-
@_cdecl("swiftjava_getType_SwiftModule_Data")
101-
public func swiftjava_getType_SwiftModule_Data() -> UnsafeMutableRawPointer /* Any.Type */ {
102-
return unsafeBitCast(Data.self, to: UnsafeMutableRawPointer.self)
103-
}
104-
""",
105-
106-
"""
107-
@_cdecl("swiftjava_SwiftModule_Data_init_bytes_count")
108-
public func swiftjava_SwiftModule_Data_init_bytes_count(_ bytes: UnsafeRawPointer, _ count: Int, _ _result: UnsafeMutableRawPointer) {
109-
_result.assumingMemoryBound(to: Data.self).initialize(to: Data(bytes: bytes, count: count))
110-
}
111-
""",
112-
113-
"""
114-
@_cdecl("swiftjava_SwiftModule_Data_count$get")
115-
public func swiftjava_SwiftModule_Data_count$get(_ self: UnsafeRawPointer) -> Int {
116-
return self.assumingMemoryBound(to: Data.self).pointee.count
117-
}
118-
""",
119-
120-
"""
121-
@_cdecl("swiftjava_SwiftModule_Data_withUnsafeBytes__")
122-
public func swiftjava_SwiftModule_Data_withUnsafeBytes__(_ body: @convention(c) (UnsafeRawPointer?, Int) -> Void, _ self: UnsafeRawPointer) {
123-
self.assumingMemoryBound(to: Data.self).pointee.withUnsafeBytes({ (_0) in
124-
return body(_0.baseAddress, _0.count)
125-
})
126-
}
127-
""",
12898
]
12999
)
130100
}
@@ -174,7 +144,7 @@ final class DataImportTests {
174144
* public func receiveData(dat: Data)
175145
* }
176146
*/
177-
public static void receiveData(Data dat) {
147+
public static void receiveData(org.swift.swiftkit.ffm.foundation.Data dat) {
178148
swiftjava_SwiftModule_receiveData_dat.call(dat.$memorySegment());
179149
}
180150
""",
@@ -212,178 +182,10 @@ final class DataImportTests {
212182
* public func returnData() -> Data
213183
* }
214184
*/
215-
public static Data returnData(AllocatingSwiftArena swiftArena) {
216-
MemorySegment result$ = swiftArena.allocate(Data.$LAYOUT);
185+
public static org.swift.swiftkit.ffm.foundation.Data returnData(AllocatingSwiftArena swiftArena) {
186+
MemorySegment result$ = swiftArena.allocate(org.swift.swiftkit.ffm.foundation.Data.$LAYOUT);
217187
swiftjava_SwiftModule_returnData.call(result$);
218-
return Data.wrapMemoryAddressUnsafe(result$, swiftArena);
219-
}
220-
""",
221-
222-
"""
223-
/**
224-
* {@snippet lang=c :
225-
* void swiftjava_SwiftModule_Data_init_bytes_count(const void *bytes, ptrdiff_t count, void *_result)
226-
* }
227-
*/
228-
private static class swiftjava_SwiftModule_Data_init_bytes_count {
229-
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
230-
/* bytes: */SwiftValueLayout.SWIFT_POINTER,
231-
/* count: */SwiftValueLayout.SWIFT_INT,
232-
/* _result: */SwiftValueLayout.SWIFT_POINTER
233-
);
234-
private static final MemorySegment ADDR =
235-
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_init_bytes_count");
236-
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
237-
public static void call(java.lang.foreign.MemorySegment bytes, long count, java.lang.foreign.MemorySegment _result) {
238-
try {
239-
if (CallTraces.TRACE_DOWNCALLS) {
240-
CallTraces.traceDowncall(bytes, count, _result);
241-
}
242-
HANDLE.invokeExact(bytes, count, _result);
243-
} catch (Throwable ex$) {
244-
throw new AssertionError("should not reach here", ex$);
245-
}
246-
}
247-
}
248-
""",
249-
250-
"""
251-
/**
252-
* Downcall to Swift:
253-
* {@snippet lang=swift :
254-
* public init(bytes: UnsafeRawPointer, count: Int)
255-
* }
256-
*/
257-
public static Data init(java.lang.foreign.MemorySegment bytes, long count, AllocatingSwiftArena swiftArena) throws SwiftIntegerOverflowException {
258-
MemorySegment result$ = swiftArena.allocate(Data.$LAYOUT);
259-
if (SwiftValueLayout.has32bitSwiftInt) {
260-
if (count < Integer.MIN_VALUE || count > Integer.MAX_VALUE) {
261-
throw new SwiftIntegerOverflowException("Parameter 'count' overflow: " + count);
262-
}
263-
}
264-
swiftjava_SwiftModule_Data_init_bytes_count.call(bytes, count, result$);
265-
return Data.wrapMemoryAddressUnsafe(result$, swiftArena);
266-
}
267-
""",
268-
269-
"""
270-
/**
271-
* {@snippet lang=c :
272-
* ptrdiff_t swiftjava_SwiftModule_Data_count$get(const void *self)
273-
* }
274-
*/
275-
private static class swiftjava_SwiftModule_Data_count$get {
276-
private static final FunctionDescriptor DESC = FunctionDescriptor.of(
277-
/* -> */SwiftValueLayout.SWIFT_INT,
278-
/* self: */SwiftValueLayout.SWIFT_POINTER
279-
);
280-
private static final MemorySegment ADDR =
281-
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_count$get");
282-
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
283-
public static long call(java.lang.foreign.MemorySegment self) {
284-
try {
285-
if (CallTraces.TRACE_DOWNCALLS) {
286-
CallTraces.traceDowncall(self);
287-
}
288-
return (long) HANDLE.invokeExact(self);
289-
} catch (Throwable ex$) {
290-
throw new AssertionError("should not reach here", ex$);
291-
}
292-
}
293-
}
294-
""",
295-
296-
"""
297-
/**
298-
* Downcall to Swift:
299-
* {@snippet lang=swift :
300-
* public var count: Int
301-
* }
302-
*/
303-
public long getCount() throws SwiftIntegerOverflowException {
304-
$ensureAlive();
305-
long result$checked = swiftjava_SwiftModule_Data_count$get.call(this.$memorySegment());
306-
if (SwiftValueLayout.has32bitSwiftInt) {
307-
if (result$checked < Integer.MIN_VALUE || result$checked > Integer.MAX_VALUE) {
308-
throw new SwiftIntegerOverflowException("Return value overflow: " + result$checked);
309-
}
310-
}
311-
return result$checked;
312-
}
313-
""",
314-
315-
"""
316-
/**
317-
* {@snippet lang=c :
318-
* void swiftjava_SwiftModule_Data_withUnsafeBytes__(void (*body)(const void *, ptrdiff_t), const void *self)
319-
* }
320-
*/
321-
private static class swiftjava_SwiftModule_Data_withUnsafeBytes__ {
322-
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
323-
/* body: */SwiftValueLayout.SWIFT_POINTER,
324-
/* self: */SwiftValueLayout.SWIFT_POINTER
325-
);
326-
private static final MemorySegment ADDR =
327-
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_withUnsafeBytes__");
328-
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
329-
public static void call(java.lang.foreign.MemorySegment body, java.lang.foreign.MemorySegment self) {
330-
try {
331-
if (CallTraces.TRACE_DOWNCALLS) {
332-
CallTraces.traceDowncall(body, self);
333-
}
334-
HANDLE.invokeExact(body, self);
335-
} catch (Throwable ex$) {
336-
throw new AssertionError("should not reach here", ex$);
337-
}
338-
}
339-
/**
340-
* {snippet lang=c :
341-
* void (*)(const void *, ptrdiff_t)
342-
* }
343-
*/
344-
private static class $body {
345-
@FunctionalInterface
346-
public interface Function {
347-
void apply(java.lang.foreign.MemorySegment _0, long _1);
348-
}
349-
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
350-
/* _0: */SwiftValueLayout.SWIFT_POINTER,
351-
/* _1: */SwiftValueLayout.SWIFT_INT
352-
);
353-
private static final MethodHandle HANDLE = SwiftRuntime.upcallHandle(Function.class, "apply", DESC);
354-
private static MemorySegment toUpcallStub(Function fi, Arena arena) {
355-
return Linker.nativeLinker().upcallStub(HANDLE.bindTo(fi), DESC, arena);
356-
}
357-
}
358-
}
359-
""",
360-
361-
"""
362-
public static class withUnsafeBytes {
363-
@FunctionalInterface
364-
public interface body {
365-
void apply(java.lang.foreign.MemorySegment _0);
366-
}
367-
private static MemorySegment $toUpcallStub(body fi, Arena arena) {
368-
return swiftjava_SwiftModule_Data_withUnsafeBytes__.$body.toUpcallStub((_0_pointer, _0_count) -> {
369-
fi.apply(_0_pointer.reinterpret(_0_count));
370-
}, arena);
371-
}
372-
}
373-
""",
374-
375-
"""
376-
/**
377-
* Downcall to Swift:
378-
* {@snippet lang=swift :
379-
* public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) -> Void)
380-
* }
381-
*/
382-
public void withUnsafeBytes(withUnsafeBytes.body body) {
383-
$ensureAlive();
384-
try(var arena$ = Arena.ofConfined()) {
385-
swiftjava_SwiftModule_Data_withUnsafeBytes__.call(withUnsafeBytes.$toUpcallStub(body, arena$), this.$memorySegment());
386-
}
188+
return org.swift.swiftkit.ffm.foundation.Data.wrapMemoryAddressUnsafe(result$, swiftArena);
387189
}
388190
""",
389191
]
@@ -413,11 +215,6 @@ final class DataImportTests {
413215
receiveDataProtocol(dat: dat.assumingMemoryBound(to: Data.self).pointee, dat2: dat2?.assumingMemoryBound(to: Data.self).pointee)
414216
}
415217
""",
416-
417-
// Just to make sure 'Data' is imported.
418-
"""
419-
@_cdecl("swiftjava_getType_SwiftModule_Data")
420-
""",
421218
]
422219
)
423220
}
@@ -470,15 +267,10 @@ final class DataImportTests {
470267
* public func receiveDataProtocol<T: DataProtocol>(dat: some DataProtocol, dat2: T?)
471268
* }
472269
*/
473-
public static void receiveDataProtocol(Data dat, Optional<Data> dat2) {
270+
public static void receiveDataProtocol(org.swift.swiftkit.ffm.foundation.Data dat, Optional<org.swift.swiftkit.ffm.foundation.Data> dat2) {
474271
swiftjava_SwiftModule_receiveDataProtocol_dat_dat2.call(dat.$memorySegment(), SwiftRuntime.toOptionalSegmentInstance(dat2));
475272
}
476273
""",
477-
478-
// Just to make sure 'Data' is imported.
479-
"""
480-
public final class Data extends FFMSwiftInstance implements SwiftValue {
481-
""",
482274
]
483275
)
484276
}
@@ -499,7 +291,7 @@ final class DataImportTests {
499291
detectChunkByInitialLines: 1,
500292
expectedChunks: [
501293
"""
502-
public static void acceptData(Data data) {
294+
public static void acceptData(org.swift.swiftkit.core.foundation.Data data) {
503295
SwiftModule.$acceptData(data.$memoryAddress());
504296
}
505297
"""
@@ -533,7 +325,7 @@ final class DataImportTests {
533325
.java,
534326
expectedChunks: [
535327
"""
536-
public static Data returnData(SwiftArena swiftArena) {
328+
public static org.swift.swiftkit.core.foundation.Data returnData(SwiftArena swiftArena) {
537329
"""
538330
]
539331
)
@@ -551,33 +343,6 @@ final class DataImportTests {
551343
)
552344
}
553345

554-
@Test("Import Data: JNI Data class")
555-
func data_jni_class() throws {
556-
let text = """
557-
import Foundation
558-
public func f() -> Data
559-
"""
560-
561-
try assertOutput(
562-
input: text,
563-
.jni,
564-
.java,
565-
detectChunkByInitialLines: 1,
566-
expectedChunks: [
567-
"public final class Data implements JNISwiftInstance, DataProtocol {",
568-
"public long getCount() {",
569-
570-
"public static Data fromByteArray(byte[] bytes, SwiftArena swiftArena) {",
571-
572-
"public byte[] toByteArray() {",
573-
"private static native byte[] $toByteArray(long selfPointer);",
574-
575-
"public byte[] toByteArrayIndirectCopy() {",
576-
"private static native byte[] $toByteArrayIndirectCopy(long selfPointer);",
577-
]
578-
)
579-
}
580-
581346
// ==== -----------------------------------------------------------------------
582347
// MARK: JNI DataProtocol generic parameter
583348

@@ -599,7 +364,7 @@ final class DataImportTests {
599364
detectChunkByInitialLines: 2,
600365
expectedChunks: [
601366
"""
602-
public static <D extends DataProtocol> MyResult processData(D data, SwiftArena swiftArena) {
367+
public static <D extends org.swift.swiftkit.core.foundation.DataProtocol> MyResult processData(D data, SwiftArena swiftArena) {
603368
"""
604369
]
605370
)
@@ -620,7 +385,7 @@ final class DataImportTests {
620385
detectChunkByInitialLines: 2,
621386
expectedChunks: [
622387
"""
623-
public static <D1 extends DataProtocol, D2 extends DataProtocol> boolean verify(D1 first, D2 second) {
388+
public static <D1 extends org.swift.swiftkit.core.foundation.DataProtocol, D2 extends org.swift.swiftkit.core.foundation.DataProtocol> boolean verify(D1 first, D2 second) {
624389
"""
625390
]
626391
)

0 commit comments

Comments
 (0)