Skip to content

Commit aafa36e

Browse files
zeitgeist87copybara-github
authored andcommitted
Refactor: Pass ActivationInterface to CEL function implementations
This change modifies the `cel::Function::Invoke` signature to include a nullable `cel::ActivationInterface*`. This allows custom CEL function implementations to access the current activation context. PiperOrigin-RevId: 823026720
1 parent c86ab58 commit aafa36e

File tree

14 files changed

+330
-162
lines changed

14 files changed

+330
-162
lines changed

eval/compiler/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ cc_test(
198198
"@com_google_absl//absl/container:flat_hash_map",
199199
"@com_google_absl//absl/status",
200200
"@com_google_absl//absl/status:status_matchers",
201+
"@com_google_absl//absl/status:statusor",
201202
"@com_google_absl//absl/strings",
202203
"@com_google_absl//absl/types:span",
203204
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",

eval/compiler/flat_expr_builder_test.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "absl/container/flat_hash_map.h"
3232
#include "absl/status/status.h"
3333
#include "absl/status/status_matchers.h"
34+
#include "absl/status/statusor.h"
3435
#include "absl/strings/str_split.h"
3536
#include "absl/strings/string_view.h"
3637
#include "absl/types/span.h"
@@ -2394,10 +2395,10 @@ struct ConstantFoldingTestCase {
23942395
};
23952396

23962397
class UnknownFunctionImpl : public cel::Function {
2397-
absl::StatusOr<Value> Invoke(absl::Span<const Value> args,
2398-
const google::protobuf::DescriptorPool* absl_nonnull,
2399-
google::protobuf::MessageFactory* absl_nonnull,
2400-
google::protobuf::Arena* absl_nonnull) const override {
2398+
absl::StatusOr<Value> Invoke(
2399+
absl::Span<const Value> args, const google::protobuf::DescriptorPool* absl_nonnull,
2400+
google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull,
2401+
const cel::ActivationInterface* absl_nullable) const override {
24012402
return cel::UnknownValue();
24022403
}
24032404
};

eval/eval/function_step.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ inline absl::StatusOr<Value> Invoke(
178178
const cel::FunctionOverloadReference& overload, int64_t expr_id,
179179
absl::Span<const cel::Value> args, ExecutionFrameBase& frame) {
180180
CEL_ASSIGN_OR_RETURN(
181-
Value result,
182-
overload.implementation.Invoke(args, frame.descriptor_pool(),
183-
frame.message_factory(), frame.arena()));
181+
Value result, overload.implementation.Invoke(
182+
args, frame.descriptor_pool(), frame.message_factory(),
183+
frame.arena(), &frame.activation()));
184184

185185
if (frame.unknown_function_results_enabled() &&
186186
IsUnknownFunctionResultError(result)) {

eval/public/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ cc_library(
197197
"//common:value",
198198
"//eval/internal:interop",
199199
"//internal:status_macros",
200+
"//runtime:activation_interface",
200201
"//runtime:function",
201202
"@com_google_absl//absl/base:nullability",
202203
"@com_google_absl//absl/status",

eval/public/cel_function.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "eval/internal/interop.h"
1111
#include "eval/public/cel_value.h"
1212
#include "internal/status_macros.h"
13+
#include "runtime/activation_interface.h"
1314
#include "google/protobuf/arena.h"
1415
#include "google/protobuf/descriptor.h"
1516
#include "google/protobuf/message.h"
@@ -57,7 +58,8 @@ absl::StatusOr<Value> CelFunction::Invoke(
5758
absl::Span<const cel::Value> arguments,
5859
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
5960
google::protobuf::MessageFactory* absl_nonnull message_factory,
60-
google::protobuf::Arena* absl_nonnull arena) const {
61+
google::protobuf::Arena* absl_nonnull arena,
62+
const cel::ActivationInterface* absl_nullable activation) const {
6163
std::vector<CelValue> legacy_args;
6264
legacy_args.reserve(arguments.size());
6365

eval/public/cel_function.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class CelFunction : public ::cel::Function {
6969
absl::Span<const cel::Value> arguments,
7070
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
7171
google::protobuf::MessageFactory* absl_nonnull message_factory,
72-
google::protobuf::Arena* absl_nonnull arena) const override;
72+
google::protobuf::Arena* absl_nonnull arena,
73+
const cel::ActivationInterface* absl_nullable activation) const override;
7374

7475
// CelFunction descriptor
7576
const CelFunctionDescriptor& descriptor() const { return descriptor_; }

eval/public/cel_function_registry.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ProxyToModernCelFunction : public CelFunction {
5050
auto modern_result,
5151
implementation_->Invoke(
5252
modern_args, google::protobuf::DescriptorPool::generated_pool(),
53-
google::protobuf::MessageFactory::generated_factory(), arena));
53+
google::protobuf::MessageFactory::generated_factory(), arena, nullptr));
5454

5555
*result = cel::interop_internal::ModernValueToLegacyValueOrDie(
5656
arena, modern_result);

runtime/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,11 @@ cc_library(
500500
":function",
501501
":register_function_helper",
502502
"//common:function_descriptor",
503-
"//common:kind",
504503
"//common:value",
505504
"//internal:status_macros",
506505
"//runtime/internal:function_adapter",
507506
"@com_google_absl//absl/base:nullability",
508507
"@com_google_absl//absl/functional:any_invocable",
509-
"@com_google_absl//absl/functional:bind_front",
510508
"@com_google_absl//absl/status",
511509
"@com_google_absl//absl/status:statusor",
512510
"@com_google_absl//absl/strings",
@@ -526,10 +524,12 @@ cc_test(
526524
"//common:value",
527525
"//common:value_testing",
528526
"//internal:testing",
527+
"@com_google_absl//absl/base:nullability",
529528
"@com_google_absl//absl/status",
530529
"@com_google_absl//absl/status:statusor",
531530
"@com_google_absl//absl/strings",
532531
"@com_google_absl//absl/time",
532+
"@com_google_protobuf//:protobuf",
533533
],
534534
)
535535

runtime/activation_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class FunctionImpl : public cel::Function {
6666
public:
6767
FunctionImpl() = default;
6868

69-
absl::StatusOr<Value> Invoke(absl::Span<const Value> args,
70-
const google::protobuf::DescriptorPool* absl_nonnull,
71-
google::protobuf::MessageFactory* absl_nonnull,
72-
google::protobuf::Arena* absl_nonnull) const override {
69+
absl::StatusOr<Value> Invoke(
70+
absl::Span<const Value> args, const google::protobuf::DescriptorPool* absl_nonnull,
71+
google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull,
72+
const ActivationInterface* absl_nullable) const override {
7373
return NullValue();
7474
}
7575
};

runtime/function.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
namespace cel {
2727

28+
class ActivationInterface;
29+
2830
// Interface for extension functions.
2931
//
3032
// The host for the CEL environment may provide implementations to define custom
@@ -47,7 +49,8 @@ class Function {
4749
absl::Span<const Value> args,
4850
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
4951
google::protobuf::MessageFactory* absl_nonnull message_factory,
50-
google::protobuf::Arena* absl_nonnull arena) const = 0;
52+
google::protobuf::Arena* absl_nonnull arena,
53+
const ActivationInterface* absl_nullable activation) const = 0;
5154
};
5255

5356
} // namespace cel

0 commit comments

Comments
 (0)