vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 09:16:53 +0100
changeset 248 0bfcb6585290
parent 217 14ce86e00bd9
child 277 e4b9eee9be83
permissions -rw-r--r--
Using the same mangling scheme as JNI
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import javax.script.Invocable;
    23 import javax.script.ScriptContext;
    24 import javax.script.ScriptEngine;
    25 import javax.script.ScriptException;
    26 import org.testng.annotations.BeforeClass;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.Test;
    29 
    30 /** Implements loading class by class.
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 public class VMLazyTest {
    35 
    36     
    37     private static CharSequence codeSeq;
    38     private static Invocable code;
    39 
    40     @BeforeClass
    41     public void compileTheCode() throws Exception {
    42         StringBuilder sb = new StringBuilder();
    43         
    44         sb.append("\nfunction test(clazz, as, method) {");
    45         sb.append("\n  var l = new lazyVM(this);");
    46         sb.append("\n  var c = l.loadClass(clazz, as);");
    47         sb.append("\n  return c[method]();");
    48         sb.append("\n}");
    49         
    50         
    51         sb.append("\nfunction lazyVM(global) {");
    52         sb.append("\n  var self = this;");
    53         sb.append("\n  var glb = global;");
    54         sb.append("\n  lazyVM.prototype.loadClass = function(res, name) {");
    55         sb.append("\n    var script = org_apidesign_vm4brwsr_VMLazy(true)."
    56             + "toJavaScript__Ljava_lang_String_2Ljava_lang_Object_2Ljava_lang_Object_2_3B("
    57             + "  glb, self,"
    58             + "  loader.get(res + '.class')"
    59             + ");");
    60         sb.append("\n    try {");
    61         sb.append("\n      new Function(script)(glb, name);");
    62         sb.append("\n    } catch (ex) {");
    63         sb.append("\n      throw 'Cannot compile ' + res + ' error: ' + ex + ' script:\\n' + script;");
    64         sb.append("\n    };");
    65         sb.append("\n    return glb[name](true);");
    66         sb.append("\n  };");
    67         sb.append("\n");
    68         sb.append("\n}\n");
    69         
    70         ScriptEngine[] arr = { null };
    71         code = StaticMethodTest.compileClass(sb, arr,
    72             "org/apidesign/vm4brwsr/VMLazy"
    73         );
    74         arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
    75         codeSeq = sb;
    76     }
    77     
    78     @Test public void invokeStaticMethod() throws Exception {
    79         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    80             "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne__I"
    81         );
    82     }
    83 
    84     @Test public void loadDependantClass() throws Exception {
    85         assertExec("Trying to get zero", "test", Double.valueOf(0),
    86             "org/apidesign/vm4brwsr/InstanceSub", "org_apidesign_vm4brwsr_InstanceSub", "recallDbl__D"
    87         );
    88     }
    89 
    90     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    91         Object ret = null;
    92         try {
    93             ret = code.invokeFunction(methodName, args);
    94         } catch (ScriptException ex) {
    95             fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    96         } catch (NoSuchMethodException ex) {
    97             fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    98         }
    99         if (ret == null && expRes == null) {
   100             return;
   101         }
   102         if (expRes.equals(ret)) {
   103             return;
   104         }
   105         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
   106     }
   107 
   108     public static final class FindBytes {
   109         public byte[] get(String name) throws IOException {
   110             InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
   111             if (is == null) {
   112                 throw new IOException("Can't find " + name);
   113             }
   114             byte[] arr = new byte[is.available()];
   115             int len = is.read(arr);
   116             if (len != arr.length) {
   117                 throw new IOException("Read only " + len + " wanting " + arr.length);
   118             }
   119             /*
   120             System.err.print("loader['" + name + "'] = [");
   121             for (int i = 0; i < arr.length; i++) {
   122                 if (i > 0) {
   123                     System.err.print(", ");
   124                 }
   125                 System.err.print(arr[i]);
   126             }
   127             System.err.println("]");
   128             */
   129             return arr;
   130         }
   131     }
   132 }