detached f69b87f

fix(scanner): construct mapped core children through public factories

commit
f69b87f26558a01f960906accbba208adb1159b1
author
brice · · 2026-09-28 07:26 UTC
parent
c47d0a5
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
3 files · +46 -3
M internal/scanner/scanner.go +14 -3
@@ -194,6 +194,7 @@ type requestData struct {
194194 CreatesChild bool
195195 ChildVar string
196196 ChildType string
197+ ChildFactory string
197198 ErrorReturn string
198199 FDArgs []string
199200 HasFDs bool
@@ -470,6 +471,9 @@ func (s *Scanner) processRequest(iface Interface, req Request, opcode int) (requ
470471 }
471472 data.CreatesChild = true
472473 data.ChildType = childType
474+ if strings.Contains(childType, ".") {
475+ data.ChildFactory = childType[:strings.LastIndex(childType, ".")+1] + "New" + s.toGoName(newIDArg.Interface)
476+ }
473477 }
474478
475479 for _, slot := range argSlots {
@@ -526,10 +530,17 @@ func (s *Scanner) processEvent(event Event, opcode int) (eventData, error) {
526530 return data, fmt.Errorf("%s.%s: %w", event.Name, arg.Name, err)
527531 }
528532 child := name + "Object"
533+ data.DecodeLines = append(data.DecodeLines, fmt.Sprintf("%sID := event.Uint32()", name))
534+ if strings.Contains(childType, ".") {
535+ factory := childType[:strings.LastIndex(childType, ".")+1] + "New" + s.toGoName(arg.Interface)
536+ data.DecodeLines = append(data.DecodeLines, fmt.Sprintf("%s := %s(o.Context())", child, factory))
537+ } else {
538+ data.DecodeLines = append(data.DecodeLines,
539+ fmt.Sprintf("%s := &%s{}", child, childType),
540+ fmt.Sprintf("%s.SetContext(o.Context())", child),
541+ )
542+ }
529543 data.DecodeLines = append(data.DecodeLines,
530- fmt.Sprintf("%sID := event.Uint32()", name),
531- fmt.Sprintf("%s := &%s{}", child, childType),
532- fmt.Sprintf("%s.SetContext(o.Context())", child),
533544 fmt.Sprintf("%s.SetID(%sID)", child, name),
534545 fmt.Sprintf("o.Context().Register(%s)", child),
535546 )
@@ -194,6 +194,7 @@ type requestData struct {
194 CreatesChild bool194 CreatesChild bool
195 ChildVar string195 ChildVar string
196 ChildType string196 ChildType string
197+ ChildFactory string
197 ErrorReturn string198 ErrorReturn string
198 FDArgs []string199 FDArgs []string
199 HasFDs bool200 HasFDs bool
@@ -470,6 +471,9 @@ func (s *Scanner) processRequest(iface Interface, req Request, opcode int) (requ
470 }471 }
471 data.CreatesChild = true472 data.CreatesChild = true
472 data.ChildType = childType473 data.ChildType = childType
474+ if strings.Contains(childType, ".") {
475+ data.ChildFactory = childType[:strings.LastIndex(childType, ".")+1] + "New" + s.toGoName(newIDArg.Interface)
476+ }
473 }477 }
474 478
475 for _, slot := range argSlots {479 for _, slot := range argSlots {
@@ -526,10 +530,17 @@ func (s *Scanner) processEvent(event Event, opcode int) (eventData, error) {
526 return data, fmt.Errorf("%s.%s: %w", event.Name, arg.Name, err)530 return data, fmt.Errorf("%s.%s: %w", event.Name, arg.Name, err)
527 }531 }
528 child := name + "Object"532 child := name + "Object"
533+ data.DecodeLines = append(data.DecodeLines, fmt.Sprintf("%sID := event.Uint32()", name))
534+ if strings.Contains(childType, ".") {
535+ factory := childType[:strings.LastIndex(childType, ".")+1] + "New" + s.toGoName(arg.Interface)
536+ data.DecodeLines = append(data.DecodeLines, fmt.Sprintf("%s := %s(o.Context())", child, factory))
537+ } else {
538+ data.DecodeLines = append(data.DecodeLines,
539+ fmt.Sprintf("%s := &%s{}", child, childType),
540+ fmt.Sprintf("%s.SetContext(o.Context())", child),
541+ )
542+ }
529 data.DecodeLines = append(data.DecodeLines,543 data.DecodeLines = append(data.DecodeLines,
530- fmt.Sprintf("%sID := event.Uint32()", name),
531- fmt.Sprintf("%s := &%s{}", child, childType),
532- fmt.Sprintf("%s.SetContext(o.Context())", child),
533 fmt.Sprintf("%s.SetID(%sID)", child, name),544 fmt.Sprintf("%s.SetID(%sID)", child, name),
534 fmt.Sprintf("o.Context().Register(%s)", child),545 fmt.Sprintf("o.Context().Register(%s)", child),
535 )546 )
M internal/scanner/scanner_test.go +28 -0
@@ -45,3 +45,31 @@ func TestCoreGeneration(t *testing.T) {
4545 t.Fatal("bootstrap generated twice")
4646 }
4747 }
48+
49+// P3 uses wl_buffer as both an event-created child and a request-created child.
50+// Verify the generated code uses the public constructor instead of embedding
51+// an inaccessible BaseProxy context field across package boundaries.
52+func TestExternalChildFactory(t *testing.T) {
53+ path := filepath.Join(t.TempDir(), "external.xml")
54+ xml := `<protocol name="fixture"><interface name="ext_params" version="1"><request name="create"><arg name="id" type="new_id" interface="wl_buffer"/></request><event name="created"><arg name="id" type="new_id" interface="wl_buffer"/></event></interface></protocol>`
55+ if err := os.WriteFile(path, []byte(xml), 0600); err != nil {
56+ t.Fatal(err)
57+ }
58+ s := NewScanner()
59+ if err := s.ParseXML(path); err != nil {
60+ t.Fatal(err)
61+ }
62+ s.CrossPackage = map[string]string{"wl_buffer": "github.com/bnema/wlturbo/protocol/core"}
63+ generated, err := s.Generate("ext")
64+ if err != nil {
65+ t.Fatal(err)
66+ }
67+ for _, part := range []string{"cross_wl_buffer.NewBuffer(o.Context())", "(*cross_wl_buffer.Buffer, error)", "idObject.SetID(idID)"} {
68+ if !strings.Contains(string(generated), part) {
69+ t.Errorf("generated code missing %q", part)
70+ }
71+ }
72+ if strings.Contains(string(generated), "&cross_wl_buffer.Buffer{}") {
73+ t.Fatal("foreign child was constructed without its package factory")
74+ }
75+}
@@ -45,3 +45,31 @@ func TestCoreGeneration(t *testing.T) {
45 t.Fatal("bootstrap generated twice")45 t.Fatal("bootstrap generated twice")
46 }46 }
47 }47 }
48+
49+// P3 uses wl_buffer as both an event-created child and a request-created child.
50+// Verify the generated code uses the public constructor instead of embedding
51+// an inaccessible BaseProxy context field across package boundaries.
52+func TestExternalChildFactory(t *testing.T) {
53+ path := filepath.Join(t.TempDir(), "external.xml")
54+ xml := `<protocol name="fixture"><interface name="ext_params" version="1"><request name="create"><arg name="id" type="new_id" interface="wl_buffer"/></request><event name="created"><arg name="id" type="new_id" interface="wl_buffer"/></event></interface></protocol>`
55+ if err := os.WriteFile(path, []byte(xml), 0600); err != nil {
56+ t.Fatal(err)
57+ }
58+ s := NewScanner()
59+ if err := s.ParseXML(path); err != nil {
60+ t.Fatal(err)
61+ }
62+ s.CrossPackage = map[string]string{"wl_buffer": "github.com/bnema/wlturbo/protocol/core"}
63+ generated, err := s.Generate("ext")
64+ if err != nil {
65+ t.Fatal(err)
66+ }
67+ for _, part := range []string{"cross_wl_buffer.NewBuffer(o.Context())", "(*cross_wl_buffer.Buffer, error)", "idObject.SetID(idID)"} {
68+ if !strings.Contains(string(generated), part) {
69+ t.Errorf("generated code missing %q", part)
70+ }
71+ }
72+ if strings.Contains(string(generated), "&cross_wl_buffer.Buffer{}") {
73+ t.Fatal("foreign child was constructed without its package factory")
74+ }
75+}
M internal/scanner/template.go +4 -0
@@ -54,8 +54,12 @@ func (o *{{$iface.GoName}}) {{.GoName}}({{.Params}}) {{.Results}} {
5454 {{.}}
5555 {{- end}}
5656 {{- if .CreatesChild}}
57+{{- if .ChildFactory}}
58+ {{.ChildVar}} := {{.ChildFactory}}(o.Context())
59+{{- else}}
5760 {{.ChildVar}} := &{{.ChildType}}{}
5861 {{.ChildVar}}.SetContext(o.Context())
62+{{- end}}
5963 {{.ChildVar}}.SetID(o.Context().AllocateID())
6064 o.Context().Register({{.ChildVar}})
6165 {{- end}}
@@ -54,8 +54,12 @@ func (o *{{$iface.GoName}}) {{.GoName}}({{.Params}}) {{.Results}} {
54 {{.}}54 {{.}}
55 {{- end}}55 {{- end}}
56 {{- if .CreatesChild}}56 {{- if .CreatesChild}}
57+{{- if .ChildFactory}}
58+ {{.ChildVar}} := {{.ChildFactory}}(o.Context())
59+{{- else}}
57 {{.ChildVar}} := &{{.ChildType}}{}60 {{.ChildVar}} := &{{.ChildType}}{}
58 {{.ChildVar}}.SetContext(o.Context())61 {{.ChildVar}}.SetContext(o.Context())
62+{{- end}}
59 {{.ChildVar}}.SetID(o.Context().AllocateID())63 {{.ChildVar}}.SetID(o.Context().AllocateID())
60 o.Context().Register({{.ChildVar}})64 o.Context().Register({{.ChildVar}})
61 {{- end}}65 {{- end}}

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 8

↑↓ moveenter openesc close