Tuesday, September 15, 2009

Hackathon Summary

During the YAPC::Asia::2009 hackathon I refactored a bunch of XS modules out of some other code, both rafl's and mine.

XS::Object::Magic

This module provides an alternative to the standard T_PTROBJ approach to creating C struct based objects in Perl.

The traditional way creates a blessed scalar reference, which contains an integer value that is cast to a pointer of the right type.

This is problematic for two reasons:

  • If the pointer value is modified (accidentally or maliciously) then this could easily corrupt memory or cause segfaults.
  • Scalar references can't be extended with more data without using inside out objects.

This module provides a C api which uses sv_magicext to create safer and more extensible objects associated with some pointer, that interoperates nicely with Moose amongst other things.

Magical::Hooker::Decorate

This module lets you decorate an arbitrary SV with any other arbitrary SV using the magic hook API.

This is similar to using a fieldhash to create inside out decorations, but is designed to be used primarily from XS code, and as such it provides a C api. The memory footprint is also slightly smaller since there is no auxillary storage.

B::Hooks::XSUB::CallAsOp

This module was refactored out of Continuation::Delimited and lets an XS routine trigger code that will be invoked in the context of its caller.

This allows a little more freedom in the stack manipulations you can do (which is of course very important for continuations).

These modules are all possible due to the awesome ExtUtils::Depends module. If you find yourself cargo culting XS consider refactoring it into a reusable module using ExtUtils::Depends instead.

2 comments:

Anonymous said...

I don't work nearly as well in a hackathon setting. I contributed a failing test file to Moose.

Oh and a module name

Yanick said...

Just want to mention that the module name Magical::Hooker::Decorate caused the mother of all double-takes when I first glanced over it. It sounds a wee bit like the battle cry of a naughty version of Sailor Moon, or something of that ilk. :-)