oracle / truffleruby

A high performance implementation of the Ruby programming language, built on GraalVM.
https://www.graalvm.org/ruby/
Other
3k stars 184 forks source link

Small standalone examples, say five different ones, for polyglot usage with native image, covering basic use case, towards somewhat more sophisticated use case? #2648

Open rubyFeedback opened 2 years ago

rubyFeedback commented 2 years ago

Hey there truffle-ruby devs and users.

I am not sure if I made this suggestion before or not (I tend to often suggest more examples and documentation in many different projects, so it is hard to keep track over all projects), but either way I'll make this suggestion. You lot can then decide whether this has merit.

GraalVM is fairly well-documented, but there is one thing I think is partially missing: and that is ... more examples.

I have gathered a few small examples via ruby + polyglot and these work for the most part (excluding my older problem related to GraalVM running out of memory, but I haven't had this issue yet with the newly released GraalVM, so perhaps it was fixed).

So simple use cases are covered quite ok-ish, but I am wondering: what are people doing after the basic steps?

So I would like to ask fore more advanced, somewhat more sophisticated use cases.

Perhaps this could go into the doc/ subdirectory, and called examples/ or tutorial/ or something like that. Or it may be elsewhere, but mentioned via cross-reference from the main README.md, so that people can find it. But anyway, this is a detail, the more important stuff is that a) it can be found and b) that it exists to begin with.

So, what are these more sophisticated use cases?

Perhaps something like a step-by-step tutorial that would be useful.

So we start with hello world, but then we add a few more examples that are more advanced.

What do I mean with advanced? What could be useful for the end user who is learning to use truffleruby

Well, I think these elements would be useful:

Anyway. These are just some rough ideas. The basic gist of this is to showcase some examples people can try to "get up and running quickly".

As always please feel free to close this issue at any moment in time and thank you for reading this issue request. Sorry for the formatting - I typed this in my local editor. For some reason when I type directly in the textarea here I tend to make a gazillion typos ...

eregon commented 2 years ago

There are polyglot examples at https://github.com/graalvm/examples, notably:

They all run GraalVM in JVM mode (--jvm --polyglot), not Native mode, that's simply for convenience because then there is no need to compile an extra native image with all the required languages, and instead one can use the GraalVM release as-is.

Is there a reason you want to run on Native Image specifically? This tends to be more complicated for polyglot examples, so if there is no specific reason then using JVM mode is simpler, at least to get started.

Also it seems most of your questions are about embedding using the Context/Engine API, that's documented in https://www.graalvm.org/22.1/reference-manual/embed-languages/. Otherwise, the way to answer most of them would be "just like with CRuby". The easiest way to use TruffleRuby is to add it as a Ruby implementation in your PATH, and use the ruby/gem/bundle/etc commands like on any other Ruby. This also makes sense for polyglot programming with then ruby --jvm --polyglot file.rb.

Indeed most of the examples at https://www.graalvm.org/22.1/reference-manual/embed-languages/ are small, but isn't it obvious how to expand them to what you need? I would think most the of Context/Engine/Value API is fairly intuitive, and there is JavaDoc for it: https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html

Embedding + having TruffleRuby in a Native Image + having some Java code compiled as Native is indeed that is less documented, and especially interactions with gems etc as you asked in #2647. That could certainly benefits from better docs. OTOH, that's also the most complicated way to embed, so I wouldn't start there, I would start with GraalVM JVM mode.