write event section if exception handling is used.

This commit is contained in:
Volker Berlin 2019-02-27 21:55:55 +01:00
parent 861ba5c62f
commit 2d2a5f4b68
5 changed files with 82 additions and 3 deletions

View File

@ -64,7 +64,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private Map<String, Function> functions = new LinkedHashMap<>();
private List<AnyType> locals = new ArrayList<>();
private List<AnyType> locals = new ArrayList<>();
private Map<String, Global> globals = new LinkedHashMap<>();
@ -74,7 +74,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private Function function;
private FunctionTypeEntry functionType;
private FunctionTypeEntry functionType;
private int exceptionSignatureIndex = -1;
/**
* Create new instance.
@ -109,6 +111,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
writeSection( SectionType.Import, imports.values() );
writeSection( SectionType.Function, functions.values() );
writeSection( SectionType.Global, globals.values() );
writeEventSection();
writeExportSection();
writeCodeSection();
writeDebugNames();
@ -138,6 +141,25 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
}
}
/**
* Write the event section if needed.
*
* @throws IOException
* if any I/O error occur
*/
private void writeEventSection() throws IOException {
if( exceptionSignatureIndex >= 0 ) {
WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( 1 );
// event declaration
stream.writeVaruint32( 0 ); // event type: exception = 0
stream.writeVaruint32( exceptionSignatureIndex );
wasm.writeSection( SectionType.Export, stream );
}
}
/**
* Write the export section to the output. This section contains a mapping from the external index to the type signature index.
*
@ -238,6 +260,22 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
return typeId;
}
/**
* {@inheritDoc}
*/
@Override
protected void writeException() throws IOException {
if( exceptionSignatureIndex <= 0 ) {
FunctionTypeEntry exceptionType = new FunctionTypeEntry();
exceptionType.params.add( ValueType.anyref );
exceptionSignatureIndex = functionTypes.indexOf( exceptionType );
if( exceptionSignatureIndex < 0 ) {
exceptionSignatureIndex = functionTypes.size();
functionTypes.add( exceptionType );
}
}
}
/**
* {@inheritDoc}
*/

View File

@ -371,6 +371,15 @@ public class ModuleGenerator {
optimizer.optimze( instructions );
for( WasmInstruction instruction : instructions ) {
switch( instruction.getType() ) {
case Block:
switch( ((WasmBlockInstruction)instruction).getOperation() ) {
case TRY:
case CATCH:
writer.writeException();
break;
default:
}
break;
case Call:
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() );
break;

View File

@ -60,6 +60,14 @@ public abstract class ModuleWriter implements Closeable {
*/
protected abstract int writeStruct( String typeName, List<NamedStorageType> fields ) throws IOException;
/**
* Mark to write exceptions
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeException() throws IOException;
/**
* Prepare a imported single function in the prepare phase.
*

View File

@ -21,7 +21,6 @@ import java.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
@ -62,6 +61,14 @@ class WasmBlockInstruction extends WasmInstruction {
return Type.Block;
}
/**
* Get the operation
* @return the op
*/
WasmBlockOperator getOperation() {
return op;
}
/**
* {@inheritDoc}
*/

View File

@ -57,6 +57,8 @@ public class TextModuleWriter extends ModuleWriter {
private HashSet<String> globals = new HashSet<>();
private boolean useExceptions;
/**
* Create a new instance.
*
@ -117,6 +119,21 @@ public class TextModuleWriter extends ModuleWriter {
return 0;
}
/**
* {@inheritDoc}
*/
@Override
protected void writeException() throws IOException {
if( !useExceptions ) {
useExceptions = true;
int oldInset = inset;
inset = 1;
newline( output );
output.append( "(event (param anyref))" );
inset = oldInset;
}
}
/**
* {@inheritDoc}
*/