Skip to content

Commit 63ec471

Browse files
committed
Respect singleton handles in codegen
Add a singleton_has_handle flag to MappedClass and initialize it when mapping classes. Compute is_singleton in TypeMapper and avoid requiring an instance handle for methods of true singletons by adjusting needs_instance_handle logic. In generator, compute has_handle_classes (any class that is not a singleton or a singleton that has a handle) and expose it in the file context as has_handle_classes. These changes make handle/FFI generation aware of singleton classes that do not require instance handles.
1 parent 14a0ac7 commit 63ec471

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

tools/bindgen/codegen/context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class MappedClass:
181181
destroy_symbol: str = ""
182182
is_singleton: bool = False
183183
singleton_symbol: str = ""
184+
singleton_has_handle: bool = False
184185
qualified_name: Optional[str] = None
185186
raw: Optional[IRClass] = None
186187

@@ -692,6 +693,7 @@ def map_method(self, ir_method: IRMethod, ir_class: IRClass) -> MappedMethod:
692693
class_key = ir_class.qualified_name or ir_class.name
693694
method_key = f"{class_key}::{ir_method.name}"
694695
force_instance = method_key in self.force_instance_methods
696+
is_singleton = self.namer.class_name(ir_class.name) in self.singleton_classes
695697

696698
call_args: List[str] = []
697699
pre_call_lines: List[str] = []
@@ -751,7 +753,7 @@ def map_method(self, ir_method: IRMethod, ir_class: IRClass) -> MappedMethod:
751753
property_name=property_name,
752754
method_kind=ir_method.kind,
753755
static=ir_method.static and not force_instance,
754-
needs_instance_handle=(not ir_method.static) or force_instance,
756+
needs_instance_handle=((not ir_method.static) or force_instance) and not is_singleton,
755757
const=ir_method.const,
756758
access=ir_method.access,
757759
variadic=ir_method.variadic,
@@ -774,6 +776,7 @@ def map_class(self, ir_class: IRClass) -> MappedClass:
774776
destroy_symbol=self._symbol_for_class_destroy(ir_class),
775777
is_singleton=is_singleton,
776778
singleton_symbol=singleton_symbol,
779+
singleton_has_handle=False,
777780
qualified_name=ir_class.qualified_name,
778781
raw=ir_class,
779782
)

tools/bindgen/codegen/generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def _iter_callable_bridges():
143143
callable_item.return_bridge in {"offset", "size", "rect"}
144144
for callable_item in callables
145145
)
146+
has_handle_classes = any(not cls.is_singleton or cls.singleton_has_handle for cls in mapped_file.classes)
146147
uses_ffi = any(
147148
callable_item.return_bridge in {"string", "struct"}
148149
or callable_item.pre_call_lines
@@ -191,6 +192,7 @@ def _iter_callable_bridges():
191192
"has_enums": bool(mapped_file.enums),
192193
"has_functions": bool(mapped_file.functions),
193194
"has_classes": bool(mapped_file.classes),
195+
"has_handle_classes": has_handle_classes,
194196
"has_constants": bool(mapped_file.constants),
195197
"has_aliases": bool(mapped_file.aliases),
196198
"uses_ffi": uses_ffi,

0 commit comments

Comments
 (0)