Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates header with WGPUFuture and callback info structs #307

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
flag.StringVar(&yamlPath, "yaml", "", "path of the yaml spec")
flag.Parse()

arrays := []string{"constants", "typedefs", "enums", "bitflags", "function_types", "structs", "functions", "objects"}
arrays := []string{"constants", "typedefs", "enums", "bitflags", "structs", "callbacks", "functions", "objects"}
for _, array := range arrays {
SortArrayByFieldInPlace(array, "name")
}
Expand Down
49 changes: 34 additions & 15 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}};
{{- end}}
{{ end}}

{{- if .Callbacks}}
// Callback info structure forward declarations
{{- range .Callbacks}}
struct WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}};
{{- end}}
{{ end}}

/**
* \defgroup Enumerations
* \brief Enums.
Expand Down Expand Up @@ -181,28 +188,17 @@ typedef WGPUFlags WGPU{{$bitflag.Name | PascalCase}}Flags{{$.ExtSuffix}} WGPU_EN
typedef void (*WGPUProc)(void) WGPU_FUNCTION_ATTRIBUTE;
{{ end}}

{{- range .FunctionTypes}}
{{- MComment .Doc 0}}
typedef {{FunctionReturns .}} (*WGPU{{.Name | PascalCase}}{{$.ExtSuffix}})({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
{{- end}}

/**
* \defgroup Callbacks
* \brief Callbacks through which asynchronous functions return.
*
* @{
*/

{{- if .Objects}}
{{ range $object := .Objects}}
{{- range $method := .Methods}}
{{- if .ReturnsAsync}}
{{- MComment .Doc 0}}
typedef void (*WGPU{{$object.Name | PascalCase}}{{$method.Name | PascalCase}}Callback{{$.ExtSuffix}})({{CallbackArgs .}}) WGPU_FUNCTION_ATTRIBUTE;
{{- end}}
{{- end}}
{{- end}}
{{ end}}
{{- range .Callbacks}}
{{- MComment .Doc 0}}
typedef void (*WGPU{{.Name | PascalCase}}Callback{{$.ExtSuffix}})({{CallbackArgs .}}) WGPU_FUNCTION_ATTRIBUTE;
{{- end}}

/** @} */

Expand Down Expand Up @@ -234,6 +230,29 @@ typedef struct WGPUChainedStructOut {
* @{
*/

/**
* \defgroup WGPUCallbackInfo
* \brief Callback info structures that are used in asynchronous functions.
*
* @{
*/

{{- range .Callbacks}}
{{- MComment .Doc 0}}
typedef struct WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} {
WGPUChainedStruct const * nextInChain;
{{- if eq .Style "callback_mode" }}
WGPUCallbackMode mode;
{{- end}}
WGPU{{.Name | PascalCase}}Callback{{$.ExtSuffix}} callback;
WGPU_NULLABLE void* userdata1;
WGPU_NULLABLE void* userdata2;
} WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE;
{{ end}}{{"\n" -}}

/** @} */

{{- "\n"}}
{{- range $struct := .Structs}}
{{- MComment .Doc 0}}
typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
Expand Down
28 changes: 16 additions & 12 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (g *Generator) Gen(dst io.Writer) error {
"Singularize": Singularize,
"IsLast": func(i int, s any) bool { return i == reflect.ValueOf(s).Len()-1 },
"FunctionReturns": func(f Function) string {
if f.Callback != nil {
return "WGPUFuture"
}
if f.Returns != nil {
return g.CType(f.Returns.Type, f.Returns.Pointer, "")
}
Expand Down Expand Up @@ -162,6 +165,9 @@ func (g *Generator) CType(typ string, pointerType PointerType, suffix string) st
case strings.HasPrefix(typ, "function_type."):
ctype := "WGPU" + PascalCase(strings.TrimPrefix(typ, "function_type.")) + suffix
return appendModifiers(ctype, pointerType)
case strings.HasPrefix(typ, "callback."):
ctype := "WGPU" + PascalCase(strings.TrimPrefix(typ, "callback.")) + "Callback" + suffix
return appendModifiers(ctype, pointerType)
case strings.HasPrefix(typ, "object."):
ctype := "WGPU" + PascalCase(strings.TrimPrefix(typ, "object.")) + suffix
return appendModifiers(ctype, pointerType)
Expand Down Expand Up @@ -206,21 +212,15 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string {
sb.WriteString(", ")
}
}
if len(f.ReturnsAsync) > 0 {
var name string
if o != nil {
name = PascalCase(o.Name) + PascalCase(f.Name)
} else {
name = PascalCase(f.Name)
}
fmt.Fprintf(sb, ", WGPU%sCallback callback, WGPU_NULLABLE void * userdata", name)
if f.Callback != nil {
fmt.Fprintf(sb, ", %s callbackInfo", g.CType(*f.Callback, "", "Info"))
}
return sb.String()
}

func (g *Generator) CallbackArgs(f Function) string {
func (g *Generator) CallbackArgs(f Callback) string {
sb := &strings.Builder{}
for _, arg := range f.ReturnsAsync {
for _, arg := range f.Args {
if arg.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
Expand All @@ -242,7 +242,7 @@ func (g *Generator) CallbackArgs(f Function) string {
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
}
}
sb.WriteString("WGPU_NULLABLE void * userdata")
sb.WriteString("WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2")
return sb.String()
}

Expand Down Expand Up @@ -324,7 +324,11 @@ func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
fmt.Fprintf(sb, "size_t %sCount;\n", CamelCase(Singularize(member.Name)))
fmt.Fprintf(sb, " %s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
if strings.HasPrefix(member.Type, "callback.") {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, "", "Info"), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
}
}
return sb.String(), nil
}
10 changes: 5 additions & 5 deletions gen/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func mergeAndValidateDuplicates(yamlPaths []string) (errs error) {
constants := make(map[string]Constant)
enums := make(map[string]Enum)
bitflags := make(map[string]Bitflag)
functionTypes := make(map[string]Function)
structs := make(map[string]Struct)
callbacks := make(map[string]Callback)
functions := make(map[string]Function)
objects := make(map[string]Object)

Expand Down Expand Up @@ -99,11 +99,11 @@ func mergeAndValidateDuplicates(yamlPaths []string) (errs error) {
bitflags[bf.Name] = bf
}
}
for _, ft := range data.FunctionTypes {
if _, ok := functionTypes[ft.Name]; ok {
errs = errors.Join(errs, fmt.Errorf("merge: function_types.%s in %s was already found previously while parsing, duplicates are not allowed", ft.Name, yamlPath))
for _, c := range data.Callbacks {
if _, ok := callbacks[c.Name]; ok {
errs = errors.Join(errs, fmt.Errorf("merge: callbacks.%s in %s was already found previously while parsing, duplicates are not allowed", c.Name, yamlPath))
}
functionTypes[ft.Name] = ft
callbacks[c.Name] = c
}
for _, s := range data.Structs {
if _, ok := structs[s.Name]; ok {
Expand Down
11 changes: 9 additions & 2 deletions gen/yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Yml struct {
Typedefs []Typedef `yaml:"typedefs"`
Enums []Enum `yaml:"enums"`
Bitflags []Bitflag `yaml:"bitflags"`
FunctionTypes []Function `yaml:"function_types"`
Structs []Struct `yaml:"structs"`
Callbacks []Callback `yaml:"callbacks"`
Functions []Function `yaml:"functions"`
Objects []Object `yaml:"objects"`
}
Expand Down Expand Up @@ -75,11 +75,18 @@ type ParameterType struct {
Namespace string `yaml:"namespace"`
}

type Callback struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Style string `yaml:"type"`
Args []ParameterType `yaml:"args"`
}

type Function struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
ReturnsAsync []ParameterType `yaml:"returns_async"`
Returns *ParameterType `yaml:"returns"`
Callback *string `yaml:"callback"`
Args []ParameterType `yaml:"args"`
}

Expand Down
65 changes: 52 additions & 13 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,20 @@
"type": "string",
"pattern": "^(array<)?(typedef\\.|enum\\.|bitflag\\.|struct\\.|function_type\\.|object\\.)([a-zA-Z0-9]([a-zA-Z0-9_]*[a-zA-Z0-9])?)(>)?$"
},
"CallbackType": {
"type": "string",
"pattern": "^(callback\\.)([a-zA-Z0-9]([a-zA-Z0-9_]*[a-zA-Z0-9])?)$"
},
"Type": {
"oneOf": [
{
"$ref": "#/definitions/PrimitiveType"
},
{
"$ref": "#/definitions/ComplexType"
},
{
"$ref": "#/definitions/CallbackType"
}
]
},
Expand All @@ -75,6 +82,41 @@
"mutable"
]
},
"CallbackStyle": {
"type": "string",
"enum": [
"callback_mode",
"immediate"
]
},
"Callback": {
"type": "object",
"properties": {
"name": {
"$ref": "#/definitions/Name",
"description": "Callback name"
},
"doc": {
"type": "string"
},
"style": {
"$ref": "#/definitions/CallbackStyle",
"description": "Callback style"
},
"args": {
"type": "array",
"description": "Optional property, list of callback arguments",
"items": {
"$ref": "#/definitions/ParameterType"
}
}
},
"required": [
"name",
"doc",
"style"
]
},
"ParameterType": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -140,12 +182,9 @@
"type"
]
},
"returns_async": {
"type": "array",
"description": "Optional property, list of async callback arguments",
"items": {
"$ref": "#/definitions/ParameterType"
}
"callback": {
"$ref": "#/definitions/CallbackType",
"description": "Optional property, callback type for async functon"
},
"args": {
"type": "array",
Expand Down Expand Up @@ -321,12 +360,6 @@
]
}
},
"function_types": {
"type": "array",
"items": {
"$ref": "#/definitions/Function"
}
},
"structs": {
"type": "array",
"items": {
Expand Down Expand Up @@ -376,6 +409,12 @@
]
}
},
"callbacks": {
"type": "array",
"items": {
"$ref": "#/definitions/Callback"
}
},
"functions": {
"type": "array",
"items": {
Expand Down Expand Up @@ -421,7 +460,7 @@
"typedefs",
"enums",
"bitflags",
"function_types",
"callbacks",
"structs",
"functions",
"objects"
Expand Down
Loading
Loading