v0.7.0 4889c1a

callback_test.go RawHistoryFolder

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3
4//go:build darwin || (linux && (!cgo || amd64 || arm64))
5
6package purego_test
7
8import (
9	"os"
10	"path/filepath"
11	"testing"
12	"unsafe"
13
14	"github.com/ebitengine/purego"
15)
16
17// TestCallGoFromSharedLib is a test that checks for stack corruption on arm64
18// when C calls Go code from a non-Go thread in a dynamically loaded share library.
19func TestCallGoFromSharedLib(t *testing.T) {
20	libFileName := filepath.Join(t.TempDir(), "libcbtest.so")
21	t.Logf("Build %v", libFileName)
22
23	if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_test.c")); err != nil {
24		t.Fatal(err)
25	}
26	defer os.Remove(libFileName)
27
28	lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
29	if err != nil {
30		t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
31	}
32
33	var callCallback func(p uintptr, s string) int
34	purego.RegisterLibFunc(&callCallback, lib, "callCallback")
35
36	goFunc := func(cstr *byte, n int) int {
37		s := string(unsafe.Slice(cstr, n))
38		t.Logf("FROM Go: %s\n", s)
39		return 1
40	}
41
42	const want = 10101
43	cb := purego.NewCallback(goFunc)
44	for i := 0; i < 10; i++ {
45		got := callCallback(cb, "a test string")
46		if got != want {
47			t.Fatalf("%d: callCallback() got %v want %v", i, got, want)
48		}
49	}
50}
51
52func TestNewCallbackFloat64(t *testing.T) {
53	// This tests the maximum number of arguments a function to NewCallback can take
54	const (
55		expectCbTotal    = -3
56		expectedCbTotalF = float64(36)
57	)
58	var cbTotal int
59	var cbTotalF float64
60	imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
61		f1, f2, f3, f4, f5, f6, f7, f8 float64,
62	) {
63		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
64		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8
65	})
66	var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
67		f1, f2, f3, f4, f5, f6, f7, f8 float64)
68	purego.RegisterFunc(&fn, imp)
69	fn(1, 2, -3, 4, -5, 6, -7, 8, -9,
70		1, 2, 3, 4, 5, 6, 7, 8)
71
72	if cbTotal != expectCbTotal {
73		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
74	}
75	if cbTotalF != expectedCbTotalF {
76		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
77	}
78}
79
80func TestNewCallbackFloat64AndIntMix(t *testing.T) {
81	// This tests interleaving float and integer arguments to NewCallback
82	const (
83		expectCbTotal = 54.75
84	)
85	var cbTotal float64
86	imp := purego.NewCallback(func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int) {
87		cbTotal = a1 + a2 + float64(a3) + float64(a4) + float64(a5) + a6 + a7 + a8 + float64(a9)
88	})
89	var fn func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int)
90	purego.RegisterFunc(&fn, imp)
91	fn(1.25, 3.25, 4, 5, 6, 7.5, 8.25, 9.5, 10)
92
93	if cbTotal != expectCbTotal {
94		t.Errorf("cbTotal not correct got %f but wanted %f", cbTotal, expectCbTotal)
95	}
96}
97
98func TestNewCallbackFloat32(t *testing.T) {
99	// This tests the maximum number of float32 arguments a function to NewCallback can take
100	const (
101		expectCbTotal    = 6
102		expectedCbTotalF = float32(45)
103	)
104	var cbTotal int
105	var cbTotalF float32
106	imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8 int,
107		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32,
108	) {
109		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
110		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9
111	})
112	var fn func(a1, a2, a3, a4, a5, a6, a7, a8 int,
113		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32)
114	purego.RegisterFunc(&fn, imp)
115	fn(1, 2, -3, 4, -5, 6, -7, 8,
116		1, 2, 3, 4, 5, 6, 7, 8, 9)
117
118	if cbTotal != expectCbTotal {
119		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
120	}
121	if cbTotalF != expectedCbTotalF {
122		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
123	}
124}
125
126func TestNewCallbackFloat32AndFloat64(t *testing.T) {
127	// This tests that calling a function with a mix of float32 and float64 arguments works
128	const (
129		expectedCbTotalF32 = float32(72)
130		expectedCbTotalF64 = float64(48)
131	)
132	var cbTotalF32 float32
133	var cbTotalF64 float64
134	imp := purego.NewCallback(func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32) {
135		cbTotalF32 = f1 + f2 + f3 + f7 + f8 + f9 + f13 + f14 + f15
136		cbTotalF64 = f4 + f5 + f6 + f10 + f11 + f12
137	})
138	var fn func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32)
139	purego.RegisterFunc(&fn, imp)
140	fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
141
142	if cbTotalF32 != expectedCbTotalF32 {
143		t.Errorf("cbTotalF32 not correct got %f but wanted %f", cbTotalF32, expectedCbTotalF32)
144	}
145	if cbTotalF64 != expectedCbTotalF64 {
146		t.Errorf("cbTotalF64 not correct got %f but wanted %f", cbTotalF64, expectedCbTotalF64)
147	}
148}

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