Skip to content
Open
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
8 changes: 5 additions & 3 deletions codegen/apipatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def get_method_def(self, classname, methodname) -> str:
# Get arg names and types
idl_line = functions[name_idl]
args = idl_line.split("(", 1)[1].split(")", 1)[0].split(",")
args = [Attribute(arg) for arg in args if arg.strip()]
args = [Attribute(arg, struct=False) for arg in args if arg.strip()]
return_type = idl_line.split()[0]
if return_type.startswith("[NewObject]"):
# [NewObject] can be skipped: https://webidl.spec.whatwg.org/#NewObject
Expand All @@ -471,8 +471,10 @@ def get_method_def(self, classname, methodname) -> str:
if len(args) == 1 and args[0].typename.endswith(
("Options", "Descriptor", "Configuration")
):
if not args[0].required:
args[0].typename = args[0].typename.removeprefix("optional ")
assert args[0].typename.startswith("GPU")
des_is_optional = bool(args[0].default)
des_is_optional = bool(args[0].default) # part of the logic above?
attributes = self.idl.structs[args[0].typename[3:]].values()
py_args = [
self._arg_from_attribute(methodname, attr, des_is_optional)
Expand Down Expand Up @@ -583,7 +585,7 @@ def get_method_comment(self, classname, methodname):
idl_line = functions[name_idl]

args = idl_line.split("(", 1)[1].split(")", 1)[0].split(",")
args = [Attribute(arg) for arg in args if arg.strip()]
args = [Attribute(arg, struct=False) for arg in args if arg.strip()]

# If one arg that is a dict, flatten dict to kwargs
if len(args) == 1 and args[0].typename.endswith(
Expand Down
31 changes: 20 additions & 11 deletions codegen/idlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,33 @@ def get_idl_parser(*, allow_cache=True):
class Attribute:
"""A little object to hold a function argument or struct field."""

def __init__(self, line):
def __init__(self, line: str, struct: bool = True):
self.line = line.strip().strip(",;").strip()

default = None # None means 'no default' and "None" kinda means "auto".
required = False
required = False # as opposed to optional, https://webidl.spec.whatwg.org/#required-dictionary-member but struct members?
# function args can be optional https://webidl.spec.whatwg.org/#dfn-optional-argument and a required if not
arg = self.line
if "=" in arg:
arg, default = arg.rsplit("=", 1)
arg, default = arg.strip(), default.strip()
arg_type, arg_name = arg.strip().rsplit(" ", 1)
if arg_type.startswith("required "):
required = True
arg_type = arg_type.split(" ", 1)[1]
# required args should not have a default
assert default is None
elif arg_type.startswith("optional "):
arg_type = arg_type.split(" ", 1)[1]
default = default or "None"
if struct:
# some struct members might be required
if arg_type.startswith("required "):
required = True
arg_type = arg_type.split(" ", 1)[1]
# required args should not have a default
assert default is None
else:
# some function args might be optional
if arg_type.startswith("optional "):
required = False
arg_type = arg_type.split(" ", 1)[1]
default = default or "None"
else:
# but otherwise they are reuired
required = True

if default:
if default in ["false", "true"]:
Expand All @@ -66,7 +75,7 @@ def __init__(self, line):
def __repr__(self):
return f"<Attribute '{self.typename} {self.name}'>"

def to_str(self):
def to_str(self) -> str:
return self.line


Expand Down
Loading
Loading