-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
classpy-wasm/src/test/java/com/github/zxh/classpy/wasm/FuncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
classpy-wasm/src/test/java/com/github/zxh/classpy/wasm/LEB128Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |