Allow uintptr in struct on amd64 (#317)
- commit
- 05b0c8f6678f4003e390daf3ea24bd99606d6e35
- author
- TotallyGamerJet · · 2025-05-13 17:54 UTC
- committer
- GitHub · · 2025-05-13 17:54 UTC
- parent
- 5047c08
- signature
- GPG Unverified
B5690EEEBB952194The key is unknown - refs
- tag: v0.9.0-alpha.5
3 files · +15 -2
M struct_amd64.go +2 -2
| @@ -200,7 +200,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp | |||
| 200 | 200 | val |= f.Uint() << shift | |
| 201 | 201 | shift += 32 | |
| 202 | 202 | class |= _INTEGER | |
| 203 | - | case reflect.Uint64, reflect.Uint: | |
| 203 | + | case reflect.Uint64, reflect.Uint, reflect.Uintptr: | |
| 204 | 204 | val = f.Uint() | |
| 205 | 205 | shift = 64 | |
| 206 | 206 | class = _INTEGER | |
| @@ -245,7 +245,7 @@ func placeStack(v reflect.Value, addStack func(uintptr)) { | |||
| 245 | 245 | addStack(f.Pointer()) | |
| 246 | 246 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |
| 247 | 247 | addStack(uintptr(f.Int())) | |
| 248 | - | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | |
| 248 | + | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | |
| 249 | 249 | addStack(uintptr(f.Uint())) | |
| 250 | 250 | case reflect.Float32: | |
| 251 | 251 | addStack(uintptr(math.Float32bits(float32(f.Float())))) | |
| @@ -200,7 +200,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp | |||||
| 200 | val |= f.Uint() << shift | 200 | val |= f.Uint() << shift | ||
| 201 | shift += 32 | 201 | shift += 32 | ||
| 202 | class |= _INTEGER | 202 | class |= _INTEGER | ||
| 203 | - | case reflect.Uint64, reflect.Uint: | 203 | + | case reflect.Uint64, reflect.Uint, reflect.Uintptr: |
| 204 | val = f.Uint() | 204 | val = f.Uint() | ||
| 205 | shift = 64 | 205 | shift = 64 | ||
| 206 | class = _INTEGER | 206 | class = _INTEGER | ||
| @@ -245,7 +245,7 @@ func placeStack(v reflect.Value, addStack func(uintptr)) { | |||||
| 245 | addStack(f.Pointer()) | 245 | addStack(f.Pointer()) | ||
| 246 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | 246 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| 247 | addStack(uintptr(f.Int())) | 247 | addStack(uintptr(f.Int())) | ||
| 248 | - | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | 248 | + | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 249 | addStack(uintptr(f.Uint())) | 249 | addStack(uintptr(f.Uint())) | ||
| 250 | case reflect.Float32: | 250 | case reflect.Float32: | ||
| 251 | addStack(uintptr(math.Float32bits(float32(f.Float())))) | 251 | addStack(uintptr(math.Float32bits(float32(f.Float())))) | ||
M struct_test.go +9 -0
| @@ -463,6 +463,15 @@ func TestRegisterFunc_structArgs(t *testing.T) { | |||
| 463 | 463 | t.Fatalf("GoUint4Fn returned %d wanted %#x", ret, expected) | |
| 464 | 464 | } | |
| 465 | 465 | } | |
| 466 | + | { | |
| 467 | + | type OneLong struct{ a uintptr } | |
| 468 | + | var TakeGoUintAndReturn func(a OneLong) uint64 | |
| 469 | + | purego.RegisterLibFunc(&TakeGoUintAndReturn, lib, "TakeGoUintAndReturn") | |
| 470 | + | expected := uint64(7) | |
| 471 | + | if ret := TakeGoUintAndReturn(OneLong{7}); ret != expected { | |
| 472 | + | t.Fatalf("TakeGoUintAndReturn returned %+v wanted %+v", ret, expected) | |
| 473 | + | } | |
| 474 | + | } | |
| 466 | 475 | } | |
| 467 | 476 | ||
| 468 | 477 | func TestRegisterFunc_structReturns(t *testing.T) { | |
| @@ -463,6 +463,15 @@ func TestRegisterFunc_structArgs(t *testing.T) { | |||||
| 463 | t.Fatalf("GoUint4Fn returned %d wanted %#x", ret, expected) | 463 | t.Fatalf("GoUint4Fn returned %d wanted %#x", ret, expected) | ||
| 464 | } | 464 | } | ||
| 465 | } | 465 | } | ||
| 466 | + | { | |||
| 467 | + | type OneLong struct{ a uintptr } | |||
| 468 | + | var TakeGoUintAndReturn func(a OneLong) uint64 | |||
| 469 | + | purego.RegisterLibFunc(&TakeGoUintAndReturn, lib, "TakeGoUintAndReturn") | |||
| 470 | + | expected := uint64(7) | |||
| 471 | + | if ret := TakeGoUintAndReturn(OneLong{7}); ret != expected { | |||
| 472 | + | t.Fatalf("TakeGoUintAndReturn returned %+v wanted %+v", ret, expected) | |||
| 473 | + | } | |||
| 474 | + | } | |||
| 466 | } | 475 | } | ||
| 467 | 476 | ||||
| 468 | func TestRegisterFunc_structReturns(t *testing.T) { | 477 | func TestRegisterFunc_structReturns(t *testing.T) { | ||
M testdata/structtest/struct_test.c +4 -0
| @@ -348,3 +348,7 @@ struct GoUint4 { | |||
| 348 | 348 | GoUint GoUint4(struct GoUint4 g) { | |
| 349 | 349 | return g.a + g.b + g.c + g.d; | |
| 350 | 350 | } | |
| 351 | + | ||
| 352 | + | GoUint TakeGoUintAndReturn(GoUint a) { | |
| 353 | + | return a; | |
| 354 | + | } | |
| @@ -348,3 +348,7 @@ struct GoUint4 { | |||||
| 348 | GoUint GoUint4(struct GoUint4 g) { | 348 | GoUint GoUint4(struct GoUint4 g) { | ||
| 349 | return g.a + g.b + g.c + g.d; | 349 | return g.a + g.b + g.c + g.d; | ||
| 350 | } | 350 | } | ||
| 351 | + | ||||
| 352 | + | GoUint TakeGoUintAndReturn(GoUint a) { | |||
| 353 | + | return a; | |||
| 354 | + | } | |||