Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Jul 9, 2023
1 parent 89fe146 commit 8b6effe
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 18 deletions.
16 changes: 9 additions & 7 deletions examples/gno.land/r/demo/bugs/slice_pop_push/slice_pop_push.gno
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package slice_pop_push

import (
"strings"
)

var slice = []string{"undead-element"}

func Pop() {
func Pop() []string {
slice = slice[:len(slice)-1]
return slice
}

func Push() {
func Push() []string {
slice = append(slice, "new-element")
return slice
}

func Render(path string) string {
os := []string{"undead-element-2"}
os = os[:len(os)-1]
os = append(os, "new-element-2")

return os[0] + slice[0]
return strings.Join(slice, ",")
}
49 changes: 48 additions & 1 deletion gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {
if rlm == nil {
return
}

shouldDebug := false
if (po != nil && strings.Contains(po.String(), "-element")) ||
(xo != nil && strings.Contains(xo.String(), "-element")) ||
(co != nil && strings.Contains(co.String(), "-element")) {
fmt.Println("intersting DidUpdate", xo, co)
shouldDebug = true
}

if debug {
if co != nil && co.GetIsDeleted() {
panic("cannot attach a deleted object")
Expand All @@ -142,6 +151,9 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {
}
}
if po == nil || !po.GetIsReal() {
if shouldDebug {
fmt.Println("DidUpdate - Do nothing 1")
}
return // do nothing.
}
if po.GetObjectID().PkgID != rlm.ID {
Expand All @@ -151,17 +163,32 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {
// Updates to .newCreated/.newEscaped /.newDeleted made here. (first gen)
// More appends happen during FinalizeRealmTransactions(). (second+ gen)
rlm.MarkDirty(po)
if shouldDebug {
fmt.Println("MarkDirty po", po)
}
if co != nil {
co.IncRefCount()
if co.GetRefCount() > 1 {
if shouldDebug {
fmt.Println("DidUpdate - ref count case")
}
if co.GetIsEscaped() {
fmt.Println("DidUpdate - already escaped")
// already escaped
} else {
fmt.Println("DidUpdate - mark escaped")
rlm.MarkNewEscaped(co)
}
} else if co.GetIsReal() {
}
if co.GetIsReal() {
if shouldDebug {
fmt.Println("MarkDirty co", co)
}
rlm.MarkDirty(co)
} else {
if shouldDebug {
fmt.Println("DidUpdate - else case")
}
co.SetOwner(po)
rlm.MarkNewReal(co)
}
Expand Down Expand Up @@ -211,6 +238,9 @@ func (rlm *Realm) MarkNewReal(oo Object) {
}

func (rlm *Realm) MarkDirty(oo Object) {
if strings.Contains(oo.(Value).String(), "-element") {
fmt.Println("MarkDirty implem", oo)
}
if debug {
if !oo.GetIsReal() && !oo.GetIsNewReal() {
panic("should not happen")
Expand Down Expand Up @@ -638,6 +668,9 @@ func (rlm *Realm) markDirtyAncestors(store Store) {
// Saves .created and .updated objects.
func (rlm *Realm) saveUnsavedObjects(store Store) {
for _, co := range rlm.created {
if strings.Contains(co.String(), "-element") {
fmt.Println("saveUnsavedObjects: created:", co.String())
}
// for i := len(rlm.created) - 1; i >= 0; i-- {
// co := rlm.created[i]
if !co.GetIsNewReal() {
Expand All @@ -649,6 +682,9 @@ func (rlm *Realm) saveUnsavedObjects(store Store) {
}
}
for _, uo := range rlm.updated {
if strings.Contains(uo.String(), "-element") {
fmt.Println("saveUnsavedObjects: updated:", uo.String())
}
// for i := len(rlm.updated) - 1; i >= 0; i-- {
// uo := rlm.updated[i]
if !uo.GetIsDirty() {
Expand Down Expand Up @@ -1057,6 +1093,9 @@ func copyValueWithRefs(parent Object, val Value) Value {
case nil:
return nil
case StringValue:
if cv == "undead-element" || cv == "new-element" {
fmt.Println("copyValueWithRefs", cv)
}
return cv
case BigintValue:
return cv
Expand Down Expand Up @@ -1088,6 +1127,9 @@ func copyValueWithRefs(parent Object, val Value) Value {
}
}
case *ArrayValue:
if strings.Contains(cv.String(), "-element") {
fmt.Println("copyValueWithRefs slice", cv)
}
if cv.Data == nil {
list := make([]TypedValue, len(cv.List))
for i, etv := range cv.List {
Expand All @@ -1104,6 +1146,11 @@ func copyValueWithRefs(parent Object, val Value) Value {
}
}
case *SliceValue:
if strings.Contains(cv.String(), "-element") {
fmt.Println("copyValueWithRefs slice", cv)
fmt.Println("copyValue base", cv.Base.(*ArrayValue))

}
return &SliceValue{
Base: toRefValue(parent, cv.Base),
Offset: cv.Offset,
Expand Down
3 changes: 3 additions & 0 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
// package values.
func (ds *defaultStore) SetObject(oo Object) {
oid := oo.GetObjectID()
if array, ok := oo.(Value).(*ArrayValue); ok && strings.Contains(array.String(), "-element") {
fmt.Println("SetObject intersting array", array)
}
// replace children/fields with Ref.
o2 := copyValueWithRefs(nil, oo)
// marshal to binary.
Expand Down
42 changes: 41 additions & 1 deletion gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ func (pv *PointerValue) GetBase(store Store) Object {
// TODO: document as something that enables into-native assignment.
// TODO: maybe consider this as entrypoint for DataByteValue too?
func (pv PointerValue) Assign2(alloc *Allocator, store Store, rlm *Realm, tv2 TypedValue, cu bool) {
shouldDebug := false
if strings.Contains(tv2.String(), "-element") {
fmt.Println("Assign2", tv2)
// rlm.MarkDirty(tv2.V.(*SliceValue).GetBase(store))
shouldDebug = true
}
// Special cases.
if pv.Index == PointerIndexNative {
// Special case if extended object && native.
Expand Down Expand Up @@ -276,10 +282,44 @@ func (pv PointerValue) Assign2(alloc *Allocator, store Store, rlm *Realm, tv2 Ty
}
// General case
if rlm != nil && pv.Base != nil {
if shouldDebug {
fmt.Println("Assign2 - DidUpdate", pv.TV, tv2)
}

oo1 := pv.TV.GetFirstObject(store)

markDirty := false
if pv.TV != nil && pv.TV.T != nil && pv.TV.V != nil && pv.TV.T.Kind() == SliceKind && tv2.T != nil && tv2.V != nil && tv2.T.Kind() == SliceKind && pv.TV.V.(*SliceValue).Length != tv2.V.(*SliceValue).Length {
//fmt.Println("MarkDirty weird", pv.TV, tv2)
markDirty = true
}

pv.TV.Assign(alloc, tv2, cu)
oo2 := pv.TV.GetFirstObject(store)
oo2 := tv2.GetFirstObject(store)
if shouldDebug {
fmt.Printf("Assign2 - DidUpdate after %p %p\n", oo1, oo2)
}
rlm.DidUpdate(pv.Base.(Object), oo1, oo2)
if markDirty {
//rlm.MarkDirty(oo1)
}

/*
if shouldDebug {
if pv.TV != nil && tv2.V != nil && oo1 != nil && oo2 != nil {
switch pv.TV.V.(type) {
case *SliceValue:
switch tv2.V.(type) {
case *SliceValue:
base := pv.TV.V.(*SliceValue).GetBase(store)
if base == tv2.V.(*SliceValue).GetBase(store) && base != nil {
rlm.MarkDirty(base)
}
}
}
}
}
*/
} else {
pv.TV.Assign(alloc, tv2, cu)
}
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216
github.com/golang/protobuf v1.5.3
github.com/google/gofuzz v1.2.0
github.com/gookit/goutil v0.6.10
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/gotuna/gotuna v0.6.0
Expand All @@ -34,7 +35,7 @@ require (
golang.org/x/crypto v0.9.0
golang.org/x/mod v0.9.0
golang.org/x/net v0.10.0
golang.org/x/term v0.8.0
golang.org/x/term v0.9.0
golang.org/x/tools v0.6.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -54,6 +55,7 @@ require (
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/klauspost/compress v1.12.3 // indirect
Expand All @@ -62,9 +64,10 @@ require (
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
19 changes: 13 additions & 6 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b6effe

Please sign in to comment.