Support Linux callbacks (#163)
Update build tags, documentation, and remove panics to allow Linux callbacks to work! Closes #124
- commit
- 092d3a90dd8b57e94832c070f4e2d4c79e410605
- author
- Jeroen Wijenbergh · · 2023-09-22 16:04 UTC
- committer
- GitHub · · 2023-09-22 16:04 UTC
- parent
- 67367ce
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
12 files · +11 -31
M callback_test.go +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin | |
| 4 | + | //go:build darwin || (linux && (!cgo || amd64 || arm64)) | |
| 5 | 5 | ||
| 6 | 6 | package purego_test | |
| 7 | 7 | ||
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin | 4 | + | //go:build darwin || (linux && (!cgo || amd64 || arm64)) |
| 5 | 5 | ||||
| 6 | package purego_test | 6 | package purego_test | ||
| 7 | 7 | ||||
M dlfcn_nocgo_linux.go +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build !cgo | |
| 4 | + | //go:build !cgo || amd64 || arm64 | |
| 5 | 5 | ||
| 6 | 6 | package purego | |
| 7 | 7 | ||
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build !cgo | 4 | + | //go:build !cgo || amd64 || arm64 |
| 5 | 5 | ||||
| 6 | package purego | 6 | package purego | ||
| 7 | 7 | ||||
M dlfcn_stubs.s +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || freebsd || (linux && !cgo) | |
| 4 | + | //go:build darwin || freebsd || (linux && (!cgo || amd64 || arm64)) | |
| 5 | 5 | ||
| 6 | 6 | #include "textflag.h" | |
| 7 | 7 | ||
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin || freebsd || (linux && !cgo) | 4 | + | //go:build darwin || freebsd || (linux && (!cgo || amd64 || arm64)) |
| 5 | 5 | ||||
| 6 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 7 | 7 | ||||
M func.go +0 -4
| @@ -62,10 +62,6 @@ func RegisterLibFunc(fptr interface{}, handle uintptr, name string) { | |||
| 62 | 62 | // This means that using arg ...interface{} is like a cast to the function with the arguments inside arg. | |
| 63 | 63 | // This is not the same as C variadic. | |
| 64 | 64 | // | |
| 65 | - | // There are some limitations when using RegisterFunc on Linux. First, there is no support for function arguments. | |
| 66 | - | // Second, float32 and float64 arguments and return values do not work when CGO_ENABLED=1. Otherwise, Linux | |
| 67 | - | // has the same feature parity as Darwin. | |
| 68 | - | // | |
| 69 | 65 | // # Memory | |
| 70 | 66 | // | |
| 71 | 67 | // In general it is not possible for purego to guarantee the lifetimes of objects returned or received from | |
| @@ -62,10 +62,6 @@ func RegisterLibFunc(fptr interface{}, handle uintptr, name string) { | |||||
| 62 | // This means that using arg ...interface{} is like a cast to the function with the arguments inside arg. | 62 | // This means that using arg ...interface{} is like a cast to the function with the arguments inside arg. | ||
| 63 | // This is not the same as C variadic. | 63 | // This is not the same as C variadic. | ||
| 64 | // | 64 | // | ||
| 65 | - | // There are some limitations when using RegisterFunc on Linux. First, there is no support for function arguments. | |||
| 66 | - | // Second, float32 and float64 arguments and return values do not work when CGO_ENABLED=1. Otherwise, Linux | |||
| 67 | - | // has the same feature parity as Darwin. | |||
| 68 | - | // | |||
| 69 | // # Memory | 65 | // # Memory | ||
| 70 | // | 66 | // | ||
| 71 | // In general it is not possible for purego to guarantee the lifetimes of objects returned or received from | 67 | // In general it is not possible for purego to guarantee the lifetimes of objects returned or received from | ||
M func_test.go +0 -11
| @@ -47,12 +47,6 @@ func TestRegisterFunc(t *testing.T) { | |||
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | 49 | func ExampleNewCallback() { | |
| 50 | - | if runtime.GOOS == "linux" { | |
| 51 | - | // TODO: enable once callbacks are working properly on Linux | |
| 52 | - | fmt.Println("1 2 3 4 5 6 7 8 9\n45") | |
| 53 | - | return | |
| 54 | - | } | |
| 55 | - | ||
| 56 | 50 | cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int { | |
| 57 | 51 | fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9) | |
| 58 | 52 | return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 | |
| @@ -69,11 +63,6 @@ func ExampleNewCallback() { | |||
| 69 | 63 | } | |
| 70 | 64 | ||
| 71 | 65 | func Test_qsort(t *testing.T) { | |
| 72 | - | if runtime.GOOS == "linux" { | |
| 73 | - | // TODO: enable once callbacks are working properly on Linux | |
| 74 | - | t.Skip("callbacks are not supported on Linux") | |
| 75 | - | } | |
| 76 | - | ||
| 77 | 66 | library, err := getSystemLibrary() | |
| 78 | 67 | if err != nil { | |
| 79 | 68 | t.Errorf("couldn't get system library: %s", err) | |
| @@ -47,12 +47,6 @@ func TestRegisterFunc(t *testing.T) { | |||||
| 47 | } | 47 | } | ||
| 48 | 48 | ||||
| 49 | func ExampleNewCallback() { | 49 | func ExampleNewCallback() { | ||
| 50 | - | if runtime.GOOS == "linux" { | |||
| 51 | - | // TODO: enable once callbacks are working properly on Linux | |||
| 52 | - | fmt.Println("1 2 3 4 5 6 7 8 9\n45") | |||
| 53 | - | return | |||
| 54 | - | } | |||
| 55 | - | ||||
| 56 | cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int { | 50 | cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int { | ||
| 57 | fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9) | 51 | fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9) | ||
| 58 | return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 | 52 | return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 | ||
| @@ -69,11 +63,6 @@ func ExampleNewCallback() { | |||||
| 69 | } | 63 | } | ||
| 70 | 64 | ||||
| 71 | func Test_qsort(t *testing.T) { | 65 | func Test_qsort(t *testing.T) { | ||
| 72 | - | if runtime.GOOS == "linux" { | |||
| 73 | - | // TODO: enable once callbacks are working properly on Linux | |||
| 74 | - | t.Skip("callbacks are not supported on Linux") | |||
| 75 | - | } | |||
| 76 | - | ||||
| 77 | library, err := getSystemLibrary() | 66 | library, err := getSystemLibrary() | ||
| 78 | if err != nil { | 67 | if err != nil { | ||
| 79 | t.Errorf("couldn't get system library: %s", err) | 68 | t.Errorf("couldn't get system library: %s", err) | ||
M sys_amd64.s +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) | |
| 4 | + | //go:build darwin || freebsd || linux | |
| 5 | 5 | ||
| 6 | 6 | #include "textflag.h" | |
| 7 | 7 | #include "abi_amd64.h" | |
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) | 4 | + | //go:build darwin || freebsd || linux |
| 5 | 5 | ||||
| 6 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 7 | #include "abi_amd64.h" | 7 | #include "abi_amd64.h" | ||
M sys_arm64.s +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) || windows | |
| 4 | + | //go:build darwin || freebsd || linux || windows | |
| 5 | 5 | ||
| 6 | 6 | #include "textflag.h" | |
| 7 | 7 | #include "go_asm.h" | |
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) || windows | 4 | + | //go:build darwin || freebsd || linux || windows |
| 5 | 5 | ||||
| 6 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 7 | #include "go_asm.h" | 7 | #include "go_asm.h" | ||
M sys_unix_arm64.s +1 -1
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) | |
| 4 | + | //go:build darwin || freebsd || linux | |
| 5 | 5 | ||
| 6 | 6 | #include "textflag.h" | |
| 7 | 7 | #include "go_asm.h" | |
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux) | 4 | + | //go:build darwin || freebsd || linux |
| 5 | 5 | ||||
| 6 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 7 | #include "go_asm.h" | 7 | #include "go_asm.h" | ||
M syscall_cgo_linux.go +2 -2
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build cgo | |
| 4 | + | //go:build cgo && !(amd64 || arm64) | |
| 5 | 5 | ||
| 6 | 6 | package purego | |
| 7 | 7 | ||
| @@ -26,5 +26,5 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, | |||
| 26 | 26 | } | |
| 27 | 27 | ||
| 28 | 28 | func NewCallback(_ interface{}) uintptr { | |
| 29 | - | panic("purego: NewCallback not supported") | |
| 29 | + | panic("purego: NewCallback on Linux is only supported on amd64/arm64") | |
| 30 | 30 | } | |
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build cgo | 4 | + | //go:build cgo && !(amd64 || arm64) |
| 5 | 5 | ||||
| 6 | package purego | 6 | package purego | ||
| 7 | 7 | ||||
| @@ -26,5 +26,5 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, | |||||
| 26 | } | 26 | } | ||
| 27 | 27 | ||||
| 28 | func NewCallback(_ interface{}) uintptr { | 28 | func NewCallback(_ interface{}) uintptr { | ||
| 29 | - | panic("purego: NewCallback not supported") | 29 | + | panic("purego: NewCallback on Linux is only supported on amd64/arm64") |
| 30 | } | 30 | } | ||
M syscall_sysv.go +1 -6
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux && (amd64 || arm64)) | |
| 4 | + | //go:build darwin || freebsd || (linux && (amd64 || arm64)) | |
| 5 | 5 | ||
| 6 | 6 | package purego | |
| 7 | 7 | ||
| @@ -37,12 +37,7 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, | |||
| 37 | 37 | // of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated | |
| 38 | 38 | // for these callbacks is never released. At least 2000 callbacks can always be created. Although this function | |
| 39 | 39 | // provides similar functionality to windows.NewCallback it is distinct. | |
| 40 | - | // | |
| 41 | - | // NOTE: Linux is currently not supported and will panic if called. | |
| 42 | 40 | func NewCallback(fn interface{}) uintptr { | |
| 43 | - | if runtime.GOOS == "linux" { | |
| 44 | - | panic("purego: NewCallback not supported") | |
| 45 | - | } | |
| 46 | 41 | return compileCallback(fn) | |
| 47 | 42 | } | |
| 48 | 43 | ||
| @@ -1,7 +1,7 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | - | //go:build darwin || freebsd || (!cgo && linux && (amd64 || arm64)) | 4 | + | //go:build darwin || freebsd || (linux && (amd64 || arm64)) |
| 5 | 5 | ||||
| 6 | package purego | 6 | package purego | ||
| 7 | 7 | ||||
| @@ -37,12 +37,7 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, | |||||
| 37 | // of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated | 37 | // of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated | ||
| 38 | // for these callbacks is never released. At least 2000 callbacks can always be created. Although this function | 38 | // for these callbacks is never released. At least 2000 callbacks can always be created. Although this function | ||
| 39 | // provides similar functionality to windows.NewCallback it is distinct. | 39 | // provides similar functionality to windows.NewCallback it is distinct. | ||
| 40 | - | // | |||
| 41 | - | // NOTE: Linux is currently not supported and will panic if called. | |||
| 42 | func NewCallback(fn interface{}) uintptr { | 40 | func NewCallback(fn interface{}) uintptr { | ||
| 43 | - | if runtime.GOOS == "linux" { | |||
| 44 | - | panic("purego: NewCallback not supported") | |||
| 45 | - | } | |||
| 46 | return compileCallback(fn) | 41 | return compileCallback(fn) | ||
| 47 | } | 42 | } | ||
| 48 | 43 | ||||
M zcallback_amd64.s +1 -1
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | |
| 2 | 2 | ||
| 3 | - | //go:build darwin || freebsd || (!cgo && linux) | |
| 3 | + | //go:build darwin || freebsd || linux | |
| 4 | 4 | ||
| 5 | 5 | // runtime·callbackasm is called by external code to | |
| 6 | 6 | // execute Go implemented callback function. It is not | |
| @@ -1,6 +1,6 @@ | |||||
| 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | ||
| 2 | 2 | ||||
| 3 | - | //go:build darwin || freebsd || (!cgo && linux) | 3 | + | //go:build darwin || freebsd || linux |
| 4 | 4 | ||||
| 5 | // runtime·callbackasm is called by external code to | 5 | // runtime·callbackasm is called by external code to | ||
| 6 | // execute Go implemented callback function. It is not | 6 | // execute Go implemented callback function. It is not | ||
M zcallback_arm64.s +1 -1
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | |
| 2 | 2 | ||
| 3 | - | //go:build darwin || freebsd || (!cgo && linux) | |
| 3 | + | //go:build darwin || freebsd || linux | |
| 4 | 4 | ||
| 5 | 5 | // External code calls into callbackasm at an offset corresponding | |
| 6 | 6 | // to the callback index. Callbackasm is a table of MOV and B instructions. | |
| @@ -1,6 +1,6 @@ | |||||
| 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | 1 | // Code generated by wincallback.go using 'go generate'. DO NOT EDIT. | ||
| 2 | 2 | ||||
| 3 | - | //go:build darwin || freebsd || (!cgo && linux) | 3 | + | //go:build darwin || freebsd || linux |
| 4 | 4 | ||||
| 5 | // External code calls into callbackasm at an offset corresponding | 5 | // External code calls into callbackasm at an offset corresponding | ||
| 6 | // to the callback index. Callbackasm is a table of MOV and B instructions. | 6 | // to the callback index. Callbackasm is a table of MOV and B instructions. | ||