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 a7fa46d4d..2b27f1779 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 @@ -41,6 +41,7 @@ public class WasmBinaryRenderer { private static final int SECTION_DATA = 11; private static final int EXTERNAL_KIND_FUNCTION = 0; + private static final int EXTERNAL_KIND_MEMORY = 2; private WasmBinaryWriter output; private WasmBinaryVersion version; @@ -186,16 +187,16 @@ public class WasmBinaryRenderer { } private void renderExport(WasmModule module) { - List functions = module.getFunctions().values().stream() - .filter(function -> function.getExportName() != null) - .collect(Collectors.toList()); - if (functions.isEmpty()) { - return; - } + + // https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#export-section WasmBinaryWriter section = new WasmBinaryWriter(); - section.writeLEB(functions.size()); + List functions = module.getFunctions().values().stream() + .filter(function -> function.getExportName() != null) + .collect(Collectors.toList()); + + section.writeLEB(functions.size() + 1); for (WasmFunction function : functions) { int functionIndex = functionIndexes.get(function.getName()); @@ -205,6 +206,11 @@ public class WasmBinaryRenderer { section.writeLEB(functionIndex); } + // We also need to export the memory to make it accessible + section.writeAsciiString("memory"); + section.writeByte(EXTERNAL_KIND_MEMORY); + section.writeLEB(0); + writeSection(SECTION_EXPORT, "export", section.getData()); } diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderer.java index 58ac4ecfb..3c421a4c7 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderer.java @@ -81,7 +81,7 @@ public class WasmRenderer { public void renderMemory(WasmModule module) { visitor.lf(); - visitor.open().append("memory " + module.getMemorySize()).close().lf(); + visitor.open().append("memory (export \"memory\") " + module.getMemorySize()).close().lf(); } public void renderData(WasmModule module) {