Returning a Perl hash with XS (Error: 'void' not in typemap)
Question
I'm trying to add a call to the Perl bindings for Augeas (Config::Augeas). I would like this new call to return a Perl hash. I wrote this:
void
aug_span(aug, path);
Config_Augeas* aug
char* path
PREINIT:
int ret ;
char *filename = NULL;
uint label_start, label_end, value_start, value_end, span_start, span_end;
HV *span_hash;
CODE:
ret = aug_span(aug, path, &filename, &label_start, &label_end, &value_start, &value_end, &span_start, &span_end);
span_hash = newHV();
hv_store(span_hash, "filename", 8, newSVpv(filename, strlen(filename)), 0);
hv_store(span_hash, "label_start", 11, newSViv(label_start), 0);
hv_store(span_hash, "label_end", 9, newSViv(label_end), 0);
hv_store(span_hash, "value_start", 11, newSViv(value_start), 0);
hv_store(span_hash, "value_end", 9, newSViv(value_end), 0);
hv_store(span_hash, "span_start", 10, newSViv(span_start), 0);
hv_store(span_hash, "span_end", 8, newSViv(span_end), 0);
RETVAL = newRV_noinc((SV *)span_hash);
OUTPUT:
RETVAL
But when I build it, I get:
lib/Config/Augeas.xs -> lib/Config/Augeas.c
Use of uninitialized value within %ExtUtils::ParseXS::type_kind in hash element at /usr/share/perl/5.10/ExtUtils/ParseXS.pm line 785, <GEN7> line 170.
Error: 'void' not in typemap in Augeas.xs, line 167
cc -I/usr/lib/perl/5.10/CORE -DXS_VERSION="0.701" -DVERSION="0.701" -fPIC -Wall -Wformat -Werror=format-security -c -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -o lib/Config/Augeas.o lib/Config/Augeas.c
lib/Config/Augeas.xs: In function \u2018XS_Config__AugeasPtr_span\u2019:
lib/Config/Augeas.xs:158: warning: value computed is not used
lib/Config/Augeas.xs:159: warning: value computed is not used
lib/Config/Augeas.xs:160: warning: value computed is not used
lib/Config/Augeas.xs:161: warning: value computed is not used
lib/Config/Augeas.xs:162: warning: value computed is not used
lib/Config/Augeas.xs:163: warning: value computed is not used
lib/Config/Augeas.xs:164: warning: value computed is not used
lib/Config/Augeas.xs:165: error: \u2018RETVAL\u2019 undeclared (first use in this function)
lib/Config/Augeas.xs:165: error: (Each undeclared identifier is reported only once
lib/Config/Augeas.xs:165: error: for each function it appears in.)
error building lib/Config/Augeas.o from 'lib/Config/Augeas.c' at /usr/share/perl/5.10/ExtUtils/CBuilder/Base.pm line 110.
What am I doing wrong?
Solution
Give this a shot:
SV *
aug_span(aug, path);
Config_Augeas* aug
char* path
PREINIT:
int ret ;
char *filename = NULL;
uint label_start, label_end, value_start, value_end, span_start, span_end;
HV *span_hash;
CODE:
ret = aug_span(aug, path, &filename, &label_start, &label_end, &value_start, &value_end, &span_start, &span_end);
span_hash = newHV();
hv_store(span_hash, "filename", 8, newSVpv(filename, strlen(filename)), 0);
hv_store(span_hash, "label_start", 11, newSViv(label_start), 0);
hv_store(span_hash, "label_end", 9, newSViv(label_end), 0);
hv_store(span_hash, "value_start", 11, newSViv(value_start), 0);
hv_store(span_hash, "value_end", 9, newSViv(value_end), 0);
hv_store(span_hash, "span_start", 10, newSViv(span_start), 0);
hv_store(span_hash, "span_end", 8, newSViv(span_end), 0);
RETVAL = sv_2mortal((SV*)newRV_noinc((SV *)span_hash));
OUTPUT:
RETVAL
Changes are: The return type (void => SV*), and I am using sv_2mortal on the returned SV*. Since the stack is not reference counted, one has to mark the SV*'s for destruction at the next "suitable" opportunity.
OTHER TIPS
Your function is of type void
when it should be an SV *
. See this section of perlxs
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow