Skip to content

Commit

Permalink
handle PointerValue in TypeValue's GetLength
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Aug 19, 2024
1 parent 25f1c92 commit cf404c6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
55 changes: 53 additions & 2 deletions gnovm/pkg/gnolang/uverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"testing"
)

type printlnTestCases struct {
type uverseTestCases struct {
name string
code string
expected string
}

func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
test := []printlnTestCases{
test := []uverseTestCases{
{
name: "print empty slice",
code: `package test
Expand Down Expand Up @@ -158,3 +158,54 @@ func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
})
}
}

func TestIssue2707PointerSliceAsParamInLen(t *testing.T) {
tests := []uverseTestCases{
{
name: "pointer slice as param in len",
code: `
package test
func main() {
exp := [...]string{"HELLO"}
x := len(&exp)
println(x)
}
`,
expected: "1\n",
},
{
name: "len of array",
code: `
package test
func main() {
exp := [...]string{"HELLO", "WORLD"}
println(len(exp))
}
`,
expected: "2\n",
},
{
name: "len of pointer to array",
code: `
package test
func main() {
exp := [...]int{1, 2, 3, 4, 5}
ptr := &exp
println(len(ptr))
}
`,
expected: "5\n",
},
}

for _, tc := range tests {
m := NewMachine("test", nil)
n := MustParseFile("main.go", tc.code)
m.RunFiles(n)
m.RunMain()
assertOutput(t, tc.code, tc.expected)
}
}
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,11 @@ func (tv *TypedValue) GetLength() int {
return cv.GetLength()
case *NativeValue:
return cv.Value.Len()
case PointerValue:
if av, ok := cv.TV.V.(*ArrayValue); ok {
return av.GetLength()
}
panic(fmt.Sprintf("unexpected pointer value for len(): %s", tv.T.String()))

Check warning on line 2156 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2156

Added line #L2156 was not covered by tests
default:
panic(fmt.Sprintf("unexpected type for len(): %s",
tv.T.String()))
Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/files/len1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func main() {
exp := [...]string{"HELLO"}
x := len(&exp)
println(x)
}

// Output:
// 1

0 comments on commit cf404c6

Please sign in to comment.