From 954e8cdcf13c1958594892b2ca72a860d3417e00 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 18 Mar 2020 11:45:15 +0300 Subject: [PATCH] vm: allow to push integer-like items to stack Perform automatic conversion of non-standard integer types to int64 if possible. Closes #776. --- pkg/vm/stack_item.go | 5 +++++ pkg/vm/stack_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/vm/stack_item.go b/pkg/vm/stack_item.go index 0a03fe20f..71529f9f9 100644 --- a/pkg/vm/stack_item.go +++ b/pkg/vm/stack_item.go @@ -87,6 +87,11 @@ func makeStackItem(v interface{}) StackItem { } return makeStackItem(a) default: + i64T := reflect.TypeOf(int64(0)) + if reflect.TypeOf(val).ConvertibleTo(i64T) { + i64Val := reflect.ValueOf(val).Convert(i64T).Interface() + return makeStackItem(i64Val) + } panic( fmt.Sprintf( "invalid stack item type: %v (%v)", diff --git a/pkg/vm/stack_test.go b/pkg/vm/stack_test.go index dc0fe8bd8..a1964bd56 100644 --- a/pkg/vm/stack_test.go +++ b/pkg/vm/stack_test.go @@ -1,9 +1,11 @@ package vm import ( + "math/big" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPushElement(t *testing.T) { @@ -20,6 +22,20 @@ func TestPushElement(t *testing.T) { } } +func TestStack_PushVal(t *testing.T) { + type ( + i32 int32 + testByte uint8 + ) + + s := NewStack("test") + require.NotPanics(t, func() { s.PushVal(i32(123)) }) + require.NotPanics(t, func() { s.PushVal(testByte(42)) }) + require.Equal(t, 2, s.Len()) + require.Equal(t, big.NewInt(42), s.Pop().Value()) + require.Equal(t, big.NewInt(123), s.Pop().Value()) +} + func TestPopElement(t *testing.T) { var ( s = NewStack("test")