Apr 022010

I’ve been working on sample code for js-ctypes lately. My demo isn’t done yet, but I thought I’d talk about the experience a little. As you’ve probably seen, js-ctypes is a new JavaScript code module that makes it possible to describe a C API and access it from JavaScript.

This opens up a lot of possibilities for extension developers, by making it vastly easier to interface with native libraries. This includes support for directly talking to system APIs.

The demo I’m working on is an extension for Mac users that adds an item to the context menu that appears when you right-click on images, “Add image to iPhoto.”

I’m using Core Foundation routines and the Launch Services API, which is part of the Application Services framework to implement this, by taking the URL from the img element’s src attribute, grabbing that, and using Launch Services to open it in iPhoto.

The trick at the moment is that I need to pass a reference to a standard, built-in structure that’s part of the Core Foundation framework to a Core Foundation routine, and at the moment, js-ctypes doesn’t support accessing these external structures. But dwitte has a patch for that, and we’re testing that patch now, so that barrier should be lifted shortly.

With that done, hopefully I’ll be able to wrap this demo up soon, then I can work on updating the existing js-ctypes documentation, which was written to an older version of the API. While the existing docs are mostly still accurate, they only cover a fraction of the current capabilities of js-ctypes.

The past week or so of working on this has been a blast. I really enjoy when my code monkey side and my documentation overlord side collide like this. It’s great fun, and, as in this case, often leads to discoveries that are helpful to the engineers behind the feature being documented.

In this case, we found a viable use case for needing to implement support for accessing external data provided by libraries. We also found a situation in which not being able to catch exceptions thrown by the external libraries can be a problem (ie it can bring down your browser), so there’s now a bug filed for that.

Also, the errors reported by js-ctypes still need some love, so there’s a bug for that now.

Also, there are cases where it would be very helpful for clarity’s sake — and for implementing things such as the concept of a CFMutableArrayRef being passable to any function that accepts a CFArrayRef — to support const declarations.

This is one of those times where being a technical writer feels especially full of win. My work toward understanding a technology has resulted directly in improving the technology itself. That reeks of awesome, and makes my job all the more worth doing.

 Posted by at 1:30 AM

This site uses Akismet to reduce spam. Learn how your comment data is processed.