diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCGenerationContext.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCGenerationContext.java index be2e049b0..fcc548726 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCGenerationContext.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCGenerationContext.java @@ -107,6 +107,7 @@ public class WasmGCGenerationContext implements BaseWasmGenerationContext { public WasmTag getExceptionTag() { if (exceptionTag == null) { exceptionTag = new WasmTag(functionTypes.of(null)); + exceptionTag.setExportName("javaException"); module.tags.add(exceptionTag); } return exceptionTag; diff --git a/core/src/main/java/org/teavm/backend/wasm/model/WasmTag.java b/core/src/main/java/org/teavm/backend/wasm/model/WasmTag.java index 349a680c6..16a32a042 100644 --- a/core/src/main/java/org/teavm/backend/wasm/model/WasmTag.java +++ b/core/src/main/java/org/teavm/backend/wasm/model/WasmTag.java @@ -17,8 +17,7 @@ package org.teavm.backend.wasm.model; public class WasmTag extends WasmEntity { private WasmFunctionType type; - WasmModule module; - int index; + private String exportName; public WasmTag(WasmFunctionType type) { this.type = type; @@ -28,6 +27,14 @@ public class WasmTag extends WasmEntity { return type; } + public String getExportName() { + return exportName; + } + + public void setExportName(String exportName) { + this.exportName = exportName; + } + public int getIndex() { return index; } diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java index 605296bf0..4a39b169a 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java @@ -48,6 +48,7 @@ public class WasmBinaryRenderer { private static final int EXTERNAL_KIND_FUNCTION = 0; private static final int EXTERNAL_KIND_MEMORY = 2; + private static final int EXTERNAL_KIND_TAG = 4; private WasmBinaryWriter output; private WasmBinaryVersion version; @@ -235,7 +236,11 @@ public class WasmBinaryRenderer { .filter(function -> function.getExportName() != null) .collect(Collectors.toList()); - section.writeLEB(functions.size() + 1); + var tags = module.tags.stream() + .filter(tag -> tag.getExportName() != null) + .collect(Collectors.toList()); + + section.writeLEB(functions.size() + tags.size() + 1); for (var function : functions) { int functionIndex = module.functions.indexOf(function); @@ -244,6 +249,13 @@ public class WasmBinaryRenderer { section.writeByte(EXTERNAL_KIND_FUNCTION); section.writeLEB(functionIndex); } + for (var tag : tags) { + var tagIndex = module.tags.indexOf(tag); + section.writeAsciiString(tag.getExportName()); + + section.writeByte(EXTERNAL_KIND_TAG); + section.writeLEB(tagIndex); + } // We also need to export the memory to make it accessible section.writeAsciiString("memory");