SWIG (Simplified Wrapper and Interface Generator, http://www.swig.org/) is an interface compiler that connects programs written in C and C++ with scripting languages. This module extends SWIG to produce wrapper code for Nasal(http://www.plausible.org/nasal/) written by Andy Ross.
This module generates extension code for Nasal from interface files.
This file contains Nasal module for SWIG and patch for some files in SWIG. This version is for Nasal 1.0.0 or later.
First of all, you need to download SWIG source code (from http://www.swig.org/) and decompress tarball.
$ tar zxf swig-1.3.29.tar.gz
Next, move to SWIG source directory, and decompress Nasal module tarball.
$ cd swig-1.3.29 $ tar zxf /tmp/swig-nasal-0.2.tar.gz
This tarball contains file named swig-nasal.patch. Applay this patch.
$ patch -p1 < swig-nasal.patch
Run autogen.sh script, configure and make SWIG with Nasal support.
$ ./autogen.sh $ ./configure --prefix=/usr/local $ make
Finally, install the files.
$ make install
SWIG documentation describe the usage of SWIG. Please read this document for the basic usage.
To generate a Nasal extension code, run SWIG with -nasal option:
$ swig -nasal example.i
To generate the extension code for C++ code, add -c++ option:
$ swig -nasal -c++ example.i
Then, SWIG generates a file example_wrap.c (example_wrap.cxx, in the case of C++). In order to use the generated code in Nasal scripts, you need to compile Nasal with example_wrap.c. In the generated code, initialization function is named "naInit_(modulename)". So, sample code using wrapped C/C++ code is fllowing:
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include "nasal.h" naRef naInit_example(naContext c); //Initialization function #define NASTR(s) naStr_fromdata(naNewString(ctx), (s), strlen((s))) int main(int argc, char** argv) { *snip* // Create an interpreter context ctx = naNewContext(); // Parse the code in the buffer. The line of a fatal parse error // is returned via the pointer. code = naParseCode(ctx, NASTR(script), 1, buf, fdat.st_size, &errLine); if(naIsNil(code)) { fprintf(stderr, "Parse error: %s at line %d\n", naGetError(ctx), errLine); exit(1); } free(buf); // Make a hash containing the standard library functions. This // will be the namespace for a new script namespace = naInit_std(ctx); // Add extra libraries as needed. naHash_set(namespace, naInternSymbol(NASTR("math")), naInit_math(ctx)); naHash_set(namespace, naInternSymbol(NASTR("example")), naInit_example(ctx)); // wrapped functions are stored in the hash named "example" ...
Then you can call the C/C++ functions described in example.i (e.g. Foo(int n)) from Nasal code like this:
a = example.Foo(10);
You can also use the generated code in the similar way when you embed Nasal in your application.
This module might have bugs. If you experience a problem, please let us know by sending a message to gaku_n@infoseek.jp.