/proc/self/cwd/runtime/internal/runtime_impl.h
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 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_ |
17 | | |
18 | | #include <memory> |
19 | | #include <utility> |
20 | | |
21 | | #include "absl/base/attributes.h" |
22 | | #include "absl/base/nullability.h" |
23 | | #include "absl/log/absl_check.h" |
24 | | #include "absl/status/statusor.h" |
25 | | #include "base/ast.h" |
26 | | #include "base/type_provider.h" |
27 | | #include "common/native_type.h" |
28 | | #include "eval/compiler/flat_expr_builder.h" |
29 | | #include "internal/well_known_types.h" |
30 | | #include "runtime/function_registry.h" |
31 | | #include "runtime/internal/runtime_env.h" |
32 | | #include "runtime/runtime.h" |
33 | | #include "runtime/runtime_options.h" |
34 | | #include "runtime/type_registry.h" |
35 | | #include "google/protobuf/descriptor.h" |
36 | | #include "google/protobuf/message.h" |
37 | | |
38 | | namespace cel::runtime_internal { |
39 | | |
40 | | class RuntimeImpl : public Runtime { |
41 | | public: |
42 | | using Environment = RuntimeEnv; |
43 | | |
44 | | RuntimeImpl(absl_nonnull std::shared_ptr<Environment> environment, |
45 | | const RuntimeOptions& options) |
46 | | : environment_(std::move(environment)), |
47 | 0 | expr_builder_(environment_, options) { |
48 | 0 | ABSL_DCHECK(environment_->well_known_types.IsInitialized()); |
49 | 0 | } |
50 | | |
51 | 0 | TypeRegistry& type_registry() ABSL_ATTRIBUTE_LIFETIME_BOUND { |
52 | 0 | return environment_->type_registry; |
53 | 0 | } |
54 | 0 | const TypeRegistry& type_registry() const ABSL_ATTRIBUTE_LIFETIME_BOUND { |
55 | 0 | return environment_->type_registry; |
56 | 0 | } |
57 | | |
58 | 0 | FunctionRegistry& function_registry() ABSL_ATTRIBUTE_LIFETIME_BOUND { |
59 | 0 | return environment_->function_registry; |
60 | 0 | } |
61 | | const FunctionRegistry& function_registry() const |
62 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
63 | 0 | return environment_->function_registry; |
64 | 0 | } |
65 | | |
66 | | const well_known_types::Reflection& well_known_types() const |
67 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
68 | 0 | return environment_->well_known_types; |
69 | 0 | } |
70 | | |
71 | 0 | Environment& environment() ABSL_ATTRIBUTE_LIFETIME_BOUND { |
72 | 0 | return *environment_; |
73 | 0 | } |
74 | 0 | const Environment& environment() const ABSL_ATTRIBUTE_LIFETIME_BOUND { |
75 | 0 | return *environment_; |
76 | 0 | } |
77 | | |
78 | | // implement Runtime |
79 | | absl::StatusOr<std::unique_ptr<Program>> CreateProgram( |
80 | | std::unique_ptr<Ast> ast, |
81 | | const Runtime::CreateProgramOptions& options) const final; |
82 | | |
83 | | absl::StatusOr<std::unique_ptr<TraceableProgram>> CreateTraceableProgram( |
84 | | std::unique_ptr<Ast> ast, |
85 | | const Runtime::CreateProgramOptions& options) const override; |
86 | | |
87 | 0 | const TypeProvider& GetTypeProvider() const override { |
88 | 0 | return environment_->type_registry.GetComposedTypeProvider(); |
89 | 0 | } |
90 | | |
91 | | const google::protobuf::DescriptorPool* absl_nonnull GetDescriptorPool() |
92 | 0 | const override { |
93 | 0 | return environment_->descriptor_pool.get(); |
94 | 0 | } |
95 | | |
96 | 0 | google::protobuf::MessageFactory* absl_nonnull GetMessageFactory() const override { |
97 | 0 | return environment_->MutableMessageFactory(); |
98 | 0 | } |
99 | | |
100 | | // exposed for extensions access |
101 | | google::api::expr::runtime::FlatExprBuilder& expr_builder() |
102 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
103 | 0 | return expr_builder_; |
104 | 0 | } |
105 | | |
106 | | private: |
107 | 0 | NativeTypeId GetNativeTypeId() const override { |
108 | 0 | return NativeTypeId::For<RuntimeImpl>(); |
109 | 0 | } |
110 | | // Note: this is mutable, but should only be accessed in a const context after |
111 | | // building is complete. |
112 | | // |
113 | | // This is used to keep alive the registries while programs reference them. |
114 | | std::shared_ptr<Environment> environment_; |
115 | | google::api::expr::runtime::FlatExprBuilder expr_builder_; |
116 | | }; |
117 | | |
118 | | // Exposed for testing to validate program is recursively planned. |
119 | | // |
120 | | // Uses dynamic_casts to test. |
121 | | bool TestOnly_IsRecursiveImpl(const Program* program); |
122 | | |
123 | | } // namespace cel::runtime_internal |
124 | | |
125 | | #endif // THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_ |