From f7f48d0048f30c8f617b9eec55fdd76c0d632ebf Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 23 Mar 2020 12:26:26 +0300 Subject: [PATCH] vm: implement ISNULL opcode --- pkg/vm/opcode/opcode.go | 2 ++ pkg/vm/vm.go | 4 ++++ pkg/vm/vm_test.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/pkg/vm/opcode/opcode.go b/pkg/vm/opcode/opcode.go index 269d24587..1aa39b64d 100644 --- a/pkg/vm/opcode/opcode.go +++ b/pkg/vm/opcode/opcode.go @@ -119,6 +119,8 @@ const ( SYSCALL Opcode = 0x68 TAILCALL Opcode = 0x69 + ISNULL Opcode = 0x70 + // Stack DUPFROMALTSTACK Opcode = 0x6A TOALTSTACK Opcode = 0x6B diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 56a9ec452..082f75b6d 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -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()) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 44c4d8ee8..7e6831d25 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -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