Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS, Ruby, Python and R versions of Graal Values that are immediately executable
package scripts;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.junit.jupiter.api.Test;
/**
* Needs to be run on a GraalVM SDK with the following components installed through {@code gu}
* <pre>
* gu install ruby
* gu install python
* gu install R
* </pre>
*
* Result should be like
* <pre>
* gu list
* ComponentId Version Component name Origin
* --------------------------------------------------------------------------------
* graalvm 19.0.2 GraalVM Core
* R 19.0.2 FastR github.com
* native-image 19.0.2 Native Image github.com
* python 19.0.2 Graal.Python github.com
* ruby 19.0.2 TruffleRuby github.com
* </pre>
*
* The only dependency one need is then
* <pre>
* &lt;dependency&gt;
* &lt;groupId&gt;org.graalvm.sdk&lt;/groupId&gt;
* &lt;artifactId&gt;graal-sdk&lt;/artifactId&gt;
* &lt;version&gt;${graal.version}&lt;/version&gt;
* &lt;/dependency&gt;
* </pre>
*/
public class ExecutableSyntaxTest {
private static final Context context = PolyglotContext.newInstance();
@Test
void js() {
Value executable = context.eval("js", "(str) => 'Hallo, ' + str");
assertTrue(executable.canExecute());
Value r = executable.execute("Michael");
assertEquals("Hallo, Michael", r.asString());
}
@Test
void ruby() {
Value executable = context.eval("ruby", "->(str) {'Hallo, ' + str}");
assertTrue(executable.canExecute());
Value r = executable.execute("Michael");
assertEquals("Hallo, Michael", r.asString());
}
@Test
void python() {
Value executable = context.eval("python", "lambda str: 'Hallo, ' + str");
assertTrue(executable.canExecute());
Value r = executable.execute("Michael");
assertEquals("Hallo, Michael", r.asString());
}
@Test
void r() {
Value executable = context.eval("R", "(function (str) paste('Hallo,', str))");
assertTrue(executable.canExecute());
Value r = executable.execute("Michael");
assertEquals("Hallo, Michael", r.asString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.