reflect: Handle calls to functions that take or return empty structs

Fixes issue 6761

This simple change seems to work fine, slightly to my surprise.

This includes the tests I submitted to the main Go repository at
https://codereview.appspot.com/26570046

From-SVN: r205001
This commit is contained in:
Ian Lance Taylor 2013-11-19 02:30:03 +00:00
parent 9d5eec2da6
commit 41674b9fe4
2 changed files with 44 additions and 1 deletions

View File

@ -1434,6 +1434,46 @@ func TestFunc(t *testing.T) {
}
}
type emptyStruct struct{}
type nonEmptyStruct struct {
member int
}
func returnEmpty() emptyStruct {
return emptyStruct{}
}
func takesEmpty(e emptyStruct) {
}
func returnNonEmpty(i int) nonEmptyStruct {
return nonEmptyStruct{member: i}
}
func takesNonEmpty(n nonEmptyStruct) int {
return n.member
}
func TestCallWithStruct(t *testing.T) {
r := ValueOf(returnEmpty).Call([]Value{})
if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) {
t.Errorf("returning empty struct returned %s instead", r)
}
r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})})
if len(r) != 0 {
t.Errorf("takesEmpty returned values: %s", r)
}
r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)})
if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 {
t.Errorf("returnNonEmpty returned %s", r)
}
r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})})
if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 {
t.Errorf("takesNonEmpty returned %s", r)
}
}
func TestMakeFunc(t *testing.T) {
switch runtime.GOARCH {
case "amd64", "386":

View File

@ -98,9 +98,12 @@ go_struct_to_ffi (const struct __go_struct_type *descriptor)
const struct __go_struct_field *fields;
int i;
field_count = descriptor->__fields.__count;
if (field_count == 0) {
return &ffi_type_void;
}
ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
ret->type = FFI_TYPE_STRUCT;
field_count = descriptor->__fields.__count;
fields = (const struct __go_struct_field *) descriptor->__fields.__values;
ret->elements = (ffi_type **) __go_alloc ((field_count + 1)
* sizeof (ffi_type *));