o
    `j5                     @   s&   d Z ddlmZmZ G dd dZdS )a  Generate flat encode/decode functions for a StructField + version.

Given a StructField and a protocol version, generates Python functions
that encode/decode directly with zero dispatch overhead - no intermediate
SimpleField/ArrayField/StructField method calls.

Usage:
    from kafka.protocol.schemas.fields.codegen import CodegenContext
    # Encode: see StructField.compiled_encode_into()
    # Decode: see StructField.compiled_decode_from()
    	pack_intounpack_fromc                   @   sB   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	dS )CodegenContextz!Shared state for code generation.c                 C   s   g | _ ttd| _d| _d S )Nr   r   )linesr   r   globs_var_counterself r   c/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/protocol/schemas/fields/codegen.py__init__   s   
zCodegenContext.__init__vc                 C   s   |  j d7  _ d| | j  S )N   _)r   )r
   prefixr   r   r   next_var   s   zCodegenContext.next_varc                 C   s   | j | |  d S )N)r   append)r
   indentliner   r   r   emit   s   zCodegenContext.emitc                 C   s$   |  |d|  |  |d|  dS )a  Emit an inline capacity check before a write of up to ``nbytes`` bytes.

        Generated encode functions keep three locals in sync:

          * ``buf``  - the destination bytearray (``out.buf``),
          * ``pos``  - the current write offset,
          * ``_cap`` - ``len(buf)``, the cached capacity.

        These are not set up per fragment: they are declared once by the
        ``def _encode(item, out):`` preamble in
        ``StructField.encode_into__optimized_context`` (the sole generator of
        the compiled encode function), and every emitted fragment - including
        this one - is spliced into that function body. Emit fragments are
        therefore only valid inside that body; they cannot stand alone.

        ``nbytes`` is the MAXIMUM number of bytes the following write can
        consume (an ``int`` for fixed/varint fields, or a string expression
        such as ``'len(_bv1)'`` for a variable payload). The fast path is a
        single comparison; only on overflow do we sync ``out.pos``, grow via
        :meth:`EncodeBuffer.ensure`, and re-read the (possibly reallocated)
        buffer back into ``buf``/``_cap``.

        Because :meth:`EncodeBuffer.ensure` may rebind ``out.buf``, code that
        instead delegates to a runtime ``encode_into`` (e.g. tagged fields)
        must re-bind ``buf``/``_cap`` itself afterwards - ``emit_reserve`` only
        covers writes emitted inline.
        zif pos + %s > _cap:zA    out.pos = pos; out.ensure(%s); buf = out.buf; _cap = len(buf)N)r   )r
   r   nbytesr   r   r   emit_reserve   s   zCodegenContext.emit_reservec                 C   s   d | jS )N
)joinr   r	   r   r   r   source>   s   zCodegenContext.sourcec                 C   sf   t d | jD ]}t d| d| j|   qt d t| jD ]\}}t |d dd|  q d S )NzGLOBALS:z     =z
SOURCE:r   z<4 )printr   	enumerater   )r
   varir   r   r   r   r   A   s   
zCodegenContext.printN)r   )
__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r      s    
r   N)r%   structr   r   r   r   r   r   r   <module>   s    