ahgilak / deno_gi

Deno port of Gnome libraries (such as Gtk).
https://deno.land/x/deno_gi
33 stars 2 forks source link

Box arguments taking into consideration deep pointers #34

Closed vixalien closed 2 months ago

vixalien commented 5 months ago

In GI, some values are nested in pointers, while others are not.

Direct values (no pointers)

For example, given a function like the following:

gdouble get_double() {}

To unbox the value above, you will just need to do dataView.getFloat64().

pointers

This is usually common in outArgs

void get_double(gdouble *out) {}

To unbox the argument above, we need to get the value pointed to, and then finally get the value.

const pointer = cast_u64_ptr(dataView.getBigUint64());
const pointed_to = deref_buf(pointer, 8);
pointed_to.getFloat64();

Doubly pointed to values

void get_double(gdouble **out) {}

While extremely rare, it is possible that values are contained in a pointer to a pointer. To check for these values, we use GITypeInfo.is_pointer. Here is an example of this usage: https://gitlab.gnome.org/GNOME/gobject-introspection/-/blob/main/tests/scanner/regress.c?ref_type=heads#L647

Because out (and inout) arguments are already pointers, if such an argument has is_pointer() set to true, it effectively means it's a pointer to a pointer.

Note that the types of GI_DIRECTION_OUT and GI_DIRECTION_INOUT parameters will only be pointers if the underlying type being transferred is a pointer (i.e. only if the type of the C function’s formal parameter is a pointer to a pointer).

To unbox these values, we need to double get the value, as implemented in this PR with a new value called nPointers.

const pointer = cast_u64_ptr(dataView.getBigUint64());
const pointed_to = deref_buf(pointer_view, 8);

const pointer2 = cast_u64_buf(new ExtendedDataView(pointed_to).getBigUint64());
const pointed_to2 = deref_buf(pointer2, 8);

pointed_to2.getFloat64();

I've also added a GStreamer example, as that should be interesting...

vixalien commented 5 months ago

I've also added various array tests for strings like strings, GTypes, etc...

I think this should be reviewed so that I can start working on the next batch of tests

ahgilak commented 5 months ago

running gstreamer example results in segmentation fault

vixalien commented 5 months ago

you're right. this is due to GStreamer's insistence on passing everything as pointers. working on it

vixalien commented 4 months ago

Hello. I haven't been able to diagnose what's wrong with GStreamer and I would suggest we merge this as-is (probably removing the gstreamer code) as other parts work.

This would allow us to iterate on other parts faster.

vixalien commented 3 months ago

ping

ahgilak commented 3 months ago

so is this ready to merge now?

vixalien commented 3 months ago

Yes, I guess. The GStreamer demo will still be (intentionally) broken though.

ahgilak commented 2 months ago

hello @vixalien

since I don't have enough time to maintain this project alone, and you did a great job with your contributions, I wanted to add you as a collaborator so you could merge and maintain codes. are you okay with that?

vixalien commented 2 months ago

Hello. no worries. Do you have a matrix account or other chat so that we can communicate more easily?

ahgilak commented 2 months ago

I'm not active on matrix. you can reach me via email ah.gilak@outlook.com or telegram ahgilak