1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3
4package purego_test
5
6import (
7 "fmt"
8 "runtime"
9 "testing"
10 "unsafe"
11
12 "github.com/ebitengine/purego"
13)
14
15// This is an internal OS-dependent function for getting the handle to a library
16//
17//go:linkname openLibrary openLibrary
18func openLibrary(name string) (uintptr, error)
19
20func getSystemLibrary() (string, error) {
21 switch runtime.GOOS {
22 case "darwin":
23 return "/usr/lib/libSystem.B.dylib", nil
24 case "linux":
25 return "libc.so.6", nil
26 case "freebsd":
27 return "libc.so.7", nil
28 case "windows":
29 return "ucrtbase.dll", nil
30 default:
31 return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)
32 }
33}
34
35func TestRegisterFunc(t *testing.T) {
36 library, err := getSystemLibrary()
37 if err != nil {
38 t.Fatalf("couldn't get system library: %s", err)
39 }
40 libc, err := openLibrary(library)
41 if err != nil {
42 t.Fatalf("failed to dlopen: %s", err)
43 }
44 var puts func(string)
45 purego.RegisterLibFunc(&puts, libc, "puts")
46 puts("Calling C from from Go without Cgo!")
47}
48
49func 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 {
57 fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9)
58 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
59 })
60
61 var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int
62 purego.RegisterFunc(&fn, cb)
63
64 ret := fn(1, 2, 3, 4, 5, 6, 7, 8, 9)
65 fmt.Println(ret)
66
67 // Output: 1 2 3 4 5 6 7 8 9
68 // 45
69}
70
71func 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()
78 if err != nil {
79 t.Fatalf("couldn't get system library: %s", err)
80 }
81 libc, err := openLibrary(library)
82 if err != nil {
83 t.Fatalf("failed to dlopen: %s", err)
84 }
85
86 data := []int{88, 56, 100, 2, 25}
87 sorted := []int{2, 25, 56, 88, 100}
88 compare := func(a, b *int) int {
89 return *a - *b
90 }
91 var qsort func(data []int, nitms uintptr, size uintptr, compar func(a, b *int) int)
92 purego.RegisterLibFunc(&qsort, libc, "qsort")
93 qsort(data, uintptr(len(data)), unsafe.Sizeof(int(0)), compare)
94 for i := range data {
95 if data[i] != sorted[i] {
96 t.Errorf("got %d wanted %d at %d", data[i], sorted[i], i)
97 }
98 }
99}
100
101func TestRegisterFunc_Floats(t *testing.T) {
102 if runtime.GOOS == "linux" {
103 // TODO: enable once callbacks are working properly on Linux
104 t.Skip("callbacks are not supported on Linux")
105 }
106
107 library, err := getSystemLibrary()
108 if err != nil {
109 t.Fatalf("couldn't get system library: %s", err)
110 }
111 libc, err := openLibrary(library)
112 if err != nil {
113 t.Fatalf("failed to dlopen: %s", err)
114 }
115 {
116 var strtof func(arg string) float32
117 purego.RegisterLibFunc(&strtof, libc, "strtof")
118 const (
119 arg = "2"
120 )
121 got := strtof(arg)
122 expected := float32(2)
123 if got != expected {
124 t.Errorf("strtof failed. got %f but wanted %f", got, expected)
125 }
126 }
127 {
128 var strtod func(arg string, ptr **byte) float64
129 purego.RegisterLibFunc(&strtod, libc, "strtod")
130 const (
131 arg = "1"
132 )
133 got := strtod(arg, nil)
134 expected := float64(1)
135 if got != expected {
136 t.Errorf("strtod failed. got %f but wanted %f", got, expected)
137 }
138 }
139}
140
141func TestRegisterLibFunc_Bool(t *testing.T) {
142 if runtime.GOOS == "linux" {
143 // TODO: enable once callbacks are working properly on Linux
144 t.Skip("callbacks are not supported on Linux")
145 }
146
147 // this callback recreates the state where the return register
148 // contains other information but the least significant byte is false
149 cbFalse := purego.NewCallback(func() uintptr {
150 return 0x7F5948AE9A00
151 })
152 var runFalse func() bool
153 purego.RegisterFunc(&runFalse, cbFalse)
154 expected := false
155 if got := runFalse(); got != expected {
156 t.Errorf("runFalse failed. got %t but wanted %t", got, expected)
157 }
158}