/proc/self/cwd/runtime/internal/runtime_impl.cc
Line | Count | Source |
1 | | // Copyright 2023 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 | | // https://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 | | #include "runtime/internal/runtime_impl.h" |
15 | | |
16 | | #include <memory> |
17 | | #include <utility> |
18 | | |
19 | | #include "absl/base/nullability.h" |
20 | | #include "absl/log/absl_check.h" |
21 | | #include "absl/status/statusor.h" |
22 | | #include "base/ast.h" |
23 | | #include "base/type_provider.h" |
24 | | #include "common/value.h" |
25 | | #include "eval/eval/evaluator_core.h" |
26 | | #include "internal/status_macros.h" |
27 | | #include "runtime/activation_interface.h" |
28 | | #include "runtime/runtime.h" |
29 | | #include "google/protobuf/arena.h" |
30 | | |
31 | | namespace cel::runtime_internal { |
32 | | namespace { |
33 | | |
34 | | using ::google::api::expr::runtime::FlatExpression; |
35 | | |
36 | | class ProgramImpl final : public TraceableProgram { |
37 | | public: |
38 | | using EvaluationListener = TraceableProgram::EvaluationListener; |
39 | | ProgramImpl( |
40 | | const std::shared_ptr<const RuntimeImpl::Environment>& environment, |
41 | | FlatExpression impl) |
42 | 0 | : environment_(environment), impl_(std::move(impl)) {} |
43 | | |
44 | | absl::StatusOr<Value> TraceImpl( |
45 | | const ActivationInterface& activation, |
46 | | EvaluationListener evaluation_listener, google::protobuf::Arena* absl_nonnull arena, |
47 | 0 | const EvaluateOptions& options) const override { |
48 | 0 | ABSL_DCHECK(arena != nullptr); |
49 | 0 | auto state = |
50 | 0 | impl_.MakeEvaluatorState(environment_->descriptor_pool.get(), |
51 | 0 | options.message_factory != nullptr |
52 | 0 | ? options.message_factory |
53 | 0 | : environment_->MutableMessageFactory(), |
54 | 0 | arena); |
55 | 0 | return impl_.EvaluateWithCallback(activation, options.embedder_context, |
56 | 0 | std::move(evaluation_listener), state); |
57 | 0 | } |
58 | | |
59 | 0 | const TypeProvider& GetTypeProvider() const override { |
60 | 0 | return environment_->type_registry.GetComposedTypeProvider(); |
61 | 0 | } |
62 | | |
63 | | private: |
64 | | // Keep the Runtime environment alive while programs reference it. |
65 | | std::shared_ptr<const RuntimeImpl::Environment> environment_; |
66 | | FlatExpression impl_; |
67 | | }; |
68 | | |
69 | | } // namespace |
70 | | |
71 | | absl::StatusOr<std::unique_ptr<Program>> RuntimeImpl::CreateProgram( |
72 | | std::unique_ptr<Ast> ast, |
73 | 0 | const Runtime::CreateProgramOptions& options) const { |
74 | 0 | return CreateTraceableProgram(std::move(ast), options); |
75 | 0 | } |
76 | | |
77 | | absl::StatusOr<std::unique_ptr<TraceableProgram>> |
78 | | RuntimeImpl::CreateTraceableProgram( |
79 | | std::unique_ptr<Ast> ast, |
80 | 0 | const Runtime::CreateProgramOptions& options) const { |
81 | 0 | CEL_ASSIGN_OR_RETURN(auto flat_expr, expr_builder_.CreateExpressionImpl( |
82 | 0 | std::move(ast), options.issues)); |
83 | |
|
84 | 0 | return std::make_unique<ProgramImpl>(environment_, std::move(flat_expr)); |
85 | 0 | } |
86 | | |
87 | | } // namespace cel::runtime_internal |