Skip to content

Commit 9f8ff77

Browse files
committed
[GR-19691] Document how to install other languages in the JVM Standalone
PullRequest: truffleruby/4011
2 parents 4887c72 + b45c57a commit 9f8ff77

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

doc/user/polyglot.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ The JVM configuration automatically has access to other languages.
2323
* [Threading and interop](#threading-and-interop)
2424
* [Embedded configuration](#embedded-configuration)
2525

26+
## Installing Other Languages
27+
28+
To use other GraalVM languages, you need the [JVM Standalone](../../README.md#getting-started).
29+
The Native Standalone does not support installing extra languages.
30+
31+
Note that `ruby`, `llvm`, the LLVM toolchain and host Java interop are available without installing anything extra.
32+
33+
Then you can install other languages with `truffleruby-polyglot-get $LANGUAGE`, for instance:
34+
```bash
35+
truffleruby-polyglot-get js
36+
truffleruby-polyglot-get python
37+
truffleruby-polyglot-get wasm
38+
truffleruby-polyglot-get java # for Java on Truffle (aka Espresso)
39+
```
40+
41+
In TruffleRuby versions before 23.1 this was done through installing GraalVM (e.g. via `truffleruby+graalvm`) and using `gu install $LANGUAGE`.
42+
2643
## Running Ruby Code from Another Language
2744

2845
When you `eval` Ruby code from the [Context API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/Context.html) in another language and mark the `Source` as interactive, the same interactive top-level binding is used each time.
@@ -33,6 +50,8 @@ This is similar to most REPL semantics.
3350

3451
## Loading Code Written in Foreign Languages
3552

53+
Note the ruby command line needs to be passed `--polyglot` to enable access to foreign languages.
54+
3655
`Polyglot.eval(id, string)` executes code in a foreign language identified by its ID.
3756

3857
`Polyglot.eval_file(id, path)` executes code in a foreign language from a file, identified by its language ID.
@@ -249,3 +268,19 @@ Also, the experimental option `--cexts=false` can disable C extensions.
249268

250269
Note: Unlike for example pure JavaScript, Ruby is more than a self-contained expression language.
251270
It has a large core library that includes low-level I/O and system and native-memory routines which may interfere with other embedded contexts or the host system.
271+
272+
## Inner Contexts
273+
274+
TruffleRuby supports creating *inner contexts*, that is multiple isolated execution/evaluation contexts (this is generally supported in GraalVM languages).
275+
Conceptually it is similar to running multiple Ruby interpreters in the same process.
276+
This can also be used with other languages, so for instance the outer/default context might run some Ruby code and some inner contexts run JavaScript code.
277+
This is very useful to interoperate with languages which do not support shared-memory multithreading like JavaScript, as one can then create one or more inner contexts per thread and still have the outer context use multithreaded Ruby.
278+
279+
Objects from an inner context can be passed to other contexts and they are treated as foreign objects:
280+
```ruby
281+
Polyglot::InnerContext.new do |context|
282+
context.eval('ruby', "p Object.new") # prints #<Object:0xd8>
283+
p context.eval('ruby', "Object.new") # prints #<Polyglot::ForeignObject[Ruby] Object:0x131d576b>
284+
end
285+
```
286+
This works by automatically wrapping every object leaving its context in a proxy, which means e.g. if a method is called on a foreign object it is executed in the context to which the object belongs.

0 commit comments

Comments
 (0)