Skip to content

Commit 2b93214

Browse files
committed
Revise Foreign Function & Memory API documentation.
1 parent 48d01a7 commit 2b93214

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

docs/reference-manual/native-image/ForeignInterface.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,34 @@ link_title: Foreign Interface
55
permalink: /reference-manual/native-image/dynamic-features/foreign-interface/
66
---
77

8-
# Foreign Interface in Native Image
8+
# Foreign Function & Memory API in Native Image
99

10-
The Foreign Interface is a native API that enables Java code to interact with native code and vice versa.
11-
It is currently a preview API of the Java platform and must be enabled with `--enable-preview`.
12-
This page gives an overview of its support in Native Image.
10+
The Foreign Function & Memory (FFM) API is a native interface that enables Java code to interact with native code and vice versa.
11+
As of [JEP 442](https://openjdk.org/jeps/442){:target="_blank"}, it is a preview API of the Java platform and must be enabled with `--enable-preview`.
12+
Modules that are permitted to perform "restricted" native operations (including creating handles for calls to or from native code) must be specified using `--enable-native-access=`.
13+
This page gives an overview of support for the FFM API in Native Image.
1314

1415
## Foreign memory
15-
Shared arenas are not supported.
16+
Foreign memory functionality is generally supported. Shared arenas are currently not supported.
1617

1718
## Foreign functions
18-
The Foreign Functions Interface (FFI) allows Java code to call native functions, and conversely allows native code to invoke Java method handles.
19-
These two kind of calls are referred to as "downcalls" and "upcalls" respectively and are collectively referred to as "foreign calls".
19+
The FFM API enables Java code to call _down_ to native functions, and conversely allows native code to call _up_ to invoke Java code via method handles.
20+
These two kinds of calls are referred to as "downcalls" and "upcalls" respectively and are collectively referred to as "foreign calls".
2021

21-
This feature is currently only supported on the AMD64 platform.
22+
Currently, only downcalls are supported, and only on the AMD64 architecture.
2223

2324
### Looking up native functions
24-
FFI provides the `SymbolLookup` interface which allows to search native libraries for functions by name.
25-
`loaderLookup` is currently the only supported `SymbolLookup`.
25+
The FFM API provides the `SymbolLookup` interface to find functions in native libraries by name.
26+
`SymbolLookup.loaderLookup()` is currently the only supported kind of `SymbolLookup`.
2627

2728
### Registering foreign calls
28-
In order to perform a call to native, some glue code is required and thus must be generated at build time.
29-
Therefore, a list of the types of downcall which will be performed must be provided to the `native-image` builder.
29+
In order to perform calls to native code at runtime, supporting code must be generated at image build time.
30+
Therefore, the `native-image` tool must be provided with descriptors that characterize functions to which downcalls may be performed at runtime.
3031

31-
This list can be specified using a custom `Feature`. For example:
32+
These descriptors can be registered using a custom `Feature`, for example:
3233
```java
34+
import static java.lang.foreign.ValueLayout.*;
35+
3336
class ForeignRegistrationFeature implements Feature {
3437
public void duringSetup(DuringSetupAccess access) {
3538
RuntimeForeignAccess.registerForDowncall(FunctionDescriptor.ofVoid());
@@ -40,7 +43,9 @@ class ForeignRegistrationFeature implements Feature {
4043
}
4144
}
4245
```
43-
To activate the custom feature `--features=<fully qualified name of ForeignRegistrationFeature class>` needs to be passed to native-image.
44-
[Native Image Build Configuration](BuildConfiguration.md#embed-a-configuration-file) explains how this can be automated with a `native-image.properties` file in `META-INF/native-image`.
46+
To activate the custom feature, `--features=com.example.ForeignRegistrationFeature` (the fully-qualified name of the feature class) needs to be passed to `native-image`.
47+
It is recommended to do so [with a _native-image.properties_ file](BuildConfiguration.md#embed-a-configuration-file).
48+
49+
### Upcalls
4550

46-
Upcalls are currently not supported.
51+
Upcalls are not yet supported.

0 commit comments

Comments
 (0)