forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_impl.h
More file actions
125 lines (105 loc) · 4.06 KB
/
Copy pathruntime_impl.h
File metadata and controls
125 lines (105 loc) · 4.06 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
// 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.
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_
#define THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_
#include <memory>
#include <utility>
#include "absl/base/attributes.h"
#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 "eval/compiler/flat_expr_builder.h"
#include "internal/well_known_types.h"
#include "runtime/function_registry.h"
#include "runtime/internal/runtime_env.h"
#include "runtime/runtime.h"
#include "runtime/runtime_options.h"
#include "runtime/type_registry.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel::runtime_internal {
class RuntimeImpl : public Runtime {
public:
using Environment = RuntimeEnv;
RuntimeImpl(absl_nonnull std::shared_ptr<Environment> environment,
const RuntimeOptions& options)
: environment_(std::move(environment)),
expr_builder_(environment_, options) {
ABSL_DCHECK(environment_->well_known_types.IsInitialized());
}
TypeRegistry& type_registry() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return environment_->type_registry;
}
const TypeRegistry& type_registry() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return environment_->type_registry;
}
FunctionRegistry& function_registry() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return environment_->function_registry;
}
const FunctionRegistry& function_registry() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return environment_->function_registry;
}
const well_known_types::Reflection& well_known_types() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return environment_->well_known_types;
}
Environment& environment() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return *environment_;
}
const Environment& environment() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return *environment_;
}
// implement Runtime
absl::StatusOr<std::unique_ptr<Program>> CreateProgram(
std::unique_ptr<Ast> ast,
const Runtime::CreateProgramOptions& options) const final;
absl::StatusOr<std::unique_ptr<TraceableProgram>> CreateTraceableProgram(
std::unique_ptr<Ast> ast,
const Runtime::CreateProgramOptions& options) const override;
const TypeProvider& GetTypeProvider() const override {
return environment_->type_registry.GetComposedTypeProvider();
}
const google::protobuf::DescriptorPool* absl_nonnull GetDescriptorPool()
const override {
return environment_->descriptor_pool.get();
}
google::protobuf::MessageFactory* absl_nonnull GetMessageFactory() const override {
return environment_->MutableMessageFactory();
}
// exposed for extensions access
google::api::expr::runtime::FlatExprBuilder& expr_builder()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return expr_builder_;
}
private:
NativeTypeId GetNativeTypeId() const override {
return NativeTypeId::For<RuntimeImpl>();
}
// Note: this is mutable, but should only be accessed in a const context after
// building is complete.
//
// This is used to keep alive the registries while programs reference them.
std::shared_ptr<Environment> environment_;
google::api::expr::runtime::FlatExprBuilder expr_builder_;
};
// Exposed for testing to validate program is recursively planned.
//
// Uses dynamic_casts to test.
bool TestOnly_IsRecursiveImpl(const Program* program);
} // namespace cel::runtime_internal
#endif // THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_