Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk/vm): support return values from main in msgrun #1591

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gno.land/cmd/gnoland/testdata/run.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gnokey maketx addpkg -pkgdir $WORK/bar -pkgpath gno.land/r/foobar/bar -gas-fee 1
gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script/script.gno

## compare render
stdout 'main: --- hello from foo ---'
stdout '("main: --- hello from foo ---" string)'
stdout 'OK!'
stdout 'GAS WANTED: 200000'
stdout 'GAS USED: '
Expand All @@ -23,6 +23,6 @@ func Render(path string) string {
-- script/script.gno --
package main
import "gno.land/r/foobar/bar"
func main() {
println("main: ---", bar.Render(""), "---")
func main() string {
return "main: --- " + bar.Render("") + " ---"
}
11 changes: 7 additions & 4 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ func TestClient_Run(t *testing.T) {

import (
"std"
"strings"

"gno.land/p/demo/ufmt"
"gno.land/r/demo/tests"
)

func main() {
println(ufmt.Sprintf("- before: %d", tests.Counter()))
func main() string {
var buf strings.Builder
buf.WriteString(ufmt.Sprintf("- before: %d\n", tests.Counter()))
for i := 0; i < 10; i++ {
tests.IncCounter()
}
println(ufmt.Sprintf("- after: %d", tests.Counter()))
buf.WriteString(ufmt.Sprintf("- after: %d\n", tests.Counter()))
return buf.String()
}`
memPkg := &std.MemPackage{
Files: []*std.MemFile{
Expand All @@ -96,5 +99,5 @@ func main() {
require.NoError(t, err)
require.NotNil(t, res)
require.NotEmpty(t, res.DeliverTx.Data)
require.Equal(t, string(res.DeliverTx.Data), "- before: 0\n- after: 10\n")
require.Contains(t, string(res.DeliverTx.Data), `"- before: 0\n- after: 10\n"`)
}
4 changes: 2 additions & 2 deletions gno.land/pkg/integration/testdata/adduser.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func Render(path string) string {
-- script/script.gno --
package main
import "gno.land/r/foobar/bar"
func main() {
println("main: ---", bar.Render(""), "---")
func main() string {
return "main: --- " + bar.Render("") + " ---"
}
32 changes: 17 additions & 15 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// TODO: move most of the logic in ROOT/gno.land/...

import (
"bytes"
"fmt"
"os"
"io"
"regexp"
"strings"

Expand Down Expand Up @@ -85,7 +84,7 @@
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: io.Discard,

Check warning on line 87 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L87

Added line #L87 was not covered by tests
Store: vm.gnoStore,
})
defer m2.Release()
Expand Down Expand Up @@ -192,7 +191,7 @@
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: io.Discard,
Store: store,
Alloc: store.GetAllocator(),
Context: msgCtx,
Expand Down Expand Up @@ -267,7 +266,7 @@
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: io.Discard,
Store: store,
Context: msgCtx,
Alloc: store.GetAllocator(),
Expand Down Expand Up @@ -330,11 +329,10 @@
Banker: NewSDKBanker(vm, ctx),
}
// Parse and run the files, construct *PV.
buf := new(bytes.Buffer)
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Output: io.Discard,
Store: store,
Alloc: store.GetAllocator(),
Context: msgCtx,
Expand All @@ -347,7 +345,7 @@
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Output: io.Discard,
Store: store,
Alloc: store.GetAllocator(),
Context: msgCtx,
Expand All @@ -362,11 +360,15 @@
}
m2.Release()
}()
m2.RunMain()
ctx.Logger().Info("CPUCYCLES call",
"cycles", m2.Cycles,
)
res = buf.String()

rtvs := m2.Eval(gno.Call(gno.X("main")))
ctx.Logger().Info("CPUCYCLES call", "num-cycles", m2.Cycles)
for i, rtv := range rtvs {
res = res + rtv.String()
if i < len(rtvs)-1 {
res += "\n"
}

Check warning on line 370 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L369-L370

Added lines #L369 - L370 were not covered by tests
}
return res, nil
}

Expand Down Expand Up @@ -464,7 +466,7 @@
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Output: io.Discard,

Check warning on line 469 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L469

Added line #L469 was not covered by tests
Store: store,
Context: msgCtx,
Alloc: alloc,
Expand Down Expand Up @@ -524,7 +526,7 @@
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Output: io.Discard,

Check warning on line 529 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L529

Added line #L529 was not covered by tests
Store: store,
Context: msgCtx,
Alloc: alloc,
Expand Down
14 changes: 7 additions & 7 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ func TestVMKeeperRunSimple(t *testing.T) {
{"script.gno", `
package main

func main() {
println("hello world!")
func main() string {
return "hello world!"
}
`},
}
Expand All @@ -362,7 +362,7 @@ func main() {
msg2 := NewMsgRun(addr, coins, files)
res, err := env.vmk.Run(ctx, msg2)
assert.NoError(t, err)
assert.Equal(t, res, "hello world!\n")
assert.Contains(t, res, `"hello world!" string`)
}

// Call Run with stdlibs.
Expand All @@ -381,9 +381,9 @@ package main

import "std"

func main() {
func main() string {
addr := std.GetOrigCaller()
println("hello world!", addr)
return "hello world! " + addr.String()
}
`},
}
Expand All @@ -392,6 +392,6 @@ func main() {
msg2 := NewMsgRun(addr, coins, files)
res, err := env.vmk.Run(ctx, msg2)
assert.NoError(t, err)
expectedString := fmt.Sprintf("hello world! %s\n", addr.String())
assert.Equal(t, res, expectedString)
expectedString := fmt.Sprintf("hello world! %s", addr.String())
assert.Contains(t, res, expectedString)
}
Loading