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
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
+
26
43
## Running Ruby Code from Another Language
27
44
28
45
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.
33
50
34
51
## Loading Code Written in Foreign Languages
35
52
53
+
Note the ruby command line needs to be passed `--polyglot` to enable access to foreign languages.
54
+
36
55
`Polyglot.eval(id, string)` executes code in a foreign language identified by its ID.
37
56
38
57
`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.
249
268
250
269
Note: Unlike for example pure JavaScript, Ruby is more than a self-contained expression language.
251
270
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.newdo |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