vm: implement ISNULL opcode

This commit is contained in:
Evgenii Stratonikov 2020-03-23 12:26:26 +03:00
parent 3db030bbb6
commit f7f48d0048
3 changed files with 22 additions and 0 deletions

View file

@ -119,6 +119,8 @@ const (
SYSCALL Opcode = 0x68
TAILCALL Opcode = 0x69
ISNULL Opcode = 0x70
// Stack
DUPFROMALTSTACK Opcode = 0x6A
TOALTSTACK Opcode = 0x6B

View file

@ -563,6 +563,10 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case opcode.PUSHNULL:
v.estack.PushVal(NullItem{})
case opcode.ISNULL:
res := v.estack.Pop().value.Equals(NullItem{})
v.estack.PushVal(res)
// Stack operations.
case opcode.TOALTSTACK:
v.astack.Push(v.estack.Pop())

View file

@ -206,6 +206,22 @@ func TestPUSHNULL(t *testing.T) {
require.True(t, v.estack.Pop().Bool())
}
func TestISNULL(t *testing.T) {
t.Run("Integer", func(t *testing.T) {
prog := makeProgram(opcode.PUSH1, opcode.ISNULL)
v := load(prog)
runVM(t, v)
require.False(t, v.estack.Pop().Bool())
})
t.Run("Null", func(t *testing.T) {
prog := makeProgram(opcode.PUSHNULL, opcode.ISNULL)
v := load(prog)
runVM(t, v)
require.True(t, v.estack.Pop().Bool())
})
}
// appendBigStruct returns a program which:
// 1. pushes size Structs on stack
// 2. packs them into a new struct