Last active
August 30, 2019 09:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
* <dependency> | |
* <groupId>org.graalvm.sdk</groupId> | |
* <artifactId>graal-sdk</artifactId> | |
* <version>${graal.version}</version> | |
* </dependency> | |
* </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