Skip to content

Commit 65c5480

Browse files
committed
Improve interop docs
1 parent 0b93b97 commit 65c5480

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

docs/user/Interoperability.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,15 @@ Ruby, and LLVM. GraalVM provides a Python API to interact with other languages
55
available on the GraalVM. In fact, GraalVM uses this API internally to
66
execute Python C extensions using the LLVM implementation in GraalVM.
77

8-
You can import the `polyglot` module to interact with other languages.
8+
You can import the `polyglot` module to interact with other languages. In order
9+
to use the polyglot functionality, you need to specify `--jvm --polyglot`
10+
arguments to the `graalpython` executable.
911

1012
```python
1113
import polyglot
1214
```
1315

14-
You can import a global value from the entire polyglot scope:
15-
```python
16-
imported_polyglot_global = polyglot.import_value("global_name")
17-
```
18-
19-
This global should then work as expected; accessing attributes assumes it reads
20-
from the `members` namespace; accessing items is supported both with strings and
21-
numbers; calling methods on the result tries to do a straight invoke and falls
22-
back to reading the member and trying to execute it.
23-
24-
You can evaluate some code in another language:
16+
## Evaluating code in other languages
2517
```python
2618
polyglot.eval(string="1 + 1", language="ruby")
2719
```
@@ -36,6 +28,7 @@ If you pass a file, you can also try to rely on the file-based language detectio
3628
polyglot.eval(path="./my_ruby_file.rb")
3729
```
3830

31+
## Exporting and importing values
3932
To export something from Python to other Polyglot languages so they can import
4033
it:
4134
```python
@@ -51,6 +44,19 @@ def python_method():
5144
return "Hello from Python!"
5245
```
5346

47+
You can import a global value from the entire polyglot scope (exported from
48+
another language):
49+
```python
50+
imported_polyglot_global = polyglot.import_value("global_name")
51+
```
52+
53+
This global should then work as expected; accessing attributes assumes it reads
54+
from the `members` namespace; accessing items is supported both with strings and
55+
numbers; calling methods on the result tries to do a straight invoke and falls
56+
back to reading the member and trying to execute it.
57+
58+
59+
## Examples
5460
Here is an example of how to use JavaScript regular expression engine to
5561
match Python strings. Save this code to the `polyglot_example.py` file:
5662

@@ -135,7 +141,7 @@ the `java` module:
135141
```python
136142
import java
137143
BigInteger = java.type("java.math.BigInteger")
138-
myBigInt = BigInteger(42)
144+
myBigInt = BigInteger.valueOf(42)
139145
myBigInt.shiftLeft(128) # public Java methods can just be called
140146
myBigInt["not"]() # Java method names that are keywords in
141147
# Python can be accessed using "[]"
@@ -144,7 +150,8 @@ print(list(byteArray)) # Java arrays can act like Python lists
144150
```
145151

146152
For packages under the `java` package, you can also use the normal Python import
147-
syntax:
153+
syntax. This syntax is limited to importing concrete classes, it doesn't work
154+
on packages, unless in [Jython Compatibility](Jython.md) mode.
148155
```python
149156
import java.util.ArrayList
150157
from java.util import ArrayList
@@ -179,5 +186,5 @@ print(java.is_function(my_list.add))# prints True, the add method of ArrayList
179186
print(java.instanceof(my_list, ArrayList)) # prints True
180187
```
181188

182-
See the [Polyglot Programming](https://www.graalvm.org/docs/reference-manual/polyglot-programming/) and the [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) reference
189+
See the [Polyglot Programming](https://www.graalvm.org/reference-manual/polyglot-programming/) and the [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) reference
183190
for more information about interoperability with other programming languages.

0 commit comments

Comments
 (0)