Skip to content

Commit d36e5e4

Browse files
committed
[GR-41686] Document new type mapping features for embedded espresso. contexts
PullRequest: graal/12913
2 parents 0895366 + 2df4f8d commit d36e5e4

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

docs/reference-manual/java-on-truffle/Interoperability.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ A number of useful context option can be set with `contextBuilder.option(key, va
149149
* `java.Verify` can be set to `none`, `remove`, or `all` to control whether bytecode verification does not happen, only happens on user code, or happens for all classes.
150150
* `java.JDWPOptions` can be set to setup and enable debugging over JDWP. For example, it could be set to `transport=dt_socket,server=y,address=localhost:8000,suspend=y`.
151151
* `java.Polyglot` can be set to `true` or `false` to allow or deny access to the polyglot features from the `com.oracle.truffle.espresso.polyglot` package.
152+
* `java.PolyglotTypeConverters` can be set to declare a type conversion function that maps a meta qualified name to a type converter class. Please refer to more details in a dedicated section below.
153+
* `java.PolyglotInterfaceMappings` can be set to a semicolon-separated list of 1:1 interface type mappings to automatically construct guest proxies for host objects that implement declared interfaces in the list. Please refer to more details in a dedicated section below.
152154

153155
***Java on Truffle does not support evaluation (`.eval`) of Java sources.**
154156

@@ -183,6 +185,79 @@ assert java_lang_Integer.equals(integer_class.getMember("static"));
183185
assert java_lang_Number.equals(java_lang_Integer.getMember("super"));
184186
```
185187

188+
### Converting host objects to guest types using type converters
189+
190+
Since version 22.3.0 Java on Truffle has built-in support for declaring type conversion of host objects to proper guest-typed objects. This is done via context builder options as described above. The main idea is to allow transparent flow of objects from host to guest without having to perform guest type checks when host objects enter an embedded Java on Truffle context. Specifically the follwoing options can be set to control type conversion for an embedded context:
191+
192+
#### java.PolyglotTypeConverters
193+
This option takes precedence over `java.PolyglotInterfaceMappings` and thus, if a dedicated type converter function is defined, no other automatic interface mapping proxies are generated by Java on Truffle.
194+
195+
*Note: Declared type converters must implement the `GuestTypeConversion` interface located in the `com.oracle.truffle.espresso.polyglot` package in `polyglor.jar`.*
196+
```java
197+
package com.oracle.truffle.espresso.polyglot;
198+
199+
public interface GuestTypeConversion<T> {
200+
T toGuest(Object polyglotInstance);
201+
}
202+
```
203+
204+
For each type converter declared use one option call like this:
205+
206+
```java
207+
// host java
208+
Context polyglot = Context.newBuilder().allowAllAccess(true).
209+
option("java.PolyglotTypeConverters.java.math.BigDecimal", "guest.context.path.BigDecimalConverter").
210+
build();
211+
...
212+
213+
// guest java
214+
package guest.context.path;
215+
216+
import com.oracle.truffle.espresso.polyglot.GuestTypeConversion;
217+
import com.oracle.truffle.espresso.polyglot.Interop;
218+
import com.oracle.truffle.espresso.polyglot.InteropException;
219+
220+
import java.math.BigDecimal;
221+
222+
public class BigDecimalConverter implements GuestTypeConversion<BigDecimal> {
223+
224+
@Override
225+
@SuppressWarnings("unchecked")
226+
public BigDecimal toGuest(Object polyglotInstance) {
227+
try {
228+
return new BigDecimal(Interop.asString(Interop.invokeMember(polyglotInstance, "toString")));
229+
} catch (InteropException e) {
230+
throw new ClassCastException("polyglot instance cannot be cast to java.math.BigDecimal");
231+
}
232+
}
233+
}
234+
235+
```
236+
The `java.math.Bigdecimal` part of the option declares the fully qualified meta name of a host object entering Java on Truffle.
237+
238+
#### java.PolyglotInterfaceMappings
239+
240+
If there are no dedicated `java.PolyglotTypeConverters` for a host object flowing into an embedded Java on Truffle context, automatic interface type mapping kicks in. `java.PolyglotInterfaceMappings` enables seamless interface type sharing between the host and the embedded context.
241+
242+
The following example shows how this option can be used to allow passing common JDK collection types by interface to an embedded Java on Truffle context:
243+
244+
```java
245+
// host java
246+
builder.option("java.PolyglotInterfaceMappings", getInterfaceMappings());
247+
248+
249+
private static String getInterfaceMappings(){
250+
return "java.lang.Iterable;"+
251+
"java.util.Collection;"+
252+
"java.util.List;"+
253+
"java.util.Set;"+
254+
"java.util.Map;"+
255+
"java.util.Iterator;"+
256+
"java.util.Spliterator;";
257+
}
258+
259+
```
260+
186261
## Multithreading
187262

188263
Java on Truffle is designed to be a multi-threaded language and much of the ecosystem expects threads to be available.

espresso/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### User-visible changes
55
* Interop invokable members of espresso objects can now also be read.
66
* Added the `addPath` invokable member to Espresso Bindings if `java.UseBindingsLoader=true`. Allows adding new path to the classloader associated with the bindings.
7+
* Added option `java.PolyglotTypeConverters` that can be set to declare a type conversion function that maps a host meta qualified name to a type converter class in an embedded Espresso context.
8+
* Added option`java.PolyglotInterfaceMappings` that can be set to a semicolon-separated list of 1:1 interface type mappings to automatically construct guest proxies for host objects that implement declared interfaces in the list.
9+
710
### Internal changes
811
### Noteworthy fixes
912
* Fix some conversions at interop boundaries: when an espresso-to-espresso conversion was seen, then an espresso-to-primitive conversion happens. The latter would fail.

0 commit comments

Comments
 (0)