Files
Mujoco_WASM/wasm/codegen/generators/structs.py
T
Google DeepMind fe1b6b7c80 Refactor: Group autogenerated structs source in bindings.cc
This change modifies the code generation for structs in `bindings.cc`. Instead of placing each struct's constructor and member functions under individual markers, all autogenerated struct source code is now grouped together under a single `// {{ AUTOGENNED_STRUCTS_SOURCE }}` marker. This improves the organization of the generated file and simplifies the template.

Future work would move the rest of the structs into this pattern

PiperOrigin-RevId: 828390272
Change-Id: Iee9d0e8766db86b2c1d4296a37ac7a8e6e05428b
2025-11-05 03:26:09 -08:00

80 lines
2.6 KiB
Python

# Copyright 2025 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generates Embind bindings for MuJoCo structs."""
from wasm.codegen.helpers import constants
from wasm.codegen.helpers import structs
class Generator:
"""Generates C++ code for binding and wrapping MuJoCo structs."""
def generate(self) -> list[tuple[str, list[str]]]:
"""Generates C++ header file for binding and wrapping MuJoCo structs."""
# Traverse the introspect dictionary to get the field
# wrapper/bindings statements set up for each struct
self.structs_to_bind_data = structs.generate_wasm_bindings(
constants.STRUCTS_TO_BIND
)
autogenned_struct_definitions = []
markers_and_content = []
# Sort by struct name by dependency to ensure deterministic output order
sorted_struct_names = structs.sort_structs_by_dependency(
self.structs_to_bind_data
)
for struct_name in sorted_struct_names:
struct_data = self.structs_to_bind_data[struct_name]
if struct_data.wrapped_header:
autogenned_struct_definitions.append(
struct_data.wrapped_header + "\n"
)
else:
markers_and_content.append((
f"// INSERT-GENERATED-{struct_data.wrap_name}-DEFINITIONS",
[
l.definition if l.definition else ""
for l in struct_data.wrapped_fields
],
))
markers_and_content.append((
"// {{ AUTOGENNED_STRUCT_DEFINITIONS }}",
autogenned_struct_definitions,
))
autogenned_struct_source = []
for struct_name in sorted_struct_names:
struct_data = self.structs_to_bind_data[struct_name]
if struct_data.wrapped_source:
autogenned_struct_source.append(
struct_data.wrapped_source + "\n"
)
# Bindings with markers
markers_and_content.append((
f"// INSERT-GENERATED-{struct_data.wrap_name}-BINDINGS",
[l.binding for l in struct_data.wrapped_fields],
))
markers_and_content.append((
"// {{ AUTOGENNED_STRUCTS_SOURCE }}",
autogenned_struct_source,
))
return markers_and_content