mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-16 10:43:29 +00:00
reflect: fix key type logic for maps
This commit is contained in:
@@ -810,8 +810,10 @@ func (v Value) MapIndex(key Value) Value {
|
|||||||
panic(&ValueError{Method: "MapIndex", Kind: v.Kind()})
|
panic(&ValueError{Method: "MapIndex", Kind: v.Kind()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vkey := v.typecode.key()
|
||||||
|
|
||||||
// compare key type with actual key type of map
|
// compare key type with actual key type of map
|
||||||
if key.typecode != v.typecode.key() {
|
if !key.typecode.AssignableTo(vkey) {
|
||||||
// type error?
|
// type error?
|
||||||
panic("reflect.Value.MapIndex: incompatible types for key")
|
panic("reflect.Value.MapIndex: incompatible types for key")
|
||||||
}
|
}
|
||||||
@@ -819,12 +821,12 @@ func (v Value) MapIndex(key Value) Value {
|
|||||||
elemType := v.typecode.Elem()
|
elemType := v.typecode.Elem()
|
||||||
elem := New(elemType)
|
elem := New(elemType)
|
||||||
|
|
||||||
if key.Kind() == String {
|
if vkey.Kind() == String {
|
||||||
if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok {
|
if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok {
|
||||||
return Value{}
|
return Value{}
|
||||||
}
|
}
|
||||||
return elem.Elem()
|
return elem.Elem()
|
||||||
} else if key.typecode.isBinary() {
|
} else if vkey.isBinary() {
|
||||||
var keyptr unsafe.Pointer
|
var keyptr unsafe.Pointer
|
||||||
if key.isIndirect() || key.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
|
if key.isIndirect() || key.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
|
||||||
keyptr = key.value
|
keyptr = key.value
|
||||||
@@ -1367,8 +1369,10 @@ func (v Value) SetMapIndex(key, elem Value) {
|
|||||||
panic(&ValueError{Method: "SetMapIndex", Kind: v.Kind()})
|
panic(&ValueError{Method: "SetMapIndex", Kind: v.Kind()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vkey := v.typecode.key()
|
||||||
|
|
||||||
// compare key type with actual key type of map
|
// compare key type with actual key type of map
|
||||||
if key.typecode != v.typecode.key() {
|
if !key.typecode.AssignableTo(vkey) {
|
||||||
panic("reflect.Value.SetMapIndex: incompatible types for key")
|
panic("reflect.Value.SetMapIndex: incompatible types for key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user