From d0a34d467bf59deb3328fe338c759afccaf17337 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 9 Nov 2020 17:40:04 +0100 Subject: [PATCH] interp: fix getting unsigned constant value The function vUint, used to get the unsigned integer value of a value, variable (frame) or constant, was broken for constant.Value expression. Fixes #948. --- interp/interp_eval_test.go | 10 ++++++++++ interp/value.go | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/interp/interp_eval_test.go b/interp/interp_eval_test.go index 2a1672488..5a7d4b4b9 100644 --- a/interp/interp_eval_test.go +++ b/interp/interp_eval_test.go @@ -82,6 +82,16 @@ func TestEvalShift(t *testing.T) { }) } +func TestOpVarConst(t *testing.T) { + i := interp.New(interp.Options{}) + runTests(t, i, []testCase{ + {pre: func() { eval(t, i, "const a uint = 8 + 2") }, src: "a", res: "10"}, + {src: "b := uint(5); a+b", res: "15"}, + {src: "b := uint(5); b+a", res: "15"}, + {src: "b := uint(5); b>a", res: "false"}, + }) +} + func TestEvalStar(t *testing.T) { i := interp.New(interp.Options{}) runTests(t, i, []testCase{ diff --git a/interp/value.go b/interp/value.go index d1dc47db7..adf5b60fa 100644 --- a/interp/value.go +++ b/interp/value.go @@ -342,6 +342,10 @@ func vInt(v reflect.Value) (i int64) { } func vUint(v reflect.Value) (i uint64) { + if c := vConstantValue(v); c != nil { + i, _ = constant.Uint64Val(constant.ToInt(c)) + return i + } switch v.Type().Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: i = uint64(v.Int())