import { Builder, Ref, VarRef } from '../core/index.js'; import { Type } from '../reflection/type.js'; import { NamingStrategy } from './naming.js'; import type { HandlerRegistry } from './registry.js'; /** * Represents a dynamic path segment that contains a Ref for the index/key value. * Used when the path segment is computed at runtime (e.g., array index or object key in a loop). */ export declare class DynamicPathSegment { ref: Ref; constructor(ref: Ref); } /** * Type for path segments - can be either a static string or a dynamic segment. */ export type PathSegment = string | DynamicPathSegment; /** * Check if a type is complex (should count toward depth limit). * Complex types are those that contain nested types: objects, classes, arrays, tuples. */ export declare function isComplexType(type: Type): boolean; /** * Options for constructing a SerializerBuildContext. */ export interface SerializerBuildContextOptions { /** Types currently being processed (for circular detection) */ typeStack?: Set; /** Current depth in the type tree */ depth?: number; /** Maximum depth before extracting to separate function */ maxDepth?: number; /** Path segments for error messages */ pathSegments?: PathSegment[]; /** Naming strategy for property name transformation */ namingStrategy?: NamingStrategy; /** Cache of extracted functions by type */ fnCache?: Map>; } /** * Abstract base class for JIT serializer code generation context. * * This class contains shared state management logic used by both @runtyped/type * (JsonBuildContext) and @runtyped/bson (BsonBuildContext). It handles: * - Circular reference detection via typeStack * - Depth tracking for controlling function extraction * - Path segments for error messages * - The build() decision logic (inline vs extract) * * Subclasses implement: * - buildExtractedCall(): create an extracted function call for depth/recursion control */ export declare abstract class SerializerBuildContext { /** Default maximum inline depth before extracting to separate function */ static readonly DEFAULT_MAX_DEPTH = 3; /** JIT2 Builder for expression tree construction */ readonly b: Builder; /** The handler registry being used */ readonly registry: HandlerRegistry; /** Serialization direction */ readonly direction: 'serialize' | 'deserialize'; /** Types currently being processed (for circular detection) */ readonly typeStack: Set; /** Current depth in the type tree */ readonly depth: number; /** Maximum depth before extracting */ readonly maxDepth: number; /** Path segments for error messages */ readonly pathSegments: PathSegment[]; /** Naming strategy for property name transformation */ readonly namingStrategy: NamingStrategy; /** Cache of extracted functions by type */ readonly fnCache: Map>; constructor(b: Builder, registry: HandlerRegistry, direction: 'serialize' | 'deserialize', options?: SerializerBuildContextOptions); /** * Check if we should extract this type to a separate function. * Returns true if: * - Type is already in typeStack (circular reference) * - Depth exceeds maxDepth AND type is complex (code bloat prevention) */ shouldExtract(type: Type): boolean; /** * Check if type is in the current type stack (circular/mutual recursion). */ isCircular(type: Type): boolean; /** * Add type to the stack. Call before processing a complex type. */ pushType(type: Type): void; /** * Remove type from the stack. Call after processing a complex type. */ popType(type: Type): void; /** * Get the current path as a string for error messages. * Static segments are joined with '.', dynamic segments are converted to their values. */ getPath(): string; /** * Get the current path as a Ref expression (for runtime path building). * Returns a Ref that evaluates to the path at runtime. */ pathRef(): Ref; /** * Build a type, deciding whether to inline or extract. * * Decision tree: * 1. typeStack.has(type)? -> Extract (circular in current path) with fnCache reuse * 2. depth >= maxDepth && isComplex? -> Extract (size control) WITHOUT fnCache reuse * 3. Default -> Inline * * The distinction between cases 1 and 2 is critical: * - Case 1 (mutual recursion): We ARE on the same code path, so fnCache reuse is safe * - Case 2 (depth-based): We might be in different code paths (e.g., inside vs outside * an if-block), so fnCache reuse would fail with "variable not defined" errors */ build(type: Type, input: Ref): Ref; /** * Build a type inline, tracking it in the typeStack. * Wraps buildInline with push/pop for circular detection. */ protected buildInlineWithTracking(type: Type, input: Ref): Ref; /** * Build a type inline without extraction. * Uses the registry to generate code for the type. * Both @runtyped/type and @runtyped/bson use HandlerRegistry. */ protected buildInline(type: Type, input: Ref): Ref; /** * Create an extracted function call for a type. * * This method handles both mutual recursion and depth-based extraction: * - For mutual recursion (isMutualRecursion=true): reuses fnCache entry * - For depth-based (isMutualRecursion=false): creates new VarRef each time * * Subclasses must implement this because the extracted function needs to create * a child state with subclass-specific fields (e.g., optionsRef, serializer, etc.). * * @param type - The type to extract * @param input - The input value ref * @param isMutualRecursion - Whether this is a circular reference (safe to reuse cache) */ abstract buildExtractedCall(type: Type, input: Ref, isMutualRecursion: boolean): Ref; } export declare type __ΩPathSegment = any[]; export declare type __ΩSerializerBuildContextOptions = any[];