-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.java
More file actions
136 lines (120 loc) · 4.53 KB
/
Generator.java
File metadata and controls
136 lines (120 loc) · 4.53 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.*;
import java.io.*;
import org.graalvm.polyglot.*;
public class Generator {
/*public static void main(String[] args) throws Exception {
Context polyglot = Context.create();
Value prototype = polyglot.eval("js", "[1,2,42,4]");
Generator test = new Generator(prototype);
test.generateClientStub();
test.generateServerStub();
}*/
public static void main(String[] args) throws Exception {
//src folder, package name, dest folder
String[] supportedLangs = { "js", "python", "ruby" };
Context polyglot = Context.newBuilder(supportedLangs).allowAllAccess(true).build();
polyglot.eval("js", "[1,2,42,4]");
Value v2 = polyglot.eval(Source.newBuilder("js", new File("./tests/classdef.js")).build());
//Value v3 = polyglot.eval(Source.newBuilder("python", new File("./tests/class.py")).build());
Value v4 = polyglot.eval(Source.newBuilder("ruby", new File("./tests/class.rb")).build());
//Value v = polyglot.getBindings("js");
//System.out.println(v.getMemberKeys());
//System.out.println(v.getMember("testjs").newInstance());
//Value v = polyglot.getBindings("python");
//Value test = v.getMember("Pythontest");
Value v = polyglot.getBindings("ruby");
System.out.println(v.getMember("class"));
System.out.println(v4);
}
public Generator(Value prototype) {
this.prototype = prototype;
}
private PrintStream out;
private Value prototype;
private int indentLevel;
public void generateClientStub() throws FileNotFoundException {
//TODO directory for package name
this.out = new PrintStream(className() + "ClientStub.java");
if(prototype.isHostObject()) {
generateJava();
} else {
generateGuest();
}
}
public void generateGuest() {
//TODO package
println("import org.graalvm.polyglot.*");
println("import java.io.ByteArrayOutputStream;");
println("public final class " + className() + "ClientStub {"); //TODO implements interface?
indentLevel++;
println("public long oid;");
println("public Context c;");
for(String m : prototype.getMemberKeys()) {
if(prototype.getMember(m).canExecute()) {
println("public Object " + m + "(Object... args) {");
indentLevel++;
println("ByteArrayOutputStream bos = new ByteArrayOutputStream();");
println("new Writer(bos).write(c.asValue(args));");
println("byte[] params = bos.toByteArray();");
println(""); //invoke on kernel with oid, m and params
indentLevel--;
println("}");
}
}
indentLevel--;
println("}");
}
//we can get arg types for java
public void generateJava() {
//TODO: for now we just use weak typing
generateGuest();
}
public void generateServerStub() throws FileNotFoundException {
//TODO directory for package name
this.out = new PrintStream(className() + "ServerStub.java");
//TODO appobject package
println("import org.graalvm.polyglot.*");
println("import java.io.ByteArrayOutputStream;");
println("public final class " + className() + "ServerStub implements AppObject {");
indentLevel++;
println("public Value obj;");
println("public Context c;");
println("public byte[] invoke(String method, byte[] parambytes) {");
indentLevel++;
println("Object[] params = (Object[])new Reader(new ByteArrayInputStream(params), c).read().asHostObject();");
println("Value ret;");
println("switch(method) {");
indentLevel++;
for(String m : prototype.getMemberKeys()) {
if(prototype.getMember(m).canExecute()) {
println("case \"" + m + "\":");
println("ret = obj.getMember(" + m + ").execute(params);");
println("break;");
}
}
indentLevel--;
println("}"); //switch
println("ByteArrayOutputStream bos = new ByteArrayOutputStream();");
println("new Writer(bos).write(ret);");
println("return bos.toByteArray();");
indentLevel--;
println("}"); //invoke
indentLevel--;
println("}"); //class
}
private String packageName() {
String fullname = prototype.getMetaObject().toString();
return fullname.substring(0, fullname.lastIndexOf(".") + 1).trim();
}
private String className() {
String fullname = prototype.getMetaObject().toString();
return fullname.substring(fullname.lastIndexOf(".") + 1).trim();
}
private void println(String s) {
String[] lines = s.split("\n");
String tabs = "\t\t\t\t\t\t\t\t\t\t\t\t".substring(0,indentLevel);
for(String l : lines) {
out.println(tabs + l);
}
}
}