|
From: Alan W. I. <ir...@be...> - 2016-10-23 17:26:58
|
On 2016-10-22 19:43+0200 Ervin Hegedüs wrote:
> Hi,
>
> I'ld like to make a library for Lua from a C code. There is a variable,
> which has int[10] type.
>
> Without typemap, Lua shows that variable as userdata. (In other languages
> also shows any different types in case of referenc of that variable, eg.
> Python shows it as SWIG_object (or something like this - it doesn't
> matter). In case of other languages (Python, Perl, Tcl) I could fixup this
> problem with own typemap.)
>
> I'm trying this way:
>
> %typemap(out) int [ANY] {
> int len,i;
> len = $1_dim0;
> lua_createtable (L, len, 0);
> for(i=0; i<len; i++) {
> lua_pushinteger(L, (lua_Integer)$1[i]);
> }
> lua_settable(L, (-1)*(len+1));
> }
>
> but in Lua script shows only this:
>
> nil
>
> What'em I missing? Could anybody helps in this problem?
Hi Ervin:
I can't help you with any details because the swig-generated PLplot lua
binding is not my code, but PLplot uses the following typemap for input
integer arrays.
//--------------------------------------------------------------------------
// PLINT arrays
//--------------------------------------------------------------------------
// With preceding count
%typemap ( in ) ( PLINT n, const PLINT * Array )
{
$2 = (PLINT *) LUA_get_int_num_array_var( L, $input, &$1 );
if ( !$2 )
SWIG_fail;
Alen = $1;
}
%typemap ( freearg ) ( PLINT n, const PLINT * Array )
{
LUA_FREE_ARRAY( $2 );
}
Note, that PLINT is a macro defining a specific integer type for
PLplot, and we use redacted argument lists (where the array dimension
is not specifically mentioned in the Lua call because that information
is already available via the Lua array). That redaction is why you
see the "n" argument included above. Also, LUA_get_int_num_array_var
and LUA_FREE_ARRAY are much! more complex macros that we #define to
vastly simplify implementing the above typemap and many others that we
use. Most of PLplot array use in argument lists is for input arrays
like above, but some of our typemaps are also for output arrays. So
the above is just a tiny part of the picture but should be enough so
you will know whether you want to look further at our Lua binding to
get some ideas. You can find the above typemap in
bindings/lua/plplotluac.i in the working directory of our git
repository which you can access following the prescription at
<https://sourceforge.net/p/plplot/plplot/ci/master/tree/>.
Just for your information the core PLplot library is written in C; we
generate our Python, Java, Octave, and Lua bindings of that library
using swig; and we confirm with tests that our standard set of ~30
examples written in all those languages produce identical plot results
as our standard set of those same examples written in C. So we are
pretty happy with the completeness and accuracy of our swig-generated
bindings!
Alan
__________________________
Alan W. Irwin
Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).
Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.sf.net); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________
Linux-powered Science
__________________________
|