Skip to content

Commit 49adfb0

Browse files
committed
support py/c++/r/ruby with OpenJDK
1 parent aa67e9e commit 49adfb0

File tree

17 files changed

+299
-13
lines changed

17 files changed

+299
-13
lines changed

pom.xml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<packaging>pom</packaging>
1717

1818
<properties>
19-
<graalvm.version>22.3.1</graalvm.version>
19+
<graalvm.version>23.1.2</graalvm.version>
2020
<maven.compiler.source>17</maven.compiler.source>
2121
<maven.compiler.target>17</maven.compiler.target>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -35,19 +35,34 @@
3535
<version>${graalvm.version}</version>
3636
</dependency>
3737
<dependency>
38-
<groupId>org.graalvm.js</groupId>
39-
<artifactId>js</artifactId>
38+
<groupId>org.graalvm.polyglot</groupId>
39+
<artifactId>js-community</artifactId>
4040
<version>${graalvm.version}</version>
41+
<type>pom</type>
4142
</dependency>
4243
<dependency>
43-
<groupId>org.graalvm.js</groupId>
44-
<artifactId>js-scriptengine</artifactId>
44+
<groupId>org.graalvm.polyglot</groupId>
45+
<artifactId>python-community</artifactId>
4546
<version>${graalvm.version}</version>
47+
<type>pom</type>
4648
</dependency>
4749
<dependency>
48-
<groupId>com.ibm.icu</groupId>
49-
<artifactId>icu4j</artifactId>
50-
<version>72.1</version>
50+
<groupId>org.graalvm.polyglot</groupId>
51+
<artifactId>ruby-community</artifactId>
52+
<version>${graalvm.version}</version>
53+
<type>pom</type>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.graalvm.polyglot</groupId>
57+
<artifactId>llvm-community</artifactId>
58+
<version>${graalvm.version}</version>
59+
<type>pom</type>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.graalvm.polyglot</groupId>
63+
<artifactId>java-community</artifactId>
64+
<version>${graalvm.version}</version>
65+
<type>pom</type>
5166
</dependency>
5267
</dependencies>
5368
</dependencyManagement>

spring-openjdk/pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,32 @@
2222
<scope>test</scope>
2323
</dependency>
2424
<dependency>
25-
<groupId>org.graalvm.js</groupId>
26-
<artifactId>js</artifactId>
25+
<groupId>org.graalvm.polyglot</groupId>
26+
<artifactId>js-community</artifactId>
27+
<type>pom</type>
28+
<scope>runtime</scope>
2729
</dependency>
2830
<dependency>
29-
<groupId>org.graalvm.js</groupId>
30-
<artifactId>js-scriptengine</artifactId>
31+
<groupId>org.graalvm.polyglot</groupId>
32+
<artifactId>python-community</artifactId>
33+
<type>pom</type>
34+
<scope>runtime</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.graalvm.polyglot</groupId>
38+
<artifactId>ruby-community</artifactId>
39+
<type>pom</type>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.graalvm.polyglot</groupId>
44+
<artifactId>llvm-community</artifactId>
45+
<type>pom</type>
46+
<scope>runtime</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.graalvm.sdk</groupId>
50+
<artifactId>graal-sdk</artifactId>
3151
</dependency>
3252
</dependencies>
3353

spring-openjdk/src/main/java/org/weaxsey/spring/openjdk/ContextExecutorService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ContextExecutorService(Map<String, Context> ctxMap) {
3333

3434
public void executeMain(Source source) {
3535
Context graalCtx = ctxMap.get("graalCtx");
36-
graalCtx.eval(source);
36+
graalCtx.eval(source);
3737
}
3838

3939
public void executeMain(String languageId, CharSequence source) throws IllegalAccessException {

spring-openjdk/src/main/java/org/weaxsey/spring/openjdk/PolyglotContextFactory.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.graalvm.polyglot.Context;
99
import org.graalvm.polyglot.HostAccess;
10+
import org.graalvm.polyglot.io.IOAccess;
1011
import org.springframework.context.annotation.Bean;
1112
import org.springframework.context.annotation.Configuration;
1213

@@ -17,9 +18,44 @@
1718
@Configuration
1819
public class PolyglotContextFactory {
1920

21+
public static final ByteArrayOutputStream PY_OUT = new ByteArrayOutputStream();
22+
public static final ByteArrayOutputStream RB_OUT = new ByteArrayOutputStream();
23+
public static final ByteArrayOutputStream LLVM_OUT = new ByteArrayOutputStream();
2024
public static final ByteArrayOutputStream JS_OUT = new ByteArrayOutputStream();
2125
public static final ByteArrayOutputStream GRAAL_OUT = new ByteArrayOutputStream();
2226

27+
28+
@Bean(destroyMethod = "close")
29+
public Context pythonCtx() {
30+
return Context.newBuilder("python")
31+
.option("python.PosixModuleBackend", "native")
32+
.option("python.ForceImportSite", "true")
33+
34+
// python venv exe path
35+
// .option("python.Executable", "/xxx/.python/venv/bin/exe")
36+
.out(PY_OUT)
37+
.allowAllAccess(true)
38+
.allowIO(IOAccess.ALL)
39+
.build();
40+
}
41+
42+
@Bean(destroyMethod = "close")
43+
public Context rubyCtx() {
44+
return Context.newBuilder("ruby")
45+
.out(RB_OUT)
46+
.allowAllAccess(true)
47+
.build();
48+
}
49+
50+
@Bean(destroyMethod = "close")
51+
public Context llvmCtx() {
52+
return Context.newBuilder("llvm")
53+
.out(LLVM_OUT)
54+
.allowAllAccess(true)
55+
.allowIO(IOAccess.ALL)
56+
.build();
57+
}
58+
2359
@Bean(destroyMethod = "close")
2460
public Context jsCtx() {
2561
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL)

spring-openjdk/src/main/java/org/weaxsey/spring/openjdk/SpringOpenJDKApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public class SpringOpenJDKApplication {
88
public static void main(String[] args) {
99
SpringApplication.run(SpringOpenJDKApplication.class, args);
1010
}
11+
12+
1113
}

spring-openjdk/src/test/java/org/weaxsey/spring/openjdk/Consts.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ public class Consts {
1212
protected static final TextNode ASSERT_OUT = new TextNode("Hello GraalVM!");
1313
protected static final String FUNC_NAME = "hello";
1414

15+
protected static final String PY_MAIN_SCRIPT = "/scripts/hello.py";
16+
protected static final String PY_FUNC_SCRIPT = "/scripts/hello_func.py";
17+
protected static final String CPP_MAIN_SCRIPT = "/scripts/hello";
18+
protected static final String CPP_FUNC_SCRIPT = "/scripts/hello_func";
19+
protected static final String RUBY_MAIN_SCRIPT = "/scripts/hello.rb";
20+
protected static final String RUBY_FUNC_SCRIPT = "/scripts/hello_func.rb";
1521
protected static final String JS_MAIN_SCRIPT = "/scripts/hello.js";
1622
protected static final String JS_FUNC_SCRIPT = "/scripts/hello_func.js";
1723

24+
25+
26+
1827
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2023 Weaxs
3+
*
4+
*/
5+
package org.weaxsey.spring.openjdk;
6+
7+
import org.graalvm.polyglot.Source;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
13+
import java.io.IOException;
14+
import java.net.URL;
15+
16+
17+
@SpringBootTest(classes = SpringOpenJDKApplication.class)
18+
public class CppScriptTest {
19+
20+
private URL mainCppUrl;
21+
private URL funcCppUrl;
22+
23+
@Autowired
24+
private ContextExecutorService service;
25+
26+
@BeforeEach
27+
void init() {
28+
mainCppUrl = getClass().getResource(Consts.CPP_MAIN_SCRIPT);
29+
funcCppUrl = getClass().getResource(Consts.CPP_FUNC_SCRIPT);
30+
}
31+
32+
@Test
33+
void executeMainTestByGraalCtx() throws IOException {
34+
Source source = Source.newBuilder("llvm", mainCppUrl).build();
35+
service.executeMain(source);
36+
}
37+
38+
@Test
39+
void executeFuncTestByGraalCtx() throws IOException {
40+
Source source = Source.newBuilder("llvm", funcCppUrl).build();
41+
service.executeMain(source);
42+
}
43+
44+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2023 Weaxs
3+
*
4+
*/
5+
package org.weaxsey.spring.openjdk;
6+
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import org.graalvm.polyglot.Source;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
15+
import java.io.IOException;
16+
import java.net.URISyntaxException;
17+
import java.net.URL;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
21+
22+
@SpringBootTest(classes = SpringOpenJDKApplication.class)
23+
public class PythonScriptTest {
24+
25+
private URL mainPyUrl;
26+
private URL funcPyUrl;
27+
private String mainPyCode;
28+
private String funcPyCode;
29+
30+
@Autowired
31+
private ContextExecutorService service;
32+
33+
@BeforeEach
34+
void init() throws URISyntaxException, IOException {
35+
mainPyUrl = getClass().getResource(Consts.PY_MAIN_SCRIPT);
36+
funcPyUrl = getClass().getResource(Consts.PY_FUNC_SCRIPT);
37+
mainPyCode = new String(Files.readAllBytes(Path.of(mainPyUrl.toURI())));
38+
funcPyCode = new String(Files.readAllBytes(Path.of(funcPyUrl.toURI())));
39+
}
40+
41+
@Test
42+
void executeMainTestByGraalCtx() throws IOException {
43+
Source source = Source.newBuilder("python", mainPyUrl).build();
44+
service.executeMain(source);
45+
}
46+
47+
@Test
48+
void executeMainTestByPyCtx() throws IllegalAccessException {
49+
service.executeMain("python", mainPyCode);
50+
}
51+
52+
@Test
53+
void executeFuncByGraalCtx() throws IOException {
54+
Source source = Source.newBuilder("python", funcPyUrl).build();
55+
JsonNode ret = service.executeFunc(source, Consts.FUNC_NAME);
56+
Assertions.assertEquals(Consts.ASSERT_OUT, ret);
57+
}
58+
59+
@Test
60+
void executeFuncByPythonCtx() throws IllegalAccessException {
61+
JsonNode ret = service.executeFunc("python", funcPyCode, Consts.FUNC_NAME);
62+
Assertions.assertEquals(Consts.ASSERT_OUT, ret);
63+
}
64+
65+
66+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2023 Weaxs
3+
*
4+
*/
5+
package org.weaxsey.spring.openjdk;
6+
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import org.graalvm.polyglot.Source;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
15+
import java.io.IOException;
16+
import java.net.URISyntaxException;
17+
import java.net.URL;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
21+
22+
@SpringBootTest(classes = SpringOpenJDKApplication.class)
23+
public class RubyScriptTest {
24+
25+
private URL mainRubyUrl;
26+
private URL funcRubyUrl;
27+
private String mainRubyCode;
28+
private String funcRubyCode;
29+
30+
@Autowired
31+
private ContextExecutorService service;
32+
33+
@BeforeEach
34+
void init() throws URISyntaxException, IOException {
35+
mainRubyUrl = getClass().getResource(Consts.RUBY_MAIN_SCRIPT);
36+
funcRubyUrl = getClass().getResource(Consts.RUBY_FUNC_SCRIPT);
37+
mainRubyCode = new String(Files.readAllBytes(Path.of(mainRubyUrl.toURI())));
38+
funcRubyCode = new String(Files.readAllBytes(Path.of(funcRubyUrl.toURI())));
39+
}
40+
41+
@Test
42+
void executeMainTestByGraalCtx() throws IOException {
43+
Source source = Source.newBuilder("ruby", mainRubyUrl).build();
44+
service.executeMain(source);
45+
}
46+
47+
@Test
48+
void executeMainTestByRubyCtx() throws IllegalAccessException {
49+
service.executeMain("ruby", mainRubyCode);
50+
}
51+
52+
@Test
53+
void executeFuncByGraalCtx() throws IOException {
54+
Source source = Source.newBuilder("ruby", funcRubyUrl).build();
55+
JsonNode ret = service.executeFunc(source, Consts.FUNC_NAME);
56+
Assertions.assertEquals(Consts.ASSERT_OUT, ret);
57+
}
58+
59+
@Test
60+
void executeFuncByRubyCtx() throws IllegalAccessException {
61+
JsonNode ret = service.executeFunc("ruby", funcRubyCode, Consts.FUNC_NAME);
62+
Assertions.assertEquals(Consts.ASSERT_OUT, ret);
63+
}
64+
65+
}
178 KB
Binary file not shown.

0 commit comments

Comments
 (0)