Skip to content

Commit

Permalink
Return true values of struct fields or reflect slice elements, rather…
Browse files Browse the repository at this point in the history
… than pointers to them. Closes #378.
  • Loading branch information
dop251 committed Apr 5, 2022
1 parent 90825c0 commit 9037c2b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion object_goarray_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (o *objectGoArrayReflect) _getIdx(idx int) Value {
if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && v.IsNil() {
return _null
}
return o.val.runtime.ToValue(v.Interface())
return o.val.runtime.toValue(v.Interface(), v)
}

func (o *objectGoArrayReflect) getIdx(idx valueInt, receiver Value) Value {
Expand Down
4 changes: 2 additions & 2 deletions object_gomap_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (o *objectGoMapReflect) _get(n Value) Value {
return nil
}
if v := o.value.MapIndex(key); v.IsValid() {
return o.val.runtime.ToValue(v.Interface())
return o.val.runtime.toValue(v.Interface(), v)
}

return nil
Expand All @@ -53,7 +53,7 @@ func (o *objectGoMapReflect) _getStr(name string) Value {
return nil
}
if v := o.value.MapIndex(key); v.IsValid() {
return o.val.runtime.ToValue(v.Interface())
return o.val.runtime.toValue(v.Interface(), v)
}

return nil
Expand Down
15 changes: 4 additions & 11 deletions object_goreflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,15 @@ func (o *objectGoReflect) _getMethod(jsName string) reflect.Value {
return reflect.Value{}
}

func (o *objectGoReflect) getAddr(v reflect.Value) reflect.Value {
if (v.Kind() == reflect.Struct || v.Kind() == reflect.Slice) && v.CanAddr() {
return v.Addr()
}
return v
}

func (o *objectGoReflect) _get(name string) Value {
if o.value.Kind() == reflect.Struct {
if v := o._getField(name); v.IsValid() {
return o.val.runtime.ToValue(o.getAddr(v).Interface())
return o.val.runtime.toValue(v.Interface(), v)
}
}

if v := o._getMethod(name); v.IsValid() {
return o.val.runtime.ToValue(v.Interface())
return o.val.runtime.toValue(v.Interface(), v)
}

return nil
Expand All @@ -181,7 +174,7 @@ func (o *objectGoReflect) getOwnPropStr(name unistring.String) Value {
if o.value.Kind() == reflect.Struct {
if v := o._getField(n); v.IsValid() {
return &valueProperty{
value: o.val.runtime.ToValue(o.getAddr(v).Interface()),
value: o.val.runtime.toValue(v.Interface(), v),
writable: v.CanSet(),
enumerable: true,
}
Expand All @@ -190,7 +183,7 @@ func (o *objectGoReflect) getOwnPropStr(name unistring.String) Value {

if v := o._getMethod(n); v.IsValid() {
return &valueProperty{
value: o.val.runtime.ToValue(v.Interface()),
value: o.val.runtime.toValue(v.Interface(), v),
enumerable: true,
}
}
Expand Down
17 changes: 16 additions & 1 deletion runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,10 @@ Note that the underlying type is not lost, calling Export() returns the original
reflect based types.
*/
func (r *Runtime) ToValue(i interface{}) Value {
return r.toValue(i, reflect.Value{})
}

func (r *Runtime) toValue(i interface{}, origValue reflect.Value) Value {
switch i := i.(type) {
case nil:
return _null
Expand Down Expand Up @@ -1739,7 +1743,18 @@ func (r *Runtime) ToValue(i interface{}) Value {
return obj
}

origValue := reflect.ValueOf(i)
if !origValue.IsValid() {
origValue = reflect.ValueOf(i)
} else {
// If origValue was a result of an Index(), or Field(), or such, its Kind may be Interface:
// a := []interface{}{(*S)(nil)}
// a0 := reflect.ValueOf(a).Index(0) // a0.Kind() is reflect.Interface
// a1 := reflect.ValueOf(a[0]) // a1.Kind() is reflect.Ptr
// Need to "dereference" it to make it consistent with plain value being passed.
for origValue.Kind() == reflect.Interface {
origValue = origValue.Elem()
}
}
value := origValue
for value.Kind() == reflect.Ptr {
value = reflect.Indirect(value)
Expand Down

0 comments on commit 9037c2b

Please sign in to comment.