jaroslav@0
|
1 |
/*
|
jaroslav@0
|
2 |
Java 4 Browser Bytecode Translator
|
jaroslav@0
|
3 |
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@0
|
4 |
|
jaroslav@0
|
5 |
This program is free software: you can redistribute it and/or modify
|
jaroslav@0
|
6 |
it under the terms of the GNU General Public License as published by
|
jaroslav@0
|
7 |
the Free Software Foundation, version 2 of the License.
|
jaroslav@0
|
8 |
|
jaroslav@0
|
9 |
This program is distributed in the hope that it will be useful,
|
jaroslav@0
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@0
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@0
|
12 |
GNU General Public License for more details.
|
jaroslav@0
|
13 |
|
jaroslav@0
|
14 |
You should have received a copy of the GNU General Public License
|
jaroslav@0
|
15 |
along with this program. Look for COPYING file in the top folder.
|
jaroslav@0
|
16 |
If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@0
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@0
|
19 |
|
jaroslav@22
|
20 |
import org.apidesign.vm4brwsr.ByteCodeToJavaScript;
|
jaroslav@0
|
21 |
import java.io.IOException;
|
jaroslav@0
|
22 |
import java.io.InputStream;
|
jaroslav@18
|
23 |
import java.util.Arrays;
|
jaroslav@19
|
24 |
import java.util.HashSet;
|
jaroslav@18
|
25 |
import java.util.Iterator;
|
jaroslav@19
|
26 |
import java.util.LinkedList;
|
jaroslav@18
|
27 |
import java.util.Set;
|
jaroslav@18
|
28 |
import java.util.TreeSet;
|
jaroslav@0
|
29 |
import javax.script.Invocable;
|
jaroslav@0
|
30 |
import javax.script.ScriptEngine;
|
jaroslav@0
|
31 |
import javax.script.ScriptEngineManager;
|
jaroslav@0
|
32 |
import javax.script.ScriptException;
|
jaroslav@0
|
33 |
import static org.testng.Assert.*;
|
jaroslav@0
|
34 |
import org.testng.annotations.Test;
|
jaroslav@0
|
35 |
|
jaroslav@0
|
36 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
37 |
*
|
jaroslav@0
|
38 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
39 |
*/
|
jaroslav@0
|
40 |
public class StaticMethodTest {
|
jaroslav@0
|
41 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
42 |
assertExec(
|
jaroslav@2
|
43 |
"Should be seven",
|
jaroslav@22
|
44 |
"org_apidesign_vm4brwsr_StaticMethod_sumIII",
|
jaroslav@2
|
45 |
Double.valueOf(7),
|
jaroslav@2
|
46 |
3, 4
|
jaroslav@2
|
47 |
);
|
jaroslav@0
|
48 |
}
|
jaroslav@0
|
49 |
|
jaroslav@1
|
50 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
51 |
assertExec(
|
jaroslav@2
|
52 |
"Should be nine",
|
jaroslav@22
|
53 |
"org_apidesign_vm4brwsr_StaticMethod_powerFF",
|
jaroslav@2
|
54 |
Double.valueOf(9),
|
jaroslav@2
|
55 |
3.0f
|
jaroslav@2
|
56 |
);
|
jaroslav@1
|
57 |
}
|
jaroslav@1
|
58 |
|
jaroslav@2
|
59 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
60 |
assertExec(
|
jaroslav@2
|
61 |
"Should be two",
|
jaroslav@22
|
62 |
"org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
|
jaroslav@2
|
63 |
Double.valueOf(2),
|
jaroslav@2
|
64 |
3.0d, 1l
|
jaroslav@2
|
65 |
);
|
jaroslav@2
|
66 |
}
|
jaroslav@3
|
67 |
|
jaroslav@3
|
68 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
69 |
assertExec(
|
jaroslav@3
|
70 |
"Should be rounded to one",
|
jaroslav@22
|
71 |
"org_apidesign_vm4brwsr_StaticMethod_divIBD",
|
jaroslav@3
|
72 |
Double.valueOf(1),
|
jaroslav@3
|
73 |
3, 3.75
|
jaroslav@3
|
74 |
);
|
jaroslav@3
|
75 |
}
|
jaroslav@3
|
76 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
77 |
assertExec(
|
jaroslav@3
|
78 |
"Should be two",
|
jaroslav@22
|
79 |
"org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
|
jaroslav@3
|
80 |
Double.valueOf(20),
|
jaroslav@3
|
81 |
2, 10l, 5, 2.0
|
jaroslav@3
|
82 |
);
|
jaroslav@3
|
83 |
}
|
jaroslav@4
|
84 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
85 |
assertExec(
|
jaroslav@4
|
86 |
"Factorial of 5 is 120",
|
jaroslav@22
|
87 |
"org_apidesign_vm4brwsr_StaticMethod_factRecJI",
|
jaroslav@4
|
88 |
Double.valueOf(120),
|
jaroslav@4
|
89 |
5
|
jaroslav@4
|
90 |
);
|
jaroslav@4
|
91 |
}
|
jaroslav@5
|
92 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
93 |
assertExec(
|
jaroslav@5
|
94 |
"Factorial of 5 is 120",
|
jaroslav@22
|
95 |
"org_apidesign_vm4brwsr_StaticMethod_factIterJI",
|
jaroslav@5
|
96 |
Double.valueOf(120),
|
jaroslav@5
|
97 |
5
|
jaroslav@5
|
98 |
);
|
jaroslav@5
|
99 |
}
|
jaroslav@2
|
100 |
|
jaroslav@6
|
101 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
102 |
assertExec(
|
jaroslav@6
|
103 |
"Xor is 4",
|
jaroslav@22
|
104 |
"org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
|
jaroslav@6
|
105 |
Double.valueOf(4),
|
jaroslav@6
|
106 |
7,
|
jaroslav@6
|
107 |
3
|
jaroslav@6
|
108 |
);
|
jaroslav@6
|
109 |
}
|
jaroslav@6
|
110 |
|
jaroslav@7
|
111 |
@Test public void or() throws Exception {
|
jaroslav@7
|
112 |
assertExec(
|
jaroslav@7
|
113 |
"Or will be 7",
|
jaroslav@22
|
114 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
115 |
Double.valueOf(7),
|
jaroslav@7
|
116 |
true,
|
jaroslav@7
|
117 |
4,
|
jaroslav@7
|
118 |
3
|
jaroslav@7
|
119 |
);
|
jaroslav@7
|
120 |
}
|
jaroslav@7
|
121 |
@Test public void and() throws Exception {
|
jaroslav@7
|
122 |
assertExec(
|
jaroslav@7
|
123 |
"And will be 3",
|
jaroslav@22
|
124 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
125 |
Double.valueOf(3),
|
jaroslav@7
|
126 |
false,
|
jaroslav@7
|
127 |
7,
|
jaroslav@7
|
128 |
3
|
jaroslav@7
|
129 |
);
|
jaroslav@7
|
130 |
}
|
jaroslav@9
|
131 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
132 |
assertExec(
|
jaroslav@9
|
133 |
"It will be 4",
|
jaroslav@22
|
134 |
"org_apidesign_vm4brwsr_StaticMethod_inc4I",
|
jaroslav@9
|
135 |
Double.valueOf(4)
|
jaroslav@9
|
136 |
);
|
jaroslav@9
|
137 |
}
|
jaroslav@7
|
138 |
|
jaroslav@2
|
139 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@2
|
140 |
StringBuilder sb = new StringBuilder();
|
jaroslav@22
|
141 |
Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
|
jaroslav@2
|
142 |
|
jaroslav@2
|
143 |
Object ret = null;
|
jaroslav@2
|
144 |
try {
|
jaroslav@2
|
145 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@5
|
146 |
} catch (ScriptException ex) {
|
jaroslav@5
|
147 |
fail("Execution failed in " + sb, ex);
|
jaroslav@2
|
148 |
} catch (NoSuchMethodException ex) {
|
jaroslav@2
|
149 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@2
|
150 |
}
|
jaroslav@2
|
151 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
152 |
return;
|
jaroslav@2
|
153 |
}
|
jaroslav@2
|
154 |
if (expRes.equals(ret)) {
|
jaroslav@2
|
155 |
return;
|
jaroslav@2
|
156 |
}
|
jaroslav@3
|
157 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@2
|
158 |
|
jaroslav@2
|
159 |
}
|
jaroslav@2
|
160 |
|
jaroslav@13
|
161 |
static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@19
|
162 |
if (sb == null) {
|
jaroslav@19
|
163 |
sb = new StringBuilder();
|
jaroslav@19
|
164 |
}
|
jaroslav@19
|
165 |
Set<String> processed = new HashSet<String>();
|
jaroslav@19
|
166 |
|
jaroslav@19
|
167 |
LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
|
jaroslav@19
|
168 |
for (;;) {
|
jaroslav@19
|
169 |
toProcess.removeAll(processed);
|
jaroslav@19
|
170 |
if (toProcess.isEmpty()) {
|
jaroslav@19
|
171 |
break;
|
jaroslav@19
|
172 |
}
|
jaroslav@19
|
173 |
String name = toProcess.getFirst();
|
jaroslav@19
|
174 |
processed.add(name);
|
jaroslav@19
|
175 |
if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
|
jaroslav@19
|
176 |
continue;
|
jaroslav@19
|
177 |
}
|
jaroslav@18
|
178 |
InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
|
jaroslav@13
|
179 |
assertNotNull(is, "Class file found");
|
jaroslav@20
|
180 |
try {
|
jaroslav@20
|
181 |
ByteCodeToJavaScript.compile(is, sb, toProcess);
|
jaroslav@20
|
182 |
} catch (RuntimeException ex) {
|
jaroslav@20
|
183 |
int lastBlock = sb.lastIndexOf("{");
|
jaroslav@20
|
184 |
throw new IllegalStateException(
|
jaroslav@20
|
185 |
"Error while compiling " + name + "\n" +
|
jaroslav@21
|
186 |
sb.substring(0, sb.length()),
|
jaroslav@20
|
187 |
ex
|
jaroslav@20
|
188 |
);
|
jaroslav@20
|
189 |
}
|
jaroslav@2
|
190 |
}
|
jaroslav@0
|
191 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
192 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@0
|
193 |
try {
|
jaroslav@0
|
194 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
195 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
196 |
return (Invocable)js;
|
jaroslav@0
|
197 |
} catch (ScriptException ex) {
|
jaroslav@0
|
198 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@0
|
199 |
return null;
|
jaroslav@0
|
200 |
}
|
jaroslav@0
|
201 |
}
|
jaroslav@0
|
202 |
}
|