mirror of
https://github.com/sirjonasxx/G-Wasm.git
synced 2024-11-22 16:30:51 +01:00
fix Module class
This commit is contained in:
parent
dc8a93518f
commit
198cea9968
@ -1,9 +1,5 @@
|
|||||||
import disassembly.InvalidOpCodeException;
|
import disassembly.InvalidOpCodeException;
|
||||||
import disassembly.modules.Magic;
|
|
||||||
import disassembly.modules.Module;
|
import disassembly.modules.Module;
|
||||||
import disassembly.modules.Version;
|
|
||||||
import disassembly.modules.sections.SectionFactory;
|
|
||||||
import disassembly.modules.sections.type.TypeSection;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
@ -2,9 +2,7 @@ package disassembly.modules;
|
|||||||
|
|
||||||
import disassembly.InvalidOpCodeException;
|
import disassembly.InvalidOpCodeException;
|
||||||
import disassembly.WASMOpCode;
|
import disassembly.WASMOpCode;
|
||||||
import disassembly.modules.indices.TypeIdx;
|
|
||||||
import disassembly.modules.sections.Section;
|
import disassembly.modules.sections.Section;
|
||||||
import disassembly.modules.sections.SectionFactory;
|
|
||||||
import disassembly.modules.sections.code.CodeSection;
|
import disassembly.modules.sections.code.CodeSection;
|
||||||
import disassembly.modules.sections.custom.CustomSection;
|
import disassembly.modules.sections.custom.CustomSection;
|
||||||
import disassembly.modules.sections.custom.CustomSectionFactory;
|
import disassembly.modules.sections.custom.CustomSectionFactory;
|
||||||
@ -23,7 +21,6 @@ import java.io.BufferedInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Module extends WASMOpCode {
|
public class Module extends WASMOpCode {
|
||||||
@ -52,27 +49,27 @@ public class Module extends WASMOpCode {
|
|||||||
version = new Version(in);
|
version = new Version(in);
|
||||||
|
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
typeSection = new TypeSection(in);
|
typeSection = isNextSection(in, 1) ? new TypeSection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
importSection = new ImportSection(in);
|
importSection = isNextSection(in, 2) ? new ImportSection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
functionSection = new FunctionSection(in);
|
functionSection = isNextSection(in, 3) ? new FunctionSection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
// tableSection = new TableSection(in);
|
tableSection = isNextSection(in, 4) ? new TableSection(in) : null;
|
||||||
// disassembleCustomSections(in);
|
|
||||||
// memorySection = new MemorySection(in);
|
|
||||||
// disassembleCustomSections(in);
|
|
||||||
globalSection = new GlobalSection(in);
|
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
exportSection = new ExportSection(in);
|
memorySection = isNextSection(in, 5) ? new MemorySection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
// startSection = new StartSection(in);
|
globalSection = isNextSection(in , 6) ? new GlobalSection(in) : null;
|
||||||
// disassembleCustomSections(in);
|
|
||||||
elementSection = new ElementSection(in);
|
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
codeSection = new CodeSection(in);
|
exportSection = isNextSection(in, 7) ? new ExportSection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
dataSection = new DataSection(in);
|
startSection = isNextSection(in, 8) ? new StartSection(in) : null;
|
||||||
|
disassembleCustomSections(in);
|
||||||
|
elementSection = isNextSection(in, 9) ? new ElementSection(in) : null;
|
||||||
|
disassembleCustomSections(in);
|
||||||
|
codeSection = isNextSection(in, 10) ? new CodeSection(in) : null;
|
||||||
|
disassembleCustomSections(in);
|
||||||
|
dataSection = isNextSection(in, 11) ? new DataSection(in) : null;
|
||||||
disassembleCustomSections(in);
|
disassembleCustomSections(in);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -91,61 +88,54 @@ public class Module extends WASMOpCode {
|
|||||||
this.elementSection = elementSection;
|
this.elementSection = elementSection;
|
||||||
this.codeSection = codeSection;
|
this.codeSection = codeSection;
|
||||||
this.dataSection = dataSection;
|
this.dataSection = dataSection;
|
||||||
|
|
||||||
|
this.customSectionsList = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 12; i++) {
|
||||||
|
customSectionsList.add(new ArrayList<>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Module(TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection, List<List<CustomSection>> customSectionsList) {
|
public Module(TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection, List<List<CustomSection>> customSectionsList) {
|
||||||
this.magic = new Magic();
|
this(new Magic(), new Version(), typeSection, importSection, functionSection, tableSection, memorySection,
|
||||||
this.version = new Version(new byte[]{1, 0, 0, 0});
|
globalSection, exportSection, startSection, elementSection, codeSection, dataSection);
|
||||||
|
|
||||||
this.typeSection = typeSection;
|
|
||||||
this.importSection = importSection;
|
|
||||||
this.functionSection = functionSection;
|
|
||||||
this.tableSection = tableSection;
|
|
||||||
this.memorySection = memorySection;
|
|
||||||
this.globalSection = globalSection;
|
|
||||||
this.exportSection = exportSection;
|
|
||||||
this.startSection = startSection;
|
|
||||||
this.elementSection = elementSection;
|
|
||||||
this.codeSection = codeSection;
|
|
||||||
this.dataSection = dataSection;
|
|
||||||
this.customSectionsList = customSectionsList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disassembleCustomSections(BufferedInputStream in) throws IOException, InvalidOpCodeException {
|
private void disassembleCustomSections(BufferedInputStream in) throws IOException, InvalidOpCodeException {
|
||||||
List<CustomSection> customSections = new ArrayList<>();
|
List<CustomSection> customSections = new ArrayList<>();
|
||||||
|
|
||||||
int nextSectionId = SectionFactory.readSectionId(in);
|
while (isNextSection(in, 0)) {
|
||||||
if (nextSectionId == 0) {
|
|
||||||
customSections.add(CustomSectionFactory.get(in));
|
customSections.add(CustomSectionFactory.get(in));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
in.reset();
|
||||||
customSectionsList.add(customSections);
|
customSectionsList.add(customSections);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNextSection(BufferedInputStream in, int id) throws IOException {
|
||||||
|
in.mark(1);
|
||||||
|
if (in.read() == id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
in.reset();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
|
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
|
||||||
// Section[] sections = new Section[]{typeSection, importSection, functionSection, tableSection,
|
|
||||||
// memorySection, globalSection, exportSection, startSection, elementSection, codeSection,
|
|
||||||
// dataSection};
|
|
||||||
//
|
|
||||||
// for (int i = 0; i < 11; i++) {
|
|
||||||
// assembleCustomSections(out, i);
|
|
||||||
// sections[i].assemble(out);
|
|
||||||
// }
|
|
||||||
// assembleCustomSections(out, 11);
|
|
||||||
|
|
||||||
magic.assemble(out);
|
magic.assemble(out);
|
||||||
version.assemble(out);
|
version.assemble(out);
|
||||||
|
|
||||||
Section[] sections = new Section[]{typeSection, importSection, functionSection, globalSection, exportSection,
|
Section[] sections = new Section[]{typeSection, importSection, functionSection, tableSection,
|
||||||
elementSection, codeSection,
|
memorySection, globalSection, exportSection, startSection, elementSection, codeSection,
|
||||||
dataSection};
|
dataSection};
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 11; i++) {
|
||||||
assembleCustomSections(out, i);
|
assembleCustomSections(out, i);
|
||||||
sections[i].assemble(out);
|
if (sections[i] != null) {
|
||||||
|
sections[i].assemble(out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assembleCustomSections(out, 8);
|
assembleCustomSections(out, 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assembleCustomSections(OutputStream out, int location) throws IOException, InvalidOpCodeException {
|
private void assembleCustomSections(OutputStream out, int location) throws IOException, InvalidOpCodeException {
|
||||||
|
@ -21,6 +21,10 @@ public class Version extends WASMOpCode {
|
|||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Version() {
|
||||||
|
version = new byte[]{1, 0, 0, 0};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
|
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
|
||||||
out.write(version);
|
out.write(version);
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
package disassembly.modules.sections;
|
|
||||||
|
|
||||||
import disassembly.InvalidOpCodeException;
|
|
||||||
import disassembly.modules.sections.code.CodeSection;
|
|
||||||
import disassembly.modules.sections.custom.CustomSectionFactory;
|
|
||||||
import disassembly.modules.sections.data.DataSection;
|
|
||||||
import disassembly.modules.sections.element.ElementSection;
|
|
||||||
import disassembly.modules.sections.export.ExportSection;
|
|
||||||
import disassembly.modules.sections.function.FunctionSection;
|
|
||||||
import disassembly.modules.sections.global.GlobalSection;
|
|
||||||
import disassembly.modules.sections.imprt.ImportSection;
|
|
||||||
import disassembly.modules.sections.memory.MemorySection;
|
|
||||||
import disassembly.modules.sections.start.StartSection;
|
|
||||||
import disassembly.modules.sections.table.TableSection;
|
|
||||||
import disassembly.modules.sections.type.TypeSection;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class SectionFactory {
|
|
||||||
|
|
||||||
private static Map<Integer, SectionSupplier> allSections;
|
|
||||||
static {
|
|
||||||
allSections = new HashMap<>();
|
|
||||||
allSections.put(0, CustomSectionFactory::get);
|
|
||||||
allSections.put(1, TypeSection::new);
|
|
||||||
allSections.put(2, ImportSection::new);
|
|
||||||
allSections.put(3, FunctionSection::new);
|
|
||||||
allSections.put(4, TableSection::new);
|
|
||||||
allSections.put(5, MemorySection::new);
|
|
||||||
allSections.put(6, GlobalSection::new);
|
|
||||||
allSections.put(7, ExportSection::new);
|
|
||||||
allSections.put(8, StartSection::new);
|
|
||||||
allSections.put(9, ElementSection::new);
|
|
||||||
allSections.put(10, CodeSection::new);
|
|
||||||
allSections.put(11, DataSection::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private int minimumSectionId = 1;
|
|
||||||
|
|
||||||
private BufferedInputStream in;
|
|
||||||
|
|
||||||
public SectionFactory(BufferedInputStream in) {
|
|
||||||
this.in = in;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns null if none left
|
|
||||||
public Section get() throws IOException, InvalidOpCodeException {
|
|
||||||
int sectionType = in.read();
|
|
||||||
if (sectionType == -1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sectionType != 0) {
|
|
||||||
if (sectionType < minimumSectionId) {
|
|
||||||
throw new InvalidOpCodeException("Unexpected WASM section");
|
|
||||||
}
|
|
||||||
minimumSectionId = sectionType + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return allSections.get(sectionType).get(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int readSectionId(BufferedInputStream in) throws IOException {
|
|
||||||
// in.mark(1);
|
|
||||||
int id = in.read();
|
|
||||||
// in.reset();
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user