Various minor cleanups in WASM bindings generation code
PiperOrigin-RevId: 829430835 Change-Id: Iadb0e4f8a197da69e093eccfd75d07cf6db5faaf
This commit is contained in:
committed by
Copybara-Service
parent
80a91cfbea
commit
45b079cfe8
@@ -73,9 +73,6 @@ class WrappedStructData:
|
|||||||
# Struct bindings code
|
# Struct bindings code
|
||||||
bindings: str = ""
|
bindings: str = ""
|
||||||
|
|
||||||
# Whether to use shallow copy for this struct
|
|
||||||
use_shallow_copy: bool = True
|
|
||||||
|
|
||||||
|
|
||||||
def _simple_property_binding(
|
def _simple_property_binding(
|
||||||
field: ast_nodes.StructFieldDecl,
|
field: ast_nodes.StructFieldDecl,
|
||||||
@@ -147,7 +144,7 @@ def _generate_field_data(
|
|||||||
|
|
||||||
anonymous_struct_name = ""
|
anonymous_struct_name = ""
|
||||||
for name, value in constants.ANONYMOUS_STRUCTS.items():
|
for name, value in constants.ANONYMOUS_STRUCTS.items():
|
||||||
if (value["parent"] == s and value["field_name"] == f.name):
|
if value["parent"] == s and value["field_name"] == f.name:
|
||||||
anonymous_struct_name = name
|
anonymous_struct_name = name
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -249,7 +246,7 @@ def _generate_field_data(
|
|||||||
return WrappedFieldData(
|
return WrappedFieldData(
|
||||||
definition=builder.to_string(),
|
definition=builder.to_string(),
|
||||||
typename=_get_field_struct_type(f.type),
|
typename=_get_field_struct_type(f.type),
|
||||||
binding=_simple_property_binding(f, w, setter=False, reference=True)
|
binding=_simple_property_binding(f, w, setter=False, reference=True),
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -293,18 +290,11 @@ def _generate_field_data(
|
|||||||
def _has_nested_wrapper_members(struct_info: ast_nodes.StructDecl) -> bool:
|
def _has_nested_wrapper_members(struct_info: ast_nodes.StructDecl) -> bool:
|
||||||
"""Checks if the struct contains other wrapped structs as direct members."""
|
"""Checks if the struct contains other wrapped structs as direct members."""
|
||||||
for field in struct_info.fields:
|
for field in struct_info.fields:
|
||||||
struct_field = cast(ast_nodes.StructFieldDecl, field)
|
member_type = cast(ast_nodes.StructFieldDecl, field).type
|
||||||
if isinstance(struct_field.type, ast_nodes.ValueType):
|
if isinstance(member_type, (ast_nodes.ArrayType, ast_nodes.PointerType)):
|
||||||
if struct_field.type.name in constants.STRUCTS_TO_BIND:
|
member_type = member_type.inner_type
|
||||||
return True
|
if isinstance(member_type, ast_nodes.ValueType):
|
||||||
if isinstance(struct_field.type, ast_nodes.ArrayType):
|
return member_type.name in constants.STRUCTS_TO_BIND
|
||||||
if isinstance(struct_field.type.inner_type, ast_nodes.ValueType):
|
|
||||||
if struct_field.type.inner_type.name in constants.STRUCTS_TO_BIND:
|
|
||||||
return True
|
|
||||||
if isinstance(struct_field.type, ast_nodes.PointerType):
|
|
||||||
if isinstance(struct_field.type.inner_type, ast_nodes.ValueType):
|
|
||||||
if struct_field.type.inner_type.name in constants.STRUCTS_TO_BIND:
|
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -357,29 +347,29 @@ def _build_struct_header_internal(
|
|||||||
|
|
||||||
def _default_function_statement(struct_name: str) -> str:
|
def _default_function_statement(struct_name: str) -> str:
|
||||||
"""Returns the default function name for the given struct."""
|
"""Returns the default function name for the given struct."""
|
||||||
if (
|
if struct_name == "mjvGeom":
|
||||||
struct_name in constants.ANONYMOUS_STRUCTS.keys()
|
f = "mjv_initGeom(ptr_, mjGEOM_NONE, nullptr, nullptr, nullptr, nullptr);"
|
||||||
or struct_name in constants.NO_DEFAULT_CONSTRUCTORS
|
return f
|
||||||
):
|
elif struct_name in constants.ANONYMOUS_STRUCTS.keys():
|
||||||
|
return ""
|
||||||
|
elif struct_name in constants.NO_DEFAULT_CONSTRUCTORS:
|
||||||
return ""
|
return ""
|
||||||
elif struct_name.startswith("mjs"):
|
elif struct_name.startswith("mjs"):
|
||||||
return f"mjs_default{struct_name.removeprefix('mjs')}(ptr_);"
|
return f"mjs_default{struct_name.removeprefix('mjs')}(ptr_);"
|
||||||
elif struct_name == "mjvGeom":
|
|
||||||
return (
|
|
||||||
"mjv_initGeom(ptr_, mjGEOM_NONE, nullptr, nullptr, nullptr, nullptr);"
|
|
||||||
)
|
|
||||||
elif struct_name.startswith("mjv"):
|
elif struct_name.startswith("mjv"):
|
||||||
return f"mjv_default{struct_name.removeprefix('mjv')}(ptr_);"
|
return f"mjv_default{struct_name.removeprefix('mjv')}(ptr_);"
|
||||||
else:
|
elif struct_name.startswith("mj"):
|
||||||
return f"mj_default{struct_name.removeprefix('mj')}(ptr_);"
|
return f"mj_default{struct_name.removeprefix('mj')}(ptr_);"
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def _delete_ptr_statement(struct_name: str) -> str:
|
def _delete_ptr_statement(struct_name: str) -> str:
|
||||||
"""Returns the delete function name for the given struct."""
|
"""Returns the delete function name for the given struct."""
|
||||||
if struct_name == "mjVFS":
|
if struct_name == "mjVFS":
|
||||||
return "mj_deleteVFS(ptr_);"
|
return "mj_deleteVFS(ptr_);"
|
||||||
else:
|
|
||||||
return "delete ptr_;"
|
return "delete ptr_;"
|
||||||
|
|
||||||
|
|
||||||
def _find_fields_with_init(
|
def _find_fields_with_init(
|
||||||
@@ -421,8 +411,7 @@ def build_struct_header(
|
|||||||
|
|
||||||
is_anonymous_struct = struct_name in constants.ANONYMOUS_STRUCTS.keys()
|
is_anonymous_struct = struct_name in constants.ANONYMOUS_STRUCTS.keys()
|
||||||
is_hardcoded_wrapper_struct = (
|
is_hardcoded_wrapper_struct = (
|
||||||
common.uppercase_first_letter(struct_name)
|
common.uppercase_first_letter(struct_name) in constants.MANUAL_STRUCTS
|
||||||
in constants.MANUAL_STRUCTS
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -603,44 +592,34 @@ def generate_wasm_bindings(
|
|||||||
|
|
||||||
wrapped_structs: Dict[str, WrappedStructData] = {}
|
wrapped_structs: Dict[str, WrappedStructData] = {}
|
||||||
for struct_name in structs_to_bind:
|
for struct_name in structs_to_bind:
|
||||||
wrapped_name = common.uppercase_first_letter(struct_name)
|
s = struct_name
|
||||||
|
w = common.uppercase_first_letter(s)
|
||||||
|
|
||||||
if struct_name in introspect_structs:
|
if s in introspect_structs:
|
||||||
struct_fields = introspect_structs[struct_name].fields
|
struct_fields = introspect_structs[s].fields
|
||||||
elif struct_name in constants.ANONYMOUS_STRUCTS:
|
elif s in constants.ANONYMOUS_STRUCTS:
|
||||||
anonymous_struct = _get_anonymous_struct_field(struct_name)
|
anonymous_struct = _get_anonymous_struct_field(s)
|
||||||
if not anonymous_struct or not isinstance(
|
if not anonymous_struct or not isinstance(
|
||||||
anonymous_struct.type, ast_nodes.AnonymousStructDecl
|
anonymous_struct.type, ast_nodes.AnonymousStructDecl
|
||||||
):
|
):
|
||||||
raise RuntimeError(f"Anonymous struct not found: {struct_name}")
|
raise RuntimeError(f"Anonymous struct not found: {s}")
|
||||||
struct_fields = anonymous_struct.type.fields
|
struct_fields = anonymous_struct.type.fields
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(f"Struct not found: {struct_name}")
|
raise RuntimeError(f"Struct not found: {s}")
|
||||||
|
|
||||||
wrapped_fields: List[WrappedFieldData] = []
|
wrapped_fields: List[WrappedFieldData] = []
|
||||||
for field in struct_fields:
|
for field in struct_fields:
|
||||||
wrapped_field = _generate_field_data(field, wrapped_name)
|
wrapped_fields.append(_generate_field_data(field, w))
|
||||||
wrapped_fields.append(wrapped_field)
|
|
||||||
|
|
||||||
wrapped_header = build_struct_header(
|
|
||||||
struct_name,
|
|
||||||
wrapped_fields,
|
|
||||||
)
|
|
||||||
wrapped_source = build_struct_source(
|
|
||||||
struct_name,
|
|
||||||
wrapped_fields,
|
|
||||||
)
|
|
||||||
bindings = _build_struct_bindings(struct_name, wrapped_fields)
|
|
||||||
wrap_data = WrappedStructData(
|
wrap_data = WrappedStructData(
|
||||||
wrap_name=wrapped_name,
|
wrap_name=w,
|
||||||
wrapped_fields=wrapped_fields,
|
wrapped_fields=wrapped_fields,
|
||||||
wrapped_header=wrapped_header,
|
wrapped_header=build_struct_header(s, wrapped_fields),
|
||||||
wrapped_source=wrapped_source,
|
wrapped_source=build_struct_source(s, wrapped_fields),
|
||||||
use_shallow_copy=use_shallow_copy(wrapped_fields),
|
bindings=_build_struct_bindings(s, wrapped_fields),
|
||||||
bindings=bindings,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
wrapped_structs[struct_name] = wrap_data
|
wrapped_structs[s] = wrap_data
|
||||||
|
|
||||||
return wrapped_structs
|
return wrapped_structs
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user