GIR: Move `tryRegister` back into core with `PuregoSafeRegister`
Signed-off-by: Felicitas Pojtinger <felicitas@pojtinger.com>
- commit
- e97e9fe5949103b3f93a37fab77a2d940701e0e7
- author
- Felicitas Pojtinger · · 2025-10-17 06:48 UTC
- committer
- Jeroen Wijenbergh · · 2025-10-22 07:42 UTC
- parent
- 5cfa967
- signature
- unsigned
3 files · +26 -33
M internal/core/core.go +12 -10
| @@ -15,13 +15,15 @@ import ( | |||
| 15 | 15 | "github.com/jwijenbergh/purego" | |
| 16 | 16 | ) | |
| 17 | 17 | ||
| 18 | - | func PuregoSafeRegister(fptr interface{}, handle uintptr, name string) error { | |
| 19 | - | sym, err := purego.Dlsym(handle, name) | |
| 20 | - | if err != nil { | |
| 21 | - | return err | |
| 18 | + | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | |
| 19 | + | for _, lib := range libs { | |
| 20 | + | sym, err := purego.Dlsym(lib, name) | |
| 21 | + | if err == nil { | |
| 22 | + | purego.RegisterFunc(fptr, sym) | |
| 23 | + | ||
| 24 | + | return | |
| 25 | + | } | |
| 22 | 26 | } | |
| 23 | - | purego.RegisterFunc(fptr, sym) | |
| 24 | - | return nil | |
| 25 | 27 | } | |
| 26 | 28 | ||
| 27 | 29 | // paths to where the shared object files should be located | |
| @@ -105,7 +107,7 @@ func findPkgConf(name string) string { | |||
| 105 | 107 | return "" | |
| 106 | 108 | } | |
| 107 | 109 | ||
| 108 | - | // GetPath gets a shared object file from a library name | |
| 110 | + | // getPath gets a shared object file from a library name | |
| 109 | 111 | // it does it in the following order | |
| 110 | 112 | // see if PUREGOTK_LIBNAME_PATH is set (full path to the lib) | |
| 111 | 113 | // - e.g. PUREGOTK_GTK_PATH | |
| @@ -115,7 +117,7 @@ func findPkgConf(name string) string { | |||
| 115 | 117 | // panic if failed | |
| 116 | 118 | // TODO: Hardcore a library shared object with linker -X flag | |
| 117 | 119 | // This is useful for packaging | |
| 118 | - | func GetPath(name string) string { | |
| 120 | + | func getPath(name string) string { | |
| 119 | 121 | // try to get from env var | |
| 120 | 122 | ev := fmt.Sprintf("PUREGOTK_%s_PATH", name) | |
| 121 | 123 | if v := os.Getenv(ev); v != "" { | |
| @@ -178,12 +180,12 @@ func GetPaths(name string) []string { | |||
| 178 | 180 | libNames, ok := names[name] | |
| 179 | 181 | if !ok || len(libNames) == 0 { | |
| 180 | 182 | // Fallback to single GetPath for backward compatibility | |
| 181 | - | return []string{GetPath(name)} | |
| 183 | + | return []string{getPath(name)} | |
| 182 | 184 | } | |
| 183 | 185 | ||
| 184 | 186 | // If only one library, use the original GetPath logic | |
| 185 | 187 | if len(libNames) == 1 { | |
| 186 | - | return []string{GetPath(name)} | |
| 188 | + | return []string{getPath(name)} | |
| 187 | 189 | } | |
| 188 | 190 | ||
| 189 | 191 | var result []string | |
| @@ -15,13 +15,15 @@ import ( | |||||
| 15 | "github.com/jwijenbergh/purego" | 15 | "github.com/jwijenbergh/purego" | ||
| 16 | ) | 16 | ) | ||
| 17 | 17 | ||||
| 18 | - | func PuregoSafeRegister(fptr interface{}, handle uintptr, name string) error { | 18 | + | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { |
| 19 | - | sym, err := purego.Dlsym(handle, name) | 19 | + | for _, lib := range libs { |
| 20 | - | if err != nil { | 20 | + | sym, err := purego.Dlsym(lib, name) |
| 21 | - | return err | 21 | + | if err == nil { |
| 22 | + | purego.RegisterFunc(fptr, sym) | |||
| 23 | + | ||||
| 24 | + | return | |||
| 25 | + | } | |||
| 22 | } | 26 | } | ||
| 23 | - | purego.RegisterFunc(fptr, sym) | |||
| 24 | - | return nil | |||
| 25 | } | 27 | } | ||
| 26 | 28 | ||||
| 27 | // paths to where the shared object files should be located | 29 | // paths to where the shared object files should be located | ||
| @@ -105,7 +107,7 @@ func findPkgConf(name string) string { | |||||
| 105 | return "" | 107 | return "" | ||
| 106 | } | 108 | } | ||
| 107 | 109 | ||||
| 108 | - | // GetPath gets a shared object file from a library name | 110 | + | // getPath gets a shared object file from a library name |
| 109 | // it does it in the following order | 111 | // it does it in the following order | ||
| 110 | // see if PUREGOTK_LIBNAME_PATH is set (full path to the lib) | 112 | // see if PUREGOTK_LIBNAME_PATH is set (full path to the lib) | ||
| 111 | // - e.g. PUREGOTK_GTK_PATH | 113 | // - e.g. PUREGOTK_GTK_PATH | ||
| @@ -115,7 +117,7 @@ func findPkgConf(name string) string { | |||||
| 115 | // panic if failed | 117 | // panic if failed | ||
| 116 | // TODO: Hardcore a library shared object with linker -X flag | 118 | // TODO: Hardcore a library shared object with linker -X flag | ||
| 117 | // This is useful for packaging | 119 | // This is useful for packaging | ||
| 118 | - | func GetPath(name string) string { | 120 | + | func getPath(name string) string { |
| 119 | // try to get from env var | 121 | // try to get from env var | ||
| 120 | ev := fmt.Sprintf("PUREGOTK_%s_PATH", name) | 122 | ev := fmt.Sprintf("PUREGOTK_%s_PATH", name) | ||
| 121 | if v := os.Getenv(ev); v != "" { | 123 | if v := os.Getenv(ev); v != "" { | ||
| @@ -178,12 +180,12 @@ func GetPaths(name string) []string { | |||||
| 178 | libNames, ok := names[name] | 180 | libNames, ok := names[name] | ||
| 179 | if !ok || len(libNames) == 0 { | 181 | if !ok || len(libNames) == 0 { | ||
| 180 | // Fallback to single GetPath for backward compatibility | 182 | // Fallback to single GetPath for backward compatibility | ||
| 181 | - | return []string{GetPath(name)} | 183 | + | return []string{getPath(name)} |
| 182 | } | 184 | } | ||
| 183 | 185 | ||||
| 184 | // If only one library, use the original GetPath logic | 186 | // If only one library, use the original GetPath logic | ||
| 185 | if len(libNames) == 1 { | 187 | if len(libNames) == 1 { | ||
| 186 | - | return []string{GetPath(name)} | 188 | + | return []string{getPath(name)} |
| 187 | } | 189 | } | ||
| 188 | 190 | ||||
| 189 | var result []string | 191 | var result []string | ||
M pkg/core/core.go +2 -3
| @@ -3,10 +3,9 @@ package core | |||
| 3 | 3 | import "github.com/jwijenbergh/puregotk/internal/core" | |
| 4 | 4 | ||
| 5 | 5 | var ( | |
| 6 | - | PuregoSafeRegister = core.PuregoSafeRegister | |
| 7 | - | GetPath = core.GetPath | |
| 8 | 6 | GetPaths = core.GetPaths | |
| 9 | 7 | GoString = core.GoString | |
| 10 | 8 | SetPackageName = core.SetPackageName | |
| 11 | - | SetSharedLibraries = core.SetSharedLibraries | |
| 9 | + | SetSharedLibraries = core.SetSharedLibraries | |
| 10 | + | PuregoSafeRegister = core.PuregoSafeRegister | |
| 12 | 11 | ) | |
| @@ -3,10 +3,9 @@ package core | |||||
| 3 | import "github.com/jwijenbergh/puregotk/internal/core" | 3 | import "github.com/jwijenbergh/puregotk/internal/core" | ||
| 4 | 4 | ||||
| 5 | var ( | 5 | var ( | ||
| 6 | - | PuregoSafeRegister = core.PuregoSafeRegister | |||
| 7 | - | GetPath = core.GetPath | |||
| 8 | GetPaths = core.GetPaths | 6 | GetPaths = core.GetPaths | ||
| 9 | GoString = core.GoString | 7 | GoString = core.GoString | ||
| 10 | SetPackageName = core.SetPackageName | 8 | SetPackageName = core.SetPackageName | ||
| 11 | - | SetSharedLibraries = core.SetSharedLibraries | 9 | + | SetSharedLibraries = core.SetSharedLibraries |
| 10 | + | PuregoSafeRegister = core.PuregoSafeRegister | |||
| 12 | ) | 11 | ) | ||
M templates/go +12 -20
| @@ -348,63 +348,55 @@ func init() { | |||
| 348 | 348 | libs = append(libs, lib) | |
| 349 | 349 | } | |
| 350 | 350 | ||
| 351 | - | tryRegister := func(fptr interface{}, name string) { | |
| 352 | - | for _, lib := range libs { | |
| 353 | - | if err := core.PuregoSafeRegister(fptr, lib, name); err == nil { | |
| 354 | - | return | |
| 355 | - | } | |
| 356 | - | } | |
| 357 | - | } | |
| 358 | - | ||
| 359 | 351 | {{range .Aliases -}} | |
| 360 | 352 | {{if .TypeGetter -}} | |
| 361 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | |
| 353 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") | |
| 362 | 354 | {{end}} | |
| 363 | 355 | {{end}} | |
| 364 | 356 | {{range .Enums -}} | |
| 365 | 357 | {{if .TypeGetter -}} | |
| 366 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | |
| 358 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") | |
| 367 | 359 | {{end}} | |
| 368 | 360 | {{end}} | |
| 369 | 361 | {{range .Functions -}} | |
| 370 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | |
| 362 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") | |
| 371 | 363 | {{end}} | |
| 372 | 364 | ||
| 373 | 365 | {{range .Records -}} | |
| 374 | 366 | {{$rec := .}} | |
| 375 | 367 | {{if .TypeGetter -}} | |
| 376 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | |
| 368 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") | |
| 377 | 369 | {{end}} | |
| 378 | 370 | {{range .Constructors -}} | |
| 379 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | |
| 371 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") | |
| 380 | 372 | {{end}} | |
| 381 | 373 | {{range .Receivers -}} | |
| 382 | - | tryRegister(&x{{$rec.Name}}{{.Name}}, "{{.CName}}") | |
| 374 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, libs, "{{.CName}}") | |
| 383 | 375 | {{end}} | |
| 384 | 376 | {{end}} | |
| 385 | 377 | ||
| 386 | 378 | {{range .Classes -}} | |
| 387 | 379 | {{$cls := .}} | |
| 388 | 380 | {{if .TypeGetter -}} | |
| 389 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | |
| 381 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") | |
| 390 | 382 | {{end}} | |
| 391 | 383 | {{range .Constructors -}} | |
| 392 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | |
| 384 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") | |
| 393 | 385 | {{end}} | |
| 394 | 386 | {{range .Receivers -}} | |
| 395 | - | tryRegister(&x{{$cls.Name}}{{.Name}}, "{{.CName}}") | |
| 387 | + | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, libs, "{{.CName}}") | |
| 396 | 388 | {{end}} | |
| 397 | 389 | {{range .Functions -}} | |
| 398 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | |
| 390 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") | |
| 399 | 391 | {{end}} | |
| 400 | 392 | {{end}} | |
| 401 | 393 | ||
| 402 | 394 | {{range .Interfaces -}} | |
| 403 | 395 | {{if .TypeGetter -}} | |
| 404 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | |
| 396 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") | |
| 405 | 397 | {{end}} | |
| 406 | 398 | {{range .Methods -}} | |
| 407 | - | tryRegister(&{{.Namespace}}X{{.FullName}}, "{{.CName}}") | |
| 399 | + | core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, libs, "{{.CName}}") | |
| 408 | 400 | {{end}} | |
| 409 | 401 | {{end}} | |
| 410 | 402 | } | |
| @@ -348,63 +348,55 @@ func init() { | |||||
| 348 | libs = append(libs, lib) | 348 | libs = append(libs, lib) | ||
| 349 | } | 349 | } | ||
| 350 | 350 | ||||
| 351 | - | tryRegister := func(fptr interface{}, name string) { | |||
| 352 | - | for _, lib := range libs { | |||
| 353 | - | if err := core.PuregoSafeRegister(fptr, lib, name); err == nil { | |||
| 354 | - | return | |||
| 355 | - | } | |||
| 356 | - | } | |||
| 357 | - | } | |||
| 358 | - | ||||
| 359 | {{range .Aliases -}} | 351 | {{range .Aliases -}} | ||
| 360 | {{if .TypeGetter -}} | 352 | {{if .TypeGetter -}} | ||
| 361 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | 353 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") |
| 362 | {{end}} | 354 | {{end}} | ||
| 363 | {{end}} | 355 | {{end}} | ||
| 364 | {{range .Enums -}} | 356 | {{range .Enums -}} | ||
| 365 | {{if .TypeGetter -}} | 357 | {{if .TypeGetter -}} | ||
| 366 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | 358 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") |
| 367 | {{end}} | 359 | {{end}} | ||
| 368 | {{end}} | 360 | {{end}} | ||
| 369 | {{range .Functions -}} | 361 | {{range .Functions -}} | ||
| 370 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | 362 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") |
| 371 | {{end}} | 363 | {{end}} | ||
| 372 | 364 | ||||
| 373 | {{range .Records -}} | 365 | {{range .Records -}} | ||
| 374 | {{$rec := .}} | 366 | {{$rec := .}} | ||
| 375 | {{if .TypeGetter -}} | 367 | {{if .TypeGetter -}} | ||
| 376 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | 368 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") |
| 377 | {{end}} | 369 | {{end}} | ||
| 378 | {{range .Constructors -}} | 370 | {{range .Constructors -}} | ||
| 379 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | 371 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") |
| 380 | {{end}} | 372 | {{end}} | ||
| 381 | {{range .Receivers -}} | 373 | {{range .Receivers -}} | ||
| 382 | - | tryRegister(&x{{$rec.Name}}{{.Name}}, "{{.CName}}") | 374 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, libs, "{{.CName}}") |
| 383 | {{end}} | 375 | {{end}} | ||
| 384 | {{end}} | 376 | {{end}} | ||
| 385 | 377 | ||||
| 386 | {{range .Classes -}} | 378 | {{range .Classes -}} | ||
| 387 | {{$cls := .}} | 379 | {{$cls := .}} | ||
| 388 | {{if .TypeGetter -}} | 380 | {{if .TypeGetter -}} | ||
| 389 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | 381 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") |
| 390 | {{end}} | 382 | {{end}} | ||
| 391 | {{range .Constructors -}} | 383 | {{range .Constructors -}} | ||
| 392 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | 384 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") |
| 393 | {{end}} | 385 | {{end}} | ||
| 394 | {{range .Receivers -}} | 386 | {{range .Receivers -}} | ||
| 395 | - | tryRegister(&x{{$cls.Name}}{{.Name}}, "{{.CName}}") | 387 | + | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, libs, "{{.CName}}") |
| 396 | {{end}} | 388 | {{end}} | ||
| 397 | {{range .Functions -}} | 389 | {{range .Functions -}} | ||
| 398 | - | tryRegister(&x{{.Name}}, "{{.CName}}") | 390 | + | core.PuregoSafeRegister(&x{{.Name}}, libs, "{{.CName}}") |
| 399 | {{end}} | 391 | {{end}} | ||
| 400 | {{end}} | 392 | {{end}} | ||
| 401 | 393 | ||||
| 402 | {{range .Interfaces -}} | 394 | {{range .Interfaces -}} | ||
| 403 | {{if .TypeGetter -}} | 395 | {{if .TypeGetter -}} | ||
| 404 | - | tryRegister(&x{{.Name}}GLibType, "{{.TypeGetter}}") | 396 | + | core.PuregoSafeRegister(&x{{.Name}}GLibType, libs, "{{.TypeGetter}}") |
| 405 | {{end}} | 397 | {{end}} | ||
| 406 | {{range .Methods -}} | 398 | {{range .Methods -}} | ||
| 407 | - | tryRegister(&{{.Namespace}}X{{.FullName}}, "{{.CName}}") | 399 | + | core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, libs, "{{.CName}}") |
| 408 | {{end}} | 400 | {{end}} | ||
| 409 | {{end}} | 401 | {{end}} | ||
| 410 | } | 402 | } | ||