1
0
mirror of https://github.com/golang/go synced 2024-11-24 07:00:13 -07:00

syscall: improve NewCallback documentation and panic message

This commit is contained in:
Jeet Parekh 2018-07-26 19:52:52 +05:30
parent eb245411f5
commit 51cc964fb7
2 changed files with 5 additions and 5 deletions

View File

@ -46,16 +46,16 @@ func compileCallback(fn eface, cleanstack bool) (code uintptr) {
} }
ft := (*functype)(unsafe.Pointer(fn._type)) ft := (*functype)(unsafe.Pointer(fn._type))
if len(ft.out()) != 1 { if len(ft.out()) != 1 {
panic("compileCallback: function must have one output parameter of type uintptr") panic("compileCallback: function must have one result of type uintptr")
} }
uintptrSize := unsafe.Sizeof(uintptr(0)) uintptrSize := unsafe.Sizeof(uintptr(0))
if ft.out()[0].size != uintptrSize { if ft.out()[0].size != uintptrSize {
panic("compileCallback: output parameter is not uintptr") panic("compileCallback: result is not of type uintptr")
} }
argsize := uintptr(0) argsize := uintptr(0)
for _, t := range ft.in() { for _, t := range ft.in() {
if t.size > uintptrSize { if t.size > uintptrSize {
panic("compileCallback: input parameter size is more than uintptr") panic("compileCallback: argument size is larger than uintptr")
} }
argsize += uintptrSize argsize += uintptrSize
} }

View File

@ -122,14 +122,14 @@ func compileCallback(fn interface{}, cleanstack bool) uintptr
// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention. // NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
// This is useful when interoperating with Windows code requiring callbacks. // This is useful when interoperating with Windows code requiring callbacks.
// The argument is expected to be a function with signature - func(args) uintptr { ... }. The size of any arg must not be more than the size of uintptr. // The argument is expected to be a function with signature - func(args) uintptr { ... }. The size of arguments must not be more than the size of uintptr.
func NewCallback(fn interface{}) uintptr { func NewCallback(fn interface{}) uintptr {
return compileCallback(fn, true) return compileCallback(fn, true)
} }
// NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention. // NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention.
// This is useful when interoperating with Windows code requiring callbacks. // This is useful when interoperating with Windows code requiring callbacks.
// The argument is expected to be a function with signature - func(args) uintptr { ... }. The size of any arg must not be more than the size of uintptr. // The argument is expected to be a function with signature - func(args) uintptr { ... }. The size of arguments must not be more than the size of uintptr.
func NewCallbackCDecl(fn interface{}) uintptr { func NewCallbackCDecl(fn interface{}) uintptr {
return compileCallback(fn, false) return compileCallback(fn, false)
} }