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

fix(gnolang): ensure complete Uverse initialization #2997

Merged
merged 7 commits into from
Oct 25, 2024
Merged
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
12 changes: 0 additions & 12 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,6 @@ func BenchmarkBenchdata(b *testing.B) {
name += "_param:" + param
}
b.Run(name, func(b *testing.B) {
if strings.HasPrefix(name, "matrix.gno_param") {
// CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./...
// That is not just exposing test and benchmark traces as output, but these benchmarks are failing
// making the output unparseable:
/*
BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered]
panic: runtime error: index out of range [31] with length 25:
...
*/
b.Skip("it panics causing an error when parsing benchmark results")
}

// Gen template with N and param.
var buf bytes.Buffer
require.NoError(b, tpl.Execute(&buf, bdataParams{
Expand Down
6 changes: 1 addition & 5 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,12 +739,8 @@
// TRANS_BLOCK -----------------------
case *FuncDecl:
// retrieve cached function type.
// the type and receiver are already set in predefineNow.
ft := getType(&n.Type).(*FuncType)
if n.IsMethod {
// recv/type set @ predefineNow().
} else {
// type set @ predefineNow().
}

// push func body block.
pushInitBlock(n, &last, &stack)
Expand Down Expand Up @@ -2531,8 +2527,8 @@
defer doRecover(stack, n)

if debug {
debug.Printf("findGotoLoopDefines %s (%v) stage:%v\n", n.String(), reflect.TypeOf(n), stage)
}

Check warning on line 2531 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2530-L2531

Added lines #L2530 - L2531 were not covered by tests

switch stage {
// ----------------------------------------
Expand Down Expand Up @@ -2569,8 +2565,8 @@
case *FuncLitExpr:
// inner funcs.
return n, TRANS_SKIP
case *FuncDecl:
panic("unexpected inner func decl")

Check warning on line 2569 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2568-L2569

Added lines #L2568 - L2569 were not covered by tests
case *NameExpr:
if n.Type == NameExprTypeDefine {
n.Type = NameExprTypeHeapDefine
Expand Down Expand Up @@ -2639,12 +2635,12 @@
// inner funcs.
return n, TRANS_SKIP
}
return n, TRANS_CONTINUE
case *FuncDecl:
if len(ns) > 0 {
panic("unexpected inner func decl")

Check warning on line 2641 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2638-L2641

Added lines #L2638 - L2641 were not covered by tests
}
return n, TRANS_CONTINUE

Check warning on line 2643 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2643

Added line #L2643 was not covered by tests
// Otherwise mark stmt as gotoloop.
case Stmt:
// we're done if we
Expand Down Expand Up @@ -2696,8 +2692,8 @@
defer doRecover(stack, n)

if debug {
debug.Printf("findLoopUses1 %s (%v) stage:%v\n", n.String(), reflect.TypeOf(n), stage)
}

Check warning on line 2696 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2695-L2696

Added lines #L2695 - L2696 were not covered by tests

switch stage {
// ----------------------------------------
Expand Down Expand Up @@ -2752,8 +2748,8 @@
// on NameExpr can know that this was loop defined
// on this block.
setAttrHeapDefine(last, n.Name)
case NameExprTypeHeapUse, NameExprTypeHeapClosure:
panic("unexpected node type")

Check warning on line 2752 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2751-L2752

Added lines #L2751 - L2752 were not covered by tests
}
}
return n, TRANS_CONTINUE
Expand All @@ -2778,7 +2774,7 @@

func assertNotHasName(names []Name, name Name) {
if slices.Contains(names, name) {
panic(fmt.Sprintf("name: %s already contained in names: %v", name, names))

Check warning on line 2777 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2777

Added line #L2777 was not covered by tests
}
}

Expand Down Expand Up @@ -2808,7 +2804,7 @@
var ok bool
idx, ok = fle.GetLocalIndex("~" + name)
if !ok {
panic("~name not added to fle atomically")

Check warning on line 2807 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2807

Added line #L2807 was not covered by tests
}
return // already exists
}
Expand All @@ -2817,7 +2813,7 @@
// define ~name to fle.
_, ok := fle.GetLocalIndex("~" + name)
if ok {
panic("~name already defined in fle")

Check warning on line 2816 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2816

Added line #L2816 was not covered by tests
}

tv := dbn.GetValueRef(nil, name, true)
Expand All @@ -2841,7 +2837,7 @@
}
}

panic("should not happen, idx not found")

Check warning on line 2840 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2840

Added line #L2840 was not covered by tests
}

// finds the first FuncLitExpr in the stack at or after stop.
Expand Down Expand Up @@ -2872,7 +2868,7 @@
}
}
}
panic("stop not found in stack")

Check warning on line 2871 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2871

Added line #L2871 was not covered by tests
}

// Convert non-loop uses of loop names to NameExprTypeHeapUse.
Expand All @@ -2889,8 +2885,8 @@
defer doRecover(stack, n)

if debug {
debug.Printf("findLoopUses2 %s (%v) stage:%v\n", n.String(), reflect.TypeOf(n), stage)
}

Check warning on line 2889 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2888-L2889

Added lines #L2888 - L2889 were not covered by tests

switch stage {
// ----------------------------------------
Expand Down Expand Up @@ -2958,7 +2954,7 @@
lds, _ := n.GetAttribute(ATTR_LOOP_DEFINES).([]Name)
lus, _ := n.GetAttribute(ATTR_LOOP_USES).([]Name)
if len(lds) < len(lus) {
panic("defines should be a superset of used-defines")

Check warning on line 2957 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2957

Added line #L2957 was not covered by tests
}
// no need anymore
n.DelAttribute(ATTR_LOOP_USES)
Expand Down Expand Up @@ -3005,7 +3001,7 @@
*last = bn
*stack = append(*stack, bn)
if len(*stack) >= math.MaxUint8 {
panic("block node depth reached maximum MaxUint8")

Check warning on line 3004 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3004

Added line #L3004 was not covered by tests
}
}

Expand All @@ -3015,7 +3011,7 @@
func pushInitBlockAndCopy(bn BlockNode, last *BlockNode, stack *[]BlockNode) {
if _, ok := bn.(*IfCaseStmt); !ok {
if _, ok := bn.(*SwitchClauseStmt); !ok {
panic("should not happen")

Check warning on line 3014 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3014

Added line #L3014 was not covered by tests
}
}
orig := *last
Expand Down Expand Up @@ -3773,7 +3769,7 @@
panic("cannot elide unknown composite type")
}
ct = t
cx.Type = constType(cx, t)

Check warning on line 3772 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L3772

Added line #L3772 was not covered by tests
} else {
un = findUndefined(store, last, cx.Type)
if un != "" {
Expand Down
47 changes: 38 additions & 9 deletions gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,55 @@
var (
uverseNode *PackageNode
uverseValue *PackageValue
uverseInit = uverseUninitialized
)

const (
uverseUninitialized = iota
uverseInitializing
uverseInitialized
)

func init() {
// Call Uverse() so we initialize the Uverse node ahead of any calls to the package.
Uverse()
}

const uversePkgPath = ".uverse"

// Always returns a new copy from the latest state of source.
// UverseNode returns the uverse PackageValue.
// If called while initializing the UverseNode itself, it will return an empty
// PackageValue.
func Uverse() *PackageValue {
if uverseValue == nil {
pn := UverseNode()
uverseValue = pn.NewPackage()
switch uverseInit {
case uverseUninitialized:
uverseInit = uverseInitializing
makeUverseNode()
uverseInit = uverseInitialized
case uverseInitializing:
return &PackageValue{}
}

return uverseValue
}

// Always returns the same instance with possibly differing completeness.
// UverseNode returns the uverse PackageNode.
// If called while initializing the UverseNode itself, it will return an empty
// PackageNode.
func UverseNode() *PackageNode {
// Global is singleton.
if uverseNode != nil {
return uverseNode
switch uverseInit {
case uverseUninitialized:
uverseInit = uverseInitializing
makeUverseNode()
uverseInit = uverseInitialized

Check warning on line 104 in gnovm/pkg/gnolang/uverse.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/uverse.go#L101-L104

Added lines #L101 - L104 were not covered by tests
case uverseInitializing:
return &PackageNode{}
}

return uverseNode
}

func makeUverseNode() {
// NOTE: uverse node is hidden, thus the leading dot in pkgPath=".uverse".
uverseNode = NewPackageNode("uverse", uversePkgPath, nil)

Expand Down Expand Up @@ -1017,7 +1046,7 @@
m.Exceptions = nil
},
)
return uverseNode
uverseValue = uverseNode.NewPackage()
}

func copyDataToList(dst []TypedValue, data []byte, et Type) {
Expand Down
Loading