vm: implement the NZ opcode

This commit is contained in:
Roman Khimov 2019-09-05 15:05:44 +03:00
parent bcd81bcfb2
commit fcf9c1213b
2 changed files with 20 additions and 2 deletions

View file

@ -499,8 +499,8 @@ func (v *VM) execute(ctx *Context, op Instruction) {
v.estack.PushVal(!x)
case NZ:
panic("todo NZ")
// x := v.estack.Pop().BigInt()
x := v.estack.Pop().BigInt()
v.estack.PushVal(x.Cmp(big.NewInt(0)) != 0)
// Object operations.
case NEWARRAY:

View file

@ -231,6 +231,24 @@ func TestSimpleCall(t *testing.T) {
assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64()))
}
func TestNZtrue(t *testing.T) {
prog := makeProgram(NZ)
vm := load(prog)
vm.estack.PushVal(1)
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, true, vm.estack.Pop().Bool())
}
func TestNZfalse(t *testing.T) {
prog := makeProgram(NZ)
vm := load(prog)
vm.estack.PushVal(0)
vm.Run()
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, false, vm.estack.Pop().Bool())
}
func makeProgram(opcodes ...Instruction) []byte {
prog := make([]byte, len(opcodes)+1) // RET
for i := 0; i < len(opcodes); i++ {