purego: change how bool return values are converted (#181)
Closes #183
- commit
- 39f180a6cfccb18fdae1af6a7999064f3bd4d0fd
- author
- JupiterRider · · 2023-11-22 02:48 UTC
- committer
- Hajime Hoshi · · 2023-11-22 02:48 UTC
- parent
- c9a29b2
- signature
- unsigned
2 files · +15 -1
M func.go +1 -1
| @@ -263,7 +263,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||
| 263 | 263 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |
| 264 | 264 | v.SetInt(int64(r1)) | |
| 265 | 265 | case reflect.Bool: | |
| 266 | - | v.SetBool(r1 != 0) | |
| 266 | + | v.SetBool(byte(r1) != 0) | |
| 267 | 267 | case reflect.UnsafePointer: | |
| 268 | 268 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | |
| 269 | 269 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | |
| @@ -263,7 +263,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||||
| 263 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | 263 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| 264 | v.SetInt(int64(r1)) | 264 | v.SetInt(int64(r1)) | ||
| 265 | case reflect.Bool: | 265 | case reflect.Bool: | ||
| 266 | - | v.SetBool(r1 != 0) | 266 | + | v.SetBool(byte(r1) != 0) |
| 267 | case reflect.UnsafePointer: | 267 | case reflect.UnsafePointer: | ||
| 268 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | 268 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | ||
| 269 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | 269 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | ||
M func_test.go +14 -0
| @@ -137,3 +137,17 @@ func TestRegisterFunc_Floats(t *testing.T) { | |||
| 137 | 137 | } | |
| 138 | 138 | } | |
| 139 | 139 | } | |
| 140 | + | ||
| 141 | + | func TestRegisterLibFunc_Bool(t *testing.T) { | |
| 142 | + | // this callback recreates the state where the return register | |
| 143 | + | // contains other information but the least significant byte is false | |
| 144 | + | cbFalse := purego.NewCallback(func() uintptr { | |
| 145 | + | return 0x7F5948AE9A00 | |
| 146 | + | }) | |
| 147 | + | var runFalse func() bool | |
| 148 | + | purego.RegisterFunc(&runFalse, cbFalse) | |
| 149 | + | expected := false | |
| 150 | + | if got := runFalse(); got != expected { | |
| 151 | + | t.Errorf("runFalse failed. got %t but wanted %t", got, expected) | |
| 152 | + | } | |
| 153 | + | } | |
| @@ -137,3 +137,17 @@ func TestRegisterFunc_Floats(t *testing.T) { | |||||
| 137 | } | 137 | } | ||
| 138 | } | 138 | } | ||
| 139 | } | 139 | } | ||
| 140 | + | ||||
| 141 | + | func TestRegisterLibFunc_Bool(t *testing.T) { | |||
| 142 | + | // this callback recreates the state where the return register | |||
| 143 | + | // contains other information but the least significant byte is false | |||
| 144 | + | cbFalse := purego.NewCallback(func() uintptr { | |||
| 145 | + | return 0x7F5948AE9A00 | |||
| 146 | + | }) | |||
| 147 | + | var runFalse func() bool | |||
| 148 | + | purego.RegisterFunc(&runFalse, cbFalse) | |||
| 149 | + | expected := false | |||
| 150 | + | if got := runFalse(); got != expected { | |||
| 151 | + | t.Errorf("runFalse failed. got %t but wanted %t", got, expected) | |||
| 152 | + | } | |||
| 153 | + | } | |||