Understands aconst_null. To achieve that we realized that invokeinterface bytecode is followed by four bytes, not just two.
2 Java 4 Browser Bytecode Translator
3 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. Look for COPYING file in the top folder.
16 If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import javax.script.Invocable;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import javax.script.ScriptException;
25 import static org.testng.Assert.*;
26 import org.testng.annotations.Test;
28 /** Checks the basic behavior of the translator.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class StaticMethodTest {
33 @Test public void threePlusFour() throws Exception {
36 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
42 @Test public void powerOfThree() throws Exception {
45 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
51 @Test public void doubleWithoutLong() throws Exception {
54 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
60 @Test public void divAndRound() throws Exception {
62 "Should be rounded to one",
63 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
68 @Test public void mixedMethodFourParams() throws Exception {
71 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
76 @Test public void factRec() throws Exception {
78 "Factorial of 5 is 120",
79 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
84 @Test public void factIter() throws Exception {
86 "Factorial of 5 is 120",
87 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
93 @Test public void xor() throws Exception {
96 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
103 @Test public void or() throws Exception {
106 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
113 @Test public void nullCheck() throws Exception {
116 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
120 @Test public void and() throws Exception {
123 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
130 @Test public void inc4() throws Exception {
133 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
138 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
139 StringBuilder sb = new StringBuilder();
140 Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
144 ret = i.invokeFunction(methodName, args);
145 } catch (ScriptException ex) {
146 fail("Execution failed in " + sb, ex);
147 } catch (NoSuchMethodException ex) {
148 fail("Cannot find method in " + sb, ex);
150 if (ret == null && expRes == null) {
153 if (expRes != null && expRes.equals(ret)) {
156 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
160 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
162 sb = new StringBuilder();
164 GenJS.compile(sb, names);
165 ScriptEngineManager sem = new ScriptEngineManager();
166 ScriptEngine js = sem.getEngineByExtension("js");
168 Object res = js.eval(sb.toString());
169 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
170 return (Invocable)js;
171 } catch (ScriptException ex) {
172 fail("Could not compile:\n" + sb, ex);