29c1a3a0a5
PiperOrigin-RevId: 827965451 Change-Id: I657fc4d3a480fbc62124c84fe9ddc0900a91183f
90 lines
2.9 KiB
Python
90 lines
2.9 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 functions."""
|
|
|
|
from typing import List, Mapping, Optional
|
|
|
|
from introspect import ast_nodes
|
|
|
|
from wasm.codegen.helpers import code_builder
|
|
from wasm.codegen.helpers import functions as function_utils
|
|
|
|
|
|
class Generator:
|
|
"""Generates Embind bindings for MuJoCo functions."""
|
|
|
|
def __init__(self, functions: Mapping[str, ast_nodes.FunctionDecl]):
|
|
self.direct_bind_functions: List[ast_nodes.FunctionDecl] = []
|
|
self.wrapper_bind_functions: List[ast_nodes.FunctionDecl] = []
|
|
|
|
for func in functions.values():
|
|
if function_utils.should_be_wrapped(func):
|
|
self.wrapper_bind_functions.append(func)
|
|
else:
|
|
self.direct_bind_functions.append(func)
|
|
|
|
def _generate_wrappers(self) -> str:
|
|
"""Generates Embind bindings for all functions that need wrappers."""
|
|
|
|
code = []
|
|
for func in self.wrapper_bind_functions:
|
|
wrapper_code = function_utils.generate_function_wrapper(func)
|
|
code.append(wrapper_code)
|
|
|
|
return "\n\n".join(code)
|
|
|
|
def _generate_direct_bindable_functions(self) -> str:
|
|
"""Generates Embind bindings for all directly bindable functions."""
|
|
|
|
result = ""
|
|
for func in self.direct_bind_functions:
|
|
result += code_builder.INDENT
|
|
result += self._generate_function_binding(func)
|
|
|
|
return result
|
|
|
|
def _generate_function_binding(
|
|
self, func: ast_nodes.FunctionDecl, is_wrapper=False
|
|
) -> str:
|
|
"""Generates the Embind code for a single function."""
|
|
|
|
js_name, cpp_func = func.name, func.name
|
|
if is_wrapper:
|
|
cpp_func += "_wrapper"
|
|
|
|
return f'function("{js_name}", &{cpp_func});\n'
|
|
|
|
def _generate_wrapper_bindable_functions(self) -> str:
|
|
"""Generates Embind bindings for all functions that need wrappers."""
|
|
|
|
result = ""
|
|
for func in self.wrapper_bind_functions:
|
|
result += code_builder.INDENT
|
|
result += self._generate_function_binding(func, True)
|
|
|
|
return result
|
|
|
|
def generate(self) -> list[tuple[str, list[str]]]:
|
|
"""Generates the bindings file for all functions."""
|
|
|
|
wrapper_functions = self._generate_wrappers()
|
|
function_bindings = self._generate_direct_bindable_functions()
|
|
function_bindings += self._generate_wrapper_bindable_functions()
|
|
|
|
return [
|
|
("// {{ WRAPPER_FUNCTIONS }}", [wrapper_functions]),
|
|
("// {{ FUNCTION_BINDINGS }}", [function_bindings]),
|
|
]
|