Skip to content

Commit 7bedf2c

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
feat: auto-infer typed enum params from int32 in idiomgen
C headers declare enum params as bare `int`, so the spec records `int32`. This made examples require ugly casts like `mgr.DefaultSensor(int32(sensor.Accelerometer))`. Fix: after collecting value enums, build a set of their Go names. When a function param has type int32 and PascalCase(paramName) matches a value enum GoName, use the typed enum instead. Also supports explicit overlay override via `param_types` field on FuncOverlay. Affects 9 params across 7 modules (sensor, asset, config, bitmap, logging, nativewindow, neuralnetworks). Also fixes capiArg to not prefix scalar Go types with `capi.` (e.g. int32 → int32(), not capi.int32()). Examples updated to remove int32() casts.
1 parent 91c6e3d commit 7bedf2c

13 files changed

Lines changed: 104 additions & 33 deletions

File tree

asset/manager.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/config.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/asset/overview/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func main() {
108108

109109
if mgr != nil {
110110
// Open a text file from the APK's assets/ directory.
111-
a := mgr.Open("data/config.json", int32(asset.Streaming))
111+
a := mgr.Open("data/config.json", asset.Streaming)
112112
defer a.Close()
113113

114114
// Query total size. Length returns off_t (32-bit on older ABIs),
@@ -141,7 +141,7 @@ func main() {
141141
// With Buffer mode, the entire asset is accessible through a
142142
// direct pointer without calling Read:
143143
//
144-
// a := mgr.Open("data/config.json", int32(asset.Buffer))
144+
// a := mgr.Open("data/config.json", asset.Buffer)
145145
// ptr := a.Buffer() // unsafe.Pointer to the raw data
146146
// data := unsafe.Slice((*byte)(ptr), a.Length64())
147147
}

examples/sensor/accelerometer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
// device lacks the requested sensor type, the underlying C pointer
3737
// is NULL and any method call will crash. We call Name() first and
3838
// treat an empty result (or panic) as "sensor not available."
39-
accel := mgr.DefaultSensor(int32(sensor.Accelerometer))
39+
accel := mgr.DefaultSensor(sensor.Accelerometer)
4040

4141
name := accel.Name()
4242
if name == "" {

examples/sensor/event-loop/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
log.Println("sensor manager obtained")
5151

5252
// --- 2. Query default accelerometer ---
53-
accel := mgr.DefaultSensor(int32(sensor.Accelerometer))
53+
accel := mgr.DefaultSensor(sensor.Accelerometer)
5454
name := accel.Name()
5555
if name == "" {
5656
log.Fatal("no default accelerometer on this device")

examples/window/software-render/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ func main() {
124124
// switching pixel formats.
125125
//
126126
// // Set 720p resolution with RGBA8888 format.
127-
// if err := win.SetBuffersGeometry(1280, 720, int32(window.Rgba8888)); err != nil {
127+
// if err := win.SetBuffersGeometry(1280, 720, window.Rgba8888); err != nil {
128128
// log.Fatalf("set geometry: %v", err)
129129
// }
130130
//
131131
// // Or keep current dimensions and only change the format:
132-
// win.SetBuffersGeometry(0, 0, int32(window.Rgb565))
132+
// win.SetBuffersGeometry(0, 0, window.Rgb565)
133133

134134
fmt.Println("Step 3: Configure buffer geometry")
135-
fmt.Println(" win.SetBuffersGeometry(1280, 720, int32(window.Rgba8888))")
135+
fmt.Println(" win.SetBuffersGeometry(1280, 720, window.Rgba8888)")
136136
fmt.Println(" // Pass 0 for width/height to keep current dimensions.")
137137
fmt.Println()
138138

log/functions.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nnapi/compilation.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sensor/manager.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/pkg/idiomgen/funcmap.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func capiArg(p MergedParam) string {
5454
}
5555
return "(" + strings.Repeat("*", stars) + "capi." + base + ")(" + p.Name + ")"
5656
}
57+
// Scalar Go types (int32, uint32, etc.) don't need a capi. prefix —
58+
// they're built-in types, not capi package types.
59+
if isScalarGoType(p.CapiType) {
60+
return p.CapiType + "(" + p.Name + ")"
61+
}
5762
return "capi." + p.CapiType + "(" + p.Name + ")"
5863
}
5964
return p.Name
@@ -256,6 +261,9 @@ func FuncMap() template.FuncMap {
256261
if strings.HasPrefix(p.CapiType, "*") {
257262
return "(*capi." + p.CapiType[1:] + ")(" + p.Name + ")"
258263
}
264+
if isScalarGoType(p.CapiType) {
265+
return p.CapiType + "(" + p.Name + ")"
266+
}
259267
return "capi." + p.CapiType + "(" + p.Name + ")"
260268
}
261269
return p.Name
@@ -287,6 +295,9 @@ func FuncMap() template.FuncMap {
287295
if strings.HasPrefix(p.CapiType, "*") {
288296
return "(*capi." + p.CapiType[1:] + ")(" + p.Name + ")"
289297
}
298+
if isScalarGoType(p.CapiType) {
299+
return p.CapiType + "(" + p.Name + ")"
300+
}
290301
return "capi." + p.CapiType + "(" + p.Name + ")"
291302
}
292303
return p.Name

0 commit comments

Comments
 (0)