This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit f3ddbb3f6b3922b31dea168b9be4c363c5a0e85b
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 15 19:08:11 2026 -0600

    eolian_cxx: callback arguments reach C++ converted like return values
    
    The generated caller of a function-pointer wrapper built each C++
    argument with out_traits<T>::type(c_arg). That is the conversion for an
    out parameter going to C, not for a value coming from C: for an object
    it asks for View_Model&(const Eo*), which does not compile, and for a
    string it builds the wrong thing from a const char*.
    
    Each argument now gets the conversion a getter result gets. An object
    arrives as a wrapper holding a reference of its own, released when the
    callable returns, and a string as a string.
    
    Two kinds of argument are not converted that way. A strbuf is written to
    by the callable and what it writes must reach C's buffer, so it is
    wrapped, not copied (Efl.Ui.Format_Func). And a value C passes by value
    (const(any_value)) gets a view on the caller's own value; a view on a
    copy would be gone when the conversion returns. A callback with an out
    or inout parameter, which has no conversion back to C here, is refused
    by a static_assert with a sentence instead of generating wrong code
    (none exists in the tree), and so is a strbuf the callback would own,
    which would need freeing after the call. The caller's own locals carry
    a prefix no parameter name can take, so a callback parameter named fw or
    cxx_call_data no longer shadows them.
    
    Source change for callables: an object or a string now arrives as a
    converted temporary, so a lambda takes it by value or by const
    reference. A lambda taking an object by non-const reference
    (efl::layout::Signal& for EflLayoutSignalCb, say) no longer compiles;
    the const& form does.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bindings/cxx/eo_cxx/eo_cxx_interop.hh          |  7 ++
 .../grammar/type_function_declaration.hpp          | 80 +++++++++++++++----
 src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc  | 91 ++++++++++++++++++++++
 src/tests/eolian_cxx/fnptr.c                       | 31 ++++++++
 src/tests/eolian_cxx/fnptr.eo                      | 65 +++++++++++++++-
 5 files changed, 253 insertions(+), 21 deletions(-)

diff --git a/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh b/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
index bf3d71be05..ecac83243b 100644
--- a/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
+++ b/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
@@ -760,6 +760,13 @@ inline efl::eina::value_view convert_to_return(Eina_Value* value, tag<Eina_Value
 {
   return efl::eina::value_view{value};
 }
+/* A value C passes by value to a callback: the view is on the caller's value,
+ * not on a copy that dies with this call (the generic by-value overload above
+ * would take it by copy and leave the view dangling). */
+inline efl::eina::value_view convert_to_return(Eina_Value const& value, tag<Eina_Value const, efl::eina::value_view const>)
+{
+  return efl::eina::value_view{const_cast<Eina_Value*>(&value)};
+}
 inline efl::eina::value_view convert_to_return(Eina_Value const* value, tag<Eina_Value const*, efl::eina::value_view const>)
 {
   return efl::eina::value_view{const_cast<Eina_Value*>(value)};
diff --git a/src/lib/eolian_cxx/grammar/type_function_declaration.hpp b/src/lib/eolian_cxx/grammar/type_function_declaration.hpp
index 5fc4eb3b70..7b117038ac 100644
--- a/src/lib/eolian_cxx/grammar/type_function_declaration.hpp
+++ b/src/lib/eolian_cxx/grammar/type_function_declaration.hpp
@@ -78,28 +78,74 @@ struct type_function_declaration_generator {
            c_args.push_back(arg);
         }
       if (!as_generator(
-             scope_tab << "static " << string << " caller(void *cxx_call_data"
+             scope_tab << "static " << string << " caller(void *__eolian_cxx_data"
              << *(", " << string) << ") {\n"
-             << scope_tab << scope_tab << "auto fw = static_cast<function_wrapper<"
-             << string << ", F, ::efl::eolian::" << string << "__function_tag>*>(cxx_call_data);\n"
+             << scope_tab << scope_tab << "auto __eolian_cxx_fw = static_cast<function_wrapper<"
+             << string << ", F, ::efl::eolian::" << string << "__function_tag>*>(__eolian_cxx_data);\n"
              ).generate(sink, std::make_tuple(f.return_type.c_type, c_args, f.c_name, f.c_name), ctx))
         return false;
 
-      if (f.return_type != attributes::void_
-          && !as_generator(scope_tab << scope_tab << "auto __return_value =\n")
-          .generate(sink, attributes::unused, ctx))
-        return false;
+      /* Each C argument reaches the callable converted as a getter result is:
+       * an object is a wrapper holding a reference of its own, a string a
+       * string. Both are temporaries, so a callable takes them by value or by
+       * const reference.
+       *
+       * A strbuf is not converted: the callable writes to it and the text has
+       * to reach C's buffer, so it is wrapped in a named local. An argument
+       * with no conversion back to C (out, inout, an owned strbuf) is refused
+       * by a static_assert. The locals of the caller carry a prefix no
+       * parameter name can take.
+       */
+      {
+         std::string call = "__eolian_cxx_fw->_cxx_func(";
+         bool first = true;
+         for (auto const& param : f.parameters)
+           {
+              if ((param.direction == attributes::parameter_direction::out)
+                  || (param.direction == attributes::parameter_direction::inout))
+                {
+                   if (!as_generator(scope_tab << scope_tab
+                                     << "static_assert(sizeof(F) == 0,\n"
+                                     << scope_tab << scope_tab << scope_tab
+                                     << "\"this callback has an out or inout parameter, which the C++ binding does not map; \"\n"
+                                     << scope_tab << scope_tab << scope_tab
+                                     << "\"it cannot be used from C++\");\n")
+                       .generate(sink, attributes::unused, ctx))
+                     return false;
+                }
 
-      if (!f.parameters.empty())
-        {
-           std::vector<attributes::parameter_def> params;
-           for (auto itr = f.parameters.begin() + 1; itr != f.parameters.end(); itr++)
-             params.push_back(*itr);
-           if (!as_generator(
-                  scope_tab << scope_tab << "fw->_cxx_func(" << parameter_as_argument << *(", " << parameter_as_argument) << ");\n"
-                  ).generate(sink, std::make_tuple(*f.parameters.begin(), params), ctx))
-               return false;
-        }
+              std::string arg;
+              if (param.type.c_type == "Eina_Strbuf *")
+                {
+                   if (param.type.has_own
+                       && !as_generator(scope_tab << scope_tab
+                                        << "static_assert(sizeof(F) == 0,\n"
+                                        << scope_tab << scope_tab << scope_tab
+                                        << "\"this callback takes a strbuf it owns, which the C++ binding does not map; \"\n"
+                                        << scope_tab << scope_tab << scope_tab
+                                        << "\"it cannot be used from C++\");\n")
+                            .generate(sink, attributes::unused, ctx))
+                     return false;
+                   arg = "__strbuf_" + param.param_name;
+                   if (!as_generator(scope_tab << scope_tab << "::efl::eina::strbuf_wrapper " << string << "(" << string << ");\n")
+                       .generate(sink, std::make_tuple(arg, param.param_name), ctx))
+                     return false;
+                }
+              else if (!as_generator(" ::efl::eolian::convert_to_return< ::efl::eolian::return_traits<"
+                                     << type << ">::type>(" << string << ")")
+                       .generate(std::back_inserter(arg), std::make_tuple(param.type, param.param_name), ctx))
+                return false;
+              call += (first ? "" : ", ") + arg;
+              first = false;
+           }
+         call += ");\n";
+         if (f.return_type != attributes::void_
+             && !as_generator(scope_tab << scope_tab << "auto __return_value =\n")
+                  .generate(sink, attributes::unused, ctx))
+           return false;
+         if (!as_generator(scope_tab << scope_tab << string).generate(sink, call, ctx))
+           return false;
+      }
 
       /* convert_to_c<c_type, cxx_type[, own]>, as the argument converter. */
       if (f.return_type != attributes::void_)
diff --git a/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc b/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
index e7374f66da..fd23dad9c4 100644
--- a/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
+++ b/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
@@ -60,9 +60,100 @@ EFL_START_TEST(eolian_cxx_test_callbacks_two_types)
 }
 EFL_END_TEST
 
+/* What C hands a callback reaches it converted the way a return value is: an
+ * object is a wrapper holding a reference of its own, a string is a string. */
+EFL_START_TEST(eolian_cxx_test_callbacks_arguments)
+{
+  efl::eo::eo_init init;
+
+  nonamespace::Fnptr obj(efl::eo::instantiate);
+  Eo* raw = obj._eo_ptr();
+  int refs_before = efl_ref_count(raw);
+  int refs_inside = 0;
+  bool same = false;
+  std::string seen;
+
+  int r = obj.visit([&](nonamespace::Fnptr o, std::string text, int n)
+                    {
+                       same = (o._eo_ptr() == raw);
+                       refs_inside = efl_ref_count(raw);
+                       seen = text;
+                       return n * 3;
+                    });
+
+  ck_assert_int_eq(r, 21);
+  ck_assert(same);
+  ck_assert_str_eq(seen.c_str(), "hello");
+  ck_assert_int_eq(refs_inside, refs_before + 1);
+  ck_assert_int_eq(efl_ref_count(raw), refs_before);
+
+  /* The converted arguments are temporaries: a const reference binds. */
+  r = obj.visit([&](nonamespace::Fnptr const& o, std::string const& text, int n)
+                {
+                   same = (o._eo_ptr() == raw);
+                   seen = text;
+                   return n + 1;
+                });
+  ck_assert_int_eq(r, 8);
+  ck_assert(same);
+  ck_assert_str_eq(seen.c_str(), "hello");
+  ck_assert_int_eq(efl_ref_count(raw), refs_before);
+}
+EFL_END_TEST
+
+/* Writes over the stack below the caller: a view on a dead frame would read
+ * what this leaves there. */
+static void __attribute__((noinline))
+_clobber_stack()
+{
+  volatile unsigned char junk[2048];
+  for (unsigned i = 0; i < sizeof(junk); ++i)
+    junk[i] = 0x55;
+}
+
+/* A callback shaped like Efl.Ui.Format_Func: the buffer it gets is C's, so
+ * what it appends is seen by C, and the value is a view on C's own value, not
+ * on a copy that is gone when the conversion returns. */
+EFL_START_TEST(eolian_cxx_test_callbacks_buffer_and_value)
+{
+  efl::eo::eo_init init;
+
+  nonamespace::Fnptr obj(efl::eo::instantiate);
+  int seen = -1;
+
+  int r = obj.format([&](efl::eina::strbuf& str, efl::eina::value_view const& v)
+                     {
+                        _clobber_stack();
+                        seen = efl::eina::get<int>(v);
+                        str.append("value ");
+                        str.append(std::to_string(seen));
+                        return true;
+                     });
+  ck_assert_int_eq(seen, 42);
+  ck_assert_int_eq(r, 1);
+
+  /* The view C++ gets of a value C passes by value is on that value itself,
+   * not on a copy of it: the address says so whatever the stack holds. */
+  Eina_Value value;
+  eina_value_setup(&value, EINA_VALUE_TYPE_INT);
+  eina_value_set(&value, 7);
+  Eina_Value const& cvalue = value;
+  auto view = efl::eolian::convert_to_return<
+    efl::eolian::return_traits<efl::eina::value_view const>::type>(cvalue);
+  ck_assert(view.native_handle() == &value);
+  eina_value_flush(&value);
+
+  /* The same, with the buffer taken by a lambda that gives a verdict. */
+  r = obj.format([](efl::eina::strbuf&, efl::eina::value_view const&) { return false; });
+  ck_assert_int_eq(r, -1);
+}
+EFL_END_TEST
+
 void
 eolian_cxx_test_callbacks(TCase* tc)
 {
   tcase_add_test(tc, eolian_cxx_test_callbacks_return_string);
+  tcase_add_test(tc, eolian_cxx_test_callbacks_arguments);
+  tcase_add_test(tc, eolian_cxx_test_callbacks_buffer_and_value);
   tcase_add_test(tc, eolian_cxx_test_callbacks_two_types);
 }
diff --git a/src/tests/eolian_cxx/fnptr.c b/src/tests/eolian_cxx/fnptr.c
index 947cba1a9e..5abf728717 100644
--- a/src/tests/eolian_cxx/fnptr.c
+++ b/src/tests/eolian_cxx/fnptr.c
@@ -43,4 +43,35 @@ _fnptr_apply(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED,
    return r;
 }
 
+static int
+_fnptr_format(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED,
+              void *cb_data, Fnptr_Format_Func cb, Eina_Free_Cb cb_free_cb)
+{
+   Eina_Strbuf *buf = eina_strbuf_new();
+   Eina_Value value;
+   int r = -1;
+
+   eina_value_setup(&value, EINA_VALUE_TYPE_INT);
+   eina_value_set(&value, 42);
+   if (cb && cb(cb_data, buf, value))
+     r = !strcmp(eina_strbuf_string_get(buf), "value 42");
+   eina_value_flush(&value);
+   eina_strbuf_free(buf);
+   if (cb_free_cb) cb_free_cb(cb_data);
+
+   return r;
+}
+
+static int
+_fnptr_visit(Eo *obj, void *pd EINA_UNUSED,
+             void *cb_data, Fnptr_Visit_Func cb, Eina_Free_Cb cb_free_cb)
+{
+   int r = 0;
+
+   if (cb) r = cb(cb_data, obj, "hello", 7);
+   if (cb_free_cb) cb_free_cb(cb_data);
+
+   return r;
+}
+
 #include "fnptr.eo.c"
diff --git a/src/tests/eolian_cxx/fnptr.eo b/src/tests/eolian_cxx/fnptr.eo
index 0c6a2f8164..e20d45fb5f 100644
--- a/src/tests/eolian_cxx/fnptr.eo
+++ b/src/tests/eolian_cxx/fnptr.eo
@@ -7,21 +7,62 @@ function Fnptr_Text_Func {
 };
 
 function Fnptr_Int_Func {
-   [[A callback taking and returning an int. @since 1.66]]
+   [[A callback taking and returning an int, whose parameter is named
+     like a local of the generated C++ caller. @since 1.66]]
    params {
-      @in x: int; [[The argument.]]
+      @in fw: int; [[The argument.]]
    }
    return: int; [[The result.]]
 };
 
 function Fnptr_Twice_Func {
-   [[Another callback type, so a method can take two different ones. @since 1.66]]
+   [[Another callback type, so a method can take two different ones. Its
+     parameter is named like the data parameter of the generated caller.
+     @since 1.66]]
    params {
-      @in x: int; [[The argument.]]
+      @in cxx_call_data: int; [[The argument.]]
    }
    return: int; [[The result.]]
 };
 
+function Fnptr_Visit_Func {
+   [[A callback taking an object and a string. @since 1.66]]
+   params {
+      @in obj: Fnptr; [[The object the callback is issued on.]]
+      @in text: string; [[A string.]]
+      @in n: int; [[An int.]]
+   }
+   return: int; [[The result.]]
+};
+
+function Fnptr_Format_Func {
+   [[A callback shaped like Efl.Ui.Format_Func: it writes into a buffer of the
+     caller's and reads a value the caller keeps. @since 1.66]]
+   params {
+      @in str: strbuf; [[The buffer to write into.]]
+      @in value: const(any_value); [[The value to format.]]
+   }
+   return: bool; [[Whether it worked.]]
+};
+
+function Fnptr_Out_Func {
+   [[A callback with an out parameter, which the C++ binding does not map. No
+     method takes it: it is here so the generated header, which declares a
+     wrapper for it that refuses to be used, is compiled with every test.
+     @since 1.66]]
+   params {
+      @out r: int; [[The result.]]
+   }
+};
+
+function Fnptr_Owned_Buffer_Func {
+   [[A callback taking a strbuf it owns, which the C++ binding does not map
+     either; here for the same reason. @since 1.66]]
+   params {
+      @in str: strbuf @move; [[The buffer, which the callback frees.]]
+   }
+};
+
 class Fnptr extends Efl.Object {
    [[A class with methods taking function pointers. @since 1.66]]
    data: null;
@@ -49,5 +90,21 @@ class Fnptr extends Efl.Object {
          }
          return: int; [[The sum of both results.]]
       }
+      format {
+         [[Calls $cb with a buffer and the int 42, and returns 1 when what $cb
+           wrote into the buffer is "value 42", 0 when it is anything else
+           and -1 when $cb failed.]]
+         params {
+            @in cb: Fnptr_Format_Func; [[Called with the buffer and the value.]]
+         }
+         return: int; [[1 when the buffer holds what is expected.]]
+      }
+      visit {
+         [[Calls $cb on itself with a string and an int, and returns its result.]]
+         params {
+            @in cb: Fnptr_Visit_Func; [[Called with this object.]]
+         }
+         return: int; [[What $cb returned.]]
+      }
    }
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to