forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.h
More file actions
229 lines (197 loc) · 8.67 KB
/
Copy pathruntime.h
File metadata and controls
229 lines (197 loc) · 8.67 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Interfaces for runtime concepts.
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_H_
#define THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_H_
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "base/ast.h"
#include "base/type_provider.h"
#include "common/native_type.h"
#include "common/value.h"
#include "runtime/activation_interface.h"
#include "runtime/runtime_issue.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
namespace runtime_internal {
class RuntimeFriendAccess;
} // namespace runtime_internal
class EmbedderContext;
// Options for the Program::Evaluate call.
struct EvaluateOptions {
// Optional message factory to use for the duration of the Evaluate call.
// If unset, a default message factory will be provided by the runtime.
google::protobuf::MessageFactory* absl_nullable message_factory = nullptr;
// Optional embedder context to use for the duration of the Evaluate call.
// This is used to access custom data in extension functions.
// This is only propagated to functions that are marked as context sensitive.
const EmbedderContext* absl_nullable embedder_context = nullptr;
};
// Representation of an evaluable CEL expression.
//
// See Runtime below for creating new programs.
class Program {
public:
virtual ~Program() = default;
// Evaluate the program.
//
// Non-recoverable errors (i.e. outside of CEL's notion of an error) are
// returned as a non-ok absl::Status. These are propagated immediately and do
// not participate in CEL's notion of error handling.
//
// CEL errors are represented as result with an Ok status and a held
// cel::ErrorValue result.
//
// Activation manages instances of variables available in the cel expression's
// environment.
//
// The arena will be used to as necessary to allocate values and must outlive
// the returned value, as must this program.
//
// For consistency, users should use the same arena to create values
// in the activation and for Program evaluation.
absl::StatusOr<Value> Evaluate(
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
const ActivationInterface& activation,
const EvaluateOptions& options = {}) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return EvaluateImpl(activation, arena, options);
}
ABSL_DEPRECATED("Use the EvaluateOptions overload instead.")
absl::StatusOr<Value> Evaluate(
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nullable message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const ActivationInterface& activation) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return EvaluateImpl(activation, arena, {message_factory});
}
virtual const TypeProvider& GetTypeProvider() const = 0;
protected:
virtual absl::StatusOr<Value> EvaluateImpl(
const ActivationInterface& activation,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
const EvaluateOptions& options) const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
};
// Representation for a traceable CEL expression.
//
// Implementations provide an additional Trace method that evaluates the
// expression and invokes a callback allowing callers to inspect intermediate
// state during evaluation.
class TraceableProgram : public Program {
public:
// EvaluationListener may be provided to an EvaluateWithCallback call to
// inspect intermediate values during evaluation.
//
// The callback is called on after every program step that corresponds
// to an AST expression node. The value provided is the top of the value
// stack, corresponding to the result of evaluating the given sub expression.
//
// A returning a non-ok status stops evaluation and forwards the error.
using EvaluationListener = absl::AnyInvocable<absl::Status(
int64_t expr_id, const Value&, const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull)>;
using Program::Evaluate;
// Evaluate the Program plan with a Listener.
//
// The given callback will be invoked after evaluating any program step
// that corresponds to an AST node in the planned CEL expression.
//
// If the callback returns a non-ok status, evaluation stops and the Status
// is forwarded as the result of the EvaluateWithCallback call.
absl::StatusOr<Value> Trace(
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
const ActivationInterface& activation,
EvaluationListener evaluation_listener,
const EvaluateOptions& options = {}) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return TraceImpl(activation, std::move(evaluation_listener), arena,
options);
}
ABSL_DEPRECATED("Use the EvaluateOptions overload instead.")
absl::StatusOr<Value> Trace(
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nullable message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const ActivationInterface& activation,
EvaluationListener evaluation_listener) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return TraceImpl(activation, std::move(evaluation_listener), arena,
{message_factory});
}
protected:
absl::StatusOr<Value> EvaluateImpl(const ActivationInterface& activation,
google::protobuf::Arena* absl_nonnull arena
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const EvaluateOptions& options) const
ABSL_ATTRIBUTE_LIFETIME_BOUND override {
return TraceImpl(activation, nullptr, arena, options);
}
virtual absl::StatusOr<Value> TraceImpl(
const ActivationInterface& activation,
EvaluationListener evaluation_listener,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
const EvaluateOptions& options) const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
};
// Interface for a CEL runtime.
//
// Manages the state necessary to generate Programs.
//
// Runtime instances should be created from a RuntimeBuilder rather than
// instantiated directly.
//
// Implementations provided by CEL will be thread-compatible, but write
// operations on the underlying environment (TypeRegistry, FunctionRegistry) or
// on the implementation via down casting must be synchronized by the caller and
// may invalidate any Programs created from the Runtime.
class Runtime {
public:
struct CreateProgramOptions {
// Optional output for collecting issues encountered while planning.
// If non-null, vector is cleared and encountered issues are added.
std::vector<RuntimeIssue>* issues = nullptr;
};
virtual ~Runtime() = default;
absl::StatusOr<std::unique_ptr<Program>> CreateProgram(
std::unique_ptr<cel::Ast> ast) const {
return CreateProgram(std::move(ast), CreateProgramOptions{});
}
virtual absl::StatusOr<std::unique_ptr<Program>> CreateProgram(
std::unique_ptr<cel::Ast> ast,
const CreateProgramOptions& options) const = 0;
absl::StatusOr<std::unique_ptr<TraceableProgram>> CreateTraceableProgram(
std::unique_ptr<cel::Ast> ast) const {
return CreateTraceableProgram(std::move(ast), CreateProgramOptions{});
}
virtual absl::StatusOr<std::unique_ptr<TraceableProgram>>
CreateTraceableProgram(std::unique_ptr<cel::Ast> ast,
const CreateProgramOptions& options) const = 0;
virtual const TypeProvider& GetTypeProvider() const = 0;
virtual const google::protobuf::DescriptorPool* absl_nonnull GetDescriptorPool()
const = 0;
virtual google::protobuf::MessageFactory* absl_nonnull GetMessageFactory() const = 0;
private:
friend class runtime_internal::RuntimeFriendAccess;
virtual NativeTypeId GetNativeTypeId() const = 0;
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_RUNTIME_RUNTIME_H_