v0.5.0-alpha.3 6319229

callback_test.go RawHistoryFolder

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3
4//go:build darwin
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 darwin/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("libcbtest", "callback.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		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
63		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8
64	})
65	var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
66		f1, f2, f3, f4, f5, f6, f7, f8 float64)
67	purego.RegisterFunc(&fn, imp)
68	fn(1, 2, -3, 4, -5, 6, -7, 8, -9,
69		1, 2, 3, 4, 5, 6, 7, 8)
70
71	if cbTotal != expectCbTotal {
72		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
73	}
74	if cbTotalF != expectedCbTotalF {
75		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
76	}
77}
78
79func TestNewCallbackFloat32(t *testing.T) {
80	// This tests the maximum number of float32 arguments a function to NewCallback can take
81	const (
82		expectCbTotal    = 6
83		expectedCbTotalF = float32(45)
84	)
85	var cbTotal int
86	var cbTotalF float32
87	imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8 int,
88		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32) {
89		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
90		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9
91	})
92	var fn func(a1, a2, a3, a4, a5, a6, a7, a8 int,
93		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32)
94	purego.RegisterFunc(&fn, imp)
95	fn(1, 2, -3, 4, -5, 6, -7, 8,
96		1, 2, 3, 4, 5, 6, 7, 8, 9)
97
98	if cbTotal != expectCbTotal {
99		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
100	}
101	if cbTotalF != expectedCbTotalF {
102		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
103	}
104}
105
106func TestNewCallbackFloat32AndFloat64(t *testing.T) {
107	// This tests that calling a function with a mix of float32 and float64 arguments works
108	const (
109		expectedCbTotalF32 = float32(30)
110		expectedCbTotalF64 = float64(15)
111	)
112	var cbTotalF32 float32
113	var cbTotalF64 float64
114	imp := purego.NewCallback(func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32) {
115		cbTotalF32 = f1 + f2 + f3 + f7 + f8 + f9
116		cbTotalF64 = f4 + f5 + f6
117
118	})
119	var fn func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32)
120	purego.RegisterFunc(&fn, imp)
121	fn(1, 2, 3, 4, 5, 6, 7, 8, 9)
122
123	if cbTotalF32 != expectedCbTotalF32 {
124		t.Errorf("cbTotalF32 not correct got %f but wanted %f", cbTotalF32, expectedCbTotalF32)
125	}
126	if cbTotalF64 != expectedCbTotalF64 {
127		t.Errorf("cbTotalF64 not correct got %f but wanted %f", cbTotalF64, expectedCbTotalF64)
128	}
129}

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