Creating Bck2Brwsr entrypoint to for those who wish to generate their JavaScript based Java VM
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 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;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import static org.testng.Assert.*;
25 import javax.script.Invocable;
26 import org.testng.annotations.BeforeClass;
27 import org.testng.annotations.Test;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 public class VMinVMTest {
35 private static CharSequence codeSeq;
36 private static Invocable code;
38 @Test public void compareTheGeneratedCode() throws Exception {
39 byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class");
40 String ret1 = VMinVM.toJavaScript(arr);
44 ret = code.invokeFunction("bck2brwsr");
45 ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
46 ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
47 } catch (Exception ex) {
48 File f = File.createTempFile("execution", ".js");
49 FileWriter w = new FileWriter(f);
50 w.append("var byteCode = [\n ");
52 for (int i = 0; i < arr.length; i++) {
53 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
62 throw new Exception(ex.getMessage() + " file: " + f, ex);
66 assertTrue(ret instanceof String, "It is string: " + ret);
68 assertEquals((String)ret, ret1.toString(), "The code is the same");
72 public void compileTheCode() throws Exception {
73 StringBuilder sb = new StringBuilder();
74 code = StaticMethodTest.compileClass(sb,
75 "org/apidesign/vm4brwsr/VMinVM"
80 private static byte[] readClass(String res) throws IOException {
81 InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
82 assertNotNull(is1, "Stream found");
83 byte[] arr = new byte[is1.available()];
84 int len = is1.read(arr);
86 if (len != arr.length) {
87 throw new IOException("Wrong len " + len + " for arr: " + arr.length);