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.
This commit is contained in:
Marc Vertes
2020-11-09 17:40:04 +01:00
committed by GitHub
parent 83676577ac
commit d0a34d467b
2 changed files with 14 additions and 0 deletions

View File

@@ -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{

View File

@@ -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())