After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 582228 - Convert arrays of lists/hashes/arrays from javascript to C.
Convert arrays of lists/hashes/arrays from javascript to C.
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on: 584047
Blocks:
 
 
Reported: 2009-05-11 19:29 UTC by C. Scott Ananian
Modified: 2018-01-21 05:16 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Convert-arrays-of-pointers-from-javascript-arrays.patch (2.71 KB, patch)
2009-05-11 19:29 UTC, C. Scott Ananian
none Details | Review
Convert-arrays-of-pointers-from-javascript-arrays.patch (3.47 KB, patch)
2009-05-18 19:45 UTC, C. Scott Ananian
reviewed Details | Review
LEAKY! Convert-arrays-of-pointers-from-javascript-arrays.patch (4.61 KB, patch)
2009-05-27 18:52 UTC, C. Scott Ananian
needs-work Details | Review

Description C. Scott Ananian 2009-05-11 19:29:13 UTC
This code adds to gjs_array_to_array to support nested array types: arrays of lists/hashes/arrays where the elements are of pointer type.  The implementation is straightforward; we just delegate each element via gjs_value_to_g_argument.
Comment 1 C. Scott Ananian 2009-05-11 19:29:46 UTC
Created attachment 134436 [details] [review]
Convert-arrays-of-pointers-from-javascript-arrays.patch
Comment 2 C. Scott Ananian 2009-05-18 19:45:53 UTC
Created attachment 134893 [details] [review]
Convert-arrays-of-pointers-from-javascript-arrays.patch

Update to git HEAD.  In particular, update to match 	4a84c965b59941c8f8ce00309d4451a2dfa89d74
Comment 3 C. Scott Ananian 2009-05-20 15:11:15 UTC
Review, please?
Comment 4 Johan (not receiving bugmail) Dahlin 2009-05-27 17:27:46 UTC
Comment on attachment 134893 [details] [review]
Convert-arrays-of-pointers-from-javascript-arrays.patch

seems that this leaks, the results is never freed right? Have you verified with valgrind.
Comment 5 C. Scott Ananian 2009-05-27 18:51:00 UTC
Hm, you're right.  This is a deep problem: gjs_g_arg_release_internal doesn't necessarily know the length of the array, which makes it very difficult to properly free the elements.

I've attached a slightly updated patch which demonstrates the problem.  I guess I can hide the array length in array[-1], say, and then use it to properly free the array.  Any better ideas?  (GArray support in bug 581687 is much cleaner!)
Comment 6 C. Scott Ananian 2009-05-27 18:52:36 UTC
Created attachment 135460 [details] [review]
LEAKY! Convert-arrays-of-pointers-from-javascript-arrays.patch
Comment 7 Owen Taylor 2009-05-27 19:01:42 UTC
A different general approach would be to keep a "cleanup list" to do after invocation - with cleanups being something like:

struct _Cleanup {
   enum { CLEANUP_G_FREE, CLEANUP_G_OBJECT_UNREF, ... } type;
   gpointer data;
}
Comment 8 C. Scott Ananian 2009-05-27 19:30:00 UTC
A radical version of this suggestion would be to move *all* the release processing into a linked list of clean up functions.  So we wouldn't handle TRANSFER_EVERYTHING / TRANSFER_NOTHING post-invocation, we'd handle it by adding or not adding a clean up handler to the list when we marshal the argument.

If the list is ordered, we could also support TRANSFER_CONTAINER nicely: when we create the contained objects we add them to the clean up list depending on the transfer setting, and then the clean up for the container need only free the container; the elements should have already been freed (or not) by that point.
Comment 9 Owen Taylor 2009-05-27 19:48:19 UTC
Yeah, I was thinking of it as comprehensive. (I thought of this approach earlier when looking at the TRANSFER_CONTAINER case)

Do you actually need to worry about ordering for the container case? It seems that it will work fine if you free the container first or second, as long as you aren't counting on the container to get to the contained elements.
Comment 10 C. Scott Ananian 2009-05-27 20:10:26 UTC
You're right, ordering shouldn't matter unless you mind being given a pointer to a container with already-freed elements.

You'd still need type-driven release functions for TRANSFER_EVERYTHING out-arguments, so maybe this patch wouldn't be all that big: CLEANUP_BY_TYPE (which would invoke gjs_g_arg_release) would probably suffice in most places.

I've opened bug 584047 for this refactoring, and made this bug depend on that one; it should be easy to fix the leak in this patch once we can use a cleanup list.
Comment 11 Owen Taylor 2009-08-24 16:56:30 UTC
Comment on attachment 135460 [details] [review]
LEAKY! Convert-arrays-of-pointers-from-javascript-arrays.patch

Marking needs-work as discussed.
Comment 12 Philip Chimento 2018-01-21 05:16:15 UTC
As far as I could tell when I tried to rebase that patch, this has already been implemented in a different way in the meantime.