From f277238d14d99aa97a7623987f0e74605ebc9068 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 3 May 2020 11:17:57 +0200 Subject: [PATCH] compile all implementations of used interfaces. --- .../jwebassembly/module/TypeManager.java | 44 ++++++++++++++++--- .../jwebassembly/module/WasmCodeBuilder.java | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index 6c68a2f..bda99f8 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -447,13 +447,9 @@ public class TypeManager { } // add all interfaces to the instanceof set - for(ConstantClass interClass : classFile.getInterfaces() ) { - StructType type = types.structTypes.get( interClass.getName() ); - if( type != null ) { - instanceOFs.add( type ); - } - } + listInterface( classFile, functions, types, classFileLoader ); + // List stuff of super class ConstantClass superClass = classFile.getSuperClass(); if( superClass != null ) { String superClassName = superClass.getName(); @@ -463,6 +459,7 @@ public class TypeManager { fields.add( new NamedStorageType( ValueType.i32, className, FIELD_HASHCODE ) ); } + // list all fields for( FieldInfo field : classFile.getFields() ) { if( field.isStatic() ) { continue; @@ -500,6 +497,41 @@ public class TypeManager { } } + /** + * List all interfaces and and mark all instance methods of used interfaces. + * + * @param classFile + * the class file + * @param functions + * the used functions for the vtables of the types + * @param types + * for types of fields + * @param classFileLoader + * for loading the class files + * @throws IOException + * if any I/O error occur on loading or writing + */ + private void listInterface( ClassFile classFile, FunctionManager functions, TypeManager types, ClassFileLoader classFileLoader ) throws IOException { + for( ConstantClass interClass : classFile.getInterfaces() ) { + String interName = interClass.getName(); + StructType type = types.structTypes.get( interName ); + if( type != null ) { + // add all interfaces to the instanceof set + instanceOFs.add( type ); + } + ClassFile interClassFile = classFileLoader.get( interName ); + for( MethodInfo interMethod : interClassFile.getMethods() ) { + FunctionName funcName = new FunctionName( interMethod ); + if( functions.isUsed( funcName ) ) { + MethodInfo method = classFile.getMethod( funcName.methodName, funcName.signature ); + if( method != null ) { + functions.markAsNeeded( new FunctionName( method ) ); + } + } + } + } + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 3689f25..7f163e4 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -651,7 +651,7 @@ public abstract class WasmCodeBuilder { * the line number in the Java source code */ protected void addCallInterfaceInstruction( FunctionName name, int javaCodePos, int lineNumber ) { - //TODO name = functions.markAsNeeded( name ); + name = functions.markAsNeeded( name ); addCallIndirectInstruction( new WasmCallInterfaceInstruction( name, javaCodePos, lineNumber, types, options ) ); functions.markClassAsUsed( name.className ); }