[Question] Are the examples in uing, also possible in ruby? #31
Replies: 1 comment
|
This is a difficult question to answer precisely. My short answer is this: Ruby LibUI can probably do it in principle, but making it fully equivalent to uing would require more investigation. At the C ABI level, uing does not use magic. Ruby LibUI should be able to do the same thing if it allocates the required memory and calls the libui-ng functions in the correct order. The real problem is the design of libui-ng. Its memory management and callback mechanisms do not fit GC-based languages very well. This affects Ruby, Python, and Crystal. Memory management is probably the simpler part. If the required structures are allocated correctly, and if they are kept alive for the required time, it should be possible to make it work. Callbacks are more difficult. In uing, Crystal uses extended handler structures. These structures extend the original handler structs, and the added fields store pointers to boxed closures. C-compatible trampoline functions receive the base handler pointer, cast it back to the extended structure, and call the stored closure. This method is not described in the official Crystal reference. I found it after a long process of trial and error. Ruby LibUI still works today, so I do not want to say that it is broken. However, I am not sure that it handles these cases as correctly as uing does. To answer that properly, we would need to investigate Ruby’s C API, Fiddle, libffi, and the lifetime rules for callbacks and allocated structures. So the practical answer is this: it is probably possible, but making Ruby LibUI fully equivalent to uing would require careful low-level investigation. |
Uh oh!
There was an error while loading. Please reload this page.
Hey there kojix2,
This is mostly just a question, as you have many examples with crystal + uing.
Are these examples also possible in ruby or are there some stoppers, such as
it being too slow or something like that? This is more a conceptual question,
so that I can understand whether there are trade-offs. For instance, it would
be kind of cool if all the examples you have, could become a "standard" for
people using other language bindings to libui-ng, to also implement one day.
Naturally this depends on those folks who maintain these language bindings,
but I kind of think it would be pretty cool if ALL languages with libui-ng
bindings, could also have those examples. (Not saying you should do that,
by the way; I just wonder if other people would be motivated to also want to
do so. Often language bindings tend to die when the main maintainer no
longer has time to maintain this. I saw that happen to many bindings of
libui.)
All reactions