GSoC Update: New objects, Perl callbacks

July already - where has the time gone? Oh wait, Twitter can tell me.

Types, Functions and something of a roadmap

In the wake of my last blog post, rurban told me Magic was more of a 'last resort', and that everything necessary for making type objects could be done with perl's pack and unpack functions, which precipitated a day or two experimenting and trying to get my head round those. Also fixed up the fundamental Ctypes::_call to return larger types properly on 64bit systems.

After flailing at that for a while I felt like I'd lost my sense of direction somewhat, and came up with a more-or-less ordered list of things still needing investigated or implemented:

  1. Function objects
  2. Library objects
  3. Callbacks
  4. Pointers (check they work)
  5. Type objects
  6. Structs / unions (issues and options for cleverness)
  7. Constants
  8. Special library defaults for Strawberry Perl (request from kthakore / SDL)
  9. Thread safety
  10. Header inspection
  11. Setup scripts (auto-generation of wrapper modules from headers)
As you can see, quite a ways still to go. ::Function took a couple of days initially, and then longer tweaking the interface. It was initially suffering from trying to be too clever (a bit like my automatic conversion of arrays idea). With feedback from rurban I now think it works in a more predictable way.


Libraries

Concurrent with this rurban put in loads of work implementing various library objects and all their autoloading magic, so you can say things like my $result = CDLL->c->toupper({sig=>'iii'})->(ord('y')) without having to predeclare the library or function you're going for. Eventually even the need to specify the function signature (in this example the argument to toupper could be eliminated through runtime inspection of headers. He also fleshed out a bunch of unglamorous but essential helper and internal functions for finding libraries and functions through Ctypes (so users don't have to fuss around with DynaLoader).

Callbacks

For the last five days I've been working on enabling the use of Perl functions as callbacks from C. When it's not making me push my fingers into my eyes and sob quietly, the libffi closures system is actually really cool and interesting (ok, it's not really that difficult, it just tests my personal skill level). In theory you could use it to call out from Perl to any other language :-)

During working on it I've had reason to need to pass pointers back and forth between C and Perl as well, so that might be solved already. Interestingly, I was finding that I wasn't able to interpret pointers created in C and passed to Perl in the same way as 'pointers' created in Perl by pack. A little look into DynaLoader.xs revealed liberal use of the INT2PTR and related macros, so I ended up with a check on the 'type of pointer' being received, based on whether the scalar is SvIOK. Might be a bit kludgey, needs more tests to see how it holds up.

In the next few days I'll be posting about various controversial topics like module frameworks and API philosophy for a cross-language project. Stay tuned!

Leave a comment

About doubi

user-pic Student perl-lover in the UK, finishing my law degree in June.