Chapter 9
// push key and value
XPUSHs(sv_2mortal(newSVpvn(key, len)));
XPUSHs(sv_mortalcopy(value));
}
// free hv explicitly
SvREFCNT_dec((SV *)hv);
// return two SVs for each key in the hash
XSRETURN(count * 2);
}
// G_SCALAR or G_VOID context return a reference to the new hash
XPUSHs(sv_2mortal(newRV_noinc((SV *)hv)));
The first change in the XSUB is in the return value. By using PPCODE instead of
CODE, the XSUB is responsible for managing the return stack. As a result, the return
type is set to void so that xsubpp knows not to provide RETVAL:
void
type_data(type)
const gchar * type
After that, aside from using PPCODE instead of CODE, the function begins much like
the XSUB in Listing 9 7. The first new statement is as follows:
// free keys GList
g_list_free(keys);
This line used to live in the CLEANUP block, but PPCODE blocks cannot have CLEANUP
blocks, so the call to g_list_free() is moved into the main code body.
The next section uses a new macro, GIMME_V, to examine the context of the
current call. GIMME_V is the XS author's version of Perl's wantarray built in. It can
return three possible values G_ARRAY,
3
G_SCALAR, and G_VOID. In this case, I test for
list context:
// test context with GIMME_V
if (GIMME_V == G_ARRAY) {
If you're in list context, the code iterates through the hash and pushes key/value
pairs onto the return stack. The hash iteration code should look familiar from the
3. Which probably should be G_LIST since there's no such thing as array context!
230
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved