Make WASM codegen output to be alphabetically ordered.

PiperOrigin-RevId: 836680575
Change-Id: I7ddb7267c0db4911b2e84528f948dee6fccdf81d
This commit is contained in:
Google DeepMind
2025-11-25 08:28:32 -08:00
committed by Copybara-Service
parent 6208730744
commit 6e59f61c70
6 changed files with 4069 additions and 4060 deletions
File diff suppressed because it is too large Load Diff
+5 -3
View File
@@ -37,7 +37,9 @@ class BindingBuilder:
def set_enums(self):
"""Generates and sets the enum bindings."""
self.markers_and_content += enums.generate(introspect_enums.ENUMS)
self.markers_and_content += enums.generate(
list(introspect_enums.ENUMS.values())
)
return self
def set_structs(self):
@@ -48,10 +50,10 @@ class BindingBuilder:
def set_functions(self):
"""Generates and sets the function wrappers and bindings."""
functions_to_bind: dict[str, ast_nodes.FunctionDecl] = {}
functions_to_bind = []
for name, func in introspect_functions.FUNCTIONS.items():
if not functions.is_excluded_function_name(name):
functions_to_bind[name] = func
functions_to_bind.append(func)
self.markers_and_content += functions.generate(functions_to_bind)
return self
+3 -3
View File
@@ -14,7 +14,7 @@
"""Generates Embind bindings for MuJoCo enums."""
from typing import Mapping
from typing import List
from introspect import ast_nodes
@@ -22,13 +22,13 @@ from wasm.codegen.generators import code_builder
def generate(
enums: Mapping[str, ast_nodes.EnumDecl],
enums: List[ast_nodes.EnumDecl],
) -> list[tuple[str, list[str]]]:
"""Generates all Embind code for the provided enums."""
builder = code_builder.CodeBuilder()
with builder.block('EMSCRIPTEN_BINDINGS(mujoco_enums)'):
for e in enums.values():
for e in sorted(enums, key=lambda e: e.name):
if e.values: # Skip empty enums.
with builder.block(f'enum_<{e.name}>("{e.name}")', braces=False):
names = list(e.values.keys())
+4 -4
View File
@@ -14,7 +14,7 @@
"""Helper functions for processing and generating bindings for MuJoCo functions."""
from typing import Mapping, Tuple, cast
from typing import List, Tuple, cast
from introspect import ast_nodes
@@ -289,18 +289,18 @@ def is_excluded_function_name(func_name: str) -> bool:
def generate(
functions: Mapping[str, ast_nodes.FunctionDecl],
functions: List[ast_nodes.FunctionDecl],
) -> list[tuple[str, list[str]]]:
"""Generates Embind bindings for MuJoCo functions."""
wrapper_functions = []
for func in functions.values():
for func in sorted(functions, key=lambda f: f.name):
if should_be_wrapped(func):
if func.name not in constants.MANUAL_WRAPPER_FUNCTIONS:
wrapper_functions.append(generate_function_wrapper(func))
wrapper_content = "\n\n".join(wrapper_functions)
function_bindings = []
for func in functions.values():
for func in sorted(functions, key=lambda f: f.name):
suffix = "_wrapper" if should_be_wrapped(func) else ""
function_bindings.append(f'function("{func.name}", &{func.name}{suffix});')
bindings_content = "\n".join(function_bindings)
+17 -10
View File
@@ -536,6 +536,7 @@ def _build_struct_bindings(
if shallow_copy and not is_mjs:
builder.line(f'.function("copy", &{w}::copy, take_ownership())')
wrapped_fields.sort(key=lambda field: field.binding)
for field in wrapped_fields[:-1]:
if field.binding:
builder.line(field.binding)
@@ -726,9 +727,7 @@ def generate(struct_to_bind: List[str]) -> list[tuple[str, list[str]]]:
# wrapper/bindings statements set up for each struct
structs_to_bind_data = generate_wasm_bindings(struct_to_bind)
autogenned_struct_definitions = []
markers_and_content = []
typedefs = []
for type_name in sorted(constants.ANONYMOUS_STRUCTS):
s = constants.ANONYMOUS_STRUCTS[type_name]
@@ -741,19 +740,23 @@ def generate(struct_to_bind: List[str]) -> list[tuple[str, list[str]]]:
))
# Sort by struct name by dependency to ensure deterministic output order
sorted_struct_names = sort_structs_by_dependency(structs_to_bind_data)
dependency_sorted_struct_names = sort_structs_by_dependency(
structs_to_bind_data
)
for struct_name in sorted_struct_names:
autogenned_struct_definitions = []
for struct_name in dependency_sorted_struct_names:
struct_data = structs_to_bind_data[struct_name]
if struct_data.wrapped_header:
autogenned_struct_definitions.append(struct_data.wrapped_header + "\n")
else:
definitions = []
for f in sorted(struct_data.wrapped_fields, key=lambda f: f.definition):
if f.definition:
definitions.append(f.definition)
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
],
definitions,
))
markers_and_content.append((
"// {{ AUTOGENNED_STRUCTS_HEADER }}",
@@ -761,11 +764,15 @@ def generate(struct_to_bind: List[str]) -> list[tuple[str, list[str]]]:
))
autogenned_struct_source = []
autogenned_struct_bindings = []
for struct_name in sorted_struct_names:
for struct_name in dependency_sorted_struct_names:
struct_data = structs_to_bind_data[struct_name]
if struct_data.wrapped_source:
autogenned_struct_source.append(struct_data.wrapped_source + "\n")
autogenned_struct_bindings = []
alphabetically_sorted_struct_names = sorted(structs_to_bind_data.keys())
for struct_name in alphabetically_sorted_struct_names:
struct_data = structs_to_bind_data[struct_name]
autogenned_struct_bindings.append(struct_data.bindings)
markers_and_content.append((
+9 -9
View File
@@ -802,34 +802,34 @@ class EnumsGeneratorTest(absltest.TestCase):
expected_code = """
EMSCRIPTEN_BINDINGS(mujoco_enums) {
enum_<AnotherEnum>("AnotherEnum")
.value("ALPHA", ALPHA)
.value("BETA", BETA);
enum_<TestEnum>("TestEnum")
.value("FIRST_VAL", FIRST_VAL)
.value("SECOND_VAL", SECOND_VAL)
.value("THIRD_VAL", THIRD_VAL);
enum_<AnotherEnum>("AnotherEnum")
.value("ALPHA", ALPHA)
.value("BETA", BETA);
}""".strip()
markers_and_content = enums.generate({
"TestEnum": ast_nodes.EnumDecl(
markers_and_content = enums.generate([
ast_nodes.EnumDecl(
name="TestEnum",
declname="enum TestEnum_",
values={"FIRST_VAL": 0, "SECOND_VAL": 1, "THIRD_VAL": 2},
),
"AnotherEnum": ast_nodes.EnumDecl(
ast_nodes.EnumDecl(
name="AnotherEnum",
declname="enum AnotherEnum_",
values={"ALPHA": 100, "BETA": 200},
),
"EmptyEnum": ast_nodes.EnumDecl(
ast_nodes.EnumDecl(
name="EmptyEnum",
declname="enum EmptyEnum_",
values={},
),
})
])
actual_code = "\n\n".join(markers_and_content[0][1])
self.assertEqual(actual_code, expected_code)