vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 28 Nov 2012 13:41:33 +0100
branchlazy
changeset 213 9dc53108d3df
parent 212 597f96a8e998
child 214 a0f4460130b9
permissions -rw-r--r--
Using named function expression allows an easy self reference from inside the function. Plus giving byte code tranlators a chance to customize the field to store the method into
     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();");
    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() {");
    52         sb.append("\n  var self = this;");
    53         sb.append("\n  this.constructor.prototype.loadClass = function(res, name) {");
    54         sb.append("\n    var script = org_apidesign_vm4brwsr_VMLazy(true).toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));");
    55         sb.append("\n    try {");
    56         sb.append("\n      new Function(script)(self, name);");
    57         sb.append("\n    } catch (ex) {");
    58         sb.append("\n      throw 'Cannot compile ' + res + ' error: ' + ex + ' script:\\n' + script;");
    59         sb.append("\n    };");
    60         sb.append("\n    return self[name](true);");
    61         sb.append("\n  };");
    62         sb.append("\n");
    63         sb.append("\n}\n");
    64         
    65         ScriptEngine[] arr = { null };
    66         code = StaticMethodTest.compileClass(sb, arr,
    67             "org/apidesign/vm4brwsr/VMLazy"
    68         );
    69         arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
    70         codeSeq = sb;
    71     }
    72     
    73     @Test public void invokeStaticMethod() throws Exception {
    74         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    75             "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOneI"
    76         );
    77     }
    78     
    79 
    80     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    81         Object ret = null;
    82         try {
    83             ret = code.invokeFunction(methodName, args);
    84         } catch (ScriptException ex) {
    85             fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    86         } catch (NoSuchMethodException ex) {
    87             fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    88         }
    89         if (ret == null && expRes == null) {
    90             return;
    91         }
    92         if (expRes.equals(ret)) {
    93             return;
    94         }
    95         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    96     }
    97 
    98     public static final class FindBytes {
    99         public byte[] get(String name) throws IOException {
   100             InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
   101             if (is == null) {
   102                 throw new IOException("Can't find " + name);
   103             }
   104             byte[] arr = new byte[is.available()];
   105             int len = is.read(arr);
   106             if (len != arr.length) {
   107                 throw new IOException("Read only " + len + " wanting " + arr.length);
   108             }
   109             System.err.print("loader['" + name + "'] = [");
   110             for (int i = 0; i < arr.length; i++) {
   111                 if (i > 0) {
   112                     System.err.print(", ");
   113                 }
   114                 System.err.print(arr[i]);
   115             }
   116             System.err.println("]");
   117             return arr;
   118         }
   119     }
   120 }