|
15 | 15 | logger: Logger = logging.getLogger(__name__) |
16 | 16 |
|
17 | 17 |
|
| 18 | +class CallHandlerRegistry: |
| 19 | + """Registry for handling different types of calls (helpers, etc.)""" |
| 20 | + |
| 21 | + _handler = None |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def set_handler(cls, handler): |
| 25 | + """Set the handler for unknown calls""" |
| 26 | + cls._handler = handler |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def handle_call( |
| 30 | + cls, call, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab |
| 31 | + ): |
| 32 | + """Handle a call using the registered handler""" |
| 33 | + if cls._handler is None: |
| 34 | + return None |
| 35 | + return cls._handler( |
| 36 | + call, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab |
| 37 | + ) |
| 38 | + |
| 39 | + |
18 | 40 | def get_operand_value( |
19 | 41 | func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None |
20 | 42 | ): |
@@ -478,51 +500,14 @@ def eval_expr( |
478 | 500 | structs_sym_tab, |
479 | 501 | ) |
480 | 502 |
|
481 | | - # delayed import to avoid circular dependency |
482 | | - from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call |
| 503 | + result = CallHandlerRegistry.handle_call( |
| 504 | + expr, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab |
| 505 | + ) |
| 506 | + if result is not None: |
| 507 | + return result |
483 | 508 |
|
484 | | - if isinstance(expr.func, ast.Name) and HelperHandlerRegistry.has_handler( |
485 | | - expr.func.id |
486 | | - ): |
487 | | - return handle_helper_call( |
488 | | - expr, |
489 | | - module, |
490 | | - builder, |
491 | | - func, |
492 | | - local_sym_tab, |
493 | | - map_sym_tab, |
494 | | - structs_sym_tab, |
495 | | - ) |
496 | | - elif isinstance(expr.func, ast.Attribute): |
497 | | - logger.info(f"Handling method call: {ast.dump(expr.func)}") |
498 | | - if isinstance(expr.func.value, ast.Call) and isinstance( |
499 | | - expr.func.value.func, ast.Name |
500 | | - ): |
501 | | - method_name = expr.func.attr |
502 | | - if HelperHandlerRegistry.has_handler(method_name): |
503 | | - return handle_helper_call( |
504 | | - expr, |
505 | | - module, |
506 | | - builder, |
507 | | - func, |
508 | | - local_sym_tab, |
509 | | - map_sym_tab, |
510 | | - structs_sym_tab, |
511 | | - ) |
512 | | - elif isinstance(expr.func.value, ast.Name): |
513 | | - obj_name = expr.func.value.id |
514 | | - method_name = expr.func.attr |
515 | | - if obj_name in map_sym_tab: |
516 | | - if HelperHandlerRegistry.has_handler(method_name): |
517 | | - return handle_helper_call( |
518 | | - expr, |
519 | | - module, |
520 | | - builder, |
521 | | - func, |
522 | | - local_sym_tab, |
523 | | - map_sym_tab, |
524 | | - structs_sym_tab, |
525 | | - ) |
| 509 | + logger.warning(f"Unknown call: {ast.dump(expr)}") |
| 510 | + return None |
526 | 511 | elif isinstance(expr, ast.Attribute): |
527 | 512 | return _handle_attribute_expr(expr, local_sym_tab, structs_sym_tab, builder) |
528 | 513 | elif isinstance(expr, ast.BinOp): |
|
0 commit comments