Skip to content

Commit

Permalink
feat(gnolang): prints nil slices as undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Nov 17, 2023
1 parent b1a53c0 commit 7dddd7a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,20 @@ func UverseNode() *PackageNode {
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = ev.Sprint(m)
// TODO: Generalize this to all types.
if ev.T.Kind() == SliceKind || ev.T.Kind() == StringKind {
if ev.V == nil {
ss[i] = "undefined"
} else {
ss[i] = ev.Sprint(m)
}
} else {
if ev.T == nil {
ss[i] = "undefined"
} else {
ss[i] = ev.Sprint(m)
}
}
}
rs := strings.Join(ss, " ") + "\n"
if debug {
Expand Down
83 changes: 83 additions & 0 deletions gnovm/pkg/gnolang/uverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gnolang

import (
"testing"
)

func TestPrintlnPrintNil(t *testing.T){
m := NewMachine("test", nil)

c := `package test
func main() {
var a []string
println(a)
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
assertOutput(t, c, "undefined\n")
}

func TestPrintlnFunction(t *testing.T) {
m := NewMachine("test", nil)

c := `package test
func foo(a, b int) int {
return a + b
}
func main() {
println(foo(1, 3))
}`

n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()

assertOutput(t, c, "4\n")
}

func TestComposite(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func main() {
a, b, c, d := 1, 2, 3, 4
x := []int{
a: b,
c: d,
}
println(x)
}`

n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()

assertOutput(t, c, "slice[(0 int),(2 int),(0 int),(4 int)]\n")
}

// TODO: Resolve runtime error
// current output: g recover <nil> wtf
func TestRecover5(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func main() {
f()
}
func f() {
defer func() { println("f recover", recover()) }()
defer g()
panic("wtf")
}
func g() {
println("g recover", recover())
}`

n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()

assertOutput(t, c, "g recover wtf\nf recover wtf\n")
}
6 changes: 6 additions & 0 deletions gnovm/tests/files/print1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
var a []string
println(a)
}

0 comments on commit 7dddd7a

Please sign in to comment.