#253 WASM Memory should be exported to make it accessible from JS

Now a default export named "memory" is created to export the WASM memory.
This commit is contained in:
sertic 2017-04-04 15:55:55 +02:00
parent 19b804b5fc
commit f84a646eae
2 changed files with 14 additions and 8 deletions

View File

@ -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<WasmFunction> 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<WasmFunction> 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());
}

View File

@ -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) {