detached 092d3a9

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 Unverified4AEE18F83AFDEB23 The key is unknown
12 files · +11 -31
M callback_test.go +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33
4-//go:build darwin
4+//go:build darwin || (linux && (!cgo || amd64 || arm64))
55
66 package purego_test
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2023 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3 3
4-//go:build darwin4+//go:build darwin || (linux && (!cgo || amd64 || arm64))
5 5
6 package purego_test6 package purego_test
7 7
M dlfcn_nocgo_linux.go +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build !cgo
4+//go:build !cgo || amd64 || arm64
55
66 package purego
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build !cgo4+//go:build !cgo || amd64 || arm64
5 5
6 package purego6 package purego
7 7
M dlfcn_stubs.s +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (linux && !cgo)
4+//go:build darwin || freebsd || (linux && (!cgo || amd64 || arm64))
55
66 #include "textflag.h"
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // 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) {
6262 // This means that using arg ...interface{} is like a cast to the function with the arguments inside arg.
6363 // This is not the same as C variadic.
6464 //
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-//
6965 // # Memory
7066 //
7167 // 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 // # Memory65 // # Memory
70 //66 //
71 // In general it is not possible for purego to guarantee the lifetimes of objects returned or received from67 // 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) {
4747 }
4848
4949 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-
5650 cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int {
5751 fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9)
5852 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
@@ -69,11 +63,6 @@ func ExampleNewCallback() {
6963 }
7064
7165 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-
7766 library, err := getSystemLibrary()
7867 if err != nil {
7968 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 + a952 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 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux)
4+//go:build darwin || freebsd || linux
55
66 #include "textflag.h"
77 #include "abi_amd64.h"
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // 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 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux) || windows
4+//go:build darwin || freebsd || linux || windows
55
66 #include "textflag.h"
77 #include "go_asm.h"
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd || (!cgo && linux) || windows4+//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 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux)
4+//go:build darwin || freebsd || linux
55
66 #include "textflag.h"
77 #include "go_asm.h"
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2023 The Ebitengine Authors2 // 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 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build cgo
4+//go:build cgo && !(amd64 || arm64)
55
66 package purego
77
@@ -26,5 +26,5 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2,
2626 }
2727
2828 func NewCallback(_ interface{}) uintptr {
29- panic("purego: NewCallback not supported")
29+ panic("purego: NewCallback on Linux is only supported on amd64/arm64")
3030 }
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build cgo4+//go:build cgo && !(amd64 || arm64)
5 5
6 package purego6 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 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux && (amd64 || arm64))
4+//go:build darwin || freebsd || (linux && (amd64 || arm64))
55
66 package purego
77
@@ -37,12 +37,7 @@ func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2,
3737 // of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
3838 // for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
3939 // provides similar functionality to windows.NewCallback it is distinct.
40-//
41-// NOTE: Linux is currently not supported and will panic if called.
4240 func NewCallback(fn interface{}) uintptr {
43- if runtime.GOOS == "linux" {
44- panic("purego: NewCallback not supported")
45- }
4641 return compileCallback(fn)
4742 }
4843
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // 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 purego6 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 allocated37 // 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 function38 // 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 @@
11 // Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
22
3-//go:build darwin || freebsd || (!cgo && linux)
3+//go:build darwin || freebsd || linux
44
55 // runtime·callbackasm is called by external code to
66 // 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 to5 // runtime·callbackasm is called by external code to
6 // execute Go implemented callback function. It is not6 // execute Go implemented callback function. It is not
M zcallback_arm64.s +1 -1
@@ -1,6 +1,6 @@
11 // Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
22
3-//go:build darwin || freebsd || (!cgo && linux)
3+//go:build darwin || freebsd || linux
44
55 // External code calls into callbackasm at an offset corresponding
66 // 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 corresponding5 // 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.

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close