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

gnolang/gno: prototype Machine.RunContext #3552

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"bytes"
"context"
"fmt"
"io"
"os"
Expand All @@ -10,6 +11,7 @@
"strings"
"testing"
"text/template"
"time"
"unsafe"

// "github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -291,6 +293,31 @@
m.RunMain()
}

func TestRunMemoryAllocationBuster(t *testing.T) {
t.Parallel()

m := NewMachine("test", nil)
c := `package test
func main() {
var x interface{}
for i := 0; i < 10000; i++ {
x = [1]interface{}{x}
}
for i := 0; i < 10000; i++ {
println(x)
}
}
`
n := MustParseFile("main.go", c)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
m.SetRunContext(ctx)

m.RunFiles(n)
m.RunMain()

}

Check failure on line 319 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / Run GnoVM suite / Go Lint / lint

unnecessary trailing newline (whitespace)

func BenchmarkPreprocessForLoop(b *testing.B) {
m := NewMachine("test", nil)
c := `package test
Expand Down
26 changes: 25 additions & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gnolang
// XXX rename file to machine.go.

import (
"context"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -79,6 +80,13 @@ type Machine struct {
// it is executed. It is reset to zero after the defer functions in the current
// scope have finished executing.
DeferPanicScope uint

runCtx context.Context
}

func (m *Machine) RunContext(ctx context.Context) {
m.runCtx = ctx
m.Run()
}

// NewMachine initializes a new gno virtual machine, acting as a shorthand
Expand All @@ -98,6 +106,12 @@ func NewMachine(pkgPath string, store Store) *Machine {
})
}

func (m *Machine) SetRunContext(ctx context.Context) {
if m != nil {
m.runCtx = ctx
}
}

// MachineOptions is used to pass options to [NewMachineWithOptions].
type MachineOptions struct {
// Active package of the given machine; must be set before execution.
Expand Down Expand Up @@ -810,7 +824,11 @@ func (m *Machine) RunStatement(s Stmt) {
m.PushOp(OpHalt)
m.PushStmt(s)
m.PushOp(OpExec)
m.Run()
ctx := m.runCtx
if ctx == nil {
ctx = context.Background()
}
m.RunContext(ctx)
}

// Runs a declaration after preprocessing d. If d was already
Expand Down Expand Up @@ -1168,6 +1186,12 @@ func (m *Machine) Run() {
if m.Debugger.enabled {
m.Debug()
}
if m.runCtx != nil {
if err := m.runCtx.Err(); err != nil {
panic(err)
}
}

op := m.PopOp()
if bm.OpsEnabled {
// benchmark the operation.
Expand Down
9 changes: 0 additions & 9 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/a.go

This file was deleted.

16 changes: 0 additions & 16 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/b.go

This file was deleted.

22 changes: 0 additions & 22 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3013.go

This file was deleted.

10 changes: 0 additions & 10 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine.go

This file was deleted.

21 changes: 0 additions & 21 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine2.go

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine4.go

This file was deleted.

13 changes: 0 additions & 13 deletions gnovm/pkg/gnolang/testdata/corpra/parsefile/bug_3014_redefine5.go

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading