Skip to content

Commit

Permalink
interp: fix getting unsigned constant value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored Nov 9, 2020
1 parent 8367657 commit d0a34d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit d0a34d4

Please sign in to comment.