Skip to content

Commit

Permalink
+ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zxh0 committed Oct 9, 2018
1 parent 8aa3af6 commit 2d981ae
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.nio.ByteOrder;

// https://en.wikipedia.org/wiki/LEB128
public class WasmBinReader extends BytesReader {

public WasmBinReader(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.zxh.classpy.wasm;

import com.github.zxh.classpy.common.FileComponent;
import com.github.zxh.classpy.wasm.instructions.Expr;
import com.github.zxh.classpy.wasm.instructions.Instr;
import com.github.zxh.classpy.wasm.sections.Code;
import org.junit.Test;

import java.util.Base64;

public class FuncTest {

@Test
public void read() {
String base64 = "AQJ/IAIEfwNAIAAsAAAiAyABLAAAIgRGBEAgAEEBaiEAIAFBAWohAUEAIAJBf2oiAkUNAxoMAQsLIANB/wFxIARB/wFxawVBAAsPCw==";
byte[] code = Base64.getDecoder().decode(base64);
Code.Func func = new Code.Func();
try {
func.read(new WasmBinReader(code));
} catch (Exception e) {
new ExprPrinter().printExpr((Expr) func.getComponents().get(1));
throw e;
}
}


private static class ExprPrinter {

private int indentation;

private void printExpr(Expr expr) {
for (FileComponent fc : expr.getComponents()) {
printInstr((Instr) fc);
}
}

private void printInstr(Instr instr) {
if (instr.getOpcode() == 0x0B
|| instr.getOpcode() == 0x05) {
indentation -= 1;
}
for (int i = 0; i < indentation; i++) {
System.out.print("\t");
}
System.out.println(instr.getName());
if (instr.getOpcode() == 0x02
|| instr.getOpcode() == 0x03
|| instr.getOpcode() == 0x04) {
indentation += 1;
}
for (FileComponent fc : instr.getComponents()) {
if (fc instanceof Instr) {
printInstr((Instr) fc);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.zxh.classpy.wasm;

import org.junit.Assert;
import org.junit.Test;

public class LEB128Test {

@Test
public void readU32() {
Assert.assertEquals(624485,
new WasmBinReader(new byte[] {(byte) 0xE5, (byte) 0x8E, 0x26}).readU32());
}

@Test
public void readS32() {
Assert.assertEquals(-624485,
new WasmBinReader(new byte[] {(byte) 0x9B, (byte) 0xF1, 0x59}).readS32());
}

}

0 comments on commit 2d981ae

Please sign in to comment.