Refactor: Generate all struct bindings within the codegen.

PiperOrigin-RevId: 828973537
Change-Id: I3f371d2f88e1cd9b1bb5c0a9c6a5ace7a21673fb
This commit is contained in:
Matias Manevi
2025-11-06 08:48:52 -08:00
committed by Copybara-Service
parent 2c2773e36d
commit 06ecdd6ba1
5 changed files with 1517 additions and 1590 deletions
File diff suppressed because it is too large Load Diff
+1 -8
View File
@@ -65,14 +65,7 @@ class Generator:
autogenned_struct_source.append(
struct_data.wrapped_source + "\n"
)
if struct_data.bindings:
autogenned_struct_bindings.append(struct_data.bindings)
else:
# Bindings with markers
markers_and_content.append((
f"// INSERT-GENERATED-{struct_data.wrap_name}-BINDINGS",
[l.binding for l in struct_data.wrapped_fields],
))
autogenned_struct_bindings.append(struct_data.bindings)
markers_and_content.append((
"// {{ AUTOGENNED_STRUCTS_SOURCE }}",
+9 -5
View File
@@ -47,13 +47,15 @@ class CodeBuilder:
class IndentBlock:
"""Helper class to manage indentation within a `with` statement."""
def __init__(self, builder: "CodeBuilder", header_line=""):
def __init__(self, builder: "CodeBuilder", header_line="", braces=True):
self._builder = builder
self._header_line = header_line
self._braces = braces
def __enter__(self):
line = self._header_line
line += " {" if line else "{"
if self._braces:
line += " {" if line else "{"
self._builder.line(line)
self._builder._indent_level += 1
self._line_count_enter = len(self._builder._lines)
@@ -65,20 +67,22 @@ class CodeBuilder:
if self._line_count_enter == len(self._builder._lines):
self._builder._lines[-1] += "}"
else:
self._builder.line("}")
if self._braces:
self._builder.line("}")
def block(self, header_line="") -> IndentBlock:
def block(self, header_line="", braces=True) -> IndentBlock:
"""Creates a block including braces and an optional header before the opening brace.
Use via a `with` statement.
Args:
header_line: Optional header line to add before the opening brace.
braces: Whether to include opening and closing braces.
Returns:
An IndentBlock instance that manages the indentation.
"""
return self.IndentBlock(self, header_line)
return self.IndentBlock(self, header_line, braces)
def function(self, signature="") -> IndentBlock:
"""Creates a function."""
+27 -22
View File
@@ -668,33 +668,38 @@ def _build_struct_bindings(
wrapped_fields: List[WrappedFieldData],
):
"""Builds the C++ bindings for a struct."""
# These structs require specific constructors
# which, for now, are hardcoded in the template file.
if struct_name in [
"mjData",
"mjModel",
"mjvScene",
"mjSpec",
]:
return ""
w = common.uppercase_first_letter(struct_name)
spc = " "
builder = code_builder.CodeBuilder()
builder.line(f"emscripten::class_<{w}>(\"{w}\")")
with builder.block(
header_line=f'emscripten::class_<{w}>("{w}")', braces=False
):
if w == "MjData":
builder.line(".constructor<MjModel *>()")
builder.line(".constructor<const MjModel &, const MjData &>()")
elif w == "MjModel":
builder.line(
'.class_function("loadFromXML", &loadFromXML, take_ownership())'
)
builder.line(".constructor<const MjModel &>()")
elif w == "MjSpec":
builder.line(".constructor<const MjSpec &>()")
elif w == "MjvScene":
builder.line(".constructor<MjModel *, int>()")
is_mjs = w.startswith("Mjs")
if not is_mjs:
builder.line(f"{spc}.constructor<>()")
is_mjs = w.startswith("Mjs")
if not is_mjs and w not in ["MjData", "MjModel", "MjSpec"]:
builder.line(".constructor<>()")
shallow_copy = use_shallow_copy(wrapped_fields)
if shallow_copy and not is_mjs:
builder.line(f"{spc}.function(\"copy\", &{w}::copy, take_ownership())")
shallow_copy = use_shallow_copy(wrapped_fields)
if shallow_copy and not is_mjs:
builder.line(f'.function("copy", &{w}::copy, take_ownership())')
for field in wrapped_fields[:-1]:
if field.binding:
builder.line(field.binding)
if wrapped_fields:
builder.line(f"{wrapped_fields[-1].binding};")
for field in wrapped_fields:
if field.binding:
builder.line(f"{spc}{field.binding}")
builder.line(f"{spc};")
return builder.to_string()
-20
View File
@@ -597,26 +597,6 @@ std::unique_ptr<MjSpec> parseXMLString(const std::string &xml) {
EMSCRIPTEN_BINDINGS(mujoco_bindings) {
function("parseXMLString", &parseXMLString, take_ownership());
emscripten::class_<MjModel>("MjModel")
.class_function("loadFromXML", &loadFromXML, take_ownership())
.constructor<const MjModel &>()
// INSERT-GENERATED-MjModel-BINDINGS
;
emscripten::class_<MjData>("MjData")
.constructor<MjModel *>()
.constructor<const MjModel &, const MjData &>()
// INSERT-GENERATED-MjData-BINDINGS
;
emscripten::class_<MjvScene>("MjvScene")
.constructor<>()
.constructor<MjModel *, int>()
// INSERT-GENERATED-MjvScene-BINDINGS
;
emscripten::class_<MjSpec>("MjSpec")
.constructor<const MjSpec &>()
// INSERT-GENERATED-MjSpec-BINDINGS
;
// {{ AUTOGENNED_STRUCTS_BINDINGS }}
// TODO: should be generated in future CLs -- //