diff --git a/call_test.go b/call_test.go index 42dc433..b780424 100644 --- a/call_test.go +++ b/call_test.go @@ -169,6 +169,14 @@ func TestCallSupportsCommonCIntegerTypes(t *testing.T) { } } +func TestCallSupportsCUintptrArgument(t *testing.T) { + t.Parallel() + + if err := callSupportsCUintptrArgument(); err != nil { + t.Fatalf("expected success, got %v", err) + } +} + func TestErrnoMapping(t *testing.T) { t.Parallel() diff --git a/call_test_support.go b/call_test_support.go index aefd2a3..bbf8a8f 100644 --- a/call_test_support.go +++ b/call_test_support.go @@ -82,6 +82,10 @@ int call_c_types_arguments(long a0, unsigned long a1, long long a2, unsigned lon return 13; } +int call_uintptr_argument(uintptr_t value) { + return value == 42 ? 0 : 13; +} + uintptr_t call_sum_length_ptr(void) { return (uintptr_t)&call_sum_length; } @@ -130,6 +134,10 @@ uintptr_t call_c_types_arguments_ptr(void) { return (uintptr_t)&call_c_types_arguments; } +uintptr_t call_uintptr_argument_ptr(void) { + return (uintptr_t)&call_uintptr_argument; +} + */ import "C" @@ -195,6 +203,11 @@ func callCTypeArgumentFunction() unsafe.Pointer { return *(*unsafe.Pointer)(unsafe.Pointer(&function)) } +func callCUintptrArgumentFunction() unsafe.Pointer { + function := C.call_uintptr_argument_ptr() + return *(*unsafe.Pointer)(unsafe.Pointer(&function)) +} + func callWithErrnoZero() (int, error) { return WithErrno(func() C.int { return 0 @@ -210,3 +223,7 @@ func callWithErrnoFailure() (int, error) { func callSupportsCIntegerArguments() error { return Call(callCTypeArgumentFunction(), C.long(10), C.ulong(11), C.longlong(12), C.ulonglong(13)) } + +func callSupportsCUintptrArgument() error { + return Call(callCUintptrArgumentFunction(), C.uintptr_t(42)) +} diff --git a/string_conversion.go b/string_conversion.go index 21a4747..d924378 100644 --- a/string_conversion.go +++ b/string_conversion.go @@ -94,6 +94,7 @@ int cgo_call_16(uintptr_t fn, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_ import "C" import ( + "reflect" "strconv" "syscall" "unsafe" @@ -805,6 +806,18 @@ func toSyscallArg(value interface{}) (uintptr, bool) { case uint64: return uintptr(typed), true default: + reflected := reflect.ValueOf(value) + switch reflected.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return uintptr(reflected.Int()), true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return uintptr(reflected.Uint()), true + case reflect.Bool: + if reflected.Bool() { + return 1, true + } + return 0, true + } return 0, false } }