Coverage Report

Created: 2026-05-27 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/runtime/function.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_COMMON_FUNCTION_H_
16
#define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_H_
17
18
#include "absl/base/attributes.h"
19
#include "absl/base/nullability.h"
20
#include "absl/status/statusor.h"
21
#include "absl/types/span.h"
22
#include "common/value.h"
23
#include "google/protobuf/arena.h"
24
#include "google/protobuf/descriptor.h"
25
#include "google/protobuf/message.h"
26
27
namespace cel {
28
29
class EmbedderContext;
30
31
// Interface for extension functions.
32
//
33
// The host for the CEL environment may provide implementations to define custom
34
// extension functions.
35
//
36
// The runtime expects functions to be deterministic and side-effect free.
37
class Function {
38
 public:
39
2.55M
  virtual ~Function() = default;
40
41
  // Context for the function invocation.
42
  //
43
  // Collects evaluation state that may be needed for the function to operate.
44
  //
45
  // The function implementation should not retain a reference to the context
46
  // object beyond the duration of the function call or modify the InvokeContext
47
  // itself.
48
  class InvokeContext {
49
   public:
50
    InvokeContext(
51
        const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
52
        google::protobuf::MessageFactory* absl_nonnull message_factory,
53
        google::protobuf::Arena* absl_nonnull arena,
54
        const EmbedderContext* absl_nullable embedder_context = nullptr)
55
35.3k
        : descriptor_pool_(descriptor_pool),
56
35.3k
          message_factory_(message_factory),
57
35.3k
          arena_(arena),
58
35.3k
          embedder_context_(embedder_context) {}
59
6.21k
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool() const {
60
6.21k
      return descriptor_pool_;
61
6.21k
    }
62
63
6.21k
    google::protobuf::MessageFactory* absl_nonnull message_factory() const {
64
6.21k
      return message_factory_;
65
6.21k
    }
66
67
6.21k
    google::protobuf::Arena* absl_nonnull arena() const { return arena_; }
68
69
0
    const EmbedderContext* absl_nullable embedder_context() const {
70
0
      return embedder_context_;
71
0
    }
72
73
    void set_embedder_context(
74
0
        const EmbedderContext* absl_nullable embedder_context) {
75
0
      embedder_context_ = embedder_context;
76
0
    }
77
78
   private:
79
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool_;
80
    google::protobuf::MessageFactory* absl_nonnull message_factory_;
81
    google::protobuf::Arena* absl_nonnull arena_;
82
    const EmbedderContext* absl_nullable embedder_context_;
83
  };
84
85
  ABSL_DEPRECATED("Use the InvokeContext overload instead.")
86
  inline absl::StatusOr<Value> Invoke(
87
      absl::Span<const Value> args,
88
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
89
      google::protobuf::MessageFactory* absl_nonnull message_factory,
90
      google::protobuf::Arena* absl_nonnull arena) const;
91
92
  // Attempt to evaluate an extension function based on the runtime arguments
93
  // during the evaluation of a CEL expression.
94
  //
95
  // A non-ok status is interpreted as an unrecoverable error in evaluation (
96
  // e.g. data corruption). This stops evaluation and is propagated immediately.
97
  //
98
  // A cel::ErrorValue typed result is considered a recoverable error and
99
  // follows CEL's logical short-circuiting behavior.
100
  virtual absl::StatusOr<Value> Invoke(absl::Span<const Value> args,
101
                                       const InvokeContext& context) const = 0;
102
};
103
104
absl::StatusOr<Value> Function::Invoke(
105
    absl::Span<const Value> args,
106
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
107
    google::protobuf::MessageFactory* absl_nonnull message_factory,
108
0
    google::protobuf::Arena* absl_nonnull arena) const {
109
0
  InvokeContext context(descriptor_pool, message_factory, arena);
110
0
  return Invoke(args, context);
111
0
}
112
113
}  // namespace cel
114
115
#endif  // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_H_