-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCallCFromJava.java
More file actions
25 lines (22 loc) · 979 Bytes
/
CallCFromJava.java
File metadata and controls
25 lines (22 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
//JAVA_OPTIONS --enable-native-access=ALL-UNNAMED
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.util.Optional;
/// Proof: call-c-from-java
/// Source: content/language/call-c-from-java.yaml
void main() throws Throwable {
try (Arena arena = Arena.ofConfined()) {
// Use a system library to prove FFM compiles and links
SymbolLookup stdlib = Linker.nativeLinker().defaultLookup();
Optional<MemorySegment> segment = stdlib.find("strlen");
MemorySegment foreignFuncAddr = segment.get();
FunctionDescriptor strlen_sig =
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS);
MethodHandle strlenMethod =
Linker.nativeLinker().downcallHandle(foreignFuncAddr, strlen_sig);
var ret = (long) strlenMethod.invokeExact(arena.allocateFrom("Bambi"));
System.out.println("Return value " + ret);
}
}