Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Last active August 30, 2019 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-simons/f475dd7f52af9b3a653e82b715297f1a to your computer and use it in GitHub Desktop.
Save michael-simons/f475dd7f52af9b3a653e82b715297f1a to your computer and use it in GitHub Desktop.
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