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: allow Args on main run #982

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions gnovm/stdlibs/os/proc.go
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried two possible solutions:

  • Directly inject os.Args values when calling RunMain function on the VM. Problem: os package is not available by default on the VM, you need VMKeeper for it.
  • Implementing runtime_args function somehow, returning the provided arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second option.

I suggest: VMKeeper to be the preferred "frontend" for configuring the VM for advanced use cases like full context, multi-transaction features etc, while the raw gno.Machine remains suitable for single-test focused tasks.

Related with #1016.
After #1016 will be finished, I plan to migrate most of the gno CLI to it instead of gno.Machine, especially the REPL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

26 changes: 20 additions & 6 deletions tm2/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}