rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 21:30:06 +0200
branchclosure
changeset 1491 4a1398eff4fb
parent 1487 84a744941c9f
child 1493 234fea368401
permissions -rw-r--r--
Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
jaroslav@708
     1
/**
jaroslav@708
     2
 * Back 2 Browser Bytecode Translator
jaroslav@708
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@708
     4
 *
jaroslav@708
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@708
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@708
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@708
     8
 *
jaroslav@708
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@708
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@708
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@708
    12
 * GNU General Public License for more details.
jaroslav@708
    13
 *
jaroslav@708
    14
 * You should have received a copy of the GNU General Public License
jaroslav@708
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@708
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@708
    17
 */
jaroslav@708
    18
package org.apidesign.vm4brwsr;
jaroslav@708
    19
jaroslav@708
    20
import java.io.File;
jaroslav@708
    21
import java.io.FileWriter;
jaroslav@708
    22
import java.io.IOException;
jaroslav@708
    23
import java.io.InputStream;
jaroslav@708
    24
import java.net.URL;
jaroslav@708
    25
import java.util.Enumeration;
jaroslav@708
    26
import javax.script.Invocable;
jaroslav@708
    27
import javax.script.ScriptEngine;
jaroslav@708
    28
import javax.script.ScriptEngineManager;
jaroslav@708
    29
import javax.script.ScriptException;
jaroslav@708
    30
import static org.testng.Assert.*;
jaroslav@708
    31
jaroslav@708
    32
final class TestVM {
jaroslav@708
    33
    private final Invocable code;
jaroslav@708
    34
    private final CharSequence codeSeq;
jaroslav@708
    35
    private final Object bck2brwsr;
jaroslav@708
    36
    
jaroslav@708
    37
    
jaroslav@708
    38
    private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
jaroslav@708
    39
        this.code = code;
jaroslav@708
    40
        this.codeSeq = codeSeq;
jaroslav@708
    41
        this.bck2brwsr = code.invokeFunction("bck2brwsr");
jaroslav@708
    42
    }
jaroslav@708
    43
    
jaroslav@708
    44
jaroslav@708
    45
    public Object execCode(
jaroslav@708
    46
        String msg, Class<?> clazz, String method, 
jaroslav@708
    47
        Object expRes, Object... args
jaroslav@708
    48
    ) throws Exception {
jaroslav@708
    49
        Object ret = null;
jaroslav@708
    50
        try {
jaroslav@708
    51
            ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
jaroslav@708
    52
            ret = code.invokeMethod(ret, method, args);
jaroslav@708
    53
        } catch (ScriptException ex) {
jaroslav@782
    54
            fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    55
        } catch (NoSuchMethodException ex) {
jaroslav@708
    56
            fail("Cannot find method in " + dumpJS(codeSeq), ex);
jaroslav@708
    57
        }
jaroslav@708
    58
        if (ret == null && expRes == null) {
jaroslav@708
    59
            return null;
jaroslav@708
    60
        }
jaroslav@747
    61
        if (expRes != null && expRes.equals(ret)) {
jaroslav@708
    62
            return null;
jaroslav@708
    63
        }
jaroslav@708
    64
        if (expRes instanceof Number) {
jaroslav@708
    65
            // in case of Long it is necessary convert it to number
jaroslav@708
    66
            // since the Long is represented by two numbers in JavaScript
jaroslav@708
    67
            try {
jaroslav@1485
    68
                final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
jaroslav@1485
    69
                if (ret instanceof Long) {
jaroslav@1485
    70
                    ret = code.invokeMethod(toFP, "call", ret);
jaroslav@1485
    71
                }
jaroslav@708
    72
                ret = code.invokeFunction("Number", ret);
jaroslav@708
    73
            } catch (ScriptException ex) {
jaroslav@784
    74
                fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    75
            } catch (NoSuchMethodException ex) {
jaroslav@784
    76
                fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    77
            }
jaroslav@708
    78
        }
jaroslav@708
    79
        return ret;
jaroslav@708
    80
    }
jaroslav@708
    81
    
jaroslav@708
    82
    void assertExec(
jaroslav@708
    83
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@708
    84
    ) throws Exception {
jaroslav@708
    85
        Object ret = execCode(msg, clazz, method, expRes, args);
jaroslav@708
    86
        if (ret == null) {
jaroslav@708
    87
            return;
jaroslav@708
    88
        }
jaroslav@1485
    89
        if (expRes instanceof Integer && ret instanceof Double) {
jaroslav@1485
    90
            expRes = ((Integer)expRes).doubleValue();
jaroslav@1485
    91
        }
jaroslav@708
    92
        if (expRes != null && expRes.equals(ret)) {
jaroslav@708
    93
            return;
jaroslav@708
    94
        }
jaroslav@708
    95
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
jaroslav@708
    96
    }    
jaroslav@708
    97
jaroslav@708
    98
    static TestVM compileClass(String... names) throws ScriptException, IOException {
jaroslav@708
    99
        return compileClass(null, names);
jaroslav@708
   100
    }
jaroslav@708
   101
    
jaroslav@708
   102
    static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@708
   103
        return compileClass(sb, null, names);
jaroslav@708
   104
    }
jaroslav@708
   105
jaroslav@708
   106
    static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
jaroslav@708
   107
        if (sb == null) {
jaroslav@708
   108
            sb = new StringBuilder();
jaroslav@708
   109
        }
jaroslav@708
   110
        Bck2Brwsr.generate(sb, new EmulationResources(), names);
jaroslav@708
   111
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@708
   112
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@708
   113
        if (eng != null) {
jaroslav@708
   114
            eng[0] = js;
jaroslav@708
   115
        }
jaroslav@708
   116
        try {
jaroslav@708
   117
            Object res = js.eval(sb.toString());
jaroslav@708
   118
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@708
   119
            return new TestVM((Invocable) js, sb);
jaroslav@708
   120
        } catch (Exception ex) {
jaroslav@708
   121
            if (sb.length() > 2000) {
jaroslav@708
   122
                sb = dumpJS(sb);
jaroslav@708
   123
            }
jaroslav@840
   124
            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
jaroslav@708
   125
            return null;
jaroslav@708
   126
        }
jaroslav@708
   127
    }
jaroslav@1487
   128
    
jaroslav@1487
   129
    static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
jaroslav@1487
   130
        if (sb == null) {
jaroslav@1487
   131
            sb = new StringBuilder();
jaroslav@1487
   132
        }
jaroslav@1487
   133
        Bck2Brwsr.generate(sb, new EmulationResources());
jaroslav@1487
   134
        Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
jaroslav@1487
   135
            resources(new EmulationResources()).
jaroslav@1491
   136
            addRootClasses(names).library(true);
jaroslav@1487
   137
        b2b.generate(sb);
jaroslav@1487
   138
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@1487
   139
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@1487
   140
        if (eng != null) {
jaroslav@1487
   141
            eng[0] = js;
jaroslav@1487
   142
        }
jaroslav@1487
   143
        try {
jaroslav@1487
   144
            Object res = js.eval(sb.toString());
jaroslav@1487
   145
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@1487
   146
            return new TestVM((Invocable) js, sb);
jaroslav@1487
   147
        } catch (Exception ex) {
jaroslav@1487
   148
            if (sb.length() > 2000) {
jaroslav@1487
   149
                sb = dumpJS(sb);
jaroslav@1487
   150
            }
jaroslav@1487
   151
            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
jaroslav@1487
   152
            return null;
jaroslav@1487
   153
        }
jaroslav@1487
   154
    }
jaroslav@708
   155
jaroslav@708
   156
    Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
jaroslav@708
   157
        return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
jaroslav@708
   158
    }
jaroslav@708
   159
    
jaroslav@708
   160
    Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
jaroslav@708
   161
        return code.invokeMethod(obj, method, params);
jaroslav@708
   162
    }
jaroslav@708
   163
jaroslav@708
   164
    Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
jaroslav@708
   165
        return code.invokeFunction(methodName, args);
jaroslav@708
   166
    }
jaroslav@708
   167
jaroslav@708
   168
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@708
   169
        File f = File.createTempFile("execution", ".js");
jaroslav@708
   170
        FileWriter w = new FileWriter(f);
jaroslav@708
   171
        w.append(sb);
jaroslav@708
   172
        w.close();
jaroslav@708
   173
        return new StringBuilder(f.getPath());
jaroslav@708
   174
    }
jaroslav@708
   175
jaroslav@708
   176
    @Override
jaroslav@708
   177
    public String toString() {
jaroslav@708
   178
        try {
jaroslav@708
   179
            return dumpJS(codeSeq).toString();
jaroslav@708
   180
        } catch (IOException ex) {
jaroslav@708
   181
            return ex.toString();
jaroslav@708
   182
        }
jaroslav@708
   183
    }
jaroslav@708
   184
    
jaroslav@708
   185
    
jaroslav@708
   186
    private static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@708
   187
        @Override
jaroslav@708
   188
        public InputStream get(String name) throws IOException {
jaroslav@708
   189
            Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
jaroslav@708
   190
            URL u = null;
jaroslav@708
   191
            while (en.hasMoreElements()) {
jaroslav@708
   192
                u = en.nextElement();
jaroslav@708
   193
            }
jaroslav@708
   194
            if (u == null) {
jaroslav@708
   195
                throw new IOException("Can't find " + name);
jaroslav@708
   196
            }
jaroslav@708
   197
            if (u.toExternalForm().contains("rt.jar!")) {
jaroslav@708
   198
                throw new IOException("No emulation for " + u);
jaroslav@708
   199
            }
jaroslav@708
   200
            return u.openStream();
jaroslav@708
   201
        }
jaroslav@708
   202
    }
jaroslav@708
   203
}