kojix2 / LibUI

A portable GUI library for Ruby
MIT License
207 stars 10 forks source link

Question: Why choose `fiddle` rather than `ffi`? #70

Closed ccmywish closed 1 year ago

ccmywish commented 1 year ago

Hi @kojix2

The ffi seems more idiomatic for Ruby, for example:

# ffi
attach_function :puts, [ :string ], :int

# fiddle
floor = Fiddle::Function.new(
          libm['floor'],
          [Fiddle::TYPE_DOUBLE],
          Fiddle::TYPE_DOUBLE
        )

As you can see, in ffi we write just symbols directly to represent C types.

So, I suspect there might be some technical reasons for the use of fiddle rather than ffi. What are them?

kojix2 commented 1 year ago

The reason for using Fiddle instead of Ruby-FFI for this project is not technical. It is because Fiddle is a Ruby standard library and is expected to work on almost all environments where Ruby is installed. I think Ruby-FFI is better than Fiddle in the technical aspects.

LibUI was created with school children in mind. Schools may not allow downloading or compiling of libraries. Ruby-FFI will only work if you can use libffi, compile C language extensions, and have an internet connection. Fiddle exchanging Ruby-FFI software with a friend on a USB stick would be easier. The idea of children programming in Ruby may sound strange today, but such a situation was possible in Japan until about 10 years ago.

In any case, Fiddle was chosen not because of technical reasons or my preference. It is because it works everywhere.

kojix2 commented 1 year ago

Also, I personally prefer Ruby-FFI, but the above comparison is not fair:

# FFI
attach_function :puts, [ :string ], :int

# Fiddle
extern "int puts(const char* s)"
ccmywish commented 1 year ago

Thank you very much for the detailed explanation.

The idea of children programming in Ruby is not strange at all, in fact, it's my wish too.

So my username is cc my wish

I create a code organization: https://gitee.com/RubyKids to solve some basic problems with using Ruby from China.


Thanks for letting me know the Fiddle way: extern "int puts(const char* s)". It's convenient too.

kojix2 commented 1 year ago

I am glad to hear of such attempts.

About 2 years ago, I copied some of my work to Gitee, in case they lost access to GitHub from China and we might not have access to Chinese repositories either, LibUI is one of them. So you can get the LibUI source code from there if you need it. It may be a bit out of date.

https://gitee.com/kojix/LibUI