improve error handling

This commit is contained in:
Volker Berlin 2019-06-23 12:55:14 +02:00
parent 91ccf92796
commit fbdd0eb3d0
2 changed files with 39 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2018 Volker Berlin (i-net software)
* Copyright 2017 - 2019 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.
@ -71,6 +71,19 @@ public class WasmException extends RuntimeException {
lineNumber = -1;
}
/**
* Create a new instance with a cause.
*
* @param message
* the detail message
* @param cause
* the cause
*/
private WasmException( String message, Throwable cause ) {
super( message, cause );
lineNumber = -1;
}
/**
* Create a wrapped exception needed.
*
@ -125,6 +138,22 @@ public class WasmException extends RuntimeException {
return new WasmException( cause );
}
/**
* Create a wrapped exception needed with an additional message.
*
* @param cause
* the wrapped cause
* @param message
* the message
* @return a new instance
*/
public static WasmException create( String message, Throwable cause ) {
if( cause instanceof WasmException ) {
return (WasmException)cause;
}
return new WasmException( message, cause );
}
/**
* Get the line number in Java code on which the error occurred.
*

View File

@ -29,6 +29,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.JWebAssembly;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.FunctionName;
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
@ -292,9 +293,14 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
int start = wasm.size();
WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( size );
for( Function func : functions.values() ) {
func.addCodeOffset( start + stream.size() );
func.functionsStream.writeTo( stream );
for( Entry<String, Function> entry : functions.entrySet() ) {
try {
Function func = entry.getValue();
func.addCodeOffset( start + stream.size() );
func.functionsStream.writeTo( stream );
} catch( RuntimeException ex ) {
throw WasmException.create( entry.getKey(), ex );
}
}
wasm.writeSection( SectionType.Code, stream );