Initial test to show how an incremental compilation could look like lazy
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Nov 2012 21:24:03 +0100
branchlazy
changeset 201f180d72cc7a4
parent 200 227bafe6ef52
child 202 d059e6eccb80
Initial test to show how an incremental compilation could look like
vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
vm/src/test/java/org/apidesign/vm4brwsr/VMLazy.java
vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Sun Nov 25 21:23:06 2012 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Sun Nov 25 21:24:03 2012 +0100
     1.3 @@ -271,12 +271,20 @@
     1.4      }
     1.5  
     1.6      static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
     1.7 +        return compileClass(sb, null, names);
     1.8 +    }
     1.9 +    static Invocable compileClass(
    1.10 +        StringBuilder sb, ScriptEngine[] eng, String... names
    1.11 +    ) throws ScriptException, IOException {
    1.12          if (sb == null) {
    1.13              sb = new StringBuilder();
    1.14          }
    1.15          GenJS.compile(sb, names);
    1.16          ScriptEngineManager sem = new ScriptEngineManager();
    1.17          ScriptEngine js = sem.getEngineByExtension("js");
    1.18 +        if (eng != null) {
    1.19 +            eng[0] = js;
    1.20 +        }
    1.21          try {
    1.22              Object res = js.eval(sb.toString());
    1.23              assertTrue(js instanceof Invocable, "It is invocable object: " + res);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazy.java	Sun Nov 25 21:24:03 2012 +0100
     2.3 @@ -0,0 +1,46 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.vm4brwsr;
    2.22 +
    2.23 +import java.io.ByteArrayInputStream;
    2.24 +import java.io.IOException;
    2.25 +
    2.26 +/**
    2.27 + *
    2.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.29 + */
    2.30 +class VMLazy extends ByteCodeToJavaScript {
    2.31 +    private VMLazy(Appendable out) {
    2.32 +        super(out);
    2.33 +    }
    2.34 +    
    2.35 +    static String toJavaScript(byte[] is) throws IOException {
    2.36 +        StringBuilder sb = new StringBuilder();
    2.37 +        new VMLazy(sb).compile(new ByteArrayInputStream(is));
    2.38 +        return sb.toString().toString();
    2.39 +    }
    2.40 +
    2.41 +    @Override
    2.42 +    protected boolean requireReference(String internalClassName) {
    2.43 +        return false;
    2.44 +    }
    2.45 +
    2.46 +    @Override
    2.47 +    protected void requireScript(String resourcePath) {
    2.48 +    }
    2.49 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java	Sun Nov 25 21:24:03 2012 +0100
     3.3 @@ -0,0 +1,124 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr;
    3.22 +
    3.23 +import java.io.IOException;
    3.24 +import java.io.InputStream;
    3.25 +import javax.script.Invocable;
    3.26 +import javax.script.ScriptContext;
    3.27 +import javax.script.ScriptEngine;
    3.28 +import javax.script.ScriptException;
    3.29 +import org.testng.annotations.BeforeClass;
    3.30 +import static org.testng.Assert.*;
    3.31 +import org.testng.annotations.Test;
    3.32 +
    3.33 +/** Implements loading class by class.
    3.34 + *
    3.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.36 + */
    3.37 +public class VMLazyTest {
    3.38 +
    3.39 +    
    3.40 +    private static CharSequence codeSeq;
    3.41 +    private static Invocable code;
    3.42 +
    3.43 +    @BeforeClass
    3.44 +    public void compileTheCode() throws Exception {
    3.45 +        StringBuilder sb = new StringBuilder();
    3.46 +        
    3.47 +        sb.append("\nfunction test(clazz, as, method) {");
    3.48 +        sb.append("\n  var l = new lazyVM();");
    3.49 +        sb.append("\n  var c = l.loadClass(clazz, as);");
    3.50 +        sb.append("\n  return c[method]();");
    3.51 +        sb.append("\n}");
    3.52 +        
    3.53 +        
    3.54 +        sb.append("\nfunction lazyVM() {");
    3.55 +        sb.append("\n  var self = this;");
    3.56 +        sb.append("\n  this.constructor.prototype.Identity = function(value) {");
    3.57 +        sb.append("\n    var self = this;");
    3.58 +        sb.append("\n    self.value = value;");
    3.59 +        sb.append("\n    self.call = function() { return self.value; };");
    3.60 +        sb.append("\n  };");
    3.61 +        sb.append("\n");
    3.62 +        sb.append("\n  this.constructor.prototype.loadClass = function(res, name) {");
    3.63 +        sb.append("\n    var script = org_apidesign_vm4brwsr_VMLazy_toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));");
    3.64 +//        sb.append("\n    throw script.toString();");
    3.65 +        sb.append("\n    new Function(");
    3.66 +        sb.append("\n      'arguments[0][arguments[1]]=new lazyVM.prototype.Identity(' + script + ').call'");
    3.67 +        sb.append("\n    )(self, name);");
    3.68 +        sb.append("\n  };");
    3.69 +        sb.append("\n");
    3.70 +        sb.append("\n}\n");
    3.71 +        
    3.72 +        ScriptEngine[] arr = { null };
    3.73 +        code = StaticMethodTest.compileClass(sb, arr,
    3.74 +            "org/apidesign/vm4brwsr/VMLazy"
    3.75 +        );
    3.76 +        arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
    3.77 +        codeSeq = sb;
    3.78 +    }
    3.79 +    
    3.80 +    @Test public void invokeStaticMethod() throws Exception {
    3.81 +        assertExec("Trying to get -1", "test", Double.valueOf(-1),
    3.82 +            "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne"
    3.83 +        );
    3.84 +    }
    3.85 +    
    3.86 +
    3.87 +    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    3.88 +        Object ret = null;
    3.89 +        try {
    3.90 +            ret = code.invokeFunction(methodName, args);
    3.91 +        } catch (ScriptException ex) {
    3.92 +            fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    3.93 +        } catch (NoSuchMethodException ex) {
    3.94 +            fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    3.95 +        }
    3.96 +        if (ret == null && expRes == null) {
    3.97 +            return;
    3.98 +        }
    3.99 +        if (expRes.equals(ret)) {
   3.100 +            return;
   3.101 +        }
   3.102 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   3.103 +    }
   3.104 +
   3.105 +    public static final class FindBytes {
   3.106 +        public byte[] get(String name) throws IOException {
   3.107 +            InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
   3.108 +            if (is == null) {
   3.109 +                throw new IOException("Can't find " + name);
   3.110 +            }
   3.111 +            byte[] arr = new byte[is.available()];
   3.112 +            int len = is.read(arr);
   3.113 +            if (len != arr.length) {
   3.114 +                throw new IOException("Read only " + len + " wanting " + arr.length);
   3.115 +            }
   3.116 +            System.err.print("loader['" + name + "'] = [");
   3.117 +            for (int i = 0; i < arr.length; i++) {
   3.118 +                if (i > 0) {
   3.119 +                    System.err.print(", ");
   3.120 +                }
   3.121 +                System.err.print(arr[i]);
   3.122 +            }
   3.123 +            System.err.println("]");
   3.124 +            return arr;
   3.125 +        }
   3.126 +    }
   3.127 +}