add support for default methods

This commit is contained in:
Volker Berlin 2020-01-12 17:13:52 +01:00
parent 735aadbab4
commit b1e89a91ca
4 changed files with 40 additions and 2 deletions

View File

@ -32,6 +32,7 @@ The project is currently not production ready but you can run already some tests
* [x] invoke instance method calls
* [ ] invoke interface method calls
* [ ] invoke dynamic method calls (lambdas)
* [x] invoke default method calls
* [ ] String support
* [ ] static constructors
* [x] Optimizer - Optimize the WASM output of a single method after transpiling before writing to output

View File

@ -228,6 +228,24 @@ public class ModuleGenerator {
ConstantClass superClass = superClassFile.getSuperClass();
superClassFile = superClass == null ? null : classFileLoader.get( superClass.getName() );
}
// search if there is a default implementation in an interface
superClassFile = classFile;
while( superClassFile != null ) {
for( ConstantClass iface : superClassFile.getInterfaces() ) {
ClassFile iClassFile = classFileLoader.get( iface.getName() );
MethodInfo method = iClassFile.getMethod( next.methodName, next.signature );
if( method != null ) {
FunctionName name = new FunctionName( method );
functions.markAsNeeded( name );
functions.setAlias( next, name );
continue NEXT; // we have found a super method
}
}
ConstantClass superClass = superClassFile.getSuperClass();
superClassFile = superClass == null ? null : classFileLoader.get( superClass.getName() );
}
throw new WasmException( "Missing function: " + next.signatureName, -1 );
}
}

View File

@ -27,6 +27,7 @@ import java.util.Map;
import javax.annotation.Nonnull;
import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.classparser.ClassFile.Type;
import de.inetsoftware.classparser.ConstantClass;
import de.inetsoftware.classparser.FieldInfo;
import de.inetsoftware.classparser.MethodInfo;
@ -231,6 +232,11 @@ public class TypeManager {
throw new WasmException( "Missing class: " + className, -1 );
}
// interface does not need to resolve
if( classFile.getType() == Type.Interface ) {
return;
}
{
// list all used fields
StructType type = types.structTypes.get( className );

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018 - 2019 Volker Berlin (i-net software)
* Copyright 2018 - 2020 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.
@ -49,6 +49,7 @@ public class StructsNonGC extends AbstractBaseTest {
addParam( list, script, "callVirtualMethod" );
addParam( list, script, "useGlobalObject" );
addParam( list, script, "multipleAssign" );
addParam( list, script, "getDefaultValue" );
}
rule.setTestParameters( list );
return list;
@ -136,9 +137,21 @@ public class StructsNonGC extends AbstractBaseTest {
}
return val.a;
}
@Export
static int getDefaultValue() {
Abc2 val = new Abc2();
return val.getDefault();
}
}
static class Abc {
interface TestDefault {
default int getDefault() {
return 7;
}
}
static class Abc implements TestDefault {
int a;
long b;