jaroslav@1794
|
1 |
/**
|
jaroslav@1794
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1794
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1794
|
4 |
*
|
jaroslav@1794
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1794
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1794
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1794
|
8 |
*
|
jaroslav@1794
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1794
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1794
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1794
|
12 |
* GNU General Public License for more details.
|
jaroslav@1794
|
13 |
*
|
jaroslav@1794
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1794
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1794
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1794
|
17 |
*/
|
jaroslav@1794
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@1794
|
19 |
|
jaroslav@1794
|
20 |
import javax.script.ScriptEngine;
|
jaroslav@1794
|
21 |
import javax.script.ScriptEngineManager;
|
jaroslav@1794
|
22 |
import static org.testng.Assert.*;
|
jaroslav@1794
|
23 |
import org.testng.annotations.Test;
|
jaroslav@1794
|
24 |
|
jaroslav@1794
|
25 |
/**
|
jaroslav@1794
|
26 |
*
|
jaroslav@1794
|
27 |
* @author Jaroslav Tulach
|
jaroslav@1794
|
28 |
*/
|
jaroslav@1794
|
29 |
public class StackMapperTest {
|
jaroslav@1794
|
30 |
|
jaroslav@1794
|
31 |
public StackMapperTest() {
|
jaroslav@1794
|
32 |
}
|
jaroslav@1794
|
33 |
|
jaroslav@1794
|
34 |
@Test public void replaceValueThatAnotherReplaceDependsOn()
|
jaroslav@1794
|
35 |
throws Exception {
|
jaroslav@1794
|
36 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1794
|
37 |
|
jaroslav@1794
|
38 |
StackMapper smapper = new StackMapper();
|
jaroslav@1794
|
39 |
|
jaroslav@1794
|
40 |
smapper.assign(sb, VarType.INTEGER, "0");
|
jaroslav@1794
|
41 |
|
jaroslav@1794
|
42 |
smapper.assign(sb, VarType.REFERENCE, "arr");
|
jaroslav@1794
|
43 |
smapper.assign(sb, VarType.INTEGER, "33");
|
jaroslav@1794
|
44 |
smapper.replace(sb, VarType.INTEGER, "@2[@1]",
|
jaroslav@1794
|
45 |
smapper.popI(), smapper.getA(0)
|
jaroslav@1794
|
46 |
);
|
jaroslav@1794
|
47 |
|
jaroslav@1794
|
48 |
smapper.replace(sb, VarType.INTEGER, "(@1) + (@2)",
|
jaroslav@1794
|
49 |
smapper.getI(1), smapper.popI()
|
jaroslav@1794
|
50 |
);
|
jaroslav@1794
|
51 |
|
jaroslav@1794
|
52 |
smapper.assign(sb, VarType.REFERENCE, "arr");
|
jaroslav@1794
|
53 |
smapper.assign(sb, VarType.INTEGER, "22");
|
jaroslav@1794
|
54 |
smapper.replace(sb, VarType.INTEGER, "@2[@1]",
|
jaroslav@1794
|
55 |
smapper.popI(), smapper.getA(0)
|
jaroslav@1794
|
56 |
);
|
jaroslav@1794
|
57 |
|
jaroslav@1794
|
58 |
smapper.replace(sb, VarType.INTEGER, "(@1) + (@2)",
|
jaroslav@1794
|
59 |
smapper.getI(1), smapper.popI()
|
jaroslav@1794
|
60 |
);
|
jaroslav@1794
|
61 |
|
jaroslav@1794
|
62 |
smapper.flush(sb);
|
jaroslav@1794
|
63 |
|
jaroslav@1794
|
64 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1794
|
65 |
ScriptEngine em = sem.getEngineByMimeType("text/javascript");
|
jaroslav@1794
|
66 |
Object ret = em.eval("var arr= []; arr[33] = 40; arr[22] = 2; " + sb + "; stI0;");
|
jaroslav@1794
|
67 |
|
jaroslav@1794
|
68 |
assertTrue(ret instanceof Number, "Result is number: " + ret);
|
jaroslav@1794
|
69 |
assertEquals(((Number)ret).intValue(), 42, "Right value");
|
jaroslav@1794
|
70 |
}
|
jaroslav@1794
|
71 |
|
jaroslav@1794
|
72 |
}
|