From 54d2deef4f27813966e74e4e2808f62e12c17f2a Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 12 Jan 2020 12:42:31 +0100 Subject: [PATCH] add option WASM_USE_EH to ignore exception support because currently not supported --- src/de/inetsoftware/jwebassembly/JWebAssembly.java | 7 ++++++- .../jwebassembly/binary/BinaryModuleWriter.java | 8 ++++++-- .../jwebassembly/module/ModuleGenerator.java | 4 +++- .../jwebassembly/text/TextModuleWriter.java | 4 ++-- .../jwebassembly/wasm/WasmOptions.java | 14 +++++++++++++- .../jwebassembly/runtime/Exceptions.java | 4 +++- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index fa2ec21..86723cd 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Volker Berlin (i-net software) + * Copyright 2017 - 2020 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. @@ -93,6 +93,11 @@ public class JWebAssembly { */ public static final String WASM_USE_GC = "wasm.use_gc"; + /** + * If the exception handling feature of WASM should be use or an unreachable instruction. If true use the exception instructions of WASM. + */ + public static final String WASM_USE_EH = "wasm.use_eh"; + /** * The logger instance */ diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index ab998b3..fbd897c 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -1248,8 +1248,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod codeStream.writeOpCode( CATCH ); break; case THROW: - codeStream.writeOpCode( THROW ); - codeStream.writeVaruint32( 0 ); // event/exception ever 0 because currently there is only one with signature anyref + if( options.useEH() ) { + codeStream.writeOpCode( THROW ); + codeStream.writeVaruint32( 0 ); // event/exception ever 0 because currently there is only one with signature anyref + } else { + codeStream.writeOpCode( UNREACHABLE ); + } break; case RETHROW: codeStream.writeOpCode( RETHROW ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index c93a987..86028ce 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -514,7 +514,9 @@ public class ModuleGenerator { case CATCH: case THROW: case RETHROW: - writer.writeException(); + if( writer.options.useEH() ) { + writer.writeException(); + } break; default: } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 326607d..446d647 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2019 Volker Berlin (i-net software) + * Copyright 2017 - 2020 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. @@ -762,7 +762,7 @@ public class TextModuleWriter extends ModuleWriter { insetAfter++; break; case THROW: - name = "throw 0"; // currently there is only one event/exception with anyref + name = options.useEH() ? "throw 0" : "unreachable"; // currently there is only one event/exception with anyref break; case RETHROW: name = "rethrow"; diff --git a/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java index 52bb0ce..2b7e28d 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java +++ b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2019 Volker Berlin (i-net software) + * Copyright 2017 - 2020 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. @@ -33,6 +33,8 @@ public class WasmOptions { private final boolean useGC; + private final boolean useEH; + @Nonnull private final String sourceMapBase; @@ -50,6 +52,7 @@ public class WasmOptions { public WasmOptions( HashMap properties ) { debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) ); useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) ); + useEH = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_EH, "false" ) ); String base = properties.getOrDefault( JWebAssembly.SOURCE_MAP_BASE, "" ); if( !base.isEmpty() && !base.endsWith( "/" ) ) { base += "/"; @@ -75,6 +78,15 @@ public class WasmOptions { return useGC; } + /** + * If the exception handling feature of WASM should be use or an unreachable instruction. + * + * @return true, use the EH instructions of WASM; false, generate an unreachable instruction + */ + public boolean useEH() { + return useEH; + } + /** * Get the relative path between the final wasm file location and the source files location. * If not empty it should end with a slash like "../../src/main/java/". diff --git a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java index eefc80d..354157d 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/Exceptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 - 2019 Volker Berlin (i-net software) + * Copyright 2018 - 2020 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. @@ -23,6 +23,7 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runners.Parameterized.Parameters; +import de.inetsoftware.jwebassembly.JWebAssembly; import de.inetsoftware.jwebassembly.ScriptEngine; import de.inetsoftware.jwebassembly.WasmRule; import de.inetsoftware.jwebassembly.api.annotation.Export; @@ -47,6 +48,7 @@ public class Exceptions extends AbstractBaseTest { addParam( list, script, "complex" ); } rule.setTestParameters( list ); + rule.setProperty( JWebAssembly.WASM_USE_EH, "true" ); return list; }