diff --git a/examples/gno.land/r/demo/tests/tests.gno b/examples/gno.land/r/demo/tests/tests.gno index 773412c3db9..2489a2214ba 100644 --- a/examples/gno.land/r/demo/tests/tests.gno +++ b/examples/gno.land/r/demo/tests/tests.gno @@ -49,6 +49,8 @@ type TestRealmObject struct { Field string } +var TestRealmObjectValue TestRealmObject + func ModifyTestRealmObject(t *TestRealmObject) { t.Field += "_modified" } diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 850da3d3c0f..c8afd774ab9 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -36,8 +36,8 @@ func (e Exception) Sprint(m *Machine) string { type Machine struct { // State - Ops []Op // main operations - NumOps int + Ops []Op // operations stack + NumOps int // number of operations Values []TypedValue // buffer of values to be operated on NumValues int // number of values Exprs []Expr // pending expressions @@ -47,9 +47,9 @@ type Machine struct { Package *PackageValue // active package Realm *Realm // active realm Alloc *Allocator // memory allocations - Exceptions []Exception - NumResults int // number of results returned - Cycles int64 // number of "cpu" cycles + Exceptions []Exception // exceptions stack + NumResults int // number of results returned + Cycles int64 // number of "cpu" cycles performed Debugger Debugger @@ -199,6 +199,7 @@ func (m *Machine) Release() { machinePool.Put(m) } +// Convenience for initial setup of the machine. func (m *Machine) SetActivePackage(pv *PackageValue) { if err := m.CheckEmpty(); err != nil { panic(errors.Wrap(err, "set package when machine not empty")) @@ -210,6 +211,14 @@ func (m *Machine) SetActivePackage(pv *PackageValue) { } } +func (m *Machine) setCurrentPackage(pv *PackageValue) { + m.Package = pv + rlm := pv.GetRealm() + if rlm != nil { + m.Realm = rlm + } +} + //---------------------------------------- // top level Run* methods. @@ -1797,29 +1806,36 @@ func (m *Machine) PushFrameCall(cx *CallExpr, fv *FuncValue, recv TypedValue) { m.Printf("+F %#v\n", fr) } m.Frames = append(m.Frames, fr) - pv := fv.GetPackage(m.Store) - if pv == nil { - panic(fmt.Sprintf("package value missing in store: %s", fv.PkgPath)) - } - rlm := pv.GetRealm() - if rlm == nil && recv.IsDefined() { + if recv.IsDefined() { + // If the receiver is defined, we enter the receiver's realm. obj := recv.GetFirstObject(m.Store) if obj == nil { // could be a nil receiver. - // just ignore. + // set package and realm of function. + pv := fv.GetPackage(m.Store) + if pv == nil { + panic(fmt.Sprintf("package value missing in store: %s", fv.PkgPath)) + } + m.setCurrentPackage(pv) // maybe new realm } else { recvOID := obj.GetObjectInfo().ID - if !recvOID.IsZero() { + if recvOID.IsZero() { + // receiver isn't owned yet. + // just continue with current package and realm. + // XXX is this reasonable? + } else { // override the pv and rlm with receiver's. recvPkgOID := ObjectIDFromPkgID(recvOID.PkgID) - pv = m.Store.GetObject(recvPkgOID).(*PackageValue) - rlm = pv.GetRealm() // done + pv := m.Store.GetObject(recvPkgOID).(*PackageValue) + m.setCurrentPackage(pv) // maybe new realm } } - } - m.Package = pv - if rlm != nil && m.Realm != rlm { - m.Realm = rlm // enter new realm + } else { + pv := fv.GetPackage(m.Store) + if pv == nil { + panic(fmt.Sprintf("package value missing in store: %s", fv.PkgPath)) + } + m.setCurrentPackage(pv) // maybe new realm } } diff --git a/gnovm/tests/files/zrealm_crossrealm16.gno b/gnovm/tests/files/zrealm_crossrealm16.gno index db924432271..0fef0148b7d 100644 --- a/gnovm/tests/files/zrealm_crossrealm16.gno +++ b/gnovm/tests/files/zrealm_crossrealm16.gno @@ -443,6 +443,7 @@ func main() { // } // ] // } +// switchrealm["gno.land/r/demo/tests/crossrealm"] // switchrealm["gno.land/r/crossrealm_test"] // u[f5a516808f8976c33939133293d598ce3bca4e8d:2]={ // "Blank": {}, @@ -593,5 +594,3 @@ func main() { // } // ] // } -// switchrealm["gno.land/r/demo/tests/crossrealm"] -// switchrealm["gno.land/r/crossrealm_test"] diff --git a/gnovm/tests/files/zrealm_crossrealm17.gno b/gnovm/tests/files/zrealm_crossrealm17.gno index cb794c380d3..b3cdfc885ca 100644 --- a/gnovm/tests/files/zrealm_crossrealm17.gno +++ b/gnovm/tests/files/zrealm_crossrealm17.gno @@ -494,6 +494,7 @@ func main() { // } // ] // } +// switchrealm["gno.land/r/demo/tests/crossrealm"] // switchrealm["gno.land/r/crossrealm_test"] // u[f5a516808f8976c33939133293d598ce3bca4e8d:2]={ // "Blank": {}, @@ -676,5 +677,3 @@ func main() { // } // ] // } -// switchrealm["gno.land/r/demo/tests/crossrealm"] -// switchrealm["gno.land/r/crossrealm_test"] diff --git a/gnovm/tests/files/zrealm_crossrealm19_stdlibs.gno b/gnovm/tests/files/zrealm_crossrealm19_stdlibs.gno new file mode 100644 index 00000000000..6a51623f7bc --- /dev/null +++ b/gnovm/tests/files/zrealm_crossrealm19_stdlibs.gno @@ -0,0 +1,37 @@ +// PKGPATH: gno.land/r/crossrealm_test +package crossrealm_test + +import ( + "std" + + crossrealm "gno.land/r/demo/tests/crossrealm" +) + +type fooer struct { + s string +} + +func (f *fooer) Foo() { + f.s = "B" + println("hello " + f.s + " " + std.CurrentRealm().PkgPath()) +} + +var f *fooer + +func init() { + f = &fooer{s: "A"} + crossrealm.SetFooer(f) + crossrealm.CallFoo() +} + +func main() { + print(".") +} + +// Output: +// hello B gno.land/r/demo/tests/crossrealm +// . + +// Error: + +// Realm: diff --git a/gnovm/tests/files/zrealm_crossrealm3_stdlibs.gno b/gnovm/tests/files/zrealm_crossrealm3_stdlibs.gno index 6aa9c5247d8..105066c5ba7 100644 --- a/gnovm/tests/files/zrealm_crossrealm3_stdlibs.gno +++ b/gnovm/tests/files/zrealm_crossrealm3_stdlibs.gno @@ -13,10 +13,15 @@ func init() { } func main() { - // NOTE: but it is invalid to modify it using an external realm function. + // NOTE: it is valid to modify it using an external realm function, + // because the receiver makes the function run under this realm. somevalue.Modify() println(somevalue) + // we can even modify it directly. + somevalue.Field = "test_modified_directly" + println(somevalue) } -// Error: -// cannot modify external-realm or non-realm object +// Output: +// (struct{("test_modified" string)} gno.land/r/demo/tests.TestRealmObject) +// (struct{("test_modified_directly" string)} gno.land/r/demo/tests.TestRealmObject) diff --git a/gnovm/tests/files/zrealm_crossrealm4_stdlibs.gno b/gnovm/tests/files/zrealm_crossrealm4_stdlibs.gno index 6aa9c5247d8..ed73b7ad6bb 100644 --- a/gnovm/tests/files/zrealm_crossrealm4_stdlibs.gno +++ b/gnovm/tests/files/zrealm_crossrealm4_stdlibs.gno @@ -5,18 +5,18 @@ import ( "gno.land/r/demo/tests" ) -// NOTE: it is valid to persist external realm types. -var somevalue tests.TestRealmObject +// NOTE: it is valid to persist a pointer to an external object +var somevalue *tests.TestRealmObject func init() { - somevalue.Field = "test" + somevalue = &tests.TestRealmObjectValue } func main() { - // NOTE: but it is invalid to modify it using an external realm function. + // NOTE: it is valid to modify it using the external realm function. somevalue.Modify() println(somevalue) } -// Error: -// cannot modify external-realm or non-realm object +// Output: +// &(struct{("_modified" string)} gno.land/r/demo/tests.TestRealmObject) diff --git a/gnovm/tests/files/zrealm_crossrealm5_stdlibs.gno b/gnovm/tests/files/zrealm_crossrealm5_stdlibs.gno index 6aa9c5247d8..c7560b21463 100644 --- a/gnovm/tests/files/zrealm_crossrealm5_stdlibs.gno +++ b/gnovm/tests/files/zrealm_crossrealm5_stdlibs.gno @@ -6,15 +6,15 @@ import ( ) // NOTE: it is valid to persist external realm types. -var somevalue tests.TestRealmObject +var somevalue *tests.TestRealmObject func init() { - somevalue.Field = "test" + somevalue = &tests.TestRealmObjectValue } func main() { - // NOTE: but it is invalid to modify it using an external realm function. - somevalue.Modify() + // NOTE: but it is invalid to modify it directly. + somevalue.Field = "test" println(somevalue) } diff --git a/gnovm/tests/files/zrealm_tests0_stdlibs.gno b/gnovm/tests/files/zrealm_tests0_stdlibs.gno index 70ac3dd810d..5cd57c71a4a 100644 --- a/gnovm/tests/files/zrealm_tests0_stdlibs.gno +++ b/gnovm/tests/files/zrealm_tests0_stdlibs.gno @@ -23,7 +23,7 @@ func main() { // Realm: // switchrealm["gno.land/r/demo/tests"] -// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18]={ +// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:19]={ // "Fields": [ // { // "T": { @@ -37,17 +37,17 @@ func main() { // } // ], // "ObjectInfo": { -// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18", +// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:19", // "ModTime": "0", -// "OwnerID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17", +// "OwnerID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18", // "RefCount": "1" // } // } -// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17]={ +// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18]={ // "ObjectInfo": { -// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17", +// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18", // "ModTime": "0", -// "OwnerID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:16", +// "OwnerID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17", // "RefCount": "1" // }, // "Value": { @@ -57,12 +57,12 @@ func main() { // }, // "V": { // "@type": "/gno.RefValue", -// "Hash": "d3d6ffa52602f2bc976051d79294d219750aca64", -// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18" +// "Hash": "6b9b731f6118c2419f23ba57e1481679f17f4a8f", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:19" // } // } // } -// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:16]={ +// c[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17]={ // "Data": null, // "List": [ // { @@ -77,8 +77,8 @@ func main() { // "@type": "/gno.PointerValue", // "Base": { // "@type": "/gno.RefValue", -// "Hash": "4ea1e08156f3849b74a0f41f92cd4b48fb94926b", -// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:11" +// "Hash": "148d314678615253ee2032d7ecff6b144b474baf", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:12" // }, // "Index": "0", // "TV": null @@ -96,8 +96,8 @@ func main() { // "@type": "/gno.PointerValue", // "Base": { // "@type": "/gno.RefValue", -// "Hash": "ce86ea1156e75a44cd9d7ba2261819b100aa4ed1", -// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:14" +// "Hash": "fa414e1770821b8deb8e6d46d97828c47f7d5fa5", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:15" // }, // "Index": "0", // "TV": null @@ -115,8 +115,8 @@ func main() { // "@type": "/gno.PointerValue", // "Base": { // "@type": "/gno.RefValue", -// "Hash": "b66192fbd8a8dde79b6f854b5cc3c4cc965cfd92", -// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17" +// "Hash": "aaa64d049cf8660d689780acac9f546f270eaa4e", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:18" // }, // "Index": "0", // "TV": null @@ -124,7 +124,7 @@ func main() { // } // ], // "ObjectInfo": { -// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:16", +// "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17", // "ModTime": "0", // "OwnerID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:2", // "RefCount": "1" @@ -135,7 +135,7 @@ func main() { // "ObjectInfo": { // "ID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:2", // "IsEscaped": true, -// "ModTime": "15", +// "ModTime": "16", // "RefCount": "5" // }, // "Parent": null, @@ -400,7 +400,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "56", +// "Line": "58", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1197,7 +1197,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "52", +// "Line": "54", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1246,7 +1246,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "74", +// "Line": "76", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1282,7 +1282,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "79", +// "Line": "81", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1318,7 +1318,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "87", +// "Line": "89", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1364,7 +1364,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "91", +// "Line": "93", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1420,7 +1420,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "95", +// "Line": "97", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1477,7 +1477,7 @@ func main() { // "Location": { // "Column": "1", // "File": "tests.gno", -// "Line": "99", +// "Line": "101", // "PkgPath": "gno.land/r/demo/tests" // } // }, @@ -1512,8 +1512,8 @@ func main() { // "@type": "/gno.SliceValue", // "Base": { // "@type": "/gno.RefValue", -// "Hash": "ad25f70f66c8c53042afd1377e5ff5ab744bf1a5", -// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:16" +// "Hash": "3c58838c5667649add1ff8ee48a1cdc187fcd2ef", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:17" // }, // "Length": "3", // "Maxcap": "3", @@ -1563,6 +1563,17 @@ func main() { // }, // { // "T": { +// "@type": "/gno.RefType", +// "ID": "gno.land/r/demo/tests.TestRealmObject" +// }, +// "V": { +// "@type": "/gno.RefValue", +// "Hash": "5e56ba76fc0add1a3a67f7a8b6709f4f27215f93", +// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:10" +// } +// }, +// { +// "T": { // "@type": "/gno.PointerType", // "Elt": { // "@type": "/gno.RefType", @@ -1590,10 +1601,7 @@ func main() { // } // ] // } -// d[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:13] -// switchrealm["gno.land/r/demo/tests_foo"] -// switchrealm["gno.land/r/demo/tests_foo"] -// switchrealm["gno.land/r/demo/tests_foo"] +// d[0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:14] // switchrealm["gno.land/r/demo/tests_foo"] // switchrealm["gno.land/r/demo/tests"] // switchrealm["gno.land/r/demo/tests_test"]