You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, gotk4 converts types back and forth primarily using 2 ways: either by directly casting the value or resorting to functions like C.CString or C.GoString. For the most part, this works great.
However, in some cases, the GIR files contain weird pointer requirements. For example, a badly annotated function parameter may require gchar** to denote an array without actually using an <array> tag. This confuses C.GoString().
An attempt to fix this was using the convertRef routine, which handled this in a slight convoluted (and very incorrect) way. It requires the conversion routine to specify the pointers it accepts and returns, and the convertRef routine will generate the appropriate reference and dereference code. This theoretically sounds like a good idea, however realistically, there is rarely ever a case where the pointer difference is more than 1. Also, in most cases, parameters like that have some special meaning to them that the code cannot guess.
Because unusual pointer requirements usually have a special meaning, the conversion routine cannot assume anything about it. This includes gchar**. Instead, the conversion routine should preserve the original type **byte, even though it might seem ugly. This gives the caller the control over how things should work, which is good, because humans can parse documentation, and machines cannot (trivially).
For example, if a function expects gchar** and the documentation states that it is a null-terminated array of strings, the caller can do this:
If the type conversion routine can be more conservative like this, then there wouldn't be a need for a convertRef routine. Instead, the conversion routine could simply only handle strings, errors and whatnot only when the conditions match up, which is most of the time.
This approach only applies to primitive types. This means that, for classes (like GtkWidget) or records (like GError), an unusual pointer requirement will force the converter to skip, as the implementation difference has to be handled internally in order to not leak the C type.
The text was updated successfully, but these errors were encountered:
This was not explicitly mentioned above, but in order to handle [][]byte, a generator still has to be made. Just taking the backing array pointer will not work as gchar**, as the slice's underlying value has more than just a pointer. This conversion does not require knowing the length or terminator, however, since it can naively malloc enough memory from the given slice's length at runtime.
Currently, gotk4 converts types back and forth primarily using 2 ways: either by directly casting the value or resorting to functions like
C.CString
orC.GoString
. For the most part, this works great.However, in some cases, the GIR files contain weird pointer requirements. For example, a badly annotated function parameter may require
gchar**
to denote an array without actually using an<array>
tag. This confusesC.GoString()
.An attempt to fix this was using the
convertRef
routine, which handled this in a slight convoluted (and very incorrect) way. It requires the conversion routine to specify the pointers it accepts and returns, and theconvertRef
routine will generate the appropriate reference and dereference code. This theoretically sounds like a good idea, however realistically, there is rarely ever a case where the pointer difference is more than 1. Also, in most cases, parameters like that have some special meaning to them that the code cannot guess.Because unusual pointer requirements usually have a special meaning, the conversion routine cannot assume anything about it. This includes
gchar**
. Instead, the conversion routine should preserve the original type**byte
, even though it might seem ugly. This gives the caller the control over how things should work, which is good, because humans can parse documentation, and machines cannot (trivially).For example, if a function expects
gchar**
and the documentation states that it is a null-terminated array of strings, the caller can do this:If the type conversion routine can be more conservative like this, then there wouldn't be a need for a
convertRef
routine. Instead, the conversion routine could simply only handle strings, errors and whatnot only when the conditions match up, which is most of the time.This approach only applies to primitive types. This means that, for classes (like
GtkWidget
) or records (likeGError
), an unusual pointer requirement will force the converter to skip, as the implementation difference has to be handled internally in order to not leak the C type.The text was updated successfully, but these errors were encountered: