forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_impl.cc
More file actions
159 lines (138 loc) · 6.16 KB
/
Copy pathruntime_impl.cc
File metadata and controls
159 lines (138 loc) · 6.16 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
// 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.
#include "runtime/internal/runtime_impl.h"
#include <memory>
#include <utility>
#include "absl/base/nullability.h"
#include "absl/log/absl_check.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 "eval/eval/attribute_trail.h"
#include "eval/eval/comprehension_slots.h"
#include "eval/eval/direct_expression_step.h"
#include "eval/eval/evaluator_core.h"
#include "internal/casts.h"
#include "internal/status_macros.h"
#include "runtime/activation_interface.h"
#include "runtime/runtime.h"
#include "google/protobuf/arena.h"
namespace cel::runtime_internal {
namespace {
using ::google::api::expr::runtime::AttributeTrail;
using ::google::api::expr::runtime::ComprehensionSlots;
using ::google::api::expr::runtime::DirectExpressionStep;
using ::google::api::expr::runtime::ExecutionFrameBase;
using ::google::api::expr::runtime::FlatExpression;
using ::google::api::expr::runtime::WrappedDirectStep;
class ProgramImpl final : public TraceableProgram {
public:
using EvaluationListener = TraceableProgram::EvaluationListener;
ProgramImpl(
const std::shared_ptr<const RuntimeImpl::Environment>& environment,
FlatExpression impl)
: environment_(environment), impl_(std::move(impl)) {}
absl::StatusOr<Value> TraceImpl(
const ActivationInterface& activation,
EvaluationListener evaluation_listener, google::protobuf::Arena* absl_nonnull arena,
const EvaluateOptions& options) const override {
ABSL_DCHECK(arena != nullptr);
auto state =
impl_.MakeEvaluatorState(environment_->descriptor_pool.get(),
options.message_factory != nullptr
? options.message_factory
: environment_->MutableMessageFactory(),
arena);
return impl_.EvaluateWithCallback(activation, options.embedder_context,
std::move(evaluation_listener), state);
}
const TypeProvider& GetTypeProvider() const override {
return environment_->type_registry.GetComposedTypeProvider();
}
private:
// Keep the Runtime environment alive while programs reference it.
std::shared_ptr<const RuntimeImpl::Environment> environment_;
FlatExpression impl_;
};
class RecursiveProgramImpl final : public TraceableProgram {
public:
using EvaluationListener = TraceableProgram::EvaluationListener;
RecursiveProgramImpl(
const std::shared_ptr<const RuntimeImpl::Environment>& environment,
FlatExpression impl, const DirectExpressionStep* absl_nonnull root)
: environment_(environment), impl_(std::move(impl)), root_(root) {}
absl::StatusOr<Value> TraceImpl(
const ActivationInterface& activation,
EvaluationListener evaluation_listener, google::protobuf::Arena* absl_nonnull arena,
const EvaluateOptions& options) const override {
ABSL_DCHECK(arena != nullptr);
ComprehensionSlots slots(impl_.comprehension_slots_size());
ExecutionFrameBase frame(activation, std::move(evaluation_listener),
impl_.options(), GetTypeProvider(),
environment_->descriptor_pool.get(),
options.message_factory != nullptr
? options.message_factory
: environment_->MutableMessageFactory(),
arena, options.embedder_context, slots);
Value result;
AttributeTrail attribute;
CEL_RETURN_IF_ERROR(root_->Evaluate(frame, result, attribute));
return result;
}
const TypeProvider& GetTypeProvider() const override {
return environment_->type_registry.GetComposedTypeProvider();
}
private:
// Keep the Runtime environment alive while programs reference it.
std::shared_ptr<const RuntimeImpl::Environment> environment_;
FlatExpression impl_;
const DirectExpressionStep* absl_nonnull root_;
};
} // namespace
absl::StatusOr<std::unique_ptr<Program>> RuntimeImpl::CreateProgram(
std::unique_ptr<Ast> ast,
const Runtime::CreateProgramOptions& options) const {
return CreateTraceableProgram(std::move(ast), options);
}
absl::StatusOr<std::unique_ptr<TraceableProgram>>
RuntimeImpl::CreateTraceableProgram(
std::unique_ptr<Ast> ast,
const Runtime::CreateProgramOptions& options) const {
CEL_ASSIGN_OR_RETURN(auto flat_expr, expr_builder_.CreateExpressionImpl(
std::move(ast), options.issues));
// Special case if the program is fully recursive.
//
// This implementation avoids unnecessary allocs at evaluation time which
// improves performance notably for small expressions.
if (expr_builder_.options().max_recursion_depth != 0 &&
!flat_expr.subexpressions().empty() &&
// mainline expression is exactly one recursive step.
flat_expr.subexpressions().front().size() == 1 &&
flat_expr.subexpressions().front().front()->GetNativeTypeId() ==
NativeTypeId::For<WrappedDirectStep>()) {
const DirectExpressionStep* root =
internal::down_cast<const WrappedDirectStep*>(
flat_expr.subexpressions().front().front().get())
->wrapped();
return std::make_unique<RecursiveProgramImpl>(environment_,
std::move(flat_expr), root);
}
return std::make_unique<ProgramImpl>(environment_, std::move(flat_expr));
}
bool TestOnly_IsRecursiveImpl(const Program* program) {
return dynamic_cast<const RecursiveProgramImpl*>(program) != nullptr;
}
} // namespace cel::runtime_internal