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
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public Object toRuntimeValue(Object value) {
.map(this::toRuntimeValue)
.map(OptionalValue::create)
.orElse(OptionalValue.EMPTY);
} else if (value instanceof Exception) {
return ErrorValue.create((Exception) value);
}

return normalizePrimitive(value);
Expand Down
6 changes: 4 additions & 2 deletions common/src/main/java/dev/cel/common/values/ErrorValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"Immutable") // Exception is technically not immutable as the stacktrace is malleable.
public abstract class ErrorValue extends CelValue {

public abstract long exprId();

@Override
public abstract Exception value();

Expand All @@ -46,7 +48,7 @@ public CelType celType() {
return SimpleType.ERROR;
}

public static ErrorValue create(Exception value) {
return new AutoValue_ErrorValue(value);
public static ErrorValue create(long exprId, Exception value) {
return new AutoValue_ErrorValue(exprId, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ public void toRuntimeValue_optionalValue() {
assertThat(optionalValue).isEqualTo(OptionalValue.create("test"));
}

@Test
public void toRuntimeValue_errorValue() {
IllegalArgumentException e = new IllegalArgumentException("error");

ErrorValue errorValue = (ErrorValue) CEL_VALUE_CONVERTER.toRuntimeValue(e);

assertThat(errorValue.value()).isEqualTo(e);
}

@Test
@SuppressWarnings("unchecked") // Test only
public void unwrap_optionalValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ public class ErrorValueTest {
@Test
public void errorValue_construct() {
IllegalArgumentException exception = new IllegalArgumentException("test");
ErrorValue opaqueValue = ErrorValue.create(exception);
ErrorValue opaqueValue = ErrorValue.create(0L, exception);

assertThat(opaqueValue.value()).isEqualTo(exception);
assertThat(opaqueValue.isZeroValue()).isFalse();
}

@Test
public void create_nullValue_throws() {
assertThrows(NullPointerException.class, () -> ErrorValue.create(null));
assertThrows(NullPointerException.class, () -> ErrorValue.create(0L, null));
}

@Test
public void celTypeTest() {
ErrorValue value = ErrorValue.create(new IllegalArgumentException("test"));
ErrorValue value = ErrorValue.create(0L, new IllegalArgumentException("test"));

assertThat(value.celType()).isEqualTo(SimpleType.ERROR);
}
Expand Down
8 changes: 7 additions & 1 deletion runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ java_library(
exports = [
":evaluation_exception",
":late_function_binding",
":metadata",
"//runtime/src/main/java/dev/cel/runtime",
"//runtime/src/main/java/dev/cel/runtime:descriptor_message_provider",
"//runtime/src/main/java/dev/cel/runtime:function_overload",
"//runtime/src/main/java/dev/cel/runtime:metadata",
"//runtime/src/main/java/dev/cel/runtime:runtime_type_provider",
],
)
Expand Down Expand Up @@ -249,3 +249,9 @@ cel_android_library(
name = "program_android",
exports = ["//runtime/src/main/java/dev/cel/runtime:program_android"],
)

java_library(
name = "metadata",
visibility = ["//:internal"],
exports = ["//runtime/src/main/java/dev/cel/runtime:metadata"],
)
50 changes: 50 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 @@ -14,6 +14,7 @@ java_library(
],
deps = [
":attribute",
":error_metadata",
":eval_and",
":eval_attribute",
":eval_conditional",
Expand All @@ -25,6 +26,7 @@ java_library(
":eval_unary",
":eval_var_args_call",
":eval_zero_arity",
":planned_interpretable",
":planned_program",
"//:auto_value",
"//common:cel_ast",
Expand All @@ -51,6 +53,9 @@ java_library(
name = "planned_program",
srcs = ["PlannedProgram.java"],
deps = [
":error_metadata",
":planned_interpretable",
":strict_error_exception",
"//:auto_value",
"//common:runtime_exception",
"//common/values",
Expand All @@ -68,6 +73,7 @@ java_library(
name = "eval_const",
srcs = ["EvalConstant.java"],
deps = [
":planned_interpretable",
"//common/values",
"//common/values:cel_byte_string",
"//runtime:evaluation_listener",
Expand Down Expand Up @@ -99,6 +105,7 @@ java_library(
srcs = ["EvalAttribute.java"],
deps = [
":attribute",
":planned_interpretable",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
Expand All @@ -111,6 +118,7 @@ java_library(
name = "eval_zero_arity",
srcs = ["EvalZeroArity.java"],
deps = [
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -123,6 +131,8 @@ java_library(
name = "eval_unary",
srcs = ["EvalUnary.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -135,6 +145,8 @@ java_library(
name = "eval_var_args_call",
srcs = ["EvalVarArgsCall.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -148,6 +160,7 @@ java_library(
srcs = ["EvalOr.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//common/values",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -161,6 +174,7 @@ java_library(
srcs = ["EvalAnd.java"],
deps = [
":eval_helpers",
":planned_interpretable",
"//common/values",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -173,6 +187,7 @@ java_library(
name = "eval_conditional",
srcs = ["EvalConditional.java"],
deps = [
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -185,6 +200,7 @@ java_library(
name = "eval_create_struct",
srcs = ["EvalCreateStruct.java"],
deps = [
":planned_interpretable",
"//common/types",
"//common/values",
"//common/values:cel_value_provider",
Expand All @@ -201,6 +217,7 @@ java_library(
name = "eval_create_list",
srcs = ["EvalCreateList.java"],
deps = [
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -214,6 +231,7 @@ java_library(
name = "eval_create_map",
srcs = ["EvalCreateMap.java"],
deps = [
":planned_interpretable",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
Expand All @@ -227,7 +245,39 @@ java_library(
name = "eval_helpers",
srcs = ["EvalHelpers.java"],
deps = [
":planned_interpretable",
":strict_error_exception",
"//common:error_codes",
"//common:runtime_exception",
"//common/values",
"//runtime:interpretable",
],
)

java_library(
name = "strict_error_exception",
srcs = ["StrictErrorException.java"],
deps = [
"//common:error_codes",
"//common:runtime_exception",
],
)

java_library(
name = "error_metadata",
srcs = ["ErrorMetadata.java"],
deps = [
"//runtime:metadata",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "planned_interpretable",
srcs = ["PlannedInterpretable.java"],
deps = [
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)
52 changes: 52 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/ErrorMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import dev.cel.runtime.Metadata;

@Immutable
final class ErrorMetadata implements Metadata {

private final ImmutableMap<Long, Integer> exprIdToPositionMap;
private final String location;

@Override
public String getLocation() {
return location;
}

@Override
public int getPosition(long exprId) {
return exprIdToPositionMap.getOrDefault(exprId, 0);
}

@Override
public boolean hasPosition(long exprId) {
return exprIdToPositionMap.containsKey(exprId);
}

static ErrorMetadata create(ImmutableMap<Long, Integer> exprIdToPositionMap, String location) {
return new ErrorMetadata(exprIdToPositionMap, location);
}

private ErrorMetadata(ImmutableMap<Long, Integer> exprIdToPositionMap, String location) {
this.exprIdToPositionMap = checkNotNull(exprIdToPositionMap);
this.location = checkNotNull(location);
}
}
14 changes: 7 additions & 7 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalAnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalAnd implements Interpretable {
final class EvalAnd extends PlannedInterpretable {

@SuppressWarnings("Immutable")
private final Interpretable[] args;
private final PlannedInterpretable[] args;

@Override
public Object eval(GlobalResolver resolver) {
ErrorValue errorValue = null;
for (Interpretable arg : args) {
for (PlannedInterpretable arg : args) {
Object argVal = evalNonstrictly(arg, resolver);
if (argVal instanceof Boolean) {
// Short-circuit on false
Expand Down Expand Up @@ -75,11 +74,12 @@ public Object eval(
throw new UnsupportedOperationException("Not yet supported");
}

static EvalAnd create(Interpretable[] args) {
return new EvalAnd(args);
static EvalAnd create(long exprId, PlannedInterpretable[] args) {
return new EvalAnd(exprId, args);
}

private EvalAnd(Interpretable[] args) {
private EvalAnd(long exprId, PlannedInterpretable[] args) {
super(exprId);
Preconditions.checkArgument(args.length == 2);
this.args = args;
}
Expand Down
11 changes: 5 additions & 6 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

@Immutable
final class EvalAttribute implements Interpretable {
final class EvalAttribute extends PlannedInterpretable {

private final Attribute attr;

Expand All @@ -47,15 +46,15 @@ public Object eval(
GlobalResolver resolver,
CelFunctionResolver lateBoundFunctionResolver,
CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

static EvalAttribute create(Attribute attr) {
return new EvalAttribute(attr);
static EvalAttribute create(long exprId, Attribute attr) {
return new EvalAttribute(exprId, attr);
}

private EvalAttribute(Attribute attr) {
private EvalAttribute(long exprId, Attribute attr) {
super(exprId);
this.attr = attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalConditional implements Interpretable {
final class EvalConditional extends PlannedInterpretable {

@SuppressWarnings("Immutable")
private final Interpretable[] args;
Expand Down Expand Up @@ -67,11 +67,12 @@ public Object eval(
throw new UnsupportedOperationException("Not yet supported");
}

static EvalConditional create(Interpretable[] args) {
return new EvalConditional(args);
static EvalConditional create(long exprId, Interpretable[] args) {
return new EvalConditional(exprId, args);
}

private EvalConditional(Interpretable[] args) {
private EvalConditional(long exprId, Interpretable[] args) {
super(exprId);
Preconditions.checkArgument(args.length == 3);
this.args = args;
}
Expand Down
Loading
Loading