Skip to content
Open
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
40 changes: 34 additions & 6 deletions src/main/java/dev/trufflecel/EvalContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ Object getRootBoundary(String name) {
}

Object getRoot(String name) {
return rootBinding(name.startsWith(".") ? absoluteName(name) : name);
boolean absolute = name.startsWith(".");
return rootBinding(absolute ? absoluteName(name) : name, absolute);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
Expand All @@ -260,26 +261,53 @@ Object getLocal(int slot) {
return readStored(((Scoped) this).scopes[slot * 2 + 1]);
}

private Object rootBinding(String name) {
private Object rootBinding(String name, boolean absolute) {
Attribute path = evaluation == null || evaluation.partial == null ? null : Attribute.of(name);
Object override = attributeRead(path, false);
if (override != PartialState.NONE) return override;
// Program normalizes host nulls to CelNull, so null denotes an absent binding.
Object value;
boolean walk = !absolute && !environment.container.isEmpty();
if (activation instanceof Object[] arguments) {
Object binding = arguments[0];
value =
binding instanceof String single
? single.equals(name) ? arguments[1] : null
: lookup(binding, name);
? (walk ? matchesRoot(single, name) : single.equals(name)) ? arguments[1] : null
: walk ? resolveRoot(binding, name) : lookup(binding, name);
} else if (this instanceof Scoped scoped && scoped.singleName != null)
value = scoped.singleName.equals(name) ? activation : null;
else value = lookup(activation, name);
value =
(walk ? matchesRoot(scoped.singleName, name) : scoped.singleName.equals(name))
? activation
: null;
else value = walk ? resolveRoot(activation, name) : lookup(activation, name);
if (value != null) return value;
if (path != null && evaluation.partial.containsUnknown(path)) return PartialState.MISSING;
throw undeclared(name);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
private Object resolveRoot(Object activation, String name) {
String prefix = environment.container;
while (!prefix.isEmpty()) {
Object value = lookup(activation, prefix + "." + name);
if (value != null) return value;
int dot = prefix.lastIndexOf('.');
prefix = dot < 0 ? "" : prefix.substring(0, dot);
}
return lookup(activation, name);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
private boolean matchesRoot(String binding, String name) {
String prefix = environment.container;
while (!prefix.isEmpty()) {
if (binding.equals(prefix + "." + name)) return true;
int dot = prefix.lastIndexOf('.');
prefix = dot < 0 ? "" : prefix.substring(0, dot);
}
return binding.equals(name);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
private static CelException undeclared(String name) {
return Values.error(CelException.Code.UNDECLARED, "no value for variable: " + name);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/dev/trufflecel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ java_test(
],
)

java_test(
name = "containerResolution",
srcs = ["ContainerResolutionTest.java"],
test_class = "dev.trufflecel.ContainerResolutionTest",
deps = [
":test_support",
"//:trufflecel",
"//:trufflecel-protobuf",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:junit_junit",
],
)

java_test(
name = "encoder",
srcs = ["EncoderTest.java"],
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/dev/trufflecel/ContainerResolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dev.trufflecel;

import static dev.trufflecel.CanonicalAst.*;
import static org.junit.Assert.*;

import com.google.protobuf.*;
import java.util.*;
import org.junit.Test;

/** Runtime container-qualified binding resolution for unchecked/parsed evaluation. */
public final class ContainerResolutionTest {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in edc1165: dropped the added @TruffleBoundary from rootBinding so the compiled root-resolution fast path matches the pre-PR allocation profile (restores the benchmarks:compiled budget); the new container-walk helpers stay @TruffleBoundary. Also added NullMarked to ContainerResolutionTest (+ @maven//:org_jspecify_jspecify dep) to satisfy RequireExplicitNullMarking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in 0a257a0: reverted the NullMarked addition. It can't land cleanly here — strict deps requires a direct @maven//:org_jspecify_jspecify dep, but Gazelle auto-removes that dep as transitively-satisfied, so the two checks conflict. The RequireExplicitNullMarking finding is WARN-level (code-scanning), not a CI failure (trufflecheck passed without the annotation), so a human can decide later whether to adopt NullMarked package-wide.

private static Program parsedProgram(String source, String container) {
Environment environment = Cel.newBuilder().container(container).inferVariables(true).build();
Message parsed = CanonicalAst.parse(environment, source);
return CanonicalAst.program(environment, parsed);
}

@Test
public void containerQualifiedBindingWinsOverBareForParsedIdent() {
Program program = parsedProgram("y", "x");
assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true, "y", "false")));
assertEquals(Boolean.TRUE, program.evaluate(Map.of("x.y", true, "y", "false")));
}

@Test
public void containerQualifiedBindingAloneResolvesForParsedIdent() {
Program program = parsedProgram("y", "x");
assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true)));
}

@Test
public void bareBindingStillResolvesWhenNoContainerQualifiedKeyExists() {
Program program = parsedProgram("y", "x");
assertEquals("false", program.evaluateInternal(Map.of("y", "false")));
}

@Test
public void genuinelyMissingIdentifierStillErrorsAfterContainerWalk() {
Program program = parsedProgram("y", "x");
assertEquals(
CelException.Code.UNDECLARED,
assertThrows(CelException.class, () -> program.evaluateInternal(Map.of("z", 1))).code());
}

@Test
public void nestedContainerWalksInnermostFirst() {
Program program = parsedProgram("y", "a.b");
assertEquals(1L, program.evaluateInternal(Map.of("a.b.y", 1L, "a.y", 2L, "y", 3L)));
assertEquals(2L, program.evaluateInternal(Map.of("a.y", 2L, "y", 3L)));
assertEquals(3L, program.evaluateInternal(Map.of("y", 3L)));
assertEquals(1L, program.evaluateInternal(Map.of("a.b.y", 1L, "y", 3L)));
}

@Test
public void multipleRootsEachResolveThroughContainer() {
Program program = parsedProgram("y + z", "x");
assertEquals(3L, program.evaluateInternal(Map.of("x.y", 1L, "x.z", 2L, "y", 10L, "z", 20L)));
}

@Test
public void absoluteReferenceSkipsContainerWalk() {
Program program = parsedProgram(".y", "x");
assertEquals(2L, program.evaluateInternal(Map.of("x.y", 1L, "y", 2L)));
}

@Test
public void checkedPathRemainsUnchangedByContainerResolution() {
Environment env =
Cel.newBuilder()
.container("x")
.variable("x.y", CelType.BOOL)
.variable("y", CelType.STRING)
.build();
Message parsed = CanonicalAst.parse(env, "y");
Message checked = CanonicalAst.check(env, parsed);
Program program = CanonicalAst.program(env, checked);
assertEquals(Boolean.TRUE, program.evaluateInternal(Map.of("x.y", true, "y", "false")));
}

@Test
public void emptyContainerFallsBackToBareNameOnly() {
Program program = parsedProgram("y", "");
assertEquals("false", program.evaluateInternal(Map.of("y", "false")));
assertEquals(
CelException.Code.UNDECLARED,
assertThrows(CelException.class, () -> program.evaluateInternal(Map.of("x.y", true)))
.code());
}

@Test
public void singleEntryActivationResolvesContainerQualifiedKey() {
Program program = parsedProgram("y", "x");
assertEquals(Boolean.TRUE, program.evaluate(Activation.snapshot(Map.of("x.y", true))));
assertEquals("false", program.evaluate(Activation.snapshot(Map.of("y", "false"))));
}

@Test
public void singleEntryActivationMissingIdentifierErrorsAfterContainerWalk() {
Program program = parsedProgram("y", "x");
// Object[] fast path: a single String binding that matches neither the container-qualified
// name nor the bare name must fall through to UNDECLARED after the container walk.
assertEquals(
CelException.Code.UNDECLARED,
assertThrows(
CelException.class, () -> program.evaluate(Activation.snapshot(Map.of("z", 1))))
.code());
// Scoped single-name path (options-driven evaluation): same container walk, no match.
assertEquals(
CelException.Code.UNDECLARED,
assertThrows(
CelException.class,
() ->
program.evaluate(
Activation.snapshot(Map.of("z", 1)),
EvaluationOptions.newBuilder().build()))
.code());
}
}
47 changes: 47 additions & 0 deletions tools/conformance/ConformanceServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,53 @@ public void checkFallsBackToGeneratedIdWhenOverloadIdMissing() {
assertTrue(has(response, "checked_expr"));
}

@Test
public void evalParsedExprResolvesContainerQualifiedBindings() {
var service = new ConformanceServer();
Message parsed =
nested(service.parse(message(P + "ParseRequest", "cel_source", "y")), "parsed_expr");
Message bindingXY =
message("cel.expr.ExprValue", "value", message("cel.expr.Value", "bool_value", true));
Message bindingY =
message("cel.expr.ExprValue", "value", message("cel.expr.Value", "string_value", "false"));
var both = new LinkedHashMap<String, Message>();
both.put("x.y", bindingXY);
both.put("y", bindingY);
Message bothResult =
nested(
service.eval(
message(
P + "EvalRequest", "parsed_expr", parsed, "container", "x", "bindings", both)),
"result");
assertEquals(true, field(nested(bothResult, "value"), "bool_value"));
Message qualifiedOnly =
nested(
service.eval(
message(
P + "EvalRequest",
"parsed_expr",
parsed,
"container",
"x",
"bindings",
Map.of("x.y", bindingXY))),
"result");
assertEquals(true, field(nested(qualifiedOnly, "value"), "bool_value"));
Message bareOnly =
nested(
service.eval(
message(
P + "EvalRequest",
"parsed_expr",
parsed,
"container",
"x",
"bindings",
Map.of("y", bindingY))),
"result");
assertEquals("false", field(nested(bareOnly, "value"), "string_value"));
}

@SuppressWarnings("unchecked")
private static List<String> overloadIds(Message checked) {
var ids = new ArrayList<String>();
Expand Down