in reply to Re^2: Problem on perl callbacks from XS
in thread Problem on perl callbacks from XS
Here is an example. It's done in Inline but is just straight vanilla XS. The stack pointer behaves itself over the separate calls.
package Serialize; use Data::Dump; sub new { bless {foo=>'bar'}, shift } sub toString { return Data::Dump::dump($_[0]) } my $obj = new Serialize; my $str = "some string"; print serialize($obj), $/; print serialize($str), $/; use Inline Config => FORCE_BUILD => 1, CLEAN_AFTER_BUILD => 0; use Inline C => <<'END_OF_C_CODE'; void serialize(SV* sv_data) { int count; dXSARGS; sp = mark; printf("\nEntry SP is %d\n", sp); if (sv_isobject(sv_data)) { // this is an object so presume a toString method PUSHMARK(sp); XPUSHs(sv_data); // whack it back on the stack PUTBACK; count = perl_call_pv("toString", G_SCALAR); if ( count != 1 ) croak("Expected 1 value got %d\n", count); XPUSHs(sv_2mortal(newSVsv(POPs))); } else { // just return SV with something appended for this example sv_catpv(sv_data, " - serialized\0"); XPUSHs(sv_2mortal(newSVsv(sv_data))); } PUTBACK; printf("Exit SP is %d\n", sp); XSRETURN(1); } END_OF_C_CODE __DATA__ bless({ foo => "bar" }, "Serialize") some string - serialized Entry SP is 32203548 Exit SP is 32203552 Entry SP is 32203548 Exit SP is 32203552
|
|---|