1. 4

    This week in an effort to make the bindings generated by Melange more idiomatic, I hacked on Dylan’s c-ffi library.

    Many C libraries use the function return value to signal an error occurred, but in Dylan this is unnecessary because we have support for error conditions and exception handling.

    Using libgit2 as an example, before my changes we had to either:

    1. Return the error along the possible function return values and let the library user check it
    2. Declare the return values as a C-mapped-subtype, and in the import-function check its value, eventually throwing an exception
    3. Write a wrapper around each function to throw an exception if the function failed

    At first we went with solution #2, the problem is that, by how the C-ffi module is designed, there was no way to call the mapping function and then discard the return value.

    So my change to C-ffi involved defining a new way to declare the return value in a C-function definition, I called it error-result. The PR is not yet merged because I need to update the release notes and then have someone review it. The hard part writing the patch was understanding C-function-definer’s control flow, when I had that clear it was actually straightforward. C-ffi uses Dylan’s procedural-macros that are only available inside the compiler, so I had to understand them too. Compared to Dylan “standard” macros, they are more powerful, like Common Lisp macros.

    Probably this week or the next I’m going to write a blog post explaining how it works in more detail.

    After that, I changed Melange to allow a new map-error-result option to function declarations. After these changes I can get back hacking on libgit2 Dylan bindings!

    1. 3

      This has been some really nice work and something I’m very happy to see happening. I’m even more happy to see someone other than myself being able to hack on the C-FFI and some of the related compiler internals. :)

    1. 5

      This week I tried to get a streak going by working on Open Dylan’s libgit2 bindings.

      The heavy lifting is done by Melange, but because of the differences between C and Dylan, e.g. multiple return values, I have to tell it how to change the generated Dylan functions to be more Dylan-ish.
      Other changes include making the interface easier to use by using named parameters (keyword parameters in Dylan) and sane default values.

      My goal is to have the library in an usable state by the end of the week, then polish it and complete the missing bindings. For this reason I’m working through the 101 Libgit2 Samples, both as to provide basic test coverage and as a way to learn how to use libgit2.

      While working on this I often noticed a crash occurring when returning a struct by value, it turns out it’s a known bug in Open Dylan’s C back-end. When I’m done with the bindings I may tackle this problem because it looks interesting and allows us to move forward on the Obj-C bridge front. And since in October I’ll be moving to another country (from Italy to London, UK) this means I’m going to use my MBP only (I’m not going to take the linux box with me) and I want to play with the Obj-C bridge a bit.