-
Notifications
You must be signed in to change notification settings - Fork 99
purego: extend struct argument support to Linux amd64/arm64 #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmc
wants to merge
11
commits into
ebitengine:main
Choose a base branch
from
tmc:linux-struct-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7e113f
purego: extend struct argument support to Linux amd64/arm64
tmc 4a44518
purego: fix struct test compatibility issues on Linux
tmc 9940abf
struct_test: address pr feedback
tmc a2cc070
purego: extract duplicate struct validation logic into helper function
tmc 3c62bd1
struct_test: use explicit signed char for Array4Chars field
tmc 424ce87
func: improve helper
tmc 3796651
func: rename to ensureStructSupportedForRegisterFunc
tmc 27181c2
struct_test: remove redundant comments from pointer wrapper tests
tmc e9f54fb
struct_test: simplify pointer wrapper tests with constant values
tmc 8aa29bd
struct_test: fix go vet warnings in pointer wrapper tests
tmc a82375f
purego: add boolean field support to struct handling
tmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2024 The Ebitengine Authors | ||
|
|
||
| //go:build darwin && (arm64 || amd64) | ||
| //go:build (darwin || linux) && (amd64 || arm64) | ||
|
|
||
| package purego_test | ||
|
|
||
|
|
@@ -373,8 +373,8 @@ func TestRegisterFunc_structArgs(t *testing.T) { | |
| } | ||
| var Array4CharsFn func(chars Array4Chars) int32 | ||
| purego.RegisterLibFunc(&Array4CharsFn, lib, "Array4Chars") | ||
| const expectedSum = 1 + 2 + 4 + 8 | ||
| if ret := Array4CharsFn(Array4Chars{a: [...]int8{1, 2, 4, 8}}); ret != expectedSum { | ||
| const expectedSum = 123 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: const expectedSum = 10 + 20 + 30 + 63 |
||
| if ret := Array4CharsFn(Array4Chars{a: [...]int8{10, 20, 30, 63}}); ret != expectedSum { | ||
| t.Fatalf("Array4CharsFn returned %d wanted %d", ret, expectedSum) | ||
| } | ||
| } | ||
|
|
@@ -499,6 +499,37 @@ func TestRegisterFunc_structArgs(t *testing.T) { | |
| t.Fatalf("FourInt32s returned %d wanted %d", result, want) | ||
| } | ||
| } | ||
| { | ||
| type PointerWrapper struct { | ||
hajimehoshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx unsafe.Pointer | ||
| } | ||
| var ExtractPointer func(wrapper PointerWrapper) uintptr | ||
| purego.RegisterLibFunc(&ExtractPointer, lib, "ExtractPointer") | ||
|
|
||
| var v int | ||
| ptr := unsafe.Pointer(&v) | ||
| expected := uintptr(ptr) | ||
| result := ExtractPointer(PointerWrapper{ctx: ptr}) | ||
| if result != expected { | ||
| t.Fatalf("ExtractPointer returned %#x wanted %#x", result, expected) | ||
| } | ||
| } | ||
| { | ||
| type TwoPointers struct { | ||
| ptr1, ptr2 unsafe.Pointer | ||
| } | ||
| var AddPointers func(wrapper TwoPointers) uintptr | ||
| purego.RegisterLibFunc(&AddPointers, lib, "AddPointers") | ||
|
|
||
| var v1, v2 int | ||
| ptr1 := unsafe.Pointer(&v1) | ||
| ptr2 := unsafe.Pointer(&v2) | ||
| expected := uintptr(ptr1) + uintptr(ptr2) | ||
| result := AddPointers(TwoPointers{ptr1, ptr2}) | ||
| if result != expected { | ||
| t.Fatalf("AddPointers returned %#x wanted %#x", result, expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestRegisterFunc_structReturns(t *testing.T) { | ||
|
|
@@ -757,6 +788,49 @@ func TestRegisterFunc_structReturns(t *testing.T) { | |
| t.Fatalf("ReturnMixed4 returned %+v wanted %+v", ret, expected) | ||
| } | ||
| } | ||
| { | ||
| type Mixed5 struct { | ||
| a *int64 | ||
| b int32 | ||
| c float32 | ||
| d int32 | ||
| } | ||
| var ReturnMixed5 func(a *int64, b int32, c float32, d int32) Mixed5 | ||
| purego.RegisterLibFunc(&ReturnMixed5, lib, "ReturnMixed5") | ||
| ptr := new(int64) | ||
| expected := Mixed5{ptr, 1, 7.2, 9} | ||
| if ret := ReturnMixed5(ptr, 1, 7.2, 9); ret != expected { | ||
| t.Fatalf("ReturnMixed5 returned %+v wanted %+v", ret, expected) | ||
| } | ||
| runtime.KeepAlive(ptr) | ||
| } | ||
| { | ||
| type SmallBool struct { | ||
| a bool | ||
| b int32 | ||
| c int64 | ||
| } | ||
| var ReturnSmallBool func(a bool, b int32, c int64) SmallBool | ||
| purego.RegisterLibFunc(&ReturnSmallBool, lib, "ReturnSmallBool") | ||
| expected := SmallBool{true, 42, 123456789} | ||
| if ret := ReturnSmallBool(true, 42, 123456789); ret != expected { | ||
| t.Fatalf("ReturnSmallBool returned %+v wanted %+v", ret, expected) | ||
| } | ||
| } | ||
| { | ||
| type LargeBool struct { | ||
| a bool | ||
| b int32 | ||
| c int64 | ||
| d int64 | ||
| } | ||
| var ReturnLargeBool func(a bool, b int32, c int64, d int64) LargeBool | ||
| purego.RegisterLibFunc(&ReturnLargeBool, lib, "ReturnLargeBool") | ||
| expected := LargeBool{false, -99, 987654321, 111222333444} | ||
| if ret := ReturnLargeBool(false, -99, 987654321, 111222333444); ret != expected { | ||
| t.Fatalf("ReturnLargeBool returned %+v wanted %+v", ret, expected) | ||
| } | ||
| } | ||
| { | ||
| type Ptr1 struct { | ||
| a *int64 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.