purego: change how bool return values are converted (#181)
Closes #183
- commit
- 192c5e846faa64025fb8a6a94532c7a77fbd8dce
- author
- JupiterRider · · 2023-11-22 02:48 UTC
- committer
- GitHub · · 2023-11-22 02:48 UTC
- parent
- 7e00a08
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown - refs
- tag: v0.6.0-alpha.2
2 files · +15 -1
M func.go +1 -1
| @@ -259,7 +259,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||
| 259 | 259 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |
| 260 | 260 | v.SetInt(int64(r1)) | |
| 261 | 261 | case reflect.Bool: | |
| 262 | - | v.SetBool(r1 != 0) | |
| 262 | + | v.SetBool(byte(r1) != 0) | |
| 263 | 263 | case reflect.UnsafePointer: | |
| 264 | 264 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | |
| 265 | 265 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | |
| @@ -259,7 +259,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||||
| 259 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | 259 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| 260 | v.SetInt(int64(r1)) | 260 | v.SetInt(int64(r1)) | ||
| 261 | case reflect.Bool: | 261 | case reflect.Bool: | ||
| 262 | - | v.SetBool(r1 != 0) | 262 | + | v.SetBool(byte(r1) != 0) |
| 263 | case reflect.UnsafePointer: | 263 | case reflect.UnsafePointer: | ||
| 264 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | 264 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | ||
| 265 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | 265 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&r1))) | ||
M func_test.go +14 -0
| @@ -121,3 +121,17 @@ func TestRegisterFunc_Floats(t *testing.T) { | |||
| 121 | 121 | } | |
| 122 | 122 | } | |
| 123 | 123 | } | |
| 124 | + | ||
| 125 | + | func TestRegisterLibFunc_Bool(t *testing.T) { | |
| 126 | + | // this callback recreates the state where the return register | |
| 127 | + | // contains other information but the least significant byte is false | |
| 128 | + | cbFalse := purego.NewCallback(func() uintptr { | |
| 129 | + | return 0x7F5948AE9A00 | |
| 130 | + | }) | |
| 131 | + | var runFalse func() bool | |
| 132 | + | purego.RegisterFunc(&runFalse, cbFalse) | |
| 133 | + | expected := false | |
| 134 | + | if got := runFalse(); got != expected { | |
| 135 | + | t.Errorf("runFalse failed. got %t but wanted %t", got, expected) | |
| 136 | + | } | |
| 137 | + | } | |
| @@ -121,3 +121,17 @@ func TestRegisterFunc_Floats(t *testing.T) { | |||||
| 121 | } | 121 | } | ||
| 122 | } | 122 | } | ||
| 123 | } | 123 | } | ||
| 124 | + | ||||
| 125 | + | func TestRegisterLibFunc_Bool(t *testing.T) { | |||
| 126 | + | // this callback recreates the state where the return register | |||
| 127 | + | // contains other information but the least significant byte is false | |||
| 128 | + | cbFalse := purego.NewCallback(func() uintptr { | |||
| 129 | + | return 0x7F5948AE9A00 | |||
| 130 | + | }) | |||
| 131 | + | var runFalse func() bool | |||
| 132 | + | purego.RegisterFunc(&runFalse, cbFalse) | |||
| 133 | + | expected := false | |||
| 134 | + | if got := runFalse(); got != expected { | |||
| 135 | + | t.Errorf("runFalse failed. got %t but wanted %t", got, expected) | |||
| 136 | + | } | |||
| 137 | + | } | |||