add a prepare phase on compiling

This commit is contained in:
Volker Berlin 2017-04-17 12:10:56 +02:00
parent 5450ccb2ce
commit f2efc5aafd
2 changed files with 87 additions and 44 deletions

View File

@ -157,6 +157,10 @@ public class JWebAssembly {
* if any conversion error occurs * if any conversion error occurs
*/ */
private void compile( ModuleWriter writer ) throws IOException, WasmException { private void compile( ModuleWriter writer ) throws IOException, WasmException {
for( URL url : classFiles ) {
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
writer.prepare( classFile );
}
for( URL url : classFiles ) { for( URL url : classFiles ) {
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) ); ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
writer.write( classFile ); writer.write( classFile );

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.Nonnegative; import javax.annotation.Nonnegative;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -30,6 +31,7 @@ import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.classparser.Code; import de.inetsoftware.classparser.Code;
import de.inetsoftware.classparser.CodeInputStream; import de.inetsoftware.classparser.CodeInputStream;
import de.inetsoftware.classparser.ConstantPool; import de.inetsoftware.classparser.ConstantPool;
import de.inetsoftware.classparser.ConstantRef;
import de.inetsoftware.classparser.LineNumberTable; import de.inetsoftware.classparser.LineNumberTable;
import de.inetsoftware.classparser.LocalVariableTable; import de.inetsoftware.classparser.LocalVariableTable;
import de.inetsoftware.classparser.MethodInfo; import de.inetsoftware.classparser.MethodInfo;
@ -51,16 +53,32 @@ public abstract class ModuleWriter implements Closeable {
private String sourceFile; private String sourceFile;
/** /**
* Write the content of the class to the * Prepare the content of the class.
* *
* @param classFile * @param classFile
* the class file * the class file
* @throws IOException
* if any I/O error occur
* @throws WasmException * @throws WasmException
* if some Java code can't converted * if some Java code can't converted
*/ */
public void write( ClassFile classFile ) throws IOException, WasmException { public void prepare( ClassFile classFile ) {
iterateMethods( classFile, m -> prepareMethod( m ) );
}
/**
* Write the content of the class to the writer.
*
* @param classFile
* the class file
* @throws WasmException
* if some Java code can't converted
*/
public void write( ClassFile classFile ) throws WasmException {
iterateMethods( classFile, m -> writeMethod( m ) );
}
private void iterateMethods( ClassFile classFile, Consumer<MethodInfo> handler ) throws WasmException {
sourceFile = null; // clear previous value for the case an IO exception occur
try {
sourceFile = classFile.getSourceFile(); sourceFile = classFile.getSourceFile();
if( sourceFile == null ) { if( sourceFile == null ) {
sourceFile = classFile.getThisClass().getName(); sourceFile = classFile.getThisClass().getName();
@ -72,8 +90,23 @@ public abstract class ModuleWriter implements Closeable {
&& code.isSuperInitReturn( classFile.getSuperClass() ) ) { && code.isSuperInitReturn( classFile.getSuperClass() ) ) {
continue; //default constructor continue; //default constructor
} }
writeMethod( method ); handler.accept( method );
} }
} catch( IOException ioex ) {
throw WasmException.create( ioex, sourceFile, -1 );
}
}
/**
* Prepare the method.
*
* @param method
* the method
* @throws WasmException
* if some Java code can't converted
*/
private void prepareMethod( MethodInfo method ) throws WasmException {
} }
/** /**
@ -81,12 +114,11 @@ public abstract class ModuleWriter implements Closeable {
* *
* @param method * @param method
* the method * the method
* @throws IOException
* if any I/O error occur
* @throws WasmException * @throws WasmException
* if some Java code can't converted * if some Java code can't converted
*/ */
private void writeMethod( MethodInfo method ) throws IOException, WasmException { private void writeMethod( MethodInfo method ) throws WasmException {
try {
Code code = method.getCode(); Code code = method.getCode();
if( code != null ) { // abstract methods and interface methods does not have code if( code != null ) { // abstract methods and interface methods does not have code
String methodName = method.getName(); // TODO naming conversion rule String methodName = method.getName(); // TODO naming conversion rule
@ -116,13 +148,20 @@ public abstract class ModuleWriter implements Closeable {
} }
writeMethodFinish( locals ); writeMethodFinish( locals );
} }
} catch( IOException ioex ) {
throw WasmException.create( ioex, sourceFile, -1 );
}
} }
/** /**
* Look for a Export annotation and if there write an export directive. * Look for a Export annotation and if there write an export directive.
*
* @param methodName * @param methodName
* the normalized method name
* @param method * @param method
* the moethod
* @throws IOException * @throws IOException
* if any IOException occur
*/ */
private void writeExport( String methodName, MethodInfo method ) throws IOException { private void writeExport( String methodName, MethodInfo method ) throws IOException {
Annotations annotations = method.getRuntimeInvisibleAnnotations(); Annotations annotations = method.getRuntimeInvisibleAnnotations();