Path: redb-route/redb.Route/Expressions/BeanCallSite.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/BeanCallSite.cs ---- using System.Runtime.CompilerServices; using redb.Route.Abstractions; using redb.Route.Components.Bean; namespace redb.Route.Expressions; /// /// One bean:name.Method(args) call in a compiled expression. The expression is compiled once and shared by every /// context that evaluates it, so the bean is resolved per route context: from that context's registry, on the first /// evaluation there, with the method bound once through (the selection and /// conversion rules of the bean: URI). A weak table keeps no context alive. /// /// Synchronous only, as in Apache Camel's Simple language: an expression has no await, so a method returning a task /// is refused when it is first resolved, pointing to the bean step. Refused under : a /// template's data never runs code. /// /// internal sealed class BeanCallSite(string reference, string method, int argumentCount) { private readonly ConditionalWeakTable _resolved = new(); private sealed record Resolved(object Instance, BeanInvoker.BoundMethod Bound, bool Awaitable, Func? TaskResult, Func? ToTask); /// /// Calls the bean with the evaluated and returns the method's result. Synchronous: a /// task-returning method is refused (). /// public object? Invoke(IExchange exchange, object?[] values) { var resolved = ResolveFor(exchange); if (resolved.Awaitable) throw new InvalidOperationException(AwaitableRefused(reference, method, resolved.Bound.Method.ReturnType)); return resolved.Bound.Call(resolved.Instance, values, exchange, CancellationToken.None); } /// /// Calls the bean and awaits a task-returning method (Method() in a condition, where the engine awaits /// ); a plain method's result is returned as it is. /// public async Task InvokeAsync(IExchange exchange, object?[] values, CancellationToken cancellationToken) { var resolved = ResolveFor(exchange); var raw = resolved.Bound.Call(resolved.Instance, values, exchange, cancellationToken); if (!resolved.Awaitable) return raw; var task = resolved.ToTask is { } toTask ? (Task)toTask(raw)! : (Task)raw!; await task.ConfigureAwait(false); return resolved.TaskResult?.Invoke(task); } private Resolved ResolveFor(IExchange exchange) { var context = exchange.Context ?? throw new InvalidOperationException( $"bean:{reference}.{method}: the exchange has no route context, so there is no registry to find the bean in."); var resolved = _resolved.GetValue(context, Resolve); if (ExpressionSandbox.IsActive) throw new ExpressionSandboxViolationException(method, resolved.Instance.GetType()); return resolved; } private Resolved Resolve(IRouteContext context) { var instance = context.GetFromRegistry(reference) ?? throw new InvalidOperationException( $"bean:{reference}.{method}: '{reference}' is not in the context registry. " + "Register it with AddToRegistry or a declaration."); var bound = BeanInvoker.ForValues(instance.GetType(), method, argumentCount, "#" + reference); var (_, taskResult, toTask) = BeanInvoker.ClassifyReturn(bound.Method.ReturnType); return new Resolved(instance, bound, BeanInvoker.IsAwaitable(bound.Method.ReturnType), taskResult, toTask); } /// The refusal of a task-returning method, worded once for the call and for the package gate. internal static string AwaitableRefused(string reference, string method, Type returnType) => $"bean:{reference}.{method} returns {returnType.Name}: a bean: call in an expression is " + $"synchronous. Call an asynchronous method as a step: " + $"or .Bean(\"#{reference}\", \"{method}(...)\")."; /// /// The bean: calls of one expression, as the engine parses it: the tool's view of what an expression will /// call (the package gate). Throws with the parser's positioned message /// when the expression does not parse. /// internal static IReadOnlyList CallsIn(string expression) => NodesIn(expression); /// /// The ref: names of one expression, as the engine parses it (the package gate). Throws /// with the parser's positioned message when the expression does not parse. /// internal static IReadOnlyList RefsIn(string expression) => NodesIn(expression); private static List NodesIn(string expression) where T : Ast.AstNode { var root = new Ast.Parser(new Ast.Tokenizer(expression).GetAllTokens()).Parse(); var nodes = new List(); Collect(root, nodes); return nodes; } private static void Collect(Ast.AstNode node, List calls) where T : Ast.AstNode { if (node is T call) calls.Add(call); foreach (var property in node.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { if (property.GetIndexParameters().Length > 0) continue; switch (property.GetValue(node)) { case Ast.AstNode child: Collect(child, calls); break; case IEnumerable children: foreach (var child in children) Collect(child, calls); break; } } } }