compiler: emit byte constants properly

This commit is contained in:
Evgenii Stratonikov 2020-05-13 10:53:11 +03:00
parent 98c508d361
commit b4f1142149
2 changed files with 12 additions and 4 deletions

View file

@ -133,6 +133,7 @@ func (c *codegen) emitLoadConst(t types.TypeAndValue) {
switch typ.Kind() {
case types.Int, types.UntypedInt, types.Uint,
types.Int8, types.Uint8,
types.Int16, types.Uint16,
types.Int32, types.Uint32, types.Int64, types.Uint64:
val, _ := constant.Int64Val(t.Value)
@ -143,10 +144,6 @@ func (c *codegen) emitLoadConst(t types.TypeAndValue) {
case types.Bool, types.UntypedBool:
val := constant.BoolVal(t.Value)
emit.Bool(c.prog.BinWriter, val)
case types.Byte:
val, _ := constant.Int64Val(t.Value)
b := byte(val)
emit.Bytes(c.prog.BinWriter, []byte{b})
default:
c.prog.Err = fmt.Errorf("compiler doesn't know how to convert this basic type: %v", t)
return

View file

@ -36,6 +36,17 @@ func TestShortHandMultiConst(t *testing.T) {
eval(t, src, big.NewInt(6))
}
func TestByteConstant(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/convert"
const a byte = 0xFF
func Main() int64 {
x := convert.ToInteger(a)
return x+1
}`
eval(t, src, big.NewInt(0x100))
}
func TestGlobalsWithFunctionParams(t *testing.T) {
src := `
package foobar