mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Introduce HelperSignature in HelperHandlerRegistry
This commit is contained in:
@ -1,17 +1,31 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from llvmlite import ir
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HelperSignature:
|
||||||
|
"""Signature of a BPF helper function"""
|
||||||
|
|
||||||
|
arg_types: list[ir.Type]
|
||||||
|
return_type: ir.Type
|
||||||
|
func: Callable
|
||||||
|
|
||||||
|
|
||||||
class HelperHandlerRegistry:
|
class HelperHandlerRegistry:
|
||||||
"""Registry for BPF helpers"""
|
"""Registry for BPF helpers"""
|
||||||
|
|
||||||
_handlers: dict[str, Callable] = {}
|
_handlers: dict[str, HelperSignature] = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register(cls, helper_name):
|
def register(cls, helper_name, param_types=None, return_type=None):
|
||||||
"""Decorator to register a handler function for a helper"""
|
"""Decorator to register a handler function for a helper"""
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
cls._handlers[helper_name] = func
|
helper_sig = HelperSignature(
|
||||||
|
arg_types=param_types, return_type=return_type, func=func
|
||||||
|
)
|
||||||
|
cls._handlers[helper_name] = helper_sig
|
||||||
return func
|
return func
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
@ -19,7 +33,8 @@ class HelperHandlerRegistry:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_handler(cls, helper_name):
|
def get_handler(cls, helper_name):
|
||||||
"""Get the handler function for a helper"""
|
"""Get the handler function for a helper"""
|
||||||
return cls._handlers.get(helper_name)
|
handler = cls._handlers.get(helper_name)
|
||||||
|
return handler.func if handler else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def has_handler(cls, helper_name):
|
def has_handler(cls, helper_name):
|
||||||
|
|||||||
Reference in New Issue
Block a user