Skip to content

Instantly share code, notes, and snippets.

Created Jul 22, 2014
What would you like to do?

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment