mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
improve exception message
This commit is contained in:
parent
c3b04e7016
commit
ce3e2d7546
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 Volker Berlin (i-net software)
|
||||
* Copyright 2017 - 2018 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.
|
||||
@ -27,6 +27,8 @@ public class WasmException extends RuntimeException {
|
||||
|
||||
private String sourceFile;
|
||||
|
||||
private String className;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
@ -34,10 +36,12 @@ public class WasmException extends RuntimeException {
|
||||
* the error message
|
||||
* @param sourceFile
|
||||
* the sourceFile of the Java code
|
||||
* @param className
|
||||
* the class name of the Java code
|
||||
* @param lineNumber
|
||||
* the line number in Java Code
|
||||
*/
|
||||
public WasmException( String message, String sourceFile, int lineNumber ) {
|
||||
public WasmException( String message, String sourceFile, String className, int lineNumber ) {
|
||||
super( message );
|
||||
this.sourceFile = sourceFile;
|
||||
this.lineNumber = lineNumber;
|
||||
@ -60,16 +64,21 @@ public class WasmException extends RuntimeException {
|
||||
* @param cause
|
||||
* the wrapped cause
|
||||
* @param sourceFile
|
||||
* the sourceFile of the Java code
|
||||
* the source file of the Java code
|
||||
* @param className
|
||||
* the class name of the Java code
|
||||
* @param lineNumber
|
||||
* the line number in Java Code
|
||||
* @return a new instance
|
||||
*/
|
||||
public static WasmException create( Throwable cause, String sourceFile, int lineNumber ) {
|
||||
public static WasmException create( Throwable cause, String sourceFile, String className, int lineNumber ) {
|
||||
WasmException wasmEx = create( cause );
|
||||
if( wasmEx.sourceFile == null ) {
|
||||
wasmEx.sourceFile = sourceFile;
|
||||
}
|
||||
if( wasmEx.className == null ) {
|
||||
wasmEx.className = className;
|
||||
}
|
||||
if( wasmEx.lineNumber < 0 ) {
|
||||
wasmEx.lineNumber = lineNumber;
|
||||
}
|
||||
@ -103,13 +112,19 @@ public class WasmException extends RuntimeException {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = super.toString();
|
||||
if( sourceFile != null || lineNumber > 0 ) {
|
||||
str += " at " + (sourceFile != null ? sourceFile : "line");
|
||||
public String getMessage() {
|
||||
String str = super.getMessage();
|
||||
if( sourceFile != null || className != null || lineNumber >= 0 ) {
|
||||
str += "\n\tat ";
|
||||
if( className != null ) {
|
||||
str += className.replace( '/', '.' ).replace( '$', '.' );
|
||||
}
|
||||
str += "(";
|
||||
str += (sourceFile != null ? sourceFile : "line");
|
||||
if( lineNumber > 0 ) {
|
||||
str += ":" + lineNumber;
|
||||
}
|
||||
str += ")";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
@ -693,7 +693,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
if( entry != null ) {
|
||||
id = entry.id;
|
||||
} else {
|
||||
throw new WasmException( "Call to unknown function: " + name, null, -1 );
|
||||
throw new WasmException( "Call to unknown function: " + name, null, null, -1 );
|
||||
}
|
||||
}
|
||||
codeStream.writeOpCode( CALL );
|
||||
|
@ -142,7 +142,7 @@ class BranchManger {
|
||||
loop.endPosition = parsedBlock.startPosition + 3;
|
||||
break;
|
||||
default:
|
||||
throw new WasmException( "Unimplemented loop code operation: " + parsedBlock.op, null, parsedBlock.lineNumber );
|
||||
throw new WasmException( "Unimplemented loop code operation: " + parsedBlock.op, null, null, parsedBlock.lineNumber );
|
||||
}
|
||||
} else {
|
||||
switch ( parsedBlock.op ) {
|
||||
@ -225,7 +225,7 @@ class BranchManger {
|
||||
calculateLoop( parent, parsedBlock, parsedOperations );
|
||||
break;
|
||||
default:
|
||||
throw new WasmException( "Unimplemented block code operation: " + parsedBlock.op, null, parsedBlock.lineNumber );
|
||||
throw new WasmException( "Unimplemented block code operation: " + parsedBlock.op, null, null, parsedBlock.lineNumber );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -538,7 +538,7 @@ class BranchManger {
|
||||
deep++;
|
||||
}
|
||||
}
|
||||
throw new WasmException( "GOTO code without target loop/block", null, gotoBlock.lineNumber );
|
||||
throw new WasmException( "GOTO code without target loop/block", null, null, gotoBlock.lineNumber );
|
||||
|
||||
}
|
||||
|
||||
@ -658,7 +658,7 @@ class BranchManger {
|
||||
newOp = NumericOperator.lt;
|
||||
break;
|
||||
default:
|
||||
throw new WasmException( "Not a compare operation: " + instr.numOp, null, lineNumber );
|
||||
throw new WasmException( "Not a compare operation: " + instr.numOp, null, null, lineNumber );
|
||||
}
|
||||
instr.numOp = newOp;
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ class LocaleVariableManager {
|
||||
LocaleVariable var = variables[slot];
|
||||
if( var.valueType != null && var.valueType != valueType ) {
|
||||
throw new WasmException( "Redefine local variable type from " + var.valueType + " to "
|
||||
+ valueType, null, -1 );
|
||||
+ valueType, null, null, -1 );
|
||||
}
|
||||
var.valueType = valueType;
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ public class ModuleGenerator {
|
||||
|
||||
private String sourceFile;
|
||||
|
||||
private String className;
|
||||
|
||||
private final List<WasmInstruction> instructions = new ArrayList<>();
|
||||
|
||||
private BranchManger branchManager = new BranchManger( instructions );
|
||||
@ -96,11 +98,10 @@ public class ModuleGenerator {
|
||||
|
||||
private void iterateMethods( ClassFile classFile, Consumer<MethodInfo> handler ) throws WasmException {
|
||||
sourceFile = null; // clear previous value for the case an IO exception occur
|
||||
className = null;
|
||||
try {
|
||||
sourceFile = classFile.getSourceFile();
|
||||
if( sourceFile == null ) {
|
||||
sourceFile = classFile.getThisClass().getName();
|
||||
}
|
||||
className = classFile.getThisClass().getName();
|
||||
MethodInfo[] methods = classFile.getMethods();
|
||||
for( MethodInfo method : methods ) {
|
||||
Code code = method.getCode();
|
||||
@ -111,7 +112,7 @@ public class ModuleGenerator {
|
||||
handler.accept( method );
|
||||
}
|
||||
} catch( IOException ioex ) {
|
||||
throw WasmException.create( ioex, sourceFile, -1 );
|
||||
throw WasmException.create( ioex, sourceFile, className, -1 );
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,7 +137,7 @@ public class ModuleGenerator {
|
||||
writer.prepareFunction( name );
|
||||
}
|
||||
} catch( Exception ioex ) {
|
||||
throw WasmException.create( ioex, sourceFile, -1 );
|
||||
throw WasmException.create( ioex, sourceFile, className, -1 );
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,7 +173,7 @@ public class ModuleGenerator {
|
||||
}
|
||||
} catch( Exception ioex ) {
|
||||
int lineNumber = byteCode == null ? -1 : byteCode.getLineNumber();
|
||||
throw WasmException.create( ioex, sourceFile, lineNumber );
|
||||
throw WasmException.create( ioex, sourceFile, className, lineNumber );
|
||||
}
|
||||
}
|
||||
|
||||
@ -411,7 +412,7 @@ public class ModuleGenerator {
|
||||
case 94: // dup2_x2
|
||||
case 95: // swap
|
||||
// can be do with functions with more as one return value in future WASM standard
|
||||
throw new WasmException( "Stack duplicate is not supported in current WASM. try to save immediate values in a local variable: " + op, sourceFile, byteCode.getLineNumber() );
|
||||
throw new WasmException( "Stack duplicate is not supported in current WASM. try to save immediate values in a local variable: " + op, sourceFile, className, byteCode.getLineNumber() );
|
||||
case 96: // iadd
|
||||
instr = new WasmNumericInstruction( NumericOperator.add, ValueType.i32, codePos);
|
||||
break;
|
||||
@ -469,7 +470,7 @@ public class ModuleGenerator {
|
||||
case 114: // frem
|
||||
case 115: // drem
|
||||
//TODO can be implemented with a helper function like: (a - (long)(a / b) * (double)b)
|
||||
throw new WasmException( "Modulo/Remainder for floating numbers is not supported in WASM. Use int or long data types." + op, sourceFile, byteCode.getLineNumber() );
|
||||
throw new WasmException( "Modulo/Remainder for floating numbers is not supported in WASM. Use int or long data types." + op, sourceFile, className, byteCode.getLineNumber() );
|
||||
case 116: // ineg
|
||||
instructions.add( new WasmConstInstruction( -1, ValueType.i32, codePos ) );
|
||||
instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i32, codePos );
|
||||
@ -698,7 +699,7 @@ public class ModuleGenerator {
|
||||
//TODO case 200: // goto_w
|
||||
//TODO case 201: // jsr_w
|
||||
default:
|
||||
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, byteCode.getLineNumber() );
|
||||
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, className, byteCode.getLineNumber() );
|
||||
}
|
||||
if( instr != null ) {
|
||||
instructions.add( instr );
|
||||
@ -715,7 +716,7 @@ public class ModuleGenerator {
|
||||
} catch( WasmException ex ) {
|
||||
throw ex;
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex, sourceFile, byteCode.getLineNumber() );
|
||||
throw WasmException.create( ex, sourceFile, className, byteCode.getLineNumber() );
|
||||
}
|
||||
}
|
||||
|
||||
@ -819,7 +820,7 @@ public class ModuleGenerator {
|
||||
return instr.getCodePosition();
|
||||
}
|
||||
}
|
||||
throw new WasmException( "Switch start position not found", sourceFile, -1 ); // should never occur
|
||||
throw new WasmException( "Switch start position not found", sourceFile, className, -1 ); // should never occur
|
||||
}
|
||||
|
||||
/**
|
||||
@ -939,7 +940,7 @@ public class ModuleGenerator {
|
||||
numOp = NumericOperator.le;
|
||||
break;
|
||||
default:
|
||||
throw new WasmException( "Unexpected compare sub operation: " + nextOp, null, -1 );
|
||||
throw new WasmException( "Unexpected compare sub operation: " + nextOp, sourceFile, className, -1 );
|
||||
}
|
||||
int offset = byteCode.readShort();
|
||||
WasmNumericInstruction compare = new WasmNumericInstruction( numOp, valueType, codePos );
|
||||
|
@ -87,7 +87,7 @@ public enum ValueType {
|
||||
default:
|
||||
javaType = javaSignature.substring( idx, idx + 1 );
|
||||
}
|
||||
throw new WasmException( "Not supported Java data type in method signature: " + javaType, null, -1 );
|
||||
throw new WasmException( "Not supported Java data type in method signature: " + javaType, null, null, -1 );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ class WasmConstInstruction extends WasmInstruction {
|
||||
} else if( clazz == Double.class ) {
|
||||
return ValueType.f64;
|
||||
} else {
|
||||
throw new WasmException( "Not supported constant type: " + clazz, null, -1 );
|
||||
throw new WasmException( "Not supported constant type: " + clazz, null, null, -1 );
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user