Duh - now cc'ing the list.

Hi Dave,

It so happens that I had something very similar (cmp instead <=>) right
here.
Even so, I stuffed up the slight modifications that were necessary - and it
took 10 times longer than it should have taken to get it to work.

########################################
use strict;
use warnings;

use Inline C => Config =>
   BUILD_NOISY => 1;

use Inline C => <<'EOC';

void my_c(char * func, int left, int right) {
 dXSARGS;
 int count;
 int ret;

 ENTER;
 SAVETMPS;

 PUSHMARK(SP);
 XPUSHs(sv_2mortal(newSViv(left)));
 XPUSHs(sv_2mortal(newSViv(right)));
 PUTBACK;

 count = call_pv(func, G_SCALAR);

 SPAGAIN;

 if (count != 1)
   croak("Big trouble\n");

 ret = POPi;
 sp = mark;
 XPUSHs(sv_2mortal(newSViv(ret)));

 PUTBACK;
 XSRETURN(1);
}

EOC

print my_c('my_test', 2, 2), "\n";
print my_c('my_test', 1, 2), "\n";
print my_c('my_test', 12, 2), "\n";

sub my_test {
 return $_[0] <=> $_[1];
}
########################################

(You might be able to trim the C code a bit - I haven't checked. All I know
is that it works for me :-)

Cheers,
Rob

-----Original Message----- From: David Oswald
Sent: Wednesday, August 14, 2013 7:24 AM
To: inline
Subject: Implementing a callback with Inline::C

I'm having difficulty finding the right way to do this, and hope that
maybe someone here is familiar enough to regurgitate some code.

use Inline C => 'DATA';

sub my_test {
 return $a <=> $b;
}

print my_c( \&my_test, 1, 2 ), "\n";

__DATA__
__C__

int my_c ( SV* code, int left, int right ) {

 SV* rv;
 // Load $a and $b with 'left' and 'right' (I think)...
 GV *agv, *bgv;

 agv = gv_fetchpv( "a", GV_ADD, SVt_PV );
 bgv = gv_fetchpv( "b", GV_ADD, SVt_PV );
 SAVESPTR(GvSV(agv));
 SAVESPTR(GvSV(bgv));
 dSP;
 // .... I'm stuck ...
 // Now call "code" and get its return value.
 // *rv = ..... (Yes, I know this is a dismal start. ;) )

 PUTBACK;
 return SvIV(*rv); // Return the value.
}


I would sure appreciate some help on this.  I just keep staring at
examples from other modules, at perlguts, perlcall, and so on, and
each time I think I've got it right I end up getting partway into it
and thinking, "Nah, this can't be right either."  lol


--

David Oswald
daosw...@gmail.com

Reply via email to