| 1 | package XS::Tutorial::Two; | ||
| 2 | 1 | require XSLoader; | |
| 3 | |||
| 4 | 1 | XSLoader::load(); | |
| 5 | 1 | 1; | |
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | XS::Tutorial::Two - working with more than one value at a time | ||
| 10 | |||
| 11 | =head2 Introduction | ||
| 12 | |||
| 13 | In L<XS::Tutorial::One>, we learned the basic components of XS, and integrated | ||
| 14 | two C functions into Perl which were slower than their Perl builtin equivalents. | ||
| 15 | Oh well, we'll strive to do better in future tutorials. This chapter is going to | ||
| 16 | show you how to define xsubs that accept multiple parameters. | ||
| 17 | |||
| 18 | I realize that sounds I<incredibly> dull, but I promise along the way you'll | ||
| 19 | pickup some invaluable XS skills that can be used to create your own super fast | ||
| 20 | programs. | ||
| 21 | |||
| 22 | =head2 Module Code | ||
| 23 | |||
| 24 | As before, we'll define the module code to load our XS. This is all that's | ||
| 25 | required: | ||
| 26 | |||
| 27 | package XS::Tutorial::Two; | ||
| 28 | require XSLoader; | ||
| 29 | |||
| 30 | XSLoader::load(); | ||
| 31 | 1; | ||
| 32 | |||
| 33 | |||
| 34 | That should be saved as C<lib/XS/Tutorial/Two.pm>. | ||
| 35 | |||
| 36 | =head2 XS Code | ||
| 37 | |||
| 38 | The top of the XS file will look similar to the previous chapter: | ||
| 39 | |||
| 40 | #define PERL_NO_GET_CONTEXT // we'll define thread context if necessary (faster) | ||
| 41 | #include "EXTERN.h" // globals/constant import locations | ||
| 42 | #include "perl.h" // Perl symbols, structures and constants definition | ||
| 43 | #include "XSUB.h" // xsubpp functions and macros | ||
| 44 | #include "stdint.h" // portable integer types | ||
| 45 | |||
| 46 | MODULE = XS::Tutorial::Two PACKAGE = XS::Tutorial::Two | ||
| 47 | PROTOTYPES: ENABLE | ||
| 48 | |||
| 49 | Remember to append any XS code after the C<PROTOTYPES> line. This should be saved | ||
| 50 | as C<lib/XS/Tutorial/Two.xs>. | ||
| 51 | |||
| 52 | =head2 Adding numbers | ||
| 53 | |||
| 54 | Here's a simple declaration of an xsub that adds two integers: | ||
| 55 | |||
| 56 | int | ||
| 57 | add_ints (addend1, addend2) | ||
| 58 | int addend1 | ||
| 59 | int addend2 | ||
| 60 | CODE: | ||
| 61 | RETVAL = addend1 + addend2; | ||
| 62 | OUTPUT: | ||
| 63 | RETVAL | ||
| 64 | |||
| 65 | This declares an xsub called C<add_ints> which accepts two integers and whose | ||
| 66 | return type is C<int>. Note the L<K&R|https://stackoverflow.com/questions/1630631/alternative-kr-c-syntax-for-function-declaration-versus-prototypes> style of the function definition. This can also be written as: | ||
| 67 | |||
| 68 | add_ints (int addend1, int addend2) | ||
| 69 | |||
| 70 | But you rarely see it done that way in the wild. I don't know if that's a cargo | ||
| 71 | cult thing or there are edge cases to the xsub compiler that I'm not aware of. | ||
| 72 | Just to be safe, I'll keep doing it the way everyone else does (the cult | ||
| 73 | persists!). | ||
| 74 | |||
| 75 | Whereas before we were essentially mapping C functions like C<srand> to Perl, | ||
| 76 | here we're declaring our own logic: C<add_ints> isn't imported from anywhere, | ||
| 77 | we're declaring it as a new function. | ||
| 78 | |||
| 79 | Since C<add_ints> is a new function, we need to define the logic of it, and | ||
| 80 | that's where the C<CODE> section comes in. Here we can write C code which | ||
| 81 | forms the body of the function. In this example, I add the two subroutine | ||
| 82 | parameters together and assign the result to C<RETVAL>. | ||
| 83 | |||
| 84 | L<RETVAL|https://perldoc.perl.org/perlxs.html#The-RETVAL-Variable> ("RETurn VALue") is a special variable that is declared by the xsub processor | ||
| 85 | (xsubpp). The C<OUTPUT> section accepts the return variable for the xsub, placing | ||
| 86 | it on the stack, so that calling code will receive it. | ||
| 87 | |||
| 88 | =head2 Adding more than two numbers | ||
| 89 | |||
| 90 | Adding two numbers is all well and good, but lists are the lingua franca of | ||
| 91 | Perl. Let's update the C<add_ints> xsub to accept n values: | ||
| 92 | |||
| 93 | int32_t | ||
| 94 | add_ints (...) | ||
| 95 | CODE: | ||
| 96 | uint32_t i; | ||
| 97 | for (i = 0; i < items; i++) { | ||
| 98 | if (!SvOK(ST(i)) || !SvIOK(ST(i))) | ||
| 99 | croak("requires a list of integers"); | ||
| 100 | |||
| 101 | RETVAL += SvIVX(ST(i)); | ||
| 102 | } | ||
| 103 | OUTPUT: | ||
| 104 | RETVAL | ||
| 105 | |||
| 106 | First off, notice I've updated the return value. One issue with using C<int> in | ||
| 107 | C is it may be a different size on different machine architectures. C<int32_t> | ||
| 108 | is from the C<stdint.h> library, and guaranteed to be a 32 bit signed integer. | ||
| 109 | |||
| 110 | I've replaced the function parameters with C<...> which indicates the function | ||
| 111 | accepts a variable number of arguments, just like in C. In the C<CODE> section, | ||
| 112 | I declare a C<uint32_t> integer called C<i> (C<uint32_t> is a 32 bit unsigned | ||
| 113 | integer). | ||
| 114 | |||
| 115 | The C<for> loop uses the special variable C<items> (the number of arguments passed | ||
| 116 | to the function) to iterate over the arguments. The C<if> statement calls | ||
| 117 | the macro C<ST> to access the stack variable at position C<i>. This is used to | ||
| 118 | check that the scalar is defined (C<SvOK>) and that it is an integer (C<SvIOK>). | ||
| 119 | If either test fails, the code calls C<croak> to throw a fatal exception. | ||
| 120 | |||
| 121 | Otherwise the integer value is extracted from the scalar (C<SvIVX>) and added | ||
| 122 | to C<RETVAL>. If all of these C macros look strange to you, don't worry, they are | ||
| 123 | weird! They are part of the Perl C API, and they're documented in L<perlapi|https://perldoc.perl.org/perlapi.html>. | ||
| 124 | |||
| 125 | =head2 Edge cases | ||
| 126 | |||
| 127 | It's probably a good time to write some tests for this function, here's a | ||
| 128 | start: | ||
| 129 | |||
| 130 | use Test::More; | ||
| 131 | |||
| 132 | BEGIN { use_ok 'XS::Tutorial::Two' } | ||
| 133 | |||
| 134 | cmp_ok XS::Tutorial::Two::add_ints(7,3), '==', 10; | ||
| 135 | cmp_ok XS::Tutorial::Two::add_ints(1500, 21000, -1000), '==', 21500; | ||
| 136 | |||
| 137 | done_testing; | ||
| 138 | |||
| 139 | I saved that file as C<t/two.t>, and run it by building the distribution with | ||
| 140 | C<make>: | ||
| 141 | |||
| 142 | perl Makefile.PL && make && make test | ||
| 143 | |||
| 144 | Do you know what the return value would be if C<add_ints> was called with no | ||
| 145 | arguments? Maybe C<undef>, since if there are no arguments, the for loop will | ||
| 146 | not have any iterations. Here's a test for that condition: | ||
| 147 | |||
| 148 | ok !defined XS::Tutorial::Two::add_ints(), 'empty list returns undef'; | ||
| 149 | |||
| 150 | Re-building and running the tests with: | ||
| 151 | |||
| 152 | make clean && perl Makefile.PL && make && make test | ||
| 153 | |||
| 154 | That test fails, because the return value is zero! This is a quirk of C: | ||
| 155 | uninitialized integers can be zero. Let's fix the xsub to return C<undef> when | ||
| 156 | it doesn't receive any arguments: | ||
| 157 | |||
| 158 | SV * | ||
| 159 | add_ints (...) | ||
| 160 | PPCODE: | ||
| 161 | uint32_t i; | ||
| 162 | int32_t total = 0; | ||
| 163 | if (items > 0) { | ||
| 164 | for (i = 0; i < items; i++) { | ||
| 165 | if (!SvOK(ST(i)) || !SvIOK(ST(i))) | ||
| 166 | croak("requires a list of integers"); | ||
| 167 | |||
| 168 | total += SvIVX(ST(i)); | ||
| 169 | } | ||
| 170 | PUSHs(sv_2mortal(newSViv(total))); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | PUSHs(sv_newmortal()); | ||
| 174 | } | ||
| 175 | |||
| 176 | Woah, quite a few changes! First I've changed the return type to C<SV *>, from | ||
| 177 | C<int32_t>. The reason for this will become clear in a moment. The C<CODE> section | ||
| 178 | is now called C<PPCODE>, which tells xsubpp that we will be managing the return | ||
| 179 | value of xsub ourselves, hence the C<OUTPUT> section is gone. | ||
| 180 | |||
| 181 | I've declared a new variable called C<total> to capture the running total of the | ||
| 182 | arguments as they're added. If we received at least one argument, total is copied | ||
| 183 | into a new scalar integer value (C<newSViv>), it's reference count is corrected | ||
| 184 | (C<sv_2mortal>) and it is pushed onto the stack pointer (C<PUSHs>). | ||
| 185 | |||
| 186 | Otherwise a new C<undef> scalar is declared with C<sv_newmortal> and that is pushed | ||
| 187 | onto the stack pointer instead. So in both cases we're returning an C<SV>. And as | ||
| 188 | we're returning a Perl type instead of a C type (C<int32_t>) there is no need for | ||
| 189 | xsubpp to cast our return value into a Perl scalar, we're already doing it. | ||
| 190 | |||
| 191 | =head2 References | ||
| 192 | |||
| 193 | =over 4 | ||
| 194 | |||
| 195 | =item * L<XS::Tutorial::One> contains the background information necessary to understand this tutorial | ||
| 196 | |||
| 197 | =item * L<perlxs|http://perldoc.perl.org/perlxs.html> defines the keywords recognized by L<xsubpp|https://metacpan.org/pod/distribution/ExtUtils-ParseXS/lib/ExtUtils/xsubpp> | ||
| 198 | =item * L<perlapi|http://perldoc.perl.org/perlapi.html> lists the C macros used to interact with Perl data structures (and the interpreter) | ||
| 199 | |||
| 200 | =item * The L<stdint.h|http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdint.h.html> C library provides sets of portable integer types | ||
| 201 | |||
| 202 | =back | ||
| 203 | |||
| 204 | =cut |