You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference-manual/embedding/embed-languages.md
+31-49Lines changed: 31 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -69,7 +69,8 @@ To use community-licensed versions instead, add the `-community` suffix to each
69
69
To access [polyglot isolate](#polyglot-isolates) artifacts, use the `-isolate` suffix instead (for example, `js-isolate`).
70
70
71
71
The artifacts `languages` and `tools` include all available languages and tools as dependencies.
72
-
This artifact might grow or shrink between major releases. We recommend selecting only the needed language(s) for a production deployment.
72
+
This artifact might grow or shrink between major releases.
73
+
We recommend selecting only the needed language(s) for a production deployment.
73
74
74
75
Additionally, your _module-info.java_ file should require `org.graalvm.polyglot` when using Java modules:
75
76
```java
@@ -81,7 +82,7 @@ module com.mycompany.app {
81
82
Whether your configuration can run with a Truffle runtime optimization depends on the GraalVM JDK you use.
82
83
For further details, refer to the [Runtime Compilation section](#runtime-optimization-support).
83
84
84
-
We recommend configuring polyglot embeddings using modules and the module path whenever possible.
85
+
We recommend configuring polyglot embeddings using modules and the module path whenever possible.
85
86
Be aware that using `org.graalvm.polyglot` from the class path instead will enable access to unsafe APIs for all libraries on the class path.
86
87
If the application is not yet modularized, hybrid use of the class path and module path is possible.
87
88
For example:
@@ -124,17 +125,16 @@ You can use this application with other code examples to demonstrate more advanc
124
125
125
126
Polyglot applications let you take values from one programming language and use them with other languages.
126
127
127
-
Use the code example in this section with your polyglot application to show how the Polyglot API can return JavaScript, R, Ruby, or Python functions as Java values.
128
+
Use the code example in this section with your polyglot application to show how the Polyglot API can return JavaScript, Python, or Ruby functions as Java values.
- `Value result` is an Object that contains three members: a number named `id`,
162
-
a string named `text`, and an array named `arr`.
163
-
- The first assertion verifies that the return value can contain members, which
164
-
indicates that the value is an object-like structure.
165
-
- The `id` variable is initialized by reading the member with the name `id` from
166
-
the resulting object. The result is then converted to a Java `int`
167
-
using `asInt()`.
159
+
In this code:
160
+
- `Value result` is an Object that contains three members: a number named `id`, a string named `text`, and an array named `arr`.
161
+
- The first assertion verifies that the return value can contain members, which indicates that the value is an object-like structure.
162
+
- The `id` variable is initialized by reading the member with the name `id` from the resulting object. The result is then converted to a Java `int` using `asInt()`.
168
163
- The next assert verifies that result has a value of `42`.
169
-
- The `text` variable is initialized using the value of the member `text`,
170
-
which is also converted to a Java `String` using `asString()`.
171
-
- The following assertion verifies the result value is equal to the
172
-
Java `String``"42"`.
164
+
- The `text` variable is initialized using the value of the member `text`, which is also converted to a Java `String` using `asString()`.
165
+
- The following assertion verifies the result value is equal to the Java `String``"42"`.
173
166
- Next the `arr` member that holds an array is read.
174
-
- Arrays return`true`for`hasArrayElements`. R array instances can have
175
-
members and array elements at the same time.
176
-
- The next assertion verifies that the size of the array equals three. The
177
-
Polyglot API supports big arrays, so the array length is of type`long`.
178
-
- Finally we verify that the array element at index `1` equals `42`. Array
179
-
indexing with polyglot values is always zero-based, even for languages such as
180
-
R where indices start with one.
167
+
- Arrays return`true`for`hasArrayElements`.
168
+
- The next assertion verifies that the size of the array equals three. The Polyglot API supports big arrays, so the array length is of type`long`.
169
+
- Finally we verify that the array element at index `1` equals `42`. Array indexing with polyglot values is always zero-based, even for languages where indices start with one.
181
170
182
171
## Access Java from Guest Languages
183
172
@@ -193,12 +182,11 @@ Use the code example in this section with your polyglot application to show how
- The Java class `ComputedArray` implements the proxy interface `ProxyArray` so
271
256
that guest languages treat instances of the Java class-like arrays.
272
257
- `ComputedArray` array overrides the method `get` and computes the value
@@ -279,7 +264,7 @@ an `UnsupportedOperationException` in the implementation of `set`.
279
264
- The guest language script imports the `arr` symbol, which returns the
280
265
exported proxy.
281
266
- The second element and the `1000000000`th element is accessed, summed up, and
282
-
then returned. Note that array indices from 1-based languages such as R are
267
+
then returned. Note that array indices from 1-based languages are
283
268
converted to 0-based indices for proxy arrays.
284
269
- The result of the language script is returned as a long value and verified.
285
270
@@ -295,15 +280,15 @@ These restrictions can be lifted entirely by setting `allowAllAccess` to `true`.
295
280
### Controlling Access to Host Functions
296
281
297
282
It might be desirable to limit the access of guest applications to the host.
298
-
For example, if a Java method is exposed that calls `System.exit`then the guest application will be able to exit the host process.
283
+
For example, if a Java method is exposed that calls `System.exit`,then the guest application will be able to exit the host process.
299
284
In order to avoid accidentally exposed methods, no host access is allowed by default and every public method or field needs to be annotated with `@HostAccess.Export` explicitly.
- The class `Employee` is declared with a field `name` of type`String`. Access to the `getName` method is explicitly allowed by annotating the method with `@HostAccess.Export`.
308
293
- The `Services` class exposes two methods, `createEmployee` and `exitVM`. The `createEmployee` method takes the name of the employee as an argument and creates a new `Employee` instance. The `createEmployee` method is annotated with `@HostAccess.Export` and therefore accessible to the guest application. The `exitVM` method is not explicitly exported and therefore not accessible.
309
294
- The `main` method first creates a new polyglot context in the default configuration, disallowing host access except for methods annotated with `@HostAccess.Export`.
@@ -708,9 +693,8 @@ For example, a dependency on isolated JavaScript can be configured by adding a M
708
693
```
709
694
710
695
Starting from the Polyglot API version 24.1.0, the polyglot engine supports polyglot isolates for individual platforms.
711
-
To download a polyglot isolate for a specific platform, append the operating system and
712
-
CPU architecture classifiers to the polyglot isolate Maven `artifactId`. For example,
713
-
to configure a dependency on isolated Python for Linux amd64, add the following Maven dependencies:
696
+
To download a polyglot isolate for a specific platform, append the operating system and CPU architecture classifiers to the polyglot isolate Maven `artifactId`.
697
+
For example, to configure a dependency on isolated Python for Linux amd64, add the following Maven dependencies:
For a complete Maven POM file that adds the polyglot isolate Native Image dependency for the current platform,
738
-
refer to the [Polyglot Embedding Demonstration](https://github.com/graalvm/polyglot-embedding-demo) on GitHub.
739
-
721
+
For a complete Maven POM file that adds the polyglot isolate Native Image dependency for the current platform, refer to the [Polyglot Embedding Demonstration](https://github.com/graalvm/polyglot-embedding-demo) on GitHub.
740
722
741
723
To enable isolate usage with the Polyglot API, the `--engine.SpawnIsolate=true` option must be passed to `Engine` or `Context` when constructed.
742
724
The option `engine.SpawnIsolate` may not be available if used on any JDK other than Oracle GraalVM.
@@ -834,7 +816,7 @@ In Linux environments that support Memory Protection Keys, the `--engine.MemoryP
834
816
If an engine is created with this option, a dedicated protection key will be allocated for the isolated engine's heap.
835
817
GraalVM only enables access to the engine's heap when executing code of the Polyglot Isolate.
836
818
837
-
## Embed Guest Languages in Java
819
+
## Embed a Guest Language in Java
838
820
839
821
The GraalVM Polyglot API can be used from within a guest language using Java interoperability.
840
822
This can be useful if a script needs to run isolated from the parent context.
@@ -911,7 +893,7 @@ for (;;) {
911
893
912
894
## Step Through with Execution Listeners
913
895
914
-
The GraalVM Polyglot API allows users to instrument the execution of guest languages through [ExecutionListener class](http://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/management/ExecutionListener.html).
896
+
The GraalVM Polyglot API allows users to instrument the execution of guest languages through the [ExecutionListener class](http://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/management/ExecutionListener.html).
915
897
For example, it lets you attach an execution listener that is invoked for every statement of the guest language program.
916
898
Execution listeners are designed as simple API for polyglot embedders and may become handy in, for example, single-stepping through the program.
0 commit comments