Skip to content

Commit 52eb305

Browse files
committed
Fixed binary operators arguments
1 parent d6d63cd commit 52eb305

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/main/java/com/github/mikhasd/wasm/eval/Evaluator.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,97 @@ public class Evaluator {
1111
private int instructionPointer;
1212

1313
public WasmValue evaluate(byte[] opcodes, Type[] parameterTypes, Object[] args, Type returnType) {
14+
System.out.println("New invocation");
1415
instructionPointer = 0;
16+
var depth = 0;
1517
WasmValue[] locals = toLocals(parameterTypes, args);
1618
Stack<WasmValue> stack = new Stack<>();
1719
execution:
1820
while (instructionPointer < opcodes.length) {
1921
var opcode = opcodes[instructionPointer++];
22+
System.out.printf("Opcode 0x%02X ", opcode);
2023

2124

2225
switch (opcode) {
2326
// block
2427
case 0x02: {
28+
System.out.println("block");
29+
depth++;
2530
var blockType = opcodes[instructionPointer++];
2631
}
2732
break;
2833

2934
// end
3035
case 0x0B:
36+
System.out.println("end");
3137
break;
3238
// br_if
3339
case 0x0D: {
40+
System.out.println("br_if");
3441
var val = stack.pop();
3542
if (!val.type().equals(Type.I32))
3643
throw new WasmRuntimeException("0x0D:br_if expects I32 in the top of the stack but got " + val.type());
3744
var label = readUnsignedLeb128(opcodes);
3845
if (val.value().equals(1)) {
39-
instructionPointer = label;
46+
while(depth != label){
47+
while(0x0B != opcodes[instructionPointer++]);
48+
depth--;
49+
}
4050
}
4151
break;
4252
}
4353
// return
4454
case 0x0F: {
55+
System.out.println("return");
4556
break execution;
4657
}
4758

4859
// local.get
4960
case 0x20:
61+
System.out.println("local.get");
5062
var local = readUnsignedLeb128(opcodes);
5163
stack.push(locals[local]);
5264
break;
5365
// i32.const
5466
case 0x41: {
67+
System.out.println("i32.const");
5568
var val = readUnsignedLeb128(opcodes);
5669
var currentValue = new WasmValue<>(Type.I32, val);
5770
stack.push(currentValue);
5871
}
5972
break;
6073
// i32.lt_s
6174
case 0x48: {
62-
WasmValue<Integer> a = stack.pop();
75+
System.out.println("i32.lt_s");
6376
WasmValue<Integer> b = stack.pop();
77+
WasmValue<Integer> a = stack.pop();
6478
Integer d = a.value() < b.value() ? 1 : 0;
6579
stack.push(new WasmValue(Type.I32, d));
6680
}
6781
break;
6882
// i32.add
6983
case 0x6A: {
70-
WasmValue<Integer> a = stack.pop();
84+
System.out.println("i32.add");
7185
WasmValue<Integer> b = stack.pop();
86+
WasmValue<Integer> a = stack.pop();
7287
Integer d = a.value() + b.value();
7388
stack.push(new WasmValue(Type.I32, d));
7489
break;
7590
}
7691
// i32.mul
7792
case 0x6C: {
78-
WasmValue<Integer> a = stack.pop();
93+
System.out.println("i32.mul");
7994
WasmValue<Integer> b = stack.pop();
95+
WasmValue<Integer> a = stack.pop();
8096
Integer d = a.value() * b.value();
8197
stack.push(new WasmValue(Type.I32, d));
8298
break;
8399
}
84100
// i32.div_u
85101
case 0x6E: {
86-
WasmValue<Integer> a = stack.pop();
102+
System.out.println("i32.div_u");
87103
WasmValue<Integer> b = stack.pop();
104+
WasmValue<Integer> a = stack.pop();
88105
Integer d = Integer.divideUnsigned(a.value(), b.value());
89106
stack.push(new WasmValue(Type.I32, d));
90107
break;

src/test/java/com/github/mikhasd/wasm/eval/EvaluationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) throws IOException {
1919
new Parser().parse(buffer, handler);
2020
var module = handler.build();
2121
var instance = module.instantiate();
22-
WasmValue result = instance.call("do_the_magic", 1, 2);
22+
WasmValue result = instance.call("do_the_magic", 20, 2);
2323
System.out.println(result.value());
2424
}
2525
}

0 commit comments

Comments
 (0)