David M. Beazley
Dept. of Computer Science
University of Chicago
Chicago, IL 60637
David Fletcher
Fusion MicroMedia, Corp.
Longmont, CO 80501
Dominique Dumont
Hewlett Packard
Lab TID
5 Ave Raymond Chanas
38053 Grenoble cedex
France
[ PDF ]
The integration of Perl and code written in compiled languages has a number of practical benefits. First, it allows existing C/C++ applications to be incorporated into a high-level interpreted environment. This environment provides greater flexibility and often simplifies development since debugging and testing can be performed using Perl scripts. Second, Perl can serve as a powerful user interface. In other words, rather than writing a user interface from scratch, it is possible to use a Perl interpreter instead. This also allows other for other possibilities such as graphical user interface development with Perl/Tk. Finally, Perl provides developers with a mechanism for assembling and controlling software components. Rather than creating a huge monolithic package, C/C++ programs can be packaged as collections of Perl extension modules. As a result, programs become more modular and easier to maintain. Furthermore, it is even possible to combine entirely different programs together within a shared Perl interpreter.
This paper provides an introduction and overview of SWIG, a tool designed to integrate C code with a variety of scripting languages including Perl, Python, and Tcl. Currently, SWIG can construct Perl extension modules on Unix and Windows-NT systems. It also supports the ActiveState Perl for Windows and Perl4. SWIG has been freely available since February, 1996 and has been previously described in Advanced Perl Programming, The Perl Journal, and Dr. Dobb's Journal[1,2,3]. In addition, SWIG is packaged with a 300 page user manual describing its use [4]. The goal of this paper is not to repeat all of this information, but to provide an overview of SWIG, demonstrate the use of some of its more advanced features, and describe some of the ways that it is currently being used. The authors include the developer of SWIG and two of SWIG's foremost Perl experts who have made substantial contributions to SWIG's development.
To wrap this function into a Perl module with xsubpp, you would write the following XS file:int fact(int n);
When processed with xsubpp, the following wrapper file is produced/* file : example.xs */ extern int fact(int n); MODULE = Example PACKAGE = Example int fact(n) int n
To use the module, the wrapper code must be compiled and linked into a shared library that can be dynamically loaded into the Perl interpreter. The easiest way to do this is with the MakeMaker utility by writing a script as follows:#include "EXTERN.h" #include "perl.h" #include "XSUB.h" extern int fact(int n); XS(XS_Example_fact) { dXSARGS; if (items != 1) croak("Usage: Example::fact(n)"); { int n = (int)SvIV(ST(0)); int RETVAL; RETVAL = fact(n); ST(0) = sv_newmortal(); sv_setiv(ST(0), (IV)RETVAL); } XSRETURN(1); } XS(boot_Example) { dXSARGS; char* file = __FILE__; XS_VERSION_BOOTCHECK ; newXS("Example::fact", XS_Example_fact, file); ST(0) = &sv_yes; XSRETURN(1); }
this script is then used to create a Makefile and module as follows:# file : Makefile.PL use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'Example', 'OBJECT' => 'example.o fact.o' );
Finally, in addition to creating the C component of the extension module, it is necessary to write a .pm file that is used to load and initialize the module. For example,unix > perl Makefile.PL unix > make unix > make install
At this point, you should have a working Perl extension module. In principle, building a Perl extension requires an XS specification for every C function that is to be accessed. To simplify the process of creating these specifications, Perl includes h2xs, a tool that converts C header files to XS descriptions. While useful, h2xs is somewhat limited in its ability to handle global variables, structures, classes, and more advanced C/C++ features. As a result, h2xs can be somewhat difficult to use with more complex applications.# file : Example.pm package Example; require Exporter; require DynaLoader; @ISA = qw(Exporter DynaLoader); bootstrap Example; 1;
Since gd is a C library, images are normally created by writing C code such as follows:
By building a Perl interface to gd, our goal is to write similar code in Perl. Thus, the functionality of the gd library must be exposed to the Perl interpreter. To do this, a SWIG interface file can be written as follows:#include "gd.h" int main() { gdImagePtr im; FILE *out; int blk,wht; /* Create an image */ im=gdImageCreate(200,200); /* Allocate some colors */ blk=gdImageColorAllocate(im,0,0,0); wht=gdImageColorAllocate(im,255,255,255); /* Draw a line */ gdImageLine(im,20,50,180,140,wht); /* Output the image */ out=fopen("test.gif","wb"); gdImageGif(im,out); fclose(out); /* Clean up */ gdImageDestroy(im); }
In this file, the ANSI C prototypes for every function that we would like to access from Perl are listed. In addition, a number of SWIG directives (which are always preceded by a ``%'') appear. The %module directive specifies the name of the extension module. The %{, %} block is used to insert literal code into the output wrapper file [footnote : This syntax is derived from lex and yacc]. In this case, we simply include the ``gd.h'' header file. Finally, a few file I/O functions also appear. While not part of gd, these functions are needed to manufacture file handles used by several gd functions.// File : gd.i %module gd %{ #include "gd.h" %} typedef gdImage *gdImagePtr; gdImagePtr gdImageCreate(int sx, int sy); void gdImageDestroy(gdImagePtr im); void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color); int gdImageColorAllocate(gdImagePtr im, int r, int g, int b); void gdImageGif(gdImagePtr im, FILE *out); // File I/O functions (explained shortly) FILE *fopen(char *name, char *mode); void fclose(FILE *);
To run SWIG, the following command is executed:
This produces two files, gd_wrap.c and gd.pm. The first file contains C wrapper functions that appear similar to the output that would have been generated by xsubpp. The .pm file contains supporting Perl code needed to load and use the module.unix > swig -perl5 gd.i Generating wrappers for Perl 5
To build the module, the wrapper file is compiled and linked into a shared library. This process varies on every machine (consult the man pages), but the following steps are performed on Linux:
At this point, the module is ready to use. For example, the earlier C program can be directly translated into the following Perl script:unix > gcc -fpic -c gd_wrap.c \ -Dbool=char \ -I/usr/lib/perl5/i586-linux/5.004/CORE unix > gcc -shared gd_wrap.o -lgd -o gd.so
#!/usr/bin/perl use gd; # Create an image $im = gd::gdImageCreate(200,200); # Allocate some colors $blk=gd::gdImageColorAllocate($im,0,0,0); $wht=gd::gdImageColorAllocate($im,255, 255,255); # Draw a line gd::gdImageLine($im,20,50,180,140,$wht); # Output the image $out=gd::fopen("test.gif","wb"); gd::gdImageGif($im,$out); gd::fclose($out); # Clean up gd::gdImageDestroy($im);
The %include directive tells SWIG to include a file and parse all of the declarations it contains. In this case, the interface would now wrap every function in the gd library as opposed to the half-dozen functions listed in the first example.%module %{ #include "gd.h" %} // Grab the declarations from gd.h %include "gd.h" // Some file I/O functions FILE *fopen(char *name, char *mode); void fclose(FILE *);
SWIG also includes a C preprocessor that can be used for macro expansion and conditional compilation. If a new application is being written with SWIG in mind, header files can be written as follows:
With this approach, the file can serve as both a valid C header file and as an interface specification. The SWIG symbol is only defined when SWIG is parsing so special directives can be easily hidden from the C compiler as needed.#ifdef SWIG %module gd %{ #include "gd.h" %} #endif /* C declarations */ ...
Finally, for the truly lazy, SWIG can sometimes be run directly on C header and source files. For example,
Most users, however, use a mix of dedicated interface files and header files.% swig -perl5 -module gd gd.h % swig -perl5 -module example example.c
SWIG uses the type-signature to perform run-time checking of all pointer values. These checks emulate many of the checks that would have been performed by a C compiler. When an invalid Perl datatype or pointer of invalid type is used, a run-time error is generated. For example,gdImagePtr=SCALAR(0x80b9914)
Type-checking is based on the name of each datatype. However, the type-checker also keeps track of C++ inheritance hierarchies and typedef definitions. Thus, an acceptable pointer type includes any alternate names that might have been created with a typedef declaration as well as any derived datatypes in C++.% perl use gd; $f = gd::fopen("test.gif","w"); gd::gdImageLine($f,20,50,180,140,0); Type error in argument 1 of gdImageLine. Expected gdImagePtr. at - line 3.
When pointers are manipulated in Perl, they are opaque values. That is, pointers can be created and passed around to other C functions, but they can not be dereferenced directly. Thus, in the example, it is difficult (or impractical) for a user to directly manipulate the internal representation of an image from the Perl interpreter. Furthermore, SWIG, by default, handles all pointers in a uniform manner. Thus, datatypes such as FILE * are represented as blessed references even though such types may appear remarkably similar to other Perl datatypes such as file handles.
would accept any object of type double *. It is up to the user to ensure that the pointer is valid and that it points to memory that has been properly allocated.void foo(double a[4][4]);
are transformed by SWIG into the following wrappers [footnote: When C++ is used, SWIG uses the default copy constructor instead of malloc]:double dot_product(Vector a, Vector b); Vector cross_product(Vector a, Vector b);
The representation of objects by reference avoids the problem of marshaling objects between a C and Perl representation--a process that would be extremely difficult for very complicated C datatypes. It also provides better performance since manipulating references is more efficient than copying object data back and forth between languages. Finally, the use of references closely matches the way in which most C/C++ programs already handle objects.double wrap_dot_product(Vector *a, Vector *b) { return dot_product(*a,*b); } Vector * wrap_cross_product(Vector *a, Vector *b) { Vector *r; r = (Vector *) malloc(sizeof(Vector)); *r = cross_product(*a,*b); return r; }
The downside to this approach is that objects are opaque in Perl. This prevents users from examining their contents directly. In addition, SWIG wrappers occasionally need to perform implicit memory allocations as shown above. It is up the user to free the resources used by such functions (or learn to live with a memory leak). Of course, this naturally brings us to the next topic.
From a user standpoint, SWIG generated C/C++ extensions follow the same memory management rules as the underlying application. Thus, if a program relies on malloc and free to allocate and deallocate objects, these will also be used from the Perl interpreter. Likewise, a C++ extension typically requires explicit invocation of constructors and destructors. Furthermore, for functions that implicitly allocate memory as in the previous section, it is up to the user to explicitly destroy the result using free or a C++ destructor. While such a scheme may seem problematic, it is no less problematic than memory management in C (which may or may not be a good thing depending on your point of view). Even if it were possible to have Perl automatically manage C/C++ objects, this would be an inherently dangerous affair--especially since Perl has no way to know how an underlying C application really operates. Furthermore, it would be a fatal error for Perl to deallocate objects that were still in use. Therefore, SWIG leaves memory management largely up the user.
The difference between Perl and C datatypes often arises with C functions such as the following:
Ideally, a user might want to pass Perl arrays as arguments as follows:/* Plot some points */ void plotpts(gdImagePtr im, int x[], int y[], int npts, int c) { for (int i = 0; i < npts; i++) { gdImageSetPixel(im,x[i],y[i],c); } }
However, this script generates a type error instead of acting as one might expect. While such behavior may seem restrictive or bizarre, SWIG has been deliberately designed to operate in this manner. In fact, there are even benefits to this approach. If Perl arrays were to be used as C arrays, a copy would be made, verified for type-correctness, and deallocated every time an array was passed to a C function. For large arrays, this would introduce a substantial performance overhead. Space requirements are also a concern for some C programs. For example, a numerical application might manipulate arrays with millions of elements. Converting such arrays to and from a Perl representation would clearly introduce substantial memory and performance overhead. In contrast, manipulating pointers to such arrays is easy and efficient.@a = (10,20,30,40); @b = (50,70,60,200); gd::plotpts($im,\@a,\@b,4,1); # Error!
It should also be noted that SWIG provides a variety of customization options that can be used to change its behavior. In fact, one can even make SWIG map Perl arrays into C arrays if desired. Therefore, most restrictions can be eliminated with a little extra work. Some of these customization techniques are described shortly.
When SWIG builds the scripting interface, these functions become part of the extension module and can be used as follows:// Add some helper functions for C arrays %inline %{ int *int_array(int size) { return (int *) malloc(sizeof(int)*size); } void int_destroy(int *a) { free(a); } void int_set(int *a, int i, int val) { a[i] = val; } int int_get(int *a, int i) { return a[i]; } %}
# Convert a Perl array into a C int array sub create_array { $len = scalar(@_); $ia = gd::int_array($len); for ($i = 0; $i < $len; $i++) { val = shift; gd::int_set($ia,$i,$val); } return $ia; } @a = (10,20,30,40); @b = (50,70,60,200); $ia = create_array(@a); # Create C arrays $ib = create_array(@b); gd::plotpts($im,$ia,$ib,4,1); ... gd::int_destroy($ia); gd::int_destroy($ib);
From a Perl script, a user simply passes an object pointer to accessor functions to extract internal information or invoke member functions./* Extract data from an object */ double Point_x_get(Point *p) { return p->x; } /* Invoke a C++ member function */ int Foo_bar(Foo *f) { return f->bar(); }
While it is possible to write accessor functions manually, SWIG automatically creates them when it is given structure and class definitions. For example, in the gd library, the following structure is used to contain image information:
If this structure definition were placed in the SWIG interface file, accessor functions would automatically be created. These could then be used to extract information about images as follows:typedef struct gdImageStruct { unsigned char ** pixels; int sx; int sy; int colorsTotal; ... } gdImage;
Accessor functions are also created for C++ classes and Objective-C interfaces. For example, the class definition#!/usr/bin/perl use gd; $im = gd::gdImageCreate(400,300); # Print out the image width print gd::gdImage_sx_get($im), "\n";
is translated into the following accessor functions:class List { public: List(); ~List(); void insert(Object *); Object *get(int i); int length(); ... };
List *new_List() { return new List; } void delete_List(List *l) { delete l; } void List_insert(List *l, Object *o) { l->insert(o); } ...
This class provides a wrapper around the underlying object and is said to ``shadow'' the original object. Shadow classes allow C and C++ objects to be used from Perl in a natural manner. For example,package List; @ISA = qw( example ); sub new { my $self = shift; my @args = @_; $self = new_List(@args); return undef if (!defined($self)); bless $self, "List"; my %retval; tie %retval, "List", $self; return bless \%retval,"List"; } sub DESTROY { delete_List(@_); } sub insert { return $result = List_insert(@_); } ...
For C structures, access to various attributes are provided through tied hash tables. For the gd library, members of the image data structure could be accessed as follows:$l = new List; $l->insert($o); ... $l->DESTROY();
The other significant aspect of shadow classes is that they allow Perl to perform a limited form of automatic memory management for C/C++ objects. If an object is created from Perl using a shadow class, the DESTROY method of that class automatically invokes the C++ destructor when the object is destroyed. As a result, C/C++ objects wrapped by shadow classes can be managed using the same reference counting scheme utilized by other Perl datatypes.$im = gd::gdImageCreate(400,400); $width = $im->{sx}; $height = $im->{sy}; ...
To make this structure more useful, we can add constructors, destructors, and various methods to it (regardless of whether it is implemented in C or C++). To do this, the SWIG %addmethods directive can be used as follows:typedef struct { int x,y; } gdPoint;
Now, in the Perl interface gdPoint will appear just like a class with constructors, destructors, and methods. For example,/* Add some methods to points */ %addmethods gdPoint { /* Create a point or an array of points */ gdPoint(int npts = 1) { return (gdPoint *) malloc(sizeof(gdPoint)*npts); } /* Destroy a point */ ~gdPoint() { free(self); } /* Array indexing */ gdPoint *get(int i) { return self+i; } /* A debugging function */ void output() { printf("(%d,%d)\n",self->x,self->y); } };
The class extension mechanism is also a powerful way to repackage existing functionality. For example, the gdImage structure and various functions in the gd library could be combined into a Perl class as follows:# Create a point $pt = new gdPoint; $pt->{x} = 20; $pt->{y} = 50; $pt->output(); # Create an array of points $pts = new gdPoint(10); for ($i = 0; $i < 10; $i++) { $p = $pts->get($i); $p->{x} = $i; $p->{y} = 10*$i; } # Pass the points to a function gd::gdImagePolygon($im,$pts,10,1); ...
Users can now write scripts as follows:%addmethods gdImage { gdImage(int w, int h) { return gdImageCreate(w,h); } ~gdImage() { gdImageDestroy(self); } int colorAllocate(int r, int g, int b) { return gdImageColorAllocate(self,r,g,b); } void line(int x1,int y1,int x2,int y2,int c) { gdImageLine(self,x1,y1,x2,y2,c); } ... };
With these simple modifications, our interface is already looking remarkably similar to that used in the GD module on CPAN. However, more improvements will be described shortly.#!/usr/bin/perl use gd; $im = new gdImage(400,400); $black = $im->colorAllocate(0,0,0); $white = $im->colorAllocate(255,255,255); $im->line(20,50,180,140,$white); ...
When read-only mode is used, attempts to modify a value from Perl result in a run-time error.// Create read-only variables %readonly int foo; // Read-only double bar; // Read-only %readwrite // Create read-only class members class List { ... %readonly int length; // Read-only member %readwrite ... }
Another common problem is changing the name of various C declarations. For example, a C function name may conflict with an existing Perl keyword or subroutine. To fix this problem, the %name directive can be used. For example,
creates a new command ``cpack.'' If name conflicts occur repeatedly, the %rename directive can be used to change all future occurrences of a particular identifier as follows:%name(cpack) void pack(Object *);
The renaming operations can also be applied to C/C++ class and structure names as needed. For example,%rename pack cpack;
%name(Image) class gdImage { ... }
When defined, the exception handling code is placed into all of the wrapper functions. In the process, the $function token is replaced by the actual C function call. For the example shown, the exception handler resets the errno variable and calls the C function. If the value of errno is modified to a non-zero value, an error message is extracted from the C library and reported back to Perl.%except(perl5) { errno = 0; $function if (errno) { die(strerror(errno)); } }
While catching errors in the C library has been illustrated, exception handlers can also be written to catch C++ exceptions or to use any special purpose error handling code that might be present in an application.
As is, this function would be difficult to use because the user must write helper functions to manufacture, dereference, and destroy integer pointers. These functions might be used as follows:void imagesize(gdImagePtr im, int *w, int *h) { *w = gdImageSX(im); *h = gdImageSY(im); }
A more elegant solution is to use the SWIG typemap library in the interface file as follows:$wptr = new_integer(); # Create an 'int *' $hptr = new_integer(); imagesize($im, $wptr, $hptr); $w = integer_value($wptr); # Dereference $h = integer_value($hptr); delete_integer($wptr); # Clean up delete_integer($hptr);
Now, in the Perl script, it is possible to do this:%include typemaps.i void imagesize(gdImagePtr im, int *OUTPUT, int *OUTPUT);
In a similar spirit, it is also possible to use Perl references. For example:($w,$h) = imagesize($im);
Now in Perl:%include typemaps.i void imagesize(gdImagePtr im, int *REFERENCE, int *REFERENCE);
To implement this behavior, the file typemaps.i defines a collection of typemap ``rules'' that are attached to specific datatypes such as int *OUTPUT and int *REFERENCE. The creation of these rules is now discussed.# Return values in $w and $h imagesize($im,\$w,\$h);
To illustrate, consider the gd example. In the original interface file, two functions were included to open and close files. These were required because SWIG normally maps all pointers (including files) into blessed references. Since a blessed reference is not the same as a Perl file handle, it is not possible to pass Perl files to functions expecting a FILE *. However, this is easily changed with a typemap as follows:
This declaration tells SWIG that whenever a FILE * appears as a function parameter, it should be converted using the supplied C code. When generating wrappers, the typemap code is inserted into all wrapper functions where a FILE * is involved. In the process the $source and $target tokens are replaced by the names of C local variables corresponding to the Perl and C representations of an object respectively. As a result, this typemap allows Perl files to be used in a natural manner. For example,%typemap(perl5,in) FILE * { $target = IoIFP(sv_2io($source)); }
Certain operations, such as output values, are implemented using a combination of typemaps as follows:open(OUT,">test.gif") || die "error!\n"; # Much better than before gd::gdImageGif($im,*OUT);
In this case, the ``ignore'' typemap tells SWIG that a parameter is going to be ignored and that the Perl interpreter will not be supplying a value. Since the underlying C function still needs a value, the typemap sets the value of the parameter to point to a temporary variable temp. The ``argout'' typemap is used to return a value held in one of the function arguments. In this case, the typemap extends the Perl stack (if needed), and creates a new return value. The argvi variable is a SWIG-specific variable containing the number of values returned to the Perl interpreter (so it is incremented for each return value).%typemap(perl5,ignore) int *OUTPUT(int temp) { $target = &temp; } %typemap(perl5,argout) int *OUTPUT { if (argvi >= items) { EXTEND(sp,1); } $target = sv_newmortal(); sv_setiv($target,(IV) *($source)); argvi++; }
The C code supplied in each typemap is placed in a private scope that is not visible to any other typemaps or other parts of a wrapper function. This allows different typemaps to be used simultaneously--even if they define variables with the same names. This also allows the same typemap to be used more once in the same wrapper function. For example, the previous section used the int *OUTPUT typemap twice in the same function without any adverse side-effects.
To use these typemaps, a user could include the file check.i and use the %apply directive. The %apply directive simply takes existing typemaps and makes them work with new datatypes. For example:// check.i // typemaps for checking argument values %typemap(perl5,check) Number POSITIVE { if ($target <= 0) croak("Expected a positive value"); } %typemap(perl5,check) Pointer *NONNULL { if ($target == NULL) croak("Received a NULL pointer!"); } ...
In this case, the typemaps we defined for checking different values have been applied to a variety of new datatypes. This has been done without having to examine the implementation of those typemaps or having to look at any Perl internals. Currently, SWIG includes a number of libraries that operate in this manner.%include check.i // Force 'double px' to be positive %apply Number Positive { double px }; // Force these pointers to be NON-NULL %apply Pointer NONNULL { FILE *, Vector *, Matrix *, gdImage * }; // Now some functions double log(double px); // 'px' positive double dot_product(Vector *, Vector *); ...
Finally, here is a simple script that uses the module. Aside from a few minor differences, the script is remarkably similar to the first example given in the standard GD module documentation.// gd.i %module gd %{ #include "gd.h" %} // Make FILE * work %typemap(perl5,in) FILE * { $target = IoIFP(sv_2io($source)); } // Grab the gd.h header file %include "gd.h" // Extend the interface a little bit %addmethods gdImage { gdImage(int w, int h) { return gdImageCreate(w,h); } ~gdImage() { gdImageDestroy(self); } ... etc ... }; %addmethods gdPoint { ... etc ... } // Wrap the fonts (readonly variables) %readonly %include "gdfontt.h" %include "gdfonts.h" %include "gdfontmb.h" %include "gdfontl.h" %include "gdfontg.h" %readwrite
use gd; $im = new gdImage(100,100); $white= $im->colorAllocate(255,255,255); $black= $im->colorAllocate(0,0,0); $red= $im->colorAllocate(255,0,0); $blue= $im->colorAllocate(0,0,255); $im->transparentcolor($white); $im->interlaced(1); $im->rectangle(0,0,99,99,$white); $im->arc(50,50,95,75,0,360,$blue); $im->fill(50,50,$red); open(IMG, ">test.gif"); $im->gif(*IMG); close(IMG);
It is also possible to catch such behavior using exception handlers. For example,void foo() { static int called = 0; if (called) return; ... called = 1; }
Another common problem is that of improper memory management. As previously mentioned, SWIG extensions use the same memory management techniques as C. Therefore, careless use may result in memory leaks, dangling pointers, and so forth. A somewhat more obscure memory related problem is caused when a C program overwrites Perl data. This can be caused by a function such as the following:%except(perl5) { static int called = 0; if (called) croak("Already executed!\n"); $function called = 1; } // List all non-reentrant functions void foo(); ... // Clear the exception handler %except(perl5);
This function copies a string into memory pointed to by msg. However, in the wrapper function, the value of msg is really a pointer to data buried deep inside a Perl scalar value. When the function overwrites the value, it corrupts the value of the Perl scalar value and can cause the Perl interpreter to crash with a memory addressing error or obscure run-time error. Again, this sort of problem can usually be fixed with the use of typemaps. For example, it is possible to turn the msg parameter into an output value as follows :void geterror(char *msg) { strcpy(msg,strerror(errno)); }
// Use a temporary array for the result %typemap(perl5,ignore) char *msg (char temp[512]) { $target = temp; } // Copy the output into a new Perl scalar %typemap(perl5,argout) char *msg { if (argvi >= items) { EXTEND(sp,1); } $target = sv_newmortal(); sv_setpv($target,$source); argvi++; }
The core components in Badger are constructed in C++ and are delivered as a set of shared (dynamically loaded) libraries. The libraries are not directly linked into an executable program. Instead, each library comes with an extension language (EL) interface that is generated by SWIG, allowing the library to be used within a Perl program [footnote: For now, Perl is the only supported extension language. Tcl and Java will be supported in the future]. The combination of a powerful EL and well-tuned, application-specific software results in a system that is potent, flexible, and easy to use.
For the most part, SWIG is used in a ``normal'' fashion: a description of the classes contained within a library is presented to SWIG, and it generates an EL interface that allows the code within that library to be accessed from an EL. There are two interesting facets to the use of SWIG within Badger: the use of ``smart references,'' and the use of callbacks from C++ to the EL,
It is possible to design Badger so that the extension language has complete control over the lifetime of all the objects within the system. Unfortunately, this approach results in a system that is too closely tied to the implementation of a particular language, and adding a new extension language to the mix is difficult. An alternate solution that is simple to implement and is portable, is to introduce ``smart references'' (also called proxies) into the design [5, pg. 207]. In effect, a smart reference is an object that has the same set of operations as a ``real'' object, but the smart reference's implementation consists solely of a single pointer to a ``real'' object of the appropriate type.
The extension language interfaces within Badger have been crafted so that the extension language manipulates smart references and that the lifetime of a smart reference is completely under the control of the extension language. Under most circumstances, the extension language performs an operation on the smart reference, and the smart reference then attempts to transfer the operation to the real object. If the real object has been destroyed then the smart reference will have been invalidated (it points to nil). In this case, the operation is aborted and, if possible, an exception is raised in the extension language. Badger contains the necessary machinery to invalidate any smart references that point to an object being destroyed.
Modern C++ compilers, with their support for templates, run-time type identification, and so forth, provide the means to automatically construct smart reference classes. For a variety of reasons, we are not able to always utilize modern compilers. Hence, we have created the implementations of the smart references manually, which is a tedious process. Fortunately, this task can be mostly automated by creating our own code generator as part of SWIG. This is a simple matter, as SWIG is a modular software system.
Badger provides a number of classes derived from Trigger that specialize its behavior for certain extension languages, for C++, or for an object request broker. For example, the Perl5Trigger class is derived from Trigger and it specializes its base class by storing a pointer to a Perl function reference (an SV*), and by providing the machinery needed to invoke that Perl function.
For example, consider the following Perl fragment:
The MyFcn() Perl function is the callback (trigger) function, and it is registered with $Object using the event name called ``Can't find file''. Now, suppose that the $Object->ReadFile() operation fails. Internally, Badger will note the failure, determine the appropriate event name, attempt to find a Trigger object associated with that event, and if found, will ``invoke the Trigger'' by calling the appropriate member function. For the example above, this means that the MyFcn() function will be called with $Object and ``Can't find file'' supplied as arguments. The function may require more information such as the file name (that could not be opened), and it might find this information by ``pulling'' data from the external library using the functions wrapped by SWIG.sub MyFcn { my $EventName = shift; my $Object = shift; # ... rest of function here. } my $Object = BadgerFunction(....); my $Name = "Can't find file"; Badger::RegisterByObject($Name, $Object, \&MyFcn); $Object->ReadFile("A bogus file name");
The RegisterByObject() function is responsible for creating an object of the Perl5Trigger class, and for creating the association between the Perl5Trigger, the event name, and the object receiving the event. There is a bit of typemap trickery involved when intercepting the arguments from Perl:
The final portion of the system left to describe is the implementation of the Perl5Trigger::Invoke() member function, which is responsible for calling the Perl function from the C++ side of the world. The implementation of this, taken nearly verbatim from the Advanced Perl Programming book [1, pg. 353], looks like this:%typemap(perl5,in) SV* pFcn { if (!SvROK($source)) croak("Expected a reference.\n"); $target = SvRV($source); } void RegisterByObject(const char* pcEventName, Ref* pRef, SV* pFcn);
bool Perl5Trigger:: Invoke(const char* pcEventName, void* pObject, const char* pcTypeName) { dSP; ENTER; SAVETMPS; PUSHMARK(sp); SV* pSV = sv_newmortal(); sv_setpv(pSV, (char*) pcEventName); XPUSHs(pSV); pSV = sv_newmortal(); sv_setref_pv(pSV, (char*) pcTypeName, pObject); XPUSHs(pSV); pSV = sv_newmortal(); sv_setpv(pSV, (char*) pcTypeName); XPUSHs(pSV); PUTBACK; int n = perl_call_sv(this->pPerl5Fcn, G_SCALAR); SPAGAIN; if (n == 1) n = POPi; PUTBACK; FREETMPS; LEAVE; return n == 0 ? false : true; }
To solve this problem, SWIG can be used to incorporate libraries into Perl extension modules where test cases can be implemented as Perl scripts. As a result, the compile-execute cycle is no longer a problem and Perl scripts can be used to implement common parts of various test cases.
This section describes the integration of Perl with an API that is part of a HP OpenCall telecom product developed at HP Grenoble. The API provides access to the TCAP and SCCP layers of the SS7 protocol and consists of about 20 function and 60 structure declarations. Furthermore, most function parameters are pointers to deeply nested structures such as follows:
From a Perl users' point of view, the functionality offered by the SWIG generated module must be not be very different from the underlying C API. Otherwise, test writers may be confused by the Perl API and testing will be unnecessarily complicated. Fortunately, SWIG addresses this problem because Perl interfaces are specified using C syntax and the resulting interface closely resembles the original API.typedef enum { ... } tc_address_nature; typedef struct { ... tc_address_nature nature; ... } tc_global_title; typedef struct tc_address_struct { ... tc_global_title gt; ... } tc_address;
As part of the interface building process, header files were to be included directly into interface files. This is easily done using the %include directive, but a number of problematic nested structure declarations had to be fixed. For example,
To make this structure more manageable in SWIG, it can be split into smaller pieces and rewritten as follows:struct tcStat { ... union { ... struct stat_p_abort { int value; tc_p_abort_cause p_abort; } abort; ... } p; } tc_stat;
Such changes have no impact on user code, but they simplify the use of SWIG.typedef struct { int value; tc_p_abort_cause p_abort; } tc_stat_abort; struct TcStat { ... tc_stat_abort abort; ... };
In addition to splitting, a number of structures in the header files were to be hidden from the SWIG compiler. While this could be done using a simple #ifndef SWIG in the code, this could potentially result in a huge customer problem if they also defined a SWIG macro in their compilation process. Therefore, conditional compilation was implemented using some clever C comments that were parsed by vpp (See the Text::Vpp module) during the build of the SWIG interface. For example,
/* HP reserved comment @if not $_hp_reserved_t */ typedef struct { int length; unsigned char datas[MAX_ABORT_LEN]; } tc_u_abort; /* @endif */
Unfortunately, using such functions is somewhat unfriendly from Perl. For example, to set a single value, it would be necessary to write the following:tc_global_title * tc_address_gt_get(tc_address *); tc_address_nature tc_global_title_nature_set( tc_global_title *t, tc_address_nature val);
Fortunately, shadow classes solve this problem by providing object-oriented access to the underlying C structures. As a result, it is possible to rewrite the above Perl code as follows:$param = new_tc_address(); tc_global_title_nature_set( tc_address_gt_get($param), $value);
Needless to say, this approach is much easier for users to grasp.$parm = new tc_address; $param->{gt}{nature} = $value;
Since tc_u_abort is defined by the structure shown earlier, SWIG normally tries to manipulate it through pointers. However, a typemap can be defined to change this behavior. In particular, it was decided that testers should be able to set and get this value using BCD encoded strings such as follows:typedef struct { ... tc_u_abort abort_reason; ... } tc_dialog_portion;
To do this, a typemap for converting BCD Perl strings into an appropriate byte sequence were developed. In addition, the typemap performs a few sanity checks to prevent invalid values.my $dialog = new tc_dialog_portion; $dialog->{abort_reason} = '0f456A'; # Or print "User abort reason is \ $dialog->{abort_reason} \n";
To return the byte buffer back to Perl as a string, a somewhat simpler typemap is used:%typemap (perl5,in) tc_u_abort * ($basetype temp) { int i; STRLEN len; short tmp; char *str; $target = &temp; /* convert scalar to char* */ str = SvPV($source,len); /* check if even # of char */ if ( (len % 2) != 0 ) { croak("Uneven # of char"); } /* set length field */ $target->length=(len/2); if ((len/2)>(sizeof($basetype)-1)) { croak("Too many bytes in value\n"); } for (i=0;i<$target->length;i++) { if (sscanf(str,"%2hx", &tmp) != 1 ) croak("sscanf failed on %s, \ is it hexa ?\n",str); $target->datas[i] = tmp; str+=2; } }
SWIG typemaps were also used to fix a few other functions. For example, some functions required an address parameter encoded as a two-element array. By default, SWIG wraps this parameter as a pointer, but this leaves the Perl writer with the painful tasks of creating and filling a C array with sensible values using the SWIG pointer library or helper functions. Fortunately, with typemaps, it was possible to create and set this parameter using Perl hashes as follows:%typemap (perl5,out) tc_u_abort * { int i; $target=newSVpvf("%x",$source->datas[0]); for (i=1; i< $source->length; i++) { sv_catpvf($target,"%x", $source->datas[i]); } argvi ++; }
The typemap implementing this behavior is as follows:# $address is an ordinary perl hash # $address will be used as an array $address->{pc} = 10; $address->{ssn}= 12; ... SCCP_oamcmd($cnxId, $time, undef, $address, $command, $cmd_parms);
%typemap (perl5,in) SccpOamAddress* { HV* passedHash; SV** valuePP; SccpOamAddress tempAddress; if (!SvOK($source)) { /* we were passed undef */ tempAddress[0] = 0; tempAddress[1] = 0; } else { if (!SvROK($source)) croak("Not a reference\n"); if (SvTYPE(SvRV($source)) != SVt_PVHV) croak("Not a hash reference\n"); passedHash = (HV*) SvRV($source); valuePP=hv_fetch(passedHash,"ssn",3,0); if (*valuePP == NULL) croak("Missing 'ssn' key\n"); tempAddress[1] = SvIV(*valuePP); valuePP=hv_fetch(passedHash,"pc",2,0); if (*valuePP == NULL) croak("Missing 'pc' key\n"); tempAddress[0] = SvIV(*valuePP); } $target = &tempAddress; } /* SccpOamAddress is returned as {'ssn'=>ssn_value, 'pc'=>pc_value} */ %typemap (perl5,out) SccpOamAddress* { HV* passedHash; SV* theSsn; SV* thePc; thePc = newSViv((*$source)[0]); theSsn = newSViv((*$source)[1]); passedHash = newHV(); hv_store(passedHash,"ssn",3,theSsn,0); hv_store(passedHash,"pc",2,thePc,0); $target = newRV_noinc((SV*) passedHash); argvi ++; }
Module | .i files | .h files | .C files | .pm files |
---|---|---|---|---|
TCAP | 434 | 977 | 16098 | 3561 |
SCPP | 364 | 494 | 13060 | 2246 |
A closely related problem is that certain C/C++ programs are not easily scripted. For example, programs that make extensive use of advanced C++ features such as templates, smart pointers, and overloaded operators can be extremely troublesome to incorporate into Perl. This is especially the case for C++ programs that override the standard behavior of pointers and deferencing operations---operations that are used extensively by SWIG generated wrapper code.
In addition, SWIG does not provide quite as much flexibility as xsubpp and other Perl specific extension building tools. In order to be general purpose, SWIG hides many of the internal implementation details of each scripting language. As a result, it can be difficult to accomplish certain tasks. For example, one such situation is the handling of functions where arguments are implicitly related to each other as follows:
Ideally, it might be desirable to pass a single Perl string to such a function and have it expanded into a data and length component. Unfortunately, SWIG has no way to know that the arguments are related to each other in this manner. Furthermore, the current typemap mechanism only applies to single arguments so it can not be used to combine arguments in this manner. XS, on the other hand, is more closely tied to the Perl interpreter and consequently provides more power in the way that arguments can be converted and passed to C functions.void foo(char *str, int len) { // str = string data // len = length of string data ... }
Finally, SWIG is still somewhat immature with respect to its overall integration with Perl. For example, SWIG does not fully support Perl's package and module naming system. In other words, SWIG can create a module ``Foo'', but can't create a module ``Foo::Bar.'' Likewise, SWIG does not currently utilize MakeMaker and other utilities (although users have successfully used SWIG with such tools). In addition, some users have reported occasional problems when SWIG modules are used with the Perl debugger and other tools.
www.perl.com/CPAN/authors/Dave_Beazley.
Additional information is also available on the SWIG homepage at www.swig.org. An active mailing list of several hundred subscribers is also available.
[2] Scott Bolte. SWIG. The Perl Journal, 2(4):26-31, Winter 1997.
[3] D.M. Beazley. SWIG and Automated C/C++ Scripting Extensions. Dr. Dobb's Journal, (282):30-36, Feb 1998.
[4] D.M. Beazley, SWIG Users Manual. Technical Report UUCS-98-012, University of Utah, 1998.
[5] E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Patterns. Addison-Wesley, 1995.