From 9282afa0c7222ff923038afadc31c66d79b6e0b8 Mon Sep 17 00:00:00 2001 From: Timi Date: Thu, 25 Sep 2025 08:59:27 +0100 Subject: [PATCH] Merge pull request #27726 from DasoTD:fix/string-property-binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix string property bindings in JS generator #27726 Fixes #27712 This PR fixes the binding generation logic in embindgen.py to correctly handle enum and string properties: Enum properties now use binding_utils::underlying_ptr(&Class::property). Standard string properties are bound directly with &Class::property. Other properties continue to use the default template. Testing: Verified generated bindings locally to ensure the expected output for enumsĀ andĀ strings. --- modules/js/generator/embindgen.py | 40 ++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/modules/js/generator/embindgen.py b/modules/js/generator/embindgen.py index d5d600e83f..e81ceb837a 100644 --- a/modules/js/generator/embindgen.py +++ b/modules/js/generator/embindgen.py @@ -320,7 +320,6 @@ class Namespace(object): class JSWrapperGenerator(object): def __init__(self, preprocessor_definitions=None): - self.bindings = [] self.wrapper_funcs = [] @@ -333,6 +332,29 @@ class JSWrapperGenerator(object): ) self.class_idx = 0 + def _is_string_type(self, tp: str) -> bool: + """Check if a type should be treated as string in bindings.""" + string_types = { + "std::string", + "char", + "signed char", + "unsigned char", + } + return tp in string_types + + def _generate_class_properties(self, class_info, class_bindings): + # Generate bindings for properties + for prop in class_info.props: + if prop.tp in type_dict and not self._is_string_type(prop.tp): + _class_property = class_property_enum_template + else: + _class_property = class_property_template + + class_bindings.append(_class_property.substitute( + js_name=prop.name, + cpp_name='::'.join([class_info.cname, prop.name]) + )) + def add_class(self, stype, name, decl): class_info = ClassInfo(name, decl) class_info.decl_idx = self.class_idx @@ -893,11 +915,17 @@ class JSWrapperGenerator(object): - # Generate bindings for properties - for property in class_info.props: - _class_property = class_property_enum_template if property.tp in type_dict else class_property_template - class_bindings.append(_class_property.substitute(js_name=property.name, cpp_name='::'.join( - [class_info.cname, property.name]))) + # Generate bindings for properties(prop) + for prop in class_info.props: + if prop.tp in type_dict and not self._is_string_type(prop.tp): + _class_property = class_property_enum_template + else: + _class_property = class_property_template + + class_bindings.append(_class_property.substitute( + js_name=prop.name, + cpp_name='::'.join([class_info.cname, prop.name]) + )) dv = '' base = Template("""base<$base>""")