            // CPD-OFF — FFI JSON-roundtrip body, structurally identical for every named-result method.
            MemorySegment resultPtr = (MemorySegment) {{ ffi_handle }}.invoke({{ args_joined }});
{{ named_frees }}            if (resultPtr.equals(MemorySegment.NULL)) {
                checkLastFfiError();
                return {{ empty_return }};
            }
            try {
                MemorySegment jsonPtr = (MemorySegment) {{ to_json }}.invoke(resultPtr);
                if (jsonPtr.equals(MemorySegment.NULL)) {
                    checkLastFfiError();
                    throw new {{ exception_class }}("{{ method_name }}: failed to serialize response", (Throwable) null);
                }
                String json = jsonPtr.reinterpret(Long.MAX_VALUE).getString(0);
                NativeLib.{{ prefix_upper }}_FREE_STRING.invoke(jsonPtr);
                {{ success_return }}
            } finally {
                {{ ret_free }}.invoke(resultPtr);
            }
            // CPD-ON
