Skip to content

Commit

Permalink
merge formatTV to Sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Nov 30, 2023
1 parent 8b998cb commit 903ae18
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
1 change: 0 additions & 1 deletion examples/gno.land/r/demo/boards/z_4_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ func main() {
// }
// }
// switchrealm["gno.land/r/demo/boards"]
// switchrealm["gno.land/r/demo/boards"]
// switchrealm["gno.land/r/demo/users"]
// switchrealm["gno.land/r/demo/users"]
// switchrealm["gno.land/r/demo/users"]
Expand Down
41 changes: 21 additions & 20 deletions gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,8 @@ func UverseNode() *PackageNode {
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = formatTypeValue(&ev, m)
// ss[i] = formatTypeValue(&ev, m)
ss[i] = ev.Sprint(m)
}
rs := strings.Join(ss, " ") + "\n"
if debug {
Expand Down Expand Up @@ -1004,26 +1005,26 @@ func copyNativeToData(dst []byte, rv reflect.Value, rvl int) {
}
}

const FormatUndefined = "undefined"
// const FormatUndefined = "undefined"

// formatTypeValue formats the given TypedValue based on its type and returns the formatted string representation.
func formatTypeValue(ev *TypedValue, m *Machine) string {
if ev.T == nil {
return FormatUndefined
}
// // formatTypeValue formats the given TypedValue based on its type and returns the formatted string representation.
// func formatTypeValue(ev *TypedValue, m *Machine) string {
// if ev.T == nil {
// return FormatUndefined
// }

if ev.V == nil {
switch ev.T.Kind() {
case SliceKind, StringKind:
return FormatUndefined
default:
ev.Sprint(m)
}
}
// if ev.V == nil {
// switch ev.T.Kind() {
// case SliceKind, StringKind:
// return FormatUndefined
// default:
// ev.Sprint(m)
// }
// }

if ev.T.Kind() == FuncKind {
return ev.T.String()
}
// if ev.T.Kind() == FuncKind {
// return ev.T.String()
// }

return ev.Sprint(m)
}
// return ev.Sprint(m)
// }
8 changes: 4 additions & 4 deletions gnovm/pkg/gnolang/uverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

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

Expand Down Expand Up @@ -116,7 +116,7 @@ func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
println("nil func")
}
}`,
expected: "func()()\nnil func\n",
expected: "nil func()\nnil func\n",
},
}

Expand All @@ -129,4 +129,4 @@ func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
assertOutput(t, tc.code, tc.expected)
})
}
}
}
3 changes: 2 additions & 1 deletion gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (*Block) assertValue() {}
func (RefValue) assertValue() {}

const (
nilStr = "nil"
nilStr = "nil"
undefinedStr = "undefined"
)

var (
Expand Down
21 changes: 20 additions & 1 deletion gnovm/pkg/gnolang/values_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,27 @@ func (v RefValue) String() string {
func (tv *TypedValue) Sprint(m *Machine) string {
// if undefined, just "undefined".
if tv == nil || tv.T == nil {
return "undefined"
return undefinedStr
}

if tv.V == nil && (tv.T.Kind() == SliceKind || tv.T.Kind() == StringKind) {
return undefinedStr
}

if tv.T.Kind() == FuncKind {
if tv.V == nil {
return "nil func()"
}
switch fv := tv.V.(type) {
case *FuncValue, *BoundMethodValue:
return fv.String()
default:
panic(fmt.Sprintf(
"unexpected func type %v",
reflect.TypeOf(tv.V)))

Check warning on line 184 in gnovm/pkg/gnolang/values_string.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_string.go#L178-L184

Added lines #L178 - L184 were not covered by tests
}
}

// if implements .String(), return it.
if IsImplementedBy(gStringerType, tv.T) {
res := m.Eval(Call(Sel(&ConstExpr{TypedValue: *tv}, "String")))
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/fun10.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func main() {
}

// Output:
// func()()
// nil func()
// nil func

0 comments on commit 903ae18

Please sign in to comment.