From 788cba4881cdb2e15434d6baafc04b872291414a Mon Sep 17 00:00:00 2001 From: Sean Parent <160927019+sparent_adobe@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:29:53 -0800 Subject: [PATCH 1/3] Add nominal record adapter support Add cel::record_adapter and route native typing/value conversion and typed execution through adapter-aware boundaries. Add value and segment tests for nominal-record round-trip, schema checks, typed callback execution, and exception-safe suffix cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CMakeLists.txt | 2 +- docs/libraries.md | 36 +++++++ include/cel/runtime.hpp | 1 + include/cel/runtime/detail/frame.hpp | 10 +- include/cel/runtime/detail/raw_segment.hpp | 4 +- include/cel/runtime/dyn_segment.hpp | 14 +-- include/cel/runtime/raw_stack.hpp | 8 +- include/cel/runtime/record_adapter.hpp | 42 ++++++++ include/cel/runtime/value.hpp | 118 +++++++++++++++++++-- src/value.cpp | 6 ++ tests/segment_test.cpp | 90 ++++++++++++++++ tests/value_test.cpp | 91 ++++++++++++++++ 12 files changed, 392 insertions(+), 30 deletions(-) create mode 100644 include/cel/runtime/record_adapter.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 87f0665..83e4c4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ cpp_library_setup( DESCRIPTION "Checked CEL programs, owned values, and reusable execution stacks" NAMESPACE cel REQUIRES_CPP_VERSION 20 - HEADERS runtime.hpp runtime/layout.hpp runtime/value_type.hpp runtime/value.hpp runtime/raw_stack.hpp runtime/dyn_segment.hpp runtime/callable.hpp runtime/detail/retirement.hpp runtime/detail/frame.hpp runtime/detail/raw_segment.hpp + HEADERS runtime.hpp runtime/layout.hpp runtime/value_type.hpp runtime/value.hpp runtime/record_adapter.hpp runtime/raw_stack.hpp runtime/dyn_segment.hpp runtime/callable.hpp runtime/detail/retirement.hpp runtime/detail/frame.hpp runtime/detail/raw_segment.hpp SOURCES storage.cpp value_type.cpp value.cpp raw_stack.cpp raw_segment.cpp dyn_segment.cpp callable.cpp TESTS storage_test.cpp value_test.cpp resource_test.cpp raw_execution_test.cpp segment_test.cpp composite_execution_test.cpp callable_test.cpp executable_retirement_test.cpp native_contract_test.cpp nontrivial_leaf_fail.cpp typed_operand_fail.cpp overloaded_operand_fail.cpp generic_conversion_fail.cpp nondeduced_generic_fail.cpp raw_interface_fail.cpp alignment_interface_fail.cpp program_metadata_fail.cpp EXAMPLES getting_started.cpp diff --git a/docs/libraries.md b/docs/libraries.md index c105365..e698163 100644 --- a/docs/libraries.md +++ b/docs/libraries.md @@ -51,6 +51,42 @@ stored bytes, not resources reached through pointers inside those bytes. Use the provided `std::vector` and `std::tuple` adapters for owning arrays and tuples instead of wrapping nontrivial containers as opaque leaves. +For a nontrivial nominal record, specialize `cel::record_adapter` with a +record descriptor and field-wise encode/decode callbacks: + +```cpp +struct point { + double x; + double y; +}; + +template <> +struct cel::record_adapter { + static cel::value_type type() { + static const auto descriptor = cel::value_type::record( + "sample.point", + std::array{cel::record_field{"x", cel::value_type::leaf()}, + cel::record_field{"y", cel::value_type::leaf()}}); + return descriptor; + } + static cel::value to_value(const point& source) { + return cel::value::record(type(), + std::array{cel::value::from(source.x), + cel::value::from(source.y)}); + } + static point from_value(const cel::value& source) { + if (!source.type().same_shape(type())) + throw std::invalid_argument("wrong point type"); + return {.x = source.field("x").as(), + .y = source.field("y").as()}; + } +}; +``` + +The runtime requires `type()` to be a nominal record. `value::from()` +verifies that `to_value()` returns exactly that schema. `value::as()` checks +complete nominal identity and schema before `from_value()`. + ## Choose the result boundary Use `prepare()` when the application knows the signature. diff --git a/include/cel/runtime.hpp b/include/cel/runtime.hpp index f16becd..15ce9e3 100644 --- a/include/cel/runtime.hpp +++ b/include/cel/runtime.hpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include diff --git a/include/cel/runtime/detail/frame.hpp b/include/cel/runtime/detail/frame.hpp index eac2cd7..033524d 100644 --- a/include/cel/runtime/detail/frame.hpp +++ b/include/cel/runtime/detail/frame.hpp @@ -48,9 +48,9 @@ struct borrowed_value { /// Example: `auto n = input.as();`. template T as() const { - const auto expected = detail::concrete_adapter::type(); + const auto expected = detail::boundary_adapter::type(); require_type(expected); - return detail::concrete_adapter::read(type, data); + return detail::boundary_adapter::read(type, data); } }; @@ -111,7 +111,7 @@ class frame { /// Complexity: native read work, result storage, and adapter auxiliary space. template T read_operand(std::size_t from_top, const value_type& expected) const { - return detail::concrete_adapter::read( + return detail::boundary_adapter::read( expected, _stack->borrow_trusted(_checkpoint, from_top, expected)); } /// Returns independent concrete operands in source order without consuming them. @@ -155,7 +155,7 @@ class frame { require_active(); _stack->push_written( type, &source, [](const void* input, const value_type& shape, std::byte* destination) { - detail::concrete_adapter::write(*static_cast(input), shape, + detail::boundary_adapter::write(*static_cast(input), shape, destination); }); } @@ -268,7 +268,7 @@ class frame { /// Example: `scope.push(2.0);`. template void push(const T& source) { - push_result(detail::concrete_adapter::type(), source); + push_result(detail::boundary_adapter::type(), source); } /// Removes and returns an independent concrete copy of the top suffix operand. /// diff --git a/include/cel/runtime/detail/raw_segment.hpp b/include/cel/runtime/detail/raw_segment.hpp index 045bdb2..802b289 100644 --- a/include/cel/runtime/detail/raw_segment.hpp +++ b/include/cel/runtime/detail/raw_segment.hpp @@ -72,14 +72,14 @@ class raw_segment { if constexpr (std::is_void_v) return std::array{}; else - return std::array{detail::concrete_adapter::type()}; + return std::array{detail::boundary_adapter::type()}; } /// Constructs signature metadata and an owned callback from its source. /// /// Complexity: operand/result descriptor construction and callback construction costs. template requires std::constructible_from explicit operation(G&& input) : - inputs{detail::concrete_adapter::type()...}, outputs(result_types()), + inputs{detail::boundary_adapter::type()...}, outputs(result_types()), callback(std::forward(input)) {} /// Consumes source-ordered operands, invokes the callback, and publishes its result. /// diff --git a/include/cel/runtime/dyn_segment.hpp b/include/cel/runtime/dyn_segment.hpp index df32d6d..5b11188 100644 --- a/include/cel/runtime/dyn_segment.hpp +++ b/include/cel/runtime/dyn_segment.hpp @@ -539,7 +539,7 @@ class dyn_segment { void just(value literal); /// Emits an arbitrary-arity const callback with exact declared operands and result. /// - /// R is a trusted concrete adapter type or void; void consumes without producing a value. + /// R is a trusted native adapter type or void; void consumes without producing a value. /// Parameter/result identities are exact after cv/reference removal. Reference results are /// copied into an independently owned result; implicit result conversions are not admitted. /// Use with_signature for ambiguous or uninspectable callback members. Generic members @@ -563,12 +563,12 @@ class dyn_segment { requires(detail::exact_callback()) void op(F&& callback) { static_assert((std::same_as> && ...)); const std::array inputs{ - detail::concrete_adapter::type()...}; + detail::boundary_adapter::type()...}; const auto outputs = [] { if constexpr (std::is_void_v) return std::array{}; else - return std::array{detail::concrete_adapter::type()}; + return std::array{detail::boundary_adapter::type()}; }(); const auto effect = check_operation(inputs, outputs); _body.template push_op( @@ -963,12 +963,12 @@ class prepared_segment { if constexpr (detail::native_leaf) { alignas(R) std::array bytes{}; _binding.call_into(stack, views, bytes.data()); - return detail::concrete_adapter::read(_binding.result_type(), bytes.data()); + return detail::boundary_adapter::read(_binding.result_type(), bytes.data()); } else { detail::result_storage result(_binding); _binding.call_into(stack, views, result.data()); result.initialized(); - return detail::concrete_adapter::read(_binding.result_type(), result.data()); + return detail::boundary_adapter::read(_binding.result_type(), result.data()); } } }; @@ -977,8 +977,8 @@ class prepared_segment { template prepared_segment compiled_segment::prepare() const { static_assert(!std::is_void_v, "use tuple<> for the source-unit result"); - const std::array types{detail::concrete_adapter::type()...}; - return prepared_segment(prepare_raw(types, detail::concrete_adapter::type())); + const std::array types{detail::boundary_adapter::type()...}; + return prepared_segment(prepare_raw(types, detail::boundary_adapter::type())); } /// @} diff --git a/include/cel/runtime/raw_stack.hpp b/include/cel/runtime/raw_stack.hpp index 25df973..22f47bc 100644 --- a/include/cel/runtime/raw_stack.hpp +++ b/include/cel/runtime/raw_stack.hpp @@ -97,8 +97,8 @@ class raw_stack { T read_value(std::size_t floor) { if (depth() <= floor) throw std::out_of_range("operand underflow"); phase_guard phase(*this); - const auto type = detail::concrete_adapter::type(); - return detail::concrete_adapter::read(type, borrow(floor, 0, type)); + const auto type = detail::boundary_adapter::type(); + return detail::boundary_adapter::read(type, borrow(floor, 0, type)); } /// Reserves an aligned uninitialized operand extent and returns its byte offset. /// @@ -288,9 +288,9 @@ class raw_stack { template void push(const T& source) { require_access(nullptr); - push_written(detail::concrete_adapter::type(), &source, + push_written(detail::boundary_adapter::type(), &source, [](const void* input, const value_type& type, std::byte* destination) { - detail::concrete_adapter::write(*static_cast(input), type, + detail::boundary_adapter::write(*static_cast(input), type, destination); }); } diff --git a/include/cel/runtime/record_adapter.hpp b/include/cel/runtime/record_adapter.hpp new file mode 100644 index 0000000..7c51e15 --- /dev/null +++ b/include/cel/runtime/record_adapter.hpp @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: BSL-1.0 +#ifndef CEL_RUNTIME_RECORD_ADAPTER_HPP +#define CEL_RUNTIME_RECORD_ADAPTER_HPP + +#include + +namespace cel { +class value; + +/// Adapts one client nominal record type to and from canonical CEL record values. +/// +/// Specialize this template for one client type T to expose it as a nominal record. +/// type() must return a nominal record descriptor. to_value() must return a value with +/// exactly that descriptor. from_value() should validate complete descriptor identity +/// before decoding and throw std::invalid_argument when mismatched. +/// +/// Example: +/// @code +/// struct point { +/// double x; +/// double y; +/// }; +/// +/// template <> +/// struct cel::record_adapter { +/// static cel::value_type type() { +/// static const auto descriptor = cel::value_type::record( +/// "sample.point", +/// std::array{cel::record_field{"x", cel::value_type::leaf()}, +/// cel::record_field{"y", cel::value_type::leaf()}}); +/// return descriptor; +/// } +/// static cel::value to_value(const point& source); +/// static point from_value(const cel::value& source); +/// }; +/// @endcode +template +struct record_adapter {}; + +} // namespace cel + +#endif diff --git a/include/cel/runtime/value.hpp b/include/cel/runtime/value.hpp index a3ee8e6..c29f7dd 100644 --- a/include/cel/runtime/value.hpp +++ b/include/cel/runtime/value.hpp @@ -2,6 +2,7 @@ #ifndef CEL_RUNTIME_VALUE_HPP #define CEL_RUNTIME_VALUE_HPP +#include #include #include @@ -12,12 +13,14 @@ #include #include #include +#include #include #include namespace cel { /// \addtogroup runtime_values /// @{ +class value; /// Provides trusted concrete boundary adapters; these are not client-leaf registrations. namespace detail { @@ -35,6 +38,14 @@ struct array_view { }; template struct concrete_adapter; +template +concept nominal_record_adapter = requires(const T& input, const value& encoded) { + { record_adapter::type() } -> std::same_as; + { record_adapter::to_value(input) } -> std::same_as; + { record_adapter::from_value(encoded) } -> std::same_as; +}; +template +struct boundary_adapter; } // namespace detail /// Holds any type representable by CEL, including client-defined types. @@ -74,6 +85,8 @@ class value { friend struct detail::value_access; template friend struct detail::concrete_adapter; + template + friend struct detail::boundary_adapter; /// Takes uniquely owned, fully initialized payload storage. /// @@ -115,6 +128,14 @@ class value { /// /// The view expires when owner changes or dies; unit can have null bytes. static const std::byte* raw_data(const value& owner) noexcept; + /// Copies one canonical owner into an independent public value. + /// + /// \pre type and data describe one complete readable owned representation. + static value copy_canonical(const value_type& type, const std::byte* data); + /// Transfers one canonical owner into destination and leaves source unit. + /// + /// \pre destination is aligned writable uninitialized storage for source.type(). + static void release_canonical(value& source, std::byte* destination) noexcept; /// Borrows packed array elements from a live canonical carrier. /// /// \pre data contains an initialized array carrier and its owner remains alive and @@ -197,7 +218,7 @@ class value { /// /// Example: `value::from(true).type().kind() == type_kind::leaf`. const value_type& type() const; - /// Copies a concrete leaf or recursively nested owning vector/tuple into a runtime value. + /// Copies a concrete leaf, nominal record adapter, or nested owning vector/tuple value. /// /// Leaves source unchanged; pointer members in opaque leaves do not confer pointee ownership. /// Complexity: linear in source area, plus first-use native_type construction; owned result @@ -206,9 +227,9 @@ class value { /// Example: `auto numbers = value::from(std::vector{1.0, 2.0});`. template static value from(const T& source) { - return detail::concrete_adapter>::from(source); + return detail::boundary_adapter>::from(source); } - /// Returns a concrete copy of the exact requested leaf, vector, or tuple type. + /// Returns a concrete copy of the exact requested adapter, leaf, vector, or tuple type. /// /// Returned vectors and tuples own independent mutable payloads; leaf pointers remain pointers. /// @@ -220,7 +241,7 @@ class value { /// Example: `value::from(2.0).as() == 2.0`. template T as() const { - return detail::concrete_adapter>::as(*this); + return detail::boundary_adapter>::as(*this); } /// Copies a sequence of elements with the specified complete shape and ownership operations. /// @@ -369,7 +390,7 @@ struct concrete_adapter> { /// Complexity: first use includes element descriptor construction; subsequent calls /// take O(1) time and auxiliary space. static value_type type() { - static const auto descriptor = value_type::array(concrete_adapter::type()); + static const auto descriptor = value_type::array(boundary_adapter::type()); return descriptor; } /// Copies all elements independently. @@ -398,7 +419,7 @@ struct concrete_adapter> { std::byte* target) { const auto& input = *static_cast*>(object); - concrete_adapter::write(input[index], element, target); + boundary_adapter::write(input[index], element, target); }); } /// Returns a vector with independently owned elements. @@ -425,7 +446,7 @@ struct concrete_adapter> { result.reserve(children.size); // Extract each element through its trusted concrete adapter. for (std::size_t i = 0; i != children.size; ++i) - result.push_back(concrete_adapter::read( + result.push_back(boundary_adapter::read( children.element, children.data ? children.data + i * children.stride : nullptr)); return result; } @@ -440,7 +461,7 @@ struct concrete_adapter> { /// time and result metadata; subsequent calls take O(1) time and auxiliary space. static value_type type() { static const auto descriptor = [] { - const std::array fields{concrete_adapter::type()...}; + const std::array fields{boundary_adapter::type()...}; return value_type::tuple(fields); }(); return descriptor; @@ -470,7 +491,7 @@ struct concrete_adapter> { std::index_sequence) { std::size_t completed = 0; try { - ((concrete_adapter::write(std::get(source), shape.fields()[I], + ((boundary_adapter::write(std::get(source), shape.fields()[I], data ? data + shape.offsets()[I] : nullptr), ++completed), ...); @@ -499,7 +520,7 @@ struct concrete_adapter> { static std::tuple extract(const value_type& shape, const std::byte* data, std::index_sequence) { - return {concrete_adapter::read(shape.fields()[I], + return {boundary_adapter::read(shape.fields()[I], data ? data + shape.offsets()[I] : nullptr)...}; } /// Returns a tuple with independently owned fields. @@ -525,10 +546,85 @@ struct concrete_adapter> { } }; +template +const value_type& adapter_record_type() { + static const auto descriptor = [] { + auto declared = record_adapter::type(); + if (declared.kind() != type_kind::record) + throw std::invalid_argument("record adapter type must be a nominal record"); + return declared; + }(); + return descriptor; +} + +template +struct boundary_adapter { + /// Returns the complete descriptor for this trusted native adapter boundary. + static value_type type() { + if constexpr (nominal_record_adapter) { + return adapter_record_type(); + } else { + return concrete_adapter::type(); + } + } + /// Returns an owned canonical value for one native source value. + static value from(const T& source) { + if constexpr (nominal_record_adapter) { + const auto& expected = adapter_record_type(); + auto encoded = record_adapter::to_value(source); + if (!encoded.type().same_shape(expected)) + throw std::invalid_argument("record adapter returned the wrong nominal record type"); + return encoded; + } else { + return concrete_adapter::from(source); + } + } + /// Returns one native value decoded from a complete descriptor-checked source value. + static T as(const value& source) { + if constexpr (nominal_record_adapter) { + const auto& expected = adapter_record_type(); + if (!source.type().same_shape(expected)) + throw std::invalid_argument("wrong nominal record type"); + return record_adapter::from_value(source); + } else { + return concrete_adapter::as(source); + } + } + /// Returns one native value decoded from trusted canonical bytes. + /// + /// \pre shape is type(), and data provides one valid readable owned representation. + static T read(const value_type& shape, const std::byte* data) { + if constexpr (nominal_record_adapter) { + const auto& expected = adapter_record_type(); + if (!shape.same_shape(expected)) + throw std::invalid_argument("wrong nominal record type"); + auto encoded = value::copy_canonical(shape, data); + return record_adapter::from_value(encoded); + } else { + return concrete_adapter::read(shape, data); + } + } + /// Writes one native value into trusted canonical bytes. + /// + /// \pre shape is type(), and data provides one writable uninitialized extent. + static void write(const T& source, const value_type& shape, std::byte* data) { + if constexpr (nominal_record_adapter) { + const auto& expected = adapter_record_type(); + if (!shape.same_shape(expected)) + throw std::invalid_argument("wrong nominal record type"); + auto encoded = from(source); + value::release_canonical(encoded, data); + } else { + concrete_adapter::write(source, shape, data); + } + } +}; + } // namespace detail /// Returns the runtime's complete descriptor for a trusted native value type. /// /// Tuples and vectors use canonical structural adapters, not native object layouts. +/// record_adapter specializations provide complete nominal-record descriptors. /// Complexity: O(V + E) time and result metadata on first use, with V distinct concrete /// descriptor types and E their field/element edges; O(concrete nesting depth) call space. /// Subsequent calls take O(1) time and auxiliary space. @@ -536,7 +632,7 @@ struct concrete_adapter> { /// Example: `auto type = native_type>>();`. template value_type native_type() { - return detail::concrete_adapter>::type(); + return detail::boundary_adapter>::type(); } /// @} diff --git a/src/value.cpp b/src/value.cpp index 185b746..1715ee6 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -485,6 +485,12 @@ void value::copy_leaf(const value_type& expected, void* destination) const { const std::byte* value::raw_data(const value& owner) noexcept { return detail::value_access::data(owner); } +value value::copy_canonical(const value_type& type, const std::byte* data) { + return detail::value_access::copy(type, data); +} +void value::release_canonical(value& source, std::byte* destination) noexcept { + detail::value_access::release_to(source, destination); +} detail::array_view value::array_data(const std::byte* data) noexcept { const auto* owner = detail::read_array(data).owner; return {owner->element, owner->bytes.data(), owner->count, owner->element.byte_layout().size}; diff --git a/tests/segment_test.cpp b/tests/segment_test.cpp index 4bc7020..c8fab03 100644 --- a/tests/segment_test.cpp +++ b/tests/segment_test.cpp @@ -38,6 +38,68 @@ struct alignas(256) client_leaf { int number; ///< Retains a client-defined scalar. }; +/// Holds one nested nominal record for typed-operation adapter tests. +struct segment_bounds { + /// Stores the horizontal extent. + double width; + /// Stores the vertical extent. + double height; +}; + +/// Holds one nominal record consumed by typed callbacks. +struct segment_widget { + /// Records whether the widget is active. + bool enabled; + /// Records nested bounds. + segment_bounds bounds; +}; + +namespace cel { +template <> +struct record_adapter { + /// Returns the complete nested descriptor. + static value_type type() { + static const auto descriptor = + value_type::record("tests.segment_bounds", + std::array{record_field{"width", value_type::leaf()}, + record_field{"height", value_type::leaf()}}); + return descriptor; + } + /// Encodes one nested record value. + static value to_value(const segment_bounds& source) { + return value::record(type(), std::array{value::from(source.width), value::from(source.height)}); + } + /// Decodes one nested record value. + static segment_bounds from_value(const value& source) { + if (!source.type().same_shape(type())) throw std::invalid_argument("wrong segment bounds type"); + return {.width = source.field("width").as(), .height = source.field("height").as()}; + } +}; + +template <> +struct record_adapter { + /// Returns the complete nominal descriptor. + static value_type type() { + static const auto descriptor = + value_type::record("tests.segment_widget", + std::array{record_field{"enabled", value_type::leaf()}, + record_field{"bounds", record_adapter::type()}}); + return descriptor; + } + /// Encodes one nominal record value. + static value to_value(const segment_widget& source) { + return value::record(type(), + std::array{value::from(source.enabled), value::from(source.bounds)}); + } + /// Decodes one nominal record value. + static segment_widget from_value(const value& source) { + if (!source.type().same_shape(type())) throw std::invalid_argument("wrong segment widget type"); + return {.enabled = source.field("enabled").as(), + .bounds = source.field("bounds").as()}; + } +}; +} // namespace cel + /// Catches byte-based counting, reversed suffixes, and inclusion of ambient operands. TEST_CASE("operand types borrow the older-first local logical suffix") { cel::dyn_segment empty; @@ -390,6 +452,34 @@ TEST_CASE("throwing callbacks preserve caller prefix and reusable raw destinatio CHECK(stack.pop>() == std::vector{7.0}); } +TEST_CASE("typed operations accept nominal record adapters and propagate callback failures") { + CHECK(cel::native_type().record_identity() == "tests.segment_widget"); + bool fail = true; + cel::dyn_segment builder; + builder.push_argument(0, cel::native_type()); + builder.op1([&fail](segment_widget value) { + if (fail) throw std::runtime_error("callback"); + value.enabled = !value.enabled; + value.bounds.width += 1.0; + value.bounds.height += 2.0; + return value; + }); + auto prepared = std::move(builder).finish().prepare(); + const segment_widget input{.enabled = true, .bounds = {.width = 3.0, .height = 5.0}}; + cel::raw_stack stack; + stack.push(std::vector{7.0}); + const auto cursor = stack.cursor(); + CHECK_THROWS_AS(prepared.call(stack, input), std::runtime_error); + CHECK(stack.depth() == 1); + CHECK(stack.cursor() == cursor); + fail = false; + const auto result = prepared.call(stack, input); + CHECK_FALSE(result.enabled); + CHECK(result.bounds.width == doctest::Approx(4.0)); + CHECK(result.bounds.height == doctest::Approx(7.0)); + CHECK(stack.pop>() == std::vector{7.0}); +} + TEST_CASE("typed calls accept unused composite arguments") { cel::dyn_segment builder; builder.op0([] { return 5.0; }); diff --git a/tests/value_test.cpp b/tests/value_test.cpp index 96fdb4d..3fc5ef7 100644 --- a/tests/value_test.cpp +++ b/tests/value_test.cpp @@ -36,6 +36,68 @@ struct other_leaf { double number; }; +/// Holds one named nested record consumed through the nominal adapter boundary. +struct nominal_bounds { + /// Stores the horizontal extent. + double width; + /// Stores the vertical extent. + double height; +}; + +/// Holds one nominal record consumed through the adapter customization point. +struct nominal_widget { + /// Records whether this widget is active. + bool enabled; + /// Records one nested nominal record. + nominal_bounds bounds; +}; + +namespace cel { +template <> +struct record_adapter { + /// Returns this adapter's complete nested nominal descriptor. + static value_type type() { + static const auto descriptor = value_type::record( + "tests.nominal_bounds", + std::array{record_field{"width", value_type::leaf()}, + record_field{"height", value_type::leaf()}}); + return descriptor; + } + /// Encodes one nested nominal value with source-order field ownership. + static value to_value(const nominal_bounds& source) { + return value::record(type(), std::array{value::from(source.width), value::from(source.height)}); + } + /// Decodes one nested nominal value after complete descriptor validation. + static nominal_bounds from_value(const value& source) { + if (!source.type().same_shape(type())) throw std::invalid_argument("wrong nominal bounds type"); + return {.width = source.field("width").as(), .height = source.field("height").as()}; + } +}; + +template <> +struct record_adapter { + /// Returns this adapter's complete nominal descriptor. + static value_type type() { + static const auto descriptor = value_type::record( + "tests.nominal_widget", + std::array{record_field{"enabled", value_type::leaf()}, + record_field{"bounds", record_adapter::type()}}); + return descriptor; + } + /// Encodes one nominal value with source-order field ownership. + static value to_value(const nominal_widget& source) { + return value::record(type(), + std::array{value::from(source.enabled), value::from(source.bounds)}); + } + /// Decodes one nominal value after complete descriptor validation. + static nominal_widget from_value(const value& source) { + if (!source.type().same_shape(type())) throw std::invalid_argument("wrong nominal widget type"); + return {.enabled = source.field("enabled").as(), + .bounds = source.field("bounds").as()}; + } +}; +} // namespace cel + /// Requests a test callback failure after acquiring a temporary resource. bool fail_test_clone = false; /// Counts live resources created by the synthetic opaque clone callback. @@ -325,6 +387,35 @@ TEST_CASE("concrete adapters preserve scalar values and reject wrong identities" CHECK(std::isnan(nan)); } +/// Catches nominal adapter layout casts and nested schema loss at the boundary. +TEST_CASE("nominal record adapter round-trips values and enforces complete schemas") { + const nominal_widget input{.enabled = true, .bounds = {.width = 2.5, .height = 4.0}}; + const auto encoded = cel::value::from(input); + const auto descriptor = encoded.type(); + CHECK(descriptor.kind() == cel::type_kind::record); + CHECK(descriptor.record_identity() == "tests.nominal_widget"); + REQUIRE(descriptor.field_names().size() == 2); + CHECK(descriptor.field_names()[0] == "enabled"); + CHECK(descriptor.field_names()[1] == "bounds"); + CHECK(descriptor.fields()[0].same_shape(cel::value_type::leaf())); + CHECK(descriptor.fields()[1].kind() == cel::type_kind::record); + CHECK(descriptor.fields()[1].record_identity() == "tests.nominal_bounds"); + REQUIRE(descriptor.fields()[1].field_names().size() == 2); + CHECK(descriptor.fields()[1].field_names()[0] == "width"); + CHECK(descriptor.fields()[1].field_names()[1] == "height"); + const auto decoded = encoded.as(); + CHECK(decoded.enabled); + CHECK(decoded.bounds.width == doctest::Approx(2.5)); + CHECK(decoded.bounds.height == doctest::Approx(4.0)); + const auto wrong = cel::value::record( + cel::value_type::record("tests.other_widget", + std::array{cel::record_field{"enabled", cel::value_type::leaf()}, + cel::record_field{"bounds", cel::record_adapter::type()}}), + std::array{cel::value::from(true), + cel::value::from(nominal_bounds{.width = 1.0, .height = 1.0})}); + CHECK_THROWS_AS(cel::record_adapter::from_value(wrong), std::invalid_argument); +} + /// Catches conflation of empty array, source unit, and singleton tuple. TEST_CASE("empty and nested owning boundary adapters retain complete shapes") { auto empty = cel::value::from(std::vector{}); From d92f28ca3b2d1fcef2a7f513d263d62c9fcc68eb Mon Sep 17 00:00:00 2001 From: Sean Parent <160927019+sparent_adobe@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:37:41 -0800 Subject: [PATCH 2/3] Enforce ownership compatibility for nominal adapters Reject nominal adapter encodings and write destinations unless descriptors are fully compatible, not just shape-equal. Add a regression test that reproduces same-shape ownership mismatch and asserts rejection before transfer with safe cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- include/cel/runtime/value.hpp | 4 +- tests/segment_test.cpp | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/include/cel/runtime/value.hpp b/include/cel/runtime/value.hpp index c29f7dd..bd73ceb 100644 --- a/include/cel/runtime/value.hpp +++ b/include/cel/runtime/value.hpp @@ -572,7 +572,7 @@ struct boundary_adapter { if constexpr (nominal_record_adapter) { const auto& expected = adapter_record_type(); auto encoded = record_adapter::to_value(source); - if (!encoded.type().same_shape(expected)) + if (!encoded.type().compatible_with(expected)) throw std::invalid_argument("record adapter returned the wrong nominal record type"); return encoded; } else { @@ -610,7 +610,7 @@ struct boundary_adapter { static void write(const T& source, const value_type& shape, std::byte* data) { if constexpr (nominal_record_adapter) { const auto& expected = adapter_record_type(); - if (!shape.same_shape(expected)) + if (!shape.compatible_with(expected)) throw std::invalid_argument("wrong nominal record type"); auto encoded = from(source); value::release_canonical(encoded, data); diff --git a/tests/segment_test.cpp b/tests/segment_test.cpp index c8fab03..5a0966e 100644 --- a/tests/segment_test.cpp +++ b/tests/segment_test.cpp @@ -54,6 +54,53 @@ struct segment_widget { segment_bounds bounds; }; +/// Holds test input for ownership-mismatch adapter regression checks. +struct ownership_gap_payload { + /// Seeds the synthetic callable carrier. + int seed; +}; + +/// Returns the callable descriptor with the primary ownership pair. +const cel::value_type& primary_callable_type() { + static const auto descriptor = cel::detail::type_access::install_callable_ownership( + cel::value_type::callable({}, cel::value_type::leaf()), + {clone_carrier, destroy_carrier}); + return descriptor; +} + +/// Returns the callable descriptor with an incompatible ownership pair. +const cel::value_type& foreign_callable_type() { + static const auto descriptor = cel::detail::type_access::install_callable_ownership( + cel::value_type::callable({}, cel::value_type::leaf()), + {clone_carrier, destroy_other_carrier}); + return descriptor; +} + +/// Returns the declared nominal record descriptor with the primary callable ownership pair. +const cel::value_type& declared_ownership_gap_type() { + static const auto descriptor = cel::value_type::record( + "tests.ownership_gap", + std::array{cel::record_field{"code", primary_callable_type()}}); + return descriptor; +} + +/// Returns a same-shape nominal record descriptor with incompatible callable ownership. +const cel::value_type& foreign_ownership_gap_type() { + static const auto descriptor = cel::value_type::record( + "tests.ownership_gap", + std::array{cel::record_field{"code", foreign_callable_type()}}); + return descriptor; +} + +/// Returns one callable value with synthetic owned carrier storage. +cel::value make_foreign_callable_value(int seed) { + int payload = seed; + int* pointer = &payload; + std::array carrier{}; + std::memcpy(carrier.data(), static_cast(&pointer), sizeof(pointer)); + return cel::detail::value_access::copy(foreign_callable_type(), carrier.data()); +} + namespace cel { template <> struct record_adapter { @@ -98,6 +145,22 @@ struct record_adapter { .bounds = source.field("bounds").as()}; } }; + +template <> +struct record_adapter { + /// Returns the declared nominal descriptor with the primary ownership pair. + static value_type type() { return declared_ownership_gap_type(); } + /// Encodes the payload as a same-shape record with incompatible callable ownership. + static value to_value(const ownership_gap_payload& source) { + auto code = make_foreign_callable_value(source.seed); + return value::record(foreign_ownership_gap_type(), std::array{code}); + } + /// Decodes this payload from a validated record value. + static ownership_gap_payload from_value(const value& source) { + if (!source.type().same_shape(type())) throw std::invalid_argument("wrong ownership-gap type"); + return {.seed = 0}; + } +}; } // namespace cel /// Catches byte-based counting, reversed suffixes, and inclusion of ambient operands. @@ -480,6 +543,37 @@ TEST_CASE("typed operations accept nominal record adapters and propagate callbac CHECK(stack.pop>() == std::vector{7.0}); } +TEST_CASE("typed record adapter rejects same-shape incompatible ownership before transfer") { + cel::dyn_segment builder; + builder.op0([] { return ownership_gap_payload{.seed = 7}; }); + auto program = std::move(builder).finish(); + const auto result_type = cel::native_type(); + auto prepared = cel::detail::segment_access::prepare_raw(program, {}, result_type); + const auto layout = result_type.byte_layout(); + auto* destination = + static_cast(::operator new(layout.size, std::align_val_t(layout.alignment))); + std::fill(destination, destination + layout.size, std::byte{0x5a}); + cel::raw_stack stack; + stack.push(std::vector{9.0}); + const auto cursor = stack.cursor(); + const auto depth = stack.depth(); + const auto live = live_carriers; + bool threw = false; + try { + prepared.call_into(stack, {}, destination); + } catch (const std::invalid_argument&) { + threw = true; + } + if (!threw) prepared.destroy_result(destination); + CHECK(threw); + if (threw) CHECK(destination[0] == std::byte{0x5a}); + CHECK(live_carriers == live); + CHECK(stack.depth() == depth); + CHECK(stack.cursor() == cursor); + CHECK(stack.pop>() == std::vector{9.0}); + ::operator delete(destination, std::align_val_t(layout.alignment)); +} + TEST_CASE("typed calls accept unused composite arguments") { cel::dyn_segment builder; builder.op0([] { return 5.0; }); From e67c9d5793367b7f5b6f0deaba1ca43b57e40933 Mon Sep 17 00:00:00 2001 From: Sean Parent <160927019+sparent_adobe@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:54:34 -0800 Subject: [PATCH 3/3] Fix adapted record typed call storage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- include/cel/runtime/dyn_segment.hpp | 4 +- tests/segment_test.cpp | 96 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/include/cel/runtime/dyn_segment.hpp b/include/cel/runtime/dyn_segment.hpp index 5b11188..dd7f0b3 100644 --- a/include/cel/runtime/dyn_segment.hpp +++ b/include/cel/runtime/dyn_segment.hpp @@ -74,9 +74,9 @@ struct stack_requirements { namespace detail { struct frozen_program; -/// Selects native leaf boundaries rather than unspecified standard composite layouts. +/// Selects borrowed native leaves rather than canonical record or composite layouts. template -inline constexpr bool native_leaf = true; +inline constexpr bool native_leaf = !nominal_record_adapter; /// Selects the trusted vector adapter. template inline constexpr bool native_leaf> = false; diff --git a/tests/segment_test.cpp b/tests/segment_test.cpp index 5a0966e..b8140d5 100644 --- a/tests/segment_test.cpp +++ b/tests/segment_test.cpp @@ -54,6 +54,20 @@ struct segment_widget { segment_bounds bounds; }; +/// Holds coordinates in an order different from its canonical record schema. +struct segment_reordered_point { + /// Stores the vertical coordinate. + double y; + /// Stores the horizontal coordinate. + double x; +}; + +/// Holds an owned callable field for typed-result lifetime tests. +struct segment_callable_record { + /// Owns one callable value. + cel::value code; +}; + /// Holds test input for ownership-mismatch adapter regression checks. struct ownership_gap_payload { /// Seeds the synthetic callable carrier. @@ -101,6 +115,15 @@ cel::value make_foreign_callable_value(int seed) { return cel::detail::value_access::copy(foreign_callable_type(), carrier.data()); } +/// Returns one callable value with the primary ownership pair. +cel::value make_primary_callable_value(int seed) { + int payload = seed; + int* pointer = &payload; + std::array carrier{}; + std::memcpy(carrier.data(), static_cast(&pointer), sizeof(pointer)); + return cel::detail::value_access::copy(primary_callable_type(), carrier.data()); +} + namespace cel { template <> struct record_adapter { @@ -146,6 +169,49 @@ struct record_adapter { } }; +template <> +struct record_adapter<::segment_reordered_point> { + /// Returns the canonical coordinate schema. + static value_type type() { + static const auto descriptor = + value_type::record("tests.segment_reordered_point", + std::array{record_field{"x", value_type::leaf()}, + record_field{"y", value_type::leaf()}}); + return descriptor; + } + /// Encodes coordinates by their names rather than their native order. + static value to_value(const ::segment_reordered_point& source) { + return value::record(type(), std::array{value::from(source.x), value::from(source.y)}); + } + /// Decodes coordinates by their canonical names. + static ::segment_reordered_point from_value(const value& source) { + if (!source.type().same_shape(type())) + throw std::invalid_argument("wrong reordered point type"); + return {.y = source.field("y").as(), .x = source.field("x").as()}; + } +}; + +template <> +struct record_adapter<::segment_callable_record> { + /// Returns the callable record schema. + static value_type type() { + static const auto descriptor = + value_type::record("tests.segment_callable_record", + std::array{record_field{"code", primary_callable_type()}}); + return descriptor; + } + /// Encodes the callable field as an owned canonical value. + static value to_value(const ::segment_callable_record& source) { + return value::record(type(), std::array{source.code}); + } + /// Copies the owned callable field from a validated record. + static ::segment_callable_record from_value(const value& source) { + if (!source.type().same_shape(type())) + throw std::invalid_argument("wrong callable record type"); + return {.code = source.field("code")}; + } +}; + template <> struct record_adapter { /// Returns the declared nominal descriptor with the primary ownership pair. @@ -543,6 +609,36 @@ TEST_CASE("typed operations accept nominal record adapters and propagate callbac CHECK(stack.pop>() == std::vector{7.0}); } +TEST_CASE("typed record operands preserve canonical field order") { + cel::dyn_segment builder; + builder.push_argument(0, cel::native_type()); + builder.op1( + [](segment_reordered_point point) { return point; }); + auto prepared = + std::move(builder).finish().prepare(); + cel::raw_stack stack; + const segment_reordered_point input{.y = 2.0, .x = 1.0}; + const auto result = prepared.call(stack, input); + CHECK(result.x == doctest::Approx(1.0)); + CHECK(result.y == doctest::Approx(2.0)); +} + +TEST_CASE("typed record results retire their canonical owned fields") { + auto code = make_primary_callable_value(17); + cel::dyn_segment builder; + builder.op0( + [code = std::move(code)] { return segment_callable_record{.code = code}; }); + auto prepared = std::move(builder).finish().prepare(); + cel::raw_stack stack; + const auto live = live_carriers; + { + const auto result = prepared.call(stack); + CHECK(result.code.type().same_shape(primary_callable_type())); + CHECK(live_carriers == live + 1); + } + CHECK(live_carriers == live); +} + TEST_CASE("typed record adapter rejects same-shape incompatible ownership before transfer") { cel::dyn_segment builder; builder.op0([] { return ownership_gap_payload{.seed = 7}; });