From fbdd0eb3d0ba6e11dc3cda13b2ce37ce37551978 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 23 Jun 2019 12:55:14 +0200 Subject: [PATCH] improve error handling --- .../jwebassembly/WasmException.java | 31 ++++++++++++++++++- .../binary/BinaryModuleWriter.java | 12 +++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/WasmException.java b/src/de/inetsoftware/jwebassembly/WasmException.java index c0194e1..9c6d207 100644 --- a/src/de/inetsoftware/jwebassembly/WasmException.java +++ b/src/de/inetsoftware/jwebassembly/WasmException.java @@ -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. * diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index b1ad3ac..dabc579 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -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 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 );