From 1ca7883d1d10785c5ad1d886a7371eb0a9be23a4 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Tue, 11 Jul 2023 15:35:54 +0200 Subject: [PATCH] WIP Signed-off-by: Antonio Navarro Perez --- gnovm/pkg/gnolang/machine.go | 2 +- gnovm/stdlibs/os/proc.go | 10 ++++++++++ tm2/pkg/sdk/vm/keeper_test.go | 26 ++++++++++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 gnovm/stdlibs/os/proc.go diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 0ad52550684..1d56945eb2e 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -565,7 +565,7 @@ func (m *Machine) RunFunc(fn Name) { m.RunStatement(S(Call(Nx(fn)))) } -func (m *Machine) RunMain() { +func (m *Machine) RunMain(args ...string) { defer func() { if r := recover(); r != nil { fmt.Printf("Machine.RunMain() panic: %v\n%s\n", diff --git a/gnovm/stdlibs/os/proc.go b/gnovm/stdlibs/os/proc.go new file mode 100644 index 00000000000..2179b8a44e3 --- /dev/null +++ b/gnovm/stdlibs/os/proc.go @@ -0,0 +1,10 @@ +package os + +// Args hold the command-line arguments, starting with the program name. +var Args []string + +func init() { + Args = runtime_args() +} + +func runtime_args() []string // in package runtime diff --git a/tm2/pkg/sdk/vm/keeper_test.go b/tm2/pkg/sdk/vm/keeper_test.go index 27a1054e914..8008054a010 100644 --- a/tm2/pkg/sdk/vm/keeper_test.go +++ b/tm2/pkg/sdk/vm/keeper_test.go @@ -67,7 +67,7 @@ func TestVMKeeperOrigSend1(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -112,7 +112,7 @@ func TestVMKeeperOrigSend2(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -166,7 +166,7 @@ func TestVMKeeperOrigSend3(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -210,7 +210,7 @@ func TestVMKeeperRealmSend1(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -254,7 +254,7 @@ func TestVMKeeperRealmSend2(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -298,7 +298,7 @@ func TestVMKeeperOrigCallerInit(t *testing.T) { // Create test package. files := []*std.MemFile{ - {"init.gno", ` + {Name: "init.gno", Body: ` package test import "std" @@ -337,3 +337,17 @@ func GetAdmin() string { assert.NoError(t, err) assert.Equal(t, res, addrString) } + +func TestVMKeeperMainWithArgs(t *testing.T) { + m := NewMachine("test", nil) + c := `package test + import "fmt" + import "os" +func main() { + args := os.Args + fmt.Println(args) +}` + n := MustParseFile("main.go", c) + m.RunFiles(n) + m.RunMain() +}