|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package interpreter |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "sync" |
| 20 | + "sync/atomic" |
| 21 | +) |
| 22 | + |
| 23 | +// evalContext contains the stateful information needed for a single evaluation. |
| 24 | +// |
| 25 | +// This state is shared across all frames within a single evaluation, including |
| 26 | +// child frames created for comprehension blocks. |
| 27 | +type evalContext struct { |
| 28 | + // interrupt exposes a callback channel for cancellation. |
| 29 | + interrupt <-chan struct{} |
| 30 | + |
| 31 | + // interruptCheckCount is the number of times the interrupt channel has been checked. |
| 32 | + interruptCheckCount atomic.Uint64 |
| 33 | + |
| 34 | + // interruptCheckFrequency is the frequency at which the interrupt channel is checked. |
| 35 | + interruptCheckFrequency uint |
| 36 | + |
| 37 | + // interrupted indicates whether the evaluation has been interrupted. |
| 38 | + interrupted atomic.Bool |
| 39 | + |
| 40 | + // state provides the context for tracking the evaluation state. |
| 41 | + state EvalState |
| 42 | + |
| 43 | + // costs provides the context for tracking the evaluation costs. |
| 44 | + costs *CostTracker |
| 45 | + |
| 46 | + // ctx is the context for async call implementations to use. |
| 47 | + ctx context.Context |
| 48 | + |
| 49 | + // cancel cancels the context when the evaluation is finished. |
| 50 | + cancel context.CancelFunc |
| 51 | +} |
| 52 | + |
| 53 | +// ExecutionFrame provides the context for a single evaluation of an expression. |
| 54 | +// |
| 55 | +// The execution frame must not be stored in any fashion as its lifecycle is completely |
| 56 | +// controlled by the CEL evaluation process. |
| 57 | +type ExecutionFrame struct { |
| 58 | + // Activation provides the context for resolving variables by name. |
| 59 | + Activation |
| 60 | + |
| 61 | + // parent provides the context for parent scopes (used for comprehension iterators). |
| 62 | + parent *ExecutionFrame |
| 63 | + |
| 64 | + // ctx provides the shared evaluation state across frames. |
| 65 | + ctx *evalContext |
| 66 | +} |
| 67 | + |
| 68 | +// NewExecutionFrame creates a new execution frame from the pool. |
| 69 | +func NewExecutionFrame(vars Activation) *ExecutionFrame { |
| 70 | + f := frameStack.Get().(*ExecutionFrame) |
| 71 | + f.Activation = vars |
| 72 | + return f |
| 73 | +} |
| 74 | + |
| 75 | +// SetContext sets the context for the execution frame. |
| 76 | +func (f *ExecutionFrame) SetContext(ctx context.Context, interruptCheckFrequency uint) { |
| 77 | + if f.ctx == nil { |
| 78 | + f.ctx = evalContextPool.Get().(*evalContext) |
| 79 | + } |
| 80 | + f.ctx.ctx, f.ctx.cancel = context.WithCancel(ctx) |
| 81 | + f.ctx.interrupt = ctx.Done() |
| 82 | + f.ctx.interruptCheckFrequency = interruptCheckFrequency |
| 83 | + f.ctx.interruptCheckCount.Store(0) |
| 84 | + f.ctx.interrupted.Store(false) |
| 85 | +} |
| 86 | + |
| 87 | +// Close releases the resources held by the execution frame and returns it to the pool. |
| 88 | +func (f *ExecutionFrame) Close() { |
| 89 | + if f.ctx != nil { |
| 90 | + if f.ctx.cancel != nil { |
| 91 | + f.ctx.cancel() |
| 92 | + f.ctx.cancel = nil |
| 93 | + } |
| 94 | + f.ctx.ctx = nil |
| 95 | + f.ctx.interrupt = nil |
| 96 | + f.ctx.state = nil |
| 97 | + f.ctx.costs = nil |
| 98 | + f.ctx.interrupted.Store(false) |
| 99 | + f.ctx.interruptCheckCount.Store(0) |
| 100 | + f.ctx.interruptCheckFrequency = 0 |
| 101 | + evalContextPool.Put(f.ctx) |
| 102 | + f.ctx = nil |
| 103 | + } |
| 104 | + f.parent = nil |
| 105 | + activationStack.release(f.Activation) |
| 106 | + f.Activation = nil |
| 107 | + frameStack.Put(f) |
| 108 | +} |
| 109 | + |
| 110 | +// push pushes the given activation onto the activation stack and returns the new frame. |
| 111 | +// |
| 112 | +// This operation is internal to the interpreter and is used to handle comprehension |
| 113 | +// scoping. The child frame inherits the shared evalContext from the parent. |
| 114 | +func (f *ExecutionFrame) push(activation Activation) *ExecutionFrame { |
| 115 | + child := frameStack.Get().(*ExecutionFrame) |
| 116 | + child.parent = f |
| 117 | + child.ctx = f.ctx |
| 118 | + child.Activation = activationStack.create(f.Activation, activation) |
| 119 | + return child |
| 120 | +} |
| 121 | + |
| 122 | +// pop returns the parent frame, releasing the current frame back to the pool. |
| 123 | +func (f *ExecutionFrame) pop() *ExecutionFrame { |
| 124 | + parent := f.parent |
| 125 | + activationStack.release(f.Activation) |
| 126 | + f.Activation = nil |
| 127 | + f.parent = nil |
| 128 | + f.ctx = nil |
| 129 | + frameStack.Put(f) |
| 130 | + return parent |
| 131 | +} |
| 132 | + |
| 133 | +// ResolveName implements the Activation interface by proxying to the internal activation. |
| 134 | +func (f *ExecutionFrame) ResolveName(name string) (any, bool) { |
| 135 | + return f.Activation.ResolveName(name) |
| 136 | +} |
| 137 | + |
| 138 | +// Parent implements the Activation interface by proxying to the internal activation. |
| 139 | +func (f *ExecutionFrame) Parent() Activation { |
| 140 | + return f.Activation.Parent() |
| 141 | +} |
| 142 | + |
| 143 | +// AsPartialActivation implements the PartialActivation interface by proxying to the internal activation. |
| 144 | +func (f *ExecutionFrame) AsPartialActivation() (PartialActivation, bool) { |
| 145 | + return AsPartialActivation(f.Activation) |
| 146 | +} |
| 147 | + |
| 148 | +// Unwrap returns the internal activation. |
| 149 | +func (f *ExecutionFrame) Unwrap() Activation { |
| 150 | + return f.Activation |
| 151 | +} |
| 152 | + |
| 153 | +// CheckInterrupt returns whether the evaluation has been interrupted. |
| 154 | +func (f *ExecutionFrame) CheckInterrupt() bool { |
| 155 | + if f.ctx == nil { |
| 156 | + return false |
| 157 | + } |
| 158 | + if f.ctx.interrupted.Load() { |
| 159 | + return true |
| 160 | + } |
| 161 | + count := f.ctx.interruptCheckCount.Add(1) |
| 162 | + if f.ctx.interruptCheckFrequency > 0 && count%uint64(f.ctx.interruptCheckFrequency) == 0 { |
| 163 | + select { |
| 164 | + case <-f.ctx.interrupt: |
| 165 | + f.ctx.interrupted.Store(true) |
| 166 | + return true |
| 167 | + default: |
| 168 | + return false |
| 169 | + } |
| 170 | + } |
| 171 | + return false |
| 172 | +} |
| 173 | + |
| 174 | +// frameStack provides a synchronized pool of ExecutionFrames. |
| 175 | +var frameStack = &sync.Pool{ |
| 176 | + New: func() any { |
| 177 | + return &ExecutionFrame{} |
| 178 | + }, |
| 179 | +} |
| 180 | + |
| 181 | +// evalContextPool provides a synchronized pool of evalContexts. |
| 182 | +var evalContextPool = &sync.Pool{ |
| 183 | + New: func() any { |
| 184 | + return &evalContext{} |
| 185 | + }, |
| 186 | +} |
| 187 | + |
| 188 | +type activationStackPool struct { |
| 189 | + sync.Pool |
| 190 | +} |
| 191 | + |
| 192 | +func (pool *activationStackPool) create(parent, child Activation) Activation { |
| 193 | + h := pool.Get().(*hierarchicalActivation) |
| 194 | + h.child = child |
| 195 | + h.parent = parent |
| 196 | + return h |
| 197 | +} |
| 198 | + |
| 199 | +func (pool *activationStackPool) release(activation Activation) { |
| 200 | + h, ok := activation.(*hierarchicalActivation) |
| 201 | + if !ok { |
| 202 | + return |
| 203 | + } |
| 204 | + h.parent = nil |
| 205 | + h.child = nil |
| 206 | + pool.Pool.Put(h) |
| 207 | +} |
| 208 | + |
| 209 | +func newActivationPool() *activationStackPool { |
| 210 | + return &activationStackPool{ |
| 211 | + Pool: sync.Pool{ |
| 212 | + New: func() any { |
| 213 | + return &hierarchicalActivation{} |
| 214 | + }, |
| 215 | + }, |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +var ( |
| 220 | + activationStack = newActivationPool() |
| 221 | +) |
0 commit comments