311 lines
8.5 KiB
Java
Raw Normal View History

/*
2021-01-24 18:57:00 +01:00
* Copyright 2018 - 2021 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.jwebassembly.runtime;
import java.util.ArrayList;
import java.util.Collection;
2020-02-26 18:02:59 +01:00
import java.util.LinkedList;
import java.util.List;
2021-01-24 18:57:00 +01:00
import java.util.function.IntSupplier;
import java.util.function.IntUnaryOperator;
import org.junit.ClassRule;
import org.junit.runners.Parameterized.Parameters;
import de.inetsoftware.jwebassembly.ScriptEngine;
import de.inetsoftware.jwebassembly.WasmRule;
import de.inetsoftware.jwebassembly.api.annotation.Export;
2020-04-25 19:31:30 +02:00
import de.inetsoftware.jwebassembly.runtime.StructsGC.Abc2;
2020-12-12 21:29:17 +01:00
import de.inetsoftware.jwebassembly.web.DOMString;
2020-03-15 12:05:54 +01:00
import de.inetsoftware.jwebassembly.web.JSObject;
public class StructsNonGC extends AbstractBaseTest {
@ClassRule
public static WasmRule rule = new WasmRule( TestClass.class, Abc.class );
public StructsNonGC( ScriptEngine script, String method, Object[] params ) {
super( rule, script, method, params );
}
@Parameters( name = "{0}-{1}" )
public static Collection<Object[]> data() {
ArrayList<Object[]> list = new ArrayList<>();
ScriptEngine[] engines = ScriptEngine.testEngines();
for( ScriptEngine script : engines ) {
addParam( list, script, "isNull" );
addParam( list, script, "isNotNull" );
addParam( list, script, "isSame" );
addParam( list, script, "isNotSame" );
addParam( list, script, "simple" );
addParam( list, script, "callSuperMethod" );
addParam( list, script, "callVirtualMethod" );
addParam( list, script, "useGlobalObject" );
addParam( list, script, "multipleAssign" );
2020-01-12 17:13:52 +01:00
addParam( list, script, "getDefaultValue" );
2020-03-21 11:25:32 +01:00
addParam( list, script, "callAbstractMethod" );
2020-02-26 18:02:59 +01:00
addParam( list, script, "instanceof1" );
addParam( list, script, "instanceof2" );
addParam( list, script, "instanceof3" );
2020-02-28 18:04:28 +01:00
addParam( list, script, "cast" );
2020-03-15 12:05:54 +01:00
addParam( list, script, "objectClassName" );
addParam( list, script, "integerClassName" );
addParam( list, script, "classClassName" );
2020-03-15 15:49:52 +01:00
addParam( list, script, "classConst" );
2020-05-02 14:42:22 +02:00
addParam( list, script, "intClassName" );
addParam( list, script, "getComponentType" );
2020-03-21 11:59:05 +01:00
addParam( list, script, "branchWithObjectResult" );
2021-01-24 18:57:00 +01:00
addParam( list, script, "callParameterFromCondition" );
addParam( list, script, "lambda0" );
addParam( list, script, "lambda1" );
}
rule.setTestParameters( list );
return list;
}
static class TestClass {
@Export
static boolean isNull() {
Object val = null;
return val == null;
}
@Export
static boolean isNotNull() {
Object val = null;
return val != null;
}
@Export
static boolean isSame() {
Object val1 = null;
Object val2 = null;
return val1 == val2;
}
@Export
static boolean isNotSame() {
Object val1 = null;
Object val2 = null;
return val1 != val2;
}
@Export
static int simple() {
Abc val = new Abc2();
val.a = 63;
return val.a;
}
/**
* Call a method that is declared in the super class of the instance
*/
@Export
static int callSuperMethod() {
Abc2 val = new Abc2();
val.foo();
return val.a;
}
/**
* Call an overridden method
*/
@Export
static int callVirtualMethod() {
Abc val = new Abc2();
val.bar();
return val.a;
}
/**
* Access a object in a global/static variable.
*/
static Abc2 valGlobal;
@Export
static int useGlobalObject() {
valGlobal = new Abc2();
valGlobal.foo();
return valGlobal.a;
}
/**
* Assign multiple with a field. There are complex stack operation
*/
@Export
static int multipleAssign() {
Abc2 val = new Abc2();
for( int i = 0; i < 1_000; i++ ) {
2020-04-25 19:31:30 +02:00
val = val.abc = new Abc2();
val.a = i;
}
return val.a;
}
2020-01-12 17:13:52 +01:00
@Export
static int getDefaultValue() {
Abc2 val = new Abc2();
return val.getDefault();
}
2020-02-26 18:02:59 +01:00
2020-03-21 11:25:32 +01:00
@Export
static int callAbstractMethod() {
Ab val = new Abc2();
return val.abstractBar();
}
2020-02-26 18:02:59 +01:00
@Export
static boolean instanceof1() {
Object obj = new Object();
return obj instanceof Integer;
}
@Export
static boolean instanceof2() {
Object obj = new Object();
return obj instanceof Object;
}
@Export
static boolean instanceof3() {
Object obj = new LinkedList();
return obj instanceof List;
}
2020-02-28 18:04:28 +01:00
@Export
static int cast() {
Object obj = new Integer(42);
Integer val = (Integer)obj;
return val.intValue();
}
2020-03-15 12:05:54 +01:00
@Export
2020-12-12 21:29:17 +01:00
static DOMString objectClassName() {
2020-03-15 12:05:54 +01:00
Object obj = new Object();
Class clazz = obj.getClass();
return JSObject.domString( clazz.getName() );
}
@Export
2020-12-12 21:29:17 +01:00
static DOMString integerClassName() {
2020-03-15 12:05:54 +01:00
Object obj = new Integer(42);
Class clazz = obj.getClass();
return JSObject.domString( clazz.getName() );
}
@Export
2020-12-12 21:29:17 +01:00
static DOMString classClassName() {
2020-03-15 12:05:54 +01:00
Object obj = new Object();
Class clazz = obj.getClass().getClass();
return JSObject.domString( clazz.getName() );
}
2020-03-15 15:49:52 +01:00
@Export
2020-12-12 21:29:17 +01:00
static DOMString classConst() {
2020-03-15 15:49:52 +01:00
Class clazz = Float.class;
return JSObject.domString( clazz.getName() );
}
2020-03-21 11:59:05 +01:00
2020-05-02 14:42:22 +02:00
@Export
static Object intClassName() {
Class<?> clazz = int.class; // Integer.TYPE;
return JSObject.domString( clazz.getName() );
}
@Export
static boolean getComponentType() {
Class<?> clazz = byte.class;
clazz = clazz.getComponentType();
return clazz == null;
}
2020-03-21 11:59:05 +01:00
@Export
static int branchWithObjectResult() {
Integer val1 = 42;
Integer val2 = 7;
Integer val = val1 == val2 ? val1 : val2;
return val;
}
/**
* To find the instruction that push the object of the method call we need to consider an IF THEN ELSE when analyzing the stack.
*/
@Export
static int callParameterFromCondition() {
Abc abc = new Abc();
return abc.add( 42, abc == null ? 7 : 13 );
}
2021-01-24 18:57:00 +01:00
@Export
static int lambda0() {
IntSupplier val = () -> 42;
return val.getAsInt();
}
@Export
static int lambda1() {
IntUnaryOperator val = (x) -> x;
return val.applyAsInt( 13 );
}
2020-01-12 17:13:52 +01:00
}
interface TestDefault {
default int getDefault() {
return 7;
}
}
2020-03-21 11:25:32 +01:00
static abstract class Ab {
abstract int abstractBar();
}
static class Abc extends Ab implements TestDefault {
int a;
long b;
final void foo() {
a = 1;
}
void bar() {
a = 2;
}
2020-03-21 11:25:32 +01:00
@Override
int abstractBar() {
return 2;
}
int add( int a, int b ) {
return a + b;
}
}
static class Abc2 extends Abc {
Abc2 abc;
void bar() {
a = 3;
}
2020-03-21 11:25:32 +01:00
@Override
int abstractBar() {
return 3;
}
}
}