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 8473cbf56aba6e7ee15e8180799f9ae610afd964
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 15 19:08:41 2026 -0600
eolian_cxx: an iterator lent to C starts at the element it points to
The C++ iterator reads the element it points to as soon as it is made
or advanced, so its native handle is already past that element. An
iterator passed to C without @move gave C that handle as it was, and
C never saw the current element: summing [20, 100, 200] gave 300.
C is now handed a primed iterator that gives the current element first
and then goes on with the native one. C does not free what it is lent,
so the C++ iterator keeps the primed iterator and frees it when it
dies. The conversion is a private helper of the iterator, not API.
After the call the C++ iterator compares equal to the end iterator.
Both sides read the same native iterator, which cannot go back, and
C++ cannot know where C stopped: an input iterator someone else has
read is used up. Lending it again gives C NULL.
A default constructed iterator now starts with no native handle
instead of an uninitialised one.
No stable .eo method takes a lent iterator today.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
src/bindings/cxx/eina_cxx/eina_iterator.hh | 126 +++++++++++++++++++++-
src/bindings/cxx/eo_cxx/eo_cxx_interop.hh | 7 +-
src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc | 31 ++++++
src/tests/eolian_cxx/fnptr.c | 14 +++
src/tests/eolian_cxx/fnptr.eo | 8 ++
5 files changed, 181 insertions(+), 5 deletions(-)
diff --git a/src/bindings/cxx/eina_cxx/eina_iterator.hh b/src/bindings/cxx/eina_cxx/eina_iterator.hh
index a08adef1d2..13b19935d9 100644
--- a/src/bindings/cxx/eina_cxx/eina_iterator.hh
+++ b/src/bindings/cxx/eina_cxx/eina_iterator.hh
@@ -38,6 +38,65 @@ namespace efl { namespace eina {
* @{
*/
+/**
+ * @internal
+ * A native @c Eina_Iterator that gives out one element it was handed first and
+ * then goes on with the iterator it wraps. The C++ iterators read the element
+ * they point to as soon as they are made or advanced, so the native handle
+ * they hold is already past it: handing that handle on as it is would lose it.
+ */
+struct _primed_iterator
+{
+ Eina_Iterator base; /**< @internal Must stay first: C sees this as an @c Eina_Iterator. */
+ Eina_Iterator* rest; /**< @internal The native iterator to go on with. */
+ void* first; /**< @internal The element to give first. */
+ bool pending; /**< @internal Whether @c first has not been given yet. */
+
+ static Eina_Iterator* create(Eina_Iterator* rest, void* first)
+ {
+ _primed_iterator* it = new _primed_iterator;
+ it->base.version = EINA_ITERATOR_VERSION;
+ it->base.next = FUNC_ITERATOR_NEXT(&_primed_iterator::next);
+ it->base.get_container = FUNC_ITERATOR_GET_CONTAINER(&_primed_iterator::get_container);
+ it->base.free = FUNC_ITERATOR_FREE(&_primed_iterator::free_it);
+ it->base.lock = FUNC_ITERATOR_LOCK(&_primed_iterator::lock);
+ it->base.unlock = FUNC_ITERATOR_LOCK(&_primed_iterator::unlock);
+ EINA_MAGIC_SET(&it->base, EINA_MAGIC_ITERATOR);
+ it->rest = rest;
+ it->first = first;
+ it->pending = true;
+ return &it->base;
+ }
+
+ static Eina_Bool next(_primed_iterator* it, void** data)
+ {
+ if(it->pending)
+ {
+ it->pending = false;
+ *data = ""
+ return EINA_TRUE;
+ }
+ return ::eina_iterator_next(it->rest, data);
+ }
+ static void* get_container(_primed_iterator* it)
+ {
+ return ::eina_iterator_container_get(it->rest);
+ }
+ static void free_it(_primed_iterator* it)
+ {
+ ::eina_iterator_free(it->rest);
+ delete it;
+ }
+ static Eina_Bool lock(_primed_iterator* it)
+ {
+ return ::eina_iterator_lock(it->rest);
+ }
+ static Eina_Bool unlock(_primed_iterator* it)
+ {
+ return ::eina_iterator_unlock(it->rest);
+ }
+};
+
/**
* Common implementations for iterators.
*/
@@ -56,7 +115,7 @@ public:
/**
* @brief Default constructor. Creates an iterator that points to nothing.
*/
- _common_iterator_base() {}
+ _common_iterator_base() : _iterator(0), _lent(0) {}
/**
* @brief Creates a iterator wrapping the given native @c Eina_Iterator handle.
@@ -69,7 +128,7 @@ public:
* and will deallocate it at destruction time.
*/
explicit _common_iterator_base(Eina_Iterator* iterator)
- : _iterator(iterator) {}
+ : _iterator(iterator), _lent(0) {}
/**
* @brief Release the internal native Eina iterator handle.
@@ -78,6 +137,8 @@ public:
{
if(_iterator)
eina_iterator_free(_iterator);
+ if(_lent)
+ eina_iterator_free(_lent);
}
/**
@@ -89,7 +150,7 @@ public:
* of this constructor.
*/
_common_iterator_base(self_type const& other)
- : _iterator(other._iterator)
+ : _iterator(other._iterator), _lent(0)
{
other._iterator = 0;
}
@@ -127,11 +188,38 @@ public:
}
protected:
+ /**
+ * @internal
+ * Lend the native handle, together with the element this iterator points
+ * to, to code that reads it but does not free it. This iterator keeps
+ * what it lent and frees it when it dies; it has nothing left afterwards,
+ * as an input iterator someone else read is used up.
+ * @return A native iterator that starts at @p current, or @c NULL when this
+ * iterator has nothing left.
+ */
+ Eina_Iterator* _lend_primed(void* current) const
+ {
+ if(!_iterator) return 0;
+ Eina_Iterator* primed = _primed_iterator::create(_iterator, current);
+ // Lent before and given a new handle since: the old one is done with.
+ if(_lent)
+ eina_iterator_free(_lent);
+ _lent = primed;
+ _iterator = 0;
+ return primed;
+ }
+
/**
* @internal
*/
mutable Eina_Iterator* _iterator;
+ /**
+ * @internal
+ * What was lent to native code, freed with this iterator.
+ */
+ mutable Eina_Iterator* _lent;
+
/**
* @brief Check if the iterators wrap the same handle.
* @param lhs Iterator at the left side of the _expression_.
@@ -240,6 +328,22 @@ public:
{
return _value;
}
+
+ /**
+ * @internal
+ * Lend the iterator to native code that reads it without freeing it, for
+ * the generated conversions: not part of the API.
+ * @return A native @c Eina_Iterator that goes on from the element this
+ * iterator points to, which stays owned by this iterator; @c NULL when
+ * there is nothing left.
+ *
+ * This iterator compares equal to the end iterator after the call: what
+ * native code did not read is not given back.
+ */
+ Eina_Iterator* _lend_native_handle() const
+ {
+ return this->_lend_primed(const_cast<void*>(static_cast<const void*>(_value)));
+ }
};
template <typename T>
@@ -323,6 +427,22 @@ public:
// relies on layout compatibility between eo::concrete and Eo*
return reinterpret_cast<T const*>(&_value);
}
+
+ /**
+ * @internal
+ * Lend the iterator to native code that reads it without freeing it, for
+ * the generated conversions: not part of the API.
+ * @return A native @c Eina_Iterator that goes on from the element this
+ * iterator points to, which stays owned by this iterator; @c NULL when
+ * there is nothing left.
+ *
+ * This iterator compares equal to the end iterator after the call: what
+ * native code did not read is not given back.
+ */
+ Eina_Iterator* _lend_native_handle() const
+ {
+ return this->_lend_primed(const_cast<void*>(static_cast<const void*>(_value)));
+ }
};
/**
diff --git a/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh b/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
index ecac83243b..6b76b25f86 100644
--- a/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
+++ b/src/bindings/cxx/eo_cxx/eo_cxx_interop.hh
@@ -515,15 +515,18 @@ Eina_Array const* convert_to_c_impl(efl::eina::array<T>const& c, tag<Eina_Array
{
return const_cast<efl::eina::array<T>&>(c).release_native_handle();
}
+// Lent, not handed over: C reads from the element the iterator points to,
+// which the native handle is already past, and the iterator frees what it
+// lent. It is used up afterwards.
template <typename T>
Eina_Iterator* convert_to_c_impl(efl::eina::iterator<T>const& i, tag<Eina_Iterator *, efl::eina::iterator<T>const&>)
{
- return const_cast<Eina_Iterator*>(i.native_handle());
+ return i._lend_native_handle();
}
template <typename T>
Eina_Iterator const* convert_to_c_impl(efl::eina::iterator<T>const& i, tag<Eina_Iterator const*, efl::eina::iterator<T>const&>)
{
- return i.native_handle();
+ return i._lend_native_handle();
}
template <typename T>
Eina_Iterator* convert_to_c_impl(efl::eina::iterator<T>const& /*i*/, tag<Eina_Iterator *, efl::eina::iterator<T>const&, true>)
diff --git a/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc b/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
index fd23dad9c4..92c0f9072a 100644
--- a/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
+++ b/src/tests/eolian_cxx/eolian_cxx_test_callbacks.cc
@@ -149,10 +149,41 @@ EFL_START_TEST(eolian_cxx_test_callbacks_buffer_and_value)
}
EFL_END_TEST
+/* An iterator lent to C, not handed over: C reads from the element the C++
+ * iterator points to, the C++ iterator frees the native one, and it is used
+ * up afterwards, as an input iterator read to the end by someone else is. */
+EFL_START_TEST(eolian_cxx_test_callbacks_lent_iterator)
+{
+ efl::eo::eo_init init;
+
+ nonamespace::Fnptr obj(efl::eo::instantiate);
+ static int values[] = { 20, 100, 200 };
+ Eina_List* list = nullptr;
+ for (int& v : values)
+ list = eina_list_append(list, &v);
+
+ {
+ efl::eina::iterator<int> it(eina_list_iterator_new(list));
+ ck_assert_int_eq(obj.sum_lent(it), 320);
+ ck_assert(it == efl::eina::iterator<int>());
+ /* Nothing left to lend: C gets NULL. */
+ ck_assert_int_eq(obj.sum_lent(it), -1);
+ }
+ {
+ efl::eina::iterator<int> it(eina_list_iterator_new(list));
+ ++it;
+ ck_assert_int_eq(obj.sum_lent(it), 300);
+ }
+ ck_assert_int_eq(obj.sum_lent(efl::eina::iterator<int>()), -1);
+ eina_list_free(list);
+}
+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_lent_iterator);
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 5abf728717..783eefbf59 100644
--- a/src/tests/eolian_cxx/fnptr.c
+++ b/src/tests/eolian_cxx/fnptr.c
@@ -43,6 +43,20 @@ _fnptr_apply(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED,
return r;
}
+static int
+_fnptr_sum_lent(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED, Eina_Iterator *l)
+{
+ int *i, sum = 0;
+
+ if (!l) return -1;
+ EINA_ITERATOR_FOREACH(l, i)
+ {
+ sum += *i;
+ }
+
+ return sum;
+}
+
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)
diff --git a/src/tests/eolian_cxx/fnptr.eo b/src/tests/eolian_cxx/fnptr.eo
index e20d45fb5f..fd30845226 100644
--- a/src/tests/eolian_cxx/fnptr.eo
+++ b/src/tests/eolian_cxx/fnptr.eo
@@ -90,6 +90,14 @@ class Fnptr extends Efl.Object {
}
return: int; [[The sum of both results.]]
}
+ sum_lent {
+ [[Adds up the ints of $l without freeing it. Returns -1 when $l is
+ $null.]]
+ params {
+ @in l: iterator<int>; [[The ints, from where the iterator is.]]
+ }
+ return: int; [[The sum.]]
+ }
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
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.