diff --git a/.bazelrc b/.bazelrc index 4e4a0184c..750977061 100644 --- a/.bazelrc +++ b/.bazelrc @@ -5,3 +5,7 @@ build --java_language_version=11 # Hide Java 8 deprecation warnings. common --javacopt=-Xlint:-options + +# MacOS Fix https://github.com/protocolbuffers/protobuf/issues/16944 +build --host_cxxopt=-std=c++14 +build --cxxopt=-std=c++14 diff --git a/runtime/BUILD.bazel b/runtime/BUILD.bazel index 1ca1e59c9..6061b1ac9 100644 --- a/runtime/BUILD.bazel +++ b/runtime/BUILD.bazel @@ -105,6 +105,11 @@ java_library( exports = ["//runtime/src/main/java/dev/cel/runtime:interpreter"], ) +java_library( + name = "interpretable", + exports = ["//runtime/src/main/java/dev/cel/runtime:interpretable"], +) + java_library( name = "runtime_helpers", visibility = ["//:internal"], diff --git a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel index e4c4dbbc5..607ee4b53 100644 --- a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel @@ -506,7 +506,6 @@ java_library( java_library( name = "interpretable", srcs = INTERPRABLE_SOURCES, - visibility = ["//visibility:private"], deps = [ ":evaluation_exception", ":evaluation_listener", diff --git a/runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java b/runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java index 14ae160fa..fdf98bebf 100644 --- a/runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java +++ b/runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java @@ -17,6 +17,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -111,7 +112,8 @@ public Interpretable createInterpretable(CelAbstractSyntaxTree ast) { } @Immutable - private static final class DefaultInterpretable + @VisibleForTesting + static final class DefaultInterpretable implements Interpretable, UnknownTrackingInterpretable { private final TypeResolver typeResolver; private final RuntimeTypeProvider typeProvider; @@ -165,12 +167,38 @@ public Object evalTrackingUnknowns( Optional extends FunctionResolver> functionResolver, CelEvaluationListener listener) throws CelEvaluationException { + ExecutionFrame frame = newExecutionFrame(resolver, functionResolver, listener); + IntermediateResult internalResult = evalInternal(frame, ast.getExpr()); + return internalResult.value(); + } + + /** + * Evaluates this interpretable and returns the resulting execution frame populated with evaluation state. + * This method is specifically designed for testing the interpreter's internal invariants. + * + *
Do not expose to public. This method is strictly for internal testing purposes only.
+ */
+ @VisibleForTesting
+ ExecutionFrame populateExecutionFrame(ExecutionFrame frame) throws CelEvaluationException {
+ evalInternal(frame, ast.getExpr());
+
+ return frame;
+ }
+
+ @VisibleForTesting
+ ExecutionFrame newTestExecutionFrame(GlobalResolver resolver) throws CelEvaluationException {
+ return newExecutionFrame(RuntimeUnknownResolver.fromResolver(resolver), Optional.empty(), CelEvaluationListener.noOpListener());
+ }
+
+
+ private ExecutionFrame newExecutionFrame(
+ RuntimeUnknownResolver resolver,
+ Optional extends FunctionResolver> functionResolver,
+ CelEvaluationListener listener) throws CelEvaluationException {
int comprehensionMaxIterations =
celOptions.enableComprehension() ? celOptions.comprehensionMaxIterations() : 0;
- ExecutionFrame frame =
+ return
new ExecutionFrame(listener, resolver, functionResolver, comprehensionMaxIterations);
- IntermediateResult internalResult = evalInternal(frame, ast.getExpr());
- return internalResult.value();
}
private IntermediateResult evalInternal(ExecutionFrame frame, CelExpr expr)
@@ -916,6 +944,7 @@ private IntermediateResult evalComprehension(
iterVar, IntermediateResult.create(iterAttr, RuntimeHelpers.maybeAdaptPrimitive(elem)));
loopVars.put(accuVar, accuValue);
+ System.out.println("Push scope");
frame.pushScope(Collections.unmodifiableMap(loopVars));
IntermediateResult evalObject = evalBooleanStrict(frame, compre.loopCondition());
if (!isUnknownValue(evalObject.value()) && !(boolean) evalObject.value()) {
@@ -927,8 +956,13 @@ private IntermediateResult evalComprehension(
}
frame.pushScope(Collections.singletonMap(accuVar, accuValue));
- IntermediateResult result = evalInternal(frame, compre.result());
- frame.popScope();
+ IntermediateResult result;
+ try {
+ result = evalInternal(frame, compre.result());
+ }
+ finally {
+ frame.popScope();
+ }
return result;
}
@@ -975,14 +1009,17 @@ private LazyExpression(CelExpr celExpr) {
}
}
+
/** This class tracks the state meaningful to a single evaluation pass. */
- private static class ExecutionFrame {
+ static class ExecutionFrame {
private final CelEvaluationListener evaluationListener;
private final int maxIterations;
private final ArrayDeque