v0.13.0-bnema.2 3a10209

super_darwin_test.go RawHistoryFolder

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2026 The Ebitengine Authors
3
4package objc_test
5
6import (
7	"fmt"
8	"os/exec"
9	"path/filepath"
10	"runtime"
11	"structs"
12	"sync/atomic"
13	"testing"
14
15	"github.com/bnema/purego"
16	"github.com/bnema/purego/objc"
17)
18
19var superTestClassID atomic.Uint64
20
21// TestSendSuperDispatch checks argument and receiver forwarding, return values,
22// and generic super calls from direct and inherited methods.
23func TestSendSuperDispatch(t *testing.T) {
24	if _, err := purego.Dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", purego.RTLD_GLOBAL|purego.RTLD_NOW); err != nil {
25		t.Fatal(err)
26	}
27	prefix := fmt.Sprintf("PuregoSuperTest%d", superTestClassID.Add(1))
28	sel := objc.RegisterName("probe:")
29	var baseCalls, child1Calls int
30	var receiver objc.ID
31	base, err := objc.RegisterClass(prefix+"Base", objc.GetClass("NSObject"), nil, nil, []objc.MethodDef{
32		{
33			Cmd: sel,
34			Fn: func(self objc.ID, cmd objc.SEL, value int) int {
35				baseCalls++
36				receiver = self
37				return value + 3
38			},
39		},
40	})
41	if err != nil {
42		t.Fatal(err)
43	}
44	var child1 objc.Class
45	child1, err = objc.RegisterClass(prefix+"Child1", base, nil, nil, []objc.MethodDef{
46		{
47			Cmd: sel,
48			Fn: func(self objc.ID, cmd objc.SEL, value int) int {
49				child1Calls++
50				// Bound recursion so an incorrect class anchor fails without overflowing the stack.
51				if child1Calls > 1 {
52					return -1
53				}
54
55				return objc.SendSuper2[int](self, child1, cmd, value) + 5
56			},
57		},
58	})
59	if err != nil {
60		t.Fatal(err)
61	}
62	child2, err := objc.RegisterClass(prefix+"Child2", child1, nil, nil, nil)
63	if err != nil {
64		t.Fatal(err)
65	}
66	for _, class := range []objc.Class{child1, child2} {
67		baseCalls, child1Calls, receiver = 0, 0, 0
68		object := objc.ID(class).Send(objc.RegisterName("new"))
69		defer object.Send(objc.RegisterName("release"))
70		if got := objc.Send[int](object, sel, 34); got != 42 {
71			t.Errorf("class %v: result = %d, want 42", class, got)
72		}
73		if baseCalls != 1 || child1Calls != 1 {
74			t.Errorf("class %v: base calls = %d, child1 calls = %d; want 1 each", class, baseCalls, child1Calls)
75		}
76		if receiver != object {
77			t.Errorf("base receiver = %v, want %v", receiver, object)
78		}
79		// Check forwarding through the ID method too.
80		receiver = 0
81		if got := object.SendSuper2(child1, sel, 34); got != 37 {
82			t.Errorf("class %v: SendSuper2 = %d, want 37", class, got)
83		}
84		if receiver != object {
85			t.Errorf("SendSuper2 receiver = %v, want %v", receiver, object)
86		}
87	}
88	// The legacy helpers retain their direct-instance behavior.
89	object := objc.ID(child1).Send(objc.RegisterName("new"))
90	defer object.Send(objc.RegisterName("release"))
91	if got := object.SendSuper(sel, 34); got != 37 {
92		t.Errorf("SendSuper = %d, want 37", got)
93	}
94	if got := objc.SendSuper[int](object, sel, 34); got != 37 {
95		t.Errorf("SendSuper[int] = %d, want 37", got)
96	}
97}
98
99func TestSendSuperStruct(t *testing.T) {
100	arch := "arm64"
101	if runtime.GOARCH == "amd64" {
102		arch = "x86_64"
103	}
104	library := filepath.Join(t.TempDir(), "super.dylib")
105	cmd := exec.Command("clang", "-dynamiclib", "-arch", arch, "-framework", "Foundation", "-o", library, "testdata/super.m")
106	if out, err := cmd.CombinedOutput(); err != nil {
107		t.Fatalf("compile super fixture: %v\n%s", err, out)
108	}
109	// Objective-C retains the registered classes and their implementations.
110	if _, err := purego.Dlopen(library, purego.RTLD_GLOBAL|purego.RTLD_NOW); err != nil {
111		t.Fatal(err)
112	}
113	type result struct {
114		_       structs.HostLayout
115		A, B, C int64
116	}
117	want := result{
118		A: 12,
119		B: 34,
120		C: 56,
121	}
122	child1 := objc.GetClass("PuregoSuperStructChild1")
123	for _, class := range []objc.Class{child1, objc.GetClass("PuregoSuperStructChild2")} {
124		object := objc.ID(class).Send(objc.RegisterName("new"))
125		defer object.Send(objc.RegisterName("release"))
126		if got := objc.SendSuper2[result](object, child1, objc.RegisterName("result")); got != want {
127			t.Errorf("class %v: result = %+v, want %+v", class, got, want)
128		}
129	}
130}

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