rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/TestVM.java
branchjdk8
changeset 1639 4b09a4b689a4
child 1654 da24a2411ee7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/TestVM.java	Thu Jun 26 23:54:17 2014 +0200
     1.3 @@ -0,0 +1,310 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.vm8;
    1.22 +
    1.23 +import java.io.ByteArrayInputStream;
    1.24 +import java.io.File;
    1.25 +import java.io.FileWriter;
    1.26 +import java.io.IOException;
    1.27 +import java.io.InputStream;
    1.28 +import java.net.URL;
    1.29 +import java.util.ArrayList;
    1.30 +import java.util.Arrays;
    1.31 +import java.util.Enumeration;
    1.32 +import java.util.HashSet;
    1.33 +import java.util.List;
    1.34 +import java.util.Set;
    1.35 +import javax.script.Invocable;
    1.36 +import javax.script.ScriptContext;
    1.37 +import javax.script.ScriptEngine;
    1.38 +import javax.script.ScriptEngineManager;
    1.39 +import javax.script.ScriptException;
    1.40 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.41 +import org.apidesign.vm4brwsr.ObfuscationLevel;
    1.42 +import static org.testng.Assert.*;
    1.43 +
    1.44 +public final class TestVM {
    1.45 +    private final Invocable code;
    1.46 +    private final CharSequence codeSeq;
    1.47 +    private final Object bck2brwsr;
    1.48 +    private BytesLoader resources;
    1.49 +    
    1.50 +    
    1.51 +    private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
    1.52 +        this.code = code;
    1.53 +        this.codeSeq = codeSeq;
    1.54 +        this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
    1.55 +        ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
    1.56 +    }
    1.57 +    
    1.58 +    public void register(BytesLoader res) {
    1.59 +        this.resources = res;
    1.60 +    }
    1.61 +    
    1.62 +    public byte[] get(String res) throws IOException {
    1.63 +        return resources != null ? resources.get(res) : null;
    1.64 +    }
    1.65 +
    1.66 +    public Object execCode(
    1.67 +        String msg, Class<?> clazz, String method, 
    1.68 +        Object expRes, Object... args
    1.69 +    ) throws Exception {
    1.70 +        Object ret = null;
    1.71 +        try {
    1.72 +            ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
    1.73 +            List<Object> ma = new ArrayList<>();
    1.74 +            ma.add(method);
    1.75 +            ma.addAll(Arrays.asList(args));
    1.76 +            ret = code.invokeMethod(ret, "invoke", ma.toArray());
    1.77 +        } catch (ScriptException ex) {
    1.78 +            fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
    1.79 +        } catch (NoSuchMethodException ex) {
    1.80 +            fail("Cannot find method in " + dumpJS(codeSeq), ex);
    1.81 +        }
    1.82 +        if (ret == null && expRes == null) {
    1.83 +            return null;
    1.84 +        }
    1.85 +        if (expRes != null && expRes.equals(ret)) {
    1.86 +            return null;
    1.87 +        }
    1.88 +        if (expRes instanceof Number) {
    1.89 +            // in case of Long it is necessary convert it to number
    1.90 +            // since the Long is represented by two numbers in JavaScript
    1.91 +            try {
    1.92 +                final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
    1.93 +                if (ret instanceof Long) {
    1.94 +                    ret = code.invokeMethod(toFP, "call", ret);
    1.95 +                }
    1.96 +                ret = code.invokeFunction("Number", ret);
    1.97 +            } catch (ScriptException ex) {
    1.98 +                fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
    1.99 +            } catch (NoSuchMethodException ex) {
   1.100 +                fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
   1.101 +            }
   1.102 +        }
   1.103 +        return ret;
   1.104 +    }
   1.105 +    
   1.106 +    void assertExec(
   1.107 +        String msg, Class clazz, String method, Object expRes, Object... args
   1.108 +    ) throws Exception {
   1.109 +        Object ret = execCode(msg, clazz, method, expRes, args);
   1.110 +        if (ret == null) {
   1.111 +            return;
   1.112 +        }
   1.113 +        if (expRes instanceof Integer && ret instanceof Double) {
   1.114 +            expRes = ((Integer)expRes).doubleValue();
   1.115 +        }
   1.116 +        if (expRes != null && expRes.equals(ret)) {
   1.117 +            return;
   1.118 +        }
   1.119 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
   1.120 +    }    
   1.121 +
   1.122 +    static TestVM compileClass(String... names) throws ScriptException, IOException {
   1.123 +        return compileClass(null, names);
   1.124 +    }
   1.125 +    
   1.126 +    static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   1.127 +        return compileClass(sb, null, names);
   1.128 +    }
   1.129 +
   1.130 +    static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
   1.131 +        if (sb == null) {
   1.132 +            sb = new StringBuilder();
   1.133 +        }
   1.134 +        Bck2Brwsr.generate(sb, new EmulationResources(), names);
   1.135 +        ScriptEngineManager sem = new ScriptEngineManager();
   1.136 +        ScriptEngine js = sem.getEngineByExtension("js");
   1.137 +        if (eng != null) {
   1.138 +            eng[0] = js;
   1.139 +        }
   1.140 +        try {
   1.141 +            Object res = js.eval(sb.toString());
   1.142 +            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   1.143 +            return new TestVM((Invocable) js, sb);
   1.144 +        } catch (Exception ex) {
   1.145 +            if (sb.length() > 2000) {
   1.146 +                sb = dumpJS(sb);
   1.147 +            }
   1.148 +            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
   1.149 +            return null;
   1.150 +        }
   1.151 +    }
   1.152 +    
   1.153 +    static TestVM compileClassAsExtension(
   1.154 +        StringBuilder sb, ScriptEngine[] eng, 
   1.155 +        String name, final String resourceName, final String resourceContent
   1.156 +    ) throws ScriptException, IOException {
   1.157 +        return compileClassesAsExtension(sb, eng, resourceName, resourceContent, name);
   1.158 +    }
   1.159 +    static TestVM compileClassesAsExtension(
   1.160 +        StringBuilder sb, ScriptEngine[] eng, 
   1.161 +        final String resourceName, final String resourceContent, String... names
   1.162 +    ) throws ScriptException, IOException {
   1.163 +        if (sb == null) {
   1.164 +            sb = new StringBuilder();
   1.165 +        }
   1.166 +        if (eng[0] == null) {
   1.167 +            ScriptEngineManager sem = new ScriptEngineManager();
   1.168 +            ScriptEngine js = sem.getEngineByExtension("js");
   1.169 +            eng[0] = js;
   1.170 +            Bck2Brwsr.generate(sb, new EmulationResources());
   1.171 +        }
   1.172 +        Set<String> exp = new HashSet<String>();
   1.173 +        for (String n : names) {
   1.174 +            int last = n.lastIndexOf('/');
   1.175 +            exp.add(n.substring(0, last + 1));
   1.176 +        }
   1.177 +        Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
   1.178 +            resources(new EmulationResources() {
   1.179 +                @Override
   1.180 +                public InputStream get(String name) throws IOException {
   1.181 +                    if (name.equals(resourceName)) {
   1.182 +                        return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
   1.183 +                    }
   1.184 +                    return super.get(name);
   1.185 +                }
   1.186 +            }).
   1.187 +            addClasses(names).
   1.188 +            addResources("org/apidesign/vm4brwsr/obj.js").
   1.189 +            addExported(exp.toArray(new String[0])).
   1.190 +            obfuscation(ObfuscationLevel.FULL).
   1.191 +            library();
   1.192 +        if (resourceName != null) {
   1.193 +            b2b = b2b.addResources(resourceName);
   1.194 +        }
   1.195 +        b2b.generate(sb);
   1.196 +        try {
   1.197 +            defineAtoB(eng[0]);
   1.198 +            Object res = eng[0].eval(sb.toString());
   1.199 +            assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
   1.200 +            return new TestVM((Invocable) eng[0], sb);
   1.201 +        } catch (Exception ex) {
   1.202 +            if (sb.length() > 2000) {
   1.203 +                sb = dumpJS(sb);
   1.204 +            }
   1.205 +            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
   1.206 +            return null;
   1.207 +        }
   1.208 +    }
   1.209 +    
   1.210 +    static TestVM compileClassAndResources(StringBuilder sb, ScriptEngine[] eng, String name, String... resources) throws ScriptException, IOException {
   1.211 +        if (sb == null) {
   1.212 +            sb = new StringBuilder();
   1.213 +        }
   1.214 +        Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
   1.215 +            resources(new EmulationResources()).
   1.216 +            addRootClasses(name).
   1.217 +            addResources(resources);
   1.218 +        b2b.generate(sb);
   1.219 +        ScriptEngineManager sem = new ScriptEngineManager();
   1.220 +        ScriptEngine js = sem.getEngineByExtension("js");
   1.221 +        if (eng != null) {
   1.222 +            eng[0] = js;
   1.223 +        }
   1.224 +        try {
   1.225 +            defineAtoB(js);
   1.226 +            
   1.227 +            Object res = js.eval(sb.toString());
   1.228 +            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   1.229 +            return new TestVM((Invocable) js, sb);
   1.230 +        } catch (Exception ex) {
   1.231 +            if (sb.length() > 2000) {
   1.232 +                sb = dumpJS(sb);
   1.233 +            }
   1.234 +            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
   1.235 +            return null;
   1.236 +        }
   1.237 +    }
   1.238 +
   1.239 +    private static void defineAtoB(ScriptEngine js) throws ScriptException {
   1.240 +        js.eval("atob = function(s) { return new String(org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s)); }");
   1.241 +    }
   1.242 +
   1.243 +//    Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
   1.244 +//        return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
   1.245 +//    }
   1.246 +    
   1.247 +    Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
   1.248 +        return code.invokeMethod(obj, method, params);
   1.249 +    }
   1.250 +
   1.251 +    Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
   1.252 +        return code.invokeFunction(methodName, args);
   1.253 +    }
   1.254 +
   1.255 +    static StringBuilder dumpJS(CharSequence sb) throws IOException {
   1.256 +        File f = File.createTempFile("execution", ".js");
   1.257 +        FileWriter w = new FileWriter(f);
   1.258 +        w.append(sb);
   1.259 +        w.close();
   1.260 +        return new StringBuilder(f.getPath());
   1.261 +    }
   1.262 +
   1.263 +    @Override
   1.264 +    public String toString() {
   1.265 +        try {
   1.266 +            return dumpJS(codeSeq).toString();
   1.267 +        } catch (IOException ex) {
   1.268 +            return ex.toString();
   1.269 +        }
   1.270 +    }
   1.271 +
   1.272 +    final CharSequence codeSeq() {
   1.273 +        return codeSeq;
   1.274 +    }
   1.275 +    
   1.276 +    private static class EmulationResources implements Bck2Brwsr.Resources {
   1.277 +        @Override
   1.278 +        public InputStream get(String name) throws IOException {
   1.279 +            if ("java/net/URI.class".equals(name)) {
   1.280 +                // skip
   1.281 +                return null;
   1.282 +            }
   1.283 +            if ("java/net/URLConnection.class".equals(name)) {
   1.284 +                // skip
   1.285 +                return null;
   1.286 +            }
   1.287 +            if ("java/lang/System.class".equals(name)) {
   1.288 +                // skip
   1.289 +                return null;
   1.290 +            }
   1.291 +            if ("java/io/PrintStream.class".equals(name)) {
   1.292 +                // skip
   1.293 +                return null;
   1.294 +            }
   1.295 +            if ("java/io/PrintWriter.class".equals(name)) {
   1.296 +                // skip
   1.297 +                return null;
   1.298 +            }
   1.299 +            Enumeration<URL> en = TestVM.class.getClassLoader().getResources(name);
   1.300 +            URL u = null;
   1.301 +            while (en.hasMoreElements()) {
   1.302 +                u = en.nextElement();
   1.303 +            }
   1.304 +            if (u == null) {
   1.305 +                throw new IOException("Can't find " + name);
   1.306 +            }
   1.307 +            if (u.toExternalForm().contains("rt.jar!")) {
   1.308 +                throw new IOException("No emulation for " + u);
   1.309 +            }
   1.310 +            return u.openStream();
   1.311 +        }
   1.312 +    }
   1.313 +}