mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
add sourceFile to WasmException
This commit is contained in:
parent
5acae6b103
commit
a3807680b7
@ -25,16 +25,21 @@ public class WasmException extends Exception {
|
|||||||
|
|
||||||
private int lineNumber;
|
private int lineNumber;
|
||||||
|
|
||||||
|
private String sourceFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance.
|
* Create a new instance.
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
* the error message
|
* the error message
|
||||||
|
* @param sourceFile
|
||||||
|
* the sourceFile of the Java code
|
||||||
* @param lineNumber
|
* @param lineNumber
|
||||||
* the line number in Java Code
|
* the line number in Java Code
|
||||||
*/
|
*/
|
||||||
public WasmException( String message, int lineNumber ) {
|
public WasmException( String message, String sourceFile, int lineNumber ) {
|
||||||
super( message );
|
super( message );
|
||||||
|
this.sourceFile = sourceFile;
|
||||||
this.lineNumber = lineNumber;
|
this.lineNumber = lineNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,12 +59,17 @@ public class WasmException extends Exception {
|
|||||||
*
|
*
|
||||||
* @param cause
|
* @param cause
|
||||||
* the wrapped cause
|
* the wrapped cause
|
||||||
|
* @param sourceFile
|
||||||
|
* the sourceFile of the Java code
|
||||||
* @param lineNumber
|
* @param lineNumber
|
||||||
* the line number in Java Code
|
* the line number in Java Code
|
||||||
* @return a new instance
|
* @return a new instance
|
||||||
*/
|
*/
|
||||||
public static WasmException create( Throwable cause, int lineNumber ) {
|
public static WasmException create( Throwable cause, String sourceFile, int lineNumber ) {
|
||||||
WasmException wasmEx = create( cause );
|
WasmException wasmEx = create( cause );
|
||||||
|
if( wasmEx.sourceFile == null ) {
|
||||||
|
wasmEx.sourceFile = sourceFile;
|
||||||
|
}
|
||||||
if( wasmEx.lineNumber < 0 ) {
|
if( wasmEx.lineNumber < 0 ) {
|
||||||
wasmEx.lineNumber = lineNumber;
|
wasmEx.lineNumber = lineNumber;
|
||||||
}
|
}
|
||||||
@ -95,8 +105,11 @@ public class WasmException extends Exception {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String str = super.toString();
|
String str = super.toString();
|
||||||
|
if( sourceFile != null || lineNumber > 0 ) {
|
||||||
|
str += " at " + (sourceFile != null ? sourceFile : "line");
|
||||||
if( lineNumber > 0 ) {
|
if( lineNumber > 0 ) {
|
||||||
str += " at line: " + lineNumber;
|
str += ":" + lineNumber;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
|
|
||||||
private ArrayList<ValueType> locals = new ArrayList<>();
|
private ArrayList<ValueType> locals = new ArrayList<>();
|
||||||
|
|
||||||
|
private String sourceFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the content of the class to the
|
* Write the content of the class to the
|
||||||
*
|
*
|
||||||
@ -55,6 +57,10 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
* if some Java code can't converted
|
* if some Java code can't converted
|
||||||
*/
|
*/
|
||||||
public void write( ClassFile classFile ) throws IOException, WasmException {
|
public void write( ClassFile classFile ) throws IOException, WasmException {
|
||||||
|
sourceFile = classFile.getSourceFile();
|
||||||
|
if( sourceFile == null ) {
|
||||||
|
sourceFile = classFile.getThisClass().getName();
|
||||||
|
}
|
||||||
MethodInfo[] methods = classFile.getMethods();
|
MethodInfo[] methods = classFile.getMethods();
|
||||||
for( MethodInfo method : methods ) {
|
for( MethodInfo method : methods ) {
|
||||||
Code code = method.getCode();
|
Code code = method.getCode();
|
||||||
@ -201,7 +207,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
javaType = signature.substring( i, i + 1 );
|
javaType = signature.substring( i, i + 1 );
|
||||||
}
|
}
|
||||||
int lineNumber = method.getCode().getFirstLineNr();
|
int lineNumber = method.getCode().getFirstLineNr();
|
||||||
throw new WasmException( "Not supported Java data type in method signature: " + javaType, lineNumber );
|
throw new WasmException( "Not supported Java data type in method signature: " + javaType, sourceFile, lineNumber );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,6 +241,8 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
* a stream of byte code
|
* a stream of byte code
|
||||||
* @param lineNumber
|
* @param lineNumber
|
||||||
* the current line number
|
* the current line number
|
||||||
|
* @param constantPool
|
||||||
|
* the constant pool of the the current class
|
||||||
* @throws WasmException
|
* @throws WasmException
|
||||||
* if some Java code can't converted
|
* if some Java code can't converted
|
||||||
*/
|
*/
|
||||||
@ -278,14 +286,24 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
writeReturn();
|
writeReturn();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new WasmException( "Unimplemented byte code operation: " + op, lineNumber );
|
throw new WasmException( "Unimplemented byte code operation: " + op, sourceFile, lineNumber );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throw WasmException.create( ex, lineNumber );
|
throw WasmException.create( ex, sourceFile, lineNumber );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a constant value.
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* the value
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
* @throws WasmException
|
||||||
|
* if the value type is not supported
|
||||||
|
*/
|
||||||
private void writeConst( Object value ) throws IOException, WasmException {
|
private void writeConst( Object value ) throws IOException, WasmException {
|
||||||
Class<?> clazz = value.getClass();
|
Class<?> clazz = value.getClass();
|
||||||
if( clazz == Integer.class ) {
|
if( clazz == Integer.class ) {
|
||||||
@ -297,7 +315,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
} else if( clazz == Double.class ) {
|
} else if( clazz == Double.class ) {
|
||||||
writeConstDouble( ((Double)value).doubleValue() );
|
writeConstDouble( ((Double)value).doubleValue() );
|
||||||
} else {
|
} else {
|
||||||
throw new WasmException( "Not supported constant type: " + clazz, -1 );
|
throw new WasmException( "Not supported constant type: " + clazz, sourceFile, -1 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,7 +379,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
}
|
}
|
||||||
ValueType oldType = locals.get( idx );
|
ValueType oldType = locals.get( idx );
|
||||||
if( oldType != null && oldType != valueType ) {
|
if( oldType != null && oldType != valueType ) {
|
||||||
throw new WasmException( "Redefine local variable type from " + oldType + " to " + valueType, -1 );
|
throw new WasmException( "Redefine local variable type from " + oldType + " to " + valueType, sourceFile, -1 );
|
||||||
}
|
}
|
||||||
locals.set( idx, valueType );
|
locals.set( idx, valueType );
|
||||||
if( load ) {
|
if( load ) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user