From 8b52741e049a111e9eb8298bbb95a0ac9e23694f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 18 Aug 2024 17:51:29 +0200 Subject: [PATCH] wasm: avoid generation of types as defined in GC spec This fixes passing tests in environments that either don't support or prohibit usage of GC spec --- .../wasm/generate/gc/classes/WasmGCClassGenerator.java | 1 + .../org/teavm/backend/wasm/model/WasmFunctionType.java | 9 +++++++++ .../wasm/render/WasmCompositeTypeBinaryRenderer.java | 10 ++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java index 5cf5d6cb7..761095795 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java @@ -407,6 +407,7 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit || expectedFunctionType != function.getType()) { var functionType = typeMapper.getFunctionType(virtualTable.getClassName(), method, true); functionType.getSupertypes().add(expectedFunctionType); + expectedFunctionType.setFinal(false); var wrapperFunction = new WasmFunction(functionType); module.functions.add(wrapperFunction); var call = new WasmCall(function); diff --git a/core/src/main/java/org/teavm/backend/wasm/model/WasmFunctionType.java b/core/src/main/java/org/teavm/backend/wasm/model/WasmFunctionType.java index 911fe89c1..7271ea63b 100644 --- a/core/src/main/java/org/teavm/backend/wasm/model/WasmFunctionType.java +++ b/core/src/main/java/org/teavm/backend/wasm/model/WasmFunctionType.java @@ -26,6 +26,7 @@ public class WasmFunctionType extends WasmCompositeType { private Supplier> parameterTypesSupplier; private Supplier returnTypeSupplier; private Set supertypes = new LinkedHashSet<>(); + private boolean isFinal = true; public WasmFunctionType(String name, WasmType returnType, List parameterTypes) { super(name); @@ -60,6 +61,14 @@ public class WasmFunctionType extends WasmCompositeType { return supertypes; } + public boolean isFinal() { + return isFinal; + } + + public void setFinal(boolean aFinal) { + isFinal = aFinal; + } + @Override public void acceptVisitor(WasmCompositeTypeVisitor visitor) { visitor.visit(this); diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmCompositeTypeBinaryRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmCompositeTypeBinaryRenderer.java index a3132396b..630c29337 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmCompositeTypeBinaryRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmCompositeTypeBinaryRenderer.java @@ -57,10 +57,12 @@ public class WasmCompositeTypeBinaryRenderer implements WasmCompositeTypeVisitor @Override public void visit(WasmFunctionType type) { - section.writeByte(0x50); - section.writeLEB(type.getSupertypes().size()); - for (var supertype : type.getSupertypes()) { - section.writeLEB(module.types.indexOf(supertype)); + if (!type.isFinal() || !type.getSupertypes().isEmpty()) { + section.writeByte(0x50); + section.writeLEB(type.getSupertypes().size()); + for (var supertype : type.getSupertypes()) { + section.writeLEB(module.types.indexOf(supertype)); + } } section.writeByte(0x60); section.writeLEB(type.getParameterTypes().size());