Skip to content

Commit 7022451

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Expose EnvRuntime::RegisterExtensionFunctions API
PiperOrigin-RevId: 896158131
1 parent 1b2a841 commit 7022451

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

env/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ cc_library(
8181
"//runtime:runtime_builder_factory",
8282
"//runtime:runtime_options",
8383
"//runtime:standard_functions",
84+
"@com_google_absl//absl/functional:any_invocable",
85+
"@com_google_absl//absl/status",
8486
"@com_google_absl//absl/status:statusor",
87+
"@com_google_absl//absl/strings:string_view",
8588
"@com_google_protobuf//:protobuf",
8689
],
8790
)
@@ -236,10 +239,14 @@ cc_test(
236239
"//common:source",
237240
"//common:value",
238241
"//compiler",
242+
"//extensions:math_ext",
243+
"//internal:status_macros",
239244
"//internal:testing",
240245
"//internal:testing_descriptor_pool",
241246
"//runtime",
242247
"//runtime:activation",
248+
"//runtime:runtime_builder",
249+
"//runtime:runtime_options",
243250
"@com_google_absl//absl/status",
244251
"@com_google_absl//absl/status:statusor",
245252
"@com_google_protobuf//:protobuf",

env/config.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ absl::Status Config::AddExtensionConfig(std::string name, int version) {
5858
if (extension_config.version == version) {
5959
return absl::OkStatus();
6060
}
61+
std::string version_str;
62+
if (version == ExtensionConfig::kLatest) {
63+
version_str = "'latest'";
64+
} else {
65+
version_str = absl::StrCat(version);
66+
}
6167
return absl::AlreadyExistsError(absl::StrCat(
6268
"Extension '", name, "' version ", extension_config.version,
63-
" is already included. Cannot also include version ", version));
69+
" is already included. Cannot also include version ", version_str));
6470
}
6571
}
6672
extension_configs_.push_back(

env/env_runtime.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#include <utility>
1919
#include <vector>
2020

21+
#include "absl/functional/any_invocable.h"
22+
#include "absl/status/status.h"
2123
#include "absl/status/statusor.h"
24+
#include "absl/strings/string_view.h"
2225
#include "env/config.h"
2326
#include "internal/status_macros.h"
2427
#include "runtime/runtime.h"
@@ -29,6 +32,15 @@
2932

3033
namespace cel {
3134

35+
void EnvRuntime::RegisterExtensionFunctions(
36+
absl::string_view name, absl::string_view alias, int version,
37+
absl::AnyInvocable<absl::Status(RuntimeBuilder&, const RuntimeOptions&)
38+
const>
39+
function_registration_callback) {
40+
extension_registry_.AddFunctionRegistration(
41+
name, alias, version, std::move(function_registration_callback));
42+
}
43+
3244
absl::StatusOr<RuntimeBuilder> EnvRuntime::CreateRuntimeBuilder() {
3345
const std::vector<Config::ExtensionConfig>& extension_configs =
3446
config_.GetExtensionConfigs();

env/env_runtime.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#include <memory>
1919
#include <utility>
2020

21+
#include "absl/functional/any_invocable.h"
22+
#include "absl/status/status.h"
2123
#include "absl/status/statusor.h"
24+
#include "absl/strings/string_view.h"
2225
#include "env/config.h"
2326
#include "env/internal/runtime_ext_registry.h"
2427
#include "runtime/runtime.h"
@@ -41,6 +44,15 @@ namespace cel {
4144
// compilation. This ensures consistency between compilation and runtime.
4245
class EnvRuntime {
4346
public:
47+
// Registers a function registration callback for an extension. The callback
48+
// is invoked when a runtime is created, if the corresponding functions are
49+
// enabled in the runtime config.
50+
void RegisterExtensionFunctions(
51+
absl::string_view name, absl::string_view alias, int version,
52+
absl::AnyInvocable<absl::Status(RuntimeBuilder&, const RuntimeOptions&)
53+
const>
54+
function_registration_callback);
55+
4456
void SetDescriptorPool(
4557
std::shared_ptr<const google::protobuf::DescriptorPool> descriptor_pool) {
4658
descriptor_pool_ = std::move(descriptor_pool);

env/env_runtime_test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
#include "env/env_std_extensions.h"
3232
#include "env/env_yaml.h"
3333
#include "env/runtime_std_extensions.h"
34+
#include "extensions/math_ext.h"
3435
#include "internal/testing.h"
3536
#include "internal/testing_descriptor_pool.h"
3637
#include "runtime/activation.h"
3738
#include "runtime/runtime.h"
39+
#include "runtime/runtime_builder.h"
40+
#include "runtime/runtime_options.h"
3841
#include "google/protobuf/arena.h"
3942

4043
namespace cel {
@@ -156,5 +159,41 @@ std::vector<TestCase> GetEnvRuntimeTestCases() {
156159
INSTANTIATE_TEST_SUITE_P(EnvRuntimeTest, EnvRuntimeTest,
157160
ValuesIn(GetEnvRuntimeTestCases()));
158161

162+
TEST(EnvRuntimeTest, RegisterExtensionFunctions) {
163+
auto descriptor_pool = cel::internal::GetSharedTestingDescriptorPool();
164+
Config config;
165+
ASSERT_THAT(config.AddExtensionConfig("math", 2), IsOk());
166+
167+
Env env;
168+
env.SetDescriptorPool(descriptor_pool);
169+
RegisterStandardExtensions(env);
170+
env.SetConfig(config);
171+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Compiler> compiler, env.NewCompiler());
172+
ASSERT_OK_AND_ASSIGN(ValidationResult result,
173+
compiler->Compile("math.sqrt(4) == 2.0"));
174+
EXPECT_THAT(result.GetIssues(), IsEmpty()) << result.FormatError();
175+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Ast> ast, result.ReleaseAst());
176+
177+
EnvRuntime env_runtime;
178+
env_runtime.SetDescriptorPool(descriptor_pool);
179+
env_runtime.RegisterExtensionFunctions(
180+
"cel.lib.math", "math", 2,
181+
[](cel::RuntimeBuilder& runtime_builder,
182+
const cel::RuntimeOptions& opts) -> absl::Status {
183+
return cel::extensions::RegisterMathExtensionFunctions(
184+
runtime_builder.function_registry(), opts, 2);
185+
});
186+
env_runtime.SetConfig(config);
187+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Runtime> runtime,
188+
env_runtime.NewRuntime());
189+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Program> program,
190+
runtime->CreateProgram(std::move(ast)));
191+
ASSERT_NE(program, nullptr);
192+
193+
google::protobuf::Arena arena;
194+
Activation activation;
195+
ASSERT_OK_AND_ASSIGN(Value value, program->Evaluate(&arena, activation));
196+
EXPECT_TRUE(value.GetBool());
197+
}
159198
} // namespace
160199
} // namespace cel

0 commit comments

Comments
 (0)