Skip to content

Instantly share code, notes, and snippets.

@Mons
Created November 27, 2012 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mons/4154184 to your computer and use it in GitHub Desktop.
Save Mons/4154184 to your computer and use it in GitHub Desktop.
Creating closure callback from XS
/*
* Usage:
my $x = XSTesting::makecb("my arg for x");
my $y = XSTesting::makecb("my arg for y");
say $x->();
say $y->();
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
XS(cb_test) {
dVAR;
dXSARGS;
dORIGMARK;
SV *var = (SV *) CvXSUBANY(cv).any_ptr;
ST(0) = var;
XSRETURN(1);
}
MODULE = XSTesting PACKAGE = XSTesting
void makecb(SV *var)
PPCODE:
CV *myxs = newXS(0, cb_test, __FILE__);
CvXSUBANY(myxs).any_ptr = (void *) var;
ST(0) = sv_2mortal( newRV_noinc( (SV *) myxs ) );
XSRETURN(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment