Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Dec 30, 2024
1 parent e0a040e commit 3ec7a2e
Show file tree
Hide file tree
Showing 54 changed files with 6,674 additions and 169 deletions.
18 changes: 0 additions & 18 deletions examples/gno.land/r/demo/tests/crossrealm/crossrealm3.gno

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func GetSlice5() []*XYZ { // TODO, unwrap
}

func GetSlice6(f func(s []*XYZ)) {
s6[1] = &XYZ{"6.1"} // the initial value is deleted, so this is not real after append
f(s6)
s6[1] = &XYZ{"6.1"}
f(s6) // not real now
}

func GetSlice7(f func(s [2]XYZ)) {
Expand Down
28 changes: 28 additions & 0 deletions examples/gno.land/r/demo/tests/crossrealm/crossrealm_struct.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package crossrealm

type Bar struct {
A int
}

var bar *Bar

// TODO: deprovision this
var Bar2 *Bar = &Bar{A: 22} // exported

func (b *Bar) String() {
println("b.A: ", b.A)
}

func SetBar(b *Bar) *Bar {
bar = b
return bar
}

func ChangeBar() {
Bar2.A += 1
//println(Bar2.A)
}

func (b *Bar) ModifyBar() {
b.A += 1
}
35 changes: 28 additions & 7 deletions gnovm/pkg/gnolang/ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ type Object interface {
SetIsDeleted(bool, uint64)
GetIsNewReal() bool
SetIsNewReal(bool)
GetLastNewEscapedRealm() PkgID
SetLastNewEscapedRealm(pkgID PkgID)
GetOriginRealm() PkgID
SetOriginRealm(pkgID PkgID)
GetIsRef() bool
SetIsRef(bool)
GetIsNewEscaped() bool
Expand Down Expand Up @@ -256,6 +256,7 @@ func (oi *ObjectInfo) GetModTime() uint64 {
}

func (oi *ObjectInfo) IncRefCount() int {
fmt.Println("---IncRefCount")
oi.RefCount++
return oi.RefCount
}
Expand Down Expand Up @@ -316,11 +317,11 @@ func (oi *ObjectInfo) SetIsNewReal(x bool) {
oi.isNewReal = x
}

func (oi *ObjectInfo) GetLastNewEscapedRealm() PkgID {
func (oi *ObjectInfo) GetOriginRealm() PkgID {
return oi.lastNewRealEscapedRealm
}

func (oi *ObjectInfo) SetLastNewEscapedRealm(pkgId PkgID) {
func (oi *ObjectInfo) SetOriginRealm(pkgId PkgID) {
fmt.Println("---SetLastNewEscapedRealm")
oi.lastNewRealEscapedRealm = pkgId
}
Expand Down Expand Up @@ -394,8 +395,12 @@ func (tv *TypedValue) GetFirstObject2(store Store) (obj Object, pkgId PkgID) {
// general case
if dt, ok := tv.T.(*DeclaredType); ok {
fmt.Printf("---dt: %v\n", dt)
if IsRealmPath(dt.PkgPath) {
pkgId = PkgIDFromPkgPath(dt.PkgPath)
fmt.Println("---dt.base: ", dt.Base)
if _, ok := dt.Base.(*FuncType); !ok {
fmt.Println("---dt.base.PkgPath: ", dt.Base.GetPkgPath())
if IsRealmPath(dt.Base.GetPkgPath()) {
pkgId = PkgIDFromPkgPath(dt.Base.GetPkgPath())
}
}
}

Expand All @@ -411,7 +416,7 @@ func (tv *TypedValue) GetFirstObject2(store Store) (obj Object, pkgId PkgID) {
fmt.Println("---v: ", v)
// TODO: check this
if dt, ok := cv.TV.T.(*DeclaredType); ok {
//fmt.Println("---dt: ", dt)
fmt.Println("---dt: ", dt)
//fmt.Println("---cv.Base: ", cv.GetBase(store), reflect.TypeOf(cv.GetBase(store)))
//if _, ok := cv.GetBase(store).(*HeapItemValue); !ok {
//println("---base is heap item")
Expand All @@ -421,6 +426,7 @@ func (tv *TypedValue) GetFirstObject2(store Store) (obj Object, pkgId PkgID) {
}
//}
}
fmt.Println("---pkgId; ", pkgId)
}
return
case *ArrayValue:
Expand All @@ -440,6 +446,21 @@ func (tv *TypedValue) GetFirstObject2(store Store) (obj Object, pkgId PkgID) {
fmt.Println("clo...PkgPath", clo.Source.GetLocation().PkgPath)
pkgId = PkgIDFromPkgPath(clo.Source.GetLocation().PkgPath)
return
case *BoundMethodValue:
fmt.Println("---BoundMethodValue, recv: ", cv.Receiver)
fmt.Println("---type of T: ", reflect.TypeOf(cv.Receiver.T))
if pv, ok := cv.Receiver.V.(PointerValue); ok {
println("---pointer value")
// TODO: check this
if dt, ok := pv.TV.T.(*DeclaredType); ok {
fmt.Println("---2, dt: ", dt)
if IsRealmPath(dt.PkgPath) {
pkgId = PkgIDFromPkgPath(dt.PkgPath)
}
}
fmt.Println("---pkgId; ", pkgId)
}
return
default:
return
}
Expand Down
57 changes: 40 additions & 17 deletions gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,13 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {

// ref value is the derived value from co, like a slice.
func (rlm *Realm) DidUpdate2(store Store, po, xo, co Object, refValue Value) {
fmt.Println("---DidUpdate2, rlm.ID: ", rlm.ID)
//fmt.Printf("---xo: %v, type of xo: %v\n", xo, reflect.TypeOf(xo))
fmt.Printf("---DidUpdate2, po: %v, type of po: %v\n", po, reflect.TypeOf(po))
fmt.Printf("---xo: %v, type of xo: %v\n", xo, reflect.TypeOf(xo))
fmt.Printf("---co: %v, type of co: %v\n", co, reflect.TypeOf(co))
fmt.Printf("---po: %v, type of po: %v\n", po, reflect.TypeOf(po))
if co != nil {
fmt.Println("co.GetOriginRealm: ", co.GetOriginRealm())
}
fmt.Println("---rlm.ID: ", rlm.ID)

if rlm == nil {
return
Expand All @@ -227,6 +230,7 @@ func (rlm *Realm) DidUpdate2(store Store, po, xo, co Object, refValue Value) {
return // do nothing.
}

// TODO: check not real external here, if po is real, association is invalid
fmt.Println("---po.GetObjectID().PkgID: ", po.GetObjectID().PkgID)
if po.GetObjectID().PkgID != rlm.ID {
panic("cannot modify external-realm or non-realm object")
Expand All @@ -252,6 +256,7 @@ func (rlm *Realm) DidUpdate2(store Store, po, xo, co Object, refValue Value) {
// XXX, why packageBlock is automatically escaped?
println("---already escaped, should check cross realm?")
// already escaped
rlm.MarkNewEscapedCheckCrossRealm(store, co, refValue)
} else {
rlm.MarkNewEscapedCheckCrossRealm(store, co, refValue)
}
Expand Down Expand Up @@ -282,7 +287,7 @@ func (rlm *Realm) DidUpdate2(store Store, po, xo, co Object, refValue Value) {
// XXX, oo coming here must be referenced type, since they already escaped.
// XXX, so oo has been persisted, thus fillValueTV
func checkCrossRealm(store Store, oo Object, refValue Value) {
fmt.Println("---checkCrossReal, oo: ", oo)
fmt.Println("---checkCrossRealm, oo: ", oo)
switch v := oo.(type) {
case *StructValue:
fmt.Println("---StructValue...")
Expand Down Expand Up @@ -377,21 +382,23 @@ func (rlm *Realm) MarkNewEscapedCheckCrossRealm(store Store, oo Object, refValue
//fmt.Println("---oo.lastNewRealEscapedRealm: ", oo.GetLastNewEscapedRealm())
//fmt.Println("---oo.GetIsReal: ", oo.GetIsReal())

if oo.GetLastNewEscapedRealm() == rlm.ID {
if oo.GetOriginRealm() == rlm.ID {
return
}

fmt.Println("---oo.GetLastNewEscapedRealm(): ", oo.GetLastNewEscapedRealm())
fmt.Println("---oo.GetLastNewEscapedRealm(): ", oo.GetOriginRealm())
fmt.Println("---rlm.ID: ", rlm.ID)
if oo.GetLastNewEscapedRealm() != rlm.ID { // crossing realm
if oo.GetOriginRealm() != rlm.ID { // crossing realm
if refValue != nil { // is reference object from external realm
checkCrossRealm(store, oo, refValue)
} else {
panic("should not happen while attempting to attach objects by value from external realm")
panic("cannot attach objects by value from external realm")
}
}

rlm.MarkNewEscaped(oo)
if !oo.GetIsEscaped() {
rlm.MarkNewEscaped(oo)
}
}

func (rlm *Realm) MarkNewReal(oo Object) {
Expand Down Expand Up @@ -609,9 +616,9 @@ func (rlm *Realm) processNewCreatedMarks(store Store) {
// oo must be marked new-real, and ref-count already incremented.
func (rlm *Realm) incRefCreatedDescendants(store Store, oo Object) {
//fmt.Println("---incRefCreatedDescendants, rlm.ID: ", rlm.ID)
//fmt.Println("---incRefCreatedDescendants oo.GetLastEscapedRealm: ", oo.GetLastNewEscapedRealm())
fmt.Println("---incRefCreatedDescendants from oo: ", oo)
//fmt.Println("---oo.GetRefCount: ", oo.GetRefCount())
fmt.Println("---oo.GetOriginRealm: ", oo.GetOriginRealm())
fmt.Println("---oo.GetRefCount: ", oo.GetRefCount())
//fmt.Println("---incRefCreatedDescendants, oo.GetObjectID: ", oo.GetObjectID())

if debug {
Expand All @@ -626,10 +633,14 @@ func (rlm *Realm) incRefCreatedDescendants(store Store, oo Object) {
// XXX, oo must be new real here, it's not escaped
// if it's reference, all right
fmt.Println("---oo.GetIsRef: ", oo.GetIsRef())
if !oo.GetLastNewEscapedRealm().IsZero() && oo.GetLastNewEscapedRealm() != rlm.ID && !oo.GetIsRef() {
if !oo.GetOriginRealm().IsZero() && oo.GetOriginRealm() != rlm.ID {
//fmt.Println("---oo.GetLastNewEscapedRealm: ", oo.GetLastNewEscapedRealm())
//fmt.Println("---rlm.ID: ", rlm.ID)
panic("should not happen while attempting to attach new real object from external realm")
if oo.GetIsRef() {
panic("cannot attach a reference to an unreal object from an external realm")
} else {
panic("cannot attach a value of a type defined by another realm")
}
}

// RECURSE GUARD
Expand Down Expand Up @@ -661,7 +672,7 @@ func (rlm *Realm) incRefCreatedDescendants(store Store, oo Object) {
}
child.IncRefCount()
rc := child.GetRefCount()
//fmt.Println("---rc after inc: ", rc)
fmt.Println("---rc after inc: ", rc)
if rc == 1 {
if child.GetIsReal() {
//fmt.Println("---child is real, child: ", child)
Expand Down Expand Up @@ -990,7 +1001,8 @@ func (rlm *Realm) saveUnsavedObjectRecursively(store Store, oo Object) {
func (rlm *Realm) saveObject(store Store, oo Object) {
fmt.Println("---saveObject: ", oo)
oid := oo.GetObjectID()
fmt.Println("---saveObject: ", oid)
fmt.Println("---oid: ", oid)
fmt.Println("---oo.GetRefCount: ", oo.GetRefCount())
if oid.IsZero() {
panic("unexpected zero object id")
}
Expand Down Expand Up @@ -1207,9 +1219,12 @@ func copyMethods(methods []TypedValue) []TypedValue {
}

func refOrCopyType(typ Type) Type {
fmt.Println("---refOrCopyType, typ: ", typ)
if dt, ok := typ.(*DeclaredType); ok {
fmt.Println("---declared type: ", dt)
return RefType{ID: dt.TypeID()}
} else {
println("---else")
return copyTypeWithRefs(typ)
}
}
Expand All @@ -1230,6 +1245,7 @@ func copyFieldsWithRefs(fields []FieldType) []FieldType {
// Copies type but with references to dependant types;
// the result is suitable for persistence bytes serialization.
func copyTypeWithRefs(typ Type) Type {
fmt.Println("---copyTypeWithRefs, typ: ", typ)
switch ct := typ.(type) {
case nil:
panic("cannot copy nil types")
Expand Down Expand Up @@ -1320,7 +1336,7 @@ func copyTypeWithRefs(typ Type) Type {
// Also checks for integrity of immediate children -- they must already be
// persistent (real), and not dirty, or else this function panics.
func copyValueWithRefs(val Value) Value {
//fmt.Println("---copyValueWithRefs, val: ", val)
fmt.Println("---copyValueWithRefs, val: ", val)
switch cv := val.(type) {
case nil:
return nil
Expand Down Expand Up @@ -1371,6 +1387,7 @@ func copyValueWithRefs(val Value) Value {
Maxcap: cv.Maxcap,
}
case *StructValue:
println("---struct value")
fields := make([]TypedValue, len(cv.Fields))
for i, ftv := range cv.Fields {
fields[i] = refOrCopyValue(ftv)
Expand Down Expand Up @@ -1495,6 +1512,7 @@ func copyValueWithRefs(val Value) Value {

// (fully) fills the type.
func fillType(store Store, typ Type) Type {
fmt.Println("---fillType, typ: ", typ)
switch ct := typ.(type) {
case nil:
return nil
Expand Down Expand Up @@ -1570,6 +1588,7 @@ func fillType(store Store, typ Type) Type {
}
return ct
case RefType:
println("---ref type")
return store.GetType(ct.TypeID())
default:
panic(fmt.Sprintf(
Expand All @@ -1578,13 +1597,15 @@ func fillType(store Store, typ Type) Type {
}

func fillTypesTV(store Store, tv *TypedValue) {
fmt.Println("---fillTypesTV, tv: ", tv)
tv.T = fillType(store, tv.T)
tv.V = fillTypesOfValue(store, tv.V)
}

// Partially fills loaded objects shallowly, similarly to
// getUnsavedTypes. Replaces all RefTypes with corresponding types.
func fillTypesOfValue(store Store, val Value) Value {
fmt.Println("---fillTypesOfValue, val: ", val)
switch cv := val.(type) {
case nil: // do nothing
return cv
Expand Down Expand Up @@ -1615,6 +1636,7 @@ func fillTypesOfValue(store Store, val Value) Value {
fillTypesOfValue(store, cv.Base)
return cv
case *StructValue:
println("struct value")
for i := 0; i < len(cv.Fields); i++ {
ctv := &cv.Fields[i]
fillTypesTV(store, ctv)
Expand Down Expand Up @@ -1780,10 +1802,11 @@ func ensureUniq(oozz ...[]Object) {
}

func refOrCopyValue(tv TypedValue) TypedValue {
//fmt.Println("---refOrCopyValue:", tv)
fmt.Println("---refOrCopyValue:", tv)
if tv.T != nil {
tv.T = refOrCopyType(tv.T)
}
fmt.Println("---Tv.T: ", tv.T)
if obj, ok := tv.V.(Object); ok {
tv.V = toRefValue(obj)
return tv
Expand Down
8 changes: 6 additions & 2 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (ds *defaultStore) GetObjectSafe(oid ObjectID) Object {
// loads and caches an object.
// CONTRACT: object isn't already in the cache.
func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
fmt.Println("---loadObjectSafe, oid: ", oid)
key := backendObjectKey(oid)
hashbz := ds.baseStore.Get([]byte(key))
if hashbz != nil {
Expand All @@ -324,11 +325,11 @@ func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
// NOTE: unlike GetObject(), SetObject() is also used to persist updated
// package values.
func (ds *defaultStore) SetObject(oo Object) {
//fmt.Println("---SetObject---,oo: ", oo)
fmt.Println("---SetObject: ", oo)
oid := oo.GetObjectID()
// replace children/fields with Ref.
o2 := copyValueWithRefs(oo)
//fmt.Println("---SetObject, o2: ", o2)
fmt.Println("---o2: ", o2)
// marshal to binary.
bz := amino.MustMarshalAny(o2)
// set hash.
Expand Down Expand Up @@ -399,7 +400,9 @@ func (ds *defaultStore) DelObject(oo Object) {
// NOTE: The implementation matches that of GetObject() in anticipation of what
// the persistent type system might work like.
func (ds *defaultStore) GetType(tid TypeID) Type {
fmt.Println("---GetType: ", tid)
tt := ds.GetTypeSafe(tid)
fmt.Println("---tt: ", tt)
if tt == nil {
ds.Print()
panic(fmt.Sprintf("unexpected type with id %s", tid.String()))
Expand All @@ -408,6 +411,7 @@ func (ds *defaultStore) GetType(tid TypeID) Type {
}

func (ds *defaultStore) GetTypeSafe(tid TypeID) Type {
fmt.Println("---GetTypeSafe: ", tid)
// check cache.
if tt, exists := ds.cacheTypes[tid]; exists {
return tt
Expand Down
Loading

0 comments on commit 3ec7a2e

Please sign in to comment.