Skip to content

Commit 8aa3af6

Browse files
committed
support WASM
1 parent 8dc5e5f commit 8aa3af6

File tree

35 files changed

+1359
-1
lines changed

35 files changed

+1359
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ subprojects {
2828
apply plugin: 'java'
2929
//apply plugin: 'application'
3030

31-
sourceCompatibility = "1.8";
31+
sourceCompatibility = "1.8"
3232
//mainClassName = 'com.github.zxh0.luago.Main'
3333

3434
sourceCompatibility = '1.8'

classpy-common/src/main/java/com/github/zxh/classpy/common/FileComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ protected final void add(String name, FileComponent subComponent) {
5555
components.add(subComponent);
5656
}
5757

58+
protected final void clear() {
59+
components.clear();
60+
}
61+
5862
/**
5963
* The returned string will be displayed by BytesTreeItem.
6064
*

classpy-gui/src/main/java/com/github/zxh/classpy/gui/MyMenuBar.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ private Menu createOpenMenu() {
4646
openMenu.getItems().add(createOpenMenuItem(FileType.JAVA_JAR));
4747
openMenu.getItems().add(createOpenMenuItem(FileType.JAVA_CLASS));
4848
openMenu.getItems().add(createOpenMenuItem(FileType.LUA_BC));
49+
openMenu.getItems().add(createOpenMenuItem(FileType.WASM));
4950
openMenu.setMnemonicParsing(true);
5051
return openMenu;
5152
}

classpy-gui/src/main/java/com/github/zxh/classpy/gui/support/FileType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public enum FileType {
1111
JAVA_JAR("/jar.png", "Java JAR", "*.jar"),
1212
JAVA_CLASS("/java.png", "Java Class", "*.class"),
1313
LUA_BC("/lua.png", "Lua Binary Chunk", "*.luac"),
14+
WASM("/wasm.png", "WebAssembly Binary Code", "*.wasm"),
1415
UNKNOWN("/file.png", "Unknown", "*.*"),
1516
;
1617

classpy-gui/src/main/java/com/github/zxh/classpy/gui/support/FileTypeInferer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class FileTypeInferer {
77

8+
private static final byte[] wasmMagicNumber = {0, 'a', 's', 'm'};
89
private static final byte[] binaryChunkSig = {0x1B, 'L', 'u', 'a'};
910
private static final byte[] classMagicNumber = {
1011
(byte) 0xCA,
@@ -24,6 +25,9 @@ public static FileType inferFileType(URL url) {
2425
if (filename.endsWith(".luac")) {
2526
return FileType.LUA_BC;
2627
}
28+
if (filename.endsWith(".wasm")) {
29+
return FileType.WASM;
30+
}
2731
return FileType.UNKNOWN;
2832
}
2933

@@ -36,6 +40,9 @@ public static FileType inferFileType(byte[] data) {
3640
if (Arrays.equals(magicNumber, binaryChunkSig)) {
3741
return FileType.LUA_BC;
3842
}
43+
if (Arrays.equals(magicNumber, wasmMagicNumber)) {
44+
return FileType.WASM;
45+
}
3946
}
4047
return FileType.UNKNOWN;
4148
}

classpy-gui/src/main/java/com/github/zxh/classpy/gui/support/OpenFileTask.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.URL;
1313
import java.util.function.Consumer;
1414

15+
import com.github.zxh.classpy.wasm.WasmBinParser;
1516
import javafx.concurrent.Task;
1617

1718
public class OpenFileTask extends Task<OpenFileResult> {
@@ -49,6 +50,7 @@ private static FileComponent parse(byte[] data, FileType fileType) {
4950
switch (fileType) {
5051
case JAVA_CLASS: return new ClassFileParser().parse(data);
5152
case LUA_BC: return new BinaryChunkParser().parse(data);
53+
case WASM: return new WasmBinParser().parse(data);
5254
default: return new FileComponent() {}; // todo
5355
}
5456
}
1.05 KB
Loading

classpy-wasm/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
compile project(':classpy-common')
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.zxh.classpy.wasm;
2+
3+
import java.util.function.Supplier;
4+
5+
public class Vector extends WasmBinComponent {
6+
7+
private Supplier<? extends WasmBinComponent> supplier;
8+
9+
public Vector(Supplier<? extends WasmBinComponent> supplier) {
10+
this.supplier = supplier;
11+
}
12+
13+
@Override
14+
protected void readContent(WasmBinReader reader) {
15+
int length = readU32(reader, "length");
16+
for (int i = 0; i < length; i++) {
17+
WasmBinComponent element = supplier.get();
18+
add(null, element);
19+
element.read(reader);
20+
}
21+
}
22+
23+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.github.zxh.classpy.wasm;
2+
3+
import com.github.zxh.classpy.common.FileComponent;
4+
import com.github.zxh.classpy.wasm.instructions.Expr;
5+
import com.github.zxh.classpy.wasm.types.ValType;
6+
import com.github.zxh.classpy.wasm.values.Byte;
7+
import com.github.zxh.classpy.wasm.values.Bytes;
8+
import com.github.zxh.classpy.wasm.values.Name;
9+
import com.github.zxh.classpy.wasm.values.U32;
10+
11+
import java.util.function.Supplier;
12+
13+
public class WasmBinComponent extends FileComponent {
14+
15+
public final void read(WasmBinReader reader) {
16+
try {
17+
int offset = reader.getPosition();
18+
readContent(reader);
19+
int length = reader.getPosition() - offset;
20+
super.setOffset(offset);
21+
super.setLength(length);
22+
} catch (Exception e) {
23+
System.out.println("error parsing: " + getClass());
24+
throw e;
25+
}
26+
}
27+
28+
protected void readContent(WasmBinReader reader) {
29+
for (FileComponent fc : getComponents()) {
30+
((WasmBinComponent) fc).read(reader);
31+
}
32+
}
33+
34+
protected void postRead() {
35+
36+
}
37+
38+
protected int readU32(WasmBinReader reader, String name) {
39+
U32 u32 = new U32();
40+
add(name, u32);
41+
u32.read(reader);
42+
return u32.getIntValue();
43+
}
44+
45+
protected int readByte(WasmBinReader reader, String name) {
46+
Byte b = new Byte();
47+
add(name, b);
48+
b.read(reader);
49+
return b.getValue();
50+
}
51+
52+
protected int readByte(WasmBinReader reader, String name, byte... expectedValues) {
53+
Byte b = new Byte(expectedValues);
54+
add(name, b);
55+
b.read(reader);
56+
return b.getValue();
57+
}
58+
59+
protected byte[] readBytes(WasmBinReader reader, String name, int n) {
60+
Bytes bytes = new Bytes(n);
61+
add(name, bytes);
62+
bytes.read(reader);
63+
return bytes.getBytes();
64+
}
65+
66+
protected <T extends WasmBinComponent> T read(WasmBinReader reader,
67+
String name,
68+
Supplier<T> supplier) {
69+
T c = supplier.get();
70+
add(name, c);
71+
c.read(reader);
72+
return c;
73+
}
74+
75+
protected void readVector(WasmBinReader reader, String name,
76+
Supplier<? extends WasmBinComponent> supplier) {
77+
Vector vec = new Vector(supplier);
78+
add(name, vec);
79+
vec.read(reader);
80+
}
81+
82+
protected void _byte(String name, byte... expectedValues) {
83+
add(name, new Byte(expectedValues));
84+
}
85+
86+
protected void u32(String name) {
87+
add(name, new U32());
88+
}
89+
90+
protected void name(String name) {
91+
add(name, new Name());
92+
}
93+
94+
protected void valType(String name) {
95+
add(name, new ValType());
96+
}
97+
98+
protected void expr(String name) {
99+
add(name, new Expr());
100+
}
101+
102+
protected void vector(String name,
103+
Supplier<? extends WasmBinComponent> supplier) {
104+
add(name, new Vector(supplier));
105+
}
106+
107+
}

0 commit comments

Comments
 (0)