Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Jan 6, 2025
1 parent beb48e7 commit 217dfc3
Show file tree
Hide file tree
Showing 34 changed files with 916 additions and 1 deletion.
31 changes: 31 additions & 0 deletions gno.land/pkg/integration/testdata/ptr_mapkey.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
gnoland start

# add contract
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/ptrmap -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

gnokey maketx call -pkgpath gno.land/r/demo/ptrmap -func AddToMap -args 5 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

gnokey maketx call -pkgpath gno.land/r/demo/ptrmap -func GetFromMap -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '(5 int)'
stdout OK!

-- gno.mod --
module gno.land/r/demo/ptrmap

-- realm.gno --
package ptrmap

var (
m = map[*int]int{}
i = new(int)
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
return m[i]
}
35 changes: 35 additions & 0 deletions gnovm/1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]int{}
i = new(int)
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
return m[i]
}

func init() {
*i = 1
AddToMap(5)
}

// ----above is initialized and persisted before main is executed.

func main() {
r := GetFromMap()
println(r == 5)

*i = 2 // this changes TV, also Base of a pointer value
r = GetFromMap()
println(r == 5)
}

// Output:
// true
// true
2 changes: 2 additions & 0 deletions gnovm/pkg/gnolang/op_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (m *Machine) doOpEval() {
debug.Printf("EVAL: (%T) %v\n", x, x)
// fmt.Println(m.String())
}
//fmt.Printf("EVAL: (%T) %v\n", x, x)
// This case moved out of switch for performance.
// TODO: understand this better.
if nx, ok := x.(*NameExpr); ok {
Expand All @@ -37,6 +38,7 @@ func (m *Machine) doOpEval() {
lb := m.LastBlock()
// Push value, done.
ptr := lb.GetPointerToMaybeHeapUse(m.Store, nx)
//fmt.Println("ptr.Deref():", ptr.Deref())
m.PushValue(ptr.Deref())
return
}
Expand Down
1 change: 1 addition & 0 deletions gnovm/pkg/gnolang/op_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ EXEC_SWITCH:
if debug {
debug.Printf("EXEC: %v\n", s)
}
//fmt.Printf("EXEC: %v\n", s)
switch cs := s.(type) {
case *AssignStmt:
switch cs.Op {
Expand Down
6 changes: 5 additions & 1 deletion gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,11 @@ func fillTypesOfValue(store Store, val Value) Value {
fillTypesTV(store, &cur.Key)
fillTypesTV(store, &cur.Value)

cv.vmap[cur.Key.ComputeMapKey(store, false)] = cur
//fmt.Println("fillTypesOfValue, cur: ", cur)
//fmt.Println("fillTypesOfValue, cur...key...v: ", reflect.TypeOf(cur.Key.V))
key2 := cur.Key.ComputeMapKey(store, false)
//fmt.Println("mapkey: ", key2)
cv.vmap[key2] = cur
}
return cv
case TypeValue:
Expand Down
34 changes: 34 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,11 @@ func (mv *MapValue) GetLength() int {
// do for structs and arrays for assigning new entries. If key
// doesn't exist, a new slot is created.
func (mv *MapValue) GetPointerForKey(alloc *Allocator, store Store, key *TypedValue) PointerValue {
//fmt.Println("GetPointerForKey, key: ", key)
kmk := key.ComputeMapKey(store, false)
//fmt.Println("kmk1: ", kmk)
if mli, ok := mv.vmap[kmk]; ok {
//fmt.Println("exist")
key2 := key.Copy(alloc)
return PointerValue{
TV: fillValueTV(store, &mli.Value),
Expand All @@ -785,6 +788,7 @@ func (mv *MapValue) GetPointerForKey(alloc *Allocator, store Store, key *TypedVa
Index: PointerIndexMap,
}
}
//fmt.Println("NOT exist, append")
mli := mv.List.Append(alloc, *key)
mv.vmap[kmk] = mli
key2 := key.Copy(alloc)
Expand All @@ -799,11 +803,16 @@ func (mv *MapValue) GetPointerForKey(alloc *Allocator, store Store, key *TypedVa
// Like GetPointerForKey, but does not create a slot if key
// doesn't exist.
func (mv *MapValue) GetValueForKey(store Store, key *TypedValue) (val TypedValue, ok bool) {
//fmt.Println("GetValueForKey, key: ", key)
kmk := key.ComputeMapKey(store, false)
//fmt.Println("---kmk2: ", kmk)
if mli, exists := mv.vmap[kmk]; exists {
//fmt.Println("---exist")
fillValueTV(store, &mli.Value)
val, ok = mli.Value, true
//fmt.Println("---val: ", val)
}
//fmt.Println("---NOT exist")
return
}

Expand Down Expand Up @@ -1544,6 +1553,15 @@ func (tv *TypedValue) AssertNonNegative(msg string) {
}

func (tv *TypedValue) ComputeMapKey(store Store, omitType bool) MapKey {
//fmt.Println("1 ComputeMapKey, tv: ", tv, reflect.TypeOf(tv.V))
fillValueTV(store, tv)
//fmt.Println("2 ComputeMapKey, tv: ", tv, reflect.TypeOf(tv.V))
//if pv, ok := tv.V.(PointerValue); ok {
// if _, ok := pv.Base.(*HeapItemValue); ok {
// omitType = true
// }
//}

// Special case when nil: has no separator.
if tv.T == nil {
if debug {
Expand All @@ -1564,6 +1582,22 @@ func (tv *TypedValue) ComputeMapKey(store Store, omitType bool) MapKey {
pbz := tv.PrimitiveBytes()
bz = append(bz, pbz...)
case *PointerType:
//fmt.Println("pv: ", tv.V.(PointerValue))
//fmt.Println("pv.Base", tv.V.(PointerValue).Base, reflect.TypeOf(tv.V.(PointerValue).Base))
//var oid string
//if hiv, ok := tv.V.(PointerValue).Base.(*HeapItemValue); ok {
// fmt.Println("---hiv: ", hiv, reflect.TypeOf(hiv))
// fmt.Println("---hiv: ", hiv.GetObjectID())
// //tv.V.(PointerValue).Base.(Object).GetObjectID()
// if !hiv.GetObjectID().IsZero() {
// oid = hiv.GetObjectID().String()
// bz = append(bz, []byte(oid)...)
// return MapKey(bz)
// }
//}
//println("---else")
//ptr := uintptr(unsafe.Pointer(tv.V.(PointerValue).TV))
//bz = append(bz, uintptrToBytes(&ptr)...)
ptr := uintptr(unsafe.Pointer(tv.V.(PointerValue).TV))
bz = append(bz, uintptrToBytes(&ptr)...)
case FieldType:
Expand Down
27 changes: 27 additions & 0 deletions gnovm/tests/files/ptrmap_0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[int]int{}
i = 0
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
return m[i]
}

func init() {
AddToMap(5)
}

func main() {
r := GetFromMap()
println(r == 5)
}

// Output:
// true
27 changes: 27 additions & 0 deletions gnovm/tests/files/ptrmap_1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

type Foo struct {
name string
}

var root *Foo

var f = &Foo{name: "a"}
var f2 = &Foo{name: "a2"}

func init() {
root = f // origin of root is set as f's
}

func main() {
f.name = "b"
println(root == f)

root = f2 // root has new origin(and pointer value)
println(root == f2)
}

// Output:
// true
// true
21 changes: 21 additions & 0 deletions gnovm/tests/files/ptrmap_10.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]string{}
a, b = 1, 2
arr = [2]*int{&a, &b}
)

func init() {
m[arr[0]] = "first key"
}

func main() {
println(m[arr[0]]) // Output: first key
println(m[arr[1]] == "")
}

// Output:
// first key
// true
22 changes: 22 additions & 0 deletions gnovm/tests/files/ptrmap_11.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]string{}
a, b = 1, 2
S = []*int{&a, &b} // slice
index = 0
)

func init() {
m[S[index]] = "first key"
}

func main() {
println(m[S[index]]) // Output: first key
println(m[S[1]] == "")
}

// Output:
// first key
// true
21 changes: 21 additions & 0 deletions gnovm/tests/files/ptrmap_12.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]string{}
a, b = 1, 2
S = []*int{&a, &b}
)

func init() {
m[S[0]] = "first key"
}

func main() {
println(m[S[0]]) // Output: first key
println(m[S[1]] == "")
}

// Output:
// first key
// true
22 changes: 22 additions & 0 deletions gnovm/tests/files/ptrmap_13.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

type MyStruct struct {
Index int
}

var m = make(map[*int]string)
var a, b = 1, 2
var s = []*int{&a, &b}
var myStruct = MyStruct{Index: 0}

func init() {
m[s[myStruct.Index]] = "a"
}

func main() {
println(m[s[myStruct.Index]])
}

// Output:
// a
35 changes: 35 additions & 0 deletions gnovm/tests/files/ptrmap_14.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]int{}
i = new(int)
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
return m[i]
}

func init() {
*i = 1
AddToMap(5)
}

// ----above is initialized and persisted before main is executed.

func main() {
r := GetFromMap()
println(r == 5)

*i = 2 // this changes TV, also Base of a pointer value
r = GetFromMap()
println(r == 5)
}

// Output:
// true
// true
34 changes: 34 additions & 0 deletions gnovm/tests/files/ptrmap_15.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]int{}
i = new(int)
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
j := *i
return m[&j]
}

func init() {
*i = 1
AddToMap(5)
}

func main() {
r := GetFromMap()
println(r == 5)

*i = 2
r = GetFromMap()
println(r == 5)
}

// Output:
// false
// false
Loading

0 comments on commit 217dfc3

Please sign in to comment.