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: Initialize static blocks before preprocess #2418

Merged
merged 21 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
14 changes: 7 additions & 7 deletions gnovm/cmd/gno/testdata/gno_test/realm_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func main() {
// },
// "Values": [
// {
// "N": "AQAAAAAAAAA=",
// "T": {
// "@type": "/gno.PrimitiveType",
// "value": "32"
// }
// },
// {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [],
Expand Down Expand Up @@ -74,13 +81,6 @@ func main() {
// "Results": []
// }
// }
// },
// {
// "N": "AQAAAAAAAAA=",
// "T": {
// "@type": "/gno.PrimitiveType",
// "value": "32"
// }
// }
// ]
// }
Expand Down
14 changes: 7 additions & 7 deletions gnovm/cmd/gno/testdata/gno_test/realm_sync.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func main() {
// },
// "Values": [
// {
// "N": "AQAAAAAAAAA=",
// "T": {
// "@type": "/gno.PrimitiveType",
// "value": "32"
// }
// },
// {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [],
Expand Down Expand Up @@ -89,13 +96,6 @@ func main() {
// "Results": []
// }
// }
// },
// {
// "N": "AQAAAAAAAAA=",
// "T": {
// "@type": "/gno.PrimitiveType",
// "value": "32"
// }
// }
// ]
// }
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func debugLookup(m *Machine, name string) (tv TypedValue, ok bool) {
}
}
// Fallback: search a global value.
if v := sblocks[0].Source.GetValueRef(m.Store, Name(name)); v != nil {
if v := sblocks[0].Source.GetValueRef(m.Store, Name(name), true); v != nil {
return *v, true
}
return tv, false
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/gonative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGoNativeDefine(t *testing.T) {
pkg := NewPackageNode("foo", "test.foo", nil)
rt := reflect.TypeOf(Foo{})
pkg.DefineGoNativeType(rt)
nt := pkg.GetValueRef(nil, Name("Foo")).GetType().(*NativeType)
nt := pkg.GetValueRef(nil, Name("Foo"), true).GetType().(*NativeType)
assert.Equal(t, rt, nt.Type)
path := pkg.GetPathForName(nil, Name("Foo"))
assert.Equal(t, uint8(1), path.Depth)
Expand Down
20 changes: 12 additions & 8 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ func (x *PackageNode) DefineNative(n Name, ps, rs FieldTypeExprs, native func(*M
panic("should not happen")
}
}
fv := x.GetValueRef(nil, n).V.(*FuncValue)
fv := x.GetValueRef(nil, n, true).V.(*FuncValue)
fv.nativeBody = native
}

Expand All @@ -1434,7 +1434,7 @@ func (x *PackageNode) DefineNativeOverride(n Name, native func(*Machine)) {
if native == nil {
panic("DefineNative expects a function, but got nil")
}
fv := x.GetValueRef(nil, n).V.(*FuncValue)
fv := x.GetValueRef(nil, n, true).V.(*FuncValue)
fv.nativeBody = native
}

Expand Down Expand Up @@ -1471,7 +1471,7 @@ type BlockNode interface {
GetPathForName(Store, Name) ValuePath
GetIsConst(Store, Name) bool
GetLocalIndex(Name) (uint16, bool)
GetValueRef(Store, Name) *TypedValue
GetValueRef(Store, Name, bool) *TypedValue
GetStaticTypeOf(Store, Name) Type
GetStaticTypeOfAt(Store, ValuePath) Type
Predefine(bool, Name)
Expand Down Expand Up @@ -1732,17 +1732,19 @@ func (sb *StaticBlock) GetLocalIndex(n Name) (uint16, bool) {
// Implemented BlockNode.
// This method is too slow for runtime, but it is used
// during preprocessing to compute types.
// If skipPredefined, skips over names that are only predefined.
// Returns nil if not defined.
func (sb *StaticBlock) GetValueRef(store Store, n Name) *TypedValue {
func (sb *StaticBlock) GetValueRef(store Store, n Name, skipPredefined bool) *TypedValue {
idx, ok := sb.GetLocalIndex(n)
bb := &sb.Block
bp := sb.GetParentNode(store)
for {
if ok {
if ok && (!skipPredefined || sb.Types[idx] != nil) {
return bb.GetPointerToInt(store, int(idx)).TV
} else if bp != nil {
idx, ok = bp.GetLocalIndex(n)
bb = bp.GetStaticBlock().GetBlock()
sb = bp.GetStaticBlock()
bb = sb.GetBlock()
bp = bp.GetParentNode(store)
} else {
return nil
Expand All @@ -1752,8 +1754,8 @@ func (sb *StaticBlock) GetValueRef(store Store, n Name) *TypedValue {

// Implements BlockNode
// Statically declares a name definition.
// At runtime, use *Block.GetValueRef() etc which take path
// values, which are pre-computeed in the preprocessor.
// At runtime, use *Block.GetPointerTo() which takes a path
// value, which is pre-computeed in the preprocessor.
// Once a typed value is defined, it cannot be changed.
//
// NOTE: Currently tv.V is only set when the value represents a Type(Value) or
Expand All @@ -1765,12 +1767,14 @@ func (sb *StaticBlock) Define(n Name, tv TypedValue) {
sb.Define2(false, n, tv.T, tv)
}

// Set type to nil, only reserving the name.
func (sb *StaticBlock) Predefine(isConst bool, n Name) {
sb.Define2(isConst, n, nil, anyValue(nil))
}

// The declared type st may not be the same as the static tv;
// e.g. var x MyInterface = MyStruct{}.
// Setting st and tv to nil/zero reserves (predefines) name for definition later.
func (sb *StaticBlock) Define2(isConst bool, n Name, st Type, tv TypedValue) {
if debug {
debug.Printf(
Expand Down
Loading