Skip to content

Commit 01e8e41

Browse files
committed
FIX
1 parent 009a0ba commit 01e8e41

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

Samples/SwiftKitSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ static void examples() {
8383
}
8484

8585
// Example of using 'Data'.
86-
try (var arena = SwiftArena.ofConfined()) {
86+
try (var arena = AllocatingSwiftArena.ofConfined()) {
8787
var origBytes = arena.allocateFrom("foobar");
8888
var origDat = Data.init(origBytes, origBytes.byteSize(), arena);
89-
SwiftKit.trace("origDat.count = " + origDat.getCount());
89+
SwiftRuntime.trace("origDat.count = " + origDat.getCount());
9090

9191
// var origBytes = arena.allocate(ValueLayout.JAVA_INT, arry.length);
9292
// origBytes.copyFrom(MemorySegment.ofArray(arry));

Tests/JExtractSwiftTests/DataImportTests.swift

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,16 @@ final class DataImportTests {
6161
""",
6262

6363
"""
64-
@_cdecl("swiftjava_SwiftModule_Data_withUnsafeBytes_body")
65-
public func swiftjava_SwiftModule_Data_withUnsafeBytes_body(_ body: @convention(c) (UnsafeRawPointer?, Int) -> Void, _ self: UnsafeRawPointer) {
66-
self.assumingMemoryBound(to: Data.self).pointee.withUnsafeBytes(body: { (_0) in
64+
@_cdecl("swiftjava_SwiftModule_Data_count$get")
65+
public func swiftjava_SwiftModule_Data_count$get(_ self: UnsafeRawPointer) -> Int {
66+
return self.assumingMemoryBound(to: Data.self).pointee.count
67+
}
68+
""",
69+
70+
"""
71+
@_cdecl("swiftjava_SwiftModule_Data_withUnsafeBytes__")
72+
public func swiftjava_SwiftModule_Data_withUnsafeBytes__(_ body: @convention(c) (UnsafeRawPointer?, Int) -> Void, _ self: UnsafeRawPointer) {
73+
self.assumingMemoryBound(to: Data.self).pointee.withUnsafeBytes({ (_0) in
6774
return body(_0.baseAddress, _0.count)
6875
})
6976
}
@@ -202,21 +209,61 @@ final class DataImportTests {
202209
"""
203210
/**
204211
* {@snippet lang=c :
205-
* void swiftjava_SwiftModule_Data_withUnsafeBytes_body(void (*body)(const void *, ptrdiff_t), const void *self)
212+
* ptrdiff_t swiftjava_SwiftModule_Data_count$get(const void *self)
213+
* }
214+
*/
215+
private static class swiftjava_SwiftModule_Data_count$get {
216+
private static final FunctionDescriptor DESC = FunctionDescriptor.of(
217+
/* -> */SwiftValueLayout.SWIFT_INT,
218+
/* self: */SwiftValueLayout.SWIFT_POINTER
219+
);
220+
private static final MemorySegment ADDR =
221+
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_count$get");
222+
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
223+
public static long call(java.lang.foreign.MemorySegment self) {
224+
try {
225+
if (SwiftRuntime.TRACE_DOWNCALLS) {
226+
SwiftRuntime.traceDowncall(self);
227+
}
228+
return (long) HANDLE.invokeExact(self);
229+
} catch (Throwable ex$) {
230+
throw new AssertionError("should not reach here", ex$);
231+
}
232+
}
233+
}
234+
""",
235+
236+
"""
237+
/**
238+
* Downcall to Swift:
239+
* {@snippet lang=swift :
240+
* public var count: Int
241+
* }
242+
*/
243+
public long getCount() {
244+
$ensureAlive();
245+
return swiftjava_SwiftModule_Data_count$get.call(this.$memorySegment());
246+
}
247+
""",
248+
249+
"""
250+
/**
251+
* {@snippet lang=c :
252+
* void swiftjava_SwiftModule_Data_withUnsafeBytes__(void (*body)(const void *, ptrdiff_t), const void *self)
206253
* }
207254
*/
208-
private static class swiftjava_SwiftModule_Data_withUnsafeBytes_body {
255+
private static class swiftjava_SwiftModule_Data_withUnsafeBytes__ {
209256
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
210257
/* body: */SwiftValueLayout.SWIFT_POINTER,
211258
/* self: */SwiftValueLayout.SWIFT_POINTER
212259
);
213260
private static final MemorySegment ADDR =
214-
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_withUnsafeBytes_body");
261+
SwiftModule.findOrThrow("swiftjava_SwiftModule_Data_withUnsafeBytes__");
215262
private static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
216263
public static void call(java.lang.foreign.MemorySegment body, java.lang.foreign.MemorySegment self) {
217264
try {
218-
if (SwiftKit.TRACE_DOWNCALLS) {
219-
SwiftKit.traceDowncall(body, self);
265+
if (SwiftRuntime.TRACE_DOWNCALLS) {
266+
SwiftRuntime.traceDowncall(body, self);
220267
}
221268
HANDLE.invokeExact(body, self);
222269
} catch (Throwable ex$) {
@@ -237,7 +284,7 @@ final class DataImportTests {
237284
/* _0: */SwiftValueLayout.SWIFT_POINTER,
238285
/* _1: */SwiftValueLayout.SWIFT_INT
239286
);
240-
private static final MethodHandle HANDLE = SwiftKit.upcallHandle(Function.class, "apply", DESC);
287+
private static final MethodHandle HANDLE = SwiftRuntime.upcallHandle(Function.class, "apply", DESC);
241288
private static MemorySegment toUpcallStub(Function fi, Arena arena) {
242289
return Linker.nativeLinker().upcallStub(HANDLE.bindTo(fi), DESC, arena);
243290
}
@@ -252,7 +299,7 @@ final class DataImportTests {
252299
void apply(java.lang.foreign.MemorySegment _0);
253300
}
254301
private static MemorySegment $toUpcallStub(body fi, Arena arena) {
255-
return swiftjava_SwiftModule_Data_withUnsafeBytes_body.$body.toUpcallStub((_0_pointer, _0_count) -> {
302+
return swiftjava_SwiftModule_Data_withUnsafeBytes__.$body.toUpcallStub((_0_pointer, _0_count) -> {
256303
fi.apply(_0_pointer.reinterpret(_0_count));
257304
}, arena);
258305
}
@@ -264,13 +311,13 @@ final class DataImportTests {
264311
/**
265312
* Downcall to Swift:
266313
* {@snippet lang=swift :
267-
* public func withUnsafeBytes(body: (UnsafeRawBufferPointer) -> Void)
314+
* public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) -> Void)
268315
* }
269316
*/
270317
public void withUnsafeBytes(withUnsafeBytes.body body) {
271318
$ensureAlive();
272319
try(var arena$ = Arena.ofConfined()) {
273-
swiftjava_SwiftModule_Data_withUnsafeBytes_body.call(withUnsafeBytes.$toUpcallStub(body, arena$), this.$memorySegment());
320+
swiftjava_SwiftModule_Data_withUnsafeBytes__.call(withUnsafeBytes.$toUpcallStub(body, arena$), this.$memorySegment());
274321
}
275322
}
276323
"""

0 commit comments

Comments
 (0)