Re: libseed-list handling gpointer args/returns



On 08/04/2010 03:21 AM, Alan Knowles wrote:
On Wednesday, August 04, 2010 04:56 AM, Jonatan Liljedahl wrote:
Could you explain why simply setting arg->v_pointer = (gpointer) value
would break stuff? And why one should wrap JS values in GValues?

The two use cases I can think of for the gpointer stuff are
a) random data stored in C that can be recovered later by the Javscript
program
b) specific data that is needed by the C api (which has been lazily
wrapped as void*)

for a) when that data is sent back into Javascript, - eg. storing it on
a tree element which can be retrieved later, there a has to be a way
that the data can be recovered and turned back into a javascript object.
for b) I guess the introspection should document it properly rather than
pretending that its' void*

I agree on b). for a) it's just a matter of casting the js value to the gpointer, or do I miss something?

Regarding memory handling, when it's an arg to a function that uses
the pointer synchronously, then I guess nothing needs to be done. Just
pass the pointer.

The problems you run into is, if the data you passed to the pointer
get's freed by the Javascript engine before it get's used somewhere -
this can be a problem where the call is async, or the data get's stored
for later use.

Yes, but any API that takes a raw gpointer to be stored should (and most often does) have a callback for when the pointer isn't needed anymore. So, when we pass the values to the function, just JSValueProtect() them and cast to gpointer. Then in the destroy-callback, unprotect them.

The two cases that I'm especially looking for is g_async_queue_push/pop and g_thread_pool_push.

In the async_queue, there is no destroy-callback.. async_queue_push() would need to protect the value, and pop() should unprotect it. I don't know if this can be solved with gi (something with transfer-ownership?) or if this needs to be wrapped manually...

With the thread_pool, you would also need to handle the casting and unprotecting in the wrapper-function that calls the js function:

void thread_pool_wrapper (gpointer value, gpointer data) {
    SeedValue v = (SeedValue) value;
    (call data->func with v...)
    seed_value_unprotect(data->ctx, v);
}

but this might be a lot easier to do manually instead of using gi, so I'll put it in my dispatch module instead...

In other cases (like callback user_data) one would need to
JSValueProtect() it and unprotect it when it's not longer needed (when
the callback is destroyed). not sure how to solve that, but I guess
this is already done for other arg-types, perhaps depending on gir
info about ownership/scope/etc..?

As your other email made me realize callback user_data is probably used
internally to pass the closure stuff around. and Javascript data is
kludged onto that..

Yes, also callback user_data is never needed on the script-side due to lexical scoping.

/Jonatan

Regards
Alan


/Jonatan

On 07/29/2010 12:52 AM, Alan Knowles wrote:
The whole pointer/ in out, memory handling needs quite some though.

I've not come up with a idea for a solution that solves all, or even
most scenarios without breaking stuff.

It needs some trial and error probably
my guess is that the solution might be only allowing GObjects to be
stored in gpointer data - and wrap JS values up in GValues

I've no idea how badly that might affect memory usage though...

Regards
Alan



--- On 29/Jul/2010, Jonatan Liljedahl wrote:
many functions in GLib take gpointer as args or return values, for
example GLib.async_queue_push(queue, data).

but this doesn't work currently, it gives errors like "ArgumentError
Unable to make argument 2 for function: async_queue_push."

There is a hack in seed_gi_make_argument() that allows strings to be
passed, but this doesn't seem to work either (async_queue_pop() don't
seem to return a pushed string, but undefined)

it should be possible and perhaps even simple to just wrap seed
objects/values in these cases.

what function does the gvalue-to-seed conversion of the return value?

/Jonatan
_______________________________________________
libseed-list mailing list
libseed-list gnome org
http://mail.gnome.org/mailman/listinfo/libseed-list

_______________________________________________
libseed-list mailing list
libseed-list gnome org
http://mail.gnome.org/mailman/listinfo/libseed-list

_______________________________________________
libseed-list mailing list
libseed-list gnome org
http://mail.gnome.org/mailman/listinfo/libseed-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]