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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 5702x 5702x 5702x 5702x 6x 5x 3x 2x 5x 116x 119x 3x 116x 322x 37x 285x 2x 141x 73x 68x 4x 64x 3x 3x 3x 61x 61x 56x 5x 3x 2x 2x 2x 2x 1x 15x 15x 1x 14x 66x 10x 141x 141x 141x 1x 141x 103x 103x 103x 306x | 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[] = [];
/**
* The root object the whole expression started from. A compound expression
* rebinds a child state's context root to the receiver, so this is kept
* separately in order to answer {@link getArgumentScope}.
*/
private originalRoot: TypedValue;
constructor(context: EvaluationContext) {
this.context = context;
this.originalRoot = context.getRootObject();
}
// ===== 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]!;
Eif (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]!;
Eif (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));
child.originalRoot = this.originalRoot;
// Inherit scopeStack
for (const scope of this.scopeStack) {
child.scopeStack.push(scope);
}
return child;
}
/**
* The scope a method's arguments are evaluated in.
*
* A compound expression rebinds the receiver as the root of the child state it
* hands to the following node, so evaluating an argument in the current state
* would read a bare name as a member of the receiver: `s.substring(i)` looked
* up `i` on the string instead of on the object the expression started from.
*
* Arguments therefore resolve at the expression's root scope, which is also
* where a top-level bare name resolves. A receiver still resolves in the
* current scope, so `items.?[price > 20]` continues to read `price` from the
* element.
*/
public getArgumentScope(): ExpressionState {
const state = new ExpressionState(
this.context.createChildContext(this.originalRoot.getValue()),
);
for (const scope of this.scopeStack) {
state.scopeStack.push(scope);
}
return state;
}
// ===== Context access =====
public getEvaluationContext(): EvaluationContext {
return this.context;
}
}
|