-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polyglot.java
111 lines (91 loc) · 3.12 KB
/
Polyglot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import org.graalvm.polyglot.*;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
class Polyglot {
public static void main(String[] args) throws Exception {
String[] supportedLangs = { "js", "python"};
Context polyglot = Context.newBuilder(supportedLangs)
.allowAllAccess(true)
.build();
Value v1 = polyglot.eval(Source.newBuilder("js", new File("./tests/arraydef.js")).build());
Value v2 = polyglot.eval(Source.newBuilder("js", new File("./tests/classdef.js")).build());
Value v3 = polyglot.asValue(new TestClass().mutate());
Value v4 = polyglot.eval(Source.newBuilder("js", new File("./tests/complexclassdef.js")).build());
//Value v5 = polyglot.eval(Source.newBuilder("python", new File("./tests/class.py")).build());
//v5.getMember("mutate").execute();
Value v6 = polyglot.asValue(ComplexClass.Init());
TypesDB.register(v1);
TypesDB.register(v2);
TypesDB.register(v3);
TypesDB.register(v4);
TypesDB.register(v4.getMember("child1"));
TypesDB.register(v4.getMember("child1").getMember("grandchild"));
//TypesDB.register(v5);
TypesDB.register(v6);
TypesDB.register(v6.getMember("child1"));
TypesDB.register(v6.getMember("child1").getMember("gc"));
Value v = v6;
System.out.println("--Writing--");
Writer w = new Writer(new FileOutputStream("file.bin"));
w.write(v);
w.close();
System.out.println();
System.out.println();
System.out.println("--Reading--");
Reader r = new Reader(new FileInputStream("file.bin"), polyglot);
Value vout = r.read();
r.close();
System.out.println();
System.out.println();
System.out.println(vout);
}
public static class TestClass {
public static int global = 5;
public String fielda = "test";
private String fieldb = "test2"; //TODO no private fields allowed as of current
public TestClass() {}
public TestClass mutate() {
fielda = "mutated";
fieldb = "mutatedb";
return this;
}
public TestClass construct() {
return new TestClass();
}
public String toString() {
return fielda + " " + fieldb;
}
}
public static class ComplexClass extends ArrayList<Integer> {
public InnerClass child1;
public InnerClass child2;
public ComplexClass construct() {
return new ComplexClass();
}
public static ComplexClass Init() {
ComplexClass out = new ComplexClass();
out.child1 = new InnerClass();
out.child2 = new InnerClass();
out.child1.gc = new GrandchildClass();
out.child2.gc = out.child1.gc;
out.child1.gc.val = 5;
return out;
}
public String toString() {
return "gc equal? " + (child1.gc == child2.gc);
}
public static class InnerClass {
public GrandchildClass gc;
public InnerClass construct() {
return new InnerClass();
}
}
public static class GrandchildClass {
public int val;
public GrandchildClass construct() {
return new GrandchildClass();
}
}
}
}