All files / src expression-state.ts

100% Statements 93/93
100% Branches 37/37
100% Functions 16/16
100% Lines 93/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171  1x 1x 1x                     1x 5206x           5206x         5206x   5206x 5206x 5206x       5206x 6x 6x   5206x 5x 3x 3x 2x 5x   5206x 5x 5x       5206x 53x 53x   5206x 56x 3x 3x 53x 56x           5206x 139x 1x 1x 138x 139x       5206x 2x 2x               5206x   116x 52x 52x     116x 3x 3x     116x 3x 3x 3x 3x 3x   58x 58x 54x 54x 4x 116x         5206x   3x 2x 2x 2x 2x 2x 2x   1x 3x       5206x 11x 11x 1x 1x 10x 11x       5206x 20x 20x       5206x 10x 10x               5206x 59x   59x 1x 1x 59x 59x       5206x 123x 123x 5206x  
import type { EvaluationContext } from './evaluation-context/evaluation-context.js';
import { TypedValue } from './typed-value.js';
import { SpelEvaluationException } from './error/spel-evaluation-exception.js';
import { SpelMessage } from './error/spel-message.js';
import type { TypeDescriptor } from './type/type-descriptor.js';
 
/**
 * Parallels Spring ExpressionState
 *
 * Manages all state during expression evaluation:
 * - scopeStack: stack of current variable contexts (for #var lookup)
 * - headIndexStack: #this reference stack (for collection selection/projection)
 * - EvaluationContext: delegated global context
 */
export class ExpressionState {
  private readonly context: EvaluationContext;
 
  /**
   * scopeStack: Top of stack is current active scope
   * Each scope is a Map<string, TypedValue>
   */
  private readonly scopeStack: Map<string, TypedValue>[] = [];
 
  /**
   * headIndexStack: Track #this references in nested iteration contexts
   */
  private readonly headIndexStack: TypedValue[] = [];
 
  constructor(context: EvaluationContext) {
    this.context = context;
  }
 
  // ===== scopeStack Management =====
 
  public pushScope(scope: Map<string, TypedValue>): void {
    this.scopeStack.push(scope);
  }
 
  public popScope(): Map<string, TypedValue> {
    if (this.scopeStack.length === 0) {
      throw new SpelEvaluationException(-1, SpelMessage.CANNOT_POP_SCOPE);
    }
    return this.scopeStack.pop()!;
  }
 
  public peekScope(): Map<string, TypedValue> | undefined {
    return this.scopeStack[this.scopeStack.length - 1];
  }
 
  // ===== headIndexStack Management =====
 
  public pushHeadIndex(value: TypedValue): void {
    this.headIndexStack.push(value);
  }
 
  public popHeadIndex(): TypedValue {
    if (this.headIndexStack.length === 0) {
      throw new SpelEvaluationException(-1, SpelMessage.CANNOT_POP_HEAD_INDEX);
    }
    return this.headIndexStack.pop()!;
  }
 
  /**
   * Get current #this value
   * headIndexStack top is current innermost #this
   */
  public getThis(): TypedValue {
    if (this.headIndexStack.length > 0) {
      return this.headIndexStack[this.headIndexStack.length - 1]!;
    }
    return this.context.getRootObject();
  }
 
  // ===== #root Resolution =====
 
  public getRoot(): TypedValue {
    return this.context.getRootObject();
  }
 
  // ===== Variable Lookup =====
 
  /**
   * Lookup variable #varName
   * Search from scopeStack top-down, then delegate to context
   */
  public lookupVariable(name: string): TypedValue {
    // #this is always the current iteration element
    if (name === 'this' && this.headIndexStack.length > 0) {
      return this.headIndexStack[this.headIndexStack.length - 1]!;
    }
 
    // #root is always the root context object
    if (name === 'root') {
      return this.context.getRootObject();
    }
 
    // Search from stack top downward
    for (let i = this.scopeStack.length - 1; i >= 0; i--) {
      const scope = this.scopeStack[i]!;
      if (scope.has(name)) {
        return scope.get(name)!;
      }
    }
    // Delegate to context
    const result = this.context.lookupVariable(name);
    if (result != null) {
      return result;
    }
    throw new SpelEvaluationException(-1, SpelMessage.VARIABLE_NOT_FOUND, name);
  }
 
  /**
   * Set variable #varName = value
   */
  public setVariable(name: string, value: unknown): void {
    // Search from stack top downward
    for (let i = this.scopeStack.length - 1; i >= 0; i--) {
      const scope = this.scopeStack[i]!;
      if (scope.has(name)) {
        scope.set(name, new TypedValue(value));
        return;
      }
    }
    // Set in context
    this.context.setVariable(name, value);
  }
 
  // ===== Function Lookup =====
 
  public lookupFunction(name: string): (...args: unknown[]) => unknown {
    const fn = this.context.lookupFunction(name);
    if (fn != null) {
      return fn;
    }
    throw new SpelEvaluationException(-1, SpelMessage.FUNCTION_NOT_FOUND, name);
  }
 
  // ===== Type Lookup (Delegate to TypeLocator) =====
 
  public findType(typeName: string): TypeDescriptor {
    return this.context.getTypeLocator().findType(typeName);
  }
 
  // ===== Bean Lookup (Delegate to BeanResolver) =====
 
  public resolveBean(beanName: string, isFactoryBean = false): unknown {
    return this.context.getBeanResolver().resolve(beanName, isFactoryBean);
  }
 
  // ===== Create Child State =====
 
  /**
   * Create a child state with given rootObject as root context
   * Used for property chain navigation in CompoundExpression
   */
  public createChildState(rootObject: unknown): ExpressionState {
    const child = new ExpressionState(this.context.createChildContext(rootObject));
    // Inherit scopeStack
    for (const scope of this.scopeStack) {
      child.scopeStack.push(scope);
    }
    return child;
  }
 
  // ===== Context access =====
 
  public getEvaluationContext(): EvaluationContext {
    return this.context;
  }
}