Created
November 27, 2012 13:17
-
-
Save Mons/4154184 to your computer and use it in GitHub Desktop.
Creating closure callback from XS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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