Gen: Add methods for records
- commit
- dfc009c4291560ff6166c2bd47614c983f16a196
- author
- Peter Fern · · 2023-12-31 04:36 UTC
- committer
- Peter Fern · · 2024-01-03 01:46 UTC
- parent
- 8fc4929
- signature
- unsigned
4 files · +49 -35
M internal/gir/pass/pass.go +23 -1
| @@ -97,7 +97,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 97 | 97 | for _, rec := range ns.Records { | |
| 98 | 98 | name := util.SnakeToCamel(rec.Name) | |
| 99 | 99 | constructors := make([]types.FuncTemplate, len(rec.Constructors)) | |
| 100 | - | fields := make([]types.RecordField, len(rec.Fields)) | |
| 100 | + | receivers := make([]types.FuncTemplate, 0, len(rec.Methods)) | |
| 101 | + | fields := make([]types.RecordField, 0, len(rec.Fields)) | |
| 101 | 102 | fn := rec.FilenameSafe() | |
| 102 | 103 | files = append(files, fn) | |
| 103 | 104 | for i, c := range rec.Constructors { | |
| @@ -131,10 +132,30 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 131 | 132 | Type: _type, | |
| 132 | 133 | }) | |
| 133 | 134 | } | |
| 135 | + | for _, f := range rec.Methods { | |
| 136 | + | name := util.SnakeToCamel(f.Name) | |
| 137 | + | if name == "" { | |
| 138 | + | name = util.SnakeToCamel(f.CIdentifier) | |
| 139 | + | } | |
| 140 | + | for _, f := range fields { | |
| 141 | + | if f.Name == name { | |
| 142 | + | name = name + "Fn" | |
| 143 | + | break | |
| 144 | + | } | |
| 145 | + | } | |
| 146 | + | receivers = append(receivers, types.FuncTemplate{ | |
| 147 | + | Doc: f.Doc.StringSafe(), | |
| 148 | + | Name: name, | |
| 149 | + | CName: f.CIdentifier, | |
| 150 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |
| 151 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |
| 152 | + | }) | |
| 153 | + | } | |
| 134 | 154 | records[fn] = append(records[fn], types.RecordTemplate{ | |
| 135 | 155 | Name: name, | |
| 136 | 156 | Doc: rec.Doc.StringSafe(), | |
| 137 | 157 | Constructors: constructors, | |
| 158 | + | Receivers: receivers, | |
| 138 | 159 | Fields: fields, | |
| 139 | 160 | }) | |
| 140 | 161 | recordLookup[name] = true | |
| @@ -291,6 +312,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 291 | 312 | } | |
| 292 | 313 | for _, i := range records[fn] { | |
| 293 | 314 | methods += len(i.Constructors) | |
| 315 | + | methods += len(i.Receivers) | |
| 294 | 316 | } | |
| 295 | 317 | for _, i := range classes[fn] { | |
| 296 | 318 | methods += len(i.Constructors) | |
| @@ -97,7 +97,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 97 | for _, rec := range ns.Records { | 97 | for _, rec := range ns.Records { | ||
| 98 | name := util.SnakeToCamel(rec.Name) | 98 | name := util.SnakeToCamel(rec.Name) | ||
| 99 | constructors := make([]types.FuncTemplate, len(rec.Constructors)) | 99 | constructors := make([]types.FuncTemplate, len(rec.Constructors)) | ||
| 100 | - | fields := make([]types.RecordField, len(rec.Fields)) | 100 | + | receivers := make([]types.FuncTemplate, 0, len(rec.Methods)) |
| 101 | + | fields := make([]types.RecordField, 0, len(rec.Fields)) | |||
| 101 | fn := rec.FilenameSafe() | 102 | fn := rec.FilenameSafe() | ||
| 102 | files = append(files, fn) | 103 | files = append(files, fn) | ||
| 103 | for i, c := range rec.Constructors { | 104 | for i, c := range rec.Constructors { | ||
| @@ -131,10 +132,30 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 131 | Type: _type, | 132 | Type: _type, | ||
| 132 | }) | 133 | }) | ||
| 133 | } | 134 | } | ||
| 135 | + | for _, f := range rec.Methods { | |||
| 136 | + | name := util.SnakeToCamel(f.Name) | |||
| 137 | + | if name == "" { | |||
| 138 | + | name = util.SnakeToCamel(f.CIdentifier) | |||
| 139 | + | } | |||
| 140 | + | for _, f := range fields { | |||
| 141 | + | if f.Name == name { | |||
| 142 | + | name = name + "Fn" | |||
| 143 | + | break | |||
| 144 | + | } | |||
| 145 | + | } | |||
| 146 | + | receivers = append(receivers, types.FuncTemplate{ | |||
| 147 | + | Doc: f.Doc.StringSafe(), | |||
| 148 | + | Name: name, | |||
| 149 | + | CName: f.CIdentifier, | |||
| 150 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |||
| 151 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |||
| 152 | + | }) | |||
| 153 | + | } | |||
| 134 | records[fn] = append(records[fn], types.RecordTemplate{ | 154 | records[fn] = append(records[fn], types.RecordTemplate{ | ||
| 135 | Name: name, | 155 | Name: name, | ||
| 136 | Doc: rec.Doc.StringSafe(), | 156 | Doc: rec.Doc.StringSafe(), | ||
| 137 | Constructors: constructors, | 157 | Constructors: constructors, | ||
| 158 | + | Receivers: receivers, | |||
| 138 | Fields: fields, | 159 | Fields: fields, | ||
| 139 | }) | 160 | }) | ||
| 140 | recordLookup[name] = true | 161 | recordLookup[name] = true | ||
| @@ -291,6 +312,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 291 | } | 312 | } | ||
| 292 | for _, i := range records[fn] { | 313 | for _, i := range records[fn] { | ||
| 293 | methods += len(i.Constructors) | 314 | methods += len(i.Constructors) | ||
| 315 | + | methods += len(i.Receivers) | |||
| 294 | } | 316 | } | ||
| 295 | for _, i := range classes[fn] { | 317 | for _, i := range classes[fn] { | ||
| 296 | methods += len(i.Constructors) | 318 | methods += len(i.Constructors) | ||
M internal/gir/types/template.go +3 -0
| @@ -177,6 +177,9 @@ type RecordTemplate struct { | |||
| 177 | 177 | // Constructors is the slice of functions that create the class struct | |
| 178 | 178 | Constructors []FuncTemplate | |
| 179 | 179 | ||
| 180 | + | // Receivers is the slice of functions that have value receivers to the struct | |
| 181 | + | Receivers []FuncTemplate | |
| 182 | + | ||
| 180 | 183 | // Fields is the list of record fields | |
| 181 | 184 | Fields []RecordField | |
| 182 | 185 | } | |
| @@ -177,6 +177,9 @@ type RecordTemplate struct { | |||||
| 177 | // Constructors is the slice of functions that create the class struct | 177 | // Constructors is the slice of functions that create the class struct | ||
| 178 | Constructors []FuncTemplate | 178 | Constructors []FuncTemplate | ||
| 179 | 179 | ||||
| 180 | + | // Receivers is the slice of functions that have value receivers to the struct | |||
| 181 | + | Receivers []FuncTemplate | |||
| 182 | + | ||||
| 180 | // Fields is the list of record fields | 183 | // Fields is the list of record fields | ||
| 181 | Fields []RecordField | 184 | Fields []RecordField | ||
| 182 | } | 185 | } | ||
M templates/go +21 -2
| @@ -31,6 +31,10 @@ type {{.Name}} struct { | |||
| 31 | 31 | {{end}} | |
| 32 | 32 | } | |
| 33 | 33 | ||
| 34 | + | func (x *{{.Name}}) GoPointer() uintptr { | |
| 35 | + | return uintptr(unsafe.Pointer(x)) | |
| 36 | + | } | |
| 37 | + | ||
| 34 | 38 | {{$outer := .}} | |
| 35 | 39 | ||
| 36 | 40 | {{range .Constructors -}} | |
| @@ -45,6 +49,18 @@ func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 45 | 49 | } | |
| 46 | 50 | {{end}} | |
| 47 | 51 | ||
| 52 | + | {{$outer := .}} | |
| 53 | + | {{range .Receivers -}} | |
| 54 | + | var x{{$outer.Name}}{{.Name}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 55 | + | ||
| 56 | + | {{.Doc}} | |
| 57 | + | func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 58 | + | {{.Ret.Preamble $NotGLib}} | |
| 59 | + | {{if .Ret.Value}}cret :={{end}}x{{$outer.Name}}{{.Name}}(x.GoPointer() {{convc .Args.API.Call}}) | |
| 60 | + | {{.Ret.Fmt $NotGObject}} | |
| 61 | + | } | |
| 62 | + | {{end}} | |
| 63 | + | ||
| 48 | 64 | {{end}} | |
| 49 | 65 | ||
| 50 | 66 | {{range .Interfaces -}} | |
| @@ -216,9 +232,12 @@ func init() { | |||
| 216 | 232 | {{end}} | |
| 217 | 233 | ||
| 218 | 234 | {{range .Records -}} | |
| 219 | - | {{$cls := .}} | |
| 235 | + | {{$rec := .}} | |
| 220 | 236 | {{range .Constructors -}} | |
| 221 | - | core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}") | |
| 237 | + | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") | |
| 238 | + | {{end}} | |
| 239 | + | {{range .Receivers -}} | |
| 240 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | |
| 222 | 241 | {{end}} | |
| 223 | 242 | {{end}} | |
| 224 | 243 | ||
| @@ -31,6 +31,10 @@ type {{.Name}} struct { | |||||
| 31 | {{end}} | 31 | {{end}} | ||
| 32 | } | 32 | } | ||
| 33 | 33 | ||||
| 34 | + | func (x *{{.Name}}) GoPointer() uintptr { | |||
| 35 | + | return uintptr(unsafe.Pointer(x)) | |||
| 36 | + | } | |||
| 37 | + | ||||
| 34 | {{$outer := .}} | 38 | {{$outer := .}} | ||
| 35 | 39 | ||||
| 36 | {{range .Constructors -}} | 40 | {{range .Constructors -}} | ||
| @@ -45,6 +49,18 @@ func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||||
| 45 | } | 49 | } | ||
| 46 | {{end}} | 50 | {{end}} | ||
| 47 | 51 | ||||
| 52 | + | {{$outer := .}} | |||
| 53 | + | {{range .Receivers -}} | |||
| 54 | + | var x{{$outer.Name}}{{.Name}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret.Raw}} | |||
| 55 | + | ||||
| 56 | + | {{.Doc}} | |||
| 57 | + | func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 58 | + | {{.Ret.Preamble $NotGLib}} | |||
| 59 | + | {{if .Ret.Value}}cret :={{end}}x{{$outer.Name}}{{.Name}}(x.GoPointer() {{convc .Args.API.Call}}) | |||
| 60 | + | {{.Ret.Fmt $NotGObject}} | |||
| 61 | + | } | |||
| 62 | + | {{end}} | |||
| 63 | + | ||||
| 48 | {{end}} | 64 | {{end}} | ||
| 49 | 65 | ||||
| 50 | {{range .Interfaces -}} | 66 | {{range .Interfaces -}} | ||
| @@ -216,9 +232,12 @@ func init() { | |||||
| 216 | {{end}} | 232 | {{end}} | ||
| 217 | 233 | ||||
| 218 | {{range .Records -}} | 234 | {{range .Records -}} | ||
| 219 | - | {{$cls := .}} | 235 | + | {{$rec := .}} |
| 220 | {{range .Constructors -}} | 236 | {{range .Constructors -}} | ||
| 221 | - | core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}") | 237 | + | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") |
| 238 | + | {{end}} | |||
| 239 | + | {{range .Receivers -}} | |||
| 240 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | |||
| 222 | {{end}} | 241 | {{end}} | ||
| 223 | {{end}} | 242 | {{end}} | ||
| 224 | 243 | ||||
M templates/gobject +2 -32
| @@ -1,11 +1,9 @@ | |||
| 1 | 1 | package gobject | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | - | "os" | |
| 5 | 4 | "reflect" | |
| 6 | 5 | ||
| 7 | 6 | "github.com/jwijenbergh/purego" | |
| 8 | - | "github.com/jwijenbergh/puregotk/internal/core" | |
| 9 | 7 | ) | |
| 10 | 8 | ||
| 11 | 9 | type Ptr interface { | |
| @@ -39,11 +37,11 @@ func (o Object) Cast(v Ptr) { | |||
| 39 | 37 | } | |
| 40 | 38 | ||
| 41 | 39 | func (o Object) ConnectSignal(signal string, cb func()) uint32 { | |
| 42 | - | return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb)) | |
| 40 | + | return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb)) | |
| 43 | 41 | } | |
| 44 | 42 | ||
| 45 | 43 | func (o Object) DisconnectSignal(handler uint32) { | |
| 46 | - | SignalHandlerDisconnect(&o, handler) | |
| 44 | + | SignalHandlerDisconnect(&o, handler) | |
| 47 | 45 | } | |
| 48 | 46 | ||
| 49 | 47 | // NewCallback is an alias to purego.NewCallback | |
| @@ -79,31 +77,3 @@ const ( | |||
| 79 | 77 | TypeReservedBseLastVal = 48 << 2 | |
| 80 | 78 | TypeReservedUserFirstVal = 49 << 2 | |
| 81 | 79 | ) | |
| 82 | - | ||
| 83 | - | var xValueGetString func(*Value) string | |
| 84 | - | ||
| 85 | - | func (v *Value) String() string { | |
| 86 | - | return xValueGetString(v) | |
| 87 | - | } | |
| 88 | - | ||
| 89 | - | var xValueGetInt func(*Value) int | |
| 90 | - | ||
| 91 | - | func (v *Value) Int() int { | |
| 92 | - | return xValueGetInt(v) | |
| 93 | - | } | |
| 94 | - | ||
| 95 | - | var xValueUnset func(*Value) | |
| 96 | - | ||
| 97 | - | func (x *Value) Unset() { | |
| 98 | - | xValueUnset(x) | |
| 99 | - | } | |
| 100 | - | ||
| 101 | - | func init() { | |
| 102 | - | lib, err := purego.Dlopen(os.Getenv("PUREGOTK_GTK_PATH"), purego.RTLD_NOW|purego.RTLD_GLOBAL) | |
| 103 | - | if err != nil { | |
| 104 | - | panic(err) | |
| 105 | - | } | |
| 106 | - | core.PuregoSafeRegister(&xValueGetString, lib, "g_value_get_string") | |
| 107 | - | core.PuregoSafeRegister(&xValueGetInt, lib, "g_value_get_int") | |
| 108 | - | core.PuregoSafeRegister(&xValueUnset, lib, "g_value_unset") | |
| 109 | - | } | |
| @@ -1,11 +1,9 @@ | |||||
| 1 | package gobject | 1 | package gobject | ||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | - | "os" | |||
| 5 | "reflect" | 4 | "reflect" | ||
| 6 | 5 | ||||
| 7 | "github.com/jwijenbergh/purego" | 6 | "github.com/jwijenbergh/purego" | ||
| 8 | - | "github.com/jwijenbergh/puregotk/internal/core" | |||
| 9 | ) | 7 | ) | ||
| 10 | 8 | ||||
| 11 | type Ptr interface { | 9 | type Ptr interface { | ||
| @@ -39,11 +37,11 @@ func (o Object) Cast(v Ptr) { | |||||
| 39 | } | 37 | } | ||
| 40 | 38 | ||||
| 41 | func (o Object) ConnectSignal(signal string, cb func()) uint32 { | 39 | func (o Object) ConnectSignal(signal string, cb func()) uint32 { | ||
| 42 | - | return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb)) | 40 | + | return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb)) |
| 43 | } | 41 | } | ||
| 44 | 42 | ||||
| 45 | func (o Object) DisconnectSignal(handler uint32) { | 43 | func (o Object) DisconnectSignal(handler uint32) { | ||
| 46 | - | SignalHandlerDisconnect(&o, handler) | 44 | + | SignalHandlerDisconnect(&o, handler) |
| 47 | } | 45 | } | ||
| 48 | 46 | ||||
| 49 | // NewCallback is an alias to purego.NewCallback | 47 | // NewCallback is an alias to purego.NewCallback | ||
| @@ -79,31 +77,3 @@ const ( | |||||
| 79 | TypeReservedBseLastVal = 48 << 2 | 77 | TypeReservedBseLastVal = 48 << 2 | ||
| 80 | TypeReservedUserFirstVal = 49 << 2 | 78 | TypeReservedUserFirstVal = 49 << 2 | ||
| 81 | ) | 79 | ) | ||
| 82 | - | ||||
| 83 | - | var xValueGetString func(*Value) string | |||
| 84 | - | ||||
| 85 | - | func (v *Value) String() string { | |||
| 86 | - | return xValueGetString(v) | |||
| 87 | - | } | |||
| 88 | - | ||||
| 89 | - | var xValueGetInt func(*Value) int | |||
| 90 | - | ||||
| 91 | - | func (v *Value) Int() int { | |||
| 92 | - | return xValueGetInt(v) | |||
| 93 | - | } | |||
| 94 | - | ||||
| 95 | - | var xValueUnset func(*Value) | |||
| 96 | - | ||||
| 97 | - | func (x *Value) Unset() { | |||
| 98 | - | xValueUnset(x) | |||
| 99 | - | } | |||
| 100 | - | ||||
| 101 | - | func init() { | |||
| 102 | - | lib, err := purego.Dlopen(os.Getenv("PUREGOTK_GTK_PATH"), purego.RTLD_NOW|purego.RTLD_GLOBAL) | |||
| 103 | - | if err != nil { | |||
| 104 | - | panic(err) | |||
| 105 | - | } | |||
| 106 | - | core.PuregoSafeRegister(&xValueGetString, lib, "g_value_get_string") | |||
| 107 | - | core.PuregoSafeRegister(&xValueGetInt, lib, "g_value_get_int") | |||
| 108 | - | core.PuregoSafeRegister(&xValueUnset, lib, "g_value_unset") | |||
| 109 | - | } | |||