Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.swift.swiftkit.ffm.AllocatingSwiftArena;

import java.lang.foreign.ValueLayout;
import java.nio.ByteBuffer;

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

Expand Down Expand Up @@ -90,6 +91,16 @@ void test_Data_fromByteArray() {
}
}

@Test
void test_Data_fromByteBuffer() {
try (var arena = AllocatingSwiftArena.ofConfined()) {
byte[] original = new byte[] { 1, 2, 3, 4, 5 };
ByteBuffer buffer = ByteBuffer.wrap(original);
var data = Data.fromByteBuffer(buffer, arena);
assertEquals(5, data.getCount());
}
}

@Test
void test_Data_toMemorySegment() {
try (var arena = AllocatingSwiftArena.ofConfined()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ extension FFMSwift2JavaGenerator {
"""
)

// TODO: Implement a fromByteBuffer as well
printer.print(
"""
/**
* Creates a new Swift {@link \(typeName)} instance from a {@link ByteBuffer}.
*
* @param buffer The ByteBuffer to copy byte from it into the \(typeName)
* @param arena The arena for memory management
* @return A new \(typeName) instance containing a copy of the bytes
*/
public static \(typeName) fromByteBuffer(java.nio.ByteBuffer buffer, AllocatingSwiftArena arena) {
Objects.requireNonNull(buffer, "buffer cannot be null");
return \(typeName).init(buffer.array(), arena);
}
"""
)

// Print the descriptor class for copyBytes native call using the shared helper
let copyBytesCFunc = CFunction(
Expand Down
Loading