-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcreate_list_step.cc
More file actions
108 lines (89 loc) · 3.45 KB
/
Copy pathcreate_list_step.cc
File metadata and controls
108 lines (89 loc) · 3.45 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
#include "eval/eval/create_list_step.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "base/handle.h"
#include "eval/eval/expression_step_base.h"
#include "eval/eval/mutable_list_impl.h"
#include "eval/internal/interop.h"
#include "eval/public/containers/container_backed_list_impl.h"
#include "extensions/protobuf/memory_manager.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::interop_internal::CreateLegacyListValue;
using ::cel::interop_internal::CreateUnknownValueFromView;
using ::cel::interop_internal::ModernValueToLegacyValueOrDie;
class CreateListStep : public ExpressionStepBase {
public:
CreateListStep(int64_t expr_id, int list_size, bool immutable)
: ExpressionStepBase(expr_id),
list_size_(list_size),
immutable_(immutable) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
int list_size_;
bool immutable_;
};
absl::Status CreateListStep::Evaluate(ExecutionFrame* frame) const {
if (list_size_ < 0) {
return absl::Status(absl::StatusCode::kInternal,
"CreateListStep: list size is <0");
}
if (!frame->value_stack().HasEnough(list_size_)) {
return absl::Status(absl::StatusCode::kInternal,
"CreateListStep: stack underflow");
}
auto args = frame->value_stack().GetSpan(list_size_);
cel::Handle<cel::Value> result;
for (const auto& arg : args) {
if (arg->Is<cel::ErrorValue>()) {
result = arg;
frame->value_stack().Pop(list_size_);
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
}
const UnknownSet* unknown_set = nullptr;
if (frame->enable_unknowns()) {
unknown_set = frame->attribute_utility().MergeUnknowns(
args, frame->value_stack().GetAttributeSpan(list_size_),
/*initial_set=*/nullptr,
/*use_partial=*/true);
if (unknown_set != nullptr) {
result = CreateUnknownValueFromView(unknown_set);
frame->value_stack().Pop(list_size_);
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
}
auto* arena = cel::extensions::ProtoMemoryManager::CastToProtoArena(
frame->memory_manager());
if (immutable_) {
// TODO(issues/5): switch to new cel::ListValue in phase 2
result =
CreateLegacyListValue(google::protobuf::Arena::Create<ContainerBackedListImpl>(
arena,
ModernValueToLegacyValueOrDie(frame->memory_manager(), args)));
} else {
// TODO(issues/5): switch to new cel::ListValue in phase 2
result = CreateLegacyListValue(google::protobuf::Arena::Create<MutableListImpl>(
arena, ModernValueToLegacyValueOrDie(frame->memory_manager(), args)));
}
frame->value_stack().Pop(list_size_);
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateCreateListStep(
const cel::ast::internal::CreateList& create_list_expr, int64_t expr_id) {
return std::make_unique<CreateListStep>(
expr_id, create_list_expr.elements().size(), /*immutable=*/true);
}
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateCreateMutableListStep(
const cel::ast::internal::CreateList& create_list_expr, int64_t expr_id) {
return std::make_unique<CreateListStep>(
expr_id, create_list_expr.elements().size(), /*immutable=*/false);
}
} // namespace google::api::expr::runtime