Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ java_library(
":resolved_overload",
"//:auto_value",
"//common:error_codes",
"//common/annotations",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand All @@ -146,6 +147,7 @@ cel_android_library(
":resolved_overload_android",
"//:auto_value",
"//common:error_codes",
"//common/annotations",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
Expand Down Expand Up @@ -1172,6 +1174,7 @@ java_library(
":function_overload",
":unknown_attributes",
"//:auto_value",
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand All @@ -1186,6 +1189,7 @@ cel_android_library(
":function_overload_android",
":unknown_attributes_android",
"//:auto_value",
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
Expand Down
15 changes: 12 additions & 3 deletions runtime/src/main/java/dev/cel/runtime/CelResolvedOverload.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;
import java.util.List;
import java.util.Map;

Expand All @@ -26,7 +27,8 @@
*/
@AutoValue
@Immutable
abstract class CelResolvedOverload {
@Internal
public abstract class CelResolvedOverload {

/** The overload id of the function. */
public abstract String getOverloadId();
Expand Down Expand Up @@ -78,7 +80,14 @@ public static CelResolvedOverload of(
* Returns true if the overload's expected argument types match the types of the given arguments.
*/
boolean canHandle(Object[] arguments) {
ImmutableList<Class<?>> parameterTypes = getParameterTypes();
return canHandle(arguments, getParameterTypes(), isStrict());
}

/**
* Returns true if the overload's expected argument types match the types of the given arguments.
*/
public static boolean canHandle(
Object[] arguments, ImmutableList<Class<?>> parameterTypes, boolean isStrict) {
if (parameterTypes.size() != arguments.length) {
return false;
}
Expand All @@ -96,7 +105,7 @@ boolean canHandle(Object[] arguments) {

if (arg instanceof Exception || arg instanceof CelUnknownSet) {
// Only non-strict functions can accept errors/unknowns as arguments to a function
if (!isStrict()) {
if (!isStrict) {
// Skip assignability check below, but continue to validate remaining args
continue;
}
Expand Down
25 changes: 19 additions & 6 deletions runtime/src/main/java/dev/cel/runtime/DefaultDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package dev.cel.runtime;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.auto.value.AutoBuilder;
Expand All @@ -22,17 +23,27 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelErrorCode;
import dev.cel.common.annotations.Internal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/** Default implementation of dispatcher. */
/**
* Default implementation of dispatcher.
*
* <p>CEL Library Internals. Do Not Use.
*/
@Immutable
final class DefaultDispatcher implements CelFunctionResolver {
@Internal
public final class DefaultDispatcher implements CelFunctionResolver {

private final ImmutableMap<String, CelResolvedOverload> overloads;

public Optional<CelResolvedOverload> findOverload(String functionName) {
return Optional.ofNullable(overloads.get(functionName));
}

@Override
public Optional<CelResolvedOverload> findOverloadMatchingArgs(
String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException {
Expand Down Expand Up @@ -101,24 +112,26 @@ Optional<CelResolvedOverload> findSingleNonStrictOverload(List<String> overloadI
return Optional.empty();
}

static Builder newBuilder() {
public static Builder newBuilder() {
return new AutoBuilder_DefaultDispatcher_Builder();
}

/** Builder for {@link DefaultDispatcher}. */
@AutoBuilder(ofClass = DefaultDispatcher.class)
abstract static class Builder {
public abstract static class Builder {

abstract ImmutableMap<String, CelResolvedOverload> overloads();

abstract ImmutableMap.Builder<String, CelResolvedOverload> overloadsBuilder();

@CanIgnoreReturnValue
Builder addOverload(
public Builder addOverload(
String overloadId,
List<Class<?>> argTypes,
boolean isStrict,
CelFunctionOverload overload) {
checkNotNull(overloadId);
checkArgument(!overloadId.isEmpty(), "Overload ID cannot be empty.");
checkNotNull(argTypes);
checkNotNull(overload);

Expand All @@ -127,7 +140,7 @@ Builder addOverload(
return this;
}

abstract DefaultDispatcher build();
public abstract DefaultDispatcher build();
}

DefaultDispatcher(ImmutableMap<String, CelResolvedOverload> overloads) {
Expand Down
42 changes: 42 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ java_library(
":eval_create_list",
":eval_create_map",
":eval_create_struct",
":eval_unary",
":eval_var_args_call",
":eval_zero_arity",
":planned_program",
"//:auto_value",
"//common:cel_ast",
Expand All @@ -28,10 +31,12 @@ java_library(
"//common/types",
"//common/types:type_providers",
"//common/values:cel_value_provider",
"//runtime:dispatcher",
"//runtime:evaluation_exception",
"//runtime:evaluation_exception_builder",
"//runtime:interpretable",
"//runtime:program",
"//runtime:resolved_overload",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand All @@ -45,6 +50,7 @@ java_library(
"//:auto_value",
"//runtime:activation",
"//runtime:evaluation_exception",
"//runtime:evaluation_exception_builder",
"//runtime:function_resolver",
"//runtime:interpretable",
"//runtime:program",
Expand Down Expand Up @@ -95,6 +101,42 @@ java_library(
],
)

java_library(
name = "eval_zero_arity",
srcs = ["EvalZeroArity.java"],
deps = [
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"//runtime:resolved_overload",
],
)

java_library(
name = "eval_unary",
srcs = ["EvalUnary.java"],
deps = [
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"//runtime:resolved_overload",
],
)

java_library(
name = "eval_var_args_call",
srcs = ["EvalVarArgsCall.java"],
deps = [
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"//runtime:resolved_overload",
],
)

java_library(
name = "eval_create_struct",
srcs = ["EvalCreateStruct.java"],
Expand Down
66 changes: 66 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalUnary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2025 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.

package dev.cel.runtime.planner;

import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.CelResolvedOverload;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalUnary implements Interpretable {

private final CelResolvedOverload resolvedOverload;
private final Interpretable arg;

@Override
public Object eval(GlobalResolver resolver) throws CelEvaluationException {
Object argVal = arg.eval(resolver);
Object[] arguments = new Object[] {argVal};

return resolvedOverload.getDefinition().apply(arguments);
}

@Override
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(
GlobalResolver resolver,
CelFunctionResolver lateBoundFunctionResolver,
CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

static EvalUnary create(CelResolvedOverload resolvedOverload, Interpretable arg) {
return new EvalUnary(resolvedOverload, arg);
}

private EvalUnary(CelResolvedOverload resolvedOverload, Interpretable arg) {
this.resolvedOverload = resolvedOverload;
this.arg = arg;
}
}
70 changes: 70 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalVarArgsCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2025 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.

package dev.cel.runtime.planner;

import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.CelResolvedOverload;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

@SuppressWarnings("Immutable")
final class EvalVarArgsCall implements Interpretable {

private final CelResolvedOverload resolvedOverload;
private final Interpretable[] args;

@Override
public Object eval(GlobalResolver resolver) throws CelEvaluationException {
Object[] argVals = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Interpretable arg = args[i];
argVals[i] = arg.eval(resolver);
}

return resolvedOverload.getDefinition().apply(argVals);
}

@Override
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(
GlobalResolver resolver,
CelFunctionResolver lateBoundFunctionResolver,
CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

static EvalVarArgsCall create(CelResolvedOverload resolvedOverload, Interpretable[] args) {
return new EvalVarArgsCall(resolvedOverload, args);
}

private EvalVarArgsCall(CelResolvedOverload resolvedOverload, Interpretable[] args) {
this.resolvedOverload = resolvedOverload;
this.args = args;
}
}
Loading
Loading